import { WebSocket as WebSocket$1 } from 'ws'; import { z } from 'zod'; type WebSocketType = WebSocket$1 | WebSocket; type RpcMethodHandler = (request: TRequest, sendUpdate: (update: TUpdate) => void) => Promise; interface RpcMethodDefinition { handler: RpcMethodHandler; } type RpcMethods = Record>; type WebSocketMessageType = 'request' | 'response' | 'update' | 'error'; interface BaseWebSocketMessage { id: string; messageType: WebSocketMessageType; method?: string; } interface RequestMessage extends BaseWebSocketMessage { messageType: 'request'; method: string; payload: T; } interface ResponseMessage extends BaseWebSocketMessage { messageType: 'response'; method: string; payload: T; } interface UpdateMessage extends BaseWebSocketMessage { messageType: 'update'; method: string; payload: T; } interface ErrorMessage extends BaseWebSocketMessage { messageType: 'error'; error: { message: string; code?: string; }; } type WebSocketMessage = RequestMessage | ResponseMessage | UpdateMessage | ErrorMessage; interface WebSocketBridgeOptions { maxReconnectAttempts?: number; reconnectDelay?: number; requestTimeout?: number; } interface PendingRequest { resolve: (value: TResponse) => void; reject: (reason: Error) => void; timeout: NodeJS.Timeout; onUpdate?: (update: TUpdate) => void; } /** * Base class for the WebSocket RPC Bridge that handles bidirectional * method registration and invocation with support for streaming updates */ declare abstract class WebSocketRpcBridge { protected ws: WebSocketType | null; protected pendingRequests: Map; protected reconnectAttempts: number; protected options: WebSocketBridgeOptions; protected methods: RpcMethods; protected isIntentionalClose: boolean; constructor(options?: WebSocketBridgeOptions); /** * Register RPC method handlers * @param methodHandlers Object containing method handlers */ register>>(methodHandlers: T): void; /** * Generic method to call a remote procedure with support for streaming updates * @param method Method name to call * @param payload Request payload * @param onUpdate Optional callback for progress updates * @returns Promise resolving with the response */ protected callMethod(method: string, payload: TRequest, onUpdate?: (update: TUpdate) => void): Promise; /** * Sets up WebSocket event handlers * @param ws WebSocket instance */ protected setupWebSocketHandlers(ws: WebSocketType): void; /** * Handles incoming WebSocket messages * @param message The message to handle */ protected handleMessage(message: WebSocketMessage): void; /** * Handle incoming requests by invoking the registered method * @param message Request message */ protected handleRequest(message: RequestMessage): Promise; /** * Handle response messages by resolving the pending request * @param id Request ID * @param payload Response payload */ protected handleResponse(id: string, payload: any): void; /** * Handle update messages by calling the update callback * @param id Request ID * @param payload Update payload */ protected handleUpdate(id: string, payload: any): void; /** * Handle error messages by rejecting the pending request * @param id Request ID * @param error Error message */ protected handleError(id: string, error: string): void; /** * Send a response message * @param id Request ID * @param method Method name * @param payload Response payload */ protected sendResponse(id: string, method: string, payload: any): void; /** * Send an update message for streaming * @param id Request ID * @param method Method name * @param payload Update payload */ protected sendUpdate(id: string, method: string, payload: any): void; /** * Send an error message * @param id Request ID * @param errorMessage Error message */ protected sendError(id: string, errorMessage: string): void; /** * Handle disconnection by attempting to reconnect */ protected handleDisconnect(): void; /** * Clear all pending requests with an error * @param error Error to reject with */ protected clearPendingRequests(error: Error): void; /** * Abstract method for reconnection logic */ protected abstract reconnect(): void; /** * Close the WebSocket connection * @returns Promise that resolves when the connection is closed */ close(): Promise; } /** * Type representing a Zod RPC method contract with request, response, and optional update schemas */ interface ZodRpcMethodContract { request: TRequest; response: TResponse; update?: TUpdate; } /** * Type for a map of endpoint methods using Zod schemas */ type ZodEndpointMethodMap = Record; /** * Type for the complete bridge contract with optional server and client method maps */ interface ZodBridgeContract { server?: ZodEndpointMethodMap; client?: ZodEndpointMethodMap; } /** * Helper type to infer the TypeScript type from a Zod schema */ type InferZodType = z.infer; /** * Helper type to extract the request type from a Zod method contract */ type InferRequestType = InferZodType; /** * Helper type to extract the response type from a Zod method contract */ type InferResponseType = InferZodType; /** * Helper type to extract the update type from a Zod method contract */ type InferUpdateType = T['update'] extends z.ZodType ? InferZodType : never; /** * Creates a strongly-typed bridge contract with Zod schemas */ declare function createBridgeContract(contract: T): T; /** * Type for method implementations using inferred types from Zod schemas */ type ZodMethodImplementations = { [K in keyof T]: (request: InferRequestType, context: { sendUpdate: T[K]['update'] extends z.ZodType ? (update: InferUpdateType) => void : never; }) => Promise>; }; /** * Type for method calls using inferred types from Zod schemas */ type ZodMethodCalls = { [K in keyof T]: T[K]['update'] extends z.ZodType ? (request: InferRequestType, options: { onUpdate: (update: InferUpdateType) => void; }) => Promise> : (request: InferRequestType) => Promise>; }; /** * Base class for Zod-enabled bridges that adds schema validation */ declare class ZodTypedBridge { protected bridge: B; protected contract: { serves: Serves; consumes: Consumes; }; call: ZodMethodCalls; constructor(bridge: B, contract: { serves: Serves; consumes: Consumes; }); private callMethod; register(implementations: ZodMethodImplementations): void; close(): Promise; } export { type BaseWebSocketMessage as B, type ErrorMessage as E, type PendingRequest as P, type RpcMethodHandler as R, type UpdateMessage as U, WebSocketRpcBridge as W, type ZodBridgeContract as Z, type RpcMethodDefinition as a, type RpcMethods as b, type WebSocketMessageType as c, type RequestMessage as d, type ResponseMessage as e, type WebSocketMessage as f, type WebSocketBridgeOptions as g, createBridgeContract as h, ZodTypedBridge as i };