Skip to content

Commit

Permalink
debugger: accommodate line chunking in Windows
Browse files Browse the repository at this point in the history
PR-URL: #38161
Backport-PR-URL: #38858
Refs: #36481
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Trott authored and richardlau committed Jul 20, 2021
1 parent 1da6921 commit b172e6f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
43 changes: 26 additions & 17 deletions lib/internal/inspector/_inspect.js
Expand Up @@ -97,8 +97,8 @@ function runScript(script, scriptArgs, inspectHost, inspectPort, childPrint) {
const child = spawn(process.execPath, args);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', childPrint);
child.stderr.on('data', childPrint);
child.stdout.on('data', (chunk) => childPrint(chunk, 'stdout'));
child.stderr.on('data', (chunk) => childPrint(chunk, 'stderr'));

let output = '';
function waitForListenHint(text) {
Expand Down Expand Up @@ -231,7 +231,7 @@ class NodeInspector {
return this.client.connect(port, host)
.then(() => {
debuglog('connection established');
this.stdout.write(' ok');
this.stdout.write(' ok\n');
}, (error) => {
debuglog('connect failed', error);
// If it's failed to connect 10 times then print failed message
Expand All @@ -245,7 +245,7 @@ class NodeInspector {
});
};

this.print(`connecting to ${host}:${port} ..`, true);
this.print(`connecting to ${host}:${port} ..`, false);
return attemptConnect();
});
}
Expand All @@ -259,23 +259,32 @@ class NodeInspector {
}
}

print(text, oneline = false) {
print(text, appendNewline = false) {
this.clearLine();
this.stdout.write(oneline ? text : `${text}\n`);
this.stdout.write(appendNewline ? `${text}\n` : text);
}

childPrint(text) {
this.print(
text.toString()
.split(/\r\n|\r|\n/g)
.filter((chunk) => !!chunk)
.map((chunk) => `< ${chunk}`)
.join('\n')
);
if (!this.paused) {
this.repl.displayPrompt(true);
#stdioBuffers = {stdout: '', stderr: ''};
childPrint(text, which) {
const lines = (this.#stdioBuffers[which] + text)
.split(/\r\n|\r|\n/g);

this.#stdioBuffers[which] = '';

if (lines[lines.length - 1] !== '') {
this.#stdioBuffers[which] = lines.pop();
}

const textToPrint = lines.map((chunk) => `< ${chunk}`).join('\n');

if (lines.length) {
this.print(textToPrint, true);
if (!this.paused) {
this.repl.displayPrompt(true);
}
}
if (/Waiting for the debugger to disconnect\.\.\.\n$/.test(text)) {

if (textToPrint.endsWith('Waiting for the debugger to disconnect...\n')) {
this.killChild();
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/internal/inspector/inspect_repl.js
Expand Up @@ -320,9 +320,9 @@ function createRepl(inspector) {
return util.inspect(value, INSPECT_OPTIONS);
}

function print(value, oneline = false) {
function print(value, addNewline = true) {
const text = typeof value === 'string' ? value : inspect(value);
return inspector.print(text, oneline);
return inspector.print(text, addNewline);
}

function getCurrentLocation() {
Expand Down Expand Up @@ -928,13 +928,13 @@ function createRepl(inspector) {
if (finished) {
print('Heap snaphost prepared.');
} else {
print(`Heap snapshot: ${done}/${total}`, true);
print(`Heap snapshot: ${done}/${total}`, false);
}
}
function onChunk({ chunk }) {
sizeWritten += chunk.length;
writer.write(chunk);
print(`Writing snapshot: ${sizeWritten}`, true);
print(`Writing snapshot: ${sizeWritten}`, false);
}
function onResolve() {
writer.end(() => {
Expand All @@ -956,7 +956,7 @@ function createRepl(inspector) {
HeapProfiler.on('reportHeapSnapshotProgress', onProgress);
HeapProfiler.on('addHeapSnapshotChunk', onChunk);

print('Heap snapshot: 0/0', true);
print('Heap snapshot: 0/0', false);
HeapProfiler.takeHeapSnapshot({ reportProgress: true })
.then(onResolve, onReject);
});
Expand Down

0 comments on commit b172e6f

Please sign in to comment.