import { r as ConnectionKey } from "./connection-key-CvX95c4J.js"; import { Channel, ChannelMsg, FrameTrace, GeneratedChannel, Transport } from "@kyneta/transport"; //#region src/connection.d.ts /** * Response to send back to the client after processing a POST. */ interface SsePostResponse { status: 200 | 202 | 400; body: { ok: true; } | { pending: true; } | { error: string; }; } /** * Result of parsing a POST body. * * Discriminated union describing what happened: * - "messages": Complete message(s) decoded, ready to deliver * - "pending": Fragment received, waiting for more * - "error": Decode/reassembly error */ type SsePostResult = { type: "messages"; messages: ChannelMsg[]; response: SsePostResponse; } | { type: "pending"; response: SsePostResponse; } | { type: "error"; response: SsePostResponse; }; /** * Default fragment threshold in characters for outbound SSE messages. * 60K chars provides a safety margin below typical infrastructure limits. */ declare const DEFAULT_FRAGMENT_THRESHOLD = 60000; /** * Configuration for creating an SseConnection. */ interface SseConnectionConfig { /** * Fragment threshold in characters. Messages larger than this are fragmented. * Set to 0 to disable fragmentation. * Default: 60000 (60K chars) */ fragmentThreshold?: number; /** * Largest inbound message payload, in bytes. Default: * `DEFAULT_MAX_MESSAGE_SIZE`. */ maxMessageSize?: number; /** Optional DevTools per-frame trace hook, threaded into the Pipeline. */ onFrame?: (ev: FrameTrace) => void; } /** * Represents a single SSE connection to a peer (server-side). * * Manages encoding, framing, fragmentation, reassembly, and alias * resolution for one connected client. Created by * `SseServerTransport.registerConnection()`. * * Uses Pipeline<"text", "binary"> — asymmetric encoding: * - Send (text): ChannelMsg → text frame → SSE data event * - Receive (binary): POST body (Uint8Array) → ChannelMsg */ declare class SseConnection { #private; /** * The key that routes this connection's POSTs. A secret: send it only as * the stream's first event, and never log it. */ readonly key: ConnectionKey; constructor(key: ConnectionKey, config?: SseConnectionConfig); /** * Set the channel reference. * Called by the transport when the channel is created. * @internal */ _setChannel(channel: Channel): void; /** * Set the function to call when sending messages to this peer. * * The function receives a fully encoded text frame string. * The framework integration just wraps it in SSE syntax: * - Express: `res.write(\`data: \${textFrame}\\n\\n\`)` * - Hono: `stream.writeSSE({ data: textFrame })` * * @param sendFn Function that writes a text frame string to the SSE stream */ setSendFunction(sendFn: (textFrame: string) => void): void; /** * Set the function to call when this connection is disconnected. */ setDisconnectHandler(handler: () => void): void; /** * Send a ChannelMsg to the peer through the SSE stream. * * Runs the Pipeline<"text"> send path: * ChannelMsg → Pipeline.send() → text frame(s) → sendFn() * * Fragmentation is transparent to callers — the pipeline splits * large frames into multiple sendFn calls automatically. */ send(msg: ChannelMsg): void; /** * Handle an inbound POST body through the Pipeline<"binary"> receive path. * * Pipeline: Uint8Array → Pipeline.receive() → ChannelMsg[] * * Messages that fail alias resolution or wire-message validation are * surfaced as `type: "error"` results — the connection continues * processing remaining messages. This matches the error-dropping * behavior of every other transport. * * @param body - Binary POST body (Uint8Array) * @returns Discriminated result: `"messages"`, `"pending"`, or `"error"` */ handlePostBody(body: Uint8Array): SsePostResult; /** * Receive a message from the peer and route it to the channel. * * Called by the framework integration after processing a POST body * through `handlePostBody`. */ receive(msg: ChannelMsg): void; /** * Disconnect this connection. */ disconnect(): void; /** * Dispose of resources held by this connection. Must be called when the * connection is closed to prevent timer leaks. Later sends do nothing. */ dispose(): void; } //#endregion //#region src/server-transport.d.ts /** * Options for the SSE server transport. */ interface SseServerTransportOptions { /** * Fragment threshold in characters. Messages larger than this are fragmented * into multiple SSE events. * Set to 0 to disable fragmentation. * Default: 60000 (60K chars) */ fragmentThreshold?: number; /** * Largest inbound message payload, in bytes. Default: * `DEFAULT_MAX_MESSAGE_SIZE`. */ maxMessageSize?: number; } /** * SSE server transport. * * Framework-agnostic — works with any HTTP framework through the * `SseConnection.setSendFunction()` callback. Use `registerConnection()` * to integrate with your framework's SSE endpoint handler. * * Each event stream becomes an `SseConnection` with one channel bound to * it: the channel's `send` and `close` act on that connection directly. * The only index, by the server-minted `ConnectionKey`, routes POSTs. * * The connection handshake: * 1. Client opens EventSource (GET /events) * 2. Server calls `registerConnection()` → mints a key, creates a channel, * and sends the key as the stream's first event * 3. Client reads the key → sends establish (POST, naming the key) * 4. Server admits the client → answers establish (SSE) * * The server does NOT call `establishChannel()`: its channels are acceptors, * answering the client's establish. */ declare class SseServerTransport extends Transport { #private; /** Largest inbound message payload, in bytes. */ readonly maxMessageSize: number; constructor(options?: SseServerTransportOptions); protected generate(connection: SseConnection): GeneratedChannel; onStart(): Promise; onStop(): Promise; /** * Register a new event stream: mint its key and create its channel. * * The caller must send `connection.key` as the stream's first event * (`event: kyneta-connection`), and route POSTs naming that key with * `connectionFor()`. Returns `undefined` when the transport is not * accepting (stopping or stopped); the caller should refuse the request. */ registerConnection(): SseConnection | undefined; /** * Forget a closed connection and remove its channel. Idempotent. Call it * when the event stream's request closes. */ unregisterConnection(connection: SseConnection): void; /** * The connection a POST names by its `X-Kyneta-Connection` header, if * that key is live. */ connectionFor(key: string): SseConnection | undefined; /** * Get the number of open connections. */ get connectionCount(): number; } //#endregion export { SseConnectionConfig as a, SseConnection as i, SseServerTransportOptions as n, SsePostResponse as o, DEFAULT_FRAGMENT_THRESHOLD as r, SsePostResult as s, SseServerTransport as t }; //# sourceMappingURL=server-transport-CvFEgWFf.d.ts.map