UNPKG

batchjs

Version:

Batch processing framework for NodeJS

81 lines (80 loc) 2.64 kB
import { InternalBufferDuplex } from "../interfaces/_index"; /** * @class * Class that allows you stream data in batches of a specified size. * @extends InternalBufferDuplex * @template T * @example * ```typescript * const stream:BufferStream<string> = new BufferStream({ * objectMode: true, * batchSize: 2, * }); * * stream.write("data1"); * stream.write("data2"); * stream.write("data3"); * stream.end(); * * stream.on("data", (chunk: Array<string>) => { * console.log(``Pushed chunk: ${JSON.stringify(chunk)}```); * }); * ``` * ```shell * >> Pushed chunk: ["data1", "data2"] * >> Pushed chunk: ["data3"] * ``` */ export class BufferStream extends InternalBufferDuplex { prebuffer = []; batchSize; /** * @constructor * @param {BufferStreamOptions} options - The options for the BufferStream. * @param [options.batchSize] {number} - The maximum number of elements in a batch. */ constructor(options) { super(options); this.batchSize = options.batchSize; } /** * A method to write data to the stream, push the chunk to the buffer, and execute the callback. * * @param {T} chunk - The data chunk to write to the stream. * @param {BufferEncoding} encoding - The encoding of the data. * @param {TransformCallback} callback - The callback function to be executed after writing the data. * @return {void} This function does not return anything. */ _write(chunk, encoding, callback) { this.prebuffer.push(chunk); if (this.prebuffer.length === this.batchSize) { const batch = this.prebuffer.splice(0, this.batchSize); this.buffer.push(batch); this._flush(); } callback(); } /** * Finalizes the stream by pushing remaining data batches, handling errors, * and executing the final callback. * * @param {TransformCallback} callback - The callback function to be executed after finalizing the stream. * @return {void} This function does not return anything. */ _final(callback) { while (this.prebuffer.length > 0) { const batch = this.prebuffer.splice(0, this.batchSize); this.buffer.push(batch); } super._final(callback); } /** * Pushes the ready chunks to the consumer stream since the buffer is empty or the size limit is reached. * * @param {number} size - The size parameter for controlling the read operation. * @return {void} This function does not return anything. */ _read() { this._flush(); } }