Skip to content

Commit 8cffe3a

Browse files
authored
Merge pull request #71 from rohittiwari-dev/master
[feat] adding typescript to the package
2 parents c499f8a + f686cd6 commit 8cffe3a

13 files changed

+379
-1
lines changed

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import RPCClient, { IHandlersOption } from "./lib/client";
2+
import RPCServer from "./lib/server";
3+
export { createRPCError } from "./lib/util";
4+
export { createValidator } from "./lib/validator";
5+
export { NOREPLY } from "./lib/symbols";
6+
export { RPCError, RPCFormatViolationError, RPCFormationViolationError, RPCFrameworkError, RPCGenericError, RPCInternalError, RPCMessageTypeNotSupportedError, RPCNotImplementedError, RPCNotSupportedError, RPCOccurenceConstraintViolationError, RPCOccurrenceConstraintViolationError, RPCPropertyConstraintViolationError, RPCProtocolError, RPCSecurityError, RPCTypeConstraintViolationError, TimeoutError, UnexpectedHttpResponse, WebsocketUpgradeError, } from "./lib/errors";
7+
export { RPCServer, RPCClient, IHandlersOption };

lib/client.d.ts

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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;

lib/errors.d.ts

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
export declare class TimeoutError extends Error {
2+
}
3+
export declare class UnexpectedHttpResponse extends Error {
4+
code: any;
5+
request: any;
6+
response: any;
7+
}
8+
export declare class RPCError extends Error {
9+
rpcErrorMessage: string;
10+
rpcErrorCode: string;
11+
}
12+
export declare class RPCGenericError extends RPCError {
13+
rpcErrorMessage: string;
14+
rpcErrorCode: string;
15+
}
16+
export declare class RPCNotImplementedError extends RPCError {
17+
rpcErrorMessage: string;
18+
rpcErrorCode: string;
19+
}
20+
export declare class RPCNotSupportedError extends RPCError {
21+
rpcErrorMessage: string;
22+
rpcErrorCode: string;
23+
}
24+
export declare class RPCInternalError extends RPCError {
25+
rpcErrorMessage: string;
26+
rpcErrorCode: string;
27+
}
28+
export declare class RPCProtocolError extends RPCError {
29+
rpcErrorMessage: string;
30+
rpcErrorCode: string;
31+
}
32+
export declare class RPCSecurityError extends RPCError {
33+
rpcErrorMessage: string;
34+
rpcErrorCode: string;
35+
}
36+
export declare class RPCFormatViolationError extends RPCError {
37+
rpcErrorMessage: string;
38+
rpcErrorCode: string;
39+
}
40+
export declare class RPCFormationViolationError extends RPCError {
41+
rpcErrorMessage: string;
42+
rpcErrorCode: string;
43+
}
44+
export declare class RPCPropertyConstraintViolationError extends RPCError {
45+
rpcErrorMessage: string;
46+
rpcErrorCode: string;
47+
}
48+
export declare class RPCOccurenceConstraintViolationError extends RPCError {
49+
rpcErrorMessage: string;
50+
rpcErrorCode: string;
51+
}
52+
export declare class RPCOccurrenceConstraintViolationError extends RPCError {
53+
rpcErrorMessage: string;
54+
rpcErrorCode: string;
55+
}
56+
export declare class RPCTypeConstraintViolationError extends RPCError {
57+
rpcErrorMessage: string;
58+
rpcErrorCode: string;
59+
}
60+
export declare class RPCMessageTypeNotSupportedError extends RPCError {
61+
rpcErrorMessage: string;
62+
rpcErrorCode: string;
63+
}
64+
export declare class RPCFrameworkError extends RPCError {
65+
rpcErrorMessage: string;
66+
rpcErrorCode: string;
67+
}
68+
export declare class WebsocketUpgradeError extends Error {
69+
code: any;
70+
constructor(code: any, message: string | undefined);
71+
}
72+
declare const _default: {
73+
WebsocketUpgradeError: typeof WebsocketUpgradeError;
74+
TimeoutError: typeof TimeoutError;
75+
UnexpectedHttpResponse: typeof UnexpectedHttpResponse;
76+
RPCError: typeof RPCError;
77+
RPCGenericError: typeof RPCGenericError;
78+
RPCNotImplementedError: typeof RPCNotImplementedError;
79+
RPCNotSupportedError: typeof RPCNotSupportedError;
80+
RPCInternalError: typeof RPCInternalError;
81+
RPCProtocolError: typeof RPCProtocolError;
82+
RPCSecurityError: typeof RPCSecurityError;
83+
RPCFormatViolationError: typeof RPCFormatViolationError;
84+
RPCFormationViolationError: typeof RPCFormationViolationError;
85+
RPCPropertyConstraintViolationError: typeof RPCPropertyConstraintViolationError;
86+
RPCOccurrenceConstraintViolationError: typeof RPCOccurrenceConstraintViolationError;
87+
RPCOccurenceConstraintViolationError: typeof RPCOccurenceConstraintViolationError;
88+
RPCTypeConstraintViolationError: typeof RPCTypeConstraintViolationError;
89+
RPCMessageTypeNotSupportedError: typeof RPCMessageTypeNotSupportedError;
90+
RPCFrameworkError: typeof RPCFrameworkError;
91+
};
92+
export default _default;

lib/event-buffer.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="node" />
2+
/// <reference types="node" />
3+
import { EventEmitter } from "stream";
4+
declare class EventBuffer {
5+
private _emitter;
6+
private _event;
7+
private _collector;
8+
private _buffer;
9+
constructor(emitter: EventEmitter, event: string | symbol);
10+
condense(): any;
11+
}
12+
export default EventBuffer;

