Skip to content

Commit 481a959

Browse files
authored
readline: remove question method from InterfaceConstructor
That method is overwritten in both `require('node:readline').Interface.prototype` and `require('node:readline/promises').Interface.prototype`, and is very much not useful outside of interacting with TTY, removing it from the parent class could enable the use of `InterfaceConstructor` in other contexts (such as interacting with files). PR-URL: #44606 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 5ec2c99 commit 481a959

File tree

4 files changed

+8
-61
lines changed

4 files changed

+8
-61
lines changed

doc/api/readline.md

-55
Original file line numberDiff line numberDiff line change
@@ -303,61 +303,6 @@ paused.
303303
If the `InterfaceConstructor` was created with `output` set to `null` or
304304
`undefined` the prompt is not written.
305305

306-
### `rl.question(query[, options], callback)`
307-
308-
<!-- YAML
309-
added: v0.3.3
310-
-->
311-
312-
* `query` {string} A statement or query to write to `output`, prepended to the
313-
prompt.
314-
* `options` {Object}
315-
* `signal` {AbortSignal} Optionally allows the `question()` to be canceled
316-
using an `AbortController`.
317-
* `callback` {Function} A callback function that is invoked with the user's
318-
input in response to the `query`.
319-
320-
The `rl.question()` method displays the `query` by writing it to the `output`,
321-
waits for user input to be provided on `input`, then invokes the `callback`
322-
function passing the provided input as the first argument.
323-
324-
When called, `rl.question()` will resume the `input` stream if it has been
325-
paused.
326-
327-
If the `InterfaceConstructor` was created with `output` set to `null` or
328-
`undefined` the `query` is not written.
329-
330-
The `callback` function passed to `rl.question()` does not follow the typical
331-
pattern of accepting an `Error` object or `null` as the first argument.
332-
The `callback` is called with the provided answer as the only argument.
333-
334-
An error will be thrown if calling `rl.question()` after `rl.close()`.
335-
336-
Example usage:
337-
338-
```js
339-
rl.question('What is your favorite food? ', (answer) => {
340-
console.log(`Oh, so your favorite food is ${answer}`);
341-
});
342-
```
343-
344-
Using an `AbortController` to cancel a question.
345-
346-
```js
347-
const ac = new AbortController();
348-
const signal = ac.signal;
349-
350-
rl.question('What is your favorite food? ', { signal }, (answer) => {
351-
console.log(`Oh, so your favorite food is ${answer}`);
352-
});
353-
354-
signal.addEventListener('abort', () => {
355-
console.log('The food question timed out');
356-
}, { once: true });
357-
358-
setTimeout(() => ac.abort(), 10000);
359-
```
360-
361306
### `rl.resume()`
362307

363308
<!-- YAML

lib/internal/readline/interface.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const lineEnding = /\r?\n|\r(?!\n)/;
8181

8282
const kLineObjectStream = Symbol('line object stream');
8383
const kQuestionCancel = Symbol('kQuestionCancel');
84+
const kQuestion = Symbol('kQuestion');
8485

8586
// GNU readline library - keyseq-timeout is 500ms (default)
8687
const ESCAPE_CODE_TIMEOUT = 500;
@@ -401,7 +402,7 @@ class Interface extends InterfaceConstructor {
401402
}
402403
}
403404

404-
question(query, cb) {
405+
[kQuestion](query, cb) {
405406
if (this.closed) {
406407
throw new ERR_USE_AFTER_CLOSE('readline');
407408
}
@@ -1405,6 +1406,7 @@ module.exports = {
14051406
kOnLine,
14061407
kPreviousKey,
14071408
kPrompt,
1409+
kQuestion,
14081410
kQuestionCallback,
14091411
kQuestionCancel,
14101412
kRefreshLine,

lib/readline.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const {
8181
kOnLine,
8282
kPreviousKey,
8383
kPrompt,
84+
kQuestion,
8485
kQuestionCallback,
8586
kQuestionCancel,
8687
kRefreshLine,
@@ -120,16 +121,14 @@ function Interface(input, output, completer, terminal) {
120121
ObjectSetPrototypeOf(Interface.prototype, _Interface.prototype);
121122
ObjectSetPrototypeOf(Interface, _Interface);
122123

123-
const superQuestion = _Interface.prototype.question;
124-
125124
/**
126125
* Displays `query` by writing it to the `output`.
127126
* @param {string} query
128127
* @param {{ signal?: AbortSignal; }} [options]
129128
* @param {Function} cb
130129
* @returns {void}
131130
*/
132-
Interface.prototype.question = function(query, options, cb) {
131+
Interface.prototype.question = function question(query, options, cb) {
133132
cb = typeof options === 'function' ? options : cb;
134133
if (options === null || typeof options !== 'object') {
135134
options = kEmptyObject;
@@ -156,7 +155,7 @@ Interface.prototype.question = function(query, options, cb) {
156155
}
157156

158157
if (typeof cb === 'function') {
159-
FunctionPrototypeCall(superQuestion, this, query, cb);
158+
this[kQuestion](query, cb);
160159
}
161160
};
162161
Interface.prototype.question[promisify.custom] = function question(query, options) {

lib/readline/promises.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010

1111
const {
1212
Interface: _Interface,
13+
kQuestion,
1314
kQuestionCancel,
1415
} = require('internal/readline/interface');
1516

@@ -49,7 +50,7 @@ class Interface extends _Interface {
4950
};
5051
}
5152

52-
super.question(query, cb);
53+
this[kQuestion](query, cb);
5354
});
5455
}
5556
}

0 commit comments

Comments
 (0)