◐ Shell
clean mode source ↗

readline: fix unresolved promise on abortion · nodejs/node@dfc61f7

Original file line numberDiff line numberDiff line change

@@ -33,10 +33,13 @@ const {

3333

SymbolDispose,

3434

} = primordials;

3535
36-

const { codes: {

37-

ERR_INVALID_ARG_VALUE,

38-

ERR_USE_AFTER_CLOSE,

39-

} } = require('internal/errors');

36+

const {

37+

AbortError,

38+

codes: {

39+

ERR_INVALID_ARG_VALUE,

40+

ERR_USE_AFTER_CLOSE,

41+

},

42+

} = require('internal/errors');

4043
4144

const {

4245

validateAbortSignal,

@@ -111,6 +114,7 @@ const kPrompt = Symbol('_prompt');

111114

const kPushToKillRing = Symbol('_pushToKillRing');

112115

const kPushToUndoStack = Symbol('_pushToUndoStack');

113116

const kQuestionCallback = Symbol('_questionCallback');

117+

const kQuestionReject = Symbol('_questionReject');

114118

const kRedo = Symbol('_redo');

115119

const kRedoStack = Symbol('_redoStack');

116120

const kRefreshLine = Symbol('_refreshLine');

@@ -1126,6 +1130,7 @@ class Interface extends InterfaceConstructor {

11261130

} else {

11271131

// This readline instance is finished

11281132

this.close();

1133+

this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+C'));

11291134

}

11301135

break;

11311136

@@ -1137,6 +1142,7 @@ class Interface extends InterfaceConstructor {

11371142

if (this.cursor === 0 && this.line.length === 0) {

11381143

// This readline instance is finished

11391144

this.close();

1145+

this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+D'));

11401146

} else if (this.cursor < this.line.length) {

11411147

this[kDeleteRight]();

11421148

}

@@ -1392,6 +1398,7 @@ module.exports = {

13921398

kQuestion,

13931399

kQuestionCallback,

13941400

kQuestionCancel,

1401+

kQuestionReject,

13951402

kRefreshLine,

13961403

kSawKeyPress,

13971404

kSawReturnAt,

Original file line numberDiff line numberDiff line change

@@ -13,6 +13,7 @@ const {

1313

Interface: _Interface,

1414

kQuestion,

1515

kQuestionCancel,

16+

kQuestionReject,

1617

} = require('internal/readline/interface');

1718
1819

const {

@@ -54,6 +55,8 @@ class Interface extends _Interface {

5455

};

5556

}

5657
58+

this[kQuestionReject] = reject;

59+
5760

this[kQuestion](query, cb);

5861

});

5962

}

Original file line numberDiff line numberDiff line change

@@ -951,6 +951,22 @@ for (let i = 0; i < 12; i++) {

951951

rli.close();

952952

}

953953
954+

// Aborting a question with ctrl+C

955+

{

956+

const [rli, fi] = getInterface({ terminal: true });

957+

assert.rejects(rli.question('hello?'), { name: 'AbortError' })

958+

.then(common.mustCall());

959+

fi.emit('keypress', '.', { ctrl: true, name: 'c' });

960+

}

961+
962+

// Aborting a question with ctrl+D

963+

{

964+

const [rli, fi] = getInterface({ terminal: true });

965+

assert.rejects(rli.question('hello?'), { name: 'AbortError' })

966+

.then(common.mustCall());

967+

fi.emit('keypress', '.', { ctrl: true, name: 'd' });

968+

}

969+
954970

(async () => {

955971

const [rli] = getInterface({ terminal });

956972

const signal = AbortSignal.abort('boom');