libshv-js 3.7.0 → 3.8.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/rpcmessage.ts +1 -0
- package/src/ws-client.ts +67 -17
package/package.json
CHANGED
package/src/rpcmessage.ts
CHANGED
|
@@ -71,6 +71,7 @@ const RpcSignalZod = z.withMeta(RpcSignalMetaZod, RpcSignalValueZod);
|
|
|
71
71
|
const RpcMessageZod = z.union([RpcRequestZod, RpcResponseZod, RpcSignalZod]);
|
|
72
72
|
export type RpcRequest = z.infer<typeof RpcRequestZod>;
|
|
73
73
|
export type RpcResponse = z.infer<typeof RpcResponseZod>;
|
|
74
|
+
export type RpcResponseValue = z.infer<typeof RpcResponseValueZod>;
|
|
74
75
|
export type RpcSignal = z.infer<typeof RpcSignalZod>;
|
|
75
76
|
export type RpcMessage = z.infer<typeof RpcMessageZod>;
|
|
76
77
|
|
package/src/ws-client.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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';
|
|
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} from './rpcmessage';
|
|
4
4
|
import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap, RpcValueWithMetaData, makeMetaMap} from './rpcvalue';
|
|
5
5
|
|
|
6
6
|
const DEFAULT_TIMEOUT = 5000;
|
|
@@ -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) => 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,37 @@ 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
|
+
respond(makeIMap({
|
|
191
|
+
[RPC_MESSAGE_RESULT]: await this.options.onRequest(rpcMsg.meta[RPC_MESSAGE_SHV_PATH], rpcMsg.meta[RPC_MESSAGE_METHOD], rpcMsg.value[RPC_MESSAGE_PARAMS]),
|
|
192
|
+
}));
|
|
193
|
+
} catch (error: unknown) {
|
|
194
|
+
const sendError = (error: RpcError) => {
|
|
195
|
+
respond(error.err_info);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
switch (true) {
|
|
199
|
+
case error instanceof RpcError:
|
|
200
|
+
sendError(error);
|
|
201
|
+
break;
|
|
202
|
+
case error instanceof Error:
|
|
203
|
+
sendError(new InternalError(error.message));
|
|
204
|
+
break;
|
|
205
|
+
default:
|
|
206
|
+
sendError(new InternalError('Unknown error'));
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
160
210
|
}
|
|
161
211
|
} else if (isResponse(rpcMsg)) {
|
|
162
212
|
const requestId = rpcMsg.meta[RPC_MESSAGE_REQUEST_ID];
|