I have some code to interface with a wire protocol that requires data to be inserted into a stream at regular byte intervals. Every 8KB (or at some other definable interval), a small chunk will be inserted. To make this easy, I decided to create a transform stream that would take a flowing stream and write fixed chunk sizes. That is, this stream can be written to in any size (2KB here, 500KB there, 5 bytes next, etc.) and it will output chunks in 8KB of size every time.
var stream = require('stream');
function ChunkerTransformStream (chunkSize) {
chunkSize = chunkSize || 8192;
var buffer = new Buffer(0);
var chunker = new stream.Transform({objectMode: true});
chunker._transform = function (chunk, encoding, done) {
buffer = Buffer.concat([buffer, chunk]);
while (buffer.length >= chunkSize) {
this.push(buffer.slice(0, chunkSize));
buffer = buffer.slice(chunkSize);
}
done();
}
chunker._flush = function (done) {
if (buffer.length) {
this.push(buffer);
done();
}
}
return chunker;
}
module.exports = ChunkerTransformStream;
This transform stream will be used heavily in my code, having several megabit pushed through it per second. Is this the most efficient way to achieve what I want? I am most concerned about my buffer operations. It's my understanding that Buffer.concat() is very expensive, as it allocates an entirely new buffer and copies the first two to it.
Any feedback, on performance or otherwise, is welcomed.