@ceeblue/web-utils 2.0.1 → 2.2.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.
@@ -140,7 +140,7 @@ type Params = {
140
140
  */
141
141
  declare enum Type {
142
142
  HESP = "HESP",
143
- WEBRTS = "WebRTS",
143
+ WRTS = "WebRTS",
144
144
  WEBRTC = "WebRTC",
145
145
  META = "Meta",
146
146
  DATA = "Data"
@@ -634,14 +634,14 @@ declare function objectEntries(value: any): [string, any][];
634
634
  * @param obj Any objects, strings, exceptions, errors, or number
635
635
  * @param params.space `''`, allows to configure space in the string representation
636
636
  * @param params.decimal `2`, allows to choose the number of decimal to display in the string representation
637
- * @param params.recursive `false`, allows to serialize recursively every object value, beware if a value refers to a already parsed value an infinite loop will occur
637
+ * @param params.recursion `1`, recursion depth to stringify recursively every object value until this depth, beware if a value refers to a already parsed value an infinite loop will occur
638
638
  * @param params.noBin `false`, when set skip binary encoding and write inplace a bin-length information
639
639
  * @returns the final string representation
640
640
  */
641
641
  declare function stringify(obj: any, params?: {
642
642
  space?: string;
643
643
  decimal?: number;
644
- recursive?: number;
644
+ recursion?: number;
645
645
  noBin?: boolean;
646
646
  }): string;
647
647
  /**
@@ -658,6 +658,10 @@ declare function safePromise<T>(timeout: number, promise: Promise<T>): Promise<u
658
658
  * Wait in milliseconds, requires a call with await keyword!
659
659
  */
660
660
  declare function sleep(ms: number): Promise<unknown>;
661
+ /**
662
+ * Test equality between two value whatever their type, array included
663
+ */
664
+ declare function equal(a: any, b: any): boolean;
661
665
  /**
662
666
  * fetch help method with few usefull fix:
663
667
  * - throw an string exception if response code is not 200 with the text of the response or uses statusText
@@ -671,6 +675,7 @@ declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Re
671
675
  declare function parseExtension(path: string): string;
672
676
 
673
677
  declare const Util_EMPTY_FUNCTION: typeof EMPTY_FUNCTION;
678
+ declare const Util_equal: typeof equal;
674
679
  declare const Util_fetch: typeof fetch;
675
680
  declare const Util_objectEntries: typeof objectEntries;
676
681
  declare const Util_objectFrom: typeof objectFrom;
@@ -683,7 +688,7 @@ declare const Util_time: typeof time;
683
688
  declare const Util_timeOrigin: typeof timeOrigin;
684
689
  declare const Util_toBin: typeof toBin;
685
690
  declare namespace Util {
686
- export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_fetch as fetch, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_parseExtension as parseExtension, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin };
691
+ export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_equal as equal, Util_fetch as fetch, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_parseExtension as parseExtension, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin };
687
692
  }
688
693
 
689
694
  /**
package/dist/web-utils.js CHANGED
@@ -521,14 +521,14 @@ function objectEntries(value) {
521
521
  * @param obj Any objects, strings, exceptions, errors, or number
522
522
  * @param params.space `''`, allows to configure space in the string representation
523
523
  * @param params.decimal `2`, allows to choose the number of decimal to display in the string representation
524
- * @param params.recursive `false`, allows to serialize recursively every object value, beware if a value refers to a already parsed value an infinite loop will occur
524
+ * @param params.recursion `1`, recursion depth to stringify recursively every object value until this depth, beware if a value refers to a already parsed value an infinite loop will occur
525
525
  * @param params.noBin `false`, when set skip binary encoding and write inplace a bin-length information
526
526
  * @returns the final string representation
527
527
  */
528
528
  // Online Javascript Editor for free
529
529
  // Write, Edit and Run your Javascript code using JS Online Compiler
530
530
  function stringify(obj, params = {}) {
531
- params = Object.assign({ space: ' ', decimal: 2, recursive: 1, noBin: false }, params);
531
+ params = Object.assign({ space: ' ', decimal: 2, recursion: 1, noBin: false }, params);
532
532
  if (obj == null) {
533
533
  return String(obj);
534
534
  }
@@ -549,7 +549,7 @@ function stringify(obj, params = {}) {
549
549
  return '[' + obj.byteLength + '#bytes]';
550
550
  }
551
551
  // boolean or string type or stop recursivity
552
- if (typeof obj === 'boolean' || obj.substring || !params.recursive) {
552
+ if (typeof obj === 'boolean' || obj.substring || !params.recursion) {
553
553
  // is already a string OR has to be stringified
554
554
  return String(obj);
555
555
  }
@@ -559,14 +559,14 @@ function stringify(obj, params = {}) {
559
559
  let res = '';
560
560
  for (const value of obj) {
561
561
  res += (res ? ',' : '[') + space;
562
- res += stringify(value, Object.assign(Object.assign({}, params), { recursive: params.recursive - 1 }));
562
+ res += stringify(value, Object.assign(Object.assign({}, params), { recursion: params.recursion - 1 }));
563
563
  }
564
564
  return (res += space + ']');
565
565
  }
566
566
  let res = '';
567
567
  for (const name in obj) {
568
568
  res += (res ? ',' : '{') + space + name + ':';
569
- res += stringify(obj[name], Object.assign(Object.assign({}, params), { recursive: params.recursive - 1 }));
569
+ res += stringify(obj[name], Object.assign(Object.assign({}, params), { recursion: params.recursion - 1 }));
570
570
  }
571
571
  return (res += space + '}');
572
572
  }
@@ -597,6 +597,34 @@ function sleep(ms) {
597
597
  setTimeout(resolve, ms);
598
598
  });
599
599
  }
600
+ /**
601
+ * Test equality between two value whatever their type, array included
602
+ */
603
+ function equal(a, b) {
604
+ if (Object(a) !== a) {
605
+ if (Object(b) === b) {
606
+ return false;
607
+ }
608
+ // both primitive (null and undefined included)
609
+ return a === b;
610
+ }
611
+ // complexe object
612
+ if (a[Symbol.iterator]) {
613
+ if (!b[Symbol.iterator]) {
614
+ return false;
615
+ }
616
+ if (a.length !== b.length) {
617
+ return false;
618
+ }
619
+ for (let i = 0; i !== a.length; ++i) {
620
+ if (a[i] !== b[i]) {
621
+ return false;
622
+ }
623
+ }
624
+ return true;
625
+ }
626
+ return a === b;
627
+ }
600
628
  /**
601
629
  * fetch help method with few usefull fix:
602
630
  * - throw an string exception if response code is not 200 with the text of the response or uses statusText
@@ -622,7 +650,7 @@ function parseExtension(path) {
622
650
  const dot = path.lastIndexOf('.');
623
651
  const ext = dot >= 0 && dot > path.lastIndexOf('/') ? path.substring(dot) : '';
624
652
  return ext;
625
- }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,fetch:fetch,objectEntries:objectEntries,objectFrom:objectFrom,options:options,parseExtension:parseExtension,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,timeOrigin:timeOrigin,toBin:toBin});/**
653
+ }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,equal:equal,fetch:fetch,objectEntries:objectEntries,objectFrom:objectFrom,options:options,parseExtension:parseExtension,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,timeOrigin:timeOrigin,toBin:toBin});/**
626
654
  * Copyright 2024 Ceeblue B.V.
627
655
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
628
656
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
@@ -795,7 +823,7 @@ class NetAddress {
795
823
  var Type;
796
824
  (function (Type) {
797
825
  Type["HESP"] = "HESP";
798
- Type["WEBRTS"] = "WebRTS";
826
+ Type["WRTS"] = "WebRTS";
799
827
  Type["WEBRTC"] = "WebRTC";
800
828
  Type["META"] = "Meta";
801
829
  Type["DATA"] = "Data";
@@ -821,8 +849,8 @@ function buildURL(type, params, protocol = 'wss') {
821
849
  case Type.WEBRTC:
822
850
  url.pathname = '/webrtc/' + params.streamName;
823
851
  break;
824
- case Type.WEBRTS:
825
- url.pathname = '/webrts/' + params.streamName;
852
+ case Type.WRTS:
853
+ url.pathname = '/wrts/' + params.streamName;
826
854
  break;
827
855
  case Type.META:
828
856
  url.pathname = '/json_' + params.streamName + '.js';
@@ -1599,15 +1627,17 @@ class WebSocketReliable extends EventEmitter {
1599
1627
  ws.onclose = (e) => {
1600
1628
  if (!this._opened) {
1601
1629
  // close during connection
1602
- this.close(url.toString() + ' connection failed (' + String(e.reason || e.code) + ')');
1630
+ // the caller can differentiate this case of one server shutdown by looking if onOpen was op
1631
+ this.close(url.toString() + ' connection failed, ' + String(e.reason || e.code));
1603
1632
  }
1604
1633
  else if (e.code === 1000 || e.code === 1005) {
1605
- // normal disconnection from server (no error code)
1634
+ // normal disconnection from server, no error to indicate that a reconnection is possible!
1635
+ // the caller can differentiate this case of one explicit websocket.close() call in the encpasulating class
1606
1636
  this.close(url.toString() + ' shutdown');
1607
1637
  }
1608
1638
  else {
1609
- // disconnection from server
1610
- this.close(url.toString() + ' disconnection (' + String(e.reason || e.code) + ')');
1639
+ // abnormal disconnection from server
1640
+ this.close(url.toString() + ' disconnection, ' + String(e.reason || e.code));
1611
1641
  }
1612
1642
  };
1613
1643
  // Wrap send method to queue messages until connection is established.
@@ -1625,7 +1655,7 @@ class WebSocketReliable extends EventEmitter {
1625
1655
  * @returns this
1626
1656
  */
1627
1657
  send(message, queueing = false) {
1628
- if (!this._ws) {
1658
+ if (this._closed) {
1629
1659
  throw Error('Open socket before to send data');
1630
1660
  }
1631
1661
  if (queueing || !this._opened) {
@@ -1661,10 +1691,12 @@ class WebSocketReliable extends EventEmitter {
1661
1691
  this._ws.onopen = this._ws.onclose = this._ws.onmessage = null; // otherwise can receive message AFTER close!
1662
1692
  this._ws.close(); // Don't set to undefined to keep this._ws properties valid!
1663
1693
  // release resources!
1664
- this._opened = false;
1665
1694
  this._queueing.length = 0;
1666
1695
  this._queueingBytes = 0;
1667
1696
  this.onClose(error);
1697
+ // Reset _opened in last to allow to differenciate in onClose an error while connecting OR while connected
1698
+ // Is welcome to attempt a reconnection when no error OR when error on connection!
1699
+ this._opened = false;
1668
1700
  }
1669
1701
  _send(message) {
1670
1702
  if (!this._ws) {
@@ -1678,4 +1710,4 @@ class WebSocketReliable extends EventEmitter {
1678
1710
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1679
1711
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1680
1712
  */
1681
- const VERSION = '2.0.1';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map
1713
+ const VERSION = '2.2.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map