/** One RPC method: argument tuple and result. */ export interface RpcMethod { readonly args: A; readonly result: R; } /** Method table one side of a link (or one remote object class) serves. */ export type RpcApi = Record>; /** * Opaque wire frame; only {@link RpcLink} produces and consumes these. * `raw`: `a`/`v` carries no wire tags (its encoding was the identity), so the * receiver skips the decode walk. */ export type RpcFrame = { t: "call"; id: number; m: string; a: unknown[]; raw: boolean; } | { t: "sync"; id: number; m: string; a: unknown[]; raw: boolean; } | { t: "ok"; id: number; v: unknown; raw: boolean; } | { t: "err"; id: number; e: unknown; } | { t: "note"; m: string; a: unknown[]; raw: boolean; } | { t: "abort"; sig: number; reason: unknown; } | { t: "settle"; p: number; ok: boolean; v: unknown; raw: boolean; } | { t: "release"; ids: Array<[number, number]>; }; /** Bidirectional message channel underneath a link. */ export interface RpcEndpoint { send(frame: RpcFrame): void; /** Attach the frame handler; returns a detach function. */ listen(handler: (frame: RpcFrame) => void): () => void; } /** Shared memory and reply port that let a worker block on main-thread replies. */ export interface RpcSyncChannel { /** Reply port; only ever drained with `receiveMessageOnPort`, never listened to. */ readonly port: MessagePort; /** Index 0 increments after every reply posted to {@link port}. */ readonly counter: Int32Array; } /** Custom wire representation for a value type both threads can rebuild (e.g. session tokens). */ export interface RpcCodec { /** Unique tag on the wire. */ readonly kind: string; is(value: unknown): value is T; encode(value: T): unknown; decode(data: unknown): unknown; } /** Calls from a remote-object proxy to its target object on the other thread. */ export interface RemoteObject

{ /** Id of the target on this link; stable for the target's lifetime. */ readonly id: number; call(method: K, ...args: P[K]["args"]): Promise; /** Worker importers only; the exporter's method MUST be synchronous. */ callSync(method: K, ...args: P[K]["args"]): P[K]["result"]; notify(method: K, ...args: P[K]["args"]): void; } /** Options for {@link RpcLink.exportClass}. */ export interface RpcExportClassOptions { /** Importers send arguments of this class's methods lossily, as for {@link rpcLossy} functions. */ readonly lossy?: boolean; } /** Exporter-side implementation of a remote-object protocol over target `T`. */ export type RpcServe = { [K in keyof P]: (target: T, ...args: P[K]["args"]) => P[K]["result"] | Promise; }; /** Options for {@link RpcLink}. */ export interface RpcLinkOptions { /** Label for logs and error messages, e.g. `main↔w1`. */ readonly name: string; /** Worker side: enables {@link RpcLink.callSync}. */ readonly sync?: RpcSyncChannel; /** Main side: where sync replies go. */ readonly syncReplies?: RpcSyncChannel; /** Upper bound for one sync call; a wedged main thread fails the call instead of hanging the worker. */ readonly syncTimeoutMs?: number; } /** A value could not be encoded for the wire. */ export declare class RpcEncodeError extends Error { constructor(message: string); } /** The link closed before the call settled, or a call was attempted after close. */ export declare class RpcClosedError extends Error { constructor(message: string); static forLink(link: string, reason?: string): RpcClosedError; } /** A synchronous call got no reply within its deadline. */ export declare class RpcTimeoutError extends Error { constructor(message: string); } type ErrorFactory = (message: string) => Error; /** * Register how to rebuild an error class by its `name` on the receiving * thread, so `instanceof` checks (abort errors, busy errors) survive a hop. * Own enumerable cloneable fields are copied onto the rebuilt error. */ export declare function registerRpcError(name: string, create: ErrorFactory): void; /** * Mark a function so a worker importing it calls it synchronously. Only * meaningful for functions the main thread sends to a worker. */ export declare function rpcSync unknown>(fn: F): F; /** * Mark a function so importers send its arguments lossily: a class instance * no codec or exported class covers arrives as a plain record of its own * enumerable fields plus the values of the getters its prototypes define * (accessors are how classes expose data, e.g. a tool's `description`; * methods are behavior and are left out), copied recursively with shared * references and cycles preserved. Functions, proxies, signals, promises, * errors, exported-class objects, and codec values cross exactly as in * strict mode. For callbacks that must observe whatever in-process callers * pass (telemetry hooks); combine with {@link rpcSync} freely. */ export declare function rpcLossy unknown>(fn: F): F; /** * Whether `value` is a proxy this thread imported over some link (a remote * function or an `importClass` object), so sending it back travels as a * reference to the other thread's original. */ export declare function isRpcProxy(value: unknown): boolean; /** One end of a main↔worker RPC channel. See the module doc. */ export declare class RpcLink { #private; readonly name: string; constructor(endpoint: RpcEndpoint, options: RpcLinkOptions); /** Whether {@link close} ran (locally or because the transport died). */ get closed(): boolean; /** Whether this end can make {@link callSync} calls (worker ends only). */ get canCallSync(): boolean; /** Serve `method` for {@link call} and {@link callSync} from the other side. */ handle(method: K, fn: (...args: Local[K]["args"]) => Local[K]["result"] | Promise): void; /** Serve `method` for {@link notify} from the other side; the return value is ignored. */ onNotify(method: K, fn: (...args: Local[K]["args"]) => void): void; /** Register a codec; the other side must register one with the same `kind`. */ codec(codec: RpcCodec): void; /** * Send instances matching `is` by reference: the other side, having called * {@link importClass} with the same `kind`, receives one canonical proxy per * object and calls `serve` through it. */ exportClass(kind: string, is: (value: unknown) => value is T, serve: RpcServe, options?: RpcExportClassOptions): void; /** Build proxies for objects the other side exports under `kind` (see {@link exportClass}). */ importClass(kind: string, create: (remote: RemoteObject

) => R): void; /** Call `method` on the other side. Rejects with the remote error, or {@link RpcClosedError}. */ call(method: K, ...args: Remote[K]["args"]): Promise; /** Fire-and-forget `method` on the other side. Dropped silently after close. */ notify(method: K, ...args: Remote[K]["args"]): void; /** * Worker only: call `method` on the main thread and block until it replies. * The main-side handler MUST be synchronous. * * @throws {RpcClosedError} after close. * @throws {RpcTimeoutError} when main does not reply within the deadline. * @throws the remote handler's error, rebuilt. */ callSync(method: K, ...args: Remote[K]["args"]): Remote[K]["result"]; /** Close the link: reject pending calls, drop handles, detach the endpoint. Idempotent. */ close(reason?: string): void; } export {};