Skip to content

Commit 1de3cfe

Browse files
committed
Use file descriptor 0 to read stdin
On Linux, when Node.js (at least, v15.2.0) does a readFileSync of /dev/tty, it hangs. The addition of /dev/tty comes from 9bcf45b, which converts to ESM. It also switches from reading /dev/stdin, to: 1. on Windows, reading file descriptor 0; 2. on other platforms, reading /dev/tty (which presumably works on macOS, but not Linux). However, based on the [Node.js documentation][fd0], reading file descriptor 0 always reads stdin on all platforms, thanks to a compatibility layer in Node.js. Subtly, for Linux specifically, using /dev/stdin instead of the zero file descriptor, enables the following use-case: just enter `commonmark` on the console, causing it to wait for prompt. You can then type text followed by Control-D to have that text be the input. | File | Linux | macOS | Windows | |============|=================|=================|===========| | | Pipe | ✓ | ✓ | ✓ | | 0 |--------|-----------------|-----------------|-----------| | | Prompt | EAGAIN | EAGAIN | TypeError | |~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~|~~~~~~~~~~~| | | ✓ | ✓ | ENOENT | | /dev/stdin |-----------------|-----------------|-----------| | | ✓ | EAGAIN | ENOENT | |~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~|~~~~~~~~~~~| | | (input ignored) | (input ignored) | ENOENT | | /dev/tty |-----------------|-----------------|-----------| | | ✓ | ✓ | ENOENT | Thus the optimal choice is to use /dev/stdin for Linux, and file descriptor zero for the others. [fd0]: https://nodejs.org/api/process.html#process_process_stdin_fd
1 parent 6d80c8e commit 1de3cfe

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

bin/commonmark

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ if (format === 'html') {
7777
}
7878

7979
if (files.length === 0) {
80-
if (os.platform() === "win32") {
81-
inps.push(fs.readFileSync(0, 'utf-8'));
82-
} else {
83-
inps.push(fs.readFileSync('/dev/tty', 'utf-8'));
84-
}
80+
if (os.platform() === 'linux') {
81+
inps.push(fs.readFileSync('/dev/stdin', 'utf-8'));
82+
} else {
83+
inps.push(fs.readFileSync(0, 'utf-8'));
84+
}
8585
} else {
8686
for (i = 0; i < files.length; i++) {
8787
var file = files[i];

0 commit comments

Comments
 (0)