import type { HttpHandler } from "@statewalker/webrun-http-streams"; import type { MessageTarget } from "../core/message-target.js"; import { type HttpLinkOptions } from "../link/http-link.js"; import { type PortWebSocketClass, type WebSocketClassOptions, type WebSocketConnection } from "../ws/client.js"; export * from "./split-service-url.js"; /** * Returns a MessagePort that transparently bridges messages to/from the * page's ServiceWorker: the one controlling the page, or — when the page is * not controlled (a hard reload, or a page Firefox left uncontrolled) — the * active worker of `registration`, which answers messages all the same. */ export declare function newServiceWorkerPort(registration?: ServiceWorkerRegistration): MessagePort; export interface InitServiceWorkerOptions { swUrl: string; scopeUrl?: string; type?: WorkerType; /** * Upper bound, in ms, for the wait for the worker to activate; past it the * promise rejects with a `ServiceWorkerControlError`. Default * `DEFAULT_SERVICE_WORKER_TIMEOUT` (30 s). */ timeout?: number; } /** * Registers a ServiceWorker and resolves with it once it is activated: the * worker controlling the page, or the registration's active worker when the * page is not controlled. Messaging works either way, which is all the relay * needs; nothing here waits for control, because an uncontrolled page (hard * reload; Firefox) may never get it. Rejects with a * `ServiceWorkerControlError` if activation takes longer than `timeout`. */ export declare function initServiceWorker(options: InitServiceWorkerOptions): Promise; export interface ServiceOptions extends HttpLinkOptions { key: string; /** * Where this service is mounted on the relay origin, e.g. `/` or `/peers/`. * Omitted, the service stays reachable at `/~/`, as before mounts. */ path?: string; port: MessageTarget; } /** * What a service answers to a `CONNECT` that speaks `HTTP_LINK_PROTOCOL`. A * service of version 1 answers `true` instead, and the caller then falls back * to version 1's transport: both ends of a call must speak the same one, and * the relay, the caller and the service may each run another version. */ export interface ConnectReply { accepted: true; wire: string; } /** A `CONNECT` asking a service for a socket. */ export interface WebSocketConnectParams { type: "ws"; key: string; url: string; protocols?: string[]; headers?: [string, string][]; } /** * Serves a service's side of one `CONNECT`: an HTTP link on `callPort`, or a * WebSocket upgrade run through `handler`. */ export declare function answerConnect(handler: HttpHandler, data: unknown, callPort: MessagePort | undefined, options?: HttpLinkOptions): Promise; /** * Registers `handler` as the server for the given service `key` on the relay: * it answers HTTP requests and WebSocket upgrades. Returns a function that * unregisters the service; its promise settles once the relay acknowledged. */ export declare function initHttpService(handler: HttpHandler, { key, path, port, streamTimeout }: ServiceOptions): Promise<() => Promise>; /** * Sends a `Request` to the service registered under `key` on the relay and * resolves with the corresponding `Response`, whose body streams with * backpressure. */ export declare function callHttpService(request: Request, { key, port, streamTimeout }: ServiceOptions): Promise; /** * Sends `request` on a port a service accepted with `reply`: over an HTTP link * when the service speaks `HTTP_LINK_PROTOCOL`, over version 1's transport * when it answered `true`. The link is closed once the response is done. */ export declare function sendOverConnection(port: MessagePort, reply: boolean | ConnectReply, request: Request, options?: HttpLinkOptions): Promise; /** * Opens the transport of a socket to the service registered under `key` on * the relay: the service runs the upgrade through its handler, and the * socket's port comes back through the relay. After that, the socket runs * between the caller and the service directly. */ export declare function callWebSocketService(url: string | URL, protocols: string[], { key, port, timeout }: Pick & { timeout?: number; }): Promise; /** * A WHATWG `WebSocket` class whose sockets reach the service registered under * `key` on the relay. */ export declare function relayWebSocketClass(service: Pick, options?: WebSocketClassOptions): PortWebSocketClass; export interface RelayWindowHandlerOptions { swUrl?: string; scopeUrl?: string; /** Passed to `initServiceWorker`: how long to wait for the relay worker to activate. */ timeout?: number; } /** * Returns a `window.onmessage` handler for use inside the relay iframe: * it accepts a CONNECT message, starts the relay ServiceWorker, and bridges * the parent's MessagePort with the SW. If the worker cannot be started, every * call the parent makes on that port is answered with the error, so the * parent's `initHttpService` / `callHttpService` reject instead of waiting. */ export declare function getRelayWindowMessageHandler({ swUrl, scopeUrl, timeout, }?: RelayWindowHandlerOptions): (ev: MessageEvent) => Promise; export interface RemoteRelayChannelOptions { baseUrl?: URL; url?: URL; container?: HTMLElement; } export interface RemoteRelayChannel { baseUrl: URL; port: MessagePort; close(): void; } /** * Embeds a hidden relay iframe, establishes a MessageChannel with it, and * returns the port to be used with `initHttpService` / `callHttpService`. */ export declare function newRemoteRelayChannel({ baseUrl, url, container, }?: RemoteRelayChannelOptions): Promise; export interface InitializeConnectionOptions { key: string; communicationPort: MessageTarget; [key: string]: unknown; } export declare function initializeConnection({ key, communicationPort, ...options }: InitializeConnectionOptions): Promise; export interface RegisterConnectionsHandlerOptions { key: string; /** Where this service is mounted; see `ServiceOptions.path`. */ path?: string; /** * Answers one `CONNECT`. Its result is the reply; wrap a result carrying a * port in `withTransfer`. */ handler: (event: MessageEvent, data: unknown, port: MessagePort) => unknown | Promise; communicationPort: MessageTarget; } /** * Registers `key` on the relay and answers its `CONNECT`s with `handler`. * Resolves with a function that unregisters it. That function's promise * settles once the relay acknowledged the `UNREGISTER`, so a caller can stop * its services and only then close the connection: closing first tore the * ports down before the message landed, and left the mount behind. */ export declare function registerConnectionsHandler({ key, path, handler, communicationPort, }: RegisterConnectionsHandlerOptions): Promise<() => Promise>; //# sourceMappingURL=index.d.ts.map