libshv-js 3.12.0 → 3.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ws-client.ts +23 -6
package/package.json
CHANGED
package/src/ws-client.ts
CHANGED
|
@@ -106,7 +106,25 @@ type DirResult = Array<IMap<{
|
|
|
106
106
|
|
|
107
107
|
class RpcError extends Error {
|
|
108
108
|
constructor(readonly err_info: ErrorMap) {
|
|
109
|
-
|
|
109
|
+
const errorCodeToMessage = (code: ErrorMap[typeof ERROR_CODE]) => {
|
|
110
|
+
switch (code) {
|
|
111
|
+
case ErrorCode.InvalidRequest: return 'InvalidRequest';
|
|
112
|
+
case ErrorCode.MethodNotFound: return 'MethodNotFound';
|
|
113
|
+
case ErrorCode.InvalidParams: return 'InvalidParams';
|
|
114
|
+
case ErrorCode.InternalError: return 'InternalError';
|
|
115
|
+
case ErrorCode.ParseError: return 'ParseError';
|
|
116
|
+
case ErrorCode.MethodCallTimeout: return 'MethodCallTimeout';
|
|
117
|
+
case ErrorCode.MethodCallCancelled: return 'MethodCallCancelled';
|
|
118
|
+
case ErrorCode.MethodCallException: return 'MethodCallException';
|
|
119
|
+
case ErrorCode.Unknown: return 'Unknown';
|
|
120
|
+
case ErrorCode.LoginRequired: return 'LoginRequired';
|
|
121
|
+
case ErrorCode.UserIDRequired: return 'UserIDRequired';
|
|
122
|
+
case ErrorCode.NotImplemented: return 'NotImplemented';
|
|
123
|
+
default: return 'Unknown RpcError';
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
super(err_info[ERROR_MESSAGE] ?? `${errorCodeToMessage(err_info[ERROR_CODE])} (${err_info[ERROR_CODE]})`);
|
|
110
128
|
}
|
|
111
129
|
|
|
112
130
|
data() {
|
|
@@ -169,7 +187,7 @@ class WsClient {
|
|
|
169
187
|
delayCallback?: (progress: number) => void;
|
|
170
188
|
timeout_handle: ReturnType<typeof globalThis.setTimeout>;
|
|
171
189
|
}> = [];
|
|
172
|
-
private requestHandlers
|
|
190
|
+
private requestHandlers = new Map<number, RequestHandler>();
|
|
173
191
|
private abortedRequests = new Set<number>();
|
|
174
192
|
private readonly subscriptions: Subscription[] = [];
|
|
175
193
|
private readonly websocket: WebSocket;
|
|
@@ -218,7 +236,7 @@ class WsClient {
|
|
|
218
236
|
};
|
|
219
237
|
|
|
220
238
|
if (RPC_MESSAGE_ABORT in request.value) {
|
|
221
|
-
const requestHandler = this.requestHandlers
|
|
239
|
+
const requestHandler = this.requestHandlers.get(requestId);
|
|
222
240
|
|
|
223
241
|
if (request.value[RPC_MESSAGE_ABORT] === true && requestHandler?.options.onAbort !== undefined) {
|
|
224
242
|
this.abortedRequests.add(requestId);
|
|
@@ -236,7 +254,7 @@ class WsClient {
|
|
|
236
254
|
try {
|
|
237
255
|
let result = this.options.onRequest(request.meta[RPC_MESSAGE_SHV_PATH], request.meta[RPC_MESSAGE_METHOD], request.value[RPC_MESSAGE_PARAMS], sendDelay);
|
|
238
256
|
if (result instanceof RequestHandler) {
|
|
239
|
-
this.requestHandlers
|
|
257
|
+
this.requestHandlers.set(requestId, result);
|
|
240
258
|
result = result.options.result;
|
|
241
259
|
}
|
|
242
260
|
|
|
@@ -251,8 +269,7 @@ class WsClient {
|
|
|
251
269
|
}
|
|
252
270
|
|
|
253
271
|
if (requestId in this.requestHandlers) {
|
|
254
|
-
|
|
255
|
-
delete this.requestHandlers[requestId];
|
|
272
|
+
this.requestHandlers.delete(requestId);
|
|
256
273
|
}
|
|
257
274
|
} catch (error: unknown) {
|
|
258
275
|
const sendError = (error: RpcError) => {
|