1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00
denoland-deno/tests/testdata/run/process_stdin_unblock.mjs
Divy Srivastava a947c6fbf7
fix(ext/node): windows cancel stdin read in line mode (#23969)
This patch fixes stdin read hanging on user input when switching tty
mode on Windows

Fixes #21111

On Windows, when switching from line to raw mode:
- Cancel ongoing console read by writing a return keypress to its input
buffer. This blocks the main thread until any ongoing read has been
cancelled to prevent interference with the screen state.
- On the read thread, restore the cursor position to where it was before
writing the enter, undoing its effect on the screen state.
- Restart reading and notify the main thread.
2024-05-29 22:23:04 +05:30

21 lines
417 B
JavaScript

import process from "node:process";
function prompt() {
process.stdin.setRawMode(true);
const { promise, resolve } = Promise.withResolvers();
const onData = (buf) => {
process.stdin.setRawMode(false);
process.stdin.removeListener("data", onData);
console.log(buf.length);
resolve();
};
process.stdin.on("data", onData);
return promise;
}
await prompt();
await prompt();
Deno.exit(0);