-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Open
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.
Description
What is the problem this feature will solve?
Here is a typical code to accepts multiple lines from stdin:
import { stdin, stderr } from "node:process";
import { createInterface } from "node:readline/promises";
const rl = createInterface({ input: stdin, output: stderr });
for await (const line of rl) {
// process line
}
console.log("Done");However, if you press Ctrl + C, it is treated as Ctrl + D—i.e. "Done" is displayed and you cannot cancel the multiline input. It is a ridiculous behavior.
await rl.question() throws AbortError and messes up your terminal:
$ node ./test.mjs
node:internal/readline/interface:1331
this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+C'));
^
AbortError: Aborted with Ctrl+C
at [_ttyWrite] (node:internal/readline/interface:1331:37)
at ReadStream.onkeypress (node:internal/readline/interface:284:20)
at ReadStream.emit (node:events:508:28)
at emitKeys (node:internal/readline/utils:371:14)
at emitKeys.next (<anonymous>)
at ReadStream.onData (node:internal/readline/emitKeypressEvents:64:36)
at ReadStream.emit (node:events:508:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5) {
code: 'ABORT_ERR'
}
It is not sophisticated.
What is the feature you are proposing to solve the problem?
Add an option to creatInterface to prevent it from trapping SIGINT by default.
const rl = createInterface({ input: stdin, output: stderr, noTrapSigInt: true });With this, you will be able to press Ctrl + C to terminate the program immediately with the proper exit code.
What alternatives have you considered?
import { platform } from "node:process";
rl.on("SIGINT", () => {
process.exit(platform === "win32" ? 2 : 130);
});Why do I have to add such a code?
Metadata
Metadata
Assignees
Labels
feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.
Type
Projects
Status
Awaiting Triage