///
import EventEmitter from "events";
import { Validator } from "./validator";
import WebSocket from "ws";
import EventBuffer from "./event-buffer";
export interface RPC_ClientOptions {
identity: string;
reconnect: boolean;
callTimeoutMs: number;
pingIntervalMs: number;
deferPingsOnActivity: boolean;
respondWithDetailedErrors: boolean;
callConcurrency: number;
strictMode: boolean | string[];
strictModeValidators: Validator[];
maxBadMessages: number;
protocols: string[];
endpoint?: string;
password: string | null;
wsOpts: any;
headers: any;
maxReconnects: number;
query?: string | Record;
backoff: {
initialDelay: number;
maxDelay: number;
factor: number;
randomisationFactor: number;
};
}
export interface IHandlersOption {
messageId?: string;
method?: string;
params?: Record;
signal?: AbortSignal;
reply?: unknown;
}
type IHandlers = ({ params, reply, method, signal, messageId, }: IHandlersOption) => Promise>;
declare class RPC_Client extends EventEmitter {
protected _identity?: string;
private _wildcardHandler;
private _handlers;
protected _state: number;
private _callQueue;
protected _ws?: WebSocket;
private _wsAbortController?;
private _keepAliveAbortController?;
private _pendingPingResponse;
private _lastPingTime;
private _closePromise?;
private _protocolOptions;
protected _protocol?: string;
private _strictProtocols;
private _strictValidators?;
private _pendingCalls;
private _pendingResponses;
private _outboundMsgBuffer;
private _connectedOnce;
private _backoffStrategy?;
private _badMessagesCount;
private _reconnectAttempt;
protected _options: RPC_ClientOptions;
private _connectionUrl;
private _connectPromise;
private _nextPingTimeout;
static OPEN: number;
static CONNECTING: number;
static CLOSING: number;
static CLOSED: number;
constructor({ ...options }: RPC_ClientOptions);
get identity(): string | undefined;
get protocol(): string | undefined;
get state(): number;
reconfigure(options: RPC_ClientOptions): void;
/**
* Attempt to connect to the RPCServer.
* @returns {Promise} Resolves when connected, rejects on failure
*/
connect(): Promise;
private _keepAlive;
private _tryReconnect;
private _beginConnect;
/**
* Start consuming from a WebSocket
* @param {WebSocket} ws - A WebSocket instance
* @param {EventBuffer} leadMsgBuffer - A buffer which traps all 'message' events
*/
protected _attachWebsocket(ws: WebSocket, leadMsgBuffer?: EventBuffer): void;
private _handleDisconnect;
private _rejectPendingCalls;
/**
* Call a method on a remote RPCClient or RPCServerClient.
* @param {string} method - The RPC method to call.
* @param {*} params - A value to be passed as params to the remote handler.
* @param {Object} options - Call options
* @param {number} options.callTimeoutMs - Call timeout (in milliseconds)
* @param {AbortSignal} options.signal - AbortSignal to cancel the call.
* @param {boolean} options.noReply - If set to true, the call will return immediately.
* @returns Promise<*> - Response value from the remote handler.
*/
call(method: any, params?: any, options?: Record): Promise;
private _call;
/**
* Closes the RPCClient.
* @param {Object} options - Close options
* @param {number} options.code - The websocket CloseEvent code.
* @param {string} options.reason - The websocket CloseEvent reason.
* @param {boolean} options.awaitPending - Wait for in-flight calls & responses to complete before closing.
* @param {boolean} options.force - Terminate websocket immediately without passing code, reason, or waiting.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code CloseEvent codes}
* @returns Promise