Files
bun.sh/test/js/node/process/stdin/pause.fixture.js
pfg 408fda7ad2 Continue emitting 'readable' events after pausing stdin (#17690)
Fixes #21189

`.pause()` should unref but it should still continue to emit `readable`
events (although it should not send `data` events)

also stdin.unref() should not pause input, it should only prevent stdin
from keeping the process alive.

DRAFT:

- [x] ~~this causes a bug where `process.stdin.on("readable", () => {});
process.stdin.pause()` will allow the process to exit when it
shouldn't.~~ fixed

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-08-04 21:04:08 -07:00

36 lines
942 B
JavaScript

console.log("%READY%");
const r = () => {
let chunk;
if (ceaseReading) return;
while ((chunk = process.stdin.read()) !== null) {
console.log("got readable", JSON.stringify(chunk.toString()));
}
};
let ceaseReading = false;
process.stdin.on("data", data => {
const dataString = data.toString().trim();
console.log("got stdin", JSON.stringify(dataString));
if (dataString === "pause") {
process.stdin.pause();
} else if (dataString === "attachReadable") {
process.stdin.on("readable", r);
} else if (dataString === "detachReadable") {
process.stdin.off("readable", r);
return;
} else if (dataString === "ceaseReading") {
ceaseReading = true;
} else if (dataString === "exit") {
process.exit(123);
}
console.log("%READY%");
});
process.on("beforeExit", code => {
console.log("beforeExit with code " + code);
});
process.on("exit", code => {
console.log("exit with code " + code);
});