Skip to content

Commit

Permalink
repl: deprecate repl.inputStream and repl.outputStream
Browse files Browse the repository at this point in the history
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 <[email protected]>

PR-URL: #33294
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and targos committed May 16, 2020
1 parent 2b50cd7 commit e11b5d3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 27 deletions.
14 changes: 14 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,20 @@ Type: Documentation-only
Use [`request.destroy()`][] instead of [`request.abort()`][].
<a id="DEP0XXX"></a>
### DEP0XXX: `repl.inputStream` and `repl.outputStream`
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33294
description: Documentation-only (supports [`--pending-deprecation`][]).
-->
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
Expand Down
84 changes: 58 additions & 26 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
}
}
});
Expand All @@ -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');
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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');
}
});
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-repl-history-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
}
});

Expand Down

0 comments on commit e11b5d3

Please sign in to comment.