From db7bb941a3bc2662bcb23734f16e3ec02e0b16a9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 8 May 2020 00:20:08 +0200 Subject: [PATCH] repl: deprecate repl.inputStream and repl.outputStream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stream is exposed twice. As such it's best to rely upon the .input and .output properties set by readline. Signed-off-by: Ruben Bridgewater PR-URL: https://github.com/nodejs/node/pull/33294 Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell --- doc/api/deprecations.md | 14 ++++ lib/repl.js | 84 +++++++++++++------ test/parallel/test-repl-history-navigation.js | 2 + test/parallel/test-repl-options.js | 6 +- 4 files changed, 79 insertions(+), 27 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index fd39c628d7f451..82d97d26257161 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2666,6 +2666,20 @@ Type: Documentation-only Use [`request.destroy()`][] instead of [`request.abort()`][]. + +### DEP0XXX: `repl.inputStream` and `repl.outputStream` + + +Type: Documentation-only (supports [`--pending-deprecation`][]) + +The `repl` module exported the input and output stream twice. Use `.input` +instead of `.inputStream` and `.output` instead of `.outputStream`. + [`--pending-deprecation`]: cli.html#cli_pending_deprecation [`--throw-deprecation`]: cli.html#cli_throw_deprecation [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size diff --git a/lib/repl.js b/lib/repl.js index 3722d50533bf87..b65e16bec2694d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -100,9 +100,11 @@ const { overrideStackTrace, } = require('internal/errors'); const { sendInspectorCommand } = require('internal/util/inspector'); -const experimentalREPLAwait = require('internal/options').getOptionValue( +const { getOptionValue } = require('internal/options'); +const experimentalREPLAwait = getOptionValue( '--experimental-repl-await' ); +const pendingDeprecation = getOptionValue('--pending-deprecation'); const { REPL_MODE_SLOPPY, REPL_MODE_STRICT, @@ -220,8 +222,39 @@ function REPLServer(prompt, const preview = options.terminal && (options.preview !== undefined ? !!options.preview : !eval_); - this.inputStream = options.input; - this.outputStream = options.output; + ObjectDefineProperty(this, 'inputStream', { + get: pendingDeprecation ? + deprecate(() => this.input, + 'repl.inputStream and repl.outputStream is deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0XXX') : + () => this.input, + set: pendingDeprecation ? + deprecate((val) => this.input = val, + 'repl.inputStream and repl.outputStream is deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0XXX') : + (val) => this.input = val, + enumerable: false, + configurable: true + }); + ObjectDefineProperty(this, 'outputStream', { + get: pendingDeprecation ? + deprecate(() => this.output, + 'repl.inputStream and repl.outputStream is deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0XXX') : + () => this.output, + set: pendingDeprecation ? + deprecate((val) => this.output = val, + 'repl.inputStream and repl.outputStream is deprecated. ' + + 'Use repl.input and repl.output instead', + 'DEP0XXX') : + (val) => this.output = val, + enumerable: false, + configurable: true + }); + this.useColors = !!options.useColors; this._domain = options.domain || domain.create(); this.useGlobal = !!useGlobal; @@ -596,16 +629,13 @@ function REPLServer(prompt, } // Normalize line endings. errStack += errStack.endsWith('\n') ? '' : '\n'; - self.outputStream.write(errStack); + self.output.write(errStack); self.clearBufferedCommand(); self.lines.level = []; self.displayPrompt(); } }); - self.resetContext(); - self.lines.level = []; - self.clearBufferedCommand(); ObjectDefineProperty(this, 'bufferedCommand', { get: deprecate(() => self[kBufferedCommandSymbol], @@ -627,14 +657,16 @@ function REPLServer(prompt, } Interface.call(this, { - input: self.inputStream, - output: self.outputStream, + input: options.input, + output: options.output, completer: self.completer, terminal: options.terminal, historySize: options.historySize, prompt }); + self.resetContext(); + this.commands = ObjectCreate(null); defineDefaultCommands(this); @@ -752,7 +784,7 @@ function REPLServer(prompt, return; } if (!self[kBufferedCommandSymbol]) { - self.outputStream.write('Invalid REPL keyword\n'); + self.output.write('Invalid REPL keyword\n'); finish(null); return; } @@ -769,7 +801,7 @@ function REPLServer(prompt, _memory.call(self, cmd); if (e && !self[kBufferedCommandSymbol] && cmd.trim().startsWith('npm ')) { - self.outputStream.write('npm should be run outside of the ' + + self.output.write('npm should be run outside of the ' + 'node repl, in your normal shell.\n' + '(Press Control-D to exit.)\n'); self.displayPrompt(); @@ -804,7 +836,7 @@ function REPLServer(prompt, if (!self.underscoreAssigned) { self.last = ret; } - self.outputStream.write(self.writer(ret) + '\n'); + self.output.write(self.writer(ret) + '\n'); } // Display prompt again @@ -814,10 +846,10 @@ function REPLServer(prompt, self.on('SIGCONT', function onSigCont() { if (self.editorMode) { - self.outputStream.write(`${self._initialPrompt}.editor\n`); - self.outputStream.write( + self.output.write(`${self._initialPrompt}.editor\n`); + self.output.write( '// Entering editor mode (^D to finish, ^C to cancel)\n'); - self.outputStream.write(`${self[kBufferedCommandSymbol]}\n`); + self.output.write(`${self[kBufferedCommandSymbol]}\n`); self.prompt(true); } else { self.displayPrompt(true); @@ -957,7 +989,7 @@ REPLServer.prototype.createContext = function() { } } context.global = context; - const _console = new Console(this.outputStream); + const _console = new Console(this.output); ObjectDefineProperty(context, 'console', { configurable: true, writable: true, @@ -998,7 +1030,7 @@ REPLServer.prototype.resetContext = function() { this.last = value; if (!this.underscoreAssigned) { this.underscoreAssigned = true; - this.outputStream.write('Expression assignment to _ now disabled.\n'); + this.output.write('Expression assignment to _ now disabled.\n'); } } }); @@ -1010,7 +1042,7 @@ REPLServer.prototype.resetContext = function() { this.lastError = value; if (!this.underscoreErrAssigned) { this.underscoreErrAssigned = true; - this.outputStream.write( + this.output.write( 'Expression assignment to _error now disabled.\n'); } } @@ -1495,7 +1527,7 @@ function defineDefaultCommands(repl) { action: function() { this.clearBufferedCommand(); if (!this.useGlobal) { - this.outputStream.write('Clearing context...\n'); + this.output.write('Clearing context...\n'); this.resetContext(); } this.displayPrompt(); @@ -1522,9 +1554,9 @@ function defineDefaultCommands(repl) { const cmd = this.commands[name]; const spaces = ' '.repeat(longestNameLength - name.length + 3); const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`; - this.outputStream.write(line); + this.output.write(line); } - this.outputStream.write('\nPress ^C to abort current expression, ' + + this.output.write('\nPress ^C to abort current expression, ' + '^D to exit the repl\n'); this.displayPrompt(); } @@ -1535,9 +1567,9 @@ function defineDefaultCommands(repl) { action: function(file) { try { fs.writeFileSync(file, this.lines.join('\n')); - this.outputStream.write(`Session saved to: ${file}\n`); + this.output.write(`Session saved to: ${file}\n`); } catch { - this.outputStream.write(`Failed to save: ${file}\n`); + this.output.write(`Failed to save: ${file}\n`); } this.displayPrompt(); } @@ -1555,12 +1587,12 @@ function defineDefaultCommands(repl) { _turnOffEditorMode(this); this.write('\n'); } else { - this.outputStream.write( + this.output.write( `Failed to load: ${file} is not a valid file\n` ); } } catch { - this.outputStream.write(`Failed to load: ${file}\n`); + this.output.write(`Failed to load: ${file}\n`); } this.displayPrompt(); } @@ -1570,7 +1602,7 @@ function defineDefaultCommands(repl) { help: 'Enter editor mode', action() { _turnOnEditorMode(this); - this.outputStream.write( + this.output.write( '// Entering editor mode (^D to finish, ^C to cancel)\n'); } }); diff --git a/test/parallel/test-repl-history-navigation.js b/test/parallel/test-repl-history-navigation.js index 275e92821a6bbf..9155ad722b8710 100644 --- a/test/parallel/test-repl-history-navigation.js +++ b/test/parallel/test-repl-history-navigation.js @@ -15,6 +15,8 @@ common.skipIfDumbTerminal(); const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); +process.throwDeprecation = true; + const defaultHistoryPath = path.join(tmpdir.path, '.node_repl_history'); // Create an input stream specialized for testing an array of actions diff --git a/test/parallel/test-repl-options.js b/test/parallel/test-repl-options.js index 99f9fa8a605142..a9d3315eb317f5 100644 --- a/test/parallel/test-repl-options.js +++ b/test/parallel/test-repl-options.js @@ -19,6 +19,8 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +// Flags: --pending-deprecation + 'use strict'; const common = require('../common'); const ArrayStream = require('../common/arraystream'); @@ -30,7 +32,9 @@ assert.strictEqual(repl.repl, undefined); common.expectWarning({ DeprecationWarning: { - DEP0124: 'REPLServer.rli is deprecated' + DEP0XXX: 'repl.inputStream and repl.outputStream is deprecated. ' + + 'Use repl.input and repl.output instead', + DEP0124: 'REPLServer.rli is deprecated', } });