Skip to content

Commit def85da

Browse files
Trotttargos
authored andcommitted
debugger: accommodate line chunking in Windows
PR-URL: #38161 Refs: #36481 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 07361e6 commit def85da

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

lib/internal/inspector/_inspect.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) {
9797
const child = spawn(process.execPath, args);
9898
child.stdout.setEncoding('utf8');
9999
child.stderr.setEncoding('utf8');
100-
child.stdout.on('data', childPrint);
101-
child.stderr.on('data', childPrint);
100+
child.stdout.on('data', (chunk) => childPrint(chunk, 'stdout'));
101+
child.stderr.on('data', (chunk) => childPrint(chunk, 'stderr'));
102102

103103
let output = '';
104104
function waitForListenHint(text) {
@@ -231,7 +231,7 @@ class NodeInspector {
231231
return this.client.connect(port, host)
232232
.then(() => {
233233
debuglog('connection established');
234-
this.stdout.write(' ok');
234+
this.stdout.write(' ok\n');
235235
}, (error) => {
236236
debuglog('connect failed', error);
237237
// If it's failed to connect 10 times then print failed message
@@ -245,7 +245,7 @@ class NodeInspector {
245245
});
246246
};
247247

248-
this.print(`connecting to ${host}:${port} ..`, true);
248+
this.print(`connecting to ${host}:${port} ..`, false);
249249
return attemptConnect();
250250
});
251251
}
@@ -259,23 +259,32 @@ class NodeInspector {
259259
}
260260
}
261261

262-
print(text, oneline = false) {
262+
print(text, appendNewline = false) {
263263
this.clearLine();
264-
this.stdout.write(oneline ? text : `${text}\n`);
264+
this.stdout.write(appendNewline ? `${text}\n` : text);
265265
}
266266

267-
childPrint(text) {
268-
this.print(
269-
text.toString()
270-
.split(/\r\n|\r|\n/g)
271-
.filter((chunk) => !!chunk)
272-
.map((chunk) => `< ${chunk}`)
273-
.join('\n')
274-
);
275-
if (!this.paused) {
276-
this.repl.displayPrompt(true);
267+
#stdioBuffers = {stdout: '', stderr: ''};
268+
childPrint(text, which) {
269+
const lines = (this.#stdioBuffers[which] + text)
270+
.split(/\r\n|\r|\n/g);
271+
272+
this.#stdioBuffers[which] = '';
273+
274+
if (lines[lines.length - 1] !== '') {
275+
this.#stdioBuffers[which] = lines.pop();
276+
}
277+
278+
const textToPrint = lines.map((chunk) => `< ${chunk}`).join('\n');
279+
280+
if (lines.length) {
281+
this.print(textToPrint, true);
282+
if (!this.paused) {
283+
this.repl.displayPrompt(true);
284+
}
277285
}
278-
if (/Waiting for the debugger to disconnect\.\.\.\n$/.test(text)) {
286+
287+
if (textToPrint.endsWith('Waiting for the debugger to disconnect...\n')) {
279288
this.killChild();
280289
}
281290
}

lib/internal/inspector/inspect_repl.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ function createRepl(inspector) {
320320
return util.inspect(value, INSPECT_OPTIONS);
321321
}
322322

323-
function print(value, oneline = false) {
323+
function print(value, addNewline = true) {
324324
const text = typeof value === 'string' ? value : inspect(value);
325-
return inspector.print(text, oneline);
325+
return inspector.print(text, addNewline);
326326
}
327327

328328
function getCurrentLocation() {
@@ -928,13 +928,13 @@ function createRepl(inspector) {
928928
if (finished) {
929929
print('Heap snaphost prepared.');
930930
} else {
931-
print(`Heap snapshot: ${done}/${total}`, true);
931+
print(`Heap snapshot: ${done}/${total}`, false);
932932
}
933933
}
934934
function onChunk({ chunk }) {
935935
sizeWritten += chunk.length;
936936
writer.write(chunk);
937-
print(`Writing snapshot: ${sizeWritten}`, true);
937+
print(`Writing snapshot: ${sizeWritten}`, false);
938938
}
939939
function onResolve() {
940940
writer.end(() => {
@@ -956,7 +956,7 @@ function createRepl(inspector) {
956956
HeapProfiler.on('reportHeapSnapshotProgress', onProgress);
957957
HeapProfiler.on('addHeapSnapshotChunk', onChunk);
958958

959-
print('Heap snapshot: 0/0', true);
959+
print('Heap snapshot: 0/0', false);
960960
HeapProfiler.takeHeapSnapshot({ reportProgress: true })
961961
.then(onResolve, onReject);
962962
});

0 commit comments

Comments
 (0)