Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ function InterfaceConstructor(input, output, completer, terminal) {
input.removeHistoryDuplicates = removeHistoryDuplicates;
}

this.setupHistoryManager(input);

if (completer !== undefined && typeof completer !== 'function') {
throw new ERR_INVALID_ARG_VALUE('completer', completer);
Expand All @@ -234,6 +233,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
this[kSubstringSearch] = null;
this.output = output;
this.input = input;
this.setupHistoryManager(input);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reodering only resolve this.input being undefine but still doesn't fix accidental REPL history init where readline.createInterface(input) is not supposed to touch REPL history but setupHistoryManager(input) ends up seeing a truthy input.onHistoryFileLoaded and calls ReplHistory.initialize() anyway

this[kUndoStack] = [];
this[kRedoStack] = [];
this[kPreviousCursorCols] = -1;
Expand Down Expand Up @@ -384,9 +384,13 @@ class Interface extends InterfaceConstructor {
setupHistoryManager(options) {
this.historyManager = new ReplHistory(this, options);

if (options.onHistoryFileLoaded) {
this.historyManager.initialize(options.onHistoryFileLoaded);
}
// Only initialize REPL history when createInterface()
// was called with an options object (options.input).
// This prevents accidental REPL history init when
// createInterface(input) is used.
if (options?.input && typeof options.onHistoryFileLoaded === 'function') {
this.historyManager.initialize(options.onHistoryFileLoaded);
}

ObjectDefineProperty(this, 'history', {
__proto__: null, configurable: true, enumerable: true,
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-readline-history-init-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const assert = require('assert');
const readline = require('readline');
const { PassThrough } = require('stream');

assert.doesNotThrow(() => {
const input = new PassThrough();

// This simulates the condition that previously caused
// accidental REPL history initialization.
input.onHistoryFileLoaded = () => {};

readline.createInterface({
input,
output: new PassThrough()
});
});