UNPKG

@push.rocks/smartstream

Version:

A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.

97 lines (83 loc) 2.23 kB
import * as plugins from './smartstream.plugins.js'; // interfaces import { Transform } from 'stream'; export interface IErrorFunction { (err: Error): any; } export interface ICustomEventFunction { (): any; } export interface ICustomEventObject { eventName: string; eventFunction: ICustomEventFunction; } /** * class Smartstream handles */ export class StreamWrapper { private streamArray: Array<plugins.stream.Duplex> = []; private customEventObjectArray: ICustomEventObject[] = []; private streamStartedDeferred = plugins.smartpromise.defer(); /** * constructor */ constructor(streamArrayArg: any[]) { this.streamArray = streamArrayArg; } /** * make something with the stream itself */ streamStarted(): Promise<any> { return this.streamStartedDeferred.promise; } /** * attach listener to custom event */ onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) { this.customEventObjectArray.push({ eventName: eventNameArg, eventFunction: eventFunctionArg, }); } /** * run the stream * @returns Promise */ run(): Promise<void> { const done = plugins.smartpromise.defer<void>(); // clone Array const streamExecutionArray: Array<plugins.stream.Duplex> = []; for (const streamItem of this.streamArray) { streamExecutionArray.push(streamItem); } // combine the stream let finalStream = null; let firstIteration: boolean = true; for (const stream of streamExecutionArray) { if (firstIteration === true) { finalStream = stream; } stream.on('error', (err) => { done.reject(err); }); for (const customEventObject of this.customEventObjectArray) { stream.on(customEventObject.eventName, customEventObject.eventFunction); } if (!firstIteration) { finalStream = finalStream.pipe(stream); } firstIteration = false; } this.streamStartedDeferred.resolve(); finalStream.on('end', () => { done.resolve(); }); finalStream.on('close', () => { done.resolve(); }); finalStream.on('finish', () => { done.resolve(); }); return done.promise; } }