diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 46674883381392..08f7aaa9e3e7e8 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -659,7 +659,17 @@ class Interface extends InterfaceConstructor { [kInsertString](c) { this[kBeforeEdit](this.line, this.cursor); if (!this.isCompletionEnabled) { - this.line += c; + if (this.cursor < this.line.length) { + const beg = StringPrototypeSlice(this.line, 0, this.cursor); + const end = StringPrototypeSlice( + this.line, + this.cursor, + this.line.length, + ); + this.line = beg + c + end; + } else { + this.line += c; + } this.cursor += c.length; this[kWriteToOutput](c); return; diff --git a/test/parallel/test-repl-paste-big-data.js b/test/parallel/test-repl-paste-big-data.js index 5abc195cecb968..78cdd93c5ba35c 100644 --- a/test/parallel/test-repl-paste-big-data.js +++ b/test/parallel/test-repl-paste-big-data.js @@ -9,6 +9,11 @@ const { startNewREPLServer } = require('../common/repl'); const cpuUsage = process.cpuUsage(); const { replServer } = startNewREPLServer({}, { disableDomainErrorAssert: true }); +replServer.input.emit('data', '{}'); +replServer.input.emit('keypress', '', { name: 'left' }); +replServer.input.emit('data', 'node'); +assert.strictEqual(replServer.line, '{node}'); + replServer.input.emit('data', 'a'.repeat(2e4) + '\n'); replServer.input.emit('data', '.exit\n');