nodejs / nodejs/node

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

Abierto
#61,487 11 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature request
Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.3k
Merge medio
4 d 2 h
PR fusionados (30 d)
283

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en el punto de entrada createInterface mostrado en el issue y sigue cómo se gestiona Ctrl+C durante la iteración multilínea y las indicaciones de preguntas. Verifica el comportamiento de la opción solicitada en las plataformas descritas y añade cobertura y documentación que muestren que Ctrl+C puede terminar normalmente sin el comportamiento actual de captura.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, nodejs
Área
cli
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.