-
-
Save TooTallNate/660eac6c5063ca3eb374 to your computer and use it in GitHub Desktop.
process.stdin semantics with streams2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the "readable" event is when you *must* read() data from the | |
// stream, so that the buffer gets below "highWaterMark", and | |
// it will begin reading some more. | |
// stdin holds the event loop open only while it's filling the | |
// buffer in between "readable" events, so it is up to you to read | |
// from some data in the "readable" event otherwise the process will exit | |
process.stdin.on('readable', function () { | |
var b; | |
while (null != (b = process.stdin.read())) { | |
// do awesome stuff... | |
} | |
// now that stdin's internal buffer is drained, it will | |
// begin reading more data from the FD until it can generate the | |
// next "readable" event | |
}); | |
// this signifies your intent in reading from stdin, | |
// and makes "readable" events happen. | |
// think of it like the equivalent of the old ".resume()". | |
process.stdin.read(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment