diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index bc8f30427616..964213d3be0e 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 000000000000..b75320c0ccea --- /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(); +}