UNPKG

@snps/streaming

Version:

Zero-dependency streaming utilities for SSE, async streams, and AI integration

180 lines 4.35 kB
/** * Streaming API - Modern streaming support for Server-Sent Events, async iteration, and real-time data * Perfect for AI streaming responses, live updates, and data processing pipelines * @packageDocumentation */ /** * Server-Sent Event message */ export interface SSEMessage { event?: string | undefined; data: string; id?: string | undefined; retry?: number | undefined; } /** * Stream options */ export interface StreamOptions { highWaterMark?: number | undefined; autoClose?: boolean | undefined; } /** * Transform function for stream processing */ export type StreamTransform<T, R> = (chunk: T) => R | Promise<R>; /** * Server-Sent Events (SSE) Stream * Enables real-time updates from server to client */ export declare class SSEStream { private controller; private closed; /** * Create a new SSE stream */ constructor(); /** * Get the ReadableStream for this SSE connection */ getStream(): ReadableStream<string>; /** * Send a message to the client */ send(message: SSEMessage): void; /** * Send a simple message (shorthand) */ message(data: string): void; /** * Send an event with data */ event(event: string, data: string): void; /** * Send JSON data */ json(data: unknown): void; /** * Close the stream */ close(): void; /** * Check if stream is closed */ isClosed(): boolean; } /** * Create a Server-Sent Events response */ export declare function createSSEResponse(stream: SSEStream): Response; /** * Async Stream - Modern async iterator-based streaming */ export declare class AsyncStream<T> { private queue; private resolvers; private done; private error; /** * Push a value to the stream */ push(value: T): void; /** * End the stream */ end(): void; /** * Error the stream */ throw(error: Error): void; /** * Async iterator interface */ [Symbol.asyncIterator](): AsyncIterator<T>; /** * Transform the stream */ map<R>(fn: StreamTransform<T, R>): AsyncStream<R>; /** * Filter the stream */ filter(predicate: (chunk: T) => boolean | Promise<boolean>): AsyncStream<T>; /** * Take only n items */ take(n: number): AsyncStream<T>; /** * Collect all values into an array */ collect(): Promise<T[]>; /** * Reduce the stream to a single value */ reduce<R>(fn: (acc: R, chunk: T) => R | Promise<R>, initial: R): Promise<R>; /** * Pipe to another stream */ pipeTo(destination: AsyncStream<T>): void; } /** * Create a stream from an array */ export declare function fromArray<T>(array: T[]): AsyncStream<T>; /** * Create a stream from an async iterable */ export declare function fromAsyncIterable<T>(iterable: AsyncIterable<T>): AsyncStream<T>; /** * Merge multiple streams into one */ export declare function merge<T>(...streams: AsyncStream<T>[]): AsyncStream<T>; /** * Create a stream from a generator function */ export declare function fromGenerator<T>(generator: () => AsyncGenerator<T>): AsyncStream<T>; /** * Create an interval stream that emits values periodically */ export declare function interval(ms: number, count?: number | undefined): AsyncStream<number>; /** * Stream for AI/LLM responses */ export declare class AIStream extends AsyncStream<string> { /** * Send to SSE stream */ toSSE(sse: SSEStream): void; /** * Concatenate all chunks */ text(): Promise<string>; /** * Stream JSON objects */ json<T>(): AsyncGenerator<T>; } /** * Create an AI stream for streaming AI responses */ export declare function createAIStream(): AIStream; /** * Transform stream - processes chunks through a transform function */ export declare class TransformStream<T, R> { private input; private output; constructor(transform: StreamTransform<T, R>); /** * Get the writable side */ writable(): AsyncStream<T>; /** * Get the readable side */ readable(): AsyncStream<R>; } /** * Create a transform stream */ export declare function transform<T, R>(fn: StreamTransform<T, R>): TransformStream<T, R>; //# sourceMappingURL=index.d.ts.map