Created
July 25, 2014 21:52
-
-
Save tjfontaine/d16713dd1d6b89f4cfaf to your computer and use it in GitHub Desktop.
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 Transform = require('stream').Transform; | |
var util = require('util'); | |
function Padstream() { | |
if (!(this instanceof Padstream)) | |
return new Padstream(); | |
Padstream.call(this); | |
this._buffer = new Buffer(64); | |
this._pos = 0; | |
} | |
util.inherits(Padstream, Transform); | |
Padstream.prototype._transform = function padTransform(chunk, encoding, done) { | |
// TODO handle chunk > this._buffer.length | |
var copied = chunk.copy(this._buffer, | |
this._pos, | |
0, | |
chunk.length - (this._buffer.length - this._pos)); | |
this._pos += copied; | |
if (this._pos === this._buffer.length) { | |
this.push(this._buffer); | |
this._pos = 0; | |
if (copied < chunk.length) | |
this._buffer = chunk.slice(copied); | |
else | |
this._buffer = new Buffer(64); | |
} | |
}; | |
Padstream.prototype._flush = function padFlush(done) { | |
if (this._pos > 0) { | |
this._buffer.fill(0, this._pos); | |
this.push(this._buffer); | |
this._buffer = false; | |
this._pos = false; | |
} | |
done(); | |
}; | |
srcstream.pipe(new Padstream()).pipe(client); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment