batchjs
Version:
Batch processing framework for NodeJS
60 lines (59 loc) • 1.84 kB
JavaScript
import { SingleStreamError } from "../errors/_index";
import { InternalBufferDuplex } from "../interfaces/_index";
/**
* @class
* Class that allows you to verify that a stream contains only one chunk.
* @extends InternalBufferDuplex
* @template T
* @example
* ```typescript
* const stream:SingleStream<string> = new SingleStream({
* objectMode: true,
* });
*
* stream.write("data1");
* stream.write("data2"); // This should launch error
* stream.end();
*
* stream.on("data", (chunk: string) => {
* console.log(``Pushed chunk: ${chunk}```);
* });
* stream.once("error", (err: SingleStreamError) => {
* console.log(``Error: ${err.message}```);
* });
* ```
* ```shell
* >> Pushed chunk: data1
* >> Error: Expected only one chunk in the stream
* ```
*/
export class SingleStream extends InternalBufferDuplex {
isFirstChunk = true;
/**
* @constructor
* @param {ObjectDuplexOptions} options - The options for the SingleStream.
*/
constructor(options) {
super(options);
}
/**
* A method to write data to the stream, push the chunk to the buffer if its the first chunk, otherwise discard it, 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) {
if (this.isFirstChunk) {
this.isFirstChunk = false;
this.buffer.push(chunk);
this._flush();
}
else {
const error = new SingleStreamError();
this.emit("error", error);
}
callback();
}
}