Skip to content

Commit 0ea2c92

Browse files
committed
wip: jsdocs
1 parent 77a9a37 commit 0ea2c92

20 files changed

+306
-227
lines changed

index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import RPCClient from './lib/client';
2-
import RPCServer from './lib/server';
3-
import errors from './lib/errors';
4-
import symbols from './lib/symbols';
5-
import { createRPCError } from './lib/util';
6-
import { createValidator } from './lib/validator';
1+
import RPCClient from './lib/client.js';
2+
import RPCServer from './lib/server.js';
3+
import errors from './lib/errors.js';
4+
import symbols from './lib/symbols.js';
5+
import { createRPCError } from './lib/util.js';
6+
import { createValidator } from './lib/validator.js';
77

88
export default {
99
RPCServer,

jsconfig.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2022",
4-
"module": "NodeNext",
5-
"moduleResolution": "nodenext",
3+
"target": "es2022",
4+
"module": "node16",
5+
"moduleResolution": "node16",
66
"resolveJsonModule": true,
77
"checkJs": true,
8-
"strict": false
8+
"strict": true
99
},
1010
"exclude": [
1111
"node_modules/**"

lib/client.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ const MSG_CALL = 2;
1717
const MSG_CALLRESULT = 3;
1818
const MSG_CALLERROR = 4;
1919

20+
21+
/**
22+
* @typedef {object} RPCClientOptions
23+
* @prop {URL | string} endpoint
24+
*/
25+
2026
/**
2127
* @typedef {CONNECTING | OPEN | CLOSING | CLOSED} ConnectionState
2228
*/
@@ -521,10 +527,11 @@ class RPCClient extends EventEmitter {
521527
try {
522528
await new Promise((resolve, reject) => {
523529
this._ws.once('unexpected-response', (request, response) => {
524-
const err = new UnexpectedHttpResponse(response.statusMessage);
525-
err.code = response.statusCode;
526-
err.request = request;
527-
err.response = response;
530+
const err = new UnexpectedHttpResponse(response.statusMessage, {
531+
code: response.statusCode,
532+
request,
533+
response,
534+
});
528535
reject(err);
529536
});
530537
this._ws.once('upgrade', (response) => {

lib/errors.d.ts

-92
This file was deleted.

lib/errors.js

+93-16
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,148 @@
11

2-
export class TimeoutError extends Error {};
3-
export class UnexpectedHttpResponse extends Error {};
2+
export class TimeoutError extends Error {
3+
4+
};
5+
6+
export class UnexpectedHttpResponse extends Error {
7+
8+
/**
9+
* An error which occurs when a Websocket HTTP upgrade fails due to receiving an unexpected response from the server.
10+
*
11+
* @param {string | undefined} message
12+
* @param {object} obj
13+
* @param {number} [obj.code]
14+
* @param {import('node:http').ClientRequest} obj.request
15+
* @param {import('node:http').IncomingMessage} obj.response
16+
*/
17+
constructor(message, {code, request, response}) {
18+
super(message);
19+
20+
/** @type {number | undefined} */
21+
this.code = code;
22+
23+
/** @type {import('node:http').ClientRequest} */
24+
this.request = request;
25+
26+
/** @type {import('node:http').IncomingMessage} */
27+
this.response = response;
28+
}
29+
};
430

531
export class RPCError extends Error {
32+
/** @const */
633
rpcErrorMessage = '';
34+
/** @const */
735
rpcErrorCode = 'GenericError';
836
}
37+
938
export class RPCGenericError extends RPCError {
39+
/** @const */
1040
rpcErrorMessage = '';
41+
/** @const */
1142
rpcErrorCode = 'GenericError';
12-
};
43+
}
44+
1345
export class RPCNotImplementedError extends RPCError {
46+
/** @const */
1447
rpcErrorMessage = 'Requested method is not known';
48+
/** @const */
1549
rpcErrorCode = 'NotImplemented';
16-
};
50+
}
51+
1752
export class RPCNotSupportedError extends RPCError {
53+
/** @const */
1854
rpcErrorMessage = 'Requested method is recognised but not supported';
55+
/** @const */
1956
rpcErrorCode = 'NotSupported';
20-
};
57+
}
58+
2159
export class RPCInternalError extends RPCError {
60+
/** @const */
2261
rpcErrorMessage = 'An internal error occurred and the receiver was not able to process the requested method successfully';
62+
/** @const */
2363
rpcErrorCode = 'InternalError';
24-
};
64+
}
65+
2566
export class RPCProtocolError extends RPCError {
67+
/** @const */
2668
rpcErrorMessage = 'Payload for method is incomplete';
69+
/** @const */
2770
rpcErrorCode = 'ProtocolError';
28-
};
71+
}
72+
2973
export class RPCSecurityError extends RPCError {
74+
/** @const */
3075
rpcErrorMessage = 'During the processing of method a security issue occurred preventing receiver from completing the method successfully';
76+
/** @const */
3177
rpcErrorCode = 'SecurityError';
32-
};
78+
}
79+
3380
export class RPCFormatViolationError extends RPCError {
81+
/** @const */
3482
rpcErrorMessage = 'Payload for the method is syntactically incorrect or not conform the PDU structure for the method';
83+
/** @const */
3584
rpcErrorCode = 'FormatViolation';
36-
};
85+
}
86+
3787
export class RPCFormationViolationError extends RPCError {
88+
/** @const */
3889
rpcErrorMessage = 'Payload for the method is syntactically incorrect or not conform the PDU structure for the method';
90+
/** @const */
3991
rpcErrorCode = 'FormationViolation';
40-
};
92+
}
93+
4194
export class RPCPropertyConstraintViolationError extends RPCError {
95+
/** @const */
4296
rpcErrorMessage = 'Payload is syntactically correct but at least one field contains an invalid value';
97+
/** @const */
4398
rpcErrorCode = 'PropertyConstraintViolation';
44-
};
99+
}
100+
45101
export class RPCOccurenceConstraintViolationError extends RPCError {
102+
/** @const */
46103
rpcErrorMessage = 'Payload for the method is syntactically correct but at least one of the fields violates occurence constraints';
104+
/** @const */
47105
rpcErrorCode = 'OccurenceConstraintViolation';
48-
};
106+
}
107+
49108
export class RPCOccurrenceConstraintViolationError extends RPCError {
109+
/** @const */
50110
rpcErrorMessage = 'Payload for the method is syntactically correct but at least one of the fields violates occurence constraints';
111+
/** @const */
51112
rpcErrorCode = 'OccurrenceConstraintViolation';
52-
};
113+
}
114+
53115
export class RPCTypeConstraintViolationError extends RPCError {
116+
/** @const */
54117
rpcErrorMessage = 'Payload for the method is syntactically correct but at least one of the fields violates data type constraints';
118+
/** @const */
55119
rpcErrorCode = 'TypeConstraintViolation';
56-
};
120+
}
121+
57122
export class RPCMessageTypeNotSupportedError extends RPCError {
123+
/** @const */
58124
rpcErrorMessage = 'A message with a Message Type Number received is not supported by this implementation.';
125+
/** @const */
59126
rpcErrorCode = 'MessageTypeNotSupported';
60-
};
127+
}
128+
61129
export class RPCFrameworkError extends RPCError {
130+
/** @const */
62131
rpcErrorMessage = 'Content of the call is not a valid RPC Request, for example: MessageId could not be read.';
132+
/** @const */
63133
rpcErrorCode = 'RpcFrameworkError';
64-
};
134+
}
65135

