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

Commit 802b88c

Browse files
author
Jan Krems
committed
fix: Delay run until breakpoints are restored
1 parent df182d9 commit 802b88c

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

lib/_inspect.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ class NodeInspector {
130130
process.once('SIGHUP', process.exit.bind(process, 0));
131131

132132
this.run()
133-
.then(() => {
134-
this.repl = startRepl();
133+
.then(() => startRepl())
134+
.then((repl) => {
135+
this.repl = repl;
135136
this.repl.on('exit', () => {
136137
process.exit(0);
137138
});
@@ -141,15 +142,19 @@ class NodeInspector {
141142
}
142143

143144
suspendReplWhile(fn) {
144-
this.repl.rli.pause();
145+
if (this.repl) {
146+
this.repl.rli.pause();
147+
}
145148
this.stdin.pause();
146149
this.paused = true;
147150
return new Promise((resolve) => {
148151
resolve(fn());
149152
}).then(() => {
150153
this.paused = false;
151-
this.repl.rli.resume();
152-
this.repl.displayPrompt();
154+
if (this.repl) {
155+
this.repl.rli.resume();
156+
this.repl.displayPrompt();
157+
}
153158
this.stdin.resume();
154159
}).then(null, (error) => process.nextTick(() => { throw error; }));
155160
}

lib/internal/inspect_client.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,7 @@ class Client extends EventEmitter {
334334
this.emit('close');
335335
});
336336

337-
Promise.all([
338-
this.callMethod('Runtime.enable'),
339-
this.callMethod('Debugger.enable'),
340-
this.callMethod('Debugger.setPauseOnExceptions', { state: 'none' }),
341-
this.callMethod('Debugger.setAsyncCallStackDepth', { maxDepth: 0 }),
342-
this.callMethod('Profiler.enable'),
343-
this.callMethod('Profiler.setSamplingInterval', { interval: 100 }),
344-
this.callMethod('Debugger.setBlackboxPatterns', { patterns: [] }),
345-
this.callMethod('Runtime.runIfWaitingForDebugger'),
346-
]).then(() => {
347-
this.emit('ready');
348-
}, (error) => {
349-
this.emit('error', error);
350-
});
337+
this.emit('ready');
351338
};
352339

353340
return new Promise((resolve, reject) => {

lib/internal/inspect_repl.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,8 @@ function createRepl(inspector) {
748748
.filter(({ location }) => !!location.scriptUrl)
749749
.map(({ location }) =>
750750
setBreakpoint(location.scriptUrl, location.lineNumber + 1));
751-
if (!newBreakpoints.length) return;
752-
Promise.all(newBreakpoints).then((results) => {
751+
if (!newBreakpoints.length) return Promise.resolve();
752+
return Promise.all(newBreakpoints).then((results) => {
753753
print(`${results.length} breakpoints restored.`);
754754
});
755755
}
@@ -1026,7 +1026,30 @@ function createRepl(inspector) {
10261026
aliasProperties(context, SHORTCUTS);
10271027
}
10281028

1029+
function initAfterStart() {
1030+
const setupTasks = [
1031+
Runtime.enable(),
1032+
Profiler.enable(),
1033+
Profiler.setSamplingInterval({ interval: 100 }),
1034+
Debugger.enable(),
1035+
Debugger.setPauseOnExceptions({ state: 'none' }),
1036+
Debugger.setAsyncCallStackDepth({ maxDepth: 0 }),
1037+
Debugger.setBlackboxPatterns({ patterns: [] }),
1038+
Debugger.setPauseOnExceptions({ state: pauseOnExceptionState }),
1039+
restoreBreakpoints(),
1040+
Runtime.runIfWaitingForDebugger(),
1041+
];
1042+
return Promise.all(setupTasks);
1043+
}
1044+
10291045
return function startRepl() {
1046+
inspector.client.on('close', () => {
1047+
resetOnStart();
1048+
});
1049+
inspector.client.on('ready', () => {
1050+
initAfterStart();
1051+
});
1052+
10301053
const replOptions = {
10311054
prompt: 'debug> ',
10321055
input: inspector.stdin,
@@ -1035,6 +1058,7 @@ function createRepl(inspector) {
10351058
useGlobal: false,
10361059
ignoreUndefined: true,
10371060
};
1061+
10381062
repl = Repl.start(replOptions); // eslint-disable-line prefer-const
10391063
initializeContext(repl.context);
10401064
repl.on('reset', initializeContext);
@@ -1044,14 +1068,8 @@ function createRepl(inspector) {
10441068
repl.rli.emit('SIGINT');
10451069
});
10461070

1047-
inspector.client.on('close', () => {
1048-
resetOnStart();
1049-
});
1050-
1051-
inspector.client.on('ready', () => {
1052-
restoreBreakpoints();
1053-
Debugger.setPauseOnExceptions({ state: pauseOnExceptionState });
1054-
});
1071+
// Init once for the initial connection
1072+
initAfterStart();
10551073

10561074
return repl;
10571075
};

test/cli/launch.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const startCLI = require('./start-cli');
88
test('examples/empty.js', (t) => {
99
const script = Path.join('examples', 'empty.js');
1010
const cli = startCLI([script]);
11-
return cli.waitForPrompt()
11+
12+
return cli.waitFor(/break/)
13+
.then(() => cli.waitForPrompt())
1214
.then(() => {
1315
t.match(cli.output, 'debug>', 'prints a prompt');
1416
t.match(

0 commit comments

Comments
 (0)