Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created June 20, 2012 17:00
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 isaacs/2960945 to your computer and use it in GitHub Desktop.
Save isaacs/2960945 to your computer and use it in GitHub Desktop.
// usage: cat readfile-stdin.js | node readfile-stdin.js
var fs = require('fs');
fs.open('/dev/stdin', 'r', function(er, fd) {
console.error('open', er, fd);
if (er) throw er;
var buf
fs.fstat(fd, function(er, st) {
console.error('stat', er, st);
if (er) throw er;
var size = st.size;
var buffer = new Buffer(size);
var pos = 0;
fs.read(fd, buffer, pos, size - pos, pos, function (er, bytes) {
console.error('read', er, bytes);
console.error('data', buffer.toString());
fs.close(fd, function(er) {
console.error('close', er);
if (er) throw er;
});
});
});
});
/* output:
open null 8
stat null { dev: 0,
ino: 298218304,
mode: 4528,
nlink: 0,
uid: 24561,
gid: 20,
rdev: 0,
size: 743,
blksize: 16384,
blocks: 1,
atime: Wed Jun 20 2012 10:00:21 GMT-0700 (PDT),
mtime: Wed Jun 20 2012 10:00:21 GMT-0700 (PDT),
ctime: Wed Jun 20 2012 10:00:21 GMT-0700 (PDT) }
read { [Error: UNKNOWN, read] errno: -1, code: 'UNKNOWN' } 0
data
close null
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment