diff --git a/lib/repl.js b/lib/repl.js index 5c3f0f15268acd..f925a3d87f5867 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -169,6 +169,34 @@ function REPLServer(prompt, eval_ = eval_ || defaultEval; + // Pause taking in new input, and store the keys in a buffer. + const pausedBuffer = []; + let paused = false; + function pause() { + paused = true; + } + function unpause() { + if (!paused) return; + paused = false; + let entry; + while (entry = pausedBuffer.shift()) { + const [type, payload] = entry; + switch (type) { + case 'key': { + const [d, key] = payload; + self._ttyWrite(d, key); + break; + } + case 'close': + self.emit('exit'); + break; + } + if (paused) { + break; + } + } + } + function defaultEval(code, context, file, cb) { var err, result, script, wrappedErr; var wrappedCmd = false; @@ -420,6 +448,10 @@ function REPLServer(prompt, 'DEP0075'); self.on('close', function emitExit() { + if (paused) { + pausedBuffer.push(['close']); + return; + } self.emit('exit'); }); @@ -557,10 +589,14 @@ function REPLServer(prompt, } }); - // Wrap readline tty to enable editor mode + // Wrap readline tty to enable editor mode and pausing. const ttyWrite = self._ttyWrite.bind(self); self._ttyWrite = (d, key) => { key = key || {}; + if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) { + pausedBuffer.push(['key', [d, key]]); + return; + } if (!self.editorMode || !self.terminal) { ttyWrite(d, key); return;