@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.
56 lines (55 loc) • 2.16 kB
TypeScript
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
import { Duplex, type DuplexOptions } from 'stream';
export interface IStreamTools {
truncate: () => void;
push: (pipeObject: any) => Promise<boolean>;
}
export interface IStreamWriteFunction<T, rT> {
(chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
}
export interface IStreamFinalFunction<rT> {
(toolsArg: IStreamTools): Promise<rT>;
}
export interface ISmartDuplexOptions<TInput, TOutput> extends DuplexOptions {
/**
* wether to print debug logs
*/
debug?: boolean;
/**
* the name of the stream
*/
name?: string;
/**
* a function that is being called to read more stuff from whereever to be processed by the stream
* @returns
*/
readFunction?: () => Promise<void>;
/**
* the write function is called for every chunk that is being written to the stream
* it can push or return chunks (but does not have to) to be written to the readable side of the stream
*/
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
/**
* a final function that is run at the end of the stream
*/
finalFunction?: IStreamFinalFunction<TOutput>;
}
export declare class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
static fromBuffer(buffer: Buffer, options?: ISmartDuplexOptions<any, any>): SmartDuplex;
static fromWebReadableStream<T = any>(readableStream: ReadableStream<T>): SmartDuplex<T, T>;
private backpressuredArray;
options: ISmartDuplexOptions<TInput, TOutput>;
private observableSubscription?;
private debugLog;
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>);
_read(size: number): Promise<void>;
backpressuredPush(pushArg: TOutput): Promise<boolean>;
private asyncWritePromiseObjectmap;
_write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void): Promise<void>;
_final(callback: (error?: Error | null) => void): Promise<void>;
getWebStreams(): Promise<{
readable: ReadableStream;
writable: WritableStream;
}>;
}