libshv-js 3.3.3 → 3.3.5
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 +2 -2
- package/src/access.ts +103 -0
- package/src/ws-client.ts +17 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libshv-js",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.5",
|
|
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
|
-
"./
|
|
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
|
@@ -41,6 +41,7 @@ type WsClientOptions = {
|
|
|
41
41
|
timeout?: number;
|
|
42
42
|
wsUri: string;
|
|
43
43
|
onConnected: () => void;
|
|
44
|
+
onConnectionFailure: (error: Error) => void;
|
|
44
45
|
onDisconnected: () => void;
|
|
45
46
|
onRequest: (rpc_msg: RpcMessage) => void;
|
|
46
47
|
};
|
|
@@ -87,6 +88,7 @@ class WsClient {
|
|
|
87
88
|
password: WsClientOptions['password'];
|
|
88
89
|
loginType: WsClientOptions['loginType'];
|
|
89
90
|
onConnected: WsClientOptions['onConnected'];
|
|
91
|
+
onConnectionFailure: WsClientOptions['onConnectionFailure'];
|
|
90
92
|
onDisconnected: WsClientOptions['onDisconnected'];
|
|
91
93
|
onRequest: WsClientOptions['onRequest'];
|
|
92
94
|
timeout: WsClientOptions['timeout'];
|
|
@@ -107,6 +109,7 @@ class WsClient {
|
|
|
107
109
|
this.websocket.binaryType = 'arraybuffer';
|
|
108
110
|
|
|
109
111
|
this.onConnected = options.onConnected ?? (() => {/* nothing */});
|
|
112
|
+
this.onConnectionFailure = options.onConnectionFailure ?? (() => {/* nothing */});
|
|
110
113
|
this.onDisconnected = options.onDisconnected ?? (() => {/* nothing */});
|
|
111
114
|
this.onRequest = options.onRequest ?? (() => {/* nothing */});
|
|
112
115
|
|
|
@@ -114,7 +117,15 @@ class WsClient {
|
|
|
114
117
|
|
|
115
118
|
this.websocket.addEventListener('open', () => {
|
|
116
119
|
this.logDebug('CONNECTED');
|
|
117
|
-
|
|
120
|
+
const handleConnectionError = (error: Error) => {
|
|
121
|
+
this.logDebug('FAILURE: couldn\'t perform initial handshake', error.message);
|
|
122
|
+
this.onConnectionFailure(error);
|
|
123
|
+
};
|
|
124
|
+
this.callRpcMethod(undefined, 'hello').then(response => {
|
|
125
|
+
if (response instanceof Error) {
|
|
126
|
+
handleConnectionError(response);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
118
129
|
const params = makeMap({
|
|
119
130
|
login: makeMap({
|
|
120
131
|
password: this.password,
|
|
@@ -126,7 +137,11 @@ class WsClient {
|
|
|
126
137
|
}),
|
|
127
138
|
});
|
|
128
139
|
return this.callRpcMethod(undefined, 'login', params);
|
|
129
|
-
}).then(
|
|
140
|
+
}).then(response => {
|
|
141
|
+
if (response instanceof Error) {
|
|
142
|
+
handleConnectionError(response);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
130
145
|
this.logDebug('SUCCESS: connected to shv broker');
|
|
131
146
|
this.onConnected();
|
|
132
147
|
}).catch(() => {
|