Skip to content

Commit db7bb94

Browse files
BridgeARcodebytere
authored andcommitted
repl: deprecate repl.inputStream and repl.outputStream
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]>
1 parent 9119344 commit db7bb94

File tree

4 files changed

+79
-27
lines changed

4 files changed

+79
-27
lines changed

doc/api/deprecations.md

+14
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,20 @@ Type: Documentation-only
26662666
26672667
Use [`request.destroy()`][] instead of [`request.abort()`][].
26682668
2669+
<a id="DEP0XXX"></a>
2670+
### DEP0XXX: `repl.inputStream` and `repl.outputStream`
2671+
<!-- YAML
2672+
changes:
2673+
- version: REPLACEME
2674+
pr-url: https://github.com/nodejs/node/pull/33294
2675+
description: Documentation-only (supports [`--pending-deprecation`][]).
2676+
-->
2677+
2678+
Type: Documentation-only (supports [`--pending-deprecation`][])
2679+
2680+
The `repl` module exported the input and output stream twice. Use `.input`
2681+
instead of `.inputStream` and `.output` instead of `.outputStream`.
2682+
26692683
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
26702684
[`--throw-deprecation`]: cli.html#cli_throw_deprecation
26712685
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size

lib/repl.js

+58-26
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ const {
100100
overrideStackTrace,
101101
} = require('internal/errors');
102102
const { sendInspectorCommand } = require('internal/util/inspector');
103-
const experimentalREPLAwait = require('internal/options').getOptionValue(
103+
const { getOptionValue } = require('internal/options');
104+
const experimentalREPLAwait = getOptionValue(
104105
'--experimental-repl-await'
105106
);
107+
const pendingDeprecation = getOptionValue('--pending-deprecation');
106108
const {
107109
REPL_MODE_SLOPPY,
108110
REPL_MODE_STRICT,
@@ -220,8 +222,39 @@ function REPLServer(prompt,
220222
const preview = options.terminal &&
221223
(options.preview !== undefined ? !!options.preview : !eval_);
222224

223-
this.inputStream = options.input;
224-
this.outputStream = options.output;
225+
ObjectDefineProperty(this, 'inputStream', {
226+
get: pendingDeprecation ?
227+
deprecate(() => this.input,
228+
'repl.inputStream and repl.outputStream is deprecated. ' +
229+
'Use repl.input and repl.output instead',
230+
'DEP0XXX') :
231+
() => this.input,
232+
set: pendingDeprecation ?
233+
deprecate((val) => this.input = val,
234+
'repl.inputStream and repl.outputStream is deprecated. ' +
235+
'Use repl.input and repl.output instead',
236+
'DEP0XXX') :
237+
(val) => this.input = val,
238+
enumerable: false,
239+
configurable: true
240+
});
241+
ObjectDefineProperty(this, 'outputStream', {
242+
get: pendingDeprecation ?
243+
deprecate(() => this.output,
244+
'repl.inputStream and repl.outputStream is deprecated. ' +
245+
'Use repl.input and repl.output instead',
246+
'DEP0XXX') :
247+
() => this.output,
248+
set: pendingDeprecation ?
249+
deprecate((val) => this.output = val,
250+
'repl.inputStream and repl.outputStream is deprecated. ' +
251+
'Use repl.input and repl.output instead',
252+
'DEP0XXX') :
253+
(val) => this.output = val,
254+
enumerable: false,
255+
configurable: true
256+
});
257+
225258
this.useColors = !!options.useColors;
226259
this._domain = options.domain || domain.create();
227260
this.useGlobal = !!useGlobal;
@@ -596,16 +629,13 @@ function REPLServer(prompt,
596629
}
597630
// Normalize line endings.
598631
errStack += errStack.endsWith('\n') ? '' : '\n';
599-
self.outputStream.write(errStack);
632+
self.output.write(errStack);
600633
self.clearBufferedCommand();
601634
self.lines.level = [];
602635
self.displayPrompt();
603636
}
604637
});
605638

