computers
Version:
computer←→computer
49 lines (46 loc) • 1.71 kB
TypeScript
import { A as API, W as WrapAPIToClient } from '../caller-CuhDuQrt.js';
interface MessageEventLike<T> {
data: T;
}
interface WebSocketLike {
send(data: string): void;
addEventListener(event: 'message', listener: (event: MessageEventLike<string>) => void): void;
removeEventListener(event: 'message', listener: (event: MessageEventLike<string>) => void): void;
}
interface WebSocketConnectionOptions {
socket: WebSocketLike;
}
/**
* Create a new WebSocket transport for making **outgoing** requests only. No API
* is exposed to the remote side.
*
* @example
* ```ts
* const client = websocket<{hello: () => Promise<string>}>({socket});
* const msg = await client.hello();
* ```
*
* @param options Options for the WebSocket connection
* @returns A proxy that asynchronously invokes the remote API methods
*/
declare function websocket<T extends API>(options: WebSocketConnectionOptions): WrapAPIToClient<T>;
/**
* Create a **bi-directional** WebSocket transport. An `api` object is exposed to
* the remote side while you simultaneously gain a proxy for calling the remote
* API.
*
* @example
* ```ts
* const api = {
* ping: () => 'pong',
* };
* const peer = websocket<{add(a:number,b:number):Promise<number>}>({socket}, api);
* const sum = await peer.add(1, 2);
* ```
*
* @param options Options for the WebSocket connection
* @param api The local API surface to expose to the other side
* @returns A proxy that asynchronously invokes the remote API
*/
declare function websocket<T extends API>(options: WebSocketConnectionOptions, api: API): WrapAPIToClient<T>;
export { type MessageEventLike, type WebSocketConnectionOptions, type WebSocketLike, websocket };