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:
Nitzan Uziely 2021-03-26 17:07:20 +03:00 committed by James M Snell
parent 54c525ef0e
commit dbab2893ef
No known key found for this signature in database
GPG key ID: 7341B15C070877AC
2 changed files with 34 additions and 0 deletions

View file

@ -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);