Created
September 11, 2016 14:54
-
-
Save catdad/357437ecf6489feb9313916166042d05 to your computer and use it in GitHub Desktop.
change chunk sizes in a stream
This file contains hidden or 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 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