livekit-client 1.12.1 → 1.12.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. package/README.md +7 -3
  2. package/dist/livekit-client.e2ee.worker.js +1 -1
  3. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  4. package/dist/livekit-client.e2ee.worker.mjs +389 -301
  5. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  6. package/dist/livekit-client.esm.mjs +11279 -13498
  7. package/dist/livekit-client.esm.mjs.map +1 -1
  8. package/dist/livekit-client.umd.js +1 -1
  9. package/dist/livekit-client.umd.js.map +1 -1
  10. package/dist/src/api/SignalClient.d.ts +2 -2
  11. package/dist/src/api/SignalClient.d.ts.map +1 -1
  12. package/dist/src/connectionHelper/ConnectionCheck.d.ts +3 -2
  13. package/dist/src/connectionHelper/ConnectionCheck.d.ts.map +1 -1
  14. package/dist/src/connectionHelper/checks/Checker.d.ts +3 -2
  15. package/dist/src/connectionHelper/checks/Checker.d.ts.map +1 -1
  16. package/dist/src/connectionHelper/checks/webrtc.d.ts.map +1 -1
  17. package/dist/src/connectionHelper/checks/websocket.d.ts.map +1 -1
  18. package/dist/src/e2ee/E2eeManager.d.ts +4 -2
  19. package/dist/src/e2ee/E2eeManager.d.ts.map +1 -1
  20. package/dist/src/e2ee/KeyProvider.d.ts +4 -2
  21. package/dist/src/e2ee/KeyProvider.d.ts.map +1 -1
  22. package/dist/src/e2ee/constants.d.ts +1 -0
  23. package/dist/src/e2ee/constants.d.ts.map +1 -1
  24. package/dist/src/e2ee/types.d.ts +1 -0
  25. package/dist/src/e2ee/types.d.ts.map +1 -1
  26. package/dist/src/e2ee/worker/FrameCryptor.d.ts +4 -2
  27. package/dist/src/e2ee/worker/FrameCryptor.d.ts.map +1 -1
  28. package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts +14 -3
  29. package/dist/src/e2ee/worker/ParticipantKeyHandler.d.ts.map +1 -1
  30. package/dist/src/index.d.ts +1 -1
  31. package/dist/src/index.d.ts.map +1 -1
  32. package/dist/src/proto/livekit_models_pb.d.ts +1264 -0
  33. package/dist/src/proto/livekit_models_pb.d.ts.map +1 -0
  34. package/dist/src/proto/livekit_rtc_pb.d.ts +1373 -0
  35. package/dist/src/proto/livekit_rtc_pb.d.ts.map +1 -0
  36. package/dist/src/room/PCTransport.d.ts +2 -1
  37. package/dist/src/room/PCTransport.d.ts.map +1 -1
  38. package/dist/src/room/RTCEngine.d.ts +9 -5
  39. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  40. package/dist/src/room/RegionUrlProvider.d.ts +4 -1
  41. package/dist/src/room/RegionUrlProvider.d.ts.map +1 -1
  42. package/dist/src/room/Room.d.ts +15 -11
  43. package/dist/src/room/Room.d.ts.map +1 -1
  44. package/dist/src/room/participant/LocalParticipant.d.ts +2 -2
  45. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  46. package/dist/src/room/participant/Participant.d.ts +5 -3
  47. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  48. package/dist/src/room/participant/ParticipantTrackPermission.d.ts +1 -1
  49. package/dist/src/room/participant/ParticipantTrackPermission.d.ts.map +1 -1
  50. package/dist/src/room/participant/RemoteParticipant.d.ts +2 -3
  51. package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
  52. package/dist/src/room/participant/publishUtils.d.ts +1 -1
  53. package/dist/src/room/participant/publishUtils.d.ts.map +1 -1
  54. package/dist/src/room/timers.d.ts +5 -4
  55. package/dist/src/room/timers.d.ts.map +1 -1
  56. package/dist/src/room/track/LocalTrack.d.ts +3 -0
  57. package/dist/src/room/track/LocalTrack.d.ts.map +1 -1
  58. package/dist/src/room/track/LocalTrackPublication.d.ts +1 -1
  59. package/dist/src/room/track/LocalTrackPublication.d.ts.map +1 -1
  60. package/dist/src/room/track/LocalVideoTrack.d.ts +2 -2
  61. package/dist/src/room/track/LocalVideoTrack.d.ts.map +1 -1
  62. package/dist/src/room/track/RemoteTrackPublication.d.ts +1 -1
  63. package/dist/src/room/track/RemoteTrackPublication.d.ts.map +1 -1
  64. package/dist/src/room/track/Track.d.ts +6 -4
  65. package/dist/src/room/track/Track.d.ts.map +1 -1
  66. package/dist/src/room/track/TrackPublication.d.ts +7 -5
  67. package/dist/src/room/track/TrackPublication.d.ts.map +1 -1
  68. package/dist/src/room/track/create.d.ts.map +1 -1
  69. package/dist/src/room/track/options.d.ts +8 -0
  70. package/dist/src/room/track/options.d.ts.map +1 -1
  71. package/dist/src/room/track/utils.d.ts +5 -1
  72. package/dist/src/room/track/utils.d.ts.map +1 -1
  73. package/dist/src/room/utils.d.ts +3 -1
  74. package/dist/src/room/utils.d.ts.map +1 -1
  75. package/dist/src/test/mocks.d.ts +4 -3
  76. package/dist/src/test/mocks.d.ts.map +1 -1
  77. package/dist/ts4.2/src/api/SignalClient.d.ts +2 -2
  78. package/dist/ts4.2/src/connectionHelper/ConnectionCheck.d.ts +3 -2
  79. package/dist/ts4.2/src/connectionHelper/checks/Checker.d.ts +3 -2
  80. package/dist/ts4.2/src/e2ee/E2eeManager.d.ts +4 -2
  81. package/dist/ts4.2/src/e2ee/KeyProvider.d.ts +4 -2
  82. package/dist/ts4.2/src/e2ee/constants.d.ts +1 -0
  83. package/dist/ts4.2/src/e2ee/types.d.ts +1 -0
  84. package/dist/ts4.2/src/e2ee/worker/FrameCryptor.d.ts +4 -2
  85. package/dist/ts4.2/src/e2ee/worker/ParticipantKeyHandler.d.ts +14 -3
  86. package/dist/ts4.2/src/index.d.ts +1 -1
  87. package/dist/ts4.2/src/proto/livekit_models_pb.d.ts +1264 -0
  88. package/dist/ts4.2/src/proto/livekit_rtc_pb.d.ts +1373 -0
  89. package/dist/ts4.2/src/room/PCTransport.d.ts +2 -1
  90. package/dist/ts4.2/src/room/RTCEngine.d.ts +9 -5
  91. package/dist/ts4.2/src/room/RegionUrlProvider.d.ts +4 -1
  92. package/dist/ts4.2/src/room/Room.d.ts +15 -11
  93. package/dist/ts4.2/src/room/participant/LocalParticipant.d.ts +2 -2
  94. package/dist/ts4.2/src/room/participant/Participant.d.ts +5 -3
  95. package/dist/ts4.2/src/room/participant/ParticipantTrackPermission.d.ts +1 -1
  96. package/dist/ts4.2/src/room/participant/RemoteParticipant.d.ts +2 -3
  97. package/dist/ts4.2/src/room/participant/publishUtils.d.ts +1 -1
  98. package/dist/ts4.2/src/room/timers.d.ts +5 -4
  99. package/dist/ts4.2/src/room/track/LocalTrack.d.ts +3 -0
  100. package/dist/ts4.2/src/room/track/LocalTrackPublication.d.ts +1 -1
  101. package/dist/ts4.2/src/room/track/LocalVideoTrack.d.ts +2 -2
  102. package/dist/ts4.2/src/room/track/RemoteTrackPublication.d.ts +1 -1
  103. package/dist/ts4.2/src/room/track/Track.d.ts +6 -4
  104. package/dist/ts4.2/src/room/track/TrackPublication.d.ts +7 -5
  105. package/dist/ts4.2/src/room/track/options.d.ts +8 -0
  106. package/dist/ts4.2/src/room/track/utils.d.ts +5 -1
  107. package/dist/ts4.2/src/room/utils.d.ts +3 -1
  108. package/dist/ts4.2/src/test/mocks.d.ts +4 -3
  109. package/package.json +10 -10
  110. package/src/api/SignalClient.ts +104 -101
  111. package/src/connectionHelper/ConnectionCheck.ts +3 -2
  112. package/src/connectionHelper/checks/Checker.ts +3 -3
  113. package/src/connectionHelper/checks/webrtc.ts +66 -2
  114. package/src/connectionHelper/checks/websocket.ts +4 -0
  115. package/src/e2ee/E2eeManager.ts +4 -3
  116. package/src/e2ee/KeyProvider.ts +3 -2
  117. package/src/e2ee/constants.ts +4 -0
  118. package/src/e2ee/types.ts +1 -0
  119. package/src/e2ee/worker/FrameCryptor.test.ts +1 -3
  120. package/src/e2ee/worker/FrameCryptor.ts +5 -5
  121. package/src/e2ee/worker/ParticipantKeyHandler.ts +37 -6
  122. package/src/e2ee/worker/e2ee.worker.ts +1 -1
  123. package/src/index.ts +1 -1
  124. package/src/proto/livekit_models_pb.ts +2096 -0
  125. package/src/proto/livekit_rtc_pb.ts +2332 -0
  126. package/src/room/PCTransport.ts +1 -1
  127. package/src/room/RTCEngine.ts +28 -22
  128. package/src/room/RegionUrlProvider.ts +11 -2
  129. package/src/room/Room.test.ts +1 -0
  130. package/src/room/Room.ts +158 -79
  131. package/src/room/participant/LocalParticipant.ts +43 -59
  132. package/src/room/participant/Participant.ts +6 -4
  133. package/src/room/participant/ParticipantTrackPermission.ts +3 -3
  134. package/src/room/participant/RemoteParticipant.ts +5 -6
  135. package/src/room/participant/publishUtils.test.ts +1 -0
  136. package/src/room/participant/publishUtils.ts +4 -2
  137. package/src/room/track/LocalTrack.ts +24 -9
  138. package/src/room/track/LocalTrackPublication.ts +1 -1
  139. package/src/room/track/LocalVideoTrack.test.ts +2 -1
  140. package/src/room/track/LocalVideoTrack.ts +28 -26
  141. package/src/room/track/RemoteTrackPublication.ts +12 -7
  142. package/src/room/track/RemoteVideoTrack.test.ts +5 -4
  143. package/src/room/track/Track.ts +9 -6
  144. package/src/room/track/TrackPublication.ts +7 -5
  145. package/src/room/track/create.ts +9 -17
  146. package/src/room/track/facingMode.test.ts +1 -0
  147. package/src/room/track/options.ts +23 -16
  148. package/src/room/track/utils.test.ts +1 -0
  149. package/src/room/track/utils.ts +44 -2
  150. package/src/room/utils.test.ts +16 -0
  151. package/src/room/utils.ts +20 -4
  152. package/src/test/mocks.ts +7 -5
  153. package/src/utils/AsyncQueue.test.ts +1 -0
  154. package/src/utils/browserParser.test.ts +33 -3
  155. package/src/utils/browserParser.ts +1 -1
  156. package/dist/src/proto/google/protobuf/timestamp.d.ts +0 -146
  157. package/dist/src/proto/google/protobuf/timestamp.d.ts.map +0 -1
  158. package/dist/src/proto/livekit_models.d.ts +0 -2399
  159. package/dist/src/proto/livekit_models.d.ts.map +0 -1
  160. package/dist/src/proto/livekit_rtc.d.ts +0 -14352
  161. package/dist/src/proto/livekit_rtc.d.ts.map +0 -1
  162. package/dist/ts4.2/src/proto/google/protobuf/timestamp.d.ts +0 -150
  163. package/dist/ts4.2/src/proto/livekit_models.d.ts +0 -2659
  164. package/dist/ts4.2/src/proto/livekit_rtc.d.ts +0 -15764
  165. package/src/proto/google/protobuf/timestamp.ts +0 -230
  166. package/src/proto/livekit_models.ts +0 -4006
  167. package/src/proto/livekit_rtc.ts +0 -4672
