Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: remove question method from InterfaceConstructor #44606

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -401,7 +402,7 @@ class Interface extends InterfaceConstructor {
}
}

question(query, cb) {
[kQuestion](query, cb) {
if (this.closed) {
throw new ERR_USE_AFTER_CLOSE('readline');
}
@@ -1405,6 +1406,7 @@ module.exports = {
kOnLine,
kPreviousKey,
kPrompt,
kQuestion,
kQuestionCallback,
kQuestionCancel,
kRefreshLine,
7 changes: 3 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@ const {
kOnLine,
kPreviousKey,
kPrompt,
kQuestion,
kQuestionCallback,
kQuestionCancel,
kRefreshLine,
@@ -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;
@@ -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) {
3 changes: 2 additions & 1 deletion lib/readline/promises.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ const {

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

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

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