node/test/parallel/test-repl-paste-big-data.js
Ruben Bridgewater 49f79b8e93
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>
2025-09-14 00:06:47 +00:00

37 lines
792 B
JavaScript

'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: ''
});
}