Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: Delay run until breakpoints are restored
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Mar 14, 2017
1 parent df182d9 commit 802b88c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
15 changes: 10 additions & 5 deletions lib/_inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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; }));
}
Expand Down
15 changes: 1 addition & 14 deletions lib/internal/inspect_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
38 changes: 28 additions & 10 deletions lib/internal/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
});
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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;
};
Expand Down
4 changes: 3 additions & 1 deletion test/cli/launch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 802b88c

Please sign in to comment.