606-
self.resetContext();
607-
self.lines.level = [];
608-
609639
self.clearBufferedCommand();
610640
ObjectDefineProperty(this, 'bufferedCommand', {
611641
get: deprecate(() => self[kBufferedCommandSymbol],
@@ -627,14 +657,16 @@ function REPLServer(prompt,
627657
}
628658

629659
Interface.call(this, {
630-
input: self.inputStream,
631-
output: self.outputStream,
660+
input: options.input,
661+
output: options.output,
632662
completer: self.completer,
633663
terminal: options.terminal,
634664
historySize: options.historySize,
635665
prompt
636666
});
637667

668+
self.resetContext();
669+
638670
this.commands = ObjectCreate(null);
639671
defineDefaultCommands(this);
640672

@@ -752,7 +784,7 @@ function REPLServer(prompt,
752784
return;
753785
}
754786
if (!self[kBufferedCommandSymbol]) {
755-
self.outputStream.write('Invalid REPL keyword\n');
787+
self.output.write('Invalid REPL keyword\n');
756788
finish(null);
757789
return;
758790
}
@@ -769,7 +801,7 @@ function REPLServer(prompt,
769801
_memory.call(self, cmd);
770802

771803
if (e && !self[kBufferedCommandSymbol] && cmd.trim().startsWith('npm ')) {
772-
self.outputStream.write('npm should be run outside of the ' +
804+
self.output.write('npm should be run outside of the ' +
773805
'node repl, in your normal shell.\n' +
774806
'(Press Control-D to exit.)\n');
775807
self.displayPrompt();
@@ -804,7 +836,7 @@ function REPLServer(prompt,
804836
if (!self.underscoreAssigned) {
805837
self.last = ret;
806838
}
807-
self.outputStream.write(self.writer(ret) + '\n');
839+
self.output.write(self.writer(ret) + '\n');
808840
}
809841

810842
// Display prompt again
@@ -814,10 +846,10 @@ function REPLServer(prompt,
814846

815847
self.on('SIGCONT', function onSigCont() {
816848
if (self.editorMode) {
817-
self.outputStream.write(`${self._initialPrompt}.editor\n`);
818-
self.outputStream.write(
849+
self.output.write(`${self._initialPrompt}.editor\n`);
850+
self.output.write(
819851
'// Entering editor mode (^D to finish, ^C to cancel)\n');
820-
self.outputStream.write(`${self[kBufferedCommandSymbol]}\n`);
852+
self.output.write(`${self[kBufferedCommandSymbol]}\n`);
821853
self.prompt(true);
822854
} else {
823855
self.displayPrompt(true);
@@ -957,7 +989,7 @@ REPLServer.prototype.createContext = function() {
957989
}
958990
}
959991
context.global = context;
960-
const _console = new Console(this.outputStream);
992+
const _console = new Console(this.output);
961993
ObjectDefineProperty(context, 'console', {
962994
configurable: true,
963995
writable: true,
@@ -998,7 +1030,7 @@ REPLServer.prototype.resetContext = function() {
9981030
this.last = value;
9991031
if (!this.underscoreAssigned) {
10001032
this.underscoreAssigned = true;
1001-
this.outputStream.write('Expression assignment to _ now disabled.\n');
1033+
this.output.write('Expression assignment to _ now disabled.\n');
10021034
}
10031035
}
10041036
});
@@ -1010,7 +1042,7 @@ REPLServer.prototype.resetContext = function() {
10101042
this.lastError = value;
10111043
if (!this.underscoreErrAssigned) {
10121044
this.underscoreErrAssigned = true;
1013-
this.outputStream.write(
1045+
this.output.write(
10141046
'Expression assignment to _error now disabled.\n');
10151047
}
10161048
}
@@ -1495,7 +1527,7 @@ function defineDefaultCommands(repl) {
14951527
action: function() {
14961528
this.clearBufferedCommand();
14971529
if (!this.useGlobal) {
1498-
this.outputStream.write('Clearing context...\n');
1530+
this.output.write('Clearing context...\n');
14991531
this.resetContext();
15001532
}
15011533
this.displayPrompt();
@@ -1522,9 +1554,9 @@ function defineDefaultCommands(repl) {
15221554
const cmd = this.commands[name];
15231555
const spaces = ' '.repeat(longestNameLength - name.length + 3);
15241556
const line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
1525-
this.outputStream.write(line);
1557+
this.output.write(line);
15261558
}
1527-
this.outputStream.write('\nPress ^C to abort current expression, ' +
1559+
this.output.write('\nPress ^C to abort current expression, ' +
15281560
'^D to exit the repl\n');
15291561
this.displayPrompt();
15301562
}
@@ -1535,9 +1567,9 @@ function defineDefaultCommands(repl) {
15351567
action: function(file) {
15361568
try {
15371569
fs.writeFileSync(file, this.lines.join('\n'));
1538-
this.outputStream.write(`Session saved to: ${file}\n`);
1570+
this.output.write(`Session saved to: ${file}\n`);
15391571
} catch {
1540-
this.outputStream.write(`Failed to save: ${file}\n`);
1572+
this.output.write(`Failed to save: ${file}\n`);
15411573
}
15421574
this.displayPrompt();
15431575
}
@@ -1555,12 +1587,12 @@ function defineDefaultCommands(repl) {
15551587
_turnOffEditorMode(this);
15561588
this.write('\n');
15571589
} else {
1558-
this.outputStream.write(
1590+
this.output.write(
15591591
`Failed to load: ${file} is not a valid file\n`
15601592
);
15611593
}
15621594
} catch {
1563-
this.outputStream.write(`Failed to load: ${file}\n`);
1595+
this.output.write(`Failed to load: ${file}\n`);
15641596
}
15651597
this.displayPrompt();
15661598
}
@@ -1570,7 +1602,7 @@ function defineDefaultCommands(repl) {
15701602
help: 'Enter editor mode',
15711603
action() {
15721604
_turnOnEditorMode(this);
1573-
this.outputStream.write(
1605+
this.output.write(
15741606
'// Entering editor mode (^D to finish, ^C to cancel)\n');
15751607
}
15761608
});

test/parallel/test-repl-history-navigation.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ common.skipIfDumbTerminal();
1515
const tmpdir = require('../common/tmpdir');
1616
tmpdir.refresh();
1717

18+
process.throwDeprecation = true;
19+
1820
const defaultHistoryPath = path.join(tmpdir.path, '.node_repl_history');
1921

2022
// Create an input stream specialized for testing an array of actions

test/parallel/test-repl-options.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// Flags: --pending-deprecation
23+
2224
'use strict';
2325
const common = require('../common');
2426
const ArrayStream = require('../common/arraystream');
@@ -30,7 +32,9 @@ assert.strictEqual(repl.repl, undefined);
3032

3133
common.expectWarning({
3234
DeprecationWarning: {
33-
DEP0124: 'REPLServer.rli is deprecated'
35+
DEP0XXX: 'repl.inputStream and repl.outputStream is deprecated. ' +
36+
'Use repl.input and repl.output instead',
37+
DEP0124: 'REPLServer.rli is deprecated',
3438
}
3539
});
3640

0 commit comments

Comments
 (0)