Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import RPCClient, { IHandlersOption } from "./lib/client";
import RPCServer from "./lib/server";
export { createRPCError } from "./lib/util";
export { createValidator } from "./lib/validator";
export { NOREPLY } from "./lib/symbols";
export { RPCError, RPCFormatViolationError, RPCFormationViolationError, RPCFrameworkError, RPCGenericError, RPCInternalError, RPCMessageTypeNotSupportedError, RPCNotImplementedError, RPCNotSupportedError, RPCOccurenceConstraintViolationError, RPCOccurrenceConstraintViolationError, RPCPropertyConstraintViolationError, RPCProtocolError, RPCSecurityError, RPCTypeConstraintViolationError, TimeoutError, UnexpectedHttpResponse, WebsocketUpgradeError, } from "./lib/errors";
export { RPCServer, RPCClient, IHandlersOption };
146 changes: 146 additions & 0 deletions lib/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/// <reference types="node" />
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<string, any>;
backoff: {
initialDelay: number;
maxDelay: number;
factor: number;
randomisationFactor: number;
};
}
export interface IHandlersOption {
messageId?: string;
method?: string;
params?: Record<string, any>;
signal?: AbortSignal;
reply?: unknown;
}
type IHandlers = ({ params, reply, method, signal, messageId, }: IHandlersOption) => Promise<Record<string, any>>;
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<undefined>} Resolves when connected, rejects on failure
*/
connect(): Promise<any>;
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<string, any>): Promise<unknown>;
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<Object> - The CloseEvent (code & reason) for closure. May be different from requested code & reason.
*/
close({ code, reason, awaitPending, force, }?: {
code?: number;
reason?: string;
awaitPending?: any;
force?: any;
}): Promise<{
code: number | undefined;
reason: string | undefined;
} | undefined>;
private _awaitUntilPendingSettled;
private _deferNextPing;
private _onMessage;
private _onCall;
private _onCallResult;
private _onCallError;
/**
* 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;
/**
*
* @param {string} [method] - The name of the handled method.
*/
removeHandler(method: string): void;
removeAllHandlers(): void;
/**
*
* @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: string | IHandlers, handler?: IHandlers): void;
}
export default RPC_Client;
92 changes: 92 additions & 0 deletions lib/errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
export declare class TimeoutError extends Error {
}
export declare class UnexpectedHttpResponse extends Error {
code: any;
request: any;
response: any;
}
export declare class RPCError extends Error {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCGenericError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCNotImplementedError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCNotSupportedError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCInternalError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCProtocolError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCSecurityError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCFormatViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCFormationViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCPropertyConstraintViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCOccurenceConstraintViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCOccurrenceConstraintViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCTypeConstraintViolationError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCMessageTypeNotSupportedError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class RPCFrameworkError extends RPCError {
rpcErrorMessage: string;
rpcErrorCode: string;
}
export declare class WebsocketUpgradeError extends Error {
code: any;
constructor(code: any, message: string | undefined);
}
declare const _default: {
WebsocketUpgradeError: typeof WebsocketUpgradeError;
TimeoutError: typeof TimeoutError;
UnexpectedHttpResponse: typeof UnexpectedHttpResponse;
RPCError: typeof RPCError;
RPCGenericError: typeof RPCGenericError;
RPCNotImplementedError: typeof RPCNotImplementedError;
RPCNotSupportedError: typeof RPCNotSupportedError;
RPCInternalError: typeof RPCInternalError;
RPCProtocolError: typeof RPCProtocolError;
RPCSecurityError: typeof RPCSecurityError;
RPCFormatViolationError: typeof RPCFormatViolationError;
RPCFormationViolationError: typeof RPCFormationViolationError;
RPCPropertyConstraintViolationError: typeof RPCPropertyConstraintViolationError;
RPCOccurrenceConstraintViolationError: typeof RPCOccurrenceConstraintViolationError;
RPCOccurenceConstraintViolationError: typeof RPCOccurenceConstraintViolationError;
RPCTypeConstraintViolationError: typeof RPCTypeConstraintViolationError;
RPCMessageTypeNotSupportedError: typeof RPCMessageTypeNotSupportedError;
RPCFrameworkError: typeof RPCFrameworkError;
};
export default _default;
12 changes: 12 additions & 0 deletions lib/event-buffer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="node" />
/// <reference types="node" />
import { EventEmitter } from "stream";
declare class EventBuffer {
private _emitter;
private _event;
private _collector;
private _buffer;
constructor(emitter: EventEmitter, event: string | symbol);
condense(): any;
}
export default EventBuffer;
10 changes: 10 additions & 0 deletions lib/queue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare class Queue {
private _pending;
private _concurrency;
private _queue;
constructor();
setConcurrency(concurrency: number): void;
push(fn: any): Promise<unknown>;
private _next;
}
export default Queue;
41 changes: 41 additions & 0 deletions lib/server.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { IncomingMessage, Server } from "http";
import { ServerOptions } from "ws";
import { EventEmitter } from "stream";
import { Validator } from "./validator";
import { IHandshakeInterface } from "./serverClient";
import { Socket } from "net";
interface IOccpServiceOptions {
wssOptions?: ServerOptions;
protocols?: string[];
callTimeoutMs?: number;
pingIntervalMs?: number;
deferPingsOnActivity?: boolean;
respondWithDetailedErrors?: boolean;
callConcurrency?: number;
maxBadMessages?: number;
strictMode?: boolean | string[];
strictModeValidators?: Validator[];
}
declare class RPCServer extends EventEmitter {
private _httpServerAbortControllers;
private _state;
private _clients;
private _pendingUpgrades;
private _options;
private _wss;
private _strictValidators;
authCallback: (accept: (session?: Record<string, any>, protocol?: string | false) => void, reject: (code: number, message: string) => void, handshake: IHandshakeInterface, signal: AbortSignal) => void;
constructor({ ...options }: IOccpServiceOptions, _callback?: () => void);
reconfigure(options: any): void;
private _onConnection;
get handleUpgrade(): (request: IncomingMessage, socket: Socket, head: Buffer) => Promise<void>;
auth(cb: (accept: (session?: Record<string, any>, protocol?: string | false) => void, reject: (code: number, message: string) => void, handshake: IHandshakeInterface, signal?: AbortSignal) => void): void;
listen(port: any, host?: any, options?: Record<string, any>): Promise<Server<typeof IncomingMessage, typeof import("http").ServerResponse>>;
close({ code, reason, awaitPending, force }: Record<string, any>): Promise<void>;
}
export default RPCServer;
28 changes: 28 additions & 0 deletions lib/serverClient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference types="node" />
/// <reference types="node" />
import WebSocket from "ws";
import RPC_Client, { RPC_ClientOptions } from "./client";
import { IncomingHttpHeaders, IncomingMessage } from "http";
export interface IHandshakeInterface {
remoteAddress: string | undefined;
headers: IncomingHttpHeaders;
protocols: Set<string>;
endpoint: string;
identity: string;
query: URLSearchParams;
request: IncomingMessage;
password: Buffer | undefined;
}
declare class RpcServerClient extends RPC_Client {
private _session;
private _handshake;
constructor({ ...options }: RPC_ClientOptions, { ws, handshake, session, }: {
ws: WebSocket;
session: Record<string, any>;
handshake: IHandshakeInterface;
});
get handshake(): IHandshakeInterface;
get session(): Record<string, any>;
connect(): Promise<void>;
}
export default RpcServerClient;
2 changes: 2 additions & 0 deletions lib/standard-validators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: readonly [import("./validator").Validator, import("./validator").Validator];
export default _default;
4 changes: 4 additions & 0 deletions lib/symbols.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const NOREPLY: {
NOREPLY: symbol;
};
export default NOREPLY;
9 changes: 9 additions & 0 deletions lib/util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export declare function getPackageIdent(): string;
export declare function getErrorPlainObject(err: Error): any;
export declare function createRPCError(type: string, message?: any, details?: {}): Record<string, any>;
declare const _default: {
getErrorPlainObject: typeof getErrorPlainObject;
createRPCError: typeof createRPCError;
getPackageIdent: typeof getPackageIdent;
};
export default _default;
9 changes: 9 additions & 0 deletions lib/validator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Ajv, { AnySchema, AsyncSchema, SchemaObject } from "ajv";
export declare class Validator {
_subprotocol: string;
_ajv: Ajv;
constructor(subprotocol: string, ajv: Ajv);
get subprotocol(): string;
validate(schemaId: string, params: any): boolean | Promise<unknown>;
}
export declare function createValidator(subprotocol: string, json: boolean | SchemaObject | AsyncSchema | AnySchema[]): Validator;
18 changes: 18 additions & 0 deletions lib/ws-util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference types="node" />
import { Socket } from "net";
export declare function abortHandshake(socket: Socket, code: number | string, message?: string, headers?: Record<string, any>): void;
export declare function parseSubprotocols(header: string): Set<string>;
/**
* Checks if a status code is allowed in a close frame.
*
* @param {Number} code The status code
* @return {Boolean} `true` if the status code is valid, else `false`
* @public
*/
export declare function isValidStatusCode(code: number): boolean;
declare const _default: {
abortHandshake: typeof abortHandshake;
parseSubprotocols: typeof parseSubprotocols;
isValidStatusCode: typeof isValidStatusCode;
};
export default _default;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.