mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
readline: fix pre-aborted signal question handling
fix pre-aborted question handling PR-URL: https://github.com/nodejs/node/pull/37929 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
54c525ef0e
commit
dbab2893ef
2 files changed
+34
No files matched your search
@@ -59,6 +59,7 @@ const {
|
||||
StringPrototypeStartsWith,
|
||||
StringPrototypeTrim,
|
||||
Promise,
|
||||
PromiseReject,
|
||||
Symbol,
|
||||
SymbolAsyncIterator,
|
||||
SafeStringIterator,
|
||||
@@ -393,6 +394,10 @@ Interface.prototype.question = function(query, options, cb) {
|
||||
options = typeof options === 'object' && options !== null ? options : {};
|
||||
|
||||
if (options.signal) {
|
||||
if (options.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
options.signal.addEventListener('abort', () => {
|
||||
this[kQuestionCancel]();
|
||||
}, { once: true });
|
||||
@@ -413,6 +418,10 @@ Interface.prototype.question = function(query, options, cb) {
|
||||
Interface.prototype.question[promisify.custom] = function(query, options) {
|
||||
options = typeof options === 'object' && options !== null ? options : {};
|
||||
|
||||
if (options.signal && options.signal.aborted) {
|
||||
return PromiseReject(new AbortError());
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.question(query, options, resolve);
|
||||
|
||||
|
||||
@@ -968,6 +968,31 @@ for (let i = 0; i < 12; i++) {
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// pre-aborted signal
|
||||
{
|
||||
const signal = AbortSignal.abort();
|
||||
const [rli] = getInterface({ terminal });
|
||||
rli.pause();
|
||||
rli.on('resume', common.mustNotCall());
|
||||
rli.question('hello?', { signal }, common.mustNotCall());
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// pre-aborted signal promisified question
|
||||
{
|
||||
const signal = AbortSignal.abort();
|
||||
const [rli] = getInterface({ terminal });
|
||||
const question = util.promisify(rli.question).bind(rli);
|
||||
rli.on('resume', common.mustNotCall());
|
||||
rli.pause();
|
||||
question('hello?', { signal })
|
||||
.then(common.mustNotCall())
|
||||
.catch(common.mustCall((error) => {
|
||||
assert.strictEqual(error.name, 'AbortError');
|
||||
}));
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// Can create a new readline Interface with a null output argument
|
||||
{
|
||||
const [rli, fi] = getInterface({ output: null, terminal });
|
||||
|
||||
Reference in new issue
Block a user