/** * Typed Server-Sent Events, adapter-agnostic. A handler returns * `sse(async (stream) => { … })`; each adapter renders it against its own * transport (a Node response on Fastify/Express, a `ReadableStream` on Hono). * The producer drives the stream: `send()` an event, `close()` to end, and * `onClose()` to react to the client disconnecting. */ export interface SseEvent { /** Payload — objects are JSON-encoded, strings sent as-is (split across `data:` lines). */ data: unknown; /** SSE event name (the client's `addEventListener(name)`). */ event?: string; /** Event id (drives `Last-Event-ID` reconnection). */ id?: string; /** Client reconnection delay hint (ms). */ retry?: number; } export interface SseStream { /** * Send an event (or a bare data string). Returns `false` if the stream is * closed OR the transport's write buffer is full — a backpressure signal a * producer should honour (await/slow down) to avoid unbounded memory growth * with a slow client. No-op once closed. */ send(event: SseEvent | string): boolean; /** End the stream. */ close(): void; readonly closed: boolean; /** Run a listener when the client disconnects or the stream closes. */ onClose(listener: () => void): void; } export type SseProducer = (stream: SseStream) => void | Promise; declare const SSE: unique symbol; /** Edge-hardening knobs for a stream — defends against dead/idle connections. */ export interface SseOptions { /** * Send a comment ping every N ms. Keeps proxies from closing an idle stream * and surfaces a dead socket (the write fails) instead of leaking the * connection. Off when unset. */ heartbeatMs?: number; /** * Hard cap on a single stream's lifetime, in ms. The stream is closed when it * elapses — a backstop against connections that never disconnect. Off when unset. */ maxDurationMs?: number; } export interface SseResponse { readonly [SSE]: SseProducer; } /** Wrap a producer as an SSE response for a route handler to return. */ export declare function sse(producer: SseProducer, options?: SseOptions): SseResponse; export declare function isSseResponse(value: unknown): value is SseResponse; export declare function sseProducerOf(value: SseResponse): SseProducer; export declare function encodeSseEvent(event: SseEvent | string): string; /** Standard SSE response headers. */ export declare const SSE_HEADERS: Record; /** A transport an adapter provides — where frames are written and how disconnects arrive. */ export interface SseSink { /** * Write a frame. Return `false` when the underlying buffer is full (Node's * `res.write` convention) so `send` can surface backpressure; returning * `void` is treated as "written, no pressure". */ write(frame: string): boolean | void; end(): void; onClose(listener: () => void): void; } /** * Drive a producer against a sink: exposes an `SseStream` to the producer, * relays client disconnects, and ends the sink when the producer finishes. * Shared by every adapter so behaviour is identical. */ export declare function driveSse(producer: SseProducer, sink: SseSink): Promise; export {};