repl: fix cpu overhead pasting big strings to the REPL

Pasting input should not trigger any completions and other
calculations. This is now handled by just writing the string to the
terminal in case the user is pasting. As soon as pasting is done,
the completions will be re-enabled.

Fixes: https://github.com/nodejs/node/issues/40626
Fixes: https://github.com/nodejs/node/issues/43343
PR-URL: https://github.com/nodejs/node/pull/59857
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2025-09-14 02:06:47 +02:00 committed by GitHub
parent d079d8f380
commit 49f79b8e93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 0 deletions

View File

@ -658,6 +658,12 @@ class Interface extends InterfaceConstructor {
[kInsertString](c) {
this[kBeforeEdit](this.line, this.cursor);
if (!this.isCompletionEnabled) {
this.line += c;
this.cursor += c.length;
this[kWriteToOutput](c);
return;
}
if (this.cursor < this.line.length) {
const beg = StringPrototypeSlice(this.line, 0, this.cursor);
const end = StringPrototypeSlice(

View File

@ -0,0 +1,36 @@
'use strict';
const common = require('../common');
const repl = require('repl');
const stream = require('stream');
const assert = require('assert');
// Pasting big input should not cause the process to have a huge CPU usage.
const cpuUsage = process.cpuUsage();
const r = initRepl();
r.input.emit('data', 'a'.repeat(2e4) + '\n');
r.input.emit('data', '.exit\n');
r.once('exit', common.mustCall(() => {
const diff = process.cpuUsage(cpuUsage);
assert.ok(diff.user < 1e6);
}));
function initRepl() {
const input = new stream();
input.write = input.pause = input.resume = () => {};
input.readable = true;
const output = new stream();
output.write = () => {};
output.writable = true;
return repl.start({
input,
output,
terminal: true,
prompt: ''
});
}