@ceeblue/web-utils 2.3.0 → 2.4.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.
@@ -184,6 +184,67 @@ declare namespace Connect {
184
184
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
185
185
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
186
186
  */
187
+ /**
188
+ * Log types
189
+ */
190
+ declare enum LogType {
191
+ ERROR = "error",
192
+ WARN = "warn",
193
+ INFO = "info",
194
+ DEBUG = "debug"
195
+ }
196
+ /**
197
+ * Log instance
198
+ */
199
+ declare class Log {
200
+ get error(): (...args: any[]) => void;
201
+ get warn(): (...args: any[]) => void;
202
+ get info(): (...args: any[]) => void;
203
+ get debug(): (...args: any[]) => void;
204
+ private _args;
205
+ private _done?;
206
+ private _onLog;
207
+ constructor(onLog: Function, ...args: unknown[]);
208
+ private _bind;
209
+ }
210
+ /**
211
+ * ILog interface used by log methods
212
+ */
213
+ interface ILog {
214
+ /**
215
+ * Build a log
216
+ */
217
+ (...args: unknown[]): Log;
218
+ /**
219
+ * Intercept or redefine any log
220
+ * @param type log level
221
+ * @param args args
222
+ * @returns
223
+ */
224
+ on: (type: LogType, args: unknown[]) => void;
225
+ }
226
+ /**
227
+ * Inherits from this class to use logs
228
+ */
229
+ declare class Loggable {
230
+ /**
231
+ * Start a log
232
+ * @param args
233
+ * @returns a Log object with the levels of log to call
234
+ */
235
+ log: ILog;
236
+ }
237
+ /**
238
+ * Global log
239
+ */
240
+ declare const log: ILog;
241
+
242
+ /**
243
+ * Copyright 2024 Ceeblue B.V.
244
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
245
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
246
+ */
247
+
187
248
  /**
188
249
  * A advanced EventEmitter which allows to declare event as natural function in the inheriting children class,
189
250
  * function must start by `on` prefix to be recognized as an event.
@@ -225,7 +286,7 @@ declare namespace Connect {
225
286
  * controller.abort();
226
287
  * logger.test(); // displays nothing
227
288
  */
228
- declare class EventEmitter {
289
+ declare class EventEmitter extends Loggable {
229
290
  private _events;
230
291
  /**
231
292
  * Build our EventEmitter, usually call from children class
@@ -280,29 +341,6 @@ declare class FixMap<KeyType, ValueType> {
280
341
  forEach(callbackfn: (value: ValueType, key: KeyType, map: Map<KeyType, ValueType>) => void, thisArg?: any): void;
281
342
  }
282
343
 
283
- /**
284
- * Copyright 2024 Ceeblue B.V.
285
- * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
286
- * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
287
- */
288
- /**
289
- * Implement this interface to throw log and error
290
- */
291
- interface ILog {
292
- /**
293
- * Call to distribute log message, default implementation is usually `onLog(log:string) { console.log(log); }`
294
- * @param log log string message
295
- * @event
296
- */
297
- onLog(log: string): void;
298
- /**
299
- * Call to distribute error message, default implementation is usually `onError(error:string) { console.error(error); }`
300
- * @param error error string message
301
- * @event
302
- */
303
- onError(error: string): void;
304
- }
305
-
306
344
  /**
307
345
  * Copyright 2024 Ceeblue B.V.
308
346
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
@@ -742,6 +780,33 @@ declare namespace Util {
742
780
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
743
781
  */
744
782
 
783
+ type WebSocketReliableError =
784
+ /**
785
+ * Represents a WebSocket disconnection error.
786
+ */
787
+ {
788
+ type: 'WebSocketReliableError';
789
+ name: 'Socket disconnection';
790
+ url: string;
791
+ reason: string;
792
+ }
793
+ /**
794
+ * Represents a server shutdown error.
795
+ */
796
+ | {
797
+ type: 'WebSocketReliableError';
798
+ name: 'Server shutdown';
799
+ url: string;
800
+ }
801
+ /**
802
+ * Represents a connection failure error.
803
+ */
804
+ | {
805
+ type: 'WebSocketReliableError';
806
+ name: 'Connection failed';
807
+ url: string;
808
+ reason: string;
809
+ };
745
810
  /**
746
811
  * The WebSocketReliable class extends WebSocket to bring up the following improvements:
747
812
  * - Fix all possible unintentional closing ways to get always a related error message, {@link onClose | onClose(error?) event}
@@ -782,7 +847,7 @@ declare class WebSocketReliable extends EventEmitter {
782
847
  * @event `close` fired on websocket close
783
848
  * @param error error description on an improper closure
784
849
  */
785
- onClose(error?: string): void;
850
+ onClose(error?: WebSocketReliableError): void;
786
851
  /**
787
852
  * binaryType, fix binary type to arrayBuffer
788
853
  */
@@ -854,8 +919,9 @@ declare class WebSocketReliable extends EventEmitter {
854
919
  /**
855
920
  * Close websocket
856
921
  * @param error the error reason if is not a proper close
922
+ * @param detail detail of the error
857
923
  */
858
- close(error?: string): void;
924
+ close(error?: WebSocketReliableError): void;
859
925
  private _send;
860
926
  }
861
927
 
@@ -867,4 +933,4 @@ declare class WebSocketReliable extends EventEmitter {
867
933
 
868
934
  declare const VERSION: string;
869
935
 
870
- export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EventEmitter, FixMap, type ILog, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable };
936
+ export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EventEmitter, FixMap, type ILog, Log, LogType, Loggable, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable, type WebSocketReliableError, log };
package/dist/web-utils.js CHANGED
@@ -987,6 +987,87 @@ function buildURL(type, params, protocol = 'wss') {
987
987
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
988
988
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
989
989
  */
990
+ let _logging = 0;
991
+ setInterval(() => {
992
+ console.assert(_logging === 0, _logging.toFixed(), 'calls to log was useless');
993
+ }, 10000);
994
+ /**
995
+ * Log types
996
+ */
997
+ var LogType;
998
+ (function (LogType) {
999
+ LogType["ERROR"] = "error";
1000
+ LogType["WARN"] = "warn";
1001
+ LogType["INFO"] = "info";
1002
+ LogType["DEBUG"] = "debug";
1003
+ })(LogType || (LogType = {}));
1004
+ /**
1005
+ * Log instance
1006
+ */
1007
+ class Log {
1008
+ get error() {
1009
+ return this._bind(LogType.ERROR);
1010
+ }
1011
+ get warn() {
1012
+ return this._bind(LogType.WARN);
1013
+ }
1014
+ get info() {
1015
+ return this._bind(LogType.INFO);
1016
+ }
1017
+ get debug() {
1018
+ return this._bind(LogType.DEBUG);
1019
+ }
1020
+ constructor(onLog, ...args) {
1021
+ if (!args.length) {
1022
+ // cannot have 0 args to be called correctly!
1023
+ args.push(undefined);
1024
+ }
1025
+ this._args = args;
1026
+ this._onLog = onLog;
1027
+ ++_logging;
1028
+ }
1029
+ _bind(type) {
1030
+ if (!this._done) {
1031
+ this._done = true;
1032
+ --_logging;
1033
+ }
1034
+ // call the local onLog
1035
+ if (this._onLog) {
1036
+ this._onLog(type, this._args);
1037
+ }
1038
+ // call the global onLog
1039
+ if (this._args.length && log.on) {
1040
+ log.on(type, this._args);
1041
+ }
1042
+ // if not intercepted display the log
1043
+ return this._args.length ? console[type].bind(console, ...this._args) : EMPTY_FUNCTION;
1044
+ }
1045
+ }
1046
+ /**
1047
+ * Inherits from this class to use logs
1048
+ */
1049
+ class Loggable {
1050
+ constructor() {
1051
+ /**
1052
+ * Start a log
1053
+ * @param args
1054
+ * @returns a Log object with the levels of log to call
1055
+ */
1056
+ this.log = ((...args) => {
1057
+ return new Log(this.log.on, ...args);
1058
+ });
1059
+ }
1060
+ }
1061
+ /**
1062
+ * Global log
1063
+ */
1064
+ const log = ((...args) => {
1065
+ return new Log(() => { }, ...args);
1066
+ });/**
1067
+ * Copyright 2024 Ceeblue B.V.
1068
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1069
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1070
+ */
990
1071
  /**
991
1072
  * A advanced EventEmitter which allows to declare event as natural function in the inheriting children class,
992
1073
  * function must start by `on` prefix to be recognized as an event.
@@ -1028,11 +1109,12 @@ function buildURL(type, params, protocol = 'wss') {
1028
1109
  * controller.abort();
1029
1110
  * logger.test(); // displays nothing
1030
1111
  */
1031
- class EventEmitter {
1112
+ class EventEmitter extends Loggable {
1032
1113
  /**
1033
1114
  * Build our EventEmitter, usually call from children class
1034
1115
  */
1035
1116
  constructor() {
1117
+ super();
1036
1118
  this._events = new Map();
1037
1119
  // Fill events with events as defined!
1038
1120
  let proto = Object.getPrototypeOf(this);
@@ -1633,11 +1715,7 @@ class WebSocketReliable extends EventEmitter {
1633
1715
  * @event `close` fired on websocket close
1634
1716
  * @param error error description on an improper closure
1635
1717
  */
1636
- onClose(error) {
1637
- if (error) {
1638
- console.error(error);
1639
- }
1640
- }
1718
+ onClose(error) { }
1641
1719
  /**
1642
1720
  * binaryType, fix binary type to arrayBuffer
1643
1721
  */
@@ -1736,18 +1814,30 @@ class WebSocketReliable extends EventEmitter {
1736
1814
  // Add details and fix close ways
1737
1815
  ws.onclose = (e) => {
1738
1816
  if (!this._opened) {
1739
- // close during connection
1740
- // the caller can differentiate this case of one server shutdown by looking if onOpen was op
1741
- this.close(url.toString() + ' connection failed, ' + String(e.reason || e.code));
1817
+ // close during connection!
1818
+ this.close({
1819
+ type: 'WebSocketReliableError',
1820
+ name: 'Connection failed',
1821
+ url: url.toString(),
1822
+ reason: String(e.reason || e.code)
1823
+ });
1742
1824
  }
1743
1825
  else if (e.code === 1000 || e.code === 1005) {
1744
- // normal disconnection from server, no error to indicate that a reconnection is possible!
1745
- // the caller can differentiate this case of one explicit websocket.close() call in the encpasulating class
1746
- this.close(url.toString() + ' shutdown');
1826
+ // normal disconnection from server
1827
+ this.close({
1828
+ type: 'WebSocketReliableError',
1829
+ name: 'Server shutdown',
1830
+ url: url.toString()
1831
+ });
1747
1832
  }
1748
1833
  else {
1749
1834
  // abnormal disconnection from server
1750
- this.close(url.toString() + ' disconnection, ' + String(e.reason || e.code));
1835
+ this.close({
1836
+ type: 'WebSocketReliableError',
1837
+ name: 'Socket disconnection',
1838
+ url: url.toString(),
1839
+ reason: String(e.reason || e.code)
1840
+ });
1751
1841
  }
1752
1842
  };
1753
1843
  // Wrap send method to queue messages until connection is established.
@@ -1792,6 +1882,7 @@ class WebSocketReliable extends EventEmitter {
1792
1882
  /**
1793
1883
  * Close websocket
1794
1884
  * @param error the error reason if is not a proper close
1885
+ * @param detail detail of the error
1795
1886
  */
1796
1887
  close(error) {
1797
1888
  if (!this._ws || this._closed) {
@@ -1803,9 +1894,11 @@ class WebSocketReliable extends EventEmitter {
1803
1894
  // release resources!
1804
1895
  this._queueing.length = 0;
1805
1896
  this._queueingBytes = 0;
1897
+ if (error) {
1898
+ this.log(error).error();
1899
+ }
1806
1900
  this.onClose(error);
1807
1901
  // Reset _opened in last to allow to differenciate in onClose an error while connecting OR while connected
1808
- // Is welcome to attempt a reconnection when no error OR when error on connection!
1809
1902
  this._opened = false;
1810
1903
  }
1811
1904
  _send(message) {
@@ -1820,4 +1913,4 @@ class WebSocketReliable extends EventEmitter {
1820
1913
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1821
1914
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1822
1915
  */
1823
- const VERSION = '2.3.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map
1916
+ const VERSION = '2.4.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,Log,LogType,Loggable,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map