@@ -27,10 +27,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
27
27
 
28
28
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
29
29
 
30
- function getDefaultExportFromCjs (x) {
31
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
32
- }
33
-
34
30
  var loglevel = {exports: {}};
35
31
 
36
32
  /*
@@ -317,6 +313,8 @@ const ENCRYPTION_ALGORITHM = 'AES-GCM';
317
313
  // encrypted with an old key. We use a size of 16 which corresponds to the four bits
318
314
  // in the frame trailer.
319
315
  const KEYRING_SIZE = 16;
316
+ // How many consecutive frames can fail decrypting before a particular key gets marked as invalid
317
+ const DECRYPTION_FAILURE_TOLERANCE = 10;
320
318
  // We copy the first bytes of the VP8 payload unencrypted.
321
319
  // For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
322
320
  // https://tools.ietf.org/html/rfc6386#section-9.1
@@ -340,7 +338,8 @@ const SALT = 'LKFrameEncryptionKey';
340
338
  const KEY_PROVIDER_DEFAULTS = {
341
339
  sharedKey: false,
342
340
  ratchetSalt: SALT,
343
- ratchetWindowSize: 8
341
+ ratchetWindowSize: 8,
342
+ failureTolerance: DECRYPTION_FAILURE_TOLERANCE
344
343
  };
345
344
 
346
345
  class LivekitError extends Error {
@@ -391,322 +390,386 @@ class CryptorError extends LivekitError {
391
390
  }
392
391
  }
393
392
 
394
- var eventemitter3 = {exports: {}};
395
-
396
- (function (module) {
397
-
398
- var has = Object.prototype.hasOwnProperty,
399
- prefix = '~';
393
+ var events = {exports: {}};
400
394
 
401
- /**
402
- * Constructor to create a storage for our `EE` objects.
403
- * An `Events` instance is a plain object whose properties are event names.
404
- *
405
- * @constructor
406
- * @private
407
- */
408
- function Events() {}
395
+ var R = typeof Reflect === 'object' ? Reflect : null;
396
+ var ReflectApply = R && typeof R.apply === 'function' ? R.apply : function ReflectApply(target, receiver, args) {
397
+ return Function.prototype.apply.call(target, receiver, args);
398
+ };
399
+ var ReflectOwnKeys;
400
+ if (R && typeof R.ownKeys === 'function') {
401
+ ReflectOwnKeys = R.ownKeys;
402
+ } else if (Object.getOwnPropertySymbols) {
403
+ ReflectOwnKeys = function ReflectOwnKeys(target) {
404
+ return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));
405
+ };
406
+ } else {
407
+ ReflectOwnKeys = function ReflectOwnKeys(target) {
408
+ return Object.getOwnPropertyNames(target);
409
+ };
410
+ }
411
+ function ProcessEmitWarning(warning) {
412
+ if (console && console.warn) console.warn(warning);
413
+ }
414
+ var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
415
+ return value !== value;
416
+ };
417
+ function EventEmitter() {
418
+ EventEmitter.init.call(this);
419
+ }
420
+ events.exports = EventEmitter;
421
+ events.exports.once = once;
409
422
 
