|
| 1 | +import * as xdebug from './xdebugConnection' |
| 2 | +import * as semver from 'semver' |
| 3 | +import * as net from 'net' |
| 4 | + |
| 5 | +export class ControlSocket { |
| 6 | + /** |
| 7 | + * @returns Returns true if the current platoform is supported for Xdebug control socket (win and linux) |
| 8 | + */ |
| 9 | + supportedPlatform(): boolean { |
| 10 | + return process.platform === 'linux' || process.platform === 'win32' |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * |
| 15 | + * @param initPacket |
| 16 | + * @returns Returns true if the current platform and xdebug version are supporting Xdebug control socket. |
| 17 | + */ |
| 18 | + supportedInitPacket(initPacket: xdebug.InitPacket): boolean { |
| 19 | + return ( |
| 20 | + this.supportedPlatform() && |
| 21 | + initPacket.engineName === 'Xdebug' && |
| 22 | + semver.valid(initPacket.engineVersion, { loose: true }) !== null && |
| 23 | + (semver.gte(initPacket.engineVersion, '3.4.0', { loose: true }) || initPacket.engineVersion === '3.4.0-dev') |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * |
| 29 | + * @param pid appId returned in the init packet |
| 30 | + * @returns |
| 31 | + */ |
| 32 | + async requestPause(pid: string): Promise<void> { |
| 33 | + let retval |
| 34 | + if (process.platform === 'linux') { |
| 35 | + retval = await this.executeLinux(pid, 'pause') |
| 36 | + } else if (process.platform === 'win32') { |
| 37 | + retval = await this.executeWindows(pid, 'pause') |
| 38 | + } else { |
| 39 | + throw new Error('Invalid platform for Xdebug control socket') |
| 40 | + } |
| 41 | + retval |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + private async executeLinux(pid: string, cmd: string): Promise<string> { |
| 46 | + const abs = await import('abstract-socket') |
| 47 | + return new Promise<string>((resolve, reject) => { |
| 48 | + const cs = `\0xdebug-ctrl.${pid}y`.padEnd(108, 'x') |
| 49 | + try { |
| 50 | + const s = abs.connect(cs, () => { |
| 51 | + s.write(`${cmd}\0`) |
| 52 | + }) |
| 53 | + s.setTimeout(3000) |
| 54 | + s.on('timeout', () => { |
| 55 | + reject(new Error('Timed out while reading from Xdebug control socket')) |
| 56 | + s.end() |
| 57 | + }) |
| 58 | + s.on('data', data => { |
| 59 | + resolve(data.toString()) |
| 60 | + }) |
| 61 | + s.on('error', error => { |
| 62 | + reject( |
| 63 | + new Error( |
| 64 | + `Cannot connect to Xdebug control socket: ${String( |
| 65 | + error instanceof Error ? error.message : error |
| 66 | + )}` |
| 67 | + ) |
| 68 | + ) |
| 69 | + }) |
| 70 | + return |
| 71 | + } catch (error) { |
| 72 | + reject( |
| 73 | + new Error( |
| 74 | + `Cannot connect to Xdebug control socket: ${String( |
| 75 | + error instanceof Error ? error.message : error |
| 76 | + )}` |
| 77 | + ) |
| 78 | + ) |
| 79 | + return |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + private async executeWindows(pid: string, cmd: string): Promise<string> { |
| 85 | + return new Promise<string>((resolve, reject) => { |
| 86 | + const s = net.createConnection(`\\\\.\\pipe\\xdebug-ctrl.${pid}`, () => { |
| 87 | + s.end(`${cmd}\0`) |
| 88 | + }) |
| 89 | + s.setTimeout(3000) |
| 90 | + s.on('timeout', () => { |
| 91 | + reject(new Error('Timed out while reading from Xdebug control socket')) |
| 92 | + s.end() |
| 93 | + }) |
| 94 | + s.on('data', data => { |
| 95 | + resolve(data.toString()) |
| 96 | + }) |
| 97 | + s.on('error', error => { |
| 98 | + // sadly this happens all the time - even on normal server-side-close, but luckily the promise is already resolved |
| 99 | + reject( |
| 100 | + new Error( |
| 101 | + `Cannot connect to Xdebug control socket: ${String( |
| 102 | + error instanceof Error ? error.message : error |
| 103 | + )}` |
| 104 | + ) |
| 105 | + ) |
| 106 | + }) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments