Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62b03bc

Browse filesBrowse files
Trotttargos
authored andcommitted
debugger: move node-inspect to internal library
node-inspect developers have agreed to move node-inspect into core rather than vendor it as a dependency. Refs: https://github.com/nodejs/node/discussions/36481 PR-URL: #38161 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent a9314cd commit 62b03bc
Copy full SHA for 62b03bc

File tree

Expand file treeCollapse file tree

5 files changed

+1840
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+1840
-4
lines changed
Open diff view settings
Collapse file

‎lib/internal/inspector/_inspect.js‎

Copy file name to clipboard
+369Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
/*
2+
* Copyright Node.js contributors. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
// TODO(trott): enable ESLint
24+
/* eslint-disable */
25+
26+
'use strict';
27+
const { spawn } = require('child_process');
28+
const { EventEmitter } = require('events');
29+
const net = require('net');
30+
const util = require('util');
31+
32+
const runAsStandalone = typeof __dirname !== 'undefined';
33+
34+
const { 0: InspectClient, 1: createRepl } =
35+
runAsStandalone ?
36+
// This copy of node-inspect is on-disk, relative paths make sense.
37+
[
38+
require('./inspect_client'),
39+
require('./inspect_repl'),
40+
] :
41+
// This copy of node-inspect is built into the node executable.
42+
[
43+
require('internal/inspector/inspect_client'),
44+
require('internal/inspector/inspect_repl'),
45+
];
46+
47+
const debuglog = util.debuglog('inspect');
48+
49+
class StartupError extends Error {
50+
constructor(message) {
51+
super(message);
52+
this.name = 'StartupError';
53+
}
54+
}
55+
56+
function portIsFree(host, port, timeout = 9999) {
57+
if (port === 0) return Promise.resolve(); // Binding to a random port.
58+
59+
const retryDelay = 150;
60+
let didTimeOut = false;
61+
62+
return new Promise((resolve, reject) => {
63+
setTimeout(() => {
64+
didTimeOut = true;
65+
reject(new StartupError(
66+
`Timeout (${timeout}) waiting for ${host}:${port} to be free`));
67+
}, timeout);
68+
69+
function pingPort() {
70+
if (didTimeOut) return;
71+
72+
const socket = net.connect(port, host);
73+
let didRetry = false;
74+
function retry() {
75+
if (!didRetry && !didTimeOut) {
76+
didRetry = true;
77+
setTimeout(pingPort, retryDelay);
78+
}
79+
}
80+
81+
socket.on('error', (error) => {
82+
if (error.code === 'ECONNREFUSED') {
83+
resolve();
84+
} else {
85+
retry();
86+
}
87+
});
88+
socket.on('connect', () => {
89+
socket.destroy();
90+
retry();
91+
});
92+
}
93+
pingPort();
94+
});
95+
}
96+
97+
function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) {
98+
return portIsFree(inspectHost, inspectPort)
99+
.then(() => {
100+
return new Promise((resolve) => {
101+
const needDebugBrk = process.version.match(/^v(6|7)\./);
102+
const args = (needDebugBrk ?
103+
['--inspect', `--debug-brk=${inspectPort}`] :
104+
[`--inspect-brk=${inspectPort}`])
105+
.concat([script], scriptArgs);
106+
const child = spawn(process.execPath, args);
107+
child.stdout.setEncoding('utf8');
108+
child.stderr.setEncoding('utf8');
109+
child.stdout.on('data', childPrint);
110+
child.stderr.on('data', childPrint);
111+
112+
let output = '';
113+
function waitForListenHint(text) {
114+
output += text;
115+
if (/Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\//.test(output)) {
116+
const host = RegExp.$1;
117+
const port = Number.parseInt(RegExp.$2);
118+
child.stderr.removeListener('data', waitForListenHint);
119+
resolve([child, port, host]);
120+
}
121+
}
122+
123+
child.stderr.on('data', waitForListenHint);
124+
});
125+
});
126+
}
127+
128+
function createAgentProxy(domain, client) {
129+
const agent = new EventEmitter();
130+
agent.then = (...args) => {
131+
// TODO: potentially fetch the protocol and pretty-print it here.
132+
const descriptor = {
133+
[util.inspect.custom](depth, { stylize }) {
134+
return stylize(`[Agent ${domain}]`, 'special');
135+
},
136+
};
137+
return Promise.resolve(descriptor).then(...args);
138+
};
139+
140+
return new Proxy(agent, {
141+
get(target, name) {
142+
if (name in target) return target[name];
143+
return function callVirtualMethod(params) {
144+
return client.callMethod(`${domain}.${name}`, params);
145+
};
146+
},
147+
});
148+
}
149+
150+
class NodeInspector {
151+
constructor(options, stdin, stdout) {
152+
this.options = options;
153+
this.stdin = stdin;
154+
this.stdout = stdout;
155+
156+
this.paused = true;
157+
this.child = null;
158+
159+
if (options.script) {
160+
this._runScript = runScript.bind(null,
161+
options.script,
162+
options.scriptArgs,
163+
options.host,
164+
options.port,
165+
this.childPrint.bind(this));
166+
} else {
167+
this._runScript =
168+
() => Promise.resolve([null, options.port, options.host]);
169+
}
170+
171+
this.client = new InspectClient();
172+
173+
this.domainNames = ['Debugger', 'HeapProfiler', 'Profiler', 'Runtime'];
174+
this.domainNames.forEach((domain) => {
175+
this[domain] = createAgentProxy(domain, this.client);
176+
});
177+
this.handleDebugEvent = (fullName, params) => {
178+
const { 0: domain, 1: name } = fullName.split('.');
179+
if (domain in this) {
180+
this[domain].emit(name, params);
181+
}
182+
};
183+
this.client.on('debugEvent', this.handleDebugEvent);
184+
const startRepl = createRepl(this);
185+
186+
// Handle all possible exits
187+
process.on('exit', () => this.killChild());
188+
process.once('SIGTERM', process.exit.bind(process, 0));
189+
process.once('SIGHUP', process.exit.bind(process, 0));
190+
191+
this.run()
192+
.then(() => startRepl())
193+
.then((repl) => {
194+
this.repl = repl;
195+
this.repl.on('exit', () => {
196+
process.exit(0);
197+
});
198+
this.paused = false;
199+
})
200+
.then(null, (error) => process.nextTick(() => { throw error; }));
201+
}
202+
203+
suspendReplWhile(fn) {
204+
if (this.repl) {
205+
this.repl.pause();
206+
}
207+
this.stdin.pause();
208+
this.paused = true;
209+
return new Promise((resolve) => {
210+
resolve(fn());
211+
}).then(() => {
212+
this.paused = false;
213+
if (this.repl) {
214+
this.repl.resume();
215+
this.repl.displayPrompt();
216+
}
217+
this.stdin.resume();
218+
}).then(null, (error) => process.nextTick(() => { throw error; }));
219+
}
220+
221+
killChild() {
222+
this.client.reset();
223+
if (this.child) {
224+
this.child.kill();
225+
this.child = null;
226+
}
227+
}
228+
229+
run() {
230+
this.killChild();
231+
232+
return this._runScript().then(({ 0: child, 1: port, 2: host }) => {
233+
this.child = child;
234+
235+
let connectionAttempts = 0;
236+
const attemptConnect = () => {
237+
++connectionAttempts;
238+
debuglog('connection attempt #%d', connectionAttempts);
239+
this.stdout.write('.');
240+
return this.client.connect(port, host)
241+
.then(() => {
242+
debuglog('connection established');
243+
this.stdout.write(' ok');
244+
}, (error) => {
245+
debuglog('connect failed', error);
246+
// If it's failed to connect 10 times then print failed message
247+
if (connectionAttempts >= 10) {
248+
this.stdout.write(' failed to connect, please retry\n');
249+
process.exit(1);
250+
}
251+
252+
return new Promise((resolve) => setTimeout(resolve, 500))
253+
.then(attemptConnect);
254+
});
255+
};
256+
257+
this.print(`connecting to ${host}:${port} ..`, true);
258+
return attemptConnect();
259+
});
260+
}
261+
262+
clearLine() {
263+
if (this.stdout.isTTY) {
264+
this.stdout.cursorTo(0);
265+
this.stdout.clearLine(1);
266+
} else {
267+
this.stdout.write('\b');
268+
}
269+
}
270+
271+
print(text, oneline = false) {
272+
this.clearLine();
273+
this.stdout.write(oneline ? text : `${text}\n`);
274+
}
275+
276+
childPrint(text) {
277+
this.print(
278+
text.toString()
279+
.split(/\r\n|\r|\n/g)
280+
.filter((chunk) => !!chunk)
281+
.map((chunk) => `< ${chunk}`)
282+
.join('\n')
283+
);
284+
if (!this.paused) {
285+
this.repl.displayPrompt(true);
286+
}
287+
if (/Waiting for the debugger to disconnect\.\.\.\n$/.test(text)) {
288+
this.killChild();
289+
}
290+
}
291+
}
292+
293+
function parseArgv([target, ...args]) {
294+
let host = '127.0.0.1';
295+
let port = 9229;
296+
let isRemote = false;
297+
let script = target;
298+
let scriptArgs = args;
299+
300+
const hostMatch = target.match(/^([^:]+):(\d+)$/);
301+
const portMatch = target.match(/^--port=(\d+)$/);
302+
303+
if (hostMatch) {
304+
// Connecting to remote debugger
305+
host = hostMatch[1];
306+
port = parseInt(hostMatch[2], 10);
307+
isRemote = true;
308+
script = null;
309+
} else if (portMatch) {
310+
// Start on custom port
311+
port = parseInt(portMatch[1], 10);
312+
script = args[0];
313+
scriptArgs = args.slice(1);
314+
} else if (args.length === 1 && /^\d+$/.test(args[0]) && target === '-p') {
315+
// Start debugger against a given pid
316+
const pid = parseInt(args[0], 10);
317+
try {
318+
process._debugProcess(pid);
319+
} catch (e) {
320+
if (e.code === 'ESRCH') {
321+
console.error(`Target process: ${pid} doesn't exist.`);
322+
process.exit(1);
323+
}
324+
throw e;
325+
}
326+
script = null;
327+
isRemote = true;
328+
}
329+
330+
return {
331+
host, port, isRemote, script, scriptArgs,
332+
};
333+
}
334+
335+
function startInspect(argv = process.argv.slice(2),
336+
stdin = process.stdin,
337+
stdout = process.stdout) {
338+
if (argv.length < 1) {
339+
const invokedAs = runAsStandalone ?
340+
'node-inspect' :
341+
`${process.argv0} ${process.argv[1]}`;
342+
343+
console.error(`Usage: ${invokedAs} script.js`);
344+
console.error(` ${invokedAs} <host>:<port>`);
345+
console.error(` ${invokedAs} -p <pid>`);
346+
process.exit(1);
347+
}
348+
349+
const options = parseArgv(argv);
350+
const inspector = new NodeInspector(options, stdin, stdout);
351+
352+
stdin.resume();
353+
354+
function handleUnexpectedError(e) {
355+
if (!(e instanceof StartupError)) {
356+
console.error('There was an internal error in Node.js. ' +
357+
'Please report this bug.');
358+
console.error(e.message);
359+
console.error(e.stack);
360+
} else {
361+
console.error(e.message);
362+
}
363+
if (inspector.child) inspector.child.kill();
364+
process.exit(1);
365+
}
366+
367+
process.on('uncaughtException', handleUnexpectedError);
368+
}
369+
exports.start = startInspect;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.