Skip to content

Commit

Permalink
readline: fix pre-aborted signal question handling
Browse files Browse the repository at this point in the history
fix pre-aborted question handling
  • Loading branch information
Linkgoron committed Mar 26, 2021
1 parent a9cdeed commit a39a0e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,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 });
Expand All @@ -397,8 +401,11 @@ Interface.prototype.question[promisify.custom] = function(query, options) {
options = typeof options === 'object' && options !== null ? options : {};

return new Promise((resolve, reject) => {
this.question(query, options, resolve);
if (options.signal?.aborted) {
throw new AbortError();
}

this.question(query, options, resolve);
if (options.signal) {
options.signal.addEventListener('abort', () => {
reject(new AbortError());
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit a39a0e7

Please sign in to comment.