Skip to content

Instantly share code, notes, and snippets.

@rjz
Created March 12, 2014 05:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjz/9501304 to your computer and use it in GitHub Desktop.
Save rjz/9501304 to your computer and use it in GitHub Desktop.
Streaming from stdin or a file
// Depends on `through`
//
// $ npm install through
//
// Usage:
//
// $ echo 'hello' | node stdin-and-fs-stream.js
// $ echo 'hello' > tmp && node stdin-and-fs-stream.js tmp
//
var fs = require('fs'),
through = require('through');
var tr = through(function (buf) {
console.log(buf.toString());
});
var stream;
if (process.argv.length > 2) {
stream = fs.createReadStream(process.argv[2]);
}
else {
stream = process.stdin;
setImmediate(function () {
stream.push(null);
});
}
stream.pipe(tr).pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment