@ceeblue/web-utils 1.2.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 +52 -11
- package/dist/web-utils.d.ts +84 -19
- package/dist/web-utils.js +445 -281
- 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 +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
[Usage](#usage) | [Building locally](#building-locally) | [Documentation](#documentation) | [Contribution](#contribution) | [License](#license)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# Ceeblue Web Utilities
|
|
4
4
|
|
|
5
|
-
|
|
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
|
|
|
9
|
+
Add the library as a dependency to your npm project using:
|
|
9
10
|
```bash
|
|
10
11
|
npm install @ceeblue/web-utils
|
|
11
12
|
```
|
|
13
|
+
Then [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) the library into your project, for example:
|
|
14
|
+
```javascript
|
|
15
|
+
import { Util, ILog } from '@ceeblue/web-utils';
|
|
16
|
+
```
|
|
17
|
+
> 💡 **TIP**
|
|
18
|
+
>
|
|
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
|
+
> ```json
|
|
22
|
+
> {
|
|
23
|
+
> "compilerOptions": {
|
|
24
|
+
> "target": "ES6",
|
|
25
|
+
> "moduleResolution": "Node"
|
|
26
|
+
> }
|
|
27
|
+
> }
|
|
28
|
+
> ```
|
|
29
|
+
|
|
30
|
+
## Building locally
|
|
31
|
+
|
|
32
|
+
1. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository
|
|
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
|
+
- **web-utils.d.ts** Typescript definitions file
|
|
36
|
+
- **web-utils.js**: Bundled JavaScript library
|
|
37
|
+
- **web-utils.js.map**: Source map that associates the bundled library with the original source files
|
|
38
|
+
- **web-utils.min.js** Minified version of the library, optimized for size
|
|
39
|
+
- **web-utils.min.js.map** Source map that associates the minified library with the original source files
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
git clone https://github.com/CeeblueTV/web-utils.git
|
|
43
|
+
cd web-utils
|
|
44
|
+
npm install
|
|
45
|
+
npm run build
|
|
46
|
+
```
|
|
12
47
|
|
|
13
|
-
|
|
48
|
+
## Documentation
|
|
14
49
|
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"@ceeblue/web-utils": "latest"
|
|
19
|
-
}
|
|
20
|
-
}
|
|
50
|
+
This monorepo also contains built-in documentation about the APIs in the library, which can be built using the following npm command:
|
|
21
51
|
```
|
|
52
|
+
npm run build:docs
|
|
53
|
+
```
|
|
54
|
+
You can access the documentation by opening the index.html file in the docs folder with your browser (`./docs/index.html`).
|
|
55
|
+
|
|
56
|
+
## Contribution
|
|
57
|
+
|
|
58
|
+
All contributions are welcome. Please see [our contribution guide](/CONTRIBUTING.md) for details.
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
By contributing code to this project, you agree to license your contribution under the [GNU Affero General Public License](/LICENSE).
|
package/dist/web-utils.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2024 Ceeblue B.V.
|
|
3
|
-
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
|
|
4
|
-
* See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* BinaryReader allows to read binary data
|
|
8
|
-
*/
|
|
9
1
|
declare class BinaryReader {
|
|
10
2
|
private _data;
|
|
11
3
|
private _size;
|
|
@@ -24,13 +16,14 @@ declare class BinaryReader {
|
|
|
24
16
|
read16(): number;
|
|
25
17
|
read24(): number;
|
|
26
18
|
read32(): number;
|
|
19
|
+
read64(): number;
|
|
27
20
|
readFloat(): number;
|
|
28
21
|
readDouble(): number;
|
|
29
22
|
read7Bit(bytes?: number): number;
|
|
30
23
|
readString(): string;
|
|
31
24
|
readHex(size: number): string;
|
|
32
25
|
/**
|
|
33
|
-
* Read bytes, to convert bytes to string use String.fromCharCode(...reader.read(size))
|
|
26
|
+
* Read bytes, to convert bytes to string use String.fromCharCode(...reader.read(size)) or Util.stringify
|
|
34
27
|
* @param {UInt32} size
|
|
35
28
|
*/
|
|
36
29
|
read(size?: number): Uint8Array;
|
|
@@ -56,14 +49,19 @@ declare class BinaryWriter {
|
|
|
56
49
|
size(): number;
|
|
57
50
|
next(count?: number): BinaryWriter;
|
|
58
51
|
clear(size?: number): BinaryWriter;
|
|
59
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Write binary data
|
|
54
|
+
* @param data
|
|
55
|
+
*/
|
|
56
|
+
write(data: ArrayLike<number>): BinaryWriter;
|
|
60
57
|
write8(value: number): BinaryWriter;
|
|
61
58
|
write16(value: number): BinaryWriter;
|
|
62
59
|
write24(value: number): BinaryWriter;
|
|
63
60
|
write32(value: number): BinaryWriter;
|
|
61
|
+
write64(value: number): BinaryWriter;
|
|
64
62
|
writeFloat(value: number): BinaryWriter;
|
|
65
63
|
writeDouble(value: number): BinaryWriter;
|
|
66
|
-
write7Bit(value: number
|
|
64
|
+
write7Bit(value: number): BinaryWriter;
|
|
67
65
|
writeString(value: string): BinaryWriter;
|
|
68
66
|
writeHex(value: string): BinaryWriter;
|
|
69
67
|
reserve(size: number): BinaryWriter;
|
|
@@ -94,6 +92,22 @@ declare class BitReader {
|
|
|
94
92
|
read32(): number;
|
|
95
93
|
}
|
|
96
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
|
+
|
|
97
111
|
/**
|
|
98
112
|
* Parameters of connections
|
|
99
113
|
*/
|
|
@@ -124,11 +138,11 @@ type Params = {
|
|
|
124
138
|
* Type of connection
|
|
125
139
|
*/
|
|
126
140
|
declare enum Type {
|
|
127
|
-
HESP = "
|
|
128
|
-
WEBRTS = "
|
|
129
|
-
WEBRTC = "
|
|
130
|
-
META = "
|
|
131
|
-
DATA = "
|
|
141
|
+
HESP = "HESP",
|
|
142
|
+
WEBRTS = "WebRTS",
|
|
143
|
+
WEBRTC = "WebRTC",
|
|
144
|
+
META = "Meta",
|
|
145
|
+
DATA = "Data"
|
|
132
146
|
}
|
|
133
147
|
/**
|
|
134
148
|
* Some connection utility functions
|
|
@@ -225,6 +239,32 @@ declare class EventEmitter {
|
|
|
225
239
|
private _event;
|
|
226
240
|
}
|
|
227
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
|
+
|
|
228
268
|
/**
|
|
229
269
|
* Copyright 2024 Ceeblue B.V.
|
|
230
270
|
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
|
|
@@ -593,13 +633,15 @@ declare function objectEntries(value: any): [string, any][];
|
|
|
593
633
|
* @param obj Any objects, strings, exceptions, errors, or number
|
|
594
634
|
* @param params.space `''`, allows to configure space in the string representation
|
|
595
635
|
* @param params.decimal `2`, allows to choose the number of decimal to display in the string representation
|
|
596
|
-
* @param params.recursive `false`, allows to serialize recursively every object value,
|
|
636
|
+
* @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.noBin `false`, when set skip binary encoding and write inplace a bin-length information
|
|
597
638
|
* @returns the final string representation
|
|
598
639
|
*/
|
|
599
640
|
declare function stringify(obj: any, params?: {
|
|
600
641
|
space?: string;
|
|
601
642
|
decimal?: number;
|
|
602
643
|
recursive?: number;
|
|
644
|
+
noBin?: boolean;
|
|
603
645
|
}): string;
|
|
604
646
|
/**
|
|
605
647
|
* Encode a string to a binary representation
|
|
@@ -611,18 +653,36 @@ declare function toBin(value: string): Uint8Array;
|
|
|
611
653
|
* Execute a promise in a safe way with a timeout if caller doesn't resolve it in the accurate time
|
|
612
654
|
*/
|
|
613
655
|
declare function safePromise<T>(timeout: number, promise: Promise<T>): Promise<unknown>;
|
|
656
|
+
/**
|
|
657
|
+
* Wait in milliseconds, requires a call with await keyword!
|
|
658
|
+
*/
|
|
659
|
+
declare function sleep(ms: number): Promise<unknown>;
|
|
660
|
+
/**
|
|
661
|
+
* fetch help method with few usefull fix:
|
|
662
|
+
* - throw an string exception if response code is not 200 with the text of the response or uses statusText
|
|
663
|
+
*/
|
|
664
|
+
declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
665
|
+
/**
|
|
666
|
+
* Extension parser
|
|
667
|
+
* @param path path to parse
|
|
668
|
+
* @returns the extension
|
|
669
|
+
*/
|
|
670
|
+
declare function parseExtension(path: string): string;
|
|
614
671
|
|
|
615
672
|
declare const Util_EMPTY_FUNCTION: typeof EMPTY_FUNCTION;
|
|
673
|
+
declare const Util_fetch: typeof fetch;
|
|
616
674
|
declare const Util_objectEntries: typeof objectEntries;
|
|
617
675
|
declare const Util_objectFrom: typeof objectFrom;
|
|
618
676
|
declare const Util_options: typeof options;
|
|
677
|
+
declare const Util_parseExtension: typeof parseExtension;
|
|
619
678
|
declare const Util_safePromise: typeof safePromise;
|
|
679
|
+
declare const Util_sleep: typeof sleep;
|
|
620
680
|
declare const Util_stringify: typeof stringify;
|
|
621
681
|
declare const Util_time: typeof time;
|
|
622
682
|
declare const Util_timeOrigin: typeof timeOrigin;
|
|
623
683
|
declare const Util_toBin: typeof toBin;
|
|
624
684
|
declare namespace Util {
|
|
625
|
-
export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_safePromise as safePromise, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin };
|
|
685
|
+
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 };
|
|
626
686
|
}
|
|
627
687
|
|
|
628
688
|
/**
|
|
@@ -676,6 +736,8 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
676
736
|
* binaryType, fix binary type to arrayBuffer
|
|
677
737
|
*/
|
|
678
738
|
get binaryType(): BinaryType;
|
|
739
|
+
get recvByteRate(): number;
|
|
740
|
+
get sendByteRate(): number;
|
|
679
741
|
/**
|
|
680
742
|
* url of connection
|
|
681
743
|
*/
|
|
@@ -714,6 +776,8 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
714
776
|
private _queueing;
|
|
715
777
|
private _queueingBytes;
|
|
716
778
|
private _ws?;
|
|
779
|
+
private _recvByteRate;
|
|
780
|
+
private _sendByteRate;
|
|
717
781
|
/**
|
|
718
782
|
* Create a WebSocketReliable object, and open it if an url is passed in argument
|
|
719
783
|
* @param url URL of the WebSocket endpoint or null to start the connection later
|
|
@@ -741,6 +805,7 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
741
805
|
* @param error the error reason if is not a proper close
|
|
742
806
|
*/
|
|
743
807
|
close(error?: string): void;
|
|
808
|
+
private _send;
|
|
744
809
|
}
|
|
745
810
|
|
|
746
811
|
/**
|
|
@@ -751,4 +816,4 @@ declare class WebSocketReliable extends EventEmitter {
|
|
|
751
816
|
|
|
752
817
|
declare const VERSION: string;
|
|
753
818
|
|
|
754
|
-
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 };
|