I have a node.js script, example.mjs which takes standard input, changes it, and sends to standard output
process.stdin.fd and process.stdout.fd are set to 0 and 1 respectively.
So in my code I have:
import { writeFileSync, readFileSync } from 'fs';
const input = readFileSync(0, 'utf-8').trim();
writeFileSync(1, input + input.length, { encoding: 'utf-8' });
It just appends the length of the input to the input
echo "0"gives "0"echo "0" | node example.mjsgives "01"echo "0" | node example.mjs | node example.mjsgives "012"echo "0" | node example.mjs | node example.mjs | node example.mjsgives "0123"
If I try the same again, but this time using process.stdin.fd and process.stdout.fd because I think it's nicer to describe what the file descriptors are instead of just using numbers:
import { writeFileSync, readFileSync } from 'fs';
const input = readFileSync(process.stdin.fd, 'utf-8').trim();
writeFileSync(process.stdout.fd, input + input.length, { encoding: 'utf-8' });
echo "0"gives "0"echo "0" | node example.mjsgives "01"echo "0" | node example.mjs | node example.mjsgives the following error
node:fs:723
handleErrorFromBinding(ctx);
^
Error: EAGAIN: resource temporarily unavailable, read
at Object.readSync (node:fs:723:3)
at tryReadSync (node:fs:433:20)
at readFileSync (node:fs:479:19)
at file:///example.mjs:2:15
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:331:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:65:12) {
errno: -11,
syscall: 'read',
code: 'EAGAIN'
}
Node.js v17.2.0
From the Manual, EAGAIN means: "Resource temporarily unavailable (may be the same value as EWOULDBLOCK) (POSIX.1-2001)."
I don't really understand what this means, but if i'm passing the same values, how can my code be crashing.
Node version and other diagnostics details are in the error message, and the same error occurs on both Windows running WSL2 and MacOS.