Skip to content

Commit 44465a6

Browse files
committed
readline: fix history manager property propagation
Fix historySize and history properties not being passed to setupHistoryManager when using options object pattern. Remove unused import and trailing whitespace. Fixes: #61526
1 parent 45e1096 commit 44465a6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/internal/readline/interface.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,24 @@ function InterfaceConstructor(input, output, completer, terminal) {
235235
this[kSubstringSearch] = null;
236236
this.output = output;
237237
this.input = input;
238-
this.setupHistoryManager(inputOptions || input);
238+
239+
// If inputOptions was provided (constructor with options object),
240+
// copy the stream's size/history properties back to it for setupHistoryManager
241+
let historyOptions = inputOptions || input;
242+
if (inputOptions !== undefined && input !== undefined &&
243+
inputOptions !== input) {
244+
if (input.size !== undefined && historyOptions.size === undefined) {
245+
historyOptions.size = input.size;
246+
}
247+
if (input.history !== undefined && historyOptions.history === undefined) {
248+
historyOptions.history = input.history;
249+
}
250+
if (input.removeHistoryDuplicates !== undefined &&
251+
historyOptions.removeHistoryDuplicates === undefined) {
252+
historyOptions.removeHistoryDuplicates = input.removeHistoryDuplicates;
253+
}
254+
}
255+
this.setupHistoryManager(historyOptions);
239256
this[kUndoStack] = [];
240257
this[kRedoStack] = [];
241258
this[kPreviousCursorCols] = -1;
@@ -387,15 +404,15 @@ class Interface extends InterfaceConstructor {
387404
this.historyManager = new ReplHistory(this, options);
388405

389406
// Only initialize REPL history when called from REPL.setupHistory(),
390-
// not from the readline constructor.
407+
// not from the readline constructor.
391408
// Constructor passes: stream OR { input: stream, output: stream, ... }
392409
// setupHistory passes: { filePath: ..., size: ..., onHistoryFileLoaded: ... }
393410
// Detect constructor calls by checking for stream.on() or options.input
394411
if (options && typeof options === 'object') {
395412
const isStream = typeof options.on === 'function';
396413
const hasInputProperty = options.input !== undefined;
397414
const isFromConstructor = isStream || hasInputProperty;
398-
415+
399416
if (!isFromConstructor && typeof options.onHistoryFileLoaded === 'function') {
400417
this.historyManager.initialize(options.onHistoryFileLoaded);
401418
}

test/parallel/test-readline-history-init-order.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const common = require('../common');
43
const readline = require('readline');
54
const { PassThrough } = require('stream');
65

0 commit comments

Comments
 (0)