/** * StreamBuf - Cross-Platform Multi-purpose Read-Write Stream * * A unified implementation that works in both Node.js and Browser environments * using the cross-platform EventEmitter from modules/stream. * * Features: * - As MemBuf: write data, then call toBuffer() to consolidate * - As StreamHub: pipe to multiple writable streams * - As readable stream: feed data into writable part and read from it */ import { StringBuf } from "./string-buf.js"; import { EventEmitter } from "../../../utils/event-emitter.js"; type TextEncoding = "utf-8" | "utf8" | BufferEncoding; interface StreamBufOptions { bufSize?: number; batch?: boolean; } /** * StreamBuf is a multi-purpose read-write stream that works in both * Node.js and Browser environments. * * It extends EventEmitter to provide stream-like events: * - 'data': emitted when data is written (flowing mode) * - 'readable': emitted when data is available to read * - 'finish': emitted when end() is called * - 'error': emitted on errors * - 'drain': emitted when buffer drains (after pipe) */ declare class StreamBuf extends EventEmitter { private bufSize; private buffers; private batch; private corked; private paused; private encoding; private pipes; private _ended; private _writableStreamWriter; private _asyncWriteQueue; constructor(options?: StreamBufOptions); /** * Returns true if the stream is writable (not ended) * Required for compatibility with Node.js pipe() */ get writable(): boolean; /** * Consolidate all buffers into a single Uint8Array */ toBuffer(): Uint8Array | null; private _getWritableBuffer; private _pipeChunk; private _writeToBuffers; /** * Write data to the stream */ write(data: Uint8Array | string | StringBuf | ArrayBuffer | ArrayBufferView, encoding?: TextEncoding | ((...args: any[]) => any), callback?: (...args: any[]) => any): Promise; /** * Cork the stream - buffer writes until uncork */ cork(): void; private _flush; /** * Uncork the stream - flush buffered writes */ uncork(): void; /** * End the stream */ end(chunk?: any, encoding?: TextEncoding, callback?: (...args: any[]) => any): void; /** * Read from the stream */ read(size?: number): Uint8Array; /** * Read from the stream and return as string. * Cross-platform compatible - works identically in Node.js and Browser. */ readString(encoding?: TextEncoding): string; /** * Set encoding for string reads */ setEncoding(encoding: string): void; /** * Pause the stream */ pause(): void; /** * Resume the stream */ resume(): void; /** * Check if stream is paused */ isPaused(): boolean; /** * Pipe to a writable stream */ pipe any; end?: (...args: any[]) => any; }>(destination: T): T; /** * Pipe to a native WritableStream (browser Streams API). * This properly handles async writes and waits for completion before finish. */ pipeTo(writableStream: WritableStream): void; /** * Remove a piped destination */ unpipe(destination: any): void; /** * Put data back at the front (not implemented) */ unshift(): void; /** * Wrap a stream (not implemented) */ wrap(): void; /** * Push data to the stream (alias for write) */ push(chunk: any): boolean; } export { StreamBuf };