@ceeblue/web-utils 4.2.0 → 6.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.
package/.codecov.yml ADDED
@@ -0,0 +1,21 @@
1
+ coverage:
2
+ status:
3
+ project:
4
+ default:
5
+ target: 90%
6
+ threshold: 10%
7
+ patch:
8
+ default:
9
+ target: 90%
10
+ threshold: 10%
11
+
12
+ comment:
13
+ layout: "reach, diff, flags, files"
14
+ behavior: default
15
+ require_changes: true
16
+
17
+ ignore:
18
+ - "**/*.test.ts"
19
+ - "**/*.spec.ts"
20
+ - "**/__tests__/**"
21
+ - "**/__mocks__/**"
@@ -273,6 +273,41 @@ declare class ByteRate {
273
273
  private updateSamples;
274
274
  }
275
275
 
276
+ /**
277
+ * Parameters of a key system for encrypted streams (DRM)
278
+ *
279
+ * If the key system is a string, it's the URL of the license server.
280
+ *
281
+ * If the key system is an object, it's a key system configuration with more parameters.
282
+ */
283
+ type KeySystem = string | {
284
+ /**
285
+ * The license server URL
286
+ */
287
+ licenseUrl: string;
288
+ /**
289
+ * The certificate URL if needed (for FairPlay)
290
+ *
291
+ * Or directly the certificate
292
+ */
293
+ certificate?: string | Uint8Array;
294
+ /**
295
+ * The additional HTTP headers to send to the license server
296
+ */
297
+ headers?: Record<string, string>;
298
+ /**
299
+ * Audio robustness level
300
+ *
301
+ * A list of robustness levels, prioritized by the order of the array.
302
+ */
303
+ audioRobustness?: string[];
304
+ /**
305
+ * Video robustness level
306
+ *
307
+ * A list of robustness levels, prioritized by the order of the array.
308
+ */
309
+ videoRobustness?: string[];
310
+ };
276
311
  /**
277
312
  * Parameters of connections
278
313
  */
