-
Please, note I'm not looking for advice on how to read I'm trying to read from the Here's a simple example. // this flips process.stdin.readableFlowing from null to false
process.stdin.pause();
// true
console.log("isPaused(): " + process.stdin.isPaused());
// true
console.log("readable: " + process.stdin.readable);
// false
console.log("readableFlowing: " + process.stdin.readableFlowing);
// this will print stdout
// console.log("fs: " + require('fs').readFileSync(process.stdin.fd).toString());
// this will produce chunk with XYZ
// process.stdin.push(Buffer.from("XYZ"));
var chunk = process.stdin.read();
// chunk is always null
console.log("Chunk? " + (chunk ? ("yes: " + chunk.toString()) : "no")); I run this on Windows 10, Node v16.13.0, as:
Using events works, as does using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I did some debugging and turns out that the internal stream read buffer is indeed not populated initially, even if there is data in the input. The implementation is such that the The example from the docs is the only way to read from console.log("isPaused(): " + process.stdin.isPaused());
console.log("readable: " + process.stdin.readable);
console.log("readableFlowing: " + process.stdin.readableFlowing);
process.stdin.on("readable", () => {
let chunk = process.stdin.read();
if(chunk)
console.log("chunk: " + chunk.toString(), "readable: " + process.stdin.readable);
else
console.log("end; readable: " + process.stdin.readable);
}); , when launched this way.
, and will produce this output:
The implementation appears fragile and may behave in unpredictable ways. For example, calling The If wish while(process.stdin.really_readabale) {
if((chunk = process.stdin.read()))
consume_chunk(chunk);
else
process_eof();
}
if(!seen_eof)
go_async_for_more(); Posting in hopes somebody finds this useful. |
Beta Was this translation helpful? Give feedback.
I did some debugging and turns out that the internal stream read buffer is indeed not populated initially, even if there is data in the input. The implementation is such that the
readable
event must be used it has nothing to do with thereadable
state indicator. In fact, the latter just indicates that the stream hasn't gone bad.The example from the docs is the only way to read from
process.stdin
viaread()
.