From c489acfd1e5ac70f92afd4d29e0e420e6022a5ae Mon Sep 17 00:00:00 2001 From: Yohannes Desta Date: Fri, 20 Nov 2026 19:54:14 +0300 Subject: [PATCH] readline: do not read history options from the input stream When createInterface() is given a stream instead of an options object, the stream itself was passed to setupHistoryManager() as the history options. A stream with an onHistoryFileLoaded property, such as a mock, started REPL history loading before this.input was set and threw "Cannot read properties of undefined (reading 'pause')". Properties named history or size on the stream were validated as history options too. Pass empty history options in that case, as before v24.2.0. History given in an options object is unchanged. Fixes: https://github.com/nodejs/node/issues/61526 Refs: https://github.com/nodejs/node/pull/61538 Signed-off-by: Yohannes Desta --- lib/internal/readline/interface.js | 3 +- ...est-readline-input-stream-history-props.js | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-readline-input-stream-history-props.js diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index bc8f3042761..964213d3be0 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -223,7 +223,8 @@ function InterfaceConstructor(input, output, completer, terminal) { }; } - this.setupHistoryManager(historyOptions ?? input); + // When only a stream is given, its own properties are not history options. + this.setupHistoryManager(historyOptions ?? { __proto__: null }); if (completer !== undefined && typeof completer !== 'function') { throw new ERR_INVALID_ARG_VALUE('completer', completer); diff --git a/test/parallel/test-readline-input-stream-history-props.js b/test/parallel/test-readline-input-stream-history-props.js new file mode 100644 index 00000000000..b75320c0cce --- /dev/null +++ b/test/parallel/test-readline-input-stream-history-props.js @@ -0,0 +1,45 @@ +'use strict'; + +// This tests that readline.createInterface(stream) does not read REPL history +// options from the input stream itself. A stream that happens to have an +// onHistoryFileLoaded property, for example a mock, used to start REPL history +// loading before the interface was set up and threw a TypeError. +// Refs: https://github.com/nodejs/node/issues/61526 + +const common = require('../common'); +const assert = require('assert'); +const readline = require('readline'); +const { PassThrough } = require('stream'); + +{ + const input = new PassThrough(); + input.onHistoryFileLoaded = common.mustNotCall(); + const rl = readline.createInterface(input); + assert.deepStrictEqual(rl.history, []); + rl.close(); +} + +{ + const input = new PassThrough(); + input.onHistoryFileLoaded = common.mustNotCall(); + const rl = readline.createInterface({ input }); + rl.close(); +} + +// Properties of the stream with the names of history options are ignored. +{ + const input = new PassThrough(); + input.history = 'not an array'; + input.size = -1; + const rl = readline.createInterface(input); + assert.deepStrictEqual(rl.history, []); + rl.close(); +} + +// History passed in an options object still works. +{ + const input = new PassThrough(); + const rl = readline.createInterface({ input, history: ['b', 'a'] }); + assert.deepStrictEqual(rl.history, ['b', 'a']); + rl.close(); +}