diff --git a/lib/_inspect.js b/lib/_inspect.js index e80a516..9f0f714 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -130,8 +130,9 @@ class NodeInspector { process.once('SIGHUP', process.exit.bind(process, 0)); this.run() - .then(() => { - this.repl = startRepl(); + .then(() => startRepl()) + .then((repl) => { + this.repl = repl; this.repl.on('exit', () => { process.exit(0); }); @@ -141,15 +142,19 @@ class NodeInspector { } suspendReplWhile(fn) { - this.repl.rli.pause(); + if (this.repl) { + this.repl.rli.pause(); + } this.stdin.pause(); this.paused = true; return new Promise((resolve) => { resolve(fn()); }).then(() => { this.paused = false; - this.repl.rli.resume(); - this.repl.displayPrompt(); + if (this.repl) { + this.repl.rli.resume(); + this.repl.displayPrompt(); + } this.stdin.resume(); }).then(null, (error) => process.nextTick(() => { throw error; })); } diff --git a/lib/internal/inspect_client.js b/lib/internal/inspect_client.js index d5ce46d..c247e2a 100644 --- a/lib/internal/inspect_client.js +++ b/lib/internal/inspect_client.js @@ -334,20 +334,7 @@ class Client extends EventEmitter { this.emit('close'); }); - Promise.all([ - this.callMethod('Runtime.enable'), - this.callMethod('Debugger.enable'), - this.callMethod('Debugger.setPauseOnExceptions', { state: 'none' }), - this.callMethod('Debugger.setAsyncCallStackDepth', { maxDepth: 0 }), - this.callMethod('Profiler.enable'), - this.callMethod('Profiler.setSamplingInterval', { interval: 100 }), - this.callMethod('Debugger.setBlackboxPatterns', { patterns: [] }), - this.callMethod('Runtime.runIfWaitingForDebugger'), - ]).then(() => { - this.emit('ready'); - }, (error) => { - this.emit('error', error); - }); + this.emit('ready'); }; return new Promise((resolve, reject) => { diff --git a/lib/internal/inspect_repl.js b/lib/internal/inspect_repl.js index c93d792..de22b75 100644 --- a/lib/internal/inspect_repl.js +++ b/lib/internal/inspect_repl.js @@ -748,8 +748,8 @@ function createRepl(inspector) { .filter(({ location }) => !!location.scriptUrl) .map(({ location }) => setBreakpoint(location.scriptUrl, location.lineNumber + 1)); - if (!newBreakpoints.length) return; - Promise.all(newBreakpoints).then((results) => { + if (!newBreakpoints.length) return Promise.resolve(); + return Promise.all(newBreakpoints).then((results) => { print(`${results.length} breakpoints restored.`); }); } @@ -1026,7 +1026,30 @@ function createRepl(inspector) { aliasProperties(context, SHORTCUTS); } + function initAfterStart() { + const setupTasks = [ + Runtime.enable(), + Profiler.enable(), + Profiler.setSamplingInterval({ interval: 100 }), + Debugger.enable(), + Debugger.setPauseOnExceptions({ state: 'none' }), + Debugger.setAsyncCallStackDepth({ maxDepth: 0 }), + Debugger.setBlackboxPatterns({ patterns: [] }), + Debugger.setPauseOnExceptions({ state: pauseOnExceptionState }), + restoreBreakpoints(), + Runtime.runIfWaitingForDebugger(), + ]; + return Promise.all(setupTasks); + } + return function startRepl() { + inspector.client.on('close', () => { + resetOnStart(); + }); + inspector.client.on('ready', () => { + initAfterStart(); + }); + const replOptions = { prompt: 'debug> ', input: inspector.stdin, @@ -1035,6 +1058,7 @@ function createRepl(inspector) { useGlobal: false, ignoreUndefined: true, }; + repl = Repl.start(replOptions); // eslint-disable-line prefer-const initializeContext(repl.context); repl.on('reset', initializeContext); @@ -1044,14 +1068,8 @@ function createRepl(inspector) { repl.rli.emit('SIGINT'); }); - inspector.client.on('close', () => { - resetOnStart(); - }); - - inspector.client.on('ready', () => { - restoreBreakpoints(); - Debugger.setPauseOnExceptions({ state: pauseOnExceptionState }); - }); + // Init once for the initial connection + initAfterStart(); return repl; }; diff --git a/test/cli/launch.test.js b/test/cli/launch.test.js index 962197e..99c6ce0 100644 --- a/test/cli/launch.test.js +++ b/test/cli/launch.test.js @@ -8,7 +8,9 @@ const startCLI = require('./start-cli'); test('examples/empty.js', (t) => { const script = Path.join('examples', 'empty.js'); const cli = startCLI([script]); - return cli.waitForPrompt() + + return cli.waitFor(/break/) + .then(() => cli.waitForPrompt()) .then(() => { t.match(cli.output, 'debug>', 'prints a prompt'); t.match(