Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Created April 13, 2016 00:51
Show Gist options
  • Save ORESoftware/e733e03334d885c9249b81866ed5b9b9 to your computer and use it in GitHub Desktop.
Save ORESoftware/e733e03334d885c9249b81866ed5b9b9 to your computer and use it in GitHub Desktop.
var BufferStream = function () {
stream.Transform.apply(this, arguments);
this.sumanReady = false;
this.buffer = [];
};
util.inherits(BufferStream, stream.Transform);
BufferStream.prototype._transform = function (chunk, encoding, done) {
// custom buffering logic
// ie. add chunk to this.buffer, check buffer size, etc.
if (this.sumanReady) {
this.push(chunk ? String(chunk) : null);
}
else {
this.buffer.push(chunk ? String(chunk) : null);
}
done()
};
BufferStream.prototype.pipe = function (destination, options) {
var res = BufferStream.super_.prototype.pipe.apply(this, arguments);
this.buffer.forEach(function (b) {
res.write(String(b));
});
this.sumanReady = true;
return res;
};
// strm.cork();
return new BufferStream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment