Skip to content

Commit

Permalink
Use /dev/stdin instead of 0 when using with readFileSync (#25)
Browse files Browse the repository at this point in the history
Sometimes, especially when reading large inputs from stdin, we get the
following error:

```
node:internal/fs/sync:25
  return binding.readFileUtf8(path, stringToFlags(flag));
                 ^

Error: EAGAIN: resource temporarily unavailable, read
    at Object.readFileUtf8 (node:internal/fs/sync:25:18)
    at Object.readFileSync (node:fs:441:19)
    at /usr/local/lib/node_modules/@watonomous/watcloud-emails/dist/cli/index.js:74:44
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/@watonomous/watcloud-emails/dist/cli/index.js:9:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/local/lib/node_modules/@watonomous/watcloud-emails/dist/cli/index.js:5:12)
    at Command.<anonymous> (/usr/local/lib/node_modules/@watonomous/watcloud-emails/dist/cli/index.js:69:41)
    at Command.listener [as _actionHandler] (/usr/local/lib/node_modules/@watonomous/watcloud-emails/node_modules/commander/lib/command.js:542:17)
    at /usr/local/lib/node_modules/@watonomous/watcloud-emails/node_modules/commander/lib/command.js:1502:14 {
  errno: -11,
  code: 'EAGAIN',
  syscall: 'read'
}
```

This appears to be because `process.stdin.fd` only operates on
nonblocking mode. This PR accesses `/dev/stdin` directly instead of
using `0`.

References:
- https://stackoverflow.com/a/75948127/4527337
  • Loading branch information
ben-z authored Dec 31, 2024
1 parent c728034 commit 764e4a7
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ program
if (options.data) {
if (options.data === '-') {
// Read from stdin
data = JSON.parse(fs.readFileSync(0, 'utf-8'));
data = JSON.parse(fs.readFileSync('/dev/stdin', 'utf-8'));
} else {
data = JSON.parse(options.data);
}
Expand Down Expand Up @@ -64,7 +64,7 @@ program
if (options.data) {
if (options.data === '-') {
// Read from stdin
data = JSON.parse(fs.readFileSync(0, 'utf-8'));
data = JSON.parse(fs.readFileSync('/dev/stdin', 'utf-8'));
} else {
data = JSON.parse(options.data);
}
Expand Down

0 comments on commit 764e4a7

Please sign in to comment.