From 76d0f7499ccc950d1d742f0697587b697dd63f93 Mon Sep 17 00:00:00 2001 From: Mhayk Whandson Date: Wed, 15 Jul 2026 11:24:17 +0100 Subject: [PATCH 1/2] repl: keep entries added while history file is loading Lines can be evaluated while the history file is still being read asynchronously by setupHistory(), e.g. when the input stream does not support pausing. The entries added to the in-memory history in the meantime were discarded once the file load completed, because the loaded entries overwrote the in-memory history. Merge the persisted entries with the in-memory ones instead. Refs: https://github.com/nodejs/node/issues/64508 Signed-off-by: Mhayk Whandson --- lib/internal/repl/history.js | 18 +++++- ...-history-load-preserves-pending-entries.js | 62 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-repl-history-load-preserves-pending-entries.js diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index b197c7049cc0..505c900f2cd6 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -4,6 +4,7 @@ const { ArrayPrototypeIndexOf, ArrayPrototypeJoin, ArrayPrototypePop, + ArrayPrototypePushApply, ArrayPrototypeShift, ArrayPrototypeSplice, ArrayPrototypeUnshift, @@ -302,10 +303,21 @@ class ReplHistory { return this[kHandleHistoryInitError](err, onReadyCallback); } - if (data) { - this[kHistory] = RegExpPrototypeSymbolSplit(/\r?\n+/, data, this[kSize]); + const loadedHistory = data ? + RegExpPrototypeSymbolSplit(/\r?\n+/, data, this[kSize]) : + []; + + // Lines can be evaluated while the history file is still being read, + // e.g. when the input stream does not support pausing. Such entries + // are already in the in-memory history (newest first), so append the + // persisted entries to them instead of discarding them. + if (this[kHistory].length > 0) { + ArrayPrototypePushApply(this[kHistory], loadedHistory); + if (this[kHistory].length > this[kSize]) { + ArrayPrototypeSplice(this[kHistory], this[kSize]); + } } else { - this[kHistory] = []; + this[kHistory] = loadedHistory; } validateArray(this[kHistory], 'history'); diff --git a/test/parallel/test-repl-history-load-preserves-pending-entries.js b/test/parallel/test-repl-history-load-preserves-pending-entries.js new file mode 100644 index 000000000000..a0e4f9f3f2bd --- /dev/null +++ b/test/parallel/test-repl-history-load-preserves-pending-entries.js @@ -0,0 +1,62 @@ +'use strict'; + +// Lines can be evaluated while the history file is still being loaded +// asynchronously by `setupHistory()`, e.g. when the input stream does not +// support pausing. Entries added to the in-memory history in the meantime +// must not be discarded once the file load completes. +// Refs: https://github.com/nodejs/node/issues/64508 + +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const stream = require('stream'); +const repl = require('repl'); + +if (process.env.TERM === 'dumb') { + common.skip('skipping - dumb terminal'); +} + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const historyPath = tmpdir.resolve('.repl_history'); +fs.writeFileSync(historyPath, 'persisted entry'); + +// An input stream that, unlike a TTY, does not buffer data while paused. +class FakeInput extends stream.Stream { + resume() {} + pause() {} +} +FakeInput.prototype.readable = true; + +const input = new FakeInput(); +const output = new stream.Writable({ + write(chunk, encoding, callback) { + callback(); + }, +}); + +const r = repl.start({ + input, + output, + prompt: '', + terminal: true, + useColors: false, +}); + +r.setupHistory(historyPath, common.mustSucceed(() => { + // The lines evaluated while the history file was being read must be kept, + // newest first, followed by the persisted entries. + assert.deepStrictEqual( + r.history, + ['const b = 2', 'const a = 1', 'persisted entry'], + ); + assert.strictEqual( + fs.readFileSync(historyPath, 'utf8'), + 'const b = 2\nconst a = 1\npersisted entry', + ); + r.close(); +})); + +// Evaluated synchronously, before the history file has been read. +input.emit('data', 'const a = 1\nconst b = 2\n'); From 59aa7e73bc5aa55bcc2cb012eba4e09095609fa6 Mon Sep 17 00:00:00 2001 From: Mhayk Whandson Date: Sat, 22 Aug 2026 08:13:37 +0100 Subject: [PATCH 2/2] test: skip repl history test without inspector The REPL eval depends on the inspector, so the test fails with ERR_INSPECTOR_NOT_AVAILABLE on builds configured --without-ssl. Skip it like the other REPL history tests do. Refs: https://github.com/nodejs/node/pull/64513#issuecomment-5377366526 --- .../test-repl-history-load-preserves-pending-entries.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/parallel/test-repl-history-load-preserves-pending-entries.js b/test/parallel/test-repl-history-load-preserves-pending-entries.js index a0e4f9f3f2bd..01e025f4267e 100644 --- a/test/parallel/test-repl-history-load-preserves-pending-entries.js +++ b/test/parallel/test-repl-history-load-preserves-pending-entries.js @@ -16,6 +16,8 @@ if (process.env.TERM === 'dumb') { common.skip('skipping - dumb terminal'); } +common.skipIfInspectorDisabled(); + const tmpdir = require('../common/tmpdir'); tmpdir.refresh();