@ceeblue/web-utils 1.3.0 → 1.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Ceeblue Web Utilities
4
4
 
5
- This is a Web base compoments for CeeblueTV's web libraries : a collection of tools and utilities used across all of CeeblueTV's web projects.
5
+ This is a basic component library for Ceeblue projects, consisting of a collection of essential tools and utilities used in all Ceeblue web projects.
6
6
 
7
7
  ## Usage
8
8
 
@@ -16,8 +16,8 @@ import { Util, ILog } from '@ceeblue/web-utils';
16
16
  ```
17
17
  > 💡 **TIP**
18
18
  >
19
- > If your project uses [TypeScript](https://www.typescriptlang.org/), it is recommended to set `"target": "ES6"` in your configuration to align with our usage of ES6 features and ensures that your build will succeed (for those requiring a backwards-compatible [UMD](https://github.com/umdjs/umd) version, a [local build](#building-locally) is advised).
20
- > Then Defining the compiler option `"moduleResolution": "Node"` in **tsconfig.json** helps with import errors by ensuring that TypeScript uses the correct strategy for resolving imports based on the targeted Node.js version.
19
+ > If your project uses TypeScript, it is recommended that you set target: "ES6" in your configuration to match our use of ES6 features and ensure that your build will succeed (for those requiring a backward-compatible UMD version, a local build is recommended).
20
+ > Then define the "moduleResolution" compiler option: "Node" in tsconfig.json helps with import failures by ensuring that TypeScript uses the correct import resolution strategy based on the targeted Node.js version.
21
21
  > ```json
22
22
  > {
23
23
  > "compilerOptions": {
@@ -30,8 +30,8 @@ import { Util, ILog } from '@ceeblue/web-utils';
30
30
  ## Building locally
31
31
 
32
32
  1. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository
33
- 2. Enter the `webr-utils` folder and run `npm install` to install packages dependencies.
34
- 3. Execute `npm run build`. The output will be five files placed in the **/dist/** folder:
33
+ 2. Got to the `web-utils` folder and run `npm install` to install the packages dependencies.
34
+ 3. Run `npm run build`. The output will be five files placed in the **/dist/** folder:
35
35
  - **web-utils.d.ts** Typescript definitions file
36
36
  - **web-utils.js**: Bundled JavaScript library
37
37
  - **web-utils.js.map**: Source map that associates the bundled library with the original source files
@@ -51,10 +51,7 @@ This monorepo also contains built-in documentation about the APIs in the library
51
51
  ```
52
52
  npm run build:docs
53
53
  ```
54
- You can access the documentation by opening the index.html file in the docs folder with your browser (`./docs/index.html`), or if you have installed and started the [http-server package](https://www.npmjs.com/package/http-server) by navigating to:
55
- ```
56
- http://localhost:8080/docs/
57
- ```
54
+ You can access the documentation by opening the index.html file in the docs folder with your browser (`./docs/index.html`).
58
55
 
59
56
  ## Contribution
60
57
 
@@ -92,6 +92,22 @@ declare class BitReader {
92
92
  read32(): number;
93
93
  }
94
94
 
95
+ /**
96
+ * Compute ByteRate every delta time
97
+ */
98
+ declare class ByteRate {
99
+ onBytes(bytes: number): void;
100
+ get delta(): number;
101
+ private _bytes;
102
+ private _time;
103
+ private _delta;
104
+ private _value;
105
+ constructor(delta?: number);
106
+ value(): number;
107
+ exact(): number;
108
+ addBytes(bytes: number): this;
109
+ }
110
+
95
111
  /**
96
112
  * Parameters of connections
97
113
  */
@@ -223,6 +239,32 @@ declare class EventEmitter {
223
239
  private _event;
224
240
  }
225
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
+ /**
248
+ * Some fix for JS MAP:
249
+ * - find(key) search an item in the map and returns undefined if not found
250
+ * - get(key) return the item if exists or otherwise create and returns it
251
+ * - set(key, value) returns the value of the item (rather the MAP)
252
+ */
253
+ declare class FixMap<KeyType, ValueType> {
254
+ private _initValue;
255
+ [Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;
256
+ get size(): number;
257
+ private _map;
258
+ constructor(_initValue: () => ValueType);
259
+ get(key: KeyType): ValueType;
260
+ find(key: KeyType): ValueType | undefined;
261
+ has(key: KeyType): boolean;
262
+ clear(): void;
263
+ delete(key: KeyType): boolean;
264
+ set(key: KeyType, value: ValueType): ValueType;
265
+ forEach(callbackfn: (value: ValueType, key: KeyType, map: Map<KeyType, ValueType>) => void, thisArg?: any): void;
266
+ }
267
+
226
268
  /**
227
269
  * Copyright 2024 Ceeblue B.V.
228
270
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
@@ -774,4 +816,4 @@ declare class WebSocketReliable extends EventEmitter {
774
816
 
775
817
  declare const VERSION: string;
776
818
 
777
- export { BinaryReader, BinaryWriter, BitReader, Connect, EventEmitter, type ILog, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable };
819
+ export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EventEmitter, FixMap, type ILog, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable };
package/dist/web-utils.js CHANGED
@@ -341,130 +341,6 @@ class BitReader {
341
341
  read32() {
342
342
  return this.read(32);
343
343
  }
344
- }/**
345
- * Copyright 2024 Ceeblue B.V.
346
- * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
347
- * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
348
- */
349
- /**
350
- * Help class to manipulate and parse a net address. The Address can be only the domain field,
351
- * or a URL format with protocol and path part `(http://)domain(:port/path)`
352
- * @example
353
- * const address = new Address('nl-ams-42.live.ceeblue.tv:80');
354
- * console.log(address.domain) // 'nl-ams-42.live.ceeblue.tv'
355
- * console.log(address.port) // '80'
356
- * console.log(address) // 'nl-ams-42.live.ceeblue.tv:80'
357
- */
358
- class NetAddress {
359
- /**
360
- * Static help function to build an end point from an address `(proto://)domain(:port/path)`
361
- *
362
- * Mainly it fix the protocol, in addition if:
363
- * - the address passed is securized (TLS) and protocol is not => it tries to fix protocol to get its securize version
364
- * - the address passed is non securized and protocol is (TLS) => it tries to fix protocol to get its unsecurized version
365
- * @param protocol protocol to set in the end point returned
366
- * @param address string address to fix with protocol as indicated
367
- * @returns the end point built
368
- * @example
369
- * console.log(NetAddress.fixProtocol('ws','http://domain/path')) // 'ws://domain/path'
370
- * console.log(NetAddress.fixProtocol('ws','https://domain/path')) // 'wss://domain/path'
371
- * console.log(NetAddress.fixProtocol('wss','http://domain/path')) // 'ws://domain/path'
372
- */
373
- static fixProtocol(protocol, address) {
374
- const found = address.indexOf('://');
375
- // isolate protocol is present in address
376
- if (found >= 0) {
377
- // In this case replace by protocol in keeping SSL like given in address
378
- if (found > 2 && address.charAt(found - 1).toLowerCase() === 's') {
379
- // SSL!
380
- if (protocol.length <= 2 || !protocol.endsWith('s')) {
381
- protocol += 's'; // Add SSL
382
- }
383
- }
384
- else {
385
- // Not SSL!
386
- if (protocol.length > 2 && protocol.endsWith('s')) {
387
- protocol = protocol.slice(0, -1); // Remove SSL
388
- }
389
- }
390
- // Build host!
391
- address = address.substring(found + 3);
392
- }
393
- return protocol + '://' + address;
394
- }
395
- /**
396
- * The domain part from address `(http://)domain(:port/path)`
397
- */
398
- get domain() {
399
- return this._domain;
400
- }
401
- /**
402
- * The port part from address `(http://)domain(:port/path)`, or defaultPort if passed in NetAddress constructor
403
- */
404
- get port() {
405
- return this._port;
406
- }
407
- /**
408
- * @returns the string address as passed in the constructor
409
- */
410
- toString() {
411
- return this._address;
412
- }
413
- /**
414
- * @returns the string address as passed in the constructor
415
- * @override
416
- */
417
- valueOf() {
418
- return this._address;
419
- }
420
- /**
421
- * Build a NetAddress object and parse address
422
- * @param address string address to parse, accept an url format with protocol and path `(http://)domain(:port/path)`
423
- * @param defaultPort set a default port to use if there is no port in the string address parsed
424
- */
425
- constructor(address, defaultPort) {
426
- this._address = address;
427
- // Remove Protocol
428
- let pos = address.indexOf('/');
429
- if (pos >= 0) {
430
- // Remove ://
431
- if (address.charCodeAt(pos + 1) === 47) {
432
- // has //
433
- if (pos > 0) {
434
- if (address.charCodeAt(pos - 1) === 58) {
435
- // has ://
436
- address = address.substring(pos + 2);
437
- } // something else #//
438
- }
439
- else {
440
- // otherwise starts by //
441
- address = address.substring(2);
442
- }
443
- }
444
- else if (!pos) {
445
- // starts by /, remove it
446
- address = address.substring(1);
447
- } // else something else #/
448
- }
449
- this._domain = address;
450
- this._port = defaultPort;
451
- // Parse Port
452
- pos = address.lastIndexOf(':');
453
- if (pos >= 0) {
454
- const port = parseInt(address.substring(pos + 1));
455
- if (port && port <= 0xffff) {
456
- this._port = port;
457
- this._domain = address.substring(0, pos);
458
- }
459
- }
460
- else {
461
- // Remove Path!
462
- pos = address.indexOf('/');
463
- if (pos >= 0) {
464
- this._domain = address.substring(0, pos);
465
- }
466
- }
467
- }
468
344
  }/******************************************************************************
469
345
  Copyright (c) Microsoft Corporation.
470
346
 
@@ -736,6 +612,168 @@ function parseExtension(path) {
736
612
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
737
613
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
738
614
  */
615
+ /**
616
+ * Compute ByteRate every delta time
617
+ */
618
+ class ByteRate {
619
+ onBytes(bytes) { }
620
+ get delta() {
621
+ return this._delta;
622
+ }
623
+ constructor(delta = 1000) {
624
+ this._time = time();
625
+ this._value = NaN;
626
+ this._delta = delta;
627
+ this._bytes = 0;
628
+ }
629
+ value() {
630
+ return Math.round(this.exact());
631
+ }
632
+ exact() {
633
+ const now = time();
634
+ const elapsed = now - this._time;
635
+ if (elapsed > this._delta || isNaN(this._value)) {
636
+ // wait "_delta" before next compute rate
637
+ this._value = (this._bytes * 1000) / elapsed;
638
+ this._bytes = 0;
639
+ this._time = now;
640
+ }
641
+ return this._value;
642
+ }
643
+ addBytes(bytes) {
644
+ this._bytes += bytes;
645
+ this.onBytes(bytes);
646
+ return this;
647
+ }
648
+ }/**
649
+ * Copyright 2024 Ceeblue B.V.
650
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
651
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
652
+ */
653
+ /**
654
+ * Help class to manipulate and parse a net address. The Address can be only the domain field,
655
+ * or a URL format with protocol and path part `(http://)domain(:port/path)`
656
+ * @example
657
+ * const address = new Address('nl-ams-42.live.ceeblue.tv:80');
658
+ * console.log(address.domain) // 'nl-ams-42.live.ceeblue.tv'
659
+ * console.log(address.port) // '80'
660
+ * console.log(address) // 'nl-ams-42.live.ceeblue.tv:80'
661
+ */
662
+ class NetAddress {
663
+ /**
664
+ * Static help function to build an end point from an address `(proto://)domain(:port/path)`
665
+ *
666
+ * Mainly it fix the protocol, in addition if:
667
+ * - the address passed is securized (TLS) and protocol is not => it tries to fix protocol to get its securize version
668
+ * - the address passed is non securized and protocol is (TLS) => it tries to fix protocol to get its unsecurized version
669
+ * @param protocol protocol to set in the end point returned
670
+ * @param address string address to fix with protocol as indicated
671
+ * @returns the end point built
672
+ * @example
673
+ * console.log(NetAddress.fixProtocol('ws','http://domain/path')) // 'ws://domain/path'
674
+ * console.log(NetAddress.fixProtocol('ws','https://domain/path')) // 'wss://domain/path'
675
+ * console.log(NetAddress.fixProtocol('wss','http://domain/path')) // 'ws://domain/path'
676
+ */
677
+ static fixProtocol(protocol, address) {
678
+ const found = address.indexOf('://');
679
+ // isolate protocol is present in address
680
+ if (found >= 0) {
681
+ // In this case replace by protocol in keeping SSL like given in address
682
+ if (found > 2 && address.charAt(found - 1).toLowerCase() === 's') {
683
+ // SSL!
684
+ if (protocol.length <= 2 || !protocol.endsWith('s')) {
685
+ protocol += 's'; // Add SSL
686
+ }
687
+ }
688
+ else {
689
+ // Not SSL!
690
+ if (protocol.length > 2 && protocol.endsWith('s')) {
691
+ protocol = protocol.slice(0, -1); // Remove SSL
692
+ }
693
+ }
694
+ // Build host!
695
+ address = address.substring(found + 3);
696
+ }
697
+ return protocol + '://' + address;
698
+ }
699
+ /**
700
+ * The domain part from address `(http://)domain(:port/path)`
701
+ */
702
+ get domain() {
703
+ return this._domain;
704
+ }
705
+ /**
706
+ * The port part from address `(http://)domain(:port/path)`, or defaultPort if passed in NetAddress constructor
707
+ */
708
+ get port() {
709
+ return this._port;
710
+ }
711
+ /**
712
+ * @returns the string address as passed in the constructor
713
+ */
714
+ toString() {
715
+ return this._address;
716
+ }
717
+ /**
718
+ * @returns the string address as passed in the constructor
719
+ * @override
720
+ */
721
+ valueOf() {
722
+ return this._address;
723
+ }
724
+ /**
725
+ * Build a NetAddress object and parse address
726
+ * @param address string address to parse, accept an url format with protocol and path `(http://)domain(:port/path)`
727
+ * @param defaultPort set a default port to use if there is no port in the string address parsed
728
+ */
729
+ constructor(address, defaultPort) {
730
+ this._address = address;
731
+ // Remove Protocol
732
+ let pos = address.indexOf('/');
733
+ if (pos >= 0) {
734
+ // Remove ://
735
+ if (address.charCodeAt(pos + 1) === 47) {
736
+ // has //
737
+ if (pos > 0) {
738
+ if (address.charCodeAt(pos - 1) === 58) {
739
+ // has ://
740
+ address = address.substring(pos + 2);
741
+ } // something else #//
742
+ }
743
+ else {
744
+ // otherwise starts by //
745
+ address = address.substring(2);
746
+ }
747
+ }
748
+ else if (!pos) {
749
+ // starts by /, remove it
750
+ address = address.substring(1);
751
+ } // else something else #/
752
+ }
753
+ this._domain = address;
754
+ this._port = defaultPort;
755
+ // Parse Port
756
+ pos = address.lastIndexOf(':');
757
+ if (pos >= 0) {
758
+ const port = parseInt(address.substring(pos + 1));
759
+ if (port && port <= 0xffff) {
760
+ this._port = port;
761
+ this._domain = address.substring(0, pos);
762
+ }
763
+ }
764
+ else {
765
+ // Remove Path!
766
+ pos = address.indexOf('/');
767
+ if (pos >= 0) {
768
+ this._domain = address.substring(0, pos);
769
+ }
770
+ }
771
+ }
772
+ }/**
773
+ * Copyright 2024 Ceeblue B.V.
774
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
775
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
776
+ */
739
777
  /**
740
778
  * Type of connection
741
779
  */
@@ -936,6 +974,55 @@ class EventEmitter {
936
974
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
937
975
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
938
976
  */
977
+ /**
978
+ * Some fix for JS MAP:
979
+ * - find(key) search an item in the map and returns undefined if not found
980
+ * - get(key) return the item if exists or otherwise create and returns it
981
+ * - set(key, value) returns the value of the item (rather the MAP)
982
+ */
983
+ class FixMap {
984
+ [Symbol.iterator]() {
985
+ return this._map[Symbol.iterator]();
986
+ }
987
+ get size() {
988
+ return this._map.size;
989
+ }
990
+ constructor(_initValue) {
991
+ this._initValue = _initValue;
992
+ this._map = new Map();
993
+ }
994
+ get(key) {
995
+ let value = this.find(key);
996
+ if (value === undefined) {
997
+ this._map.set(key, (value = this._initValue()));
998
+ }
999
+ return value;
1000
+ }
1001
+ find(key) {
1002
+ return this._map.get(key);
1003
+ }
1004
+ has(key) {
1005
+ return this._map.has(key);
1006
+ }
1007
+ clear() {
1008
+ this._map.clear();
1009
+ }
1010
+ delete(key) {
1011
+ return this._map.delete(key);
1012
+ }
1013
+ set(key, value) {
1014
+ this._map.set(key, value);
1015
+ return value;
1016
+ }
1017
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1018
+ forEach(callbackfn, thisArg) {
1019
+ this._map.forEach(callbackfn, thisArg);
1020
+ }
1021
+ }/**
1022
+ * Copyright 2024 Ceeblue B.V.
1023
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1024
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1025
+ */
939
1026
  /**
940
1027
  * Queue typed similar to a {@link https://en.cppreference.com/w/cpp/container/queue | std::queue<Type>} with possibility to limit the capacity like a FIFO
941
1028
  * @example
@@ -1353,44 +1440,6 @@ Object.freeze(SDP);/**
1353
1440
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1354
1441
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1355
1442
  */
1356
- /**
1357
- * Compute ByteRate every delta time
1358
- */
1359
- class ByteRate {
1360
- onBytes(bytes) { }
1361
- get delta() {
1362
- return this._delta;
1363
- }
1364
- constructor(delta = 1000) {
1365
- this._time = time();
1366
- this._value = NaN;
1367
- this._delta = delta;
1368
- this._bytes = 0;
1369
- }
1370
- value() {
1371
- return Math.round(this.exact());
1372
- }
1373
- exact() {
1374
- const now = time();
1375
- const elapsed = now - this._time;
1376
- if (elapsed > this._delta || isNaN(this._value)) {
1377
- // wait "_delta" before next compute rate
1378
- this._value = (this._bytes * 1000) / elapsed;
1379
- this._bytes = 0;
1380
- this._time = now;
1381
- }
1382
- return this._value;
1383
- }
1384
- addBytes(bytes) {
1385
- this._bytes += bytes;
1386
- this.onBytes(bytes);
1387
- return this;
1388
- }
1389
- }/**
1390
- * Copyright 2024 Ceeblue B.V.
1391
- * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1392
- * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1393
- */
1394
1443
  /**
1395
1444
  * The WebSocketReliable class extends WebSocket to bring up the following improvements:
1396
1445
  * - Fix all possible unintentional closing ways to get always a related error message, {@link onClose | onClose(error?) event}
@@ -1614,4 +1663,4 @@ class WebSocketReliable extends EventEmitter {
1614
1663
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1615
1664
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1616
1665
  */
1617
- const VERSION = '1.3.0';export{BinaryReader,BinaryWriter,BitReader,Connect,EventEmitter,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map
1666
+ const VERSION = '1.4.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EventEmitter,FixMap,NetAddress,Numbers,Queue,SDP,Util,VERSION,WebSocketReliable};//# sourceMappingURL=web-utils.js.map