410
- //
411
- // We try to not inherit from `Object.prototype`. In some engines creating an
412
- // instance in this way is faster than calling `Object.create(null)` directly.
413
- // If `Object.create(null)` is not supported we prefix the event names with a
414
- // character to make sure that the built-in object properties are not
415
- // overridden or used as an attack vector.
416
- //
417
- if (Object.create) {
418
- Events.prototype = Object.create(null);
423
+ // Backwards-compat with node 0.10.x
424
+ EventEmitter.EventEmitter = EventEmitter;
425
+ EventEmitter.prototype._events = undefined;
426
+ EventEmitter.prototype._eventsCount = 0;
427
+ EventEmitter.prototype._maxListeners = undefined;
419
428
 
420
- //
421
- // This hack is needed because the `__proto__` property is still inherited in
422
- // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
423
- //
424
- if (!new Events().__proto__) prefix = false;
429
+ // By default EventEmitters will print a warning if more than 10 listeners are
430
+ // added to it. This is a useful default which helps finding memory leaks.
431
+ var defaultMaxListeners = 10;
432
+ function checkListener(listener) {
433
+ if (typeof listener !== 'function') {
434
+ throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
425
435
  }
426
-
427
- /**
428
- * Representation of a single event listener.
429
- *
430
- * @param {Function} fn The listener function.
431
- * @param {*} context The context to invoke the listener with.
432
- * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
433
- * @constructor
434
- * @private
435
- */
436
- function EE(fn, context, once) {
437
- this.fn = fn;
438
- this.context = context;
439
- this.once = once || false;
436
+ }
437
+ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
438
+ enumerable: true,
439
+ get: function () {
440
+ return defaultMaxListeners;
441
+ },
442
+ set: function (arg) {
443
+ if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
444
+ throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
445
+ }
446
+ defaultMaxListeners = arg;
447
+ }
448
+ });
449
+ EventEmitter.init = function () {
450
+ if (this._events === undefined || this._events === Object.getPrototypeOf(this)._events) {
451
+ this._events = Object.create(null);
452
+ this._eventsCount = 0;
440
453
  }
454
+ this._maxListeners = this._maxListeners || undefined;
455
+ };
441
456
 
