Skip to content

Instantly share code, notes, and snippets.

@othiym23
Created August 8, 2012 23:22
Show Gist options
  • Save othiym23/3299666 to your computer and use it in GitHub Desktop.
Save othiym23/3299666 to your computer and use it in GitHub Desktop.
shove stuff in a Buffer, hand it to a callback when it's done
var events = require('events')
, util = require('util')
;
function StreamBuffer(callback) {
events.EventEmitter.call(this);
this.writable = true;
var self = this;
this.on('data', function (data) {
if (!self.buffer) {
self.buffer = new Buffer(data);
}
else {
self.buffer = Buffer.concat([self.buffer, data]);
}
});
this.on('error', function (exception) {
self.writable = false;
return callback(exception);
});
this.on('end', function () {
return callback(null, self.buffer);
});
}
util.inherits(StreamBuffer, events.EventEmitter);
module.exports = StreamBuffer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment