import type { Reader, Writer } from "@endo/stream"; import type { Dispatchable } from "./utils.mjs"; /** * A function that is called when a stream ends. Useful for cleanup, such as closing a * message port. */ export type OnEnd = (error?: Error) => void | Promise; /** * A function that validates input to a readable stream. */ export type ValidateInput = (input: unknown) => input is Read; /** * A function that receives input from a transport mechanism to a readable stream. * Validates that the input is an {@link IteratorResult}, and throws if it is not. */ export type ReceiveInput = (input: unknown) => Promise; export type BaseReaderArgs = { name?: string | undefined; onEnd?: OnEnd | undefined; validateInput?: ValidateInput | undefined; }; /** * The base of a readable async iterator stream. * * Subclasses must forward input received from the transport mechanism via the function * returned by `getReceiveInput()`. Any cleanup required by subclasses should be performed * in a callback passed to `setOnEnd()`. * * The result of any value received before the stream ends is guaranteed to be observable * by the consumer. */ export declare class BaseReader implements Reader { #private; /** * Constructs a {@link BaseReader}. * * @param options - Options bag for configuring the reader. * @param options.name - The name of the stream, for logging purposes. Defaults to the class name. * @param options.onEnd - A function that is called when the stream ends. For any cleanup that * should happen when the stream ends, such as closing a message port. * @param options.validateInput - A function that validates input from the transport. */ constructor({ name, onEnd, validateInput }: BaseReaderArgs); /** * Returns the `receiveInput()` method, which is used to receive input from the stream. * Attempting to call this method more than once will throw an error. * * @returns The `receiveInput()` method. */ protected getReceiveInput(): ReceiveInput; /** * Returns the async iterator for this stream. * * @returns This stream as an async iterator. */ [Symbol.asyncIterator](): typeof this; /** * Reads the next message from the transport. * * @returns The next message from the transport. */ next(): Promise>; /** * Closes the underlying transport and returns. Any unread messages will be lost. * * @returns The final result for this stream. */ return(): Promise>; /** * Rejects all pending reads with the specified error, closes the underlying transport, * and returns. * * @param error - The error to reject pending reads with. * @returns The final result for this stream. */ throw(error: Error): Promise>; /** * Closes the stream. Syntactic sugar for `return()` or `throw(error)`. Idempotent. * * @param error - The error to close the stream with. * @returns The final result for this stream. */ end(error?: Error): Promise>; } export type Dispatch = (value: Dispatchable) => void | Promise; export type BaseWriterArgs = { onDispatch: Dispatch; name?: string | undefined; onEnd?: OnEnd | undefined; }; /** * The base of a writable async iterator stream. */ export declare class BaseWriter implements Writer { #private; /** * Constructs a {@link BaseWriter}. * * @param options - Options bag for configuring the writer. * @param options.onDispatch - A function that dispatches messages over the underlying transport mechanism. * @param options.name - The name of the stream, for logging purposes. Defaults to the class name. * @param options.onEnd - A function that is called when the stream ends. For any cleanup that * should happen when the stream ends, such as closing a message port. */ constructor({ name, onDispatch, onEnd }: BaseWriterArgs); /** * Returns the async iterator for this stream. * * @returns This stream as an async iterator. */ [Symbol.asyncIterator](): typeof this; /** * Writes the next message to the transport. * * @param value - The next message to write to the transport. * @returns The result of writing the message. */ next(value: Write): Promise>; /** * Closes the underlying transport and returns. Idempotent. * * @returns The final result for this stream. */ return(): Promise>; /** * Forwards the error to the transport and closes this stream. Idempotent. * * @param error - The error to forward to the transport. * @returns The final result for this stream. */ throw(error: Error): Promise>; /** * Closes the stream. Syntactic sugar for `return()` or `throw(error)`. Idempotent. * * @param error - The error to close the stream with. * @returns The final result for this stream. */ end(error?: Error): Promise>; } //# sourceMappingURL=BaseStream.d.mts.map