mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 01:35:51 +00:00

Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/41008 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
Promise,
|
|
} = primordials;
|
|
|
|
const {
|
|
Readline,
|
|
} = require('internal/readline/promises');
|
|
|
|
const {
|
|
Interface: _Interface,
|
|
kQuestionCancel,
|
|
} = require('internal/readline/interface');
|
|
|
|
const {
|
|
AbortError,
|
|
} = require('internal/errors');
|
|
const { validateAbortSignal } = require('internal/validators');
|
|
|
|
class Interface extends _Interface {
|
|
// eslint-disable-next-line no-useless-constructor
|
|
constructor(input, output, completer, terminal) {
|
|
super(input, output, completer, terminal);
|
|
}
|
|
question(query, options = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
let cb = resolve;
|
|
|
|
if (options?.signal) {
|
|
validateAbortSignal(options.signal, 'options.signal');
|
|
if (options.signal.aborted) {
|
|
return reject(
|
|
new AbortError(undefined, { cause: options.signal.reason }));
|
|
}
|
|
|
|
const onAbort = () => {
|
|
this[kQuestionCancel]();
|
|
reject(new AbortError(undefined, { cause: options.signal.reason }));
|
|
};
|
|
options.signal.addEventListener('abort', onAbort, { once: true });
|
|
cb = (answer) => {
|
|
options.signal.removeEventListener('abort', onAbort);
|
|
resolve(answer);
|
|
};
|
|
}
|
|
|
|
super.question(query, cb);
|
|
});
|
|
}
|
|
}
|
|
|
|
function createInterface(input, output, completer, terminal) {
|
|
return new Interface(input, output, completer, terminal);
|
|
}
|
|
|
|
module.exports = {
|
|
Interface,
|
|
Readline,
|
|
createInterface,
|
|
};
|