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