readline: fix question stack overflow

This commit fixes readline interface's question callback wrapping when
it is being called with `signal` option. Previous version completely
overwrites passed callback and throws "Maximum call stack size exceeded"
error.

PR-URL: https://github.com/nodejs/node/pull/43320
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Eugene Chapko authored and Danielle Adams committed 2022-06-12 20:24:57 -04:00
1 parent 112518fb1d
commit 7e8a00a26d
3 files changed
+37 -1

No files matched your search

+2 -1
View File
@@ -148,9 +148,10 @@ Interface.prototype.question = function(query, options, cb) {
const cleanup = () => {
options.signal.removeEventListener(onAbort);
};
const originalCb = cb;
cb = typeof cb === 'function' ? (answer) => {
cleanup();
return cb(answer);
return originalCb(answer);
} : cleanup;
}
+24
View File
@@ -1006,6 +1006,17 @@ for (let i = 0; i < 12; i++) {
rli.close();
}
// Calling the question callback with abort signal
{
const [rli] = getInterface({ terminal });
const { signal } = new AbortController();
rli.question('foo?', { signal }, common.mustCall((answer) => {
assert.strictEqual(answer, 'bar');
}));
rli.write('bar\n');
rli.close();
}
// Calling the question multiple times
{
const [rli] = getInterface({ terminal });
@@ -1030,6 +1041,19 @@ for (let i = 0; i < 12; i++) {
rli.close();
}
// Calling the promisified question with abort signal
{
const [rli] = getInterface({ terminal });
const question = util.promisify(rli.question).bind(rli);
const { signal } = new AbortController();
question('foo?', { signal })
.then(common.mustCall((answer) => {
assert.strictEqual(answer, 'bar');
}));
rli.write('bar\n');
rli.close();
}
// Aborting a question
{
const ac = new AbortController();
@@ -909,6 +909,17 @@ for (let i = 0; i < 12; i++) {
rli.close();
}
// Calling the question callback with abort signal
{
const [rli] = getInterface({ terminal });
const { signal } = new AbortController();
rli.question('foo?', { signal }).then(common.mustCall((answer) => {
assert.strictEqual(answer, 'bar');
}));
rli.write('bar\n');
rli.close();
}
// Aborting a question
{
const ac = new AbortController();