libshv-js 10.0.3 → 11.0.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.
@@ -1,10 +1,12 @@
1
1
  declare class UInt<T extends number = number> {
2
+ private readonly uintBrand;
2
3
  readonly value: T;
3
4
  constructor(u: T | UInt<T>);
4
5
  [Symbol.toPrimitive](): T;
5
6
  toString(): string;
6
7
  }
7
8
  declare class Double {
9
+ private readonly doubleBrand;
8
10
  readonly value: number;
9
11
  constructor(u: number);
10
12
  }
@@ -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 rpcVal = dataToRpcValue(evt.data);
372
- const parseMessage = this.options.parseMessage ?? defaultParseMessage;
373
- const rpcMsg = parseMessage(rpcVal);
374
- this.logDebug(`message received: ${toCpon(rpcMsg)}`);
375
- if (isSignal(rpcMsg)) {
376
- await handleSignal(rpcMsg);
377
- }
378
- else if (isRequest(rpcMsg)) {
379
- await handleRequest(rpcMsg);
380
- }
381
- else if (isResponse(rpcMsg)) {
382
- handleResponse(rpcMsg);
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",
3
+ "version": "11.0.0",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",