Skip to content

Commit 5be09ef

Browse files
committed
Use file descriptor 0 or /dev/tty 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 we split the decision in a platform-independent way: 1. When in a TTY situation, use /dev/tty. It does not work on Windows, but nothing simple will, and obviouly this is not a regression anyway. 2. Otherwise, use file descriptor 0, which works across all platforms. [fd0]: https://nodejs.org/api/process.html#process_process_stdin_fd
1 parent 6d80c8e commit 5be09ef

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

bin/commonmark

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import util from "util";
55
import fs from "fs";
6-
import os from "os";
76
import * as commonmark from "../lib/index.js";
87
var version = require('../package.json').version;
98
import parseArgs from "minimist";
@@ -77,11 +76,11 @@ if (format === 'html') {
7776
}
7877

7978
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-
}
79+
if (process.stdin.isTTY) {
80+
inps.push(fs.readFileSync('/dev/tty', 'utf-8'));
81+
} else {
82+
inps.push(fs.readFileSync(0, 'utf-8'));
83+
}
8584
} else {
8685
for (i = 0; i < files.length; i++) {
8786
var file = files[i];

0 commit comments

Comments
 (0)