Skip to content

Instantly share code, notes, and snippets.

@catdad
Created September 11, 2016 14:54
Show Gist options
  • Save catdad/357437ecf6489feb9313916166042d05 to your computer and use it in GitHub Desktop.
Save catdad/357437ecf6489feb9313916166042d05 to your computer and use it in GitHub Desktop.
change chunk sizes in a stream
var through = require('through2');
function changeChunkSizes() {
var buff = '';
var data;
var keepIdx;
return through(function (chunk, enc, cb) {
buff += chunk.toString();
data = keepIdx = undefined;
if (buff.length > 30) {
// TODO make sure this index does not fall on a \r\n
keepIdx = parseInt(buff.length / 2, 10);
data = buff.slice(0, keepIdx);
buff = buff.slice(keepIdx);
return cb(null, data);
}
cb();
}, function () {
this.push(buff);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment