podasync-ws-only 2.9.0-snapshot.6 → 2.9.0-snapshot.7

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.
@@ -33,7 +33,8 @@
33
33
  message: {},
34
34
  asyncReady: {},
35
35
  stateChange: {},
36
- error: {}
36
+ error: {},
37
+ msgLogs: {}
37
38
  },
38
39
  ackCallback = {},
39
40
  socket,
@@ -388,6 +389,11 @@
388
389
  });
389
390
  },
390
391
  handleSocketMessage = function (msg) {
392
+ fireEvent("msgLogs", {
393
+ msg,
394
+ direction: "receive",
395
+ time: new Date().getTime()
396
+ });
391
397
  var ack;
392
398
  if (msg.type === asyncMessageType.MESSAGE_ACK_NEEDED || msg.type === asyncMessageType.MESSAGE_SENDER_ACK_NEEDED) {
393
399
  ack = function () {
@@ -582,6 +588,11 @@
582
588
  }
583
589
  },
584
590
  pushSendData = function (msg) {
591
+ fireEvent("msgLogs", {
592
+ msg,
593
+ direction: "send",
594
+ time: new Date().getTime()
595
+ });
585
596
  if (onSendLogging) {
586
597
  asyncLogger('Send', msg);
587
598
  }
@@ -704,7 +715,7 @@
704
715
  peerId: peerId
705
716
  });
706
717
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
707
- socket.close();
718
+ socket && socket.close();
708
719
  break;
709
720
  case 'webrtc':
710
721
  socketState = socketStateType.CLOSED;
@@ -716,7 +727,7 @@
716
727
  peerId: peerId
717
728
  });
718
729
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
719
- webRTCClass.close();
730
+ webRTCClass && webRTCClass.close();
720
731
  break;
721
732
  }
722
733
  };
@@ -743,7 +754,7 @@
743
754
  reconnOnClose.set(false);
744
755
  // reconnectOnClose = false;
745
756
 
746
- socket.close();
757
+ socket && socket.close();
747
758
  break;
748
759
  case 'webrtc':
749
760
  socketState = socketStateType.CLOSED;
@@ -756,7 +767,7 @@
756
767
  });
757
768
  reconnOnClose.set(false);
758
769
  // reconnectOnClose = false;
759
- webRTCClass.close();
770
+ webRTCClass && webRTCClass.close();
760
771
  break;
761
772
  }
762
773
  };
@@ -775,7 +786,7 @@
775
786
  peerId: peerId
776
787
  });
777
788
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
778
- if (protocol === "websocket") socket.close();else if (protocol == "webrtc") webRTCClass.close();
789
+ if (protocol === "websocket") socket && socket.close();else if (protocol == "webrtc") webRTCClass && webRTCClass.close();
779
790
 
780
791
  // let tmpReconnectOnClose = reconnectOnClose;
781
792
  // reconnectOnClose = false;
@@ -239,12 +239,14 @@ let dataChannelCallbacks = {
239
239
  },
240
240
  onmessage: function (event) {
241
241
  variables.pingController.resetPingLoop();
242
- var messageData = JSON.parse(event.data);
243
- console.log("[Async][WebRTC] Receive ", event.data);
244
- eventCallback["message"](messageData);
242
+ decompressResponse(event.data).then(result => {
243
+ var messageData = JSON.parse(result);
244
+ console.log("[Async][WebRTC] Receive ", result);
245
+ eventCallback["message"](messageData);
246
+ });
245
247
  },
246
248
  onerror: function (error) {
247
- logLevel.debug && console.debug("[Async][Socket.js] dataChannel.onerror happened. EventData:", event);
249
+ defaultConfig.logLevel.debug && console.debug("[Async][Socket.js] dataChannel.onerror happened. EventData:", event);
248
250
  eventCallback["error"](event);
249
251
  },
250
252
  onclose: function (event) {
@@ -375,9 +377,9 @@ function resetVariables() {
375
377
  console.log("resetVariables");
376
378
  eventCallback["close"]();
377
379
  variables.pingController.stopPingLoop();
378
- variables.dataChannel.close();
380
+ variables.dataChannel && variables.dataChannel.close();
379
381
  variables.dataChannel = null;
380
- variables.peerConnection.close();
382
+ variables.peerConnection && variables.peerConnection.close();
381
383
  variables.peerConnection = null;
382
384
  variables.candidatesQueue = [];
383
385
  variables.clientId = null;
@@ -425,4 +427,57 @@ let publicized = {
425
427
  resetVariables();
426
428
  }
427
429
  };
430
+
431
+ /**
432
+ * Decompress results
433
+ */
434
+ function decompress(byteArray, encoding) {
435
+ const cs = new DecompressionStream(encoding);
436
+ const writer = cs.writable.getWriter();
437
+ writer.write(byteArray);
438
+ writer.close();
439
+ return new Response(cs.readable).arrayBuffer().then(function (arrayBuffer) {
440
+ return new TextDecoder().decode(arrayBuffer);
441
+ });
442
+ }
443
+ async function decompressResponse(compressedData) {
444
+ return await decompress(_base64UrlToArrayBuffer(compressedData), 'gzip');
445
+ }
446
+
447
+ //utility
448
+
449
+ /**
450
+ * Array buffer to base64Url string
451
+ * - arrBuff->byte[]->biStr->b64->b64u
452
+ * @param arrayBuffer
453
+ * @returns {string}
454
+ * @private
455
+ */
456
+ function _arrayBufferToBase64Url(arrayBuffer) {
457
+ console.log('base64Url from array buffer:', arrayBuffer);
458
+ let base64Url = window.btoa(String.fromCodePoint(...new Uint8Array(arrayBuffer)));
459
+ base64Url = base64Url.replaceAll('+', '-');
460
+ base64Url = base64Url.replaceAll('/', '_');
461
+ console.log('base64Url:', base64Url);
462
+ return base64Url;
463
+ }
464
+
465
+ /**
466
+ * Base64Url string to array buffer
467
+ * - b64u->b64->biStr->byte[]->arrBuff
468
+ * @param base64Url
469
+ * @returns {ArrayBufferLike}
470
+ * @private
471
+ */
472
+ function _base64UrlToArrayBuffer(base64) {
473
+ console.log('array buffer from base64Url:', base64);
474
+ const binaryString = window.atob(base64);
475
+ const length = binaryString.length;
476
+ const bytes = new Uint8Array(length);
477
+ for (let i = 0; i < length; i++) {
478
+ bytes[i] = binaryString.charCodeAt(i);
479
+ }
480
+ console.log('array buffer:', bytes.buffer);
481
+ return bytes.buffer;
482
+ }
428
483
  module.exports = WebRTCClass;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "podasync-ws-only",
3
- "version": "2.9.0-snapshot.6",
3
+ "version": "2.9.0-snapshot.7",
4
4
  "description": "Fanap's POD Async service (DIRANA) - Websocket only",
5
5
  "main": "./dist/node/network/async.js",
6
6
  "scripts": {
@@ -40,7 +40,8 @@
40
40
  message: {},
41
41
  asyncReady: {},
42
42
  stateChange: {},
43
- error: {}
43
+ error: {},
44
+ msgLogs: {}
44
45
  },
45
46
  ackCallback = {},
46
47
  socket,
@@ -203,6 +204,7 @@
203
204
  });
204
205
 
205
206
  socket.on('message', function (msg) {
207
+
206
208
  handleSocketMessage(msg);
207
209
  if (onReceiveLogging) {
208
210
  asyncLogger('Receive', msg);
@@ -435,6 +437,12 @@
435
437
  },
436
438
 
437
439
  handleSocketMessage = function (msg) {
440
+ fireEvent("msgLogs", {
441
+ msg,
442
+ direction: "receive",
443
+ time: new Date().getTime()
444
+ });
445
+
438
446
  var ack;
439
447
 
440
448
  if (msg.type === asyncMessageType.MESSAGE_ACK_NEEDED || msg.type === asyncMessageType.MESSAGE_SENDER_ACK_NEEDED) {
@@ -671,6 +679,12 @@
671
679
  },
672
680
 
673
681
  pushSendData = function (msg) {
682
+ fireEvent("msgLogs", {
683
+ msg,
684
+ direction: "send",
685
+ time: new Date().getTime()
686
+ });
687
+
674
688
  if (onSendLogging) {
675
689
  asyncLogger('Send', msg);
676
690
  }
@@ -821,7 +835,7 @@
821
835
  });
822
836
 
823
837
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
824
- socket.close();
838
+ socket && socket.close();
825
839
  break;
826
840
  case 'webrtc':
827
841
  socketState = socketStateType.CLOSED;
@@ -834,7 +848,7 @@
834
848
  });
835
849
 
836
850
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
837
- webRTCClass.close();
851
+ webRTCClass && webRTCClass.close();
838
852
 
839
853
  break;
840
854
  }
@@ -864,7 +878,7 @@
864
878
  reconnOnClose.set(false)
865
879
  // reconnectOnClose = false;
866
880
 
867
- socket.close();
881
+ socket && socket.close();
868
882
  break;
869
883
  case 'webrtc':
870
884
  socketState = socketStateType.CLOSED;
@@ -877,7 +891,7 @@
877
891
  });
878
892
  reconnOnClose.set(false)
879
893
  // reconnectOnClose = false;
880
- webRTCClass.close();
894
+ webRTCClass && webRTCClass.close();
881
895
 
882
896
  break;
883
897
  }
@@ -901,9 +915,9 @@
901
915
 
902
916
  socketReconnectRetryInterval && clearTimeout(socketReconnectRetryInterval);
903
917
  if(protocol === "websocket")
904
- socket.close();
918
+ socket && socket.close();
905
919
  else if(protocol == "webrtc")
906
- webRTCClass.close()
920
+ webRTCClass && webRTCClass.close()
907
921
 
908
922
  // let tmpReconnectOnClose = reconnectOnClose;
909
923
  // reconnectOnClose = false;
@@ -257,13 +257,15 @@ let dataChannelCallbacks = {
257
257
  onmessage: function (event) {
258
258
 
259
259
  variables.pingController.resetPingLoop();
260
- var messageData = JSON.parse(event.data);
261
- console.log("[Async][WebRTC] Receive ", event.data);
262
- eventCallback["message"](messageData);
260
+ decompressResponse(event.data).then(result => {
261
+ var messageData = JSON.parse(result);
262
+ console.log("[Async][WebRTC] Receive ", result);
263
+ eventCallback["message"](messageData);
264
+ });
263
265
  },
264
266
 
265
267
  onerror: function (error) {
266
- logLevel.debug && console.debug("[Async][Socket.js] dataChannel.onerror happened. EventData:", event);
268
+ defaultConfig.logLevel.debug && console.debug("[Async][Socket.js] dataChannel.onerror happened. EventData:", event);
267
269
  eventCallback["error"](event);
268
270
  },
269
271
  onclose: function (event) {
@@ -411,9 +413,9 @@ function resetVariables() {
411
413
  console.log("resetVariables");
412
414
  eventCallback["close"]();
413
415
  variables.pingController.stopPingLoop();
414
- variables.dataChannel.close();
416
+ variables.dataChannel && variables.dataChannel.close();
415
417
  variables.dataChannel = null;
416
- variables.peerConnection.close();
418
+ variables.peerConnection && variables.peerConnection.close();
417
419
  variables.peerConnection = null;
418
420
  variables.candidatesQueue = [];
419
421
  variables.clientId = null;
@@ -471,4 +473,61 @@ let publicized = {
471
473
  }
472
474
  };
473
475
 
476
+
477
+ /**
478
+ * Decompress results
479
+ */
480
+ function decompress(byteArray, encoding) {
481
+ const cs = new DecompressionStream(encoding);
482
+ const writer = cs.writable.getWriter();
483
+ writer.write(byteArray);
484
+ writer.close();
485
+ return new Response(cs.readable).arrayBuffer().then(function (arrayBuffer) {
486
+ return new TextDecoder().decode(arrayBuffer);
487
+ });
488
+ }
489
+
490
+ async function decompressResponse(compressedData){
491
+ return await decompress(_base64UrlToArrayBuffer(compressedData), 'gzip');
492
+ }
493
+
494
+ //utility
495
+
496
+ /**
497
+ * Array buffer to base64Url string
498
+ * - arrBuff->byte[]->biStr->b64->b64u
499
+ * @param arrayBuffer
500
+ * @returns {string}
501
+ * @private
502
+ */
503
+ function _arrayBufferToBase64Url(arrayBuffer) {
504
+ console.log('base64Url from array buffer:', arrayBuffer);
505
+
506
+ let base64Url = window.btoa(String.fromCodePoint(...new Uint8Array(arrayBuffer)));
507
+ base64Url = base64Url.replaceAll('+', '-');
508
+ base64Url = base64Url.replaceAll('/', '_');
509
+
510
+ console.log('base64Url:', base64Url);
511
+ return base64Url;
512
+ }
513
+
514
+ /**
515
+ * Base64Url string to array buffer
516
+ * - b64u->b64->biStr->byte[]->arrBuff
517
+ * @param base64Url
518
+ * @returns {ArrayBufferLike}
519
+ * @private
520
+ */
521
+ function _base64UrlToArrayBuffer(base64) {
522
+ console.log('array buffer from base64Url:', base64);
523
+ const binaryString = window.atob(base64);
524
+ const length = binaryString.length;
525
+ const bytes = new Uint8Array(length);
526
+ for (let i = 0; i < length; i++) {
527
+ bytes[i] = binaryString.charCodeAt(i);
528
+ }
529
+ console.log('array buffer:', bytes.buffer);
530
+ return bytes.buffer;
531
+ }
532
+
474
533
  module.exports = WebRTCClass;