@@ -296,6 +331,11 @@ type Params = {
296
331
  * iceServer to use while connecting to a WebRTC stream
297
332
  */
298
333
  iceServer?: RTCIceServer;
334
+ /**
335
+ * Map of keys to content protection settings for encrypted streams
336
+ * The key can be "com.apple.fps" for example for FairPlay
337
+ */
338
+ contentProtection?: Record<string, KeySystem>;
299
339
  /**
300
340
  * Optional media extension (mp4, flv, ts, rts), usefull for protocol like WebRTS which supports different container type.
301
341
  * When not set, it's also an output parameter for {@link defineMediaExt} to indicate what is the media type selected
@@ -337,13 +377,14 @@ declare function defineMediaExt(type: Type, params: Params): void;
337
377
  */
338
378
  declare function buildURL(type: Type, params: Params, protocol?: string): URL;
339
379
 
380
+ type Connect_KeySystem = KeySystem;
340
381
  type Connect_Params = Params;
341
382
  type Connect_Type = Type;
342
383
  declare const Connect_Type: typeof Type;
343
384
  declare const Connect_buildURL: typeof buildURL;
344
385
  declare const Connect_defineMediaExt: typeof defineMediaExt;
345
386
  declare namespace Connect {
346
- export { type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL, Connect_defineMediaExt as defineMediaExt };
387
+ export { type Connect_KeySystem as KeySystem, type Connect_Params as Params, Connect_Type as Type, Connect_buildURL as buildURL, Connect_defineMediaExt as defineMediaExt };
347
388
  }
348
389
 
349
390
  /**
@@ -776,13 +817,22 @@ declare const SDP: {
776
817
  */
777
818
  declare const EMPTY_FUNCTION: () => void;
778
819
  /**
779
- * Efficient and high resolution timestamp in milliseconds elapsed since {@link Util.timeOrigin}
820
+ * Returns an efficient timestamp in milliseconds elapsed since {@link performance.timeOrigin},
821
+ * representing the start of the current JavaScript execution context.
822
+ *
823
+ * Note: Each Web Worker runs in a separate JS context, so timestamps
824
+ * are not directly comparable between different workers. Use {@link unixTime}
825
+ * for comparable timestamps across different Web Workers.
780
826
  */
781
827
  declare function time(): number;
782
828
  /**
783
- * Time origin represents the time when the application has started
829
+ * Returns an efficient Unix timestamp in milliseconds.
830
+ *
831
+ * Unix timestamps are universally comparable across different JavaScript
832
+ * contexts (e.g., Web Workers), as they reference the number of milliseconds
833
+ * elapsed since January 1, 1970 (UNIX epoch).
784
834
  */
785
- declare function timeOrigin(): number;
835
+ declare function unixTime(): number;
786
836
  /**
787
837
  * Parse query and returns it in an easy-to-use Javascript object form
788
838
  * @param urlOrQueryOrSearch string, url, or searchParams containing query. If not set it uses `location.search` to determinate query.
@@ -801,14 +851,14 @@ declare function objectFrom(value: any, params: {
801
851
  noEmptyString: boolean;
802
852
  }): any;
803
853
  /**
804
- * Returns entries from an iterable input like Map, Set, or Array.
854
+ * Returns a IterableIterator<[string, any]> from any iterable input like Map, Set, Array, or Object.
805
855
  *
806
- * For all other types of values (including `null` or `undefined`), it returns an empty array.
856
+ * For all other types of values (including `null` or `undefined`) it returns an empty iterator.
807
857
  *
808
858
  * @param value An iterable input
809
- * @returns An array of key-value pairs
859
+ * @returns a IterableIterator<[string, any]>
810
860
  */
811
- declare function objectEntries(value: any): [string, any][];
861
+ declare function iterableEntries(value: any): IterableIterator<[string, any]>;
812
862
  /**
813
863
  * Converts various data types, such as objects, strings, exceptions, errors,
814
864
  * or numbers, into a string representation. Since it offers a more comprehensive format,
@@ -895,20 +945,20 @@ declare const Util_fetch: typeof fetch;
895
945
  declare const Util_getBaseFile: typeof getBaseFile;
896
946
  declare const Util_getExtension: typeof getExtension;
897
947
  declare const Util_getFile: typeof getFile;
898
- declare const Util_objectEntries: typeof objectEntries;
948
+ declare const Util_iterableEntries: typeof iterableEntries;
899
949
  declare const Util_objectFrom: typeof objectFrom;
900
950
  declare const Util_options: typeof options;
901
951
  declare const Util_safePromise: typeof safePromise;
902
952
  declare const Util_sleep: typeof sleep;
903
953
  declare const Util_stringify: typeof stringify;
904
954
  declare const Util_time: typeof time;
905
- declare const Util_timeOrigin: typeof timeOrigin;
906
955
  declare const Util_toBin: typeof toBin;
907
956
  declare const Util_trim: typeof trim;
908
957
  declare const Util_trimEnd: typeof trimEnd;
909
958
  declare const Util_trimStart: typeof trimStart;
959
+ declare const Util_unixTime: typeof unixTime;
910
960
  declare namespace Util {
911
- export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_equal as equal, Util_fetch as fetch, Util_getBaseFile as getBaseFile, Util_getExtension as getExtension, Util_getFile as getFile, Util_objectEntries as objectEntries, Util_objectFrom as objectFrom, Util_options as options, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_timeOrigin as timeOrigin, Util_toBin as toBin, Util_trim as trim, Util_trimEnd as trimEnd, Util_trimStart as trimStart };
961
+ export { Util_EMPTY_FUNCTION as EMPTY_FUNCTION, Util_equal as equal, Util_fetch as fetch, Util_getBaseFile as getBaseFile, Util_getExtension as getExtension, Util_getFile as getFile, Util_iterableEntries as iterableEntries, Util_objectFrom as objectFrom, Util_options as options, Util_safePromise as safePromise, Util_sleep as sleep, Util_stringify as stringify, Util_time as time, Util_toBin as toBin, Util_trim as trim, Util_trimEnd as trimEnd, Util_trimStart as trimStart, Util_unixTime as unixTime };
912
962
  }
913
963
 
914
964
  /**
@@ -1183,6 +1233,7 @@ declare class UIMetrics {
1183
1233
  private _legendFontSize;
1184
1234
  private _stepSize;
1185
1235
  private _ranges;
1236
+ private _mouseX?;
1186
1237
  constructor(ui: HTMLElement);
1187
1238
  /**
1188
1239
  * Reset metrics stats, essentially rescaling the metrics
package/dist/web-utils.js CHANGED
@@ -164,16 +164,25 @@ const _perf = performance; // to increase x10 now performance!
164
164
  */
165
165
  const EMPTY_FUNCTION = () => { };
166
166
  /**
167
- * Efficient and high resolution timestamp in milliseconds elapsed since {@link Util.timeOrigin}
167
+ * Returns an efficient timestamp in milliseconds elapsed since {@link performance.timeOrigin},
168
+ * representing the start of the current JavaScript execution context.
169
+ *
170
+ * Note: Each Web Worker runs in a separate JS context, so timestamps
171
+ * are not directly comparable between different workers. Use {@link unixTime}
172
+ * for comparable timestamps across different Web Workers.
168
173
  */
169
174
  function time() {
170
175
  return Math.floor(_perf.now());
171
176
  }
172
177
  /**
173
- * Time origin represents the time when the application has started
178
+ * Returns an efficient Unix timestamp in milliseconds.
179
+ *
180
+ * Unix timestamps are universally comparable across different JavaScript
181
+ * contexts (e.g., Web Workers), as they reference the number of milliseconds
182
+ * elapsed since January 1, 1970 (UNIX epoch).
174
183
  */
175
- function timeOrigin() {
176
- return Math.floor(_perf.now() + _perf.timeOrigin);
184
+ function unixTime() {
185
+ return Math.floor(_perf.timeOrigin + _perf.now());
177
186
  }
178
187
  /**
179
188
  * Parse query and returns it in an easy-to-use Javascript object form
@@ -214,7 +223,7 @@ function objectFrom(value, params) {
214
223
  if (!value) {
215
224
  return obj;
216
225
  }
217
- for (const [key, val] of objectEntries(value)) {
226
+ for (const [key, val] of iterableEntries(value)) {
218
227
  value = val;
219
228
  if (params.withType && value != null && value.substring) {
220
229
  if (value) {
@@ -257,28 +266,28 @@ function objectFrom(value, params) {
257
266
  return obj;
258
267
  }
259
268
  /**
260
- * Returns entries from an iterable input like Map, Set, or Array.
269
+ * Returns a IterableIterator<[string, any]> from any iterable input like Map, Set, Array, or Object.
261
270
  *
262
- * For all other types of values (including `null` or `undefined`), it returns an empty array.
271
+ * For all other types of values (including `null` or `undefined`) it returns an empty iterator.
263
272
  *
264
273
  * @param value An iterable input
265
- * @returns An array of key-value pairs
274
+ * @returns a IterableIterator<[string, any]>
266
275
  */
267
- function objectEntries(value) {
276
+ function iterableEntries(value) {
268
277
  if (!value) {
269
- return [];
278
+ return (function* () { })();
270
279
  }
271
- if (value.entries && typeof value.entries === 'function') {
280
+ if (typeof value.entries === 'function') {
272
281
  value = value.entries();
273
- if (Array.isArray(value)) {
274
- return value;
275
- }
276
282
  }
277
- const entries = [];
278
- for (const key of Object.keys(value)) {
279
- entries.push([key, value[key]]);
283
+ if (typeof value[Symbol.iterator] === 'function') {
284
+ return value;
280
285
  }
281
- return entries;
286
+ return (function* () {
287
+ for (const key in value) {
288
+ yield [key.toString(), value[key]];
289
+ }
290
+ })();
282
291
  }
283
292
  /**
284
293
  * Converts various data types, such as objects, strings, exceptions, errors,
@@ -491,7 +500,7 @@ function trimEnd(value, chars = ' ') {
491
500
  --i;
492
501
  }
493
502
  return value.substring(0, i);
494
- }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,equal:equal,fetch:fetch,getBaseFile:getBaseFile,getExtension:getExtension,getFile:getFile,objectEntries:objectEntries,objectFrom:objectFrom,options:options,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,timeOrigin:timeOrigin,toBin:toBin,trim:trim,trimEnd:trimEnd,trimStart:trimStart});/**
503
+ }var Util=/*#__PURE__*/Object.freeze({__proto__:null,EMPTY_FUNCTION:EMPTY_FUNCTION,equal:equal,fetch:fetch,getBaseFile:getBaseFile,getExtension:getExtension,getFile:getFile,iterableEntries:iterableEntries,objectFrom:objectFrom,options:options,safePromise:safePromise,sleep:sleep,stringify:stringify,time:time,toBin:toBin,trim:trim,trimEnd:trimEnd,trimStart:trimStart,unixTime:unixTime});/**
495
504
  * Copyright 2024 Ceeblue B.V.
496
505
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
497
506
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
@@ -1032,6 +1041,8 @@ class NetAddress {
1032
1041
  * console.log(NetAddress.fixProtocol('wss','http://domain/path')) // 'ws://domain/path'
1033
1042
  */
1034
1043
  static fixProtocol(protocol, address) {
1044
+ // Remove leading slashes for absolute pathes!
1045
+ address = address.replace(/^[\/]+/, '');
1035
1046
  const found = address.indexOf('://');
1036
1047
  // isolate protocol is present in address
1037
1048
  if (found >= 0) {
@@ -1118,14 +1129,14 @@ class NetAddress {
1118
1129
  this._host = address;
1119
1130
  this._domain = address;
1120
1131
  this._port = defaultPort;
1121
- // Parse Port
1122
- pos = this._host.lastIndexOf(':');
1123
- const endOfBrace = this._host.lastIndexOf(']'); // to support IPv6
1124
- if (pos > endOfBrace) {
1125
- const port = parseInt(address.substring(pos + 1));
1126
- if (port >= 0 && port <= 0xffff) {
1127
- this._port = port;
1128
- this._domain = address.substring(0, pos);
1132
+ const domainPortMatch = this._host.match(/^(?:\[([0-9a-fA-F:]+)\]|([^:/?#]+))(?::(\d+))?(?=[/#?]|$)/);
1133
+ if (domainPortMatch) {
1134
+ this._domain = domainPortMatch[1] || domainPortMatch[2];
1135
+ if (domainPortMatch[3]) {
1136
+ const port = parseInt(domainPortMatch[3]);
1137
+ if (port >= 0 && port <= 0xffff) {
1138
+ this._port = port;
1139
+ }
1129
1140
  }
1130
1141
  }
1131
1142
  }
@@ -1205,6 +1216,7 @@ function defineMediaExt(type, params) {
1205
1216
  * @returns The URL of connection
1206
1217
  */
1207
1218
  function buildURL(type, params, protocol = 'wss') {
1219
+ var _a;
1208
1220
  defineMediaExt(type, params);
1209
1221
  const url = new URL(NetAddress.fixProtocol(protocol, params.endPoint));
1210
1222
  if (url.pathname.length <= 1) {
@@ -1241,7 +1253,7 @@ function buildURL(type, params, protocol = 'wss') {
1241
1253
  if (params.accessToken) {
1242
1254
  url.searchParams.set('id', params.accessToken);
1243
1255
  }
1244
- for (const [key, value] of objectEntries(params.query)) {
1256
+ for (const [key, value] of (_a = params.query) !== null && _a !== void 0 ? _a : []) {
1245
1257
  url.searchParams.set(key, value);
1246
1258
  }
1247
1259
  return url;
@@ -1359,11 +1371,12 @@ class EventEmitter extends Loggable {
1359
1371
  throw Error('event callback must be a function');
1360
1372
  }
1361
1373
  const events = this._event(name);
1362
- events.add((...args) => {
1363
- events.delete(event); // delete from events
1374
+ const wrapper = (...args) => {
1375
+ events.delete(wrapper); // delete the wrapper from events
1364
1376
  event(...args); // execute event
1365
- });
1366
- (_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => events.delete(event), { once: true });
1377
+ };
1378
+ events.add(wrapper);
1379
+ (_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => events.delete(wrapper), { once: true });
1367
1380
  }
1368
1381
  /**
1369
1382
  * Event unsubscription
@@ -2287,6 +2300,12 @@ class UIMetrics {
2287
2300
  }
2288
2301
  constructor(ui) {
2289
2302
  this._ui = ui;
2303
+ ui.addEventListener('mousemove', (event) => {
2304
+ this._mouseX = event.offsetX;
2305
+ });
2306
+ ui.addEventListener('mouseleave', (event) => {
2307
+ this._mouseX = undefined;
2308
+ });
2290
2309
  // default values in pixels
2291
2310
  this._lineHeight = 40;
2292
2311
  this._labelWidth = 170;
@@ -2370,6 +2389,7 @@ class UIMetrics {
2370
2389
  const delta = range.max - range.min;
2371
2390
  let minCircle = '';
2372
2391
  let maxCircle = '';
2392
+ let mouseCircle = this._mouseX == null || this._mouseX > x ? null : '';
2373
2393
  for (let i = 0; i < values.length; ++i) {
2374
2394
  x -= this._stepSize;
2375
2395
  const value = parseFloat(values[i].toString());
@@ -2381,6 +2401,9 @@ class UIMetrics {
2381
2401
  else if (value === max) {
2382
2402
  minCircle = minCircle || this._drawCircle(x, y, value);
2383
2403
  }
2404
+ if (mouseCircle === '' && x <= (this._mouseX || 0)) {
2405
+ mouseCircle = this._drawCircle(x, y, value, 'blue', '');
2406
+ }
2384
2407
  }
2385
2408
  this._html += '" />'; // end path
2386
2409
  // Average
@@ -2395,7 +2418,10 @@ class UIMetrics {
2395
2418
  '</tspan>';
2396
2419
  this._html += '<tspan x="' + (width + averageCenter) + '" dy="1em">±' + average + '</tspan>';
2397
2420
  this._html += '</text>';
2398
- this._html += minCircle + maxCircle;
2421
+ this._html += minCircle + maxCircle + (mouseCircle !== null && mouseCircle !== void 0 ? mouseCircle : '');
2422
+ if (mouseCircle) {
2423
+ this._html += mouseCircle;
2424
+ }
2399
2425
  this._html += '</svg>';
2400
2426
  }
2401
2427
  requestAnimationFrame(() => {
@@ -2405,8 +2431,8 @@ class UIMetrics {
2405
2431
  }
2406
2432
  });
2407
2433
  }
2408
- _drawCircle(x, y, value) {
2409
- let circle = '<circle cx="' + x + '" cy="' + y + '" r="2" fill="green" />';
2434
+ _drawCircle(x, y, value, color = 'green', fontStyle = 'italic') {
2435
+ let circle = '<circle cx="' + x + '" cy="' + y + '" r="2" fill="' + color + '" />';
2410
2436
  const legendFontHeight = 0.7 * this._legendFontSize;
2411
2437
  const graphMiddle = Math.round(this._lineHeight / 2);
2412
2438
  if (y < graphMiddle) {
@@ -2418,7 +2444,9 @@ class UIMetrics {
2418
2444
  y -= this.textMargin;
2419
2445
  }
2420
2446
  circle +=
2421
- '<text font-style="italic" font-size="' +
2447
+ '<text font-style="' +
2448
+ fontStyle +
2449
+ '" font-size="' +
2422
2450
  this._legendFontSize +
2423
2451
  '" x="' +
2424
2452
  (x - this._legendFontSize) +
@@ -2434,4 +2462,4 @@ class UIMetrics {
2434
2462
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
2435
2463
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
2436
2464
  */
2437
- const VERSION = '4.2.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EpochTime,EventEmitter,FixMap,Log,LogLevel,Loggable,NetAddress,Numbers,Queue,SDP,UIMetrics,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map
2465
+ const VERSION = '6.0.0';export{BinaryReader,BinaryWriter,BitReader,ByteRate,Connect,EpochTime,EventEmitter,FixMap,Log,LogLevel,Loggable,NetAddress,Numbers,Queue,SDP,UIMetrics,Util,VERSION,WebSocketReliable,log};//# sourceMappingURL=web-utils.js.map