nodejs / nodejs/node

Add an option not to trap SIGINT (Ctrl + C) to `readline.creatInterface`

Aperta
#61,487 11 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature request
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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" ? -1073741510 : 130);
});

Why do I have to add such a code? Who the hell can remember such numbers for both platforms?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal punto di ingresso createInterface mostrato nell’issue e traccia come viene gestito Ctrl+C durante l’iterazione multilinea e i prompt delle domande. Verifica il comportamento dell’opzione richiesta sulle piattaforme descritte, quindi aggiungi test e documentazione che mostrino che Ctrl+C può terminare normalmente senza l’attuale comportamento di intercettazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, nodejs
Ambito
cli
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.