export interface ReadableStreamEvents { /** * The 'data' event is emitted whenever the stream is * relinquishing ownership of a chunk of data to a consumer. */ on(event: 'data', callback: (data: T) => void): void; /** * Emitted when any error occurs. */ on(event: 'error', callback: (err: Error) => void): void; /** * The 'end' event is emitted when there is no more data * to be consumed from the stream. The 'end' event will * not be emitted unless the data is completely consumed. */ on(event: 'end', callback: () => void): void; } /** * A interface that emulates the API shape of a node.js readable * stream for use in desktop and web environments. */ export interface ReadableStream extends ReadableStreamEvents { /** * Stops emitting any events until resume() is called. */ pause(): void; /** * Starts emitting events again after pause() was called. */ resume(): void; /** * Destroys the stream and stops emitting any event. */ destroy(): void; /** * Allows to remove a listener that was previously added. */ removeListener(event: string, callback: Function): void; } /** * A interface that emulates the API shape of a node.js readable * for use in desktop and web environments. */ export interface Readable { /** * Read data from the underlying source. Will return * null to indicate that no more data can be read. */ read(): T | null; } export declare namespace Readable { function fromString(value: string): Readable; function toString(readable: Readable): string; } /** * A interface that emulates the API shape of a node.js writeable * stream for use in desktop and web environments. */ export interface WriteableStream extends ReadableStream { /** * Writing data to the stream will trigger the on('data') * event listener if the stream is flowing and buffer the * data otherwise until the stream is flowing. * * If a `highWaterMark` is configured and writing to the * stream reaches this mark, a promise will be returned * that should be awaited on before writing more data. * Otherwise there is a risk of buffering a large number * of data chunks without consumer. */ write(data: T): void | Promise; /** * Signals an error to the consumer of the stream via the * on('error') handler if the stream is flowing. */ error(error: Error): void; /** * Signals the end of the stream to the consumer. If the * result is not an error, will trigger the on('data') event * listener if the stream is flowing and buffer the data * otherwise until the stream is flowing. * * In case of an error, the on('error') event will be used * if the stream is flowing. */ end(result?: T | Error): void; } /** * A stream that has a buffer already read. Returns the original stream * that was read as well as the chunks that got read. * * The `ended` flag indicates if the stream has been fully consumed. */ export interface ReadableBufferedStream { /** * The original stream that is being read. */ stream: ReadableStream; /** * An array of chunks already read from this stream. */ buffer: T[]; /** * Signals if the stream has ended or not. If not, consumers * should continue to read from the stream until consumed. */ ended: boolean; } export declare function isReadableStream(obj: unknown): obj is ReadableStream; export declare function isReadableBufferedStream(obj: unknown): obj is ReadableBufferedStream; export interface Reducer { (data: T[]): T; } export interface DataTransformer { (data: Original): Transformed; } export interface ErrorTransformer { (error: Error): Error; } export interface ITransformer { data: DataTransformer; error?: ErrorTransformer; } export declare function newWriteableStream(reducer: Reducer, options?: WriteableStreamOptions): WriteableStream; export interface WriteableStreamOptions { /** * The number of objects to buffer before WriteableStream#write() * signals back that the buffer is full. Can be used to reduce * the memory pressure when the stream is not flowing. */ highWaterMark?: number; } /** * Helper to fully read a T readable into a T. */ export declare function consumeReadable(readable: Readable, reducer: Reducer): T; /** * Helper to read a T readable up to a maximum of chunks. If the limit is * reached, will return a readable instead to ensure all data can still * be read. */ export declare function consumeReadableWithLimit(readable: Readable, reducer: Reducer, maxChunks: number): T | Readable; /** * Helper to read a T readable up to a maximum of chunks. If the limit is * reached, will return a readable instead to ensure all data can still * be read. */ export declare function peekReadable(readable: Readable, reducer: Reducer, maxChunks: number): T | Readable; /** * Helper to fully read a T stream into a T. */ export declare function consumeStream(stream: ReadableStream, reducer: Reducer): Promise; /** * Helper to peek up to `maxChunks` into a stream. The return type signals if * the stream has ended or not. If not, caller needs to add a `data` listener * to continue reading. */ export declare function peekStream(stream: ReadableStream, maxChunks: number): Promise>; /** * Helper to read a T stream up to a maximum of chunks. If the limit is * reached, will return a stream instead to ensure all data can still * be read. */ export declare function consumeStreamWithLimit(stream: ReadableStream, reducer: Reducer, maxChunks: number): Promise>; /** * Helper to create a readable stream from an existing T. */ export declare function toStream(t: T, reducer: Reducer): ReadableStream; /** * Helper to convert a T into a Readable. */ export declare function toReadable(t: T): Readable; /** * Helper to transform a readable stream into another stream. */ export declare function transform(stream: ReadableStreamEvents, transformer: ITransformer, reducer: Reducer): ReadableStream; //# sourceMappingURL=stream.d.ts.map