Skip to content

Commit

Permalink
readline: remove question method from InterfaceConstructor
Browse files Browse the repository at this point in the history
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 <joyeec9h3@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
aduh95 committed Sep 14, 2022
1 parent 5ec2c99 commit 481a959
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 61 deletions.
55 changes: 0 additions & 55 deletions doc/api/readline.md
Expand Up @@ -303,61 +303,6 @@ paused.
If the `InterfaceConstructor` was created with `output` set to `null` or
`undefined` the prompt is not written.

### `rl.question(query[, options], callback)`

<!-- YAML
added: v0.3.3
-->

* `query` {string} A statement or query to write to `output`, prepended to the
prompt.
* `options` {Object}
* `signal` {AbortSignal} Optionally allows the `question()` to be canceled
using an `AbortController`.
* `callback` {Function} A callback function that is invoked with the user's
input in response to the `query`.

The `rl.question()` method displays the `query` by writing it to the `output`,
waits for user input to be provided on `input`, then invokes the `callback`
function passing the provided input as the first argument.

When called, `rl.question()` will resume the `input` stream if it has been
paused.

If the `InterfaceConstructor` was created with `output` set to `null` or
`undefined` the `query` is not written.

The `callback` function passed to `rl.question()` does not follow the typical
pattern of accepting an `Error` object or `null` as the first argument.
The `callback` is called with the provided answer as the only argument.

An error will be thrown if calling `rl.question()` after `rl.close()`.

Example usage:

```js
rl.question('What is your favorite food? ', (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});
```

Using an `AbortController` to cancel a question.

```js
const ac = new AbortController();
const signal = ac.signal;

rl.question('What is your favorite food? ', { signal }, (answer) => {
console.log(`Oh, so your favorite food is ${answer}`);
});

signal.addEventListener('abort', () => {
console.log('The food question timed out');
}, { once: true });

setTimeout(() => ac.abort(), 10000);
```

### `rl.resume()`

<!-- YAML
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/readline/interface.js
Expand Up @@ -81,6 +81,7 @@ const lineEnding = /\r?\n|\r(?!\n)/;

const kLineObjectStream = Symbol('line object stream');
const kQuestionCancel = Symbol('kQuestionCancel');
const kQuestion = Symbol('kQuestion');

// GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500;
Expand Down Expand Up @@ -401,7 +402,7 @@ class Interface extends InterfaceConstructor {
}
}

question(query, cb) {
[kQuestion](query, cb) {
if (this.closed) {
throw new ERR_USE_AFTER_CLOSE('readline');
}
Expand Down Expand Up @@ -1405,6 +1406,7 @@ module.exports = {
kOnLine,
kPreviousKey,
kPrompt,
kQuestion,
kQuestionCallback,
kQuestionCancel,
kRefreshLine,
Expand Down
7 changes: 3 additions & 4 deletions lib/readline.js
Expand Up @@ -81,6 +81,7 @@ const {
kOnLine,
kPreviousKey,
kPrompt,
kQuestion,
kQuestionCallback,
kQuestionCancel,
kRefreshLine,
Expand Down Expand Up @@ -120,16 +121,14 @@ function Interface(input, output, completer, terminal) {
ObjectSetPrototypeOf(Interface.prototype, _Interface.prototype);
ObjectSetPrototypeOf(Interface, _Interface);

const superQuestion = _Interface.prototype.question;

/**
* Displays `query` by writing it to the `output`.
* @param {string} query
* @param {{ signal?: AbortSignal; }} [options]
* @param {Function} cb
* @returns {void}
*/
Interface.prototype.question = function(query, options, cb) {
Interface.prototype.question = function question(query, options, cb) {
cb = typeof options === 'function' ? options : cb;
if (options === null || typeof options !== 'object') {
options = kEmptyObject;
Expand All @@ -156,7 +155,7 @@ Interface.prototype.question = function(query, options, cb) {
}

if (typeof cb === 'function') {
FunctionPrototypeCall(superQuestion, this, query, cb);
this[kQuestion](query, cb);
}
};
Interface.prototype.question[promisify.custom] = function question(query, options) {
Expand Down
3 changes: 2 additions & 1 deletion lib/readline/promises.js
Expand Up @@ -10,6 +10,7 @@ const {

const {
Interface: _Interface,
kQuestion,
kQuestionCancel,
} = require('internal/readline/interface');

Expand Down Expand Up @@ -49,7 +50,7 @@ class Interface extends _Interface {
};
}

super.question(query, cb);
this[kQuestion](query, cb);
});
}
}
Expand Down

0 comments on commit 481a959

Please sign in to comment.