@ceeblue/web-utils 2.6.1 → 3.1.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
@@ -27,6 +27,13 @@ import { Util, ILog } from '@ceeblue/web-utils';
27
27
  > }
28
28
  > ```
29
29
 
30
+ > ⚠️ **REMARKS**
31
+ >
32
+ > To debug production code without modifying it, the library can use special query parameter of the main page's URL:
33
+ > - __!cb-override-log-level__ : allows to override the log level for the entire library, see [Log.ts](./src/Log.ts) for details on handling log levels.
34
+
35
+
36
+
30
37
  ## Building locally
31
38
 
32
39
  1. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository
@@ -72,10 +72,124 @@ declare class BinaryWriter {
72
72
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
73
73
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
74
74
  */
75
+ /**
76
+ * Log levels
77
+ */
78
+ declare enum LogLevel {
79
+ ERROR = "error",
80
+ WARN = "warn",
81
+ INFO = "info",
82
+ DEBUG = "debug"
83
+ }
84
+ /**
85
+ * Log interface to deal with log everywhere:
86
+ * - filter log level: filter log level independantly of the browser
87
+ * - subscribe logs: listen logs to effectuate some specific job related logs
88
+ * - intercept logs: intercept logs to change the default behavior
89
+ * - redirect logs: redirect logs to one other logger engine
90
+ * - redefine logs: change log text like adding a prefix
91
+ *
92
+ * You have 4 {@link LogLevel} 'error','warn','info',and 'debug', as commonly managed by browsers.
93
+ * You can use {@link ILog.level} or {@link ILog.on} to customer log level and behavior.
94
+ *
95
+ * @example
96
+ * // filter log level globally, independantly of the browser
97
+ * import { log } from '@ceeblue/web-utils';
98
+ * log.level = LogLevel.WARN; // displays errors and warns
99
+ *
100
+ * // filter log level only for Player compoment
101
+ * player.log.level = false; // no logs at all for player compoment
102
+ *
103
+ * // Intercept and redirect all the logs to the console (default behavior)
104
+ * import { log } from '@ceeblue/web-utils';
105
+ * log.on = (level:LogLevel,args:unknown[]) => {
106
+ * console[level](...args.splice(0)); // args is empty after this call = final interception
107
+ * }
108
+ *
109
+ * // Intercept the logs from Player compoment and displays only WARN logs
110
+ * player.log.on = (level:LogLevel,args:unknown[]) => {
111
+ * if (level !== LogLevel.WARN) {
112
+ * args.length = 0; // args is empty after this call = final interception
113
+ * }
114
+ * }
115
+ *
116
+ * // Subscribe and redirect all the logs to a file logger
117
+ * import { log } from '@ceeblue/web-utils';
118
+ * log.on = (level:LogLevel,args:unknown[]) => {
119
+ * fileLogger[level](...args); // args stays unchanged to let's continue the default behavior
120
+ * }
121
+ *
122
+ * // Redefine the log to add some prefix indication
123
+ * class Player {
124
+ * connector = new Connector();
125
+ * constructor() {
126
+ * connector.log = this.log.bind(this,"Connector log:");
127
+ * }
128
+ * }
129
+ *
130
+ */
131
+ interface ILog {
132
+ /**
133
+ * Build a log
134
+ */
135
+ (...args: unknown[]): Log;
136
+ /**
137
+ * Intercept,redefine or redirect any log
138
+ * If you clear args you intercept the log and nothing happen more after this call.
139
+ * @param type log level
140
+ * @param args args
141
+ * @returns
142
+ */
143
+ on: (level: LogLevel, args: unknown[]) => void;
144
+ /**
145
+ * Change log level, default log level is {@link LogLevel.INFO},
146
+ * Boolean can be user to lets pass all the logs with `true` or disables all the logs with `false`.
147
+ * @note To debug production code without modifying it you can use a special query parameter
148
+ * called !cb-override-log-level to override this configuration.
149
+ */
150
+ level?: LogLevel | boolean;
151
+ }
152
+ /**
153
+ * Log instance
154
+ */
155
+ declare class Log {
156
+ get error(): (...args: any[]) => void;
157
+ get warn(): (...args: any[]) => void;
158
+ get info(): (...args: any[]) => void;
159
+ get debug(): (...args: any[]) => void;
160
+ private _args;
161
+ private _done?;
162
+ private _log;
163
+ constructor(log: ILog, ...args: unknown[]);
164
+ private _onLog;
165
+ private _bind;
166
+ }
167
+ /**
168
+ * Inherits from this class to use logs
169
+ */
170
+ declare class Loggable {
171
+ /**
172
+ * Start a log
173
+ * @param args
174
+ * @returns a Log object with the levels of log to call
175
+ */
176
+ log: ILog;
177
+ }
178
+ /**
179
+ * Global log
180
+ */
181
+ declare const log: ILog;
182
+
183
+ /**
184
+ * Copyright 2024 Ceeblue B.V.
185
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
186
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
187
+ */
188
+
75
189
  /**
76
190
  * BitReader allows to read binary data bit by bit
77
191
  */
78
- declare class BitReader {
192
+ declare class BitReader extends Loggable {
79
193
  private _data;
80
194
  private _size;
81
195
  private _position;
@@ -94,19 +208,69 @@ declare class BitReader {
94
208
  }
95
209
 
96
210
  /**
97
- * Compute ByteRate every delta time
211
+ * Class to compute a weighted average byte rate over a specified time interval.
212
+ *
213
+ * This class continuously tracks data transmission and computes the byte rate
214
+ * based on a weighted average, considering both the duration and the number of
215
+ * bytes in each sample. It allows for real-time monitoring of bandwidth usage
216
+ * and provides mechanisms to dynamically adjust the measurement interval.
217
+ *
218
+ * Features:
219
+ * - Computes the byte rate using a **weighted average** approach.
220
+ * - Allows setting a custom interval for tracking.
221
+ * - Supports dynamic clipping to manually shrink the observation window.
98
222
  */
99
223
  declare class ByteRate {
224
+ /**
225
+ * Raised when new bytes are added
226
+ */
100
227
  onBytes(bytes: number): void;
101
- get delta(): number;
228
+ /**
229
+ * Returns the interval used for computing the byte rate
230
+ */
231
+ get interval(): number;
232
+ /**
233
+ * Sets a new interval for computing the average byte rate
234
+ */
235
+ set interval(value: number);
236
+ private _interval;
102
237
  private _bytes;
103
238
  private _time;
104
- private _delta;
105
- private _value;
106
- constructor(delta?: number);
239
+ private _samples;
240
+ private _clip;
241
+ /**
242
+ * Constructor initializes the ByteRate object with a specified interval (default: 1000ms).
243
+ * It sets up necessary variables to track byte rate over time.
244
+ *
245
+ * @param interval - Time interval in milliseconds to compute the byte rate.
246
+ */
247
+ constructor(interval?: number);
248
+ /**
249
+ * Returns the computed byte rate rounded to the nearest integer
250
+ */
107
251
  value(): number;
252
+ /**
253
+ * Computes the exact byte rate in bytes per second
254
+ */
108
255
  exact(): number;
109
- addBytes(bytes: number): this;
256
+ /**
257
+ * Adds a new byte sample to the tracking system.
258
+ * Updates the list of samples and recomputes the byte rate
259
+ *
260
+ * @param bytes - Number of bytes added in this interval
261
+ */
262
+ addBytes(bytes: number): ByteRate;
263
+ /**
264
+ * Clears all recorded byte rate data.
265
+ */
266
+ clear(): ByteRate;
267
+ /**
268
+ * Clips the byte rate tracking by marking the last sample as clipped.
269
+ * If a previous clip exists, removes the clipped sample and all preceding samples.
270
+ * Allows to shrink the interval manually between two positions.
271
+ */
272
+ clip(): ByteRate;
273
+ private updateSamples;
110
274
  }
111
275
 
112
276
  /**
@@ -179,99 +343,6 @@ declare namespace Connect {
179
343
  export { type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL, Connect_defineMediaExt as defineMediaExt };
180
344
  }
181
345
 
182
- /**
183
- * Copyright 2024 Ceeblue B.V.
184
- * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
185
- * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
186
- */
187
- /**
188
- * Log interface to deal with log everywhere:
189
- * - subscribe logs: listen logs to effectuate some specific job related logs
190
- * - intercept logs: intercept logs to change the default behavior
191
- * - redirect logs: redirect logs to one other logger engine
192
- * - redefine logs: change log text like adding a prefix
193
- *
194
- * You have 4 {@link LogType} 'error', 'warn', 'info', and 'debug', as commonly managed by browsers.
195
- *
196
- * @example
197
- * // Intercept and redirect all the logs to the console (default behavior)
198
- * import { log } from '@ceeblue/web-utils';
199
- * log.on(type:LogType, args:uknown[]) => {
200
- * console[type](...args.splice(0)); // args is empty after this call = final interception
201
- * }
202
- *
203
- * // Intercept and redirect the logs from Player compoment to the console
204
- * player.log.on(type:LogType, args:uknown[]) => {
205
- * console[type](...args.splice(0)); // args is empty after this call = final interception
206
- * }
207
- *
208
- * // Subscribe and redirect all the logs to a file logger
209
- * log.on(type:LogType, args:uknown[]) => {
210
- * fileLogger[type](...args); // args stays unchanged to let's continue the default behavior
211
- * }
212
- *
213
- * // Redefine the log to add some prefix indication
214
- * class Player {
215
- * connector = new Connector();
216
- * constructor() {
217
- * connector.log = this.log.bind(this, "Connector log:");
218
- * }
219
- * }
220
- *
221
- */
222
- interface ILog {
223
- /**
224
- * Build a log
225
- */
226
- (...args: unknown[]): Log;
227
- /**
228
- * Intercept, redefine or redirect any log
229
- * If you clear args you intercept the log and nothing happen more after this call.
230
- * @param type log level
231
- * @param args args
232
- * @returns
233
- */
234
- on: (type: LogType, args: unknown[]) => void;
235
- }
236
- /**
237
- * Log types
238
- */
239
- declare enum LogType {
240
- ERROR = "error",
241
- WARN = "warn",
242
- INFO = "info",
243
- DEBUG = "debug"
244
- }
245
- /**
246
- * Log instance
247
- */
248
- declare class Log {
249
- get error(): (...args: any[]) => void;
250
- get warn(): (...args: any[]) => void;
251
- get info(): (...args: any[]) => void;
252
- get debug(): (...args: any[]) => void;
253
- private _args;
254
- private _done?;
255
- private _onLog;
256
- constructor(onLog: Function, ...args: unknown[]);
257
- private _bind;
258
- }
259
- /**
260
- * Inherits from this class to use logs
261
- */
262
- declare class Loggable {
263
- /**
264
- * Start a log
265
- * @param args
266
- * @returns a Log object with the levels of log to call
267
- */
268
- log: ILog;
269
- }
270
- /**
271
- * Global log
272
- */
273
- declare const log: ILog;
274
-
275
346
  /**
276
347
  * Copyright 2024 Ceeblue B.V.
277
348
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
@@ -329,16 +400,20 @@ declare class EventEmitter extends Loggable {
329
400
  * Event subscription
330
401
  * @param name Name of event without the `on` prefix (ex: `log` to `onLog` event declared)
331
402
  * @param event Subscriber Function
332
- * @param abort Optional `AbortController` to stop this or multiple subscriptions in same time
403
+ * @param options.signal Optional `AbortSignal` to stop this or multiple subscriptions in same time
333
404
  */
334
- on(name: string, event: Function, abort?: AbortController): void;
405
+ on(name: string, event: Function, options?: {
406
+ signal?: AbortSignal;
407
+ }): void;
335
408
  /**
336
409
  * Event subscription only one time, once time fired it's automatically unsubscribe
337
410
  * @param name Name of event without the `on` prefix (ex: `log` to `onLog` event declared)
338
411
  * @param event Subscriber Function
339
- * @param abort Optional `AbortController` to stop this or multiple subscriptions in same time
412
+ * @param options.abortSignal Optional `AbortSignal` to stop this or multiple subscriptions in same time
340
413
  */
341
- once(name: string, event: Function, abort?: AbortController): void;
414
+ once(name: string, event: Function, options?: {
415
+ signal?: AbortSignal;
416
+ }): void;
342
417
  /**
343
418
  * Event unsubscription
344
419
  * @param name Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)
@@ -694,7 +769,7 @@ declare function timeOrigin(): number;
694
769
  * @param urlOrQueryOrSearch string, url, or searchParams containing query. If not set it uses `location.search` to determinate query.
695
770
  * @returns An javascript object containing each option
696
771
  */
697
- declare function options(urlOrQueryOrSearch?: URL | URLSearchParams | string | object | undefined): object;
772
+ declare function options(urlOrQueryOrSearch?: URL | URLSearchParams | string | object | undefined): any;
698
773
  /**
699
774
  * Returns an easy-to-use Javascript object something iterable, such as a Map, Set, or Array
700
775
  * @param value iterable input
@@ -705,7 +780,7 @@ declare function options(urlOrQueryOrSearch?: URL | URLSearchParams | string | o
705
780
  declare function objectFrom(value: any, params: {
706
781
  withType: boolean;
707
782
  noEmptyString: boolean;
708
- }): object;
783
+ }): any;
709
784
  /**
710
785
  * Returns entries from something iterable, such as a Map, Set, or Array
711
786
  * @param value iterable input
@@ -1012,6 +1087,86 @@ declare namespace EpochTime {
1012
1087
  export { EpochTime_decodeTimestamp as decodeTimestamp, EpochTime_encodeTimestamp as encodeTimestamp, EpochTime_getLatency as getLatency };
1013
1088
  }
1014
1089
 
1090
+ /**
1091
+ * Copyright 2024 Ceeblue B.V.
1092
+ * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
1093
+ * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
1094
+ */
1095
+ /**
1096
+ * An user-interface compoment to vizualize real-time metrics
1097
+ */
1098
+ declare class UIMetrics {
1099
+ /**
1100
+ * get graph margin in pixels
1101
+ */
1102
+ get graphMargin(): number;
1103
+ /**
1104
+ * set graph margin in pixels
1105
+ */
1106
+ set graphMargin(value: number);
1107
+ /**
1108
+ * get text margin in pixels
1109
+ */
1110
+ get textMargin(): number;
1111
+ /**
1112
+ * set text margin in pixels
1113
+ */
1114
+ set textMargin(value: number);
1115
+ /**
1116
+ * get metric line height in pixels
1117
+ */
1118
+ get lineHeight(): number;
1119
+ /**
1120
+ * set metric line height in pixels
1121
+ */
1122
+ set lineHeight(value: number);
1123
+ /**
1124
+ * get label width in pixels
1125
+ */
1126
+ get labelWidth(): number;
1127
+ /**
1128
+ * set label width in pixels
1129
+ */
1130
+ set labelWidth(value: number);
1131
+ /**
1132
+ * get legend font size in pixels
1133
+ */
1134
+ get legendFontSize(): number;
1135
+ /**
1136
+ * set legend font size in pixels
1137
+ */
1138
+ set legendFontSize(value: number);
1139
+ /**
1140
+ * get the metric unit-step in pixels
1141
+ */
1142
+ get stepSize(): number;
1143
+ /**
1144
+ * set the metric unit-step in pixels
1145
+ */
1146
+ set stepSize(value: number);
1147
+ private _ui;
1148
+ private _html?;
1149
+ private _lineHeight;
1150
+ private _labelWidth;
1151
+ private _graphMargin;
1152
+ private _textMargin;
1153
+ private _legendFontSize;
1154
+ private _stepSize;
1155
+ private _ranges;
1156
+ constructor(ui: HTMLElement);
1157
+ /**
1158
+ * Reset metrics stats, essentially rescaling the metrics
1159
+ */
1160
+ reset(): void;
1161
+ /**
1162
+ * build metric from stats
1163
+ * @param stats Map with stats per entry
1164
+ * @returns
1165
+ */
1166
+ display(stats: Map<string, Array<string | number>>): void;
1167
+ private _drawCircle;
1168
+ }
1169
+
1015
1170
  /**
1016
1171
  * Copyright 2024 Ceeblue B.V.
1017
1172
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
@@ -1020,4 +1175,4 @@ declare namespace EpochTime {
1020
1175
 
1021
1176
  declare const VERSION: string;
1022
1177
 
1023
- export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EpochTime, EventEmitter, FixMap, type ILog, Log, LogType, Loggable, NetAddress, Numbers, Queue, SDP, Util, VERSION, WebSocketReliable, type WebSocketReliableError, log };
1178
+ export { BinaryReader, BinaryWriter, BitReader, ByteRate, Connect, EpochTime, EventEmitter, FixMap, type ILog, Log, LogLevel, Loggable, NetAddress, Numbers, Queue, SDP, UIMetrics, Util, VERSION, WebSocketReliable, type WebSocketReliableError, log };