|
| 1 | +/// <reference types="node" /> |
| 2 | +import EventEmitter from "events"; |
| 3 | +import { Validator } from "./validator"; |
| 4 | +import WebSocket from "ws"; |
| 5 | +import EventBuffer from "./event-buffer"; |
| 6 | +export interface RPC_ClientOptions { |
| 7 | + identity: string; |
| 8 | + reconnect: boolean; |
| 9 | + callTimeoutMs: number; |
| 10 | + pingIntervalMs: number; |
| 11 | + deferPingsOnActivity: boolean; |
| 12 | + respondWithDetailedErrors: boolean; |
| 13 | + callConcurrency: number; |
| 14 | + strictMode: boolean | string[]; |
| 15 | + strictModeValidators: Validator[]; |
| 16 | + maxBadMessages: number; |
| 17 | + protocols: string[]; |
| 18 | + endpoint?: string; |
| 19 | + password: string | null; |
| 20 | + wsOpts: any; |
| 21 | + headers: any; |
| 22 | + maxReconnects: number; |
| 23 | + query?: string | Record<string, any>; |
| 24 | + backoff: { |
| 25 | + initialDelay: number; |
| 26 | + maxDelay: number; |
| 27 | + factor: number; |
| 28 | + randomisationFactor: number; |
| 29 | + }; |
| 30 | +} |
| 31 | +export interface IHandlersOption { |
| 32 | + messageId?: string; |
| 33 | + method?: string; |
| 34 | + params?: Record<string, any>; |
| 35 | + signal?: AbortSignal; |
| 36 | + reply?: unknown; |
| 37 | +} |
| 38 | +type IHandlers = ({ params, reply, method, signal, messageId, }: IHandlersOption) => Promise<Record<string, any>>; |
| 39 | +declare class RPC_Client extends EventEmitter { |
| 40 | + protected _identity?: string; |
| 41 | + private _wildcardHandler; |
| 42 | + private _handlers; |
| 43 | + protected _state: number; |
| 44 | + private _callQueue; |
| 45 | + protected _ws?: WebSocket; |
| 46 | + private _wsAbortController?; |
| 47 | + private _keepAliveAbortController?; |
| 48 | + private _pendingPingResponse; |
| 49 | + private _lastPingTime; |
| 50 | + private _closePromise?; |
| 51 | + private _protocolOptions; |
| 52 | + protected _protocol?: string; |
| 53 | + private _strictProtocols; |
| 54 | + private _strictValidators?; |
| 55 | + private _pendingCalls; |
| 56 | + private _pendingResponses; |
| 57 | + private _outboundMsgBuffer; |
| 58 | + private _connectedOnce; |
| 59 | + private _backoffStrategy?; |
| 60 | + private _badMessagesCount; |
| 61 | + private _reconnectAttempt; |
| 62 | + protected _options: RPC_ClientOptions; |
| 63 | + private _connectionUrl; |
| 64 | + private _connectPromise; |
| 65 | + private _nextPingTimeout; |
| 66 | + static OPEN: number; |
| 67 | + static CONNECTING: number; |
| 68 | + static CLOSING: number; |
| 69 | + static CLOSED: number; |
| 70 | + constructor({ ...options }: RPC_ClientOptions); |
| 71 | + get identity(): string | undefined; |
| 72 | + get protocol(): string | undefined; |
| 73 | + get state(): number; |
| 74 | + reconfigure(options: RPC_ClientOptions): void; |
| 75 | + /** |
| 76 | + * Attempt to connect to the RPCServer. |
| 77 | + * @returns {Promise<undefined>} Resolves when connected, rejects on failure |
| 78 | + */ |
| 79 | + connect(): Promise<any>; |
| 80 | + private _keepAlive; |
| 81 | + private _tryReconnect; |
| 82 | + private _beginConnect; |
| 83 | + /** |
| 84 | + * Start consuming from a WebSocket |
| 85 | + * @param {WebSocket} ws - A WebSocket instance |
| 86 | + * @param {EventBuffer} leadMsgBuffer - A buffer which traps all 'message' events |
| 87 | + */ |
| 88 | + protected _attachWebsocket(ws: WebSocket, leadMsgBuffer?: EventBuffer): void; |
| 89 | + private _handleDisconnect; |
| 90 | + private _rejectPendingCalls; |
| 91 | + /** |
| 92 | + * Call a method on a remote RPCClient or RPCServerClient. |
| 93 | + * @param {string} method - The RPC method to call. |
| 94 | + * @param {*} params - A value to be passed as params to the remote handler. |
| 95 | + * @param {Object} options - Call options |
| 96 | + * @param {number} options.callTimeoutMs - Call timeout (in milliseconds) |
| 97 | + * @param {AbortSignal} options.signal - AbortSignal to cancel the call. |
| 98 | + * @param {boolean} options.noReply - If set to true, the call will return immediately. |
| 99 | + * @returns Promise<*> - Response value from the remote handler. |
| 100 | + */ |
| 101 | + call(method: any, params?: any, options?: Record<string, any>): Promise<unknown>; |
| 102 | + private _call; |
| 103 | + /** |
| 104 | + * Closes the RPCClient. |
| 105 | + * @param {Object} options - Close options |
| 106 | + * @param {number} options.code - The websocket CloseEvent code. |
| 107 | + * @param {string} options.reason - The websocket CloseEvent reason. |
| 108 | + * @param {boolean} options.awaitPending - Wait for in-flight calls & responses to complete before closing. |
| 109 | + * @param {boolean} options.force - Terminate websocket immediately without passing code, reason, or waiting. |
| 110 | + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code CloseEvent codes} |
| 111 | + * @returns Promise<Object> - The CloseEvent (code & reason) for closure. May be different from requested code & reason. |
| 112 | + */ |
| 113 | + close({ code, reason, awaitPending, force, }?: { |
| 114 | + code?: number; |
| 115 | + reason?: string; |
| 116 | + awaitPending?: any; |
| 117 | + force?: any; |
| 118 | + }): Promise<{ |
| 119 | + code: number | undefined; |
| 120 | + reason: string | undefined; |
| 121 | + } | undefined>; |
| 122 | + private _awaitUntilPendingSettled; |
| 123 | + private _deferNextPing; |
| 124 | + private _onMessage; |
| 125 | + private _onCall; |
| 126 | + private _onCallResult; |
| 127 | + private _onCallError; |
| 128 | + /** |
| 129 | + * Send a message to the RPCServer. While socket is connecting, the message is queued and send when open. |
| 130 | + * @param {Buffer|String} message - String to send via websocket |
| 131 | + */ |
| 132 | + sendRaw(message: string): void; |
| 133 | + /** |
| 134 | + * |
| 135 | + * @param {string} [method] - The name of the handled method. |
| 136 | + */ |
| 137 | + removeHandler(method: string): void; |
| 138 | + removeAllHandlers(): void; |
| 139 | + /** |
| 140 | + * |
| 141 | + * @param {string} [method] - The name of the RPC method to handle. |
| 142 | + * @param {Function} handler - A function that can handle incoming calls for this method. |
| 143 | + */ |
| 144 | + handle(method: string | IHandlers, handler?: IHandlers): void; |
| 145 | +} |
| 146 | +export default RPC_Client; |
0 commit comments