libshv-js 3.7.0 → 3.9.0
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/chainpack.ts +15 -1
- package/src/rpcmessage.ts +5 -0
- package/src/ws-client.ts +74 -18
package/package.json
CHANGED
package/src/chainpack.ts
CHANGED
|
@@ -416,7 +416,8 @@ class ChainPackWriter {
|
|
|
416
416
|
this.writeDateTime(rpcVal);
|
|
417
417
|
break;
|
|
418
418
|
case rpcVal instanceof Double:
|
|
419
|
-
|
|
419
|
+
this.writeDouble(rpcVal);
|
|
420
|
+
break;
|
|
420
421
|
case typeof rpcVal === 'object':
|
|
421
422
|
switch (rpcVal[shvMapType]) {
|
|
422
423
|
case 'imap':
|
|
@@ -575,6 +576,19 @@ class ChainPackWriter {
|
|
|
575
576
|
this.writeUIntDataHelper(bi, 'UInt');
|
|
576
577
|
}
|
|
577
578
|
|
|
579
|
+
writeDouble(double: Double) {
|
|
580
|
+
this.ctx.putByte(PackingSchema.Double);
|
|
581
|
+
const buffer = new ArrayBuffer(8);
|
|
582
|
+
const view = new DataView(buffer);
|
|
583
|
+
|
|
584
|
+
view.setFloat64(0, double.value, true);
|
|
585
|
+
|
|
586
|
+
const data = new Uint8Array(buffer);
|
|
587
|
+
for (let i = 0; i < 8; i++) {
|
|
588
|
+
this.ctx.putByte(data[i]);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
578
592
|
writeList(lst: RpcValue[]) {
|
|
579
593
|
this.ctx.putByte(PackingSchema.List);
|
|
580
594
|
for (const element of lst) {
|
package/src/rpcmessage.ts
CHANGED
|
@@ -8,6 +8,7 @@ export const RPC_MESSAGE_CALLER_IDS = 11;
|
|
|
8
8
|
export const RPC_MESSAGE_PARAMS = 1;
|
|
9
9
|
export const RPC_MESSAGE_RESULT = 2;
|
|
10
10
|
export const RPC_MESSAGE_ERROR = 3;
|
|
11
|
+
export const RPC_MESSAGE_DELAY = 4;
|
|
11
12
|
|
|
12
13
|
export const ERROR_CODE = 1;
|
|
13
14
|
export const ERROR_MESSAGE = 2;
|
|
@@ -51,10 +52,13 @@ const RpcResponseMetaZod = z.metamap({
|
|
|
51
52
|
[RPC_MESSAGE_CALLER_IDS]: z.int().or(z.array(z.int())).optional(),
|
|
52
53
|
[RPC_MESSAGE_REQUEST_ID]: z.number(),
|
|
53
54
|
});
|
|
55
|
+
|
|
54
56
|
const RpcResponseValueZod = z.imap({
|
|
55
57
|
[RPC_MESSAGE_RESULT]: z.rpcvalue(),
|
|
56
58
|
}).or(z.imap({
|
|
57
59
|
[RPC_MESSAGE_ERROR]: ErrorMapZod,
|
|
60
|
+
})).or(z.imap({
|
|
61
|
+
[RPC_MESSAGE_DELAY]: z.double(),
|
|
58
62
|
}));
|
|
59
63
|
|
|
60
64
|
const RpcSignalMetaZod = z.metamap({
|
|
@@ -71,6 +75,7 @@ const RpcSignalZod = z.withMeta(RpcSignalMetaZod, RpcSignalValueZod);
|
|
|
71
75
|
const RpcMessageZod = z.union([RpcRequestZod, RpcResponseZod, RpcSignalZod]);
|
|
72
76
|
export type RpcRequest = z.infer<typeof RpcRequestZod>;
|
|
73
77
|
export type RpcResponse = z.infer<typeof RpcResponseZod>;
|
|
78
|
+
export type RpcResponseValue = z.infer<typeof RpcResponseValueZod>;
|
|
74
79
|
export type RpcSignal = z.infer<typeof RpcSignalZod>;
|
|
75
80
|
export type RpcMessage = z.infer<typeof RpcMessageZod>;
|
|
76
81
|
|
package/src/ws-client.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {ChainPackReader, CHAINPACK_PROTOCOL_TYPE, ChainPackWriter, toChainPack} from './chainpack';
|
|
2
2
|
import {type CponReader, CPON_PROTOCOL_TYPE, toCpon} from './cpon';
|
|
3
|
-
import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessageZod, type RpcMessage, isSignal, isRequest, type RpcRequest, isResponse, ERROR_DATA, type ErrorMap, RPC_MESSAGE_METHOD, RPC_MESSAGE_SHV_PATH, RPC_MESSAGE_REQUEST_ID, RPC_MESSAGE_PARAMS, RPC_MESSAGE_ERROR, RPC_MESSAGE_RESULT, RPC_MESSAGE_CALLER_IDS} from './rpcmessage';
|
|
4
|
-
import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap, RpcValueWithMetaData, makeMetaMap} from './rpcvalue';
|
|
3
|
+
import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessageZod, type RpcMessage, isSignal, isRequest, type RpcRequest, isResponse, ERROR_DATA, type ErrorMap, RPC_MESSAGE_METHOD, RPC_MESSAGE_SHV_PATH, RPC_MESSAGE_REQUEST_ID, RPC_MESSAGE_PARAMS, RPC_MESSAGE_ERROR, RPC_MESSAGE_RESULT, RPC_MESSAGE_CALLER_IDS, RpcResponseValue, RPC_MESSAGE_DELAY} from './rpcmessage';
|
|
4
|
+
import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap, RpcValueWithMetaData, makeMetaMap, Double} from './rpcvalue';
|
|
5
5
|
|
|
6
6
|
const DEFAULT_TIMEOUT = 5000;
|
|
7
7
|
const DEFAULT_PING_INTERVAL = 30 * 1000;
|
|
@@ -58,7 +58,7 @@ type WsClientOptionsLogin = WsClientOptionsCommon & {
|
|
|
58
58
|
onConnected: () => void;
|
|
59
59
|
onConnectionFailure: (error: Error) => void;
|
|
60
60
|
onDisconnected: () => void;
|
|
61
|
-
onRequest: (
|
|
61
|
+
onRequest: (shvPath: string, method: string, param: RpcValue, delay: (progress: number) => void) => Promise<RpcValue>;
|
|
62
62
|
};
|
|
63
63
|
|
|
64
64
|
type WsClientOptionsWorkflows = WsClientOptionsCommon & {
|
|
@@ -95,7 +95,7 @@ type DirResult = Array<IMap<{
|
|
|
95
95
|
}>>;
|
|
96
96
|
|
|
97
97
|
class RpcError extends Error {
|
|
98
|
-
constructor(
|
|
98
|
+
constructor(readonly err_info: ErrorMap) {
|
|
99
99
|
super(err_info[ERROR_MESSAGE] ?? 'Unknown RpcError');
|
|
100
100
|
}
|
|
101
101
|
|
|
@@ -104,18 +104,38 @@ class RpcError extends Error {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
107
|
+
const makeErrInfo = (errInfoOrMsg: ErrorMap | string, code: ErrorCode) => {
|
|
108
|
+
if (typeof errInfoOrMsg === 'object') {
|
|
109
|
+
return errInfoOrMsg;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return makeIMap({
|
|
113
|
+
[ERROR_CODE]: code,
|
|
114
|
+
[ERROR_MESSAGE]: errInfoOrMsg,
|
|
115
|
+
[ERROR_DATA]: undefined,
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const createErrorClass = (code: ErrorCode) => class extends RpcError {
|
|
120
|
+
constructor(errInfoOrMsg: ErrorMap | string) {
|
|
121
|
+
super(makeErrInfo(errInfoOrMsg, code));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
126
|
+
export const InvalidRequest = createErrorClass(ErrorCode.InvalidRequest);
|
|
127
|
+
export const MethodNotFound = createErrorClass(ErrorCode.MethodNotFound);
|
|
128
|
+
export const InvalidParams = createErrorClass(ErrorCode.InvalidParams);
|
|
129
|
+
export const InternalError = createErrorClass(ErrorCode.InternalError);
|
|
130
|
+
export const ParseError = createErrorClass(ErrorCode.ParseError);
|
|
131
|
+
export const MethodCallTimeout = createErrorClass(ErrorCode.MethodCallTimeout);
|
|
132
|
+
export const MethodCallCancelled = createErrorClass(ErrorCode.MethodCallCancelled);
|
|
133
|
+
export const MethodCallException = createErrorClass(ErrorCode.MethodCallException);
|
|
134
|
+
export const Unknown = createErrorClass(ErrorCode.Unknown);
|
|
135
|
+
export const LoginRequired = createErrorClass(ErrorCode.LoginRequired);
|
|
136
|
+
export const UserIDRequired = createErrorClass(ErrorCode.UserIDRequired);
|
|
137
|
+
export const NotImplemented = createErrorClass(ErrorCode.NotImplemented);
|
|
138
|
+
/* eslint-enable @typescript-eslint/naming-convention */
|
|
119
139
|
|
|
120
140
|
class WsClient {
|
|
121
141
|
private requestId = 1;
|
|
@@ -140,7 +160,7 @@ class WsClient {
|
|
|
140
160
|
this.websocket = new WebSocket(options.wsUri);
|
|
141
161
|
this.websocket.binaryType = 'arraybuffer';
|
|
142
162
|
|
|
143
|
-
this.websocket.addEventListener('message', (evt: MessageEvent<ArrayBuffer>) => {
|
|
163
|
+
this.websocket.addEventListener('message', async (evt: MessageEvent<ArrayBuffer>) => {
|
|
144
164
|
const rpcVal = dataToRpcValue(evt.data);
|
|
145
165
|
const rpcMsg = RpcMessageZod.parse(rpcVal);
|
|
146
166
|
this.logDebug(`message received: ${toCpon(rpcMsg)}`);
|
|
@@ -156,7 +176,43 @@ class WsClient {
|
|
|
156
176
|
}
|
|
157
177
|
} else if (isRequest(rpcMsg)) {
|
|
158
178
|
if ('onRequest' in this.options) {
|
|
159
|
-
|
|
179
|
+
const respond = (value: RpcResponseValue) => {
|
|
180
|
+
this.sendRpcMessage(new RpcValueWithMetaData(
|
|
181
|
+
makeMetaMap({
|
|
182
|
+
[RPC_MESSAGE_CALLER_IDS]: rpcMsg.meta[RPC_MESSAGE_CALLER_IDS],
|
|
183
|
+
[RPC_MESSAGE_REQUEST_ID]: rpcMsg.meta[RPC_MESSAGE_REQUEST_ID],
|
|
184
|
+
}),
|
|
185
|
+
value,
|
|
186
|
+
));
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
const sendDelay = (progress: number) => {
|
|
191
|
+
respond(makeIMap({
|
|
192
|
+
[RPC_MESSAGE_DELAY]: new Double(progress),
|
|
193
|
+
}));
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
respond(makeIMap({
|
|
197
|
+
[RPC_MESSAGE_RESULT]: await this.options.onRequest(rpcMsg.meta[RPC_MESSAGE_SHV_PATH], rpcMsg.meta[RPC_MESSAGE_METHOD], rpcMsg.value[RPC_MESSAGE_PARAMS], sendDelay),
|
|
198
|
+
}));
|
|
199
|
+
} catch (error: unknown) {
|
|
200
|
+
const sendError = (error: RpcError) => {
|
|
201
|
+
respond(error.err_info);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
switch (true) {
|
|
205
|
+
case error instanceof RpcError:
|
|
206
|
+
sendError(error);
|
|
207
|
+
break;
|
|
208
|
+
case error instanceof Error:
|
|
209
|
+
sendError(new InternalError(error.message));
|
|
210
|
+
break;
|
|
211
|
+
default:
|
|
212
|
+
sendError(new InternalError('Unknown error'));
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
160
216
|
}
|
|
161
217
|
} else if (isResponse(rpcMsg)) {
|
|
162
218
|
const requestId = rpcMsg.meta[RPC_MESSAGE_REQUEST_ID];
|