-
-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathcontrolSocket.ts
More file actions
175 lines (158 loc) Β· 5.91 KB
/
Copy pathcontrolSocket.ts
File metadata and controls
175 lines (158 loc) Β· 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as xdebug from './xdebugConnection'
import * as net from 'net'
import * as CP from 'child_process'
import { promisify } from 'util'
import { supportedEngine } from './xdebugUtils'
import { DOMParser } from '@xmldom/xmldom'
import * as fs from 'fs'
import { decode } from 'iconv-lite'
import { ENCODING } from './dbgp'
export class ControlSocket {
/**
* @returns Returns true if the current platoform is supported for Xdebug control socket (win and linux)
*/
supportedPlatform(): boolean {
return process.platform === 'linux' || process.platform === 'win32'
}
/**
*
* @param initPacket
* @returns Returns true if the current platform and xdebug version are supporting Xdebug control socket.
*/
supportedInitPacket(initPacket: xdebug.InitPacket): boolean {
return this.supportedPlatform() && supportedEngine(initPacket, '3.5.0')
}
/**
* Request the pause control socket command
* @param ctrlSocket Control socket full path
* @returns
*/
async requestPause(ctrlSocket: string): Promise<void> {
await this.executeCtrlCmd(ctrlSocket, 'pause')
}
async requestPS(ctrlSocket: string): Promise<ControlPS> {
const xml = await this.executeCtrlCmd(ctrlSocket, 'ps')
const parser = new DOMParser()
const document = <unknown>parser.parseFromString(xml, 'application/xml')
return new ControlPS(<XMLDocument>document)
}
private async executeCtrlCmd(ctrlSocket: string, cmd: string): Promise<string> {
let rawCtrlSocket: string
if (process.platform === 'linux') {
rawCtrlSocket = `\0${ctrlSocket}`
} else if (process.platform === 'win32') {
rawCtrlSocket = `\\\\.\\pipe\\${ctrlSocket}`
} else {
throw new Error('Invalid platform for Xdebug control socket')
}
return new Promise<string>((resolve, reject) => {
const s = net.createConnection(rawCtrlSocket, () => {
s.end(`${cmd}\0`)
})
s.setTimeout(3000)
s.on('timeout', () => {
reject(new Error('Timed out while reading from Xdebug control socket'))
s.end()
})
s.on('data', data => {
s.destroy()
if (data.length > 0 && data.at(data.length - 1) == 0) {
resolve(decode(data.subarray(0, data.length - 1), ENCODING))
} else {
resolve(decode(data, ENCODING))
}
})
s.on('error', error => {
reject(
new Error(
`Cannot connect to Xdebug control socket: ${String(
error instanceof Error ? error.message : error
)}`
)
)
})
return
})
}
async listControlSockets(): Promise<XdebugRunningProcess[]> {
let retval: XdebugRunningProcess[]
if (process.platform === 'linux') {
retval = await this.listControlSocketsLinux()
} else if (process.platform === 'win32') {
retval = await this.listControlSocketsWin()
} else {
throw new Error('Invalid platform for Xdebug control socket')
}
const retval2 = Promise.all(
retval.map(async v => {
try {
v.ps = await this.requestPS(v.ctrlSocket)
} catch {
// ignore
}
return v
})
)
return retval2
}
private async listControlSocketsLinux(): Promise<XdebugRunningProcess[]> {
const re = /@(xdebug-ctrl\.\d+)$/
const data = await fs.promises.readFile('/proc/net/unix')
const lines = data.toString().split('\n')
const sockets: XdebugRunningProcess[] = []
for (const line of lines) {
const matches = line.match(re)
if (matches && matches.length > 0) {
sockets.push({ ctrlSocket: matches[1] })
}
}
return sockets
}
private async listControlSocketsWin(): Promise<XdebugRunningProcess[]> {
const exec = promisify(CP.exec)
try {
const ret = await exec('cmd /C "dir \\\\.\\pipe\\\\xdebug-ctrl* /b"')
const lines = ret.stdout.split('\r\n')
const retval = lines
.filter(v => v.length != 0)
.map<XdebugRunningProcess>(v => <XdebugRunningProcess>{ ctrlSocket: v })
return retval
} catch (err) {
if (err instanceof Error && (<ExecError>err).stderr == 'File Not Found\r\n') {
return []
}
throw err
}
}
}
interface ExecError extends Error {
stderr: string
}
export interface XdebugRunningProcess {
readonly ctrlSocket: string
ps?: ControlPS
// todo
}
export class ControlPS {
/** The file that was requested as a file:// URI */
fileUri: string
/** the version of Xdebug */
engineVersion: string
/** the name of the engine */
engineName: string
/** the internal PID */
pid: string
/** memory consumption */
memory: number
/**
* @param {XMLDocument} document - An XML document to read from
*/
constructor(document: XMLDocument) {
const documentElement = <Element>document.documentElement.firstChild
this.fileUri = documentElement.getElementsByTagName('fileuri').item(0)?.textContent ?? ''
this.engineVersion = documentElement.getElementsByTagName('engine').item(0)?.getAttribute('version') ?? ''
this.engineName = documentElement.getElementsByTagName('engine').item(0)?.textContent ?? ''
this.pid = documentElement.getElementsByTagName('pid').item(0)?.textContent ?? ''
this.memory = parseInt(documentElement.getElementsByTagName('memory').item(0)?.textContent ?? '0')
}
}