Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created December 6, 2012 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooTallNate/660eac6c5063ca3eb374 to your computer and use it in GitHub Desktop.
Save TooTallNate/660eac6c5063ca3eb374 to your computer and use it in GitHub Desktop.
process.stdin semantics with streams2
// 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