@ceeblue/web-utils 1.0.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.
@@ -0,0 +1,731 @@
1
+ /**
2
+ * Copyright 2023 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
+ declare class BinaryReader {
10
+ private _data;
11
+ private _size;
12
+ private _position;
13
+ private _view;
14
+ constructor(data: BufferSource);
15
+ data(): Uint8Array;
16
+ size(): number;
17
+ available(): number;
18
+ value(position?: number): number;
19
+ position(): number;
20
+ reset(position?: number): void;
21
+ shrink(available: number): number;
22
+ next(count?: number): number;
23
+ read8(): number;
24
+ read16(): number;
25
+ read24(): number;
26
+ read32(): number;
27
+ readFloat(): number;
28
+ readDouble(): number;
29
+ read7Bit(bytes?: number): number;
30
+ readString(): string;
31
+ readHex(size: number): string;
32
+ /**
33
+ * Read bytes, to convert bytes to string use String.fromCharCode(...reader.read(size))
34
+ * @param {UInt32} size
35
+ */
36
+ read(size?: number): Uint8Array;
37
+ }
38
+
39
+ /**
40
+ * Copyright 2023 Ceeblue B.V.
41
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
42
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
43
+ */
44
+ /**
45
+ * BinaryWriter allows to write data in its binary form
46
+ */
47
+ declare class BinaryWriter {
48
+ get view(): DataView;
49
+ get capacity(): number;
50
+ private _data;
51
+ private _size;
52
+ private _view?;
53
+ private _isConst?;
54
+ constructor(dataOrSize?: BufferSource | number, offset?: number, length?: number);
55
+ data(): Uint8Array;
56
+ size(): number;
57
+ next(count?: number): BinaryWriter;
58
+ clear(size?: number): BinaryWriter;
59
+ write(data: string | ArrayLike<number>): BinaryWriter;
60
+ write8(value: number): BinaryWriter;
61
+ write16(value: number): BinaryWriter;
62
+ write24(value: number): BinaryWriter;
63
+ write32(value: number): BinaryWriter;
64
+ writeFloat(value: number): BinaryWriter;
65
+ writeDouble(value: number): BinaryWriter;
66
+ write7Bit(value: number, bytes?: number): BinaryWriter;
67
+ writeString(value: string): BinaryWriter;
68
+ writeHex(value: string): BinaryWriter;
69
+ reserve(size: number): BinaryWriter;
70
+ }
71
+
72
+ /**
73
+ * Copyright 2023 Ceeblue B.V.
74
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
75
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
76
+ */
77
+ declare class BitReader {
78
+ private _data;
79
+ private _size;
80
+ private _position;
81
+ private _bit;
82
+ constructor(data: BufferSource);
83
+ data(): Uint8Array;
84
+ size(): number;
85
+ available(): number;
86
+ next(count?: number): number;
87
+ read(count?: number): number;
88
+ read8(): number;
89
+ read16(): number;
90
+ read24(): number;
91
+ read32(): number;
92
+ }
93
+
94
+ /**
95
+ * Parameters of connections
96
+ */
97
+ type Params = {
98
+ /**
99
+ * Host to connect. Can include port, and accept also an url format with port and path,
100
+ * it can help to force a path OR try to give a protocol preference
101
+ */
102
+ host: string;
103
+ /**
104
+ * The name of the stream to join
105
+ */
106
+ streamName: string;
107
+ /**
108
+ * Optional access token to use to join a private stream
109
+ */
110
+ accessToken?: string;
111
+ /**
112
+ * iceServer to use while connecting to a WebRTC stream
113
+ */
114
+ iceServer?: RTCIceServer;
115
+ /**
116
+ * Optional query to add into the generated url of connection
117
+ */
118
+ query?: Record<string, string>;
119
+ };
120
+ /**
121
+ * Type of connection
122
+ */
123
+ declare enum Type {
124
+ HESP = "hesp",
125
+ WEBRTS = "webrts",
126
+ WEBRTC = "webrtc",
127
+ META = "meta",
128
+ DATA = "data"
129
+ }
130
+ /**
131
+ * Some connection utility functions
132
+ */
133
+ /**
134
+ * Build an URL from {@link Type | type} and {@link Params | params}
135
+ * @param type Type of the connection wanted
136
+ * @param params Connection parameters
137
+ * @param protocol Optional parameter to choose the prefered protocol to connect
138
+ * @returns The URL of connection
139
+ */
140
+ declare function buildURL(type: Type, params: Params, protocol?: string): URL;
141
+
142
+ type Connect_Params = Params;
143
+ type Connect_Type = Type;
144
+ declare const Connect_Type: typeof Type;
145
+ declare const Connect_buildURL: typeof buildURL;
146
+ declare namespace Connect {
147
+ export { type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL };
148
+ }
149
+
150
+ /**
151
+ * Copyright 2023 Ceeblue B.V.
152
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
153
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
154
+ */
155
+ /**
156
+ * A advanced EventEmitter which allows to declare event as natural function in the inheriting children class,
157
+ * function must start by `on` prefix to be recognized as an event.
158
+ * The function can define a behavior by default, and user can choose to redefine this behavior,
159
+ * or add an additionnal subscription for this event.
160
+ * In addition you can unsubscribe to multiple events with an `AbortController`
161
+ * @example
162
+ * class Logger extends EventEmitter {
163
+ * onLog(log:string) { console.log(log); } // behavior by default
164
+ *
165
+ * test() {
166
+ * // raise event onLog
167
+ * this.onLog('test');
168
+ * }
169
+ * }
170
+ *
171
+ * const logger = new Logger();
172
+ * logger.test(); // displays a log 'test'
173
+ *
174
+ * // redefine default behavior to display nothing
175
+ * logger.onLog = () => {}
176
+ * logger.test(); // displays nothing
177
+ *
178
+ * // add an additionnal subscription
179
+ * logger.on('log', console.log);
180
+ * logger.test(); // displays a log 'test'
181
+ *
182
+ * // remove the additionnal subscription
183
+ * logger.off('log', console.log);
184
+ * logger.test(); // displays nothing
185
+ *
186
+ * // add two additionnal subscriptions with a AbortController
187
+ * const controller = new AbortController();
188
+ * logger.on('log', log => console.log(log), controller);
189
+ * logger.on('log', log => console.error(log), controller);
190
+ * logger.test(); // displays a log 'test' + an error 'test'
191
+ *
192
+ * // Unsubscribe all the subscription with the AbortController
193
+ * controller.abort();
194
+ * logger.test(); // displays nothing
195
+ */
196
+ declare class EventEmitter {
197
+ private _events;
198
+ /**
199
+ * Build our EventEmitter, usually call from children class
200
+ */
201
+ constructor();
202
+ /**
203
+ * Event subscription
204
+ * @param name Name of event without the `on` prefix (ex: `log` to `onLog` event declared)
205
+ * @param event Subscriber Function
206
+ * @param abort Optional `AbortController` to stop this or multiple subscriptions in same time
207
+ */
208
+ on(name: string, event: Function, abort?: AbortController): void;
209
+ /**
210
+ * Event subscription only one time, once time fired it's automatically unsubscribe
211
+ * @param name Name of event without the `on` prefix (ex: `log` to `onLog` event declared)
212
+ * @param event Subscriber Function
213
+ * @param abort Optional `AbortController` to stop this or multiple subscriptions in same time
214
+ */
215
+ once(name: string, event: Function, abort?: AbortController): void;
216
+ /**
217
+ * Event unsubscription
218
+ * @param name Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)
219
+ * @param event Unsubscriber Function, must be the one passed to {@link on} or {@link once} subscription methods
220
+ */
221
+ off(name: string, event: Function): void;
222
+ private _event;
223
+ }
224
+
225
+ /**
226
+ * Copyright 2023 Ceeblue B.V.
227
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
228
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
229
+ */
230
+ /**
231
+ * Implement this interface to throw log and error
232
+ */
233
+ interface ILog {
234
+ /**
235
+ * Call to distribute log message, default implementation is usually `onLog(log:string) { console.log(log); }`
236
+ * @param log log string message
237
+ * @event
238
+ */
239
+ onLog(log: string): void;
240
+ /**
241
+ * Call to distribute error message, default implementation is usually `onError(error:string) { console.error(error); }`
242
+ * @param error error string message
243
+ * @event
244
+ */
245
+ onError(error: string): void;
246
+ }
247
+
248
+ /**
249
+ * Copyright 2023 Ceeblue B.V.
250
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
251
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
252
+ */
253
+ /**
254
+ * Help class to manipulate and parse a net address. The Address can be only the domain field,
255
+ * or a URL format with protocol and path part `(http://)domain(:port/path)`
256
+ * @example
257
+ * const address = new Address('nl-ams-42.live.ceeblue.tv:80');
258
+ * console.log(address.domain) // 'nl-ams-42.live.ceeblue.tv'
259
+ * console.log(address.port) // '80'
260
+ * console.log(address) // 'nl-ams-42.live.ceeblue.tv:80'
261
+ */
262
+ declare class NetAddress {
263
+ /**
264
+ * Static help function to build an end point from an address `(proto://)domain(:port/path)`
265
+ *
266
+ * Mainly it fix the protocol, in addition if:
267
+ * - the address passed is securized (TLS) and protocol is not => it tries to fix protocol to get its securize version
268
+ * - the address passed is non securized and protocol is (TLS) => it tries to fix protocol to get its unsecurized version
269
+ * @param protocol protocol to set in the end point returned
270
+ * @param address string address to fix with protocol as indicated
271
+ * @returns the end point built
272
+ * @example
273
+ * console.log(NetAddress.fixProtocol('ws','http://domain/path')) // 'ws://domain/path'
274
+ * console.log(NetAddress.fixProtocol('ws','https://domain/path')) // 'wss://domain/path'
275
+ * console.log(NetAddress.fixProtocol('wss','http://domain/path')) // 'ws://domain/path'
276
+ */
277
+ static fixProtocol(protocol: string, address: string): string;
278
+ /**
279
+ * The domain part from address `(http://)domain(:port/path)`
280
+ */
281
+ get domain(): string;
282
+ /**
283
+ * The port part from address `(http://)domain(:port/path)`, or defaultPort if passed in NetAddress constructor
284
+ */
285
+ get port(): number | undefined;
286
+ /**
287
+ * @returns the string address as passed in the constructor
288
+ */
289
+ toString(): string;
290
+ /**
291
+ * @returns the string address as passed in the constructor
292
+ * @override
293
+ */
294
+ valueOf(): string;
295
+ private _address;
296
+ private _domain;
297
+ private _port?;
298
+ /**
299
+ * Build a NetAddress object and parse address
300
+ * @param address string address to parse, accept an url format with protocol and path `(http://)domain(:port/path)`
301
+ * @param defaultPort set a default port to use if there is no port in the string address parsed
302
+ */
303
+ constructor(address: string, defaultPort?: number);
304
+ }
305
+
306
+ /**
307
+ * Copyright 2023 Ceeblue B.V.
308
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
309
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
310
+ */
311
+ /**
312
+ * 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
313
+ * @example
314
+ * const queue = new Queue<number>(2);
315
+ * queue.push(1); // [1]
316
+ * queue.push(2); // [1,2]
317
+ * queue.push(3); // [2,3] 1 has been removed to respect 2 capacity
318
+ */
319
+ declare class Queue<Type> {
320
+ /**
321
+ * Number of element in the queue
322
+ */
323
+ get size(): number;
324
+ /**
325
+ * Maximum capacity for the queue, if not set queue has unlimited capacity
326
+ */
327
+ get capacity(): number | undefined;
328
+ /**
329
+ * Set a maximum capacity for the queue,
330
+ * if you push new value exceding this capacity the firsts are removed (FIFO)
331
+ * if set to undefined the queue is unlimited
332
+ */
333
+ set capacity(value: number | undefined);
334
+ /**
335
+ * The front element
336
+ */
337
+ get front(): Type;
338
+ /**
339
+ * The back element
340
+ */
341
+ get back(): Type;
342
+ /**
343
+ * Iterator though queue's elements
344
+ */
345
+ [Symbol.iterator](): IterableIterator<Type>;
346
+ private _capacity?;
347
+ private _queue;
348
+ /**
349
+ * Instanciate a new queue object with the type passed as template
350
+ * @param capacity if set it limits the size of the queue, any exceding element pops the first element pushed (FIFO)
351
+ */
352
+ constructor(capacity?: number);
353
+ /**
354
+ * Push a new element in the queue
355
+ * @param value value of the element
356
+ * @returns this
357
+ */
358
+ push(value: Type): Queue<Type>;
359
+ /**
360
+ * Pop the first element from the queue
361
+ * @returns The first element removed
362
+ */
363
+ pop(): Type | undefined;
364
+ /**
365
+ * Clear all the elements, queue becomes empty
366
+ * @returns this
367
+ */
368
+ clear(): Queue<Type>;
369
+ }
370
+
371
+ /**
372
+ * Copyright 2023 Ceeblue B.V.
373
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
374
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
375
+ */
376
+
377
+ /**
378
+ * A collection of number Queue<number> with the following efficient mathematic computation:
379
+ * - minimum value in the collection
380
+ * - maximum value in the collection
381
+ * - average value of the collection
382
+ */
383
+ declare class Numbers extends Queue<number> {
384
+ /**
385
+ * minimum value in the collection, or 0 if colleciton is empty
386
+ */
387
+ get minimum(): number;
388
+ /**
389
+ * maximum value in the collection, or 0 if colleciton is empty
390
+ */
391
+ get maximum(): number;
392
+ /**
393
+ * average value of the collection, or 0 if collection if empty
394
+ */
395
+ get average(): number;
396
+ private _average?;
397
+ private _sum;
398
+ private _min;
399
+ private _max;
400
+ /**
401
+ * Instantiate the collection of the number
402
+ * @param capacity if set it limits the number of values stored, any exceding number pops the first number pushed (FIFO)
403
+ */
404
+ constructor(capacity?: number);
405
+ /**
406
+ * Push a value to the back to the collection
407
+ * @param value number to add
408
+ * @returns this
409
+ */
410
+ push(value: number): Numbers;
411
+ /**
412
+ * Pop the front number from the collection
413
+ * @returns the front number removed
414
+ */
415
+ pop(): number | undefined;
416
+ /**
417
+ * Clear all the numbers, collection becomes empty
418
+ * @returns this
419
+ */
420
+ clear(): this;
421
+ }
422
+
423
+ /**
424
+ * Copyright 2023 Ceeblue B.V.
425
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
426
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
427
+ */
428
+ /**
429
+ * Toolkit to deal with Session Description Protocol (SDP),
430
+ * mainly to tranform a SDP string Offer/Answer to a manipulable JS object representation
431
+ * @example
432
+ * const peerConnection = new RTCPeerConnection();
433
+ * peerConnection.createOffer()
434
+ * .then( offer =>
435
+ * // Change offer.sdp string to a JS manipulable object
436
+ * const sdp = SDP.fromString(offer.sdp || '');
437
+ * // Change a property of SDP
438
+ * sdp.v = 2; // change SDP version
439
+ * // Reserialize to the legal SDP string format
440
+ * offer.sdp = SDP.toString(sdp);
441
+ * // Set SDP offer to peerConnection
442
+ * peerConnection.setLocalDescription(offer);
443
+ * )
444
+ */
445
+ declare const SDP: {
446
+ /**
447
+ * Unserialize SDP string to a manipulable JS object representation
448
+ * It's an object with:
449
+ * - root SDP properties
450
+ * - media description iterable
451
+ * @param lines SDP string reprensentation
452
+ * @returns SDP object representation, iterable through media description
453
+ * @example
454
+ * [
455
+ * group: "DUNBLE 0 1",
456
+ * o: "- 1699450751193623 0 IN IP4 0.0.0.0",
457
+ * s: "-",
458
+ * t: "0 0",
459
+ * v: "0",
460
+ * ice-lite: "",
461
+ * length: 2,
462
+ * {
463
+ * m: "audio 9 UDP/TLS/RTP/SAVPF 111",
464
+ * c: "IN IP4 0.0.0.0",
465
+ * rtcp: "9",
466
+ * sendonly: "",
467
+ * setup: "passive",
468
+ * fingerprint: "sha-256 51:36:ED:78:A4:9F:25:8C:39:9A:0E:A0:B4:9B:6E:04:37:FF:AD:96:93:71:43:88:2C:0B:0F:AB:6F:9A:52:B8",
469
+ * ice-ufrag: "fa37",
470
+ * ice-pwd: "JncCHryDsbzayy4cBWDxS2",
471
+ * rtcp-mux: "",
472
+ * rtcp-rsize: "",
473
+ * rtpmap: "111 opus/48000/2",
474
+ * rtcp-fb: "111 nack",
475
+ * id: "0",
476
+ * fmtp: "111 minptime=10;useinbandfec=1",
477
+ * candidate: "1 1 udp 2130706431 89.105.221.108 56643 typ host",
478
+ * end-of-candidates: ""
479
+ * },
480
+ * {
481
+ * m: "video 9 UDP/TLS/RTP/SAVPF 106",
482
+ * c: "IN IP4 0.0.0.0",
483
+ * rtcp: "9",
484
+ * sendonly: "",
485
+ * setup: "passive",
486
+ * fingerprint: "sha-256 51:36:ED:78:A4:9F:25:8C:39:9A:0E:A0:B4:9B:6E:04:37:FF:AD:96:93:71:43:88:2C:0B:0F:AB:6F:9A:52:B8",
487
+ * ice-ufrag: "fa37",
488
+ * ice-pwd: "JncCHryDsbzayy4cBWDxS2",
489
+ * rtcp-mux: "",
490
+ * rtcp-rsize: "",
491
+ * rtpmap: "106 H264/90000",
492
+ * rtcp-fb: [
493
+ * "106 nack",
494
+ * "106 goog-remb"
495
+ * ],
496
+ * mid: "1",
497
+ * fmtp: "106 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1",
498
+ * candidate: "1 1 udp 2130706431 89.105.221.108 56643 typ host",
499
+ * end-of-candidates: ""
500
+ * }
501
+ * ]
502
+ */
503
+ fromString(lines: string): any;
504
+ /**
505
+ * Serialize SDP JS object to a SDP string legal representation
506
+ * @param sdp SDP object reprensetation
507
+ * @returns SDP string reprensentation
508
+ */
509
+ toString(sdp: any): string;
510
+ /**
511
+ * While set a property to a SDP object representation is possible directly,
512
+ * we could prefer add a new property without overload a possible already existing value.
513
+ * This function allows to add a property to our SDP representation:
514
+ * - if the key's attribute doesn't exists yet it adds it like a simple JS property sdp[key] = value
515
+ * - if the key's attribute exists already it morphs the value to a Array and push it inside
516
+ * @param sdp the SDP object representation on which added the attribute
517
+ * @param attribute the string attribut in a format "key:value" or just "key" to add an attribute without value
518
+ * @returns the key part of the attribute added (or if value is empty it returns the same attribute as passed in argument)
519
+ */
520
+ addAttribute(sdp: object, attribute: string): string;
521
+ /**
522
+ * While it's possible to delete a attribute manually on the SDP object representation with a delete sdp.key,
523
+ * we could prefer remove only a value in a sdp.key array containing multiple values.
524
+ * Like opposite to addAttribute this method allows to remove an unique attribute value.
525
+ * @param sdp the SDP object representation on which removed the attribute
526
+ * @param attribute the string attribut in a format "key:value" or just "key" to remove the whole attribute and all its values
527
+ * @returns the key part of the attribute removed (or if value is empty it returns the same attribute as passed in argument)
528
+ */
529
+ removeAttribute(sdp: object, attribute: string): string;
530
+ /**
531
+ * Parse an attribute in a format "key:value"
532
+ * @param attribute string attribute to parse
533
+ * @returns the {key, value} result, with value undefined if attribute was a "key" without value
534
+ */
535
+ parseAttribute(attribute: string): {
536
+ key: string;
537
+ value: string | undefined;
538
+ };
539
+ };
540
+
541
+ /**
542
+ * Copyright 2023 Ceeblue B.V.
543
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
544
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
545
+ */
546
+ /**
547
+ * Some basic utility functions
548
+ */
549
+ /**
550
+ * Version of the library
551
+ */
552
+ declare const VERSION = "?";
553
+ /**
554
+ * An empty lambda function, pratical to disable default behavior of function or events which are not expected to be null
555
+ * @example
556
+ * console.log = Util.EMPTY_FUNCTION; // disable logs without breaking calls
557
+ */
558
+ declare const EMPTY_FUNCTION: () => void;
559
+ /**
560
+ * Efficient and high resolution timestamp in milliseconds elapsed since {@link Util.timeOrigin}
561
+ */
562
+ declare function time(): number;
563
+ /**
564
+ * Time origin represents the time when the application has started
565
+ */
566
+ declare function timeOrigin(): number;
567
+ /**
568
+ * Parse query and returns it in an easy-to-use Javascript object form
569
+ * @param urlOrQueryOrSearch string, url, or searchParams containing query. If not set it uses `location.search` to determinate query.
570
+ * @returns An javascript object containing each option
571
+ */
572
+ declare function options(urlOrQueryOrSearch?: URL | URLSearchParams | string | object | undefined): object;
573
+ /**
574
+ * Returns an easy-to-use Javascript object something iterable, such as a Map, Set, or Array
575
+ * @param value iterable input
576
+ * @param params.withType `false`, if set it tries to cast string value to a JS number/boolean/undefined/null type.
577
+ * @param params.noEmptyString `false`, if set it converts empty string value to a true boolean, usefull to allow a `if(result.key)` check for example
578
+ * @returns An javascript object
579
+ */
580
+ declare function objectFrom(value: any, params: {
581
+ withType: boolean;
582
+ noEmptyString: boolean;
583
+ }): object;
584
+ /**
585
+ * Converts various data types, such as objects, strings, exceptions, errors,
586
+ * or numbers, into a string representation. Since it offers a more comprehensive format,
587
+ * this function is preferred to `JSON.stringify()`.
588
+ * @param obj Any objects, strings, exceptions, errors, or number
589
+ * @param params.space `''`, allows to configure space in the string representation
590
+ * @param params.decimal `2`, allows to choose the number of decimal to display in the string representation
591
+ * @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.
592
+ * @returns the final string representation
593
+ */
594
+ declare function stringify(obj: any, params?: {
595
+ space?: string;
596
+ decimal?: number;
597
+ recursive?: number;
598
+ }): string;
599
+ declare function toBin(value: string): Uint8Array;
600
+
601
+ declare const Util_EMPTY_FUNCTION: typeof EMPTY_FUNCTION;
602
+ declare const Util_VERSION: typeof VERSION;
603
+ declare const Util_objectFrom: typeof objectFrom;
604
+ declare const Util_options: typeof options;
605
+ declare const Util_stringify: typeof stringify;
606
+ declare const Util_time: typeof time;
607
+ declare const Util_timeOrigin: typeof timeOrigin;
608
+ declare const Util_toBin: typeof toBin;
609
+ declare namespace Util {
610
+ export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_VERSION as VERSION, Util_objectFrom as objectFrom, Util_options as options, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin };
611
+ }
612
+
613
+ /**
614
+ * Copyright 2023 Ceeblue B.V.
615
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
616
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
617
+ */
618
+
619
+ /**
620
+ * The WebSocketReliable class extends WebSocket to bring up the following improvements:
621
+ * - Fix all possible unintentional closing ways to get always a related error message, {@link onClose | onClose(error?) event}
622
+ * - Make possible message sending while connecting. Indeed no need to wait {@link onOpen} before to send message,
623
+ * you can open the socket and immediately send messages, it will be queue and flushs on connection etablishment
624
+ * - Make possible a delayed connection, or a reconnection. Indeed you can create an unconnected Websocket instance
625
+ * without passing any url argument and starts the conneciton more later with {@link WebSocketReliable.open(url) | open(url)} method
626
+ * - Make possible to control sending/queueing message: send method take an optional queueing=true argument to
627
+ * queue message rather send it, a futur call to flush will send it. Then queueing getter allows to handle the queue
628
+ * if need to purge it or remove some queued message. Use it all together can help to prioritize messages or control overload.
629
+ * @example
630
+ * const ws = new WebSocketReliable(url);
631
+ * ws.onClose = (error?:string) => {
632
+ * if(error) {
633
+ * console.error(error);
634
+ * }
635
+ * // reconnection attempt every seconds
636
+ * setTimeout(() => ws.open(url), 1000);
637
+ * }
638
+ * ws.onMessage = (message:string) => {
639
+ * console.log(message);
640
+ * }
641
+ * ws.send('hello'); // send immediatly a hello message is possible (no need to wait onOpen)
642
+ */
643
+ declare class WebSocketReliable extends EventEmitter {
644
+ /**
645
+ * @event `open` fired when socket is connected
646
+ */
647
+ onOpen(): void;
648
+ /**
649
+ * @event `message` fired on message reception
650
+ * @param message can be binary or string.
651
+ * If you subscribe to the event with message as string type (and not union),
652
+ * it means that you know that all your messages are distributed in a string format
653
+ */
654
+ onMessage(message: ArrayBuffer | string): void;
655
+ /**
656
+ * @event `close` fired on websocket close
657
+ * @param error error description on an improper closure
658
+ */
659
+ onClose(error?: string): void;
660
+ /**
661
+ * binaryType, fix binary type to arrayBuffer
662
+ */
663
+ get binaryType(): BinaryType;
664
+ /**
665
+ * url of connection
666
+ */
667
+ get url(): string;
668
+ /**
669
+ * extensions negociated by the server
670
+ */
671
+ get extensions(): string;
672
+ /**
673
+ * protocol negociated by the server
674
+ */
675
+ get protocol(): string;
676
+ /**
677
+ * opened equals true when connection is etablished, in other word when onOpen event is fired
678
+ */
679
+ get opened(): boolean;
680
+ /**
681
+ * {@link https://developer.mozilla.org/docs/Web/API/WebSocket/readyState | Official websocket readyState}
682
+ */
683
+ get readyState(): number;
684
+ /**
685
+ * True when connection is closed, in other words when {@link onClose} event is fired
686
+ * or when WebSocketReliable is build without url (disconnected creation)
687
+ */
688
+ get closed(): boolean;
689
+ /**
690
+ * The number of bytes of data that were queued during calls to send() but not yet transmitted to the network
691
+ */
692
+ get bufferedAmount(): number;
693
+ /**
694
+ * Queued messages from a call to send() waiting to be transmit one time websocket connection opened (or with an explicit call to flush() method)
695
+ */
696
+ get queueing(): Array<string | ArrayBuffer | ArrayBufferView>;
697
+ private _opened;
698
+ private _closed;
699
+ private _queueing;
700
+ private _queueingBytes;
701
+ private _ws?;
702
+ /**
703
+ * Create a WebSocketReliable object, and open it if an url is passed in argument
704
+ * @param url URL of the WebSocket endpoint or null to start the connection later
705
+ */
706
+ constructor(url?: string | URL, protocols?: string | string[]);
707
+ /**
708
+ * Open a WebSocket connection
709
+ * @param url url of the websocket endpoint
710
+ * @returns this
711
+ */
712
+ open(url: URL | string, protocols?: string | string[]): this;
713
+ /**
714
+ * Send a message
715
+ * @param message
716
+ * @param queueing When set it reports the sending to a more later call to flush
717
+ * @returns this
718
+ */
719
+ send(message: string | ArrayBuffer | ArrayBufferView, queueing?: boolean): this;
720
+ /**
721
+ * Send queueing messages
722
+ */
723
+ flush(): void;
724
+ /**
725
+ * Close websocket
726
+ * @param error the error reason if is not a proper close
727
+ */
728
+ close(error?: string): void;
729
+ }
730
+
731
+ export { BinaryReader, BinaryWriter, BitReader, Connect, EventEmitter, type ILog, NetAddress, Numbers, Queue, SDP, Util, WebSocketReliable };