Created
August 8, 2012 23:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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