442
- /**
443
- * Add a listener for a given event.
444
- *
445
- * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
446
- * @param {(String|Symbol)} event The event name.
447
- * @param {Function} fn The listener function.
448
- * @param {*} context The context to invoke the listener with.
449
- * @param {Boolean} once Specify if the listener is a one-time listener.
450
- * @returns {EventEmitter}
451
- * @private
452
- */
453
- function addListener(emitter, event, fn, context, once) {
454
- if (typeof fn !== 'function') {
455
- throw new TypeError('The listener must be a function');
456
- }
457
- var listener = new EE(fn, context || emitter, once),
458
- evt = prefix ? prefix + event : event;
459
- if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);else emitter._events[evt] = [emitter._events[evt], listener];
460
- return emitter;
457
+ // Obviously not all Emitters should be limited to 10. This function allows
458
+ // that to be increased. Set to zero for unlimited.
459
+ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
460
+ if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
461
+ throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
461
462
  }
463
+ this._maxListeners = n;
464
+ return this;
465
+ };
466
+ function _getMaxListeners(that) {
467
+ if (that._maxListeners === undefined) return EventEmitter.defaultMaxListeners;
468
+ return that._maxListeners;
469
+ }
470
+ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
471
+ return _getMaxListeners(this);
472
+ };
473
+ EventEmitter.prototype.emit = function emit(type) {
474
+ var args = [];
475
+ for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
476
+ var doError = type === 'error';
477
+ var events = this._events;
478
+ if (events !== undefined) doError = doError && events.error === undefined;else if (!doError) return false;
462
479
 
463
- /**
464
- * Clear event by name.
465
- *
466
- * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
467
- * @param {(String|Symbol)} evt The Event name.
468
- * @private
469
- */
470
- function clearEvent(emitter, evt) {
471
- if (--emitter._eventsCount === 0) emitter._events = new Events();else delete emitter._events[evt];
480
+ // If there is no 'error' event listener then throw.
481
+ if (doError) {
482
+ var er;
483
+ if (args.length > 0) er = args[0];
484
+ if (er instanceof Error) {
485
+ // Note: The comments on the `throw` lines are intentional, they show
486
+ // up in Node's output if this results in an unhandled exception.
487
+ throw er; // Unhandled 'error' event
488
+ }
489
+ // At least give some kind of context to the user
490
+ var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
491
+ err.context = er;
492
+ throw err; // Unhandled 'error' event
472
493
  }
473
494
 
474
- /**
475
- * Minimal `EventEmitter` interface that is molded against the Node.js
476
- * `EventEmitter` interface.
477
- *
478
- * @constructor
479
- * @public
480
- */
481
- function EventEmitter() {
482
- this._events = new Events();
483
- this._eventsCount = 0;
495
+ var handler = events[type];
496
+ if (handler === undefined) return false;
497
+ if (typeof handler === 'function') {
498
+ ReflectApply(handler, this, args);
499
+ } else {
500
+ var len = handler.length;
501
+ var listeners = arrayClone(handler, len);
502
+ for (var i = 0; i < len; ++i) ReflectApply(listeners[i], this, args);
484
503
  }
504
+ return true;
505
+ };
506
+ function _addListener(target, type, listener, prepend) {
507
+ var m;
508
+ var events;
509
+ var existing;
510
+ checkListener(listener);
511
+ events = target._events;
512
+ if (events === undefined) {
513
+ events = target._events = Object.create(null);
514
+ target._eventsCount = 0;
515
+ } else {
516
+ // To avoid recursion in the case that type === "newListener"! Before
517
+ // adding it to the listeners, first emit "newListener".
518
+ if (events.newListener !== undefined) {
519
+ target.emit('newListener', type, listener.listener ? listener.listener : listener);
485
520
 
486
- /**
487
- * Return an array listing the events for which the emitter has registered
488
- * listeners.
489
- *
490
- * @returns {Array}
491
- * @public
492
- */
493
- EventEmitter.prototype.eventNames = function eventNames() {
494
- var names = [],
495
- events,
496
- name;
497
- if (this._eventsCount === 0) return names;
498
- for (name in events = this._events) {
499
- if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
521
+ // Re-assign `events` because a newListener handler could have caused the
522
+ // this._events to be assigned to a new object
523
+ events = target._events;
500
524
  }
501
- if (Object.getOwnPropertySymbols) {
502
- return names.concat(Object.getOwnPropertySymbols(events));
525
+ existing = events[type];
526
+ }
527
+ if (existing === undefined) {
528
+ // Optimize the case of one listener. Don't need the extra array object.
529
+ existing = events[type] = listener;
530
+ ++target._eventsCount;
531
+ } else {
532
+ if (typeof existing === 'function') {
533
+ // Adding the second element, need to change to array.
534
+ existing = events[type] = prepend ? [listener, existing] : [existing, listener];
535
+ // If we've already got an array, just append.
536
+ } else if (prepend) {
537
+ existing.unshift(listener);
538
+ } else {
539
+ existing.push(listener);
503
540
  }
504
- return names;
505
- };
506
541
 
507
- /**
508
- * Return the listeners registered for a given event.
509
- *
510
- * @param {(String|Symbol)} event The event name.
511
- * @returns {Array} The registered listeners.
512
- * @public
513
- */
514
- EventEmitter.prototype.listeners = function listeners(event) {
515
- var evt = prefix ? prefix + event : event,
516
- handlers = this._events[evt];
517
- if (!handlers) return [];
518
- if (handlers.fn) return [handlers.fn];
519
- for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
520
- ee[i] = handlers[i].fn;
542
+ // Check for listener leak
543
+ m = _getMaxListeners(target);
544
+ if (m > 0 && existing.length > m && !existing.warned) {
545
+ existing.warned = true;
546
+ // No error code for this since it is a Warning
547
+ // eslint-disable-next-line no-restricted-syntax
548
+ var w = new Error('Possible EventEmitter memory leak detected. ' + existing.length + ' ' + String(type) + ' listeners ' + 'added. Use emitter.setMaxListeners() to ' + 'increase limit');
549
+ w.name = 'MaxListenersExceededWarning';
550
+ w.emitter = target;
551
+ w.type = type;
552
+ w.count = existing.length;
553
+ ProcessEmitWarning(w);
521
554
  }
522
- return ee;
523
- };
524
-
525
- /**
526
- * Return the number of listeners listening to a given event.
527
- *
528
- * @param {(String|Symbol)} event The event name.
529
- * @returns {Number} The number of listeners.
530
- * @public
531
- */
532
- EventEmitter.prototype.listenerCount = function listenerCount(event) {
533
- var evt = prefix ? prefix + event : event,
534
- listeners = this._events[evt];
535
- if (!listeners) return 0;
536
- if (listeners.fn) return 1;
537
- return listeners.length;
555
+ }
556
+ return target;
557
+ }
558
+ EventEmitter.prototype.addListener = function addListener(type, listener) {
559
+ return _addListener(this, type, listener, false);
560
+ };
561
+ EventEmitter.prototype.on = EventEmitter.prototype.addListener;
562
+ EventEmitter.prototype.prependListener = function prependListener(type, listener) {
563
+ return _addListener(this, type, listener, true);
564
+ };
565
+ function onceWrapper() {
566
+ if (!this.fired) {
567
+ this.target.removeListener(this.type, this.wrapFn);
568
+ this.fired = true;
569
+ if (arguments.length === 0) return this.listener.call(this.target);
570
+ return this.listener.apply(this.target, arguments);
571
+ }
572
+ }
573
+ function _onceWrap(target, type, listener) {
574
+ var state = {
575
+ fired: false,
576
+ wrapFn: undefined,
577
+ target: target,
578
+ type: type,
579
+ listener: listener
538
580
  };
581
+ var wrapped = onceWrapper.bind(state);
582
+ wrapped.listener = listener;
583
+ state.wrapFn = wrapped;
584
+ return wrapped;
585
+ }
586
+ EventEmitter.prototype.once = function once(type, listener) {
587
+ checkListener(listener);
588
+ this.on(type, _onceWrap(this, type, listener));
589
+ return this;
590
+ };
591
+ EventEmitter.prototype.prependOnceListener = function prependOnceListener(type, listener) {
592
+ checkListener(listener);
593
+ this.prependListener(type, _onceWrap(this, type, listener));
594
+ return this;
595
+ };
539
596
 
540
- /**
541
- * Calls each of the listeners registered for a given event.
542
- *
543
- * @param {(String|Symbol)} event The event name.
544
- * @returns {Boolean} `true` if the event had listeners, else `false`.
545
- * @public
546
- */
547
- EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
548
- var evt = prefix ? prefix + event : event;
549
- if (!this._events[evt]) return false;
550
- var listeners = this._events[evt],
551
- len = arguments.length,
552
- args,
553
- i;
554
- if (listeners.fn) {
555
- if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
556
- switch (len) {
557
- case 1:
558
- return listeners.fn.call(listeners.context), true;
559
- case 2:
560
- return listeners.fn.call(listeners.context, a1), true;
561
- case 3:
562
- return listeners.fn.call(listeners.context, a1, a2), true;
563
- case 4:
564
- return listeners.fn.call(listeners.context, a1, a2, a3), true;
565
- case 5:
566
- return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
567
- case 6:
568
- return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
569
- }
570
- for (i = 1, args = new Array(len - 1); i < len; i++) {
571
- args[i - 1] = arguments[i];
572
- }
573
- listeners.fn.apply(listeners.context, args);
574
- } else {
575
- var length = listeners.length,
576
- j;
577
- for (i = 0; i < length; i++) {
578
- if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
579
- switch (len) {
580
- case 1:
581
- listeners[i].fn.call(listeners[i].context);
582
- break;
583
- case 2:
584
- listeners[i].fn.call(listeners[i].context, a1);
585
- break;
586
- case 3:
587
- listeners[i].fn.call(listeners[i].context, a1, a2);
588
- break;
589
- case 4:
590
- listeners[i].fn.call(listeners[i].context, a1, a2, a3);
591
- break;
592
- default:
593
- if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) {
594
- args[j - 1] = arguments[j];
595
- }
596
- listeners[i].fn.apply(listeners[i].context, args);
597
- }
597
+ // Emits a 'removeListener' event if and only if the listener was removed.
598
+ EventEmitter.prototype.removeListener = function removeListener(type, listener) {
599
+ var list, events, position, i, originalListener;
600
+ checkListener(listener);
601
+ events = this._events;
602
+ if (events === undefined) return this;
603
+ list = events[type];
604
+ if (list === undefined) return this;
605
+ if (list === listener || list.listener === listener) {
606
+ if (--this._eventsCount === 0) this._events = Object.create(null);else {
607
+ delete events[type];
608
+ if (events.removeListener) this.emit('removeListener', type, list.listener || listener);
609
+ }
610
+ } else if (typeof list !== 'function') {
611
+ position = -1;
612
+ for (i = list.length - 1; i >= 0; i--) {
613
+ if (list[i] === listener || list[i].listener === listener) {
614
+ originalListener = list[i].listener;
615
+ position = i;
616
+ break;
598
617
  }
599
618
  }
600
- return true;
601
- };
602
-
603
- /**
604
- * Add a listener for a given event.
605
- *
606
- * @param {(String|Symbol)} event The event name.
607
- * @param {Function} fn The listener function.
608
- * @param {*} [context=this] The context to invoke the listener with.
609
- * @returns {EventEmitter} `this`.
610
- * @public
611
- */
612
- EventEmitter.prototype.on = function on(event, fn, context) {
613
- return addListener(this, event, fn, context, false);
614
- };
615
-
616
- /**
617
- * Add a one-time listener for a given event.
618
- *
619
- * @param {(String|Symbol)} event The event name.
620
- * @param {Function} fn The listener function.
621
- * @param {*} [context=this] The context to invoke the listener with.
622
- * @returns {EventEmitter} `this`.
623
- * @public
624
- */
625
- EventEmitter.prototype.once = function once(event, fn, context) {
626
- return addListener(this, event, fn, context, true);
627
- };
628
-
629
- /**
630
- * Remove the listeners of a given event.
631
- *
632
- * @param {(String|Symbol)} event The event name.
633
- * @param {Function} fn Only remove the listeners that match this function.
634
- * @param {*} context Only remove the listeners that have this context.
635
- * @param {Boolean} once Only remove one-time listeners.
636
- * @returns {EventEmitter} `this`.
637
- * @public
638
- */
639
- EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
640
- var evt = prefix ? prefix + event : event;
641
- if (!this._events[evt]) return this;
642
- if (!fn) {
643
- clearEvent(this, evt);
644
- return this;
619
+ if (position < 0) return this;
620
+ if (position === 0) list.shift();else {
621
+ spliceOne(list, position);
645
622
  }
646
- var listeners = this._events[evt];
647
- if (listeners.fn) {
648
- if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
649
- clearEvent(this, evt);
650
- }
651
- } else {
652
- for (var i = 0, events = [], length = listeners.length; i < length; i++) {
653
- if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
654
- events.push(listeners[i]);
655
- }
656
- }
623
+ if (list.length === 1) events[type] = list[0];
624
+ if (events.removeListener !== undefined) this.emit('removeListener', type, originalListener || listener);
625
+ }
626
+ return this;
627
+ };
628
+ EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
629
+ EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) {
630
+ var listeners, events, i;
631
+ events = this._events;
632
+ if (events === undefined) return this;
657
633
 
658
- //
659
- // Reset the array, or remove it completely if we have no more listeners.
660
- //
661
- if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;else clearEvent(this, evt);
634
+ // not listening for removeListener, no need to emit
635
+ if (events.removeListener === undefined) {
636
+ if (arguments.length === 0) {
637
+ this._events = Object.create(null);
638
+ this._eventsCount = 0;
639
+ } else if (events[type] !== undefined) {
640
+ if (--this._eventsCount === 0) this._events = Object.create(null);else delete events[type];
662
641
  }
663
642
  return this;
664
- };
643
+ }
665
644
 
666
- /**
667
- * Remove all listeners, or those of the specified event.
668
- *
669
- * @param {(String|Symbol)} [event] The event name.
670
- * @returns {EventEmitter} `this`.
671
- * @public
672
- */
673
- EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
674
- var evt;
675
- if (event) {
676
- evt = prefix ? prefix + event : event;
677
- if (this._events[evt]) clearEvent(this, evt);
678
- } else {
679
- this._events = new Events();
680
- this._eventsCount = 0;
645
+ // emit removeListener for all listeners on all events
646
+ if (arguments.length === 0) {
647
+ var keys = Object.keys(events);
648
+ var key;
649
+ for (i = 0; i < keys.length; ++i) {
650
+ key = keys[i];
651
+ if (key === 'removeListener') continue;
652
+ this.removeAllListeners(key);
681
653
  }
654
+ this.removeAllListeners('removeListener');
655
+ this._events = Object.create(null);
656
+ this._eventsCount = 0;
682
657
  return this;
683
- };
684
-
685
- //
686
- // Alias methods names because people roll like that.
687
- //
688
- EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
689
- EventEmitter.prototype.addListener = EventEmitter.prototype.on;
690
-
691
- //
692
- // Expose the prefix.
693
- //
694
- EventEmitter.prefixed = prefix;
695
-
696
- //
697
- // Allow `EventEmitter` to be imported as module namespace.
698
- //
699
- EventEmitter.EventEmitter = EventEmitter;
700
-
701
- //
702
- // Expose the module.
703
- //
704
- {
705
- module.exports = EventEmitter;
706
658
  }
707
- })(eventemitter3);
708
- var eventemitter3Exports = eventemitter3.exports;
709
- var EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
659
+ listeners = events[type];
660
+ if (typeof listeners === 'function') {
661
+ this.removeListener(type, listeners);
662
+ } else if (listeners !== undefined) {
663
+ // LIFO order
664
+ for (i = listeners.length - 1; i >= 0; i--) {
665
+ this.removeListener(type, listeners[i]);
666
+ }
667
+ }
668
+ return this;
669
+ };
670
+ function _listeners(target, type, unwrap) {
671
+ var events = target._events;
672
+ if (events === undefined) return [];
673
+ var evlistener = events[type];
674
+ if (evlistener === undefined) return [];
675
+ if (typeof evlistener === 'function') return unwrap ? [evlistener.listener || evlistener] : [evlistener];
676
+ return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
677
+ }
678
+ EventEmitter.prototype.listeners = function listeners(type) {
679
+ return _listeners(this, type, true);
680
+ };
681
+ EventEmitter.prototype.rawListeners = function rawListeners(type) {
682
+ return _listeners(this, type, false);
683
+ };
684
+ EventEmitter.listenerCount = function (emitter, type) {
685
+ if (typeof emitter.listenerCount === 'function') {
686
+ return emitter.listenerCount(type);
687
+ } else {
688
+ return listenerCount.call(emitter, type);
689
+ }
690
+ };
691
+ EventEmitter.prototype.listenerCount = listenerCount;
692
+ function listenerCount(type) {
693
+ var events = this._events;
694
+ if (events !== undefined) {
695
+ var evlistener = events[type];
696
+ if (typeof evlistener === 'function') {
697
+ return 1;
698
+ } else if (evlistener !== undefined) {
699
+ return evlistener.length;
700
+ }
701
+ }
702
+ return 0;
703
+ }
704
+ EventEmitter.prototype.eventNames = function eventNames() {
705
+ return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
706
+ };
707
+ function arrayClone(arr, n) {
708
+ var copy = new Array(n);
709
+ for (var i = 0; i < n; ++i) copy[i] = arr[i];
710
+ return copy;
711
+ }
712
+ function spliceOne(list, index) {
713
+ for (; index + 1 < list.length; index++) list[index] = list[index + 1];
714
+ list.pop();
715
+ }
716
+ function unwrapListeners(arr) {
717
+ var ret = new Array(arr.length);
718
+ for (var i = 0; i < ret.length; ++i) {
719
+ ret[i] = arr[i].listener || arr[i];
720
+ }
721
+ return ret;
722
+ }
723
+ function once(emitter, name) {
724
+ return new Promise(function (resolve, reject) {
725
+ function errorListener(err) {
726
+ emitter.removeListener(name, resolver);
727
+ reject(err);
728
+ }
729
+ function resolver() {
730
+ if (typeof emitter.removeListener === 'function') {
731
+ emitter.removeListener('error', errorListener);
732
+ }
733
+ resolve([].slice.call(arguments));
734
+ }
735
+ eventTargetAgnosticAddListener(emitter, name, resolver, {
736
+ once: true
737
+ });
738
+ if (name !== 'error') {
739
+ addErrorHandlerIfEventEmitter(emitter, errorListener, {
740
+ once: true
741
+ });
742
+ }
743
+ });
744
+ }
745
+ function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
746
+ if (typeof emitter.on === 'function') {
747
+ eventTargetAgnosticAddListener(emitter, 'error', handler, flags);
748
+ }
749
+ }
750
+ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
751
+ if (typeof emitter.on === 'function') {
752
+ if (flags.once) {
753
+ emitter.once(name, listener);
754
+ } else {
755
+ emitter.on(name, listener);
756
+ }
757
+ } else if (typeof emitter.addEventListener === 'function') {
758
+ // EventTarget does not have `error` event semantics like Node
759
+ // EventEmitters, we do not listen for `error` events here.
760
+ emitter.addEventListener(name, function wrapListener(arg) {
761
+ // IE does not have builtin `{ once: true }` support so we
762
+ // have to do it manually.
763
+ if (flags.once) {
764
+ emitter.removeEventListener(name, wrapListener);
765
+ }
766
+ listener(arg);
767
+ });
768
+ } else {
769
+ throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter);
770
+ }
771
+ }
772
+ var eventsExports = events.exports;
710
773
 
711
774
  const CryptorEvent = {
712
775
  Error: 'cryptorError'
@@ -802,7 +865,7 @@ function ratchet(material, salt) {
802
865
  });
803
866
  }
804
867
 
805
- class BaseFrameCryptor extends EventEmitter {
868
+ class BaseFrameCryptor extends eventsExports.EventEmitter {
806
869
  encodeFunction(encodedFrame, controller) {
807
870
  throw Error('not implemented for subclass');
808
871
  }
@@ -965,6 +1028,7 @@ class FrameCryptor extends BaseFrameCryptor {
965
1028
  if (this.keys.getKeySet(keyIndex) && this.keys.hasValidKey) {
966
1029
  try {
967
1030
  const decodedFrame = yield this.decryptFrame(encodedFrame, keyIndex);
1031
+ this.keys.decryptionSuccess();
968
1032
  if (decodedFrame) {
969
1033
  return controller.enqueue(decodedFrame);
970
1034
  }
@@ -973,7 +1037,7 @@ class FrameCryptor extends BaseFrameCryptor {
973
1037
  if (this.keys.hasValidKey) {
974
1038
  workerLogger.warn('invalid key');
975
1039
  this.emit(CryptorEvent.Error, new CryptorError("invalid key for participant ".concat(this.participantId), CryptorErrorReason.InvalidKey));
976
- this.keys.hasValidKey = false;
1040
+ this.keys.decryptionFailure();
977
1041
  }
978
1042
  } else {
979
1043
  workerLogger.warn('decoding frame failed', {
@@ -1054,7 +1118,6 @@ class FrameCryptor extends BaseFrameCryptor {
1054
1118
  workerLogger.debug('resetting to initial material');
1055
1119
  this.keys.setKeyFromMaterial(initialMaterial.material, keyIndex);
1056
1120
  }
1057
- this.keys.hasValidKey = false;
1058
1121
  workerLogger.warn('maximum ratchet attempts exceeded, resetting key');
1059
1122
  this.emit(CryptorEvent.Error, new CryptorError("valid key missing for participant ".concat(this.participantId), CryptorErrorReason.MissingKey));
1060
1123
  }
@@ -1242,20 +1305,45 @@ function isFrameServerInjected(frameData, trailerBytes) {
1242
1305
  * if decryption fails or can be triggered manually on both sender and receiver side.
1243
1306
  *
1244
1307
  */
1245
- class ParticipantKeyHandler extends EventEmitter {
1308
+ class ParticipantKeyHandler extends eventsExports.EventEmitter {
1309
+ get hasValidKey() {
1310
+ return this._hasValidKey;
1311
+ }
1246
1312
  constructor(participantId, isEnabled, keyProviderOptions) {
1247
1313
  super();
1314
+ this.decryptionFailureCount = 0;
1315
+ this._hasValidKey = true;
1248
1316
  this.currentKeyIndex = 0;
1249
1317
  this.cryptoKeyRing = new Array(KEYRING_SIZE);
1250
1318
  this.enabled = isEnabled;
1251
1319
  this.keyProviderOptions = keyProviderOptions;
1252
1320
  this.ratchetPromiseMap = new Map();
1253
1321
  this.participantId = participantId;
1254
- this.hasValidKey = false;
1322
+ this.resetKeyStatus();
1255
1323
  }
1256
1324
  setEnabled(enabled) {
1257
1325
  this.enabled = enabled;
1258
1326
  }
1327
+ decryptionFailure() {
1328
+ if (this.keyProviderOptions.failureTolerance < 0) {
1329
+ return;
1330
+ }
1331
+ this.decryptionFailureCount += 1;
1332
+ if (this.decryptionFailureCount > this.keyProviderOptions.failureTolerance) {
1333
+ this._hasValidKey = false;
1334
+ }
1335
+ }
1336
+ decryptionSuccess() {
1337
+ this.resetKeyStatus();
1338
+ }
1339
+ /**
1340
+ * Call this after user initiated ratchet or a new key has been set in order to make sure to mark potentially
1341
+ * invalid keys as valid again
1342
+ */
1343
+ resetKeyStatus() {
1344
+ this.decryptionFailureCount = 0;
1345
+ this._hasValidKey = true;
1346
+ }
1259
1347
  /**
1260
1348
  * Ratchets the current key (or the one at keyIndex if provided) and
1261
1349
  * returns the ratcheted material
@@ -1298,7 +1386,7 @@ class ParticipantKeyHandler extends EventEmitter {
1298
1386
  let keyIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
1299
1387
  return __awaiter(this, void 0, void 0, function* () {
1300
1388
  yield this.setKeyFromMaterial(material, keyIndex);
1301
- this.hasValidKey = true;
1389
+ this.resetKeyStatus();
1302
1390
  });
1303
1391
  }
1304
1392
  /**
@@ -1331,7 +1419,7 @@ class ParticipantKeyHandler extends EventEmitter {
1331
1419
  setCurrentKeyIndex(index) {
1332
1420
  return __awaiter(this, void 0, void 0, function* () {
1333
1421
  this.currentKeyIndex = index % this.cryptoKeyRing.length;
1334
- this.hasValidKey = true;
1422
+ this.resetKeyStatus();
1335
1423
  });
1336
1424
  }
1337
1425
  isEnabled() {
@@ -1423,7 +1511,7 @@ function handleRatchetRequest(data) {
1423
1511
  return __awaiter(this, void 0, void 0, function* () {
1424
1512
  const keyHandler = getParticipantKeyHandler(data.participantId);
1425
1513
  yield keyHandler.ratchetKey(data.keyIndex);
1426
- keyHandler.hasValidKey = true;
1514
+ keyHandler.resetKeyStatus();
1427
1515
  });
1428
1516
  }
1429
1517
  function getTrackCryptor(participantId, trackId) {