@openevstack/ocpp-rpc
Version:
⚡ A lightweight, production-ready RPC server built with Express and WebSocket for handling OCPP-based EV charger communication. Part of the OpenEVStack ecosystem.
118 lines (117 loc) • 4.99 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { EventEmitter } from "events";
import { ClientOptions, IClose } from "./interfaces/Options.interface";
import WebSocket from "ws";
import EventBuffer from "./utils/event-buffer";
import { ICallOptions, IDisconnect, IRpcClient } from "./interfaces/Client.interface";
import { IncomingMessage } from "http";
declare class RpcClient extends EventEmitter implements IRpcClient {
_options: ClientOptions;
_identity: string | undefined;
_connectionUrl: string | undefined;
_wildcardHandler: Function | undefined;
_handlers: Map<string, Function>;
_state: number;
_callQueue: any;
_ws: WebSocket | undefined;
_wsAbortController: any;
_keepAliveAbortController: any;
_pendingPingResponse: boolean;
_lastPingTime: number;
_closePromise: any;
protocol?: string;
_protocolOptions: string[];
_protocol: any;
_strictProtocols: string[];
_strictValidators: any;
_pendingCalls: Map<string, any>;
_pendingResponses: Map<string, any>;
_outboundMsgBuffer: string[];
_connectedOnce: boolean;
_backoffStrategy: any;
_badMessagesCount: number;
_reconnectAttempt: number;
_connectPromise: Promise<{
response: IncomingMessage | undefined;
} | undefined> | undefined;
_nextPingTimeout: any;
static OPEN: 1;
static CONNECTING: 0;
static CLOSING: 2;
static CLOSED: 3;
constructor(_: ClientOptions);
get identity(): string | undefined;
set identity(id: string | undefined);
get state(): number;
reconfigure(options: ClientOptions): void;
/**
* Attempt to connect to the RPCServer
* @returns {Promise<void>}
*/
connect(): Promise<{
response: IncomingMessage | undefined;
} | undefined>;
/**
* Send a message to the RPCServer. While socket is connecting, the message is queued and send when open.
* @param {Buffer|String} message - String to send via websocket
*/
sendRaw(message: string): void;
/**
* 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<Object> - The CloseEvent (code & reason) for closure. May be different from requested code & reason.
*/
close({ code, reason, awaitPending, force }: IClose): Promise<any>;
/**
*
* @param {string} [method] - The name of the RPC method to handle.
* @param {Function} handler - A function that can handle incoming calls for this method.
*/
handle(method: any, handler: any): void;
/**
*
* @param {string} [method] - The name of the handled method.
*/
removeHandler(method: string): void;
removeAllHandlers(): void;
/**
* 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: string, params: string, options: ICallOptions): Promise<void>;
_call(method: string, params: string, options: ICallOptions): Promise<any>;
/**
* Start consuming from a WebSocket
* @param {WebSocket} ws - A WebSocket instance
* @param {EventBuffer} leadMsgBuffer - A buffer which traps all 'message' events
*/
_attachWebsocket(ws: WebSocket, leadMsgBuffer: EventBuffer): void;
_rejectPendingCalls(abortReason: string): void;
_awaitUntilPendingSettled(): Promise<PromiseSettledResult<any>[]>;
_handleDisconnect({ code, reason }: IDisconnect): void;
_beginConnect(): Promise<{
response: IncomingMessage | undefined;
} | undefined>;
_deferNextPing(): void;
_keepAlive(): Promise<void>;
_tryReconnect(): Promise<void>;
_onMessage(buffer: string | ArrayBuffer | Buffer[]): void;
_onCall(msgId: string, method: string, params: any): Promise<void>;
_onCallResult(msgId: string, result: any): any;
_onCallError(msgId: string, errorCode: number, errorDescription: string, errorDetails: Record<string, any> | undefined): void;
}
export default RpcClient;