import { type HttpLinkOptions } from "../link/http-link.js"; import type { WebSocketConnection } from "../ws/client.js"; export type FetchHandler = (request: Request) => Response | Promise; export interface HandlerEndpointOptions extends HttpLinkOptions { /** Asks whoever attaches this endpoint to do it again (its links died). */ requestRelink?: () => void; /** Pokes the ServiceWorker so that it stays awake while calls are in flight. */ keepAlive?: () => void; /** Period of `keepAlive` while calls are in flight, in ms. Default 20 s. */ keepAliveMs?: number; /** How often a waiting call asks for a link again, in ms. Default 2 s. */ relinkRetryMs?: number; /** How long a call waits for a link before failing, in ms. Default 30 s. */ relinkTimeoutMs?: number; /** How long a socket waits for the far handler's answer, in ms. Default 15 s. */ wsConnectTimeoutMs?: number; /** * How long a link replaced by a newer one of the same router keeps serving * what it has, in ms. The ServiceWorker closes it first, once its requests * are done; this only bounds a link it forgot. Default 30 s. */ supersededLinkMs?: number; } export interface EndpointLinkPorts { down: MessagePort; ctl?: MessagePort; up?: MessagePort; } /** The message calls through a lost ServiceWorker fail with. */ export declare const ROUTER_LOST_MESSAGE = "The ServiceWorker was lost while this call was in flight"; /** * The handler's end of its links to the ServiceWorker, the same in a page and * in a Dedicated Worker. * * - Serves `handler` on each link's `down` port, and answers WebSocket * upgrades arriving on its `ctl` port by running them through `handler`. * - Sends requests through the router on `up` (`fetch`) and opens sockets to * other handlers over `ctl` (`connectWebSocket`). * - Watches the ServiceWorker's router lock. When the ServiceWorker dies, the * links made for it are dropped: calls in flight on them fail with * `LinkLostError` ("may or may not have been applied") instead of hanging, * and new calls wait for a link, asking for one (`requestRelink`) every * `relinkRetryMs` until it comes. * - While calls are in flight, calls `keepAlive` every `keepAliveMs`: port * messages are not events, so without it the browser may stop the * ServiceWorker in the middle of a long call. */ export declare class HandlerEndpoint { readonly handler: FetchHandler; readonly options: HandlerEndpointOptions; private readonly _links; private _waiters; private readonly _sockets; private _keepAliveTimer?; private _closed; constructor(handler: FetchHandler, options?: HandlerEndpointOptions); /** Whether any link is live. */ get linked(): boolean; /** Installs a fresh link made for the router holding `routerLock`. */ link(key: string, ports: EndpointLinkPorts, routerLock?: string): Promise; /** Drops the links attached under `key` (all of them without a key). */ unlink(key?: string, reason?: Error): Promise; /** The routers this endpoint has live links for. */ routerLocks(): Set; /** * Sends `request` through the ServiceWorker's router, as if the browser had * fetched it: it reaches the handler its url routes to. */ fetch(request: Request): Promise; /** Opens a socket to the handler `url` routes to, through the ServiceWorker. */ connectWebSocket(url: string, protocols?: string[]): Promise; close(): Promise; private _drop; private _routerLost; /** A live link with `port`, waiting (and asking) for one when there is none. */ private _linkWith; private _startKeepAlive; private _onControl; private _acceptSocket; } //# sourceMappingURL=handler-endpoint.d.ts.map