From 80750e0147c662b1d0c2949d6bae0d466b3c98a0 Mon Sep 17 00:00:00 2001 From: Daniel Venable Date: Thu, 25 Jul 2024 02:01:26 +0000 Subject: [PATCH 1/3] readline: fix unresolved promise on abortion Fixes: https://github.com/nodejs/node/issues/53497 --- lib/internal/readline/interface.js | 7 +++++++ lib/readline/promises.js | 3 +++ .../parallel/test-readline-promises-interface.js | 16 ++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index bd63420a1ea3f7..e3c362eccbed74 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -66,6 +66,9 @@ const { cursorTo, moveCursor, } = require('internal/readline/callbacks'); +const { + AbortError +} = require('internal/errors'); const { StringDecoder } = require('string_decoder'); @@ -111,6 +114,7 @@ const kPrompt = Symbol('_prompt'); const kPushToKillRing = Symbol('_pushToKillRing'); const kPushToUndoStack = Symbol('_pushToUndoStack'); const kQuestionCallback = Symbol('_questionCallback'); +const kQuestionReject = Symbol('_questionReject'); const kRedo = Symbol('_redo'); const kRedoStack = Symbol('_redoStack'); const kRefreshLine = Symbol('_refreshLine'); @@ -1126,6 +1130,7 @@ class Interface extends InterfaceConstructor { } else { // This readline instance is finished this.close(); + this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+C')); } break; @@ -1137,6 +1142,7 @@ class Interface extends InterfaceConstructor { if (this.cursor === 0 && this.line.length === 0) { // This readline instance is finished this.close(); + this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+D')); } else if (this.cursor < this.line.length) { this[kDeleteRight](); } @@ -1392,6 +1398,7 @@ module.exports = { kQuestion, kQuestionCallback, kQuestionCancel, + kQuestionReject, kRefreshLine, kSawKeyPress, kSawReturnAt, diff --git a/lib/readline/promises.js b/lib/readline/promises.js index ccd0745a33f941..a4ad0adabaf228 100644 --- a/lib/readline/promises.js +++ b/lib/readline/promises.js @@ -13,6 +13,7 @@ const { Interface: _Interface, kQuestion, kQuestionCancel, + kQuestionReject, } = require('internal/readline/interface'); const { @@ -54,6 +55,8 @@ class Interface extends _Interface { }; } + this[kQuestionReject] = reject; + this[kQuestion](query, cb); }); } diff --git a/test/parallel/test-readline-promises-interface.js b/test/parallel/test-readline-promises-interface.js index 97424c1372629c..6c9629bf819a66 100644 --- a/test/parallel/test-readline-promises-interface.js +++ b/test/parallel/test-readline-promises-interface.js @@ -951,6 +951,22 @@ for (let i = 0; i < 12; i++) { rli.close(); } + // Aborting a question with ctrl+D + { + const [rli, fi] = getInterface({ terminal: true }); + assert.rejects(rli.question('hello?'), { name: 'AbortError' }) + .then(common.mustCall()); + fi.emit('keypress', '.', { ctrl: true, name: 'd' }); + } + + // Aborting a question with ctrl+D + { + const [rli, fi] = getInterface({ terminal: true }); + assert.rejects(rli.question('hello?'), { name: 'AbortError' }) + .then(common.mustCall()); + fi.emit('keypress', '.', { ctrl: true, name: 'd' }); + } + (async () => { const [rli] = getInterface({ terminal }); const signal = AbortSignal.abort('boom'); From cab60dc7aef029476add1793b4f2d8d7222f0f63 Mon Sep 17 00:00:00 2001 From: Daniel Venable Date: Thu, 25 Jul 2024 22:18:43 +0000 Subject: [PATCH 2/3] readline: fix js-lint errors --- lib/internal/readline/interface.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index e3c362eccbed74..aa2f46123a9e55 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -33,10 +33,13 @@ const { SymbolDispose, } = primordials; -const { codes: { - ERR_INVALID_ARG_VALUE, - ERR_USE_AFTER_CLOSE, -} } = require('internal/errors'); +const { + AbortError, + codes: { + ERR_INVALID_ARG_VALUE, + ERR_USE_AFTER_CLOSE, + }, +} = require('internal/errors'); const { validateAbortSignal, @@ -66,9 +69,6 @@ const { cursorTo, moveCursor, } = require('internal/readline/callbacks'); -const { - AbortError -} = require('internal/errors'); const { StringDecoder } = require('string_decoder'); From 30387cf01b5f064deb7b5187b7b5f4043c9603b0 Mon Sep 17 00:00:00 2001 From: Daniel Venable Date: Sat, 25 Jan 2025 19:48:09 +0000 Subject: [PATCH 3/3] readline: fix test so it tests aborting a question with both ctrl+C and ctrl+D --- test/parallel/test-readline-promises-interface.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-readline-promises-interface.js b/test/parallel/test-readline-promises-interface.js index 6c9629bf819a66..32aab1b60c2ee5 100644 --- a/test/parallel/test-readline-promises-interface.js +++ b/test/parallel/test-readline-promises-interface.js @@ -951,12 +951,12 @@ for (let i = 0; i < 12; i++) { rli.close(); } - // Aborting a question with ctrl+D + // Aborting a question with ctrl+C { const [rli, fi] = getInterface({ terminal: true }); assert.rejects(rli.question('hello?'), { name: 'AbortError' }) .then(common.mustCall()); - fi.emit('keypress', '.', { ctrl: true, name: 'd' }); + fi.emit('keypress', '.', { ctrl: true, name: 'c' }); } // Aborting a question with ctrl+D