import { UpgradeWebSocket } from "hono/ws"; import { TheLink, Tunnel, type Deserialize, type Serialize, type Subscriber } from "@the-link/core"; import { HonoOptions } from "hono/hono-base"; import ServerSocket from "./server-socket.js"; import { ServerStreams } from "./streams.js"; import { BlankEnv } from "hono/types"; import { Hono } from "hono"; /** * Hono server adapter for TheLink over HTTP and WebSocket endpoints. * * Provides a POST `/publish` endpoint for client-to-server events and an * optional WebSocket `/subscribe` endpoint for long-lived server-to-client * communication. Each accepted WebSocket connection gets a dedicated * private Link instance, allowing application code to route events per client. * * The adapter uses UTF-8 JSON bytes by default, with hooks for custom codecs * such as compression, encryption, or type preservation. * * Connection lifecycle events: * - `subscribe`: Published on the internal tunnel when a socket connects * - `unsubscribe`: Published through the connection Link when a socket closes * * @example * ```typescript * const server = new HttpServer() * * server.prepareConnection(upgradeWebSocket) * * server.onSubscribe((socket) => { * socket.autoJoin(server) * }) * ``` */ export declare class HttpServer extends TheLink { /** * Internal tunnel for server-only lifecycle events. * * Keeps connection coordination separate from application inbound and * outbound event traffic. */ readonly $internal: Tunnel; /** * Hono application that owns the HTTP and WebSocket routes. */ readonly app: Hono; /** * Controls whether handler errors are exposed to clients. * * Keep disabled in production to avoid leaking internal details. */ private debugging; private serialize; private deserialize; /** * Active WebSocket-backed Links keyed by connection UUID. */ readonly sockets: Map; /** * Streams crossing this server's messages as references, with their chunks on their own requests. */ readonly streams: ServerStreams; /** * Initialize an HttpServer with an optional Hono configuration object. * * @param options Hono application options passed to the underlying app */ constructor(options?: HonoOptions); /** * Register server communication routes on the Hono application. * * Always registers the POST `/publish` endpoint. When an Hono WebSocket * upgrader is provided, also registers GET `/subscribe` for WebSocket * subscriptions. * * @param upgradeWebSocket Optional Hono WebSocket upgrade helper */ prepareConnection(upgradeWebSocket?: UpgradeWebSocket): void; /** * Enable detailed client-facing error messages. */ enableDebugging(): void; /** * Disable detailed client-facing error messages. */ disableDebugging(): void; /** * Configure the serialization function used for HTTP and WebSocket payloads. * * @param serialize Custom function that converts values to bytes */ setSerialize(serialize: Serialize): void; /** * Configure the deserialization function used for HTTP and WebSocket payloads. * * @param deserialize Custom function that parses values from bytes */ setDeserialize(deserialize: Deserialize): void; /** * Register a handler for accepted WebSocket subscriptions. * * The subscriber receives the private Link created for the connection and can * attach handlers, join tunnels, or return connection initialization data. * * @param subscriber Handler invoked with each new Link * @returns Function that removes this subscriber */ onSubscribe(subscriber: Subscriber<[ServerSocket]>): () => void; /** * Process a client POST request and publish it through the inbound tunnel. * * @param context Hono request context containing serialized event data * @returns Serialized event handler results, or a 500 error response */ private publishHandler; /** * Create the Hono WebSocket event handlers for a new subscription request. * * @param context Hono request context containing subscription query data * @returns WebSocket lifecycle callbacks for this connection */ private webSocketHandler; /** * Accept a WebSocket connection and create its Link facade. * * Reads the serialized `payload` query parameter, initializes a Link, * publishes the internal subscribe event, and sends the subscription response * back to the client. * * @param context Hono request context containing subscription payload data * @param socket Accepted WebSocket context * @param uuid Connection identifier used for socket tracking */ private webSocketOpenHandler; /** * Route a WebSocket message through the matching Link inbound tunnel. * * @param event WebSocket message containing a serialized event envelope * @param uuid Connection identifier used to find the Link */ private webSocketMessageHandler; /** * Clean up a closed WebSocket connection. * * Publishes the socket-level unsubscribe event before removing the connection * from the active socket map. * * @param event WebSocket close event * @param uuid Connection identifier used to find the Link */ private webSocketCloseHandler; }