From def85daaceb96e511d7568ad8acdc34fc39b593e Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 20 Apr 2021 22:46:42 -0700 Subject: [PATCH] debugger: accommodate line chunking in Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/38161 Refs: https://github.com/nodejs/node/discussions/36481 Reviewed-By: Matteo Collina Reviewed-By: Jan Krems Reviewed-By: Colin Ihrig Reviewed-By: Stephen Belanger Reviewed-By: Gerhard Stöbich Reviewed-By: Michaël Zasso --- lib/internal/inspector/_inspect.js | 43 ++++++++++++++++---------- lib/internal/inspector/inspect_repl.js | 10 +++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/internal/inspector/_inspect.js b/lib/internal/inspector/_inspect.js index 8bc1f13e5726f0..b58b4b695e8505 100644 --- a/lib/internal/inspector/_inspect.js +++ b/lib/internal/inspector/_inspect.js @@ -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) { @@ -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 @@ -245,7 +245,7 @@ class NodeInspector { }); }; - this.print(`connecting to ${host}:${port} ..`, true); + this.print(`connecting to ${host}:${port} ..`, false); return attemptConnect(); }); } @@ -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(); } } diff --git a/lib/internal/inspector/inspect_repl.js b/lib/internal/inspector/inspect_repl.js index 7d5b019e28506d..7fd9772eef733b 100644 --- a/lib/internal/inspector/inspect_repl.js +++ b/lib/internal/inspector/inspect_repl.js @@ -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() { @@ -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(() => { @@ -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); });