lib/queue.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare class Queue {
2+
private _pending;
3+
private _concurrency;
4+
private _queue;
5+
constructor();
6+
setConcurrency(concurrency: number): void;
7+
push(fn: any): Promise<unknown>;
8+
private _next;
9+
}
10+
export default Queue;

lib/server.d.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// <reference types="node" />
2+
/// <reference types="node" />
3+
/// <reference types="node" />
4+
/// <reference types="node" />
5+
/// <reference types="node" />
6+
import { IncomingMessage, Server } from "http";
7+
import { ServerOptions } from "ws";
8+
import { EventEmitter } from "stream";
9+
import { Validator } from "./validator";
10+
import { IHandshakeInterface } from "./serverClient";
11+
import { Socket } from "net";
12+
interface IOccpServiceOptions {
13+
wssOptions?: ServerOptions;
14+
protocols?: string[];
15+
callTimeoutMs?: number;
16+
pingIntervalMs?: number;
17+
deferPingsOnActivity?: boolean;
18+
respondWithDetailedErrors?: boolean;
19+
callConcurrency?: number;
20+
maxBadMessages?: number;
21+
strictMode?: boolean | string[];
22+
strictModeValidators?: Validator[];
23+
}
24+
declare class RPCServer extends EventEmitter {
25+
private _httpServerAbortControllers;
26+
private _state;
27+
private _clients;
28+
private _pendingUpgrades;
29+
private _options;
30+
private _wss;
31+
private _strictValidators;
32+
authCallback: (accept: (session?: Record<string, any>, protocol?: string | false) => void, reject: (code: number, message: string) => void, handshake: IHandshakeInterface, signal: AbortSignal) => void;
33+
constructor({ ...options }: IOccpServiceOptions, _callback?: () => void);
34+
reconfigure(options: any): void;
35+
private _onConnection;
36+
get handleUpgrade(): (request: IncomingMessage, socket: Socket, head: Buffer) => Promise<void>;
37+
auth(cb: (accept: (session?: Record<string, any>, protocol?: string | false) => void, reject: (code: number, message: string) => void, handshake: IHandshakeInterface, signal?: AbortSignal) => void): void;
38+
listen(port: any, host?: any, options?: Record<string, any>): Promise<Server<typeof IncomingMessage, typeof import("http").ServerResponse>>;
39+
close({ code, reason, awaitPending, force }: Record<string, any>): Promise<void>;
40+
}
41+
export default RPCServer;

lib/serverClient.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference types="node" />
2+
/// <reference types="node" />
3+
import WebSocket from "ws";
4+
import RPC_Client, { RPC_ClientOptions } from "./client";
5+
import { IncomingHttpHeaders, IncomingMessage } from "http";
6+
export interface IHandshakeInterface {
7+
remoteAddress: string | undefined;
8+
headers: IncomingHttpHeaders;
9+
protocols: Set<string>;
10+
endpoint: string;
11+
identity: string;
12+
query: URLSearchParams;
13+
request: IncomingMessage;
14+
password: Buffer | undefined;
15+
}
16+
declare class RpcServerClient extends RPC_Client {
17+
private _session;
18+
private _handshake;
19+
constructor({ ...options }: RPC_ClientOptions, { ws, handshake, session, }: {
20+
ws: WebSocket;
21+
session: Record<string, any>;
22+
handshake: IHandshakeInterface;
23+
});
24+
get handshake(): IHandshakeInterface;
25+
get session(): Record<string, any>;
26+
connect(): Promise<void>;
27+
}
28+
export default RpcServerClient;

lib/standard-validators.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const _default: readonly [import("./validator").Validator, import("./validator").Validator];
2+
export default _default;

lib/symbols.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare const NOREPLY: {
2+
NOREPLY: symbol;
3+
};
4+
export default NOREPLY;

lib/util.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export declare function getPackageIdent(): string;
2+
export declare function getErrorPlainObject(err: Error): any;
3+
export declare function createRPCError(type: string, message?: any, details?: {}): Record<string, any>;
4+
declare const _default: {
5+
getErrorPlainObject: typeof getErrorPlainObject;
6+
createRPCError: typeof createRPCError;
7+
getPackageIdent: typeof getPackageIdent;
8+
};
9+
export default _default;

lib/validator.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Ajv, { AnySchema, AsyncSchema, SchemaObject } from "ajv";
2+
export declare class Validator {
3+
_subprotocol: string;
4+
_ajv: Ajv;
5+
constructor(subprotocol: string, ajv: Ajv);
6+
get subprotocol(): string;
7+
validate(schemaId: string, params: any): boolean | Promise<unknown>;
8+
}
9+
export declare function createValidator(subprotocol: string, json: boolean | SchemaObject | AsyncSchema | AnySchema[]): Validator;

lib/ws-util.d.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference types="node" />
2+
import { Socket } from "net";
3+
export declare function abortHandshake(socket: Socket, code: number | string, message?: string, headers?: Record<string, any>): void;
4+
export declare function parseSubprotocols(header: string): Set<string>;
5+
/**
6+
* Checks if a status code is allowed in a close frame.
7+
*
8+
* @param {Number} code The status code
9+
* @return {Boolean} `true` if the status code is valid, else `false`
10+
* @public
11+
*/
12+
export declare function isValidStatusCode(code: number): boolean;
13+
declare const _default: {
14+
abortHandshake: typeof abortHandshake;
15+
parseSubprotocols: typeof parseSubprotocols;
16+
isValidStatusCode: typeof isValidStatusCode;
17+
};
18+
export default _default;

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)