66136
export class WebsocketUpgradeError extends Error {
137+
/**
138+
*
139+
* @param {number} code
140+
* @param {string} [message]
141+
*/
67142
constructor(code, message) {
68143
super(message);
144+
145+
/** @type {number} */
69146
this.code = code;
70147
}
71148
}

lib/event-buffer.d.ts

-12
This file was deleted.

lib/event-buffer.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11

2-
class EventBuffer {
2+
/**
3+
* A util class to buffer events fired by an emitter.
4+
*/
5+
export class EventBuffer {
6+
7+
/**
8+
* @param {import("node:events").EventEmitter} emitter
9+
* @param {string | symbol} event
10+
*/
311
constructor(emitter, event) {
12+
/** @type {import("node:events").EventEmitter} */
413
this._emitter = emitter;
14+
15+
/** @type {string | symbol} */
516
this._event = event;
617

18+
/**
19+
* @param {...*} args Event data
20+
*/
721
this._collecter = (...args) => {
822
this._buffer.push(args);
923
};
24+
25+
/** @type {*[]} */
1026
this._buffer = [];
1127
this._emitter.on(event, this._collecter);
1228
}
1329

30+
/**
31+
* Returns the collected event data.
32+
*
33+
* @returns {*[]}
34+
*/
1435
condense() {
1536
this._emitter.off(this._event, this._collecter);
1637
return this._buffer;
1738
}
1839
}
19-
20-
export default EventBuffer;

lib/queue.d.ts

-10
This file was deleted.

0 commit comments

Comments
 (0)