libshv-js 3.3.4 → 3.3.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.3.4",
3
+ "version": "3.3.6",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/silicon-heaven/libshv-js",
19
19
  "exports": {
20
- "./bint.ts": "./src/bint.ts",
20
+ "./access.ts": "./src/access.ts",
21
21
  "./chainpack.ts": "./src/chainpack.ts",
22
22
  "./cpcontext.ts": "./src/cpcontext.ts",
23
23
  "./cpon.ts": "./src/cpon.ts",
package/src/access.ts ADDED
@@ -0,0 +1,103 @@
1
+ enum Role {
2
+ Browse = 'bws',
3
+ Read = 'rd',
4
+ Write = 'wr',
5
+ Command = 'cmd',
6
+ Config = 'cfg',
7
+ Service = 'srv',
8
+ SuperService = 'ssrv',
9
+ Devel = 'dev',
10
+ Admin = 'su',
11
+ }
12
+
13
+ enum AccessLevel {
14
+ None = 0,
15
+
16
+ Browse = 1,
17
+ Browse1,
18
+ Browse2,
19
+ Browse3,
20
+ Browse4,
21
+ Browse5,
22
+ Browse6,
23
+
24
+ Read = 8,
25
+ Read1,
26
+ Read2,
27
+ Read3,
28
+ Read4,
29
+ Read5,
30
+ Read6,
31
+ Read7,
32
+
33
+ Write = 16,
34
+ Write1,
35
+ Write2,
36
+ Write3,
37
+ Write4,
38
+ Write5,
39
+ Write6,
40
+ Write7,
41
+
42
+ Command = 24,
43
+ Command1,
44
+ Command2,
45
+ Command3,
46
+ Command4,
47
+ Command5,
48
+ Command6,
49
+ Command7,
50
+
51
+ Config = 32,
52
+ Config1,
53
+ Config2,
54
+ Config3,
55
+ Config4,
56
+ Config5,
57
+ Config6,
58
+ Config7,
59
+
60
+ Service = 40,
61
+ Service1,
62
+ Service2,
63
+ Service3,
64
+ Service4,
65
+ Service5,
66
+ Service6,
67
+ Service7,
68
+
69
+ SuperService = 48,
70
+ SuperService1,
71
+ SuperService2,
72
+ SuperService3,
73
+ SuperService4,
74
+ SuperService5,
75
+ SuperService6,
76
+ SuperService7,
77
+
78
+ Devel = 56,
79
+ Devel1,
80
+ Devel2,
81
+ Devel3,
82
+ Devel4,
83
+ Devel5,
84
+ Devel6,
85
+ Devel7,
86
+
87
+ Admin = 63,
88
+ }
89
+
90
+ export const accessLevelFromAccessString = (s: string): AccessLevel => {
91
+ switch (s as unknown) {
92
+ case Role.Browse: return AccessLevel.Browse;
93
+ case Role.Read: return AccessLevel.Read;
94
+ case Role.Write: return AccessLevel.Write;
95
+ case Role.Command: return AccessLevel.Command;
96
+ case Role.Config: return AccessLevel.Config;
97
+ case Role.Service: return AccessLevel.Service;
98
+ case Role.SuperService: return AccessLevel.SuperService;
99
+ case Role.Devel: return AccessLevel.Devel;
100
+ case Role.Admin: return AccessLevel.Admin;
101
+ default: throw new Error(`unknown access grant string: ${s}`);
102
+ }
103
+ };
package/src/ws-client.ts CHANGED
@@ -4,6 +4,7 @@ import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessage, type RpcResponse, Meth
4
4
  import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap} from './rpcvalue';
5
5
 
6
6
  const DEFAULT_TIMEOUT = 5000;
7
+ const DEFAULT_PING_INTERVAL = 30 * 1000;
7
8
 
8
9
  const dataToRpcValue = (buff: ArrayBuffer) => {
9
10
  const rd: ChainPackReader | CponReader = new ChainPackReader(buff);
@@ -39,6 +40,7 @@ type WsClientOptions = {
39
40
  password: string;
40
41
  loginType?: 'PLAIN' | 'AZURE';
41
42
  timeout?: number;
43
+ pingInterval?: number;
42
44
  wsUri: string;
43
45
  onConnected: () => void;
44
46
  onConnectionFailure: (error: Error) => void;
@@ -74,6 +76,8 @@ type DirResult = Array<IMap<{
74
76
 
75
77
  class WsClient {
76
78
  requestId = 1;
79
+ pingTimerId = -1;
80
+
77
81
  rpcHandlers: Array<{
78
82
  resolve: RpcResponseResolver;
79
83
  timeout_handle: number;
@@ -92,6 +96,7 @@ class WsClient {
92
96
  onDisconnected: WsClientOptions['onDisconnected'];
93
97
  onRequest: WsClientOptions['onRequest'];
94
98
  timeout: WsClientOptions['timeout'];
99
+ pingInterval: WsClientOptions['pingInterval'];
95
100
 
96
101
  constructor(options: WsClientOptions) {
97
102
  if (typeof options !== 'object') {
@@ -108,6 +113,7 @@ class WsClient {
108
113
  this.websocket = new WebSocket(options.wsUri);
109
114
  this.websocket.binaryType = 'arraybuffer';
110
115
 
116
+ this.pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
111
117
  this.onConnected = options.onConnected ?? (() => {/* nothing */});
112
118
  this.onConnectionFailure = options.onConnectionFailure ?? (() => {/* nothing */});
113
119
  this.onDisconnected = options.onDisconnected ?? (() => {/* nothing */});
@@ -144,6 +150,9 @@ class WsClient {
144
150
  }
145
151
  this.logDebug('SUCCESS: connected to shv broker');
146
152
  this.onConnected();
153
+ this.pingTimerId = window.setInterval(() => {
154
+ this.sendPing();
155
+ }, this.pingInterval);
147
156
  }).catch(() => {
148
157
  this.logDebug('FAILURE: couldn\' connected to shv broker');
149
158
  });
@@ -153,6 +162,7 @@ class WsClient {
153
162
  this.logDebug('DISCONNECTED');
154
163
  this.subscriptions.length = 0;
155
164
  this.onDisconnected();
165
+ window.clearInterval(this.pingTimerId);
156
166
  });
157
167
 
158
168
  this.websocket.addEventListener('message', evt => {
@@ -223,7 +233,7 @@ class WsClient {
223
233
  }
224
234
 
225
235
  sendRpcMessage(rpc_msg: RpcMessage) {
226
- if (this.websocket && this.websocket.readyState === 1) {
236
+ if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
227
237
  this.logDebug('sending rpc message:', rpc_msg.toCpon());
228
238
  const msg_data = new Uint8Array(rpc_msg.toChainPack());
229
239
 
@@ -284,6 +294,12 @@ class WsClient {
284
294
  });
285
295
  }
286
296
 
297
+ sendPing() {
298
+ this.callRpcMethod('.broker/app', 'ping').catch((error: unknown) => {
299
+ console.log('Failed to send ping:', error);
300
+ });
301
+ }
302
+
287
303
  accessGrantForMethodCall(path: string, method: string) {
288
304
  return this.callRpcMethod('.broker/currentClient', 'accessGrantForMethodCall', [path, method]);
289
305
  }