@ceeblue/web-utils 2.3.0 → 2.4.1
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.
- package/dist/web-utils.d.ts +98 -30
- package/dist/web-utils.js +113 -14
- package/dist/web-utils.js.map +1 -1
- package/dist/web-utils.min.js +1 -1
- package/dist/web-utils.min.js.map +1 -1
- package/package.json +7 -6
package/dist/web-utils.d.ts
CHANGED
|
@@ -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}
|
|
@@ -768,21 +833,24 @@ declare namespace Util {
|
|
|
768
833
|
*/
|
|
769
834
|
declare class WebSocketReliable extends EventEmitter {
|
|
770
835
|
/**
|
|
771
|
-
*
|
|
836
|
+
* Fired when socket is connected
|
|
837
|
+
* @event
|
|
772
838
|
*/
|
|
773
839
|
onOpen(): void;
|
|
774
840
|
/**
|
|
775
|
-
*
|
|
841
|
+
* Fired on message reception
|
|
776
842
|
* @param message can be binary or string.
|
|
777
843
|
* If you subscribe to the event with message as string type (and not union),
|
|
778
844
|
* it means that you know that all your messages are distributed in a string format
|
|
845
|
+
* @event
|
|
779
846
|
*/
|
|
780
847
|
onMessage(message: ArrayBuffer | string): void;
|
|
781
848
|
/**
|
|
782
|
-
*
|
|
849
|
+
* Fired on websocket close
|
|
783
850
|
* @param error error description on an improper closure
|
|
851
|
+
* @event
|
|
784
852
|
*/
|
|
785
|
-
onClose(error?:
|
|
853
|
+
onClose(error?: WebSocketReliableError): void;
|
|
786
854
|
/**
|
|
787
855
|
* binaryType, fix binary type to arrayBuffer
|
|
788
856
|
*/
|
|
@@ -855,7 +923,7 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
855
923
|
* Close websocket
|
|
856
924
|
* @param error the error reason if is not a proper close
|
|
857
925
|
*/
|
|
858
|
-
close(error?:
|
|
926
|
+
close(error?: WebSocketReliableError): void;
|
|
859
927
|
private _send;
|
|
860
928
|
}
|
|
861
929
|
|
|
@@ -867,4 +935,4 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
867
935
|
|
|
868
936
|
declare const VERSION: string;
|
|
869
937
|
|
|
870
|
-
export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EventEmitter, FixMap, type ILog, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable };
|
|
938
|
+
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);
|
|
@@ -1619,23 +1701,29 @@ Object.freeze(SDP);/**
|
|
|
1619
1701
|
*/
|
|
1620
1702
|
class WebSocketReliable extends EventEmitter {
|
|
1621
1703
|
/**
|
|
1622
|
-
*
|
|
1704
|
+
* Fired when socket is connected
|
|
1705
|
+
* @event
|
|
1623
1706
|
*/
|
|
1624
1707
|
onOpen() { }
|
|
1625
1708
|
/**
|
|
1626
|
-
*
|
|
1709
|
+
* Fired on message reception
|
|
1627
1710
|
* @param message can be binary or string.
|
|
1628
1711
|
* If you subscribe to the event with message as string type (and not union),
|
|
1629
1712
|
* it means that you know that all your messages are distributed in a string format
|
|
1713
|
+
* @event
|
|
1630
1714
|
*/
|
|
1631
1715
|
onMessage(message) { }
|
|
1632
1716
|
/**
|
|
1633
|
-
*
|
|
1717
|
+
* Fired on websocket close
|
|
1634
1718
|
* @param error error description on an improper closure
|
|
1719
|
+
* @event
|
|
1635
1720
|
*/
|
|
1636
1721
|
onClose(error) {
|
|
1637
1722
|
if (error) {
|
|
1638
|
-
|
|
1723
|
+
this.log('onClose', error).error();
|
|
1724
|
+
}
|
|
1725
|
+
else {
|
|
1726
|
+
this.log('onClose').info();
|
|
1639
1727
|
}
|
|
1640
1728
|
}
|
|
1641
1729
|
/**
|
|
@@ -1736,18 +1824,30 @@ class WebSocketReliable extends EventEmitter {
|
|
|
1736
1824
|
// Add details and fix close ways
|
|
1737
1825
|
ws.onclose = (e) => {
|
|
1738
1826
|
if (!this._opened) {
|
|
1739
|
-
// close during connection
|
|
1740
|
-
|
|
1741
|
-
|
|
1827
|
+
// close during connection!
|
|
1828
|
+
this.close({
|
|
1829
|
+
type: 'WebSocketReliableError',
|
|
1830
|
+
name: 'Connection failed',
|
|
1831
|
+
url: url.toString(),
|
|
1832
|
+
reason: String(e.reason || e.code)
|
|
1833
|
+
});
|
|
1742
1834
|
}
|
|
1743
1835
|
else if (e.code === 1000 || e.code === 1005) {
|
|
1744
|
-
// normal disconnection from server
|
|
1745
|
-
|
|
1746
|
-
|
|
1836
|
+
// normal disconnection from server
|
|
1837
|
+
this.close({
|
|
1838
|
+
type: 'WebSocketReliableError',
|
|
1839
|
+
name: 'Server shutdown',
|
|
1840
|
+
url: url.toString()
|
|
1841
|
+
});
|
|
1747
1842
|
}
|
|
1748
1843
|
else {
|
|
1749
1844
|
// abnormal disconnection from server
|
|
1750
|
-
this.close(
|
|
1845
|
+
this.close({
|
|
1846
|
+
type: 'WebSocketReliableError',
|
|
1847
|
+
name: 'Socket disconnection',
|
|
1848
|
+
url: url.toString(),
|
|
1849
|
+
reason: String(e.reason || e.code)
|
|
1850
|
+
});
|
|
1751
1851
|
}
|
|
1752
1852
|
};
|
|
1753
1853
|
// Wrap send method to queue messages until connection is established.
|
|
@@ -1805,7 +1905,6 @@ class WebSocketReliable extends EventEmitter {
|
|
|
1805
1905
|
this._queueingBytes = 0;
|
|
1806
1906
|
this.onClose(error);
|
|
1807
1907
|
// 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
1908
|
this._opened = false;
|
|
1810
1909
|
}
|
|
1811
1910
|
_send(message) {
|
|
@@ -1820,4 +1919,4 @@ class WebSocketReliable extends EventEmitter {
|
|
|
1820
1919
|
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
|
|
1821
1920
|
* See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
|
|
1822
1921
|
*/
|
|
1823
|
-
const VERSION = '2.
|
|
1922
|
+
const VERSION = '2.4.1';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,Log,LogType,Loggable,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map
|