@ceeblue/web-utils 2.0.0 → 2.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.
@@ -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
  /**
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
  }
@@ -541,8 +541,15 @@ function stringify(obj, params = {}) {
541
541
  if (obj.toFixed) {
542
542
  return obj.toFixed(Number(params.decimal) || 0);
543
543
  }
544
+ if (obj.byteLength != null && (obj === null || obj === void 0 ? void 0 : obj[Symbol.iterator])) {
545
+ // Binary!
546
+ if (!params.noBin) {
547
+ return _decoder.decode(obj);
548
+ }
549
+ return '[' + obj.byteLength + '#bytes]';
550
+ }
544
551
  // boolean or string type or stop recursivity
545
- if (typeof obj === 'boolean' || obj.substring || !params.recursive) {
552
+ if (typeof obj === 'boolean' || obj.substring || !params.recursion) {
546
553
  // is already a string OR has to be stringified
547
554
  return String(obj);
548
555
  }
@@ -552,21 +559,14 @@ function stringify(obj, params = {}) {
552
559
  let res = '';
553
560
  for (const value of obj) {
554
561
  res += (res ? ',' : '[') + space;
555
- 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 }));
556
563
  }
557
564
  return (res += space + ']');
558
565
  }
559
- if (obj.byteLength != null && (obj === null || obj === void 0 ? void 0 : obj[Symbol.iterator])) {
560
- // Binary!
561
- return _decoder.decode(obj);
562
- }
563
566
  let res = '';
564
- if (params.noBin) {
565
- return '[' + obj.byteLength + '#bytes]';
566
- }
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
  }
@@ -1599,15 +1599,17 @@ class WebSocketReliable extends EventEmitter {
1599
1599
  ws.onclose = (e) => {
1600
1600
  if (!this._opened) {
1601
1601
  // close during connection
1602
- this.close(url.toString() + ' connection failed (' + String(e.reason || e.code) + ')');
1602
+ // the caller can differentiate this case of one server shutdown by looking if onOpen was op
1603
+ this.close(url.toString() + ' connection failed, ' + String(e.reason || e.code));
1603
1604
  }
1604
1605
  else if (e.code === 1000 || e.code === 1005) {
1605
- // normal disconnection from server (no error code)
1606
+ // normal disconnection from server, no error to indicate that a reconnection is possible!
1607
+ // the caller can differentiate this case of one explicit websocket.close() call in the encpasulating class
1606
1608
  this.close(url.toString() + ' shutdown');
1607
1609
  }
1608
1610
  else {
1609
- // disconnection from server
1610
- this.close(url.toString() + ' disconnection (' + String(e.reason || e.code) + ')');
1611
+ // abnormal disconnection from server
1612
+ this.close(url.toString() + ' disconnection, ' + String(e.reason || e.code));
1611
1613
  }
1612
1614
  };
1613
1615
  // Wrap send method to queue messages until connection is established.
@@ -1625,7 +1627,7 @@ class WebSocketReliable extends EventEmitter {
1625
1627
  * @returns this
1626
1628
  */
1627
1629
  send(message, queueing = false) {
1628
- if (!this._ws) {
1630
+ if (this._closed) {
1629
1631
  throw Error('Open socket before to send data');
1630
1632
  }
1631
1633
  if (queueing || !this._opened) {
@@ -1661,10 +1663,12 @@ class WebSocketReliable extends EventEmitter {
1661
1663
  this._ws.onopen = this._ws.onclose = this._ws.onmessage = null; // otherwise can receive message AFTER close!
1662
1664
  this._ws.close(); // Don't set to undefined to keep this._ws properties valid!
1663
1665
  // release resources!
1664
- this._opened = false;
1665
1666
  this._queueing.length = 0;
1666
1667
  this._queueingBytes = 0;
1667
1668
  this.onClose(error);
1669
+ // Reset _opened in last to allow to differenciate in onClose an error while connecting OR while connected
1670
+ // Is welcome to attempt a reconnection when no error OR when error on connection!
1671
+ this._opened = false;
1668
1672
  }
1669
1673
  _send(message) {
1670
1674
  if (!this._ws) {
@@ -1678,4 +1682,4 @@ class WebSocketReliable extends EventEmitter {
1678
1682
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1679
1683
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1680
1684
  */
1681
- const VERSION = '2.0.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map
1685
+ const VERSION = '2.1.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map