Skip to content

Instantly share code, notes, and snippets.

@3AHAT0P
Created August 19, 2020 13:25
Show Gist options
  • Save 3AHAT0P/bfba68f7f9869b90aabc4b0afbebb604 to your computer and use it in GitHub Desktop.
Save 3AHAT0P/bfba68f7f9869b90aabc4b0afbebb604 to your computer and use it in GitHub Desktop.
I created a NodeJS Transform Stream which repack data chunks to necessary size chunks
const { Transform } = require('stream');
const sliceBySize = (buffer, size, callback) => {
let chunk = buffer.slice(0, size);
let index = size;
while (chunk.length >= size) {
callback(chunk);
chunk = buffer.slice(index, index + size);
index += size;
}
return chunk;
};
class ChunkerTransformStream extends Transform {
_chunkSize = 512;
_chunk = Buffer.alloc(0);
constructor(chunkSize) {
super();
if (!Number.isNaN(Number(chunkSize))) this._chunkSize = Number(chunkSize);
}
_transform(oldChunk, encoding, callback) {
const freeSpaceLength = this._chunkSize - this._chunk.length;
if (oldChunk.length > freeSpaceLength) {
this.push(Buffer.concat([this._chunk, oldChunk.slice(0, freeSpaceLength)]));
this._chunk = sliceBySize(
oldChunk.slice(freeSpaceLength),
this._chunkSize,
(chunk) => this.push(chunk)
);
} else {
this._chunk = Buffer.concat([this._chunk, oldChunk]);
if (this._chunk.length >= this._chunkSize) {
this.push(this._chunk);
this._chunk = Buffer.alloc(0);
}
}
callback();
}
_flush(callback) {
this.push(this._chunk);
callback();
}
}
module.exports = ChunkerTransformStream;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment