import type { WebSocket as WSWebSocket } from 'ws'; import type { InitRequest, RPCMessage, RPCMessages, ServiceSignatureType, TunnelMessage } from '../proto/api'; import type { Logger } from './general'; import type { RPCEvent, RPCEventMap, RPCEventType, RPCRequestData, RPCResponseData, RPCType } from './rpc'; import type { TCPSocketProperties, Tunnel } from './tunnel'; /** * Any WebSocket implementation -- either the native * WebSocket or the WebSocket from the `ws` package. */ export type AnyWebSocket = WebSocket | WSWebSocket; export type AnyWebSocketConstructor = new (url: string | URL) => AnyWebSocket; export type IWitnessClientCreateOpts = { /** * Witness WS URL */ url: string | URL; signatureType?: ServiceSignatureType; logger?: Logger; /** * Initial messages to send to the server * in the query parameter used to establish * the connection. */ initMessages?: Partial[]; /** * Provide a custom WebSocket implementation, * will use the native WebSocket if not provided. */ Websocket?: AnyWebSocketConstructor; }; /** * Base layer for the WebSocket connection on * the client and server. */ export declare class IWitnessSocket { metadata: InitRequest; logger: Logger; /** * Is the WebSocket connection open? */ isOpen: boolean; /** * Has the WebSocket connection been closed */ isClosed: boolean; /** * Whether the WebSocket has been initialised * by receiving an "init-response" message. */ isInitialised: boolean; /** * Sends RPC messages to the server in a single packet. * If the ID is not provided, it will be generated. * * Promisify the `send` method if using the `ws` package's * WebSocket implementation. */ sendMessage(...msgs: Partial[]): Promise; /** * Sends a "terminateConnectionAlert" message to the server * with the specified error (if any), if the connection is * still open and then closes the connection. */ terminateConnection(err?: Error): Promise; /** * Use this to listen to events on the WebSocket. */ addEventListener(type: K, listener: (data: RPCEvent) => void): void; removeEventListener(type: K, listener: (data: RPCEvent) => void): void; /** * Syntactic sugar for emitting events on the WebSocket. * Wraps the `makeRpcEvent` call internally */ dispatchRPCEvent(type: K, data: RPCEventMap[K]): void; /** * Starts processing RPC messages from the WebSocket * & emits events for each message type. These can be * captured by the `addEventListener` method. * * Will also listen to "error" & "close" events on the WebSocket * and emit a "witness-error" event with the error. * So, you only need to listen to the "witness-error" * event to capture anything you're interested in. */ constructor(ws: WebSocket, metadata: InitRequest, logger: Logger); } export declare class IWitnessServerSocket extends IWitnessSocket { /** * Unique identifier for this WebSocket connection */ sessionId: number; /** * Set of tunnels this client created. Only available * when WS is created by the server */ tunnels: { [id: TunnelMessage['tunnelId']]: Tunnel; }; /** * Fetches a tunnel by its ID. * If the tunnel does not exist, it will throw an error. */ getTunnel(tunnelId: TunnelMessage['tunnelId']): Tunnel; } export declare class IWitnessClient extends IWitnessSocket { constructor(opts: IWitnessClientCreateOpts); /** * Waits for a particular message to come in. * If the connection is closed before the message is received, * the promise will reject. */ waitForResponse(id: number): Promise>; /** * Make an RPC request to the other end of the WebSocket. */ rpc(type: T, request: Partial>): Promise>; /** * Waits for the "init" request to be responded to */ waitForInit(): Promise; } interface WebSocketWithServerSocket { /** * Our RPC socket instance */ serverSocket?: IWitnessServerSocket; } declare module 'ws' { namespace WebSocket { interface WebSocket extends WebSocketWithServerSocket { } } interface WebSocket extends WebSocketWithServerSocket { } } export {};