@ceeblue/web-utils 3.2.1 → 4.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.
@@ -283,7 +283,9 @@ type Params = {
283
283
  */
284
284
  endPoint: string;
285
285
  /**
286
- * The name of the stream to join
286
+ * The name of the stream to join.
287
+ * If `endPoint` is a complete URL and `streamName` is not provided, {@link buildURL} will set this parameter automatically
288
+ * using the second part of the URL's path (the first part being the protocol name), or the first path if no other part exists.
287
289
  */
288
290
  streamName: string;
289
291
  /**
@@ -296,13 +298,13 @@ type Params = {
296
298
  iceServer?: RTCIceServer;
297
299
  /**
298
300
  * Optional media extension (mp4, flv, ts, rts), usefull for protocol like WebRTS which supports different container type.
299
- * When not set, it's also an output parameter to indicate what is the media type selected
301
+ * When not set, it's also an output parameter for {@link defineMediaExt} to indicate what is the media type selected
300
302
  */
301
303
  mediaExt?: string;
302
304
  /**
303
305
  * Optional query to add into the generated url of connection
304
306
  */
305
- query?: Record<string, string>;
307
+ query?: URLSearchParams;
306
308
  };
307
309
  /**
308
310
  * Type of connection
@@ -327,6 +329,7 @@ declare enum Type {
327
329
  declare function defineMediaExt(type: Type, params: Params): void;
328
330
  /**
329
331
  * Build an URL from {@link Type | type} and {@link Params | params}
332
+ * Can assign {@link Params.mediaExt | params.mediaExt} or {@link Params.streamName | params.streamName}
330
333
  * @param type Type of the connection wanted
331
334
  * @param params Connection parameters
332
335
  * @param protocol Optional parameter to choose the prefered protocol to connect
@@ -349,6 +352,20 @@ declare namespace Connect {
349
352
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
350
353
  */
351
354
 
355
+ type FilterOnKey<T> = T extends 'once' | 'on' ? never : T extends `on${infer R}` ? R : never;
356
+ type CaseVariations<T extends string> = string extends T ? string : Lowercase<T> | Capitalize<T>;
357
+ /**
358
+ * Extract all event keys from a class.
359
+ * @example
360
+ * class Logger extends EventEmitter {
361
+ * onLog(log:string) { console.log(log); }
362
+ * onClick(log:string) { console.log(log); }
363
+ * }
364
+ * type LoggerEvents = EventKeys<Logger>; // "log" | "click"
365
+ */
366
+ type EventKeys<Keys> = keyof {
367
+ [K in keyof Keys as CaseVariations<FilterOnKey<K>>]: never;
368
+ };
352
369
  /**
353
370
  * A advanced EventEmitter which allows to declare event as natural function in the inheriting children class,
354
371
  * function must start by `on` prefix to be recognized as an event.
@@ -402,7 +419,7 @@ declare class EventEmitter extends Loggable {
402
419
  * @param event Subscriber Function
403
420
  * @param options.signal Optional `AbortSignal` to stop this or multiple subscriptions in same time
404
421
  */
405
- on(name: string, event: Function, options?: {
422
+ on(name: EventKeys<this>, event: Function, options?: {
406
423
  signal?: AbortSignal;
407
424
  }): void;
408
425
  /**
@@ -411,7 +428,7 @@ declare class EventEmitter extends Loggable {
411
428
  * @param event Subscriber Function
412
429
  * @param options.abortSignal Optional `AbortSignal` to stop this or multiple subscriptions in same time
413
430
  */
414
- once(name: string, event: Function, options?: {
431
+ once(name: EventKeys<this>, event: Function, options?: {
415
432
  signal?: AbortSignal;
416
433
  }): void;
417
434
  /**
@@ -419,7 +436,7 @@ declare class EventEmitter extends Loggable {
419
436
  * @param name Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)
420
437
  * @param event Unsubscriber Function, must be the one passed to {@link on} or {@link once} subscription methods
421
438
  */
422
- off(name: string, event: Function): void;
439
+ off(name: EventKeys<this>, event: Function): boolean;
423
440
  private _event;
424
441
  }
425
442
 
@@ -782,9 +799,12 @@ declare function objectFrom(value: any, params: {
782
799
  noEmptyString: boolean;
783
800
  }): any;
784
801
  /**
785
- * Returns entries from something iterable, such as a Map, Set, or Array
786
- * @param value iterable input
787
- * @returns An javascript object
802
+ * Returns entries from an iterable input like Map, Set, or Array.
803
+ *
804
+ * For all other types of values (including `null` or `undefined`), it returns an empty array.
805
+ *
806
+ * @param value An iterable input
807
+ * @returns An array of key-value pairs
788
808
  */
789
809
  declare function objectEntries(value: any): [string, any][];
790
810
  /**
package/dist/web-utils.js CHANGED
@@ -257,21 +257,28 @@ function objectFrom(value, params) {
257
257
  return obj;
258
258
  }
259
259
  /**
260
- * Returns entries from something iterable, such as a Map, Set, or Array
261
- * @param value iterable input
262
- * @returns An javascript object
260
+ * Returns entries from an iterable input like Map, Set, or Array.
261
+ *
262
+ * For all other types of values (including `null` or `undefined`), it returns an empty array.
263
+ *
264
+ * @param value An iterable input
265
+ * @returns An array of key-value pairs
263
266
  */
264
267
  function objectEntries(value) {
265
- if (value.entries) {
266
- return value.entries();
268
+ if (!value) {
269
+ return [];
267
270
  }
268
- return Array.from({
269
- [Symbol.iterator]: function* () {
270
- for (const key in value) {
271
- yield [key, value[key]];
272
- }
271
+ if (value.entries && typeof value.entries === 'function') {
272
+ value = value.entries();
273
+ if (Array.isArray(value)) {
274
+ return value;
273
275
  }
274
- });
276
+ }
277
+ const entries = [];
278
+ for (const key of Object.keys(value)) {
279
+ entries.push([key, value[key]]);
280
+ }
281
+ return entries;
275
282
  }
276
283
  /**
277
284
  * Converts various data types, such as objects, strings, exceptions, errors,
@@ -1188,6 +1195,7 @@ function defineMediaExt(type, params) {
1188
1195
  }
1189
1196
  /**
1190
1197
  * Build an URL from {@link Type | type} and {@link Params | params}
1198
+ * Can assign {@link Params.mediaExt | params.mediaExt} or {@link Params.streamName | params.streamName}
1191
1199
  * @param type Type of the connection wanted
1192
1200
  * @param params Connection parameters
1193
1201
  * @param protocol Optional parameter to choose the prefered protocol to connect
@@ -1218,12 +1226,20 @@ function buildURL(type, params, protocol = 'wss') {
1218
1226
  console.warn('Unknown url type ' + type);
1219
1227
  break;
1220
1228
  }
1221
- } // Host has already a path! keep it unchanged, it's user intentionnal (used with some other WHIP/WHEP server?)
1229
+ }
1230
+ else {
1231
+ // Host has already a path! keep it unchanged, it's user intentionnal (used with some other WHIP/WHEP server?)
1232
+ if (!params.streamName) {
1233
+ // extract the second part of the URL's path (the first part being the protocol name), or the first path if no other part exists
1234
+ const parts = url.pathname.split('/');
1235
+ params.streamName = parts[2] || parts[1] || parts[0];
1236
+ }
1237
+ }
1222
1238
  if (params.accessToken) {
1223
1239
  url.searchParams.set('id', params.accessToken);
1224
1240
  }
1225
- for (const key in params.query) {
1226
- url.searchParams.set(key, params.query[key]);
1241
+ for (const [key, value] of objectEntries(params.query)) {
1242
+ url.searchParams.set(key, value);
1227
1243
  }
1228
1244
  return url;
1229
1245
  }var Connect=/*#__PURE__*/Object.freeze({__proto__:null,get Type(){return Type},buildURL:buildURL,defineMediaExt:defineMediaExt});/**
@@ -1286,19 +1302,18 @@ class EventEmitter extends Loggable {
1286
1302
  if (name.length < 3 || !name.startsWith('on')) {
1287
1303
  continue;
1288
1304
  }
1289
- if (proto[name] instanceof Function) {
1305
+ let defaultEvent = proto[name];
1306
+ if (defaultEvent instanceof Function) {
1290
1307
  const events = new Set();
1291
1308
  this._events.set(name.substring(2).toLowerCase(), events);
1292
- let defaultEvent = proto[name];
1293
1309
  const raise = (...args) => {
1294
- // Call default event if not null (can happen in JS usage)
1295
- if (defaultEvent) {
1296
- defaultEvent.call(this, ...args);
1297
- }
1310
+ // Call default event if not undefined, can happen if assigned to null/undefined
1311
+ const result = defaultEvent ? defaultEvent.call(this, ...args) : undefined;
1298
1312
  // Call subscribers
1299
1313
  for (const event of events) {
1300
1314
  event(...args);
1301
1315
  }
1316
+ return result;
1302
1317
  };
1303
1318
  Object.defineProperties(this, {
1304
1319
  [name]: {
@@ -1322,8 +1337,8 @@ class EventEmitter extends Loggable {
1322
1337
  */
1323
1338
  on(name, event, options) {
1324
1339
  var _a;
1325
- if (!event) {
1326
- throw Error('event to subscribe cannot be null');
1340
+ if (typeof event !== 'function') {
1341
+ throw Error('event callback must be a function');
1327
1342
  }
1328
1343
  const events = this._event(name);
1329
1344
  events.add(event);
@@ -1337,8 +1352,8 @@ class EventEmitter extends Loggable {
1337
1352
  */
1338
1353
  once(name, event, options) {
1339
1354
  var _a;
1340
- if (!event) {
1341
- throw Error('event to subscribe cannot be null');
1355
+ if (typeof event !== 'function') {
1356
+ throw Error('event callback must be a function');
1342
1357
  }
1343
1358
  const events = this._event(name);
1344
1359
  events.add((...args) => {
@@ -1356,7 +1371,7 @@ class EventEmitter extends Loggable {
1356
1371
  if (!event) {
1357
1372
  throw Error('event to unsubscribe cannot be null');
1358
1373
  }
1359
- this._event(name).delete(event);
1374
+ return this._event(name).delete(event);
1360
1375
  }
1361
1376
  _event(name) {
1362
1377
  const events = this._events.get(name.toLowerCase());
@@ -2416,4 +2431,4 @@ class UIMetrics {
2416
2431
  * This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
2417
2432
  * See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
2418
2433
  */
2419
- const VERSION = '3.2.1';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
2434
+ const VERSION = '4.1.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