Skip to content

Instantly share code, notes, and snippets.

@bryanmacfarlane
Created March 1, 2015 13:20
Show Gist options
  • Save bryanmacfarlane/aa7541927aa05eab8fa2 to your computer and use it in GitHub Desktop.
Save bryanmacfarlane/aa7541927aa05eab8fa2 to your computer and use it in GitHub Desktop.
Node Stream bug v2
var util = require('util');
var Q = require('q');
var path = require('path');
var stream = require('stream');
var fs = require('fs');
var NullStream = function () {
stream.Writable.call(this, { objectMode: true });
this._write = function (data, encoding, callback) {
};
};
util.inherits(NullStream, stream.Writable);
var streamThrough = function(filePath, done) {
var len = 0;
var inputStream = fs.createReadStream(filePath);
var ps = new NullStream();
ps.on('error', function (err) {
done(err, len);
});
inputStream.on('error', function (err) {
done(err, len);
});
inputStream.on('end', function () {
console.log();
done(null, len);
});
inputStream.on('data', function (chunk) {
process.stdout.write('.');
len += chunk.length;
});
inputStream.pipe(ps);
}
var fileArg = process.argv[2];
if (!fileArg) {
console.error('pass file as arg');
process.exit(1);
}
var filePath = path.resolve(__dirname, fileArg);
streamThrough(filePath, function(err, len) {
if (err) {
console.error(err.message);
}
console.log('size: ' + len);
})
process.on('uncaughtException', function (err) {
console.error(err.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment