libshv-js 3.3.2 → 3.3.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ws-client.ts +22 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.3.2",
3
+ "version": "3.3.4",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
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
- this.callRpcMethod(undefined, 'hello').then(() => {
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(() => {
@@ -178,6 +193,7 @@ class WsClient {
178
193
  });
179
194
  }
180
195
 
196
+ callRpcMethod(shv_path: '.broker/currentClient', method: 'accessGrantForMethodCall', params: [string, string]): Promise<RpcResponse<string>>;
181
197
  callRpcMethod(shv_path: string | undefined, method: 'dir', params?: RpcValue): Promise<RpcResponse<DirResult>>;
182
198
  callRpcMethod(shv_path: string | undefined, method: 'ls', params?: RpcValue): Promise<RpcResponse<LsResult>>;
183
199
  callRpcMethod(shv_path: string | undefined, method: string, params?: RpcValue): Promise<RpcResponse>;
@@ -268,6 +284,10 @@ class WsClient {
268
284
  });
269
285
  }
270
286
 
287
+ accessGrantForMethodCall(path: string, method: string) {
288
+ return this.callRpcMethod('.broker/currentClient', 'accessGrantForMethodCall', [path, method]);
289
+ }
290
+
271
291
  close() {
272
292
  this.websocket.close();
273
293
  }