diff options
Diffstat (limited to 'deps/node-inspect/lib/_inspect.js')
-rw-r--r-- | deps/node-inspect/lib/_inspect.js | 134 |
1 files changed, 75 insertions, 59 deletions
diff --git a/deps/node-inspect/lib/_inspect.js b/deps/node-inspect/lib/_inspect.js index 7c13ef786e..26912274cd 100644 --- a/deps/node-inspect/lib/_inspect.js +++ b/deps/node-inspect/lib/_inspect.js @@ -42,7 +42,7 @@ const [ InspectClient, createRepl ] = const debuglog = util.debuglog('inspect'); -const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)-port=(\d+)$/; +const DEBUG_PORT_PATTERN = /^--(?:debug|inspect)(?:-port|-brk)?=(\d{1,5})$/; function getDefaultPort() { for (const arg of process.execArgv) { const match = arg.match(DEBUG_PORT_PATTERN); @@ -53,54 +53,6 @@ function getDefaultPort() { return 9229; } -function runScript(script, scriptArgs, inspectPort, childPrint) { - return new Promise((resolve) => { - const needDebugBrk = process.version.match(/^v(6|7)\./); - const args = (needDebugBrk ? - ['--inspect', `--debug-brk=${inspectPort}`] : - [`--inspect-brk=${inspectPort}`]) - .concat([script], scriptArgs); - const child = spawn(process.execPath, args); - child.stdout.setEncoding('utf8'); - child.stderr.setEncoding('utf8'); - child.stdout.on('data', childPrint); - child.stderr.on('data', childPrint); - - let output = ''; - function waitForListenHint(text) { - output += text; - if (/chrome-devtools:\/\//.test(output)) { - child.stderr.removeListener('data', waitForListenHint); - resolve(child); - } - } - - child.stderr.on('data', waitForListenHint); - }); -} - -function createAgentProxy(domain, client) { - const agent = new EventEmitter(); - agent.then = (...args) => { - // TODO: potentially fetch the protocol and pretty-print it here. - const descriptor = { - [util.inspect.custom](depth, { stylize }) { - return stylize(`[Agent ${domain}]`, 'special'); - }, - }; - return Promise.resolve(descriptor).then(...args); - }; - - return new Proxy(agent, { - get(target, name) { - if (name in target) return target[name]; - return function callVirtualMethod(params) { - return client.callMethod(`${domain}.${name}`, params); - }; - }, - }); -} - function portIsFree(host, port, timeout = 2000) { const retryDelay = 150; let didTimeOut = false; @@ -140,6 +92,57 @@ function portIsFree(host, port, timeout = 2000) { }); } +function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) { + return portIsFree(inspectHost, inspectPort) + .then(() => { + return new Promise((resolve) => { + const needDebugBrk = process.version.match(/^v(6|7)\./); + const args = (needDebugBrk ? + ['--inspect', `--debug-brk=${inspectPort}`] : + [`--inspect-brk=${inspectPort}`]) + .concat([script], scriptArgs); + const child = spawn(process.execPath, args); + child.stdout.setEncoding('utf8'); + child.stderr.setEncoding('utf8'); + child.stdout.on('data', childPrint); + child.stderr.on('data', childPrint); + + let output = ''; + function waitForListenHint(text) { + output += text; + if (/Debugger listening on/.test(output)) { + child.stderr.removeListener('data', waitForListenHint); + resolve(child); + } + } + + child.stderr.on('data', waitForListenHint); + }); + }); +} + +function createAgentProxy(domain, client) { + const agent = new EventEmitter(); + agent.then = (...args) => { + // TODO: potentially fetch the protocol and pretty-print it here. + const descriptor = { + [util.inspect.custom](depth, { stylize }) { + return stylize(`[Agent ${domain}]`, 'special'); + }, + }; + return Promise.resolve(descriptor).then(...args); + }; + + return new Proxy(agent, { + get(target, name) { + if (name in target) return target[name]; + return function callVirtualMethod(params) { + return client.callMethod(`${domain}.${name}`, params); + }; + }, + }); +} + class NodeInspector { constructor(options, stdin, stdout) { this.options = options; @@ -153,6 +156,7 @@ class NodeInspector { this._runScript = runScript.bind(null, options.script, options.scriptArgs, + options.host, options.port, this.childPrint.bind(this)); } else { @@ -221,12 +225,7 @@ class NodeInspector { this.killChild(); const { host, port } = this.options; - const runOncePortIsFree = () => { - return portIsFree(host, port) - .then(() => this._runScript()); - }; - - return runOncePortIsFree().then((child) => { + return this._runScript().then((child) => { this.child = child; let connectionAttempts = 0; @@ -296,6 +295,7 @@ function parseArgv([target, ...args]) { const hostMatch = target.match(/^([^:]+):(\d+)$/); const portMatch = target.match(/^--port=(\d+)$/); + if (hostMatch) { // Connecting to remote debugger // `node-inspect localhost:9229` @@ -304,16 +304,31 @@ function parseArgv([target, ...args]) { isRemote = true; script = null; } else if (portMatch) { - // Start debugger on custom port - // `node debug --port=8058 app.js` + // start debugee on custom port + // `node inspect --port=9230 script.js` port = parseInt(portMatch[1], 10); script = args[0]; scriptArgs = args.slice(1); + } else if (args.length === 1 && /^\d+$/.test(args[0]) && target === '-p') { + // Start debugger against a given pid + const pid = parseInt(args[0], 10); + try { + process._debugProcess(pid); + } catch (e) { + if (e.code === 'ESRCH') { + /* eslint-disable no-console */ + console.error(`Target process: ${pid} doesn't exist.`); + /* eslint-enable no-console */ + process.exit(1); + } + throw e; + } + script = null; + isRemote = true; } return { - host, port, - isRemote, script, scriptArgs, + host, port, isRemote, script, scriptArgs, }; } @@ -328,6 +343,7 @@ function startInspect(argv = process.argv.slice(2), console.error(`Usage: ${invokedAs} script.js`); console.error(` ${invokedAs} <host>:<port>`); + console.error(` ${invokedAs} -p <pid>`); process.exit(1); } |