import { CancellationToken } from "./cancellation.js"; /** * The payload that flows in readable stream events. */ export type ReadableStreamEventPayload = T | Error | "end"; export interface ReadableStreamEvents { /** * The 'data' event is emitted whenever the stream is * relinquishing ownership of a chunk of data to a consumer. * * NOTE: PLEASE UNDERSTAND THAT ADDING A DATA LISTENER CAN * TURN THE STREAM INTO FLOWING MODE. IT IS THEREFOR THE * LAST LISTENER THAT SHOULD BE ADDED AND NOT THE FIRST * * Use `listenStream` as a helper method to listen to * stream events in the right order. */ 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 native 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 native 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 function isReadable(obj: unknown): obj is Readable; /** * A interface that emulates the API shape of a node.js writeable * stream for use in native 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. * * NOTE: call `end` to signal that the stream has ended, * this DOES NOT happen automatically from `error`. */ error(error: Error): void; /** * Signals the end of the stream to the consumer. If the * result is provided, will trigger the on('data') event * listener if the stream is flowing and buffer the data * otherwise until the stream is flowing. */ end(result?: T): 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 IReducer { (data: T[]): R; } export interface IDataTransformer { (data: Original): Transformed; } export interface IErrorTransformer { (error: Error): Error; } export interface ITransformer { data: IDataTransformer; error?: IErrorTransformer; } export declare function newWriteableStream(reducer: IReducer | null, 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: IReducer): 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 peekReadable(readable: Readable, reducer: IReducer, maxChunks: number): T | Readable; /** * Helper to fully read a T stream into a T or consuming * a stream fully, awaiting all the events without caring * about the data. */ export declare function consumeStream(stream: ReadableStreamEvents, reducer: IReducer): Promise; export declare function consumeStream(stream: ReadableStreamEvents): Promise; export interface IStreamListener { /** * The 'data' event is emitted whenever the stream is * relinquishing ownership of a chunk of data to a consumer. */ onData(data: T): void; /** * Emitted when any error occurs. */ onError(err: Error): 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. */ onEnd(): void; } /** * Helper to listen to all events of a T stream in proper order. */ export declare function listenStream(stream: ReadableStreamEvents, listener: IStreamListener, token?: CancellationToken): void; /** * 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 create a readable stream from an existing T. */ export declare function toStream(t: T, reducer: IReducer): ReadableStream; /** * Helper to create an empty stream */ export declare function emptyStream(): 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: IReducer): ReadableStream; /** * Helper to take an existing readable that will * have a prefix injected to the beginning. */ export declare function prefixedReadable(prefix: T, readable: Readable, reducer: IReducer): Readable; /** * Helper to take an existing stream that will * have a prefix injected to the beginning. */ export declare function prefixedStream(prefix: T, stream: ReadableStream, reducer: IReducer): ReadableStream;