Skip to content

Instantly share code, notes, and snippets.

@tjfontaine
Created July 25, 2014 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjfontaine/d16713dd1d6b89f4cfaf to your computer and use it in GitHub Desktop.
Save tjfontaine/d16713dd1d6b89f4cfaf to your computer and use it in GitHub Desktop.
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