libshv-js 10.0.2 → 10.1.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/dist/ws-client.d.ts +2 -0
- package/dist/ws-client.js +45 -12
- package/package.json +3 -3
package/dist/ws-client.d.ts
CHANGED
|
@@ -227,7 +227,9 @@ declare class WsClient {
|
|
|
227
227
|
private readonly options;
|
|
228
228
|
private readonly timeout;
|
|
229
229
|
private shvApiVersion;
|
|
230
|
+
private pendingWebSocketData;
|
|
230
231
|
constructor(options: WsClientOptions<MethodHandler>);
|
|
232
|
+
private extractWebSocketDatagrams;
|
|
231
233
|
private workflowsProcedure;
|
|
232
234
|
private loginProcedure;
|
|
233
235
|
callRpcMethod(shvPath: '.broker/currentClient', method: 'accessGrantForMethodCall', params: [string, string], options?: CallRpcMethodOptions): RpcRequestPromise<ResultOrError<string>>;
|
package/dist/ws-client.js
CHANGED
|
@@ -148,6 +148,7 @@ class WsClient {
|
|
|
148
148
|
options;
|
|
149
149
|
timeout;
|
|
150
150
|
shvApiVersion = undefined;
|
|
151
|
+
pendingWebSocketData = new Uint8Array(0);
|
|
151
152
|
constructor(options) {
|
|
152
153
|
if (typeof options !== 'object') {
|
|
153
154
|
throw new TypeError('No options object supplied');
|
|
@@ -368,19 +369,25 @@ class WsClient {
|
|
|
368
369
|
};
|
|
369
370
|
// eslint-disable-next-line @typescript-eslint/strict-void-return
|
|
370
371
|
this.websocket.addEventListener('message', async (evt) => {
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
372
|
+
const incomingData = new Uint8Array(evt.data);
|
|
373
|
+
this.pendingWebSocketData = Uint8Array.from([...this.pendingWebSocketData, ...incomingData]);
|
|
374
|
+
const pendingHandlers = [];
|
|
375
|
+
for (const datagram of this.extractWebSocketDatagrams()) {
|
|
376
|
+
const rpcVal = dataToRpcValue(datagram);
|
|
377
|
+
const parseMessage = this.options.parseMessage ?? defaultParseMessage;
|
|
378
|
+
const rpcMsg = parseMessage(rpcVal);
|
|
379
|
+
this.logDebug(`message received: ${toCpon(rpcMsg)}`);
|
|
380
|
+
if (isSignal(rpcMsg)) {
|
|
381
|
+
pendingHandlers.push(handleSignal(rpcMsg));
|
|
382
|
+
}
|
|
383
|
+
else if (isRequest(rpcMsg)) {
|
|
384
|
+
pendingHandlers.push(handleRequest(rpcMsg));
|
|
385
|
+
}
|
|
386
|
+
else if (isResponse(rpcMsg)) {
|
|
387
|
+
handleResponse(rpcMsg);
|
|
388
|
+
}
|
|
383
389
|
}
|
|
390
|
+
await Promise.all(pendingHandlers);
|
|
384
391
|
});
|
|
385
392
|
this.websocket.addEventListener('error', evt => {
|
|
386
393
|
console.log('WebSocket ERROR', evt);
|
|
@@ -393,6 +400,32 @@ class WsClient {
|
|
|
393
400
|
this.loginProcedure(this.options);
|
|
394
401
|
}
|
|
395
402
|
}
|
|
403
|
+
extractWebSocketDatagrams() {
|
|
404
|
+
const datagrams = [];
|
|
405
|
+
let offset = 0;
|
|
406
|
+
while (offset < this.pendingWebSocketData.length) {
|
|
407
|
+
const reader = new ChainPackReader(this.pendingWebSocketData.slice(offset).buffer);
|
|
408
|
+
let length;
|
|
409
|
+
try {
|
|
410
|
+
length = reader.readUIntData();
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
if (error instanceof RangeError) {
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
throw error;
|
|
417
|
+
}
|
|
418
|
+
const prefixLength = reader.ctx.index;
|
|
419
|
+
if (this.pendingWebSocketData.length - offset - prefixLength < length) {
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
const end = offset + prefixLength + length;
|
|
423
|
+
datagrams.push(this.pendingWebSocketData.slice(offset, end).buffer);
|
|
424
|
+
offset = end;
|
|
425
|
+
}
|
|
426
|
+
this.pendingWebSocketData = this.pendingWebSocketData.slice(offset);
|
|
427
|
+
return datagrams;
|
|
428
|
+
}
|
|
396
429
|
workflowsProcedure(options) {
|
|
397
430
|
let workflowsObtained = false;
|
|
398
431
|
this.websocket.addEventListener('open', () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libshv-js",
|
|
3
|
-
"version": "10.0
|
|
3
|
+
"version": "10.1.0",
|
|
4
4
|
"description": "Typescript implementation of libshv",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
"@typescript-eslint/parser": "^8.56.1",
|
|
42
42
|
"@eslint/js": "^10.0.1",
|
|
43
43
|
"eslint": "^10.0.0",
|
|
44
|
-
"eslint-config-xo": "^
|
|
44
|
+
"eslint-config-xo": "^5.0.0",
|
|
45
45
|
"eslint-formatter-unix": "^9.0.1",
|
|
46
|
-
"eslint-plugin-unicorn": "^
|
|
46
|
+
"eslint-plugin-unicorn": "^77.0.0",
|
|
47
47
|
"typescript": "^6.0.2",
|
|
48
48
|
"typescript-eslint": "^8.46.1"
|
|
49
49
|
},
|