genesys-cloud-streaming-client 18.0.0 → 18.0.1-master-to-develop.3

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.
@@ -216,19 +216,19 @@ function chdir (dir) {
216
216
  }function umask() { return 0; }
217
217
 
218
218
  // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js
219
- var performance = global$1.performance || {};
219
+ var performance$1 = global$1.performance || {};
220
220
  var performanceNow =
221
- performance.now ||
222
- performance.mozNow ||
223
- performance.msNow ||
224
- performance.oNow ||
225
- performance.webkitNow ||
221
+ performance$1.now ||
222
+ performance$1.mozNow ||
223
+ performance$1.msNow ||
224
+ performance$1.oNow ||
225
+ performance$1.webkitNow ||
226
226
  function(){ return (new Date()).getTime() };
227
227
 
228
228
  // generate timestamp or delta
229
229
  // see http://nodejs.org/api/process.html#process_process_hrtime
230
230
  function hrtime(previousTimestamp){
231
- var clocktime = performanceNow.call(performance)*1e-3;
231
+ var clocktime = performanceNow.call(performance$1)*1e-3;
232
232
  var seconds = Math.floor(clocktime);
233
233
  var nanoseconds = Math.floor((clocktime%1)*1e9);
234
234
  if (previousTimestamp) {
@@ -682,14 +682,14 @@ function _addListener(target, type, listener, prepend) {
682
682
  w.emitter = target;
683
683
  w.type = type;
684
684
  w.count = existing.length;
685
- emitWarning(w);
685
+ emitWarning$1(w);
686
686
  }
687
687
  }
688
688
  }
689
689
 
690
690
  return target;
691
691
  }
692
- function emitWarning(e) {
692
+ function emitWarning$1(e) {
693
693
  typeof console.warn === 'function' ? console.warn(e) : console.log(e);
694
694
  }
695
695
  EventEmitter.prototype.addListener = function addListener(type, listener) {
@@ -1324,7 +1324,7 @@ const [isReadableStream, isRequest, isResponse, isHeaders] = ['ReadableStream',
1324
1324
  *
1325
1325
  * @returns {String} The String freed of excess whitespace
1326
1326
  */
1327
- const trim$1 = (str) => str.trim ?
1327
+ const trim = (str) => str.trim ?
1328
1328
  str.trim() : str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
1329
1329
 
1330
1330
  /**
@@ -1833,7 +1833,7 @@ var utils$1 = {
1833
1833
  forEach,
1834
1834
  merge,
1835
1835
  extend,
1836
- trim: trim$1,
1836
+ trim,
1837
1837
  stripBOM,
1838
1838
  inherits,
1839
1839
  toFlatObject,
@@ -10181,781 +10181,1547 @@ function toURI(data) {
10181
10181
  }
10182
10182
  JID$8.toURI = toURI;
10183
10183
 
10184
- var iterator;
10185
- var hasRequiredIterator;
10186
-
10187
- function requireIterator () {
10188
- if (hasRequiredIterator) return iterator;
10189
- hasRequiredIterator = 1;
10190
- iterator = function (Yallist) {
10191
- Yallist.prototype[Symbol.iterator] = function* () {
10192
- for (let walker = this.head; walker; walker = walker.next) {
10193
- yield walker.value;
10194
- }
10195
- };
10196
- };
10197
- return iterator;
10198
- }
10199
-
10200
- var yallist = Yallist$1;
10201
-
10202
- Yallist$1.Node = Node;
10203
- Yallist$1.create = Yallist$1;
10204
-
10205
- function Yallist$1 (list) {
10206
- var self = this;
10207
- if (!(self instanceof Yallist$1)) {
10208
- self = new Yallist$1();
10209
- }
10210
-
10211
- self.tail = null;
10212
- self.head = null;
10213
- self.length = 0;
10214
-
10215
- if (list && typeof list.forEach === 'function') {
10216
- list.forEach(function (item) {
10217
- self.push(item);
10218
- });
10219
- } else if (arguments.length > 0) {
10220
- for (var i = 0, l = arguments.length; i < l; i++) {
10221
- self.push(arguments[i]);
10222
- }
10223
- }
10224
-
10225
- return self
10226
- }
10227
-
10228
- Yallist$1.prototype.removeNode = function (node) {
10229
- if (node.list !== this) {
10230
- throw new Error('removing node which does not belong to this list')
10231
- }
10232
-
10233
- var next = node.next;
10234
- var prev = node.prev;
10235
-
10236
- if (next) {
10237
- next.prev = prev;
10238
- }
10239
-
10240
- if (prev) {
10241
- prev.next = next;
10242
- }
10243
-
10244
- if (node === this.head) {
10245
- this.head = next;
10246
- }
10247
- if (node === this.tail) {
10248
- this.tail = prev;
10249
- }
10250
-
10251
- node.list.length--;
10252
- node.next = null;
10253
- node.prev = null;
10254
- node.list = null;
10255
-
10256
- return next
10257
- };
10258
-
10259
- Yallist$1.prototype.unshiftNode = function (node) {
10260
- if (node === this.head) {
10261
- return
10262
- }
10263
-
10264
- if (node.list) {
10265
- node.list.removeNode(node);
10266
- }
10267
-
10268
- var head = this.head;
10269
- node.list = this;
10270
- node.next = head;
10271
- if (head) {
10272
- head.prev = node;
10273
- }
10274
-
10275
- this.head = node;
10276
- if (!this.tail) {
10277
- this.tail = node;
10278
- }
10279
- this.length++;
10280
- };
10281
-
10282
- Yallist$1.prototype.pushNode = function (node) {
10283
- if (node === this.tail) {
10284
- return
10285
- }
10286
-
10287
- if (node.list) {
10288
- node.list.removeNode(node);
10289
- }
10290
-
10291
- var tail = this.tail;
10292
- node.list = this;
10293
- node.prev = tail;
10294
- if (tail) {
10295
- tail.next = node;
10296
- }
10297
-
10298
- this.tail = node;
10299
- if (!this.head) {
10300
- this.head = node;
10301
- }
10302
- this.length++;
10303
- };
10304
-
10305
- Yallist$1.prototype.push = function () {
10306
- for (var i = 0, l = arguments.length; i < l; i++) {
10307
- push(this, arguments[i]);
10308
- }
10309
- return this.length
10310
- };
10311
-
10312
- Yallist$1.prototype.unshift = function () {
10313
- for (var i = 0, l = arguments.length; i < l; i++) {
10314
- unshift(this, arguments[i]);
10315
- }
10316
- return this.length
10317
- };
10318
-
10319
- Yallist$1.prototype.pop = function () {
10320
- if (!this.tail) {
10321
- return undefined
10322
- }
10323
-
10324
- var res = this.tail.value;
10325
- this.tail = this.tail.prev;
10326
- if (this.tail) {
10327
- this.tail.next = null;
10328
- } else {
10329
- this.head = null;
10330
- }
10331
- this.length--;
10332
- return res
10333
- };
10334
-
10335
- Yallist$1.prototype.shift = function () {
10336
- if (!this.head) {
10337
- return undefined
10338
- }
10339
-
10340
- var res = this.head.value;
10341
- this.head = this.head.next;
10342
- if (this.head) {
10343
- this.head.prev = null;
10344
- } else {
10345
- this.tail = null;
10346
- }
10347
- this.length--;
10348
- return res
10349
- };
10350
-
10351
- Yallist$1.prototype.forEach = function (fn, thisp) {
10352
- thisp = thisp || this;
10353
- for (var walker = this.head, i = 0; walker !== null; i++) {
10354
- fn.call(thisp, walker.value, i, this);
10355
- walker = walker.next;
10356
- }
10357
- };
10358
-
10359
- Yallist$1.prototype.forEachReverse = function (fn, thisp) {
10360
- thisp = thisp || this;
10361
- for (var walker = this.tail, i = this.length - 1; walker !== null; i--) {
10362
- fn.call(thisp, walker.value, i, this);
10363
- walker = walker.prev;
10364
- }
10365
- };
10366
-
10367
- Yallist$1.prototype.get = function (n) {
10368
- for (var i = 0, walker = this.head; walker !== null && i < n; i++) {
10369
- // abort out of the list early if we hit a cycle
10370
- walker = walker.next;
10371
- }
10372
- if (i === n && walker !== null) {
10373
- return walker.value
10374
- }
10375
- };
10376
-
10377
- Yallist$1.prototype.getReverse = function (n) {
10378
- for (var i = 0, walker = this.tail; walker !== null && i < n; i++) {
10379
- // abort out of the list early if we hit a cycle
10380
- walker = walker.prev;
10381
- }
10382
- if (i === n && walker !== null) {
10383
- return walker.value
10384
- }
10385
- };
10386
-
10387
- Yallist$1.prototype.map = function (fn, thisp) {
10388
- thisp = thisp || this;
10389
- var res = new Yallist$1();
10390
- for (var walker = this.head; walker !== null;) {
10391
- res.push(fn.call(thisp, walker.value, this));
10392
- walker = walker.next;
10393
- }
10394
- return res
10395
- };
10396
-
10397
- Yallist$1.prototype.mapReverse = function (fn, thisp) {
10398
- thisp = thisp || this;
10399
- var res = new Yallist$1();
10400
- for (var walker = this.tail; walker !== null;) {
10401
- res.push(fn.call(thisp, walker.value, this));
10402
- walker = walker.prev;
10403
- }
10404
- return res
10405
- };
10406
-
10407
- Yallist$1.prototype.reduce = function (fn, initial) {
10408
- var acc;
10409
- var walker = this.head;
10410
- if (arguments.length > 1) {
10411
- acc = initial;
10412
- } else if (this.head) {
10413
- walker = this.head.next;
10414
- acc = this.head.value;
10415
- } else {
10416
- throw new TypeError('Reduce of empty list with no initial value')
10417
- }
10418
-
10419
- for (var i = 0; walker !== null; i++) {
10420
- acc = fn(acc, walker.value, i);
10421
- walker = walker.next;
10422
- }
10423
-
10424
- return acc
10425
- };
10426
-
10427
- Yallist$1.prototype.reduceReverse = function (fn, initial) {
10428
- var acc;
10429
- var walker = this.tail;
10430
- if (arguments.length > 1) {
10431
- acc = initial;
10432
- } else if (this.tail) {
10433
- walker = this.tail.prev;
10434
- acc = this.tail.value;
10435
- } else {
10436
- throw new TypeError('Reduce of empty list with no initial value')
10437
- }
10438
-
10439
- for (var i = this.length - 1; walker !== null; i--) {
10440
- acc = fn(acc, walker.value, i);
10441
- walker = walker.prev;
10442
- }
10443
-
10444
- return acc
10445
- };
10446
-
10447
- Yallist$1.prototype.toArray = function () {
10448
- var arr = new Array(this.length);
10449
- for (var i = 0, walker = this.head; walker !== null; i++) {
10450
- arr[i] = walker.value;
10451
- walker = walker.next;
10452
- }
10453
- return arr
10454
- };
10455
-
10456
- Yallist$1.prototype.toArrayReverse = function () {
10457
- var arr = new Array(this.length);
10458
- for (var i = 0, walker = this.tail; walker !== null; i++) {
10459
- arr[i] = walker.value;
10460
- walker = walker.prev;
10461
- }
10462
- return arr
10463
- };
10464
-
10465
- Yallist$1.prototype.slice = function (from, to) {
10466
- to = to || this.length;
10467
- if (to < 0) {
10468
- to += this.length;
10469
- }
10470
- from = from || 0;
10471
- if (from < 0) {
10472
- from += this.length;
10473
- }
10474
- var ret = new Yallist$1();
10475
- if (to < from || to < 0) {
10476
- return ret
10477
- }
10478
- if (from < 0) {
10479
- from = 0;
10480
- }
10481
- if (to > this.length) {
10482
- to = this.length;
10483
- }
10484
- for (var i = 0, walker = this.head; walker !== null && i < from; i++) {
10485
- walker = walker.next;
10486
- }
10487
- for (; walker !== null && i < to; i++, walker = walker.next) {
10488
- ret.push(walker.value);
10489
- }
10490
- return ret
10491
- };
10492
-
10493
- Yallist$1.prototype.sliceReverse = function (from, to) {
10494
- to = to || this.length;
10495
- if (to < 0) {
10496
- to += this.length;
10497
- }
10498
- from = from || 0;
10499
- if (from < 0) {
10500
- from += this.length;
10501
- }
10502
- var ret = new Yallist$1();
10503
- if (to < from || to < 0) {
10504
- return ret
10505
- }
10506
- if (from < 0) {
10507
- from = 0;
10508
- }
10509
- if (to > this.length) {
10510
- to = this.length;
10511
- }
10512
- for (var i = this.length, walker = this.tail; walker !== null && i > to; i--) {
10513
- walker = walker.prev;
10514
- }
10515
- for (; walker !== null && i > from; i--, walker = walker.prev) {
10516
- ret.push(walker.value);
10517
- }
10518
- return ret
10519
- };
10520
-
10521
- Yallist$1.prototype.splice = function (start, deleteCount, ...nodes) {
10522
- if (start > this.length) {
10523
- start = this.length - 1;
10524
- }
10525
- if (start < 0) {
10526
- start = this.length + start;
10527
- }
10528
-
10529
- for (var i = 0, walker = this.head; walker !== null && i < start; i++) {
10530
- walker = walker.next;
10531
- }
10532
-
10533
- var ret = [];
10534
- for (var i = 0; walker && i < deleteCount; i++) {
10535
- ret.push(walker.value);
10536
- walker = this.removeNode(walker);
10537
- }
10538
- if (walker === null) {
10539
- walker = this.tail;
10540
- }
10541
-
10542
- if (walker !== this.head && walker !== this.tail) {
10543
- walker = walker.prev;
10544
- }
10545
-
10546
- for (var i = 0; i < nodes.length; i++) {
10547
- walker = insert(this, walker, nodes[i]);
10548
- }
10549
- return ret;
10550
- };
10551
-
10552
- Yallist$1.prototype.reverse = function () {
10553
- var head = this.head;
10554
- var tail = this.tail;
10555
- for (var walker = head; walker !== null; walker = walker.prev) {
10556
- var p = walker.prev;
10557
- walker.prev = walker.next;
10558
- walker.next = p;
10559
- }
10560
- this.head = tail;
10561
- this.tail = head;
10562
- return this
10184
+ /**
10185
+ * @module LRUCache
10186
+ */
10187
+ const perf = typeof performance === 'object' &&
10188
+ performance &&
10189
+ typeof performance.now === 'function'
10190
+ ? performance
10191
+ : Date;
10192
+ const warned = new Set();
10193
+ /* c8 ignore start */
10194
+ const PROCESS = (typeof browser$1$1 === 'object' && !!browser$1$1 ? browser$1$1 : {});
10195
+ /* c8 ignore start */
10196
+ const emitWarning = (msg, type, code, fn) => {
10197
+ typeof PROCESS.emitWarning === 'function'
10198
+ ? PROCESS.emitWarning(msg, type, code, fn)
10199
+ : console.error(`[${code}] ${type}: ${msg}`);
10563
10200
  };
10564
-
10565
- function insert (self, node, value) {
10566
- var inserted = node === self.head ?
10567
- new Node(value, null, node, self) :
10568
- new Node(value, node, node.next, self);
10569
-
10570
- if (inserted.next === null) {
10571
- self.tail = inserted;
10572
- }
10573
- if (inserted.prev === null) {
10574
- self.head = inserted;
10575
- }
10576
-
10577
- self.length++;
10578
-
10579
- return inserted
10580
- }
10581
-
10582
- function push (self, item) {
10583
- self.tail = new Node(item, self.tail, null, self);
10584
- if (!self.head) {
10585
- self.head = self.tail;
10586
- }
10587
- self.length++;
10588
- }
10589
-
10590
- function unshift (self, item) {
10591
- self.head = new Node(item, null, self.head, self);
10592
- if (!self.tail) {
10593
- self.tail = self.head;
10594
- }
10595
- self.length++;
10201
+ let AC = globalThis.AbortController;
10202
+ let AS = globalThis.AbortSignal;
10203
+ /* c8 ignore start */
10204
+ if (typeof AC === 'undefined') {
10205
+ //@ts-ignore
10206
+ AS = class AbortSignal {
10207
+ onabort;
10208
+ _onabort = [];
10209
+ reason;
10210
+ aborted = false;
10211
+ addEventListener(_, fn) {
10212
+ this._onabort.push(fn);
10213
+ }
10214
+ };
10215
+ //@ts-ignore
10216
+ AC = class AbortController {
10217
+ constructor() {
10218
+ warnACPolyfill();
10219
+ }
10220
+ signal = new AS();
10221
+ abort(reason) {
10222
+ if (this.signal.aborted)
10223
+ return;
10224
+ //@ts-ignore
10225
+ this.signal.reason = reason;
10226
+ //@ts-ignore
10227
+ this.signal.aborted = true;
10228
+ //@ts-ignore
10229
+ for (const fn of this.signal._onabort) {
10230
+ fn(reason);
10231
+ }
10232
+ this.signal.onabort?.(reason);
10233
+ }
10234
+ };
10235
+ let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1';
10236
+ const warnACPolyfill = () => {
10237
+ if (!printACPolyfillWarning)
10238
+ return;
10239
+ printACPolyfillWarning = false;
10240
+ emitWarning('AbortController is not defined. If using lru-cache in ' +
10241
+ 'node 14, load an AbortController polyfill from the ' +
10242
+ '`node-abort-controller` package. A minimal polyfill is ' +
10243
+ 'provided for use by LRUCache.fetch(), but it should not be ' +
10244
+ 'relied upon in other contexts (eg, passing it to other APIs that ' +
10245
+ 'use AbortController/AbortSignal might have undesirable effects). ' +
10246
+ 'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.', 'NO_ABORT_CONTROLLER', 'ENOTSUP', warnACPolyfill);
10247
+ };
10596
10248
  }
10597
-
10598
- function Node (value, prev, next, list) {
10599
- if (!(this instanceof Node)) {
10600
- return new Node(value, prev, next, list)
10601
- }
10602
-
10603
- this.list = list;
10604
- this.value = value;
10605
-
10606
- if (prev) {
10607
- prev.next = this;
10608
- this.prev = prev;
10609
- } else {
10610
- this.prev = null;
10611
- }
10612
-
10613
- if (next) {
10614
- next.prev = this;
10615
- this.next = next;
10616
- } else {
10617
- this.next = null;
10618
- }
10249
+ /* c8 ignore stop */
10250
+ const shouldWarn = (code) => !warned.has(code);
10251
+ const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
10252
+ /* c8 ignore start */
10253
+ // This is a little bit ridiculous, tbh.
10254
+ // The maximum array length is 2^32-1 or thereabouts on most JS impls.
10255
+ // And well before that point, you're caching the entire world, I mean,
10256
+ // that's ~32GB of just integers for the next/prev links, plus whatever
10257
+ // else to hold that many keys and values. Just filling the memory with
10258
+ // zeroes at init time is brutal when you get that big.
10259
+ // But why not be complete?
10260
+ // Maybe in the future, these limits will have expanded.
10261
+ const getUintArray = (max) => !isPosInt(max)
10262
+ ? null
10263
+ : max <= Math.pow(2, 8)
10264
+ ? Uint8Array
10265
+ : max <= Math.pow(2, 16)
10266
+ ? Uint16Array
10267
+ : max <= Math.pow(2, 32)
10268
+ ? Uint32Array
10269
+ : max <= Number.MAX_SAFE_INTEGER
10270
+ ? ZeroArray
10271
+ : null;
10272
+ /* c8 ignore stop */
10273
+ class ZeroArray extends Array {
10274
+ constructor(size) {
10275
+ super(size);
10276
+ this.fill(0);
10277
+ }
10278
+ }
10279
+ class Stack {
10280
+ heap;
10281
+ length;
10282
+ // private constructor
10283
+ static #constructing = false;
10284
+ static create(max) {
10285
+ const HeapCls = getUintArray(max);
10286
+ if (!HeapCls)
10287
+ return [];
10288
+ Stack.#constructing = true;
10289
+ const s = new Stack(max, HeapCls);
10290
+ Stack.#constructing = false;
10291
+ return s;
10292
+ }
10293
+ constructor(max, HeapCls) {
10294
+ /* c8 ignore start */
10295
+ if (!Stack.#constructing) {
10296
+ throw new TypeError('instantiate Stack using Stack.create(n)');
10297
+ }
10298
+ /* c8 ignore stop */
10299
+ this.heap = new HeapCls(max);
10300
+ this.length = 0;
10301
+ }
10302
+ push(n) {
10303
+ this.heap[this.length++] = n;
10304
+ }
10305
+ pop() {
10306
+ return this.heap[--this.length];
10307
+ }
10619
10308
  }
10620
-
10621
- try {
10622
- // add if support for Symbol.iterator is present
10623
- requireIterator()(Yallist$1);
10624
- } catch (er) {}
10625
-
10626
- // A linked list to keep track of recently-used-ness
10627
- const Yallist = yallist;
10628
-
10629
- const MAX = Symbol('max');
10630
- const LENGTH = Symbol('length');
10631
- const LENGTH_CALCULATOR = Symbol('lengthCalculator');
10632
- const ALLOW_STALE = Symbol('allowStale');
10633
- const MAX_AGE = Symbol('maxAge');
10634
- const DISPOSE = Symbol('dispose');
10635
- const NO_DISPOSE_ON_SET = Symbol('noDisposeOnSet');
10636
- const LRU_LIST = Symbol('lruList');
10637
- const CACHE = Symbol('cache');
10638
- const UPDATE_AGE_ON_GET = Symbol('updateAgeOnGet');
10639
-
10640
- const naiveLength = () => 1;
10641
-
10642
- // lruList is a yallist where the head is the youngest
10643
- // item, and the tail is the oldest. the list contains the Hit
10644
- // objects as the entries.
10645
- // Each Hit object has a reference to its Yallist.Node. This
10646
- // never changes.
10647
- //
10648
- // cache is a Map (or PseudoMap) that matches the keys to
10649
- // the Yallist.Node object.
10309
+ /**
10310
+ * Default export, the thing you're using this module to get.
10311
+ *
10312
+ * The `K` and `V` types define the key and value types, respectively. The
10313
+ * optional `FC` type defines the type of the `context` object passed to
10314
+ * `cache.fetch()` and `cache.memo()`.
10315
+ *
10316
+ * Keys and values **must not** be `null` or `undefined`.
10317
+ *
10318
+ * All properties from the options object (with the exception of `max`,
10319
+ * `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
10320
+ * added as normal public members. (The listed options are read-only getters.)
10321
+ *
10322
+ * Changing any of these will alter the defaults for subsequent method calls.
10323
+ */
10650
10324
  class LRUCache {
10651
- constructor (options) {
10652
- if (typeof options === 'number')
10653
- options = { max: options };
10654
-
10655
- if (!options)
10656
- options = {};
10657
-
10658
- if (options.max && (typeof options.max !== 'number' || options.max < 0))
10659
- throw new TypeError('max must be a non-negative number')
10660
- // Kind of weird to have a default max of Infinity, but oh well.
10661
- this[MAX] = options.max || Infinity;
10662
-
10663
- const lc = options.length || naiveLength;
10664
- this[LENGTH_CALCULATOR] = (typeof lc !== 'function') ? naiveLength : lc;
10665
- this[ALLOW_STALE] = options.stale || false;
10666
- if (options.maxAge && typeof options.maxAge !== 'number')
10667
- throw new TypeError('maxAge must be a number')
10668
- this[MAX_AGE] = options.maxAge || 0;
10669
- this[DISPOSE] = options.dispose;
10670
- this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false;
10671
- this[UPDATE_AGE_ON_GET] = options.updateAgeOnGet || false;
10672
- this.reset();
10673
- }
10674
-
10675
- // resize the cache when the max changes.
10676
- set max (mL) {
10677
- if (typeof mL !== 'number' || mL < 0)
10678
- throw new TypeError('max must be a non-negative number')
10679
-
10680
- this[MAX] = mL || Infinity;
10681
- trim(this);
10682
- }
10683
- get max () {
10684
- return this[MAX]
10685
- }
10686
-
10687
- set allowStale (allowStale) {
10688
- this[ALLOW_STALE] = !!allowStale;
10689
- }
10690
- get allowStale () {
10691
- return this[ALLOW_STALE]
10692
- }
10693
-
10694
- set maxAge (mA) {
10695
- if (typeof mA !== 'number')
10696
- throw new TypeError('maxAge must be a non-negative number')
10697
-
10698
- this[MAX_AGE] = mA;
10699
- trim(this);
10700
- }
10701
- get maxAge () {
10702
- return this[MAX_AGE]
10703
- }
10704
-
10705
- // resize the cache when the lengthCalculator changes.
10706
- set lengthCalculator (lC) {
10707
- if (typeof lC !== 'function')
10708
- lC = naiveLength;
10709
-
10710
- if (lC !== this[LENGTH_CALCULATOR]) {
10711
- this[LENGTH_CALCULATOR] = lC;
10712
- this[LENGTH] = 0;
10713
- this[LRU_LIST].forEach(hit => {
10714
- hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key);
10715
- this[LENGTH] += hit.length;
10716
- });
10325
+ // options that cannot be changed without disaster
10326
+ #max;
10327
+ #maxSize;
10328
+ #dispose;
10329
+ #disposeAfter;
10330
+ #fetchMethod;
10331
+ #memoMethod;
10332
+ /**
10333
+ * {@link LRUCache.OptionsBase.ttl}
10334
+ */
10335
+ ttl;
10336
+ /**
10337
+ * {@link LRUCache.OptionsBase.ttlResolution}
10338
+ */
10339
+ ttlResolution;
10340
+ /**
10341
+ * {@link LRUCache.OptionsBase.ttlAutopurge}
10342
+ */
10343
+ ttlAutopurge;
10344
+ /**
10345
+ * {@link LRUCache.OptionsBase.updateAgeOnGet}
10346
+ */
10347
+ updateAgeOnGet;
10348
+ /**
10349
+ * {@link LRUCache.OptionsBase.updateAgeOnHas}
10350
+ */
10351
+ updateAgeOnHas;
10352
+ /**
10353
+ * {@link LRUCache.OptionsBase.allowStale}
10354
+ */
10355
+ allowStale;
10356
+ /**
10357
+ * {@link LRUCache.OptionsBase.noDisposeOnSet}
10358
+ */
10359
+ noDisposeOnSet;
10360
+ /**
10361
+ * {@link LRUCache.OptionsBase.noUpdateTTL}
10362
+ */
10363
+ noUpdateTTL;
10364
+ /**
10365
+ * {@link LRUCache.OptionsBase.maxEntrySize}
10366
+ */
10367
+ maxEntrySize;
10368
+ /**
10369
+ * {@link LRUCache.OptionsBase.sizeCalculation}
10370
+ */
10371
+ sizeCalculation;
10372
+ /**
10373
+ * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
10374
+ */
10375
+ noDeleteOnFetchRejection;
10376
+ /**
10377
+ * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
10378
+ */
10379
+ noDeleteOnStaleGet;
10380
+ /**
10381
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
10382
+ */
10383
+ allowStaleOnFetchAbort;
10384
+ /**
10385
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
10386
+ */
10387
+ allowStaleOnFetchRejection;
10388
+ /**
10389
+ * {@link LRUCache.OptionsBase.ignoreFetchAbort}
10390
+ */
10391
+ ignoreFetchAbort;
10392
+ // computed properties
10393
+ #size;
10394
+ #calculatedSize;
10395
+ #keyMap;
10396
+ #keyList;
10397
+ #valList;
10398
+ #next;
10399
+ #prev;
10400
+ #head;
10401
+ #tail;
10402
+ #free;
10403
+ #disposed;
10404
+ #sizes;
10405
+ #starts;
10406
+ #ttls;
10407
+ #hasDispose;
10408
+ #hasFetchMethod;
10409
+ #hasDisposeAfter;
10410
+ /**
10411
+ * Do not call this method unless you need to inspect the
10412
+ * inner workings of the cache. If anything returned by this
10413
+ * object is modified in any way, strange breakage may occur.
10414
+ *
10415
+ * These fields are private for a reason!
10416
+ *
10417
+ * @internal
10418
+ */
10419
+ static unsafeExposeInternals(c) {
10420
+ return {
10421
+ // properties
10422
+ starts: c.#starts,
10423
+ ttls: c.#ttls,
10424
+ sizes: c.#sizes,
10425
+ keyMap: c.#keyMap,
10426
+ keyList: c.#keyList,
10427
+ valList: c.#valList,
10428
+ next: c.#next,
10429
+ prev: c.#prev,
10430
+ get head() {
10431
+ return c.#head;
10432
+ },
10433
+ get tail() {
10434
+ return c.#tail;
10435
+ },
10436
+ free: c.#free,
10437
+ // methods
10438
+ isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
10439
+ backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
10440
+ moveToTail: (index) => c.#moveToTail(index),
10441
+ indexes: (options) => c.#indexes(options),
10442
+ rindexes: (options) => c.#rindexes(options),
10443
+ isStale: (index) => c.#isStale(index),
10444
+ };
10717
10445
  }
10718
- trim(this);
10719
- }
10720
- get lengthCalculator () { return this[LENGTH_CALCULATOR] }
10721
-
10722
- get length () { return this[LENGTH] }
10723
- get itemCount () { return this[LRU_LIST].length }
10724
-
10725
- rforEach (fn, thisp) {
10726
- thisp = thisp || this;
10727
- for (let walker = this[LRU_LIST].tail; walker !== null;) {
10728
- const prev = walker.prev;
10729
- forEachStep(this, fn, walker, thisp);
10730
- walker = prev;
10446
+ // Protected read-only members
10447
+ /**
10448
+ * {@link LRUCache.OptionsBase.max} (read-only)
10449
+ */
10450
+ get max() {
10451
+ return this.#max;
10731
10452
  }
10732
- }
10733
-
10734
- forEach (fn, thisp) {
10735
- thisp = thisp || this;
10736
- for (let walker = this[LRU_LIST].head; walker !== null;) {
10737
- const next = walker.next;
10738
- forEachStep(this, fn, walker, thisp);
10739
- walker = next;
10453
+ /**
10454
+ * {@link LRUCache.OptionsBase.maxSize} (read-only)
10455
+ */
10456
+ get maxSize() {
10457
+ return this.#maxSize;
10740
10458
  }
10741
- }
10742
-
10743
- keys () {
10744
- return this[LRU_LIST].toArray().map(k => k.key)
10745
- }
10746
-
10747
- values () {
10748
- return this[LRU_LIST].toArray().map(k => k.value)
10749
- }
10750
-
10751
- reset () {
10752
- if (this[DISPOSE] &&
10753
- this[LRU_LIST] &&
10754
- this[LRU_LIST].length) {
10755
- this[LRU_LIST].forEach(hit => this[DISPOSE](hit.key, hit.value));
10459
+ /**
10460
+ * The total computed size of items in the cache (read-only)
10461
+ */
10462
+ get calculatedSize() {
10463
+ return this.#calculatedSize;
10756
10464
  }
10757
-
10758
- this[CACHE] = new Map(); // hash of items by key
10759
- this[LRU_LIST] = new Yallist(); // list of items in order of use recency
10760
- this[LENGTH] = 0; // length of items in the list
10761
- }
10762
-
10763
- dump () {
10764
- return this[LRU_LIST].map(hit =>
10765
- isStale(this, hit) ? false : {
10766
- k: hit.key,
10767
- v: hit.value,
10768
- e: hit.now + (hit.maxAge || 0)
10769
- }).toArray().filter(h => h)
10770
- }
10771
-
10772
- dumpLru () {
10773
- return this[LRU_LIST]
10774
- }
10775
-
10776
- set (key, value, maxAge) {
10777
- maxAge = maxAge || this[MAX_AGE];
10778
-
10779
- if (maxAge && typeof maxAge !== 'number')
10780
- throw new TypeError('maxAge must be a number')
10781
-
10782
- const now = maxAge ? Date.now() : 0;
10783
- const len = this[LENGTH_CALCULATOR](value, key);
10784
-
10785
- if (this[CACHE].has(key)) {
10786
- if (len > this[MAX]) {
10787
- del(this, this[CACHE].get(key));
10788
- return false
10789
- }
10790
-
10791
- const node = this[CACHE].get(key);
10792
- const item = node.value;
10793
-
10794
- // dispose of the old one before overwriting
10795
- // split out into 2 ifs for better coverage tracking
10796
- if (this[DISPOSE]) {
10797
- if (!this[NO_DISPOSE_ON_SET])
10798
- this[DISPOSE](key, item.value);
10799
- }
10800
-
10801
- item.now = now;
10802
- item.maxAge = maxAge;
10803
- item.value = value;
10804
- this[LENGTH] += len - item.length;
10805
- item.length = len;
10806
- this.get(key);
10807
- trim(this);
10808
- return true
10465
+ /**
10466
+ * The number of items stored in the cache (read-only)
10467
+ */
10468
+ get size() {
10469
+ return this.#size;
10809
10470
  }
10810
-
10811
- const hit = new Entry(key, value, len, now, maxAge);
10812
-
10813
- // oversized objects fall out of cache automatically.
10814
- if (hit.length > this[MAX]) {
10815
- if (this[DISPOSE])
10816
- this[DISPOSE](key, value);
10817
-
10818
- return false
10471
+ /**
10472
+ * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
10473
+ */
10474
+ get fetchMethod() {
10475
+ return this.#fetchMethod;
10819
10476
  }
10820
-
10821
- this[LENGTH] += hit.length;
10822
- this[LRU_LIST].unshift(hit);
10823
- this[CACHE].set(key, this[LRU_LIST].head);
10824
- trim(this);
10825
- return true
10826
- }
10827
-
10828
- has (key) {
10829
- if (!this[CACHE].has(key)) return false
10830
- const hit = this[CACHE].get(key).value;
10831
- return !isStale(this, hit)
10832
- }
10833
-
10834
- get (key) {
10835
- return get(this, key, true)
10836
- }
10837
-
10838
- peek (key) {
10839
- return get(this, key, false)
10840
- }
10841
-
10842
- pop () {
10843
- const node = this[LRU_LIST].tail;
10844
- if (!node)
10845
- return null
10846
-
10847
- del(this, node);
10848
- return node.value
10849
- }
10850
-
10851
- del (key) {
10852
- del(this, this[CACHE].get(key));
10853
- }
10854
-
10855
- load (arr) {
10856
- // reset the cache
10857
- this.reset();
10858
-
10859
- const now = Date.now();
10860
- // A previous serialized cache has the most recent items first
10861
- for (let l = arr.length - 1; l >= 0; l--) {
10862
- const hit = arr[l];
10863
- const expiresAt = hit.e || 0;
10864
- if (expiresAt === 0)
10865
- // the item was created without expiration in a non aged cache
10866
- this.set(hit.k, hit.v);
10867
- else {
10868
- const maxAge = expiresAt - now;
10869
- // dont add already expired items
10870
- if (maxAge > 0) {
10871
- this.set(hit.k, hit.v, maxAge);
10477
+ get memoMethod() {
10478
+ return this.#memoMethod;
10479
+ }
10480
+ /**
10481
+ * {@link LRUCache.OptionsBase.dispose} (read-only)
10482
+ */
10483
+ get dispose() {
10484
+ return this.#dispose;
10485
+ }
10486
+ /**
10487
+ * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
10488
+ */
10489
+ get disposeAfter() {
10490
+ return this.#disposeAfter;
10491
+ }
10492
+ constructor(options) {
10493
+ const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, } = options;
10494
+ if (max !== 0 && !isPosInt(max)) {
10495
+ throw new TypeError('max option must be a nonnegative integer');
10496
+ }
10497
+ const UintArray = max ? getUintArray(max) : Array;
10498
+ if (!UintArray) {
10499
+ throw new Error('invalid max value: ' + max);
10500
+ }
10501
+ this.#max = max;
10502
+ this.#maxSize = maxSize;
10503
+ this.maxEntrySize = maxEntrySize || this.#maxSize;
10504
+ this.sizeCalculation = sizeCalculation;
10505
+ if (this.sizeCalculation) {
10506
+ if (!this.#maxSize && !this.maxEntrySize) {
10507
+ throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
10508
+ }
10509
+ if (typeof this.sizeCalculation !== 'function') {
10510
+ throw new TypeError('sizeCalculation set to non-function');
10511
+ }
10512
+ }
10513
+ if (memoMethod !== undefined &&
10514
+ typeof memoMethod !== 'function') {
10515
+ throw new TypeError('memoMethod must be a function if defined');
10516
+ }
10517
+ this.#memoMethod = memoMethod;
10518
+ if (fetchMethod !== undefined &&
10519
+ typeof fetchMethod !== 'function') {
10520
+ throw new TypeError('fetchMethod must be a function if specified');
10521
+ }
10522
+ this.#fetchMethod = fetchMethod;
10523
+ this.#hasFetchMethod = !!fetchMethod;
10524
+ this.#keyMap = new Map();
10525
+ this.#keyList = new Array(max).fill(undefined);
10526
+ this.#valList = new Array(max).fill(undefined);
10527
+ this.#next = new UintArray(max);
10528
+ this.#prev = new UintArray(max);
10529
+ this.#head = 0;
10530
+ this.#tail = 0;
10531
+ this.#free = Stack.create(max);
10532
+ this.#size = 0;
10533
+ this.#calculatedSize = 0;
10534
+ if (typeof dispose === 'function') {
10535
+ this.#dispose = dispose;
10536
+ }
10537
+ if (typeof disposeAfter === 'function') {
10538
+ this.#disposeAfter = disposeAfter;
10539
+ this.#disposed = [];
10540
+ }
10541
+ else {
10542
+ this.#disposeAfter = undefined;
10543
+ this.#disposed = undefined;
10544
+ }
10545
+ this.#hasDispose = !!this.#dispose;
10546
+ this.#hasDisposeAfter = !!this.#disposeAfter;
10547
+ this.noDisposeOnSet = !!noDisposeOnSet;
10548
+ this.noUpdateTTL = !!noUpdateTTL;
10549
+ this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
10550
+ this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
10551
+ this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
10552
+ this.ignoreFetchAbort = !!ignoreFetchAbort;
10553
+ // NB: maxEntrySize is set to maxSize if it's set
10554
+ if (this.maxEntrySize !== 0) {
10555
+ if (this.#maxSize !== 0) {
10556
+ if (!isPosInt(this.#maxSize)) {
10557
+ throw new TypeError('maxSize must be a positive integer if specified');
10558
+ }
10559
+ }
10560
+ if (!isPosInt(this.maxEntrySize)) {
10561
+ throw new TypeError('maxEntrySize must be a positive integer if specified');
10562
+ }
10563
+ this.#initializeSizeTracking();
10564
+ }
10565
+ this.allowStale = !!allowStale;
10566
+ this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
10567
+ this.updateAgeOnGet = !!updateAgeOnGet;
10568
+ this.updateAgeOnHas = !!updateAgeOnHas;
10569
+ this.ttlResolution =
10570
+ isPosInt(ttlResolution) || ttlResolution === 0
10571
+ ? ttlResolution
10572
+ : 1;
10573
+ this.ttlAutopurge = !!ttlAutopurge;
10574
+ this.ttl = ttl || 0;
10575
+ if (this.ttl) {
10576
+ if (!isPosInt(this.ttl)) {
10577
+ throw new TypeError('ttl must be a positive integer if specified');
10578
+ }
10579
+ this.#initializeTTLTracking();
10580
+ }
10581
+ // do not allow completely unbounded caches
10582
+ if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
10583
+ throw new TypeError('At least one of max, maxSize, or ttl is required');
10584
+ }
10585
+ if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
10586
+ const code = 'LRU_CACHE_UNBOUNDED';
10587
+ if (shouldWarn(code)) {
10588
+ warned.add(code);
10589
+ const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
10590
+ 'result in unbounded memory consumption.';
10591
+ emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
10592
+ }
10872
10593
  }
10873
- }
10874
10594
  }
10875
- }
10876
-
10877
- prune () {
10878
- this[CACHE].forEach((value, key) => get(this, key, false));
10879
- }
10880
- }
10881
-
10882
- const get = (self, key, doUse) => {
10883
- const node = self[CACHE].get(key);
10884
- if (node) {
10885
- const hit = node.value;
10886
- if (isStale(self, hit)) {
10887
- del(self, node);
10888
- if (!self[ALLOW_STALE])
10889
- return undefined
10890
- } else {
10891
- if (doUse) {
10892
- if (self[UPDATE_AGE_ON_GET])
10893
- node.value.now = Date.now();
10894
- self[LRU_LIST].unshiftNode(node);
10895
- }
10595
+ /**
10596
+ * Return the number of ms left in the item's TTL. If item is not in cache,
10597
+ * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
10598
+ */
10599
+ getRemainingTTL(key) {
10600
+ return this.#keyMap.has(key) ? Infinity : 0;
10601
+ }
10602
+ #initializeTTLTracking() {
10603
+ const ttls = new ZeroArray(this.#max);
10604
+ const starts = new ZeroArray(this.#max);
10605
+ this.#ttls = ttls;
10606
+ this.#starts = starts;
10607
+ this.#setItemTTL = (index, ttl, start = perf.now()) => {
10608
+ starts[index] = ttl !== 0 ? start : 0;
10609
+ ttls[index] = ttl;
10610
+ if (ttl !== 0 && this.ttlAutopurge) {
10611
+ const t = setTimeout(() => {
10612
+ if (this.#isStale(index)) {
10613
+ this.#delete(this.#keyList[index], 'expire');
10614
+ }
10615
+ }, ttl + 1);
10616
+ // unref() not supported on all platforms
10617
+ /* c8 ignore start */
10618
+ if (t.unref) {
10619
+ t.unref();
10620
+ }
10621
+ /* c8 ignore stop */
10622
+ }
10623
+ };
10624
+ this.#updateItemAge = index => {
10625
+ starts[index] = ttls[index] !== 0 ? perf.now() : 0;
10626
+ };
10627
+ this.#statusTTL = (status, index) => {
10628
+ if (ttls[index]) {
10629
+ const ttl = ttls[index];
10630
+ const start = starts[index];
10631
+ /* c8 ignore next */
10632
+ if (!ttl || !start)
10633
+ return;
10634
+ status.ttl = ttl;
10635
+ status.start = start;
10636
+ status.now = cachedNow || getNow();
10637
+ const age = status.now - start;
10638
+ status.remainingTTL = ttl - age;
10639
+ }
10640
+ };
10641
+ // debounce calls to perf.now() to 1s so we're not hitting
10642
+ // that costly call repeatedly.
10643
+ let cachedNow = 0;
10644
+ const getNow = () => {
10645
+ const n = perf.now();
10646
+ if (this.ttlResolution > 0) {
10647
+ cachedNow = n;
10648
+ const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
10649
+ // not available on all platforms
10650
+ /* c8 ignore start */
10651
+ if (t.unref) {
10652
+ t.unref();
10653
+ }
10654
+ /* c8 ignore stop */
10655
+ }
10656
+ return n;
10657
+ };
10658
+ this.getRemainingTTL = key => {
10659
+ const index = this.#keyMap.get(key);
10660
+ if (index === undefined) {
10661
+ return 0;
10662
+ }
10663
+ const ttl = ttls[index];
10664
+ const start = starts[index];
10665
+ if (!ttl || !start) {
10666
+ return Infinity;
10667
+ }
10668
+ const age = (cachedNow || getNow()) - start;
10669
+ return ttl - age;
10670
+ };
10671
+ this.#isStale = index => {
10672
+ const s = starts[index];
10673
+ const t = ttls[index];
10674
+ return !!t && !!s && (cachedNow || getNow()) - s > t;
10675
+ };
10896
10676
  }
10897
- return hit.value
10898
- }
10899
- };
10900
-
10901
- const isStale = (self, hit) => {
10902
- if (!hit || (!hit.maxAge && !self[MAX_AGE]))
10903
- return false
10904
-
10905
- const diff = Date.now() - hit.now;
10906
- return hit.maxAge ? diff > hit.maxAge
10907
- : self[MAX_AGE] && (diff > self[MAX_AGE])
10908
- };
10909
-
10910
- const trim = self => {
10911
- if (self[LENGTH] > self[MAX]) {
10912
- for (let walker = self[LRU_LIST].tail;
10913
- self[LENGTH] > self[MAX] && walker !== null;) {
10914
- // We know that we're about to delete this one, and also
10915
- // what the next least recently used key will be, so just
10916
- // go ahead and set it now.
10917
- const prev = walker.prev;
10918
- del(self, walker);
10919
- walker = prev;
10677
+ // conditionally set private methods related to TTL
10678
+ #updateItemAge = () => { };
10679
+ #statusTTL = () => { };
10680
+ #setItemTTL = () => { };
10681
+ /* c8 ignore stop */
10682
+ #isStale = () => false;
10683
+ #initializeSizeTracking() {
10684
+ const sizes = new ZeroArray(this.#max);
10685
+ this.#calculatedSize = 0;
10686
+ this.#sizes = sizes;
10687
+ this.#removeItemSize = index => {
10688
+ this.#calculatedSize -= sizes[index];
10689
+ sizes[index] = 0;
10690
+ };
10691
+ this.#requireSize = (k, v, size, sizeCalculation) => {
10692
+ // provisionally accept background fetches.
10693
+ // actual value size will be checked when they return.
10694
+ if (this.#isBackgroundFetch(v)) {
10695
+ return 0;
10696
+ }
10697
+ if (!isPosInt(size)) {
10698
+ if (sizeCalculation) {
10699
+ if (typeof sizeCalculation !== 'function') {
10700
+ throw new TypeError('sizeCalculation must be a function');
10701
+ }
10702
+ size = sizeCalculation(v, k);
10703
+ if (!isPosInt(size)) {
10704
+ throw new TypeError('sizeCalculation return invalid (expect positive integer)');
10705
+ }
10706
+ }
10707
+ else {
10708
+ throw new TypeError('invalid size value (must be positive integer). ' +
10709
+ 'When maxSize or maxEntrySize is used, sizeCalculation ' +
10710
+ 'or size must be set.');
10711
+ }
10712
+ }
10713
+ return size;
10714
+ };
10715
+ this.#addItemSize = (index, size, status) => {
10716
+ sizes[index] = size;
10717
+ if (this.#maxSize) {
10718
+ const maxSize = this.#maxSize - sizes[index];
10719
+ while (this.#calculatedSize > maxSize) {
10720
+ this.#evict(true);
10721
+ }
10722
+ }
10723
+ this.#calculatedSize += sizes[index];
10724
+ if (status) {
10725
+ status.entrySize = size;
10726
+ status.totalCalculatedSize = this.#calculatedSize;
10727
+ }
10728
+ };
10729
+ }
10730
+ #removeItemSize = _i => { };
10731
+ #addItemSize = (_i, _s, _st) => { };
10732
+ #requireSize = (_k, _v, size, sizeCalculation) => {
10733
+ if (size || sizeCalculation) {
10734
+ throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
10735
+ }
10736
+ return 0;
10737
+ };
10738
+ *#indexes({ allowStale = this.allowStale } = {}) {
10739
+ if (this.#size) {
10740
+ for (let i = this.#tail; true;) {
10741
+ if (!this.#isValidIndex(i)) {
10742
+ break;
10743
+ }
10744
+ if (allowStale || !this.#isStale(i)) {
10745
+ yield i;
10746
+ }
10747
+ if (i === this.#head) {
10748
+ break;
10749
+ }
10750
+ else {
10751
+ i = this.#prev[i];
10752
+ }
10753
+ }
10754
+ }
10755
+ }
10756
+ *#rindexes({ allowStale = this.allowStale } = {}) {
10757
+ if (this.#size) {
10758
+ for (let i = this.#head; true;) {
10759
+ if (!this.#isValidIndex(i)) {
10760
+ break;
10761
+ }
10762
+ if (allowStale || !this.#isStale(i)) {
10763
+ yield i;
10764
+ }
10765
+ if (i === this.#tail) {
10766
+ break;
10767
+ }
10768
+ else {
10769
+ i = this.#next[i];
10770
+ }
10771
+ }
10772
+ }
10773
+ }
10774
+ #isValidIndex(index) {
10775
+ return (index !== undefined &&
10776
+ this.#keyMap.get(this.#keyList[index]) === index);
10777
+ }
10778
+ /**
10779
+ * Return a generator yielding `[key, value]` pairs,
10780
+ * in order from most recently used to least recently used.
10781
+ */
10782
+ *entries() {
10783
+ for (const i of this.#indexes()) {
10784
+ if (this.#valList[i] !== undefined &&
10785
+ this.#keyList[i] !== undefined &&
10786
+ !this.#isBackgroundFetch(this.#valList[i])) {
10787
+ yield [this.#keyList[i], this.#valList[i]];
10788
+ }
10789
+ }
10790
+ }
10791
+ /**
10792
+ * Inverse order version of {@link LRUCache.entries}
10793
+ *
10794
+ * Return a generator yielding `[key, value]` pairs,
10795
+ * in order from least recently used to most recently used.
10796
+ */
10797
+ *rentries() {
10798
+ for (const i of this.#rindexes()) {
10799
+ if (this.#valList[i] !== undefined &&
10800
+ this.#keyList[i] !== undefined &&
10801
+ !this.#isBackgroundFetch(this.#valList[i])) {
10802
+ yield [this.#keyList[i], this.#valList[i]];
10803
+ }
10804
+ }
10805
+ }
10806
+ /**
10807
+ * Return a generator yielding the keys in the cache,
10808
+ * in order from most recently used to least recently used.
10809
+ */
10810
+ *keys() {
10811
+ for (const i of this.#indexes()) {
10812
+ const k = this.#keyList[i];
10813
+ if (k !== undefined &&
10814
+ !this.#isBackgroundFetch(this.#valList[i])) {
10815
+ yield k;
10816
+ }
10817
+ }
10818
+ }
10819
+ /**
10820
+ * Inverse order version of {@link LRUCache.keys}
10821
+ *
10822
+ * Return a generator yielding the keys in the cache,
10823
+ * in order from least recently used to most recently used.
10824
+ */
10825
+ *rkeys() {
10826
+ for (const i of this.#rindexes()) {
10827
+ const k = this.#keyList[i];
10828
+ if (k !== undefined &&
10829
+ !this.#isBackgroundFetch(this.#valList[i])) {
10830
+ yield k;
10831
+ }
10832
+ }
10833
+ }
10834
+ /**
10835
+ * Return a generator yielding the values in the cache,
10836
+ * in order from most recently used to least recently used.
10837
+ */
10838
+ *values() {
10839
+ for (const i of this.#indexes()) {
10840
+ const v = this.#valList[i];
10841
+ if (v !== undefined &&
10842
+ !this.#isBackgroundFetch(this.#valList[i])) {
10843
+ yield this.#valList[i];
10844
+ }
10845
+ }
10846
+ }
10847
+ /**
10848
+ * Inverse order version of {@link LRUCache.values}
10849
+ *
10850
+ * Return a generator yielding the values in the cache,
10851
+ * in order from least recently used to most recently used.
10852
+ */
10853
+ *rvalues() {
10854
+ for (const i of this.#rindexes()) {
10855
+ const v = this.#valList[i];
10856
+ if (v !== undefined &&
10857
+ !this.#isBackgroundFetch(this.#valList[i])) {
10858
+ yield this.#valList[i];
10859
+ }
10860
+ }
10861
+ }
10862
+ /**
10863
+ * Iterating over the cache itself yields the same results as
10864
+ * {@link LRUCache.entries}
10865
+ */
10866
+ [Symbol.iterator]() {
10867
+ return this.entries();
10868
+ }
10869
+ /**
10870
+ * A String value that is used in the creation of the default string
10871
+ * description of an object. Called by the built-in method
10872
+ * `Object.prototype.toString`.
10873
+ */
10874
+ [Symbol.toStringTag] = 'LRUCache';
10875
+ /**
10876
+ * Find a value for which the supplied fn method returns a truthy value,
10877
+ * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
10878
+ */
10879
+ find(fn, getOptions = {}) {
10880
+ for (const i of this.#indexes()) {
10881
+ const v = this.#valList[i];
10882
+ const value = this.#isBackgroundFetch(v)
10883
+ ? v.__staleWhileFetching
10884
+ : v;
10885
+ if (value === undefined)
10886
+ continue;
10887
+ if (fn(value, this.#keyList[i], this)) {
10888
+ return this.get(this.#keyList[i], getOptions);
10889
+ }
10890
+ }
10891
+ }
10892
+ /**
10893
+ * Call the supplied function on each item in the cache, in order from most
10894
+ * recently used to least recently used.
10895
+ *
10896
+ * `fn` is called as `fn(value, key, cache)`.
10897
+ *
10898
+ * If `thisp` is provided, function will be called in the `this`-context of
10899
+ * the provided object, or the cache if no `thisp` object is provided.
10900
+ *
10901
+ * Does not update age or recenty of use, or iterate over stale values.
10902
+ */
10903
+ forEach(fn, thisp = this) {
10904
+ for (const i of this.#indexes()) {
10905
+ const v = this.#valList[i];
10906
+ const value = this.#isBackgroundFetch(v)
10907
+ ? v.__staleWhileFetching
10908
+ : v;
10909
+ if (value === undefined)
10910
+ continue;
10911
+ fn.call(thisp, value, this.#keyList[i], this);
10912
+ }
10913
+ }
10914
+ /**
10915
+ * The same as {@link LRUCache.forEach} but items are iterated over in
10916
+ * reverse order. (ie, less recently used items are iterated over first.)
10917
+ */
10918
+ rforEach(fn, thisp = this) {
10919
+ for (const i of this.#rindexes()) {
10920
+ const v = this.#valList[i];
10921
+ const value = this.#isBackgroundFetch(v)
10922
+ ? v.__staleWhileFetching
10923
+ : v;
10924
+ if (value === undefined)
10925
+ continue;
10926
+ fn.call(thisp, value, this.#keyList[i], this);
10927
+ }
10928
+ }
10929
+ /**
10930
+ * Delete any stale entries. Returns true if anything was removed,
10931
+ * false otherwise.
10932
+ */
10933
+ purgeStale() {
10934
+ let deleted = false;
10935
+ for (const i of this.#rindexes({ allowStale: true })) {
10936
+ if (this.#isStale(i)) {
10937
+ this.#delete(this.#keyList[i], 'expire');
10938
+ deleted = true;
10939
+ }
10940
+ }
10941
+ return deleted;
10942
+ }
10943
+ /**
10944
+ * Get the extended info about a given entry, to get its value, size, and
10945
+ * TTL info simultaneously. Returns `undefined` if the key is not present.
10946
+ *
10947
+ * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
10948
+ * serialization, the `start` value is always the current timestamp, and the
10949
+ * `ttl` is a calculated remaining time to live (negative if expired).
10950
+ *
10951
+ * Always returns stale values, if their info is found in the cache, so be
10952
+ * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
10953
+ * if relevant.
10954
+ */
10955
+ info(key) {
10956
+ const i = this.#keyMap.get(key);
10957
+ if (i === undefined)
10958
+ return undefined;
10959
+ const v = this.#valList[i];
10960
+ const value = this.#isBackgroundFetch(v)
10961
+ ? v.__staleWhileFetching
10962
+ : v;
10963
+ if (value === undefined)
10964
+ return undefined;
10965
+ const entry = { value };
10966
+ if (this.#ttls && this.#starts) {
10967
+ const ttl = this.#ttls[i];
10968
+ const start = this.#starts[i];
10969
+ if (ttl && start) {
10970
+ const remain = ttl - (perf.now() - start);
10971
+ entry.ttl = remain;
10972
+ entry.start = Date.now();
10973
+ }
10974
+ }
10975
+ if (this.#sizes) {
10976
+ entry.size = this.#sizes[i];
10977
+ }
10978
+ return entry;
10979
+ }
10980
+ /**
10981
+ * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
10982
+ * passed to {@link LRLUCache#load}.
10983
+ *
10984
+ * The `start` fields are calculated relative to a portable `Date.now()`
10985
+ * timestamp, even if `performance.now()` is available.
10986
+ *
10987
+ * Stale entries are always included in the `dump`, even if
10988
+ * {@link LRUCache.OptionsBase.allowStale} is false.
10989
+ *
10990
+ * Note: this returns an actual array, not a generator, so it can be more
10991
+ * easily passed around.
10992
+ */
10993
+ dump() {
10994
+ const arr = [];
10995
+ for (const i of this.#indexes({ allowStale: true })) {
10996
+ const key = this.#keyList[i];
10997
+ const v = this.#valList[i];
10998
+ const value = this.#isBackgroundFetch(v)
10999
+ ? v.__staleWhileFetching
11000
+ : v;
11001
+ if (value === undefined || key === undefined)
11002
+ continue;
11003
+ const entry = { value };
11004
+ if (this.#ttls && this.#starts) {
11005
+ entry.ttl = this.#ttls[i];
11006
+ // always dump the start relative to a portable timestamp
11007
+ // it's ok for this to be a bit slow, it's a rare operation.
11008
+ const age = perf.now() - this.#starts[i];
11009
+ entry.start = Math.floor(Date.now() - age);
11010
+ }
11011
+ if (this.#sizes) {
11012
+ entry.size = this.#sizes[i];
11013
+ }
11014
+ arr.unshift([key, entry]);
11015
+ }
11016
+ return arr;
11017
+ }
11018
+ /**
11019
+ * Reset the cache and load in the items in entries in the order listed.
11020
+ *
11021
+ * The shape of the resulting cache may be different if the same options are
11022
+ * not used in both caches.
11023
+ *
11024
+ * The `start` fields are assumed to be calculated relative to a portable
11025
+ * `Date.now()` timestamp, even if `performance.now()` is available.
11026
+ */
11027
+ load(arr) {
11028
+ this.clear();
11029
+ for (const [key, entry] of arr) {
11030
+ if (entry.start) {
11031
+ // entry.start is a portable timestamp, but we may be using
11032
+ // node's performance.now(), so calculate the offset, so that
11033
+ // we get the intended remaining TTL, no matter how long it's
11034
+ // been on ice.
11035
+ //
11036
+ // it's ok for this to be a bit slow, it's a rare operation.
11037
+ const age = Date.now() - entry.start;
11038
+ entry.start = perf.now() - age;
11039
+ }
11040
+ this.set(key, entry.value, entry);
11041
+ }
11042
+ }
11043
+ /**
11044
+ * Add a value to the cache.
11045
+ *
11046
+ * Note: if `undefined` is specified as a value, this is an alias for
11047
+ * {@link LRUCache#delete}
11048
+ *
11049
+ * Fields on the {@link LRUCache.SetOptions} options param will override
11050
+ * their corresponding values in the constructor options for the scope
11051
+ * of this single `set()` operation.
11052
+ *
11053
+ * If `start` is provided, then that will set the effective start
11054
+ * time for the TTL calculation. Note that this must be a previous
11055
+ * value of `performance.now()` if supported, or a previous value of
11056
+ * `Date.now()` if not.
11057
+ *
11058
+ * Options object may also include `size`, which will prevent
11059
+ * calling the `sizeCalculation` function and just use the specified
11060
+ * number if it is a positive integer, and `noDisposeOnSet` which
11061
+ * will prevent calling a `dispose` function in the case of
11062
+ * overwrites.
11063
+ *
11064
+ * If the `size` (or return value of `sizeCalculation`) for a given
11065
+ * entry is greater than `maxEntrySize`, then the item will not be
11066
+ * added to the cache.
11067
+ *
11068
+ * Will update the recency of the entry.
11069
+ *
11070
+ * If the value is `undefined`, then this is an alias for
11071
+ * `cache.delete(key)`. `undefined` is never stored in the cache.
11072
+ */
11073
+ set(k, v, setOptions = {}) {
11074
+ if (v === undefined) {
11075
+ this.delete(k);
11076
+ return this;
11077
+ }
11078
+ const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
11079
+ let { noUpdateTTL = this.noUpdateTTL } = setOptions;
11080
+ const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
11081
+ // if the item doesn't fit, don't do anything
11082
+ // NB: maxEntrySize set to maxSize by default
11083
+ if (this.maxEntrySize && size > this.maxEntrySize) {
11084
+ if (status) {
11085
+ status.set = 'miss';
11086
+ status.maxEntrySizeExceeded = true;
11087
+ }
11088
+ // have to delete, in case something is there already.
11089
+ this.#delete(k, 'set');
11090
+ return this;
11091
+ }
11092
+ let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
11093
+ if (index === undefined) {
11094
+ // addition
11095
+ index = (this.#size === 0
11096
+ ? this.#tail
11097
+ : this.#free.length !== 0
11098
+ ? this.#free.pop()
11099
+ : this.#size === this.#max
11100
+ ? this.#evict(false)
11101
+ : this.#size);
11102
+ this.#keyList[index] = k;
11103
+ this.#valList[index] = v;
11104
+ this.#keyMap.set(k, index);
11105
+ this.#next[this.#tail] = index;
11106
+ this.#prev[index] = this.#tail;
11107
+ this.#tail = index;
11108
+ this.#size++;
11109
+ this.#addItemSize(index, size, status);
11110
+ if (status)
11111
+ status.set = 'add';
11112
+ noUpdateTTL = false;
11113
+ }
11114
+ else {
11115
+ // update
11116
+ this.#moveToTail(index);
11117
+ const oldVal = this.#valList[index];
11118
+ if (v !== oldVal) {
11119
+ if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
11120
+ oldVal.__abortController.abort(new Error('replaced'));
11121
+ const { __staleWhileFetching: s } = oldVal;
11122
+ if (s !== undefined && !noDisposeOnSet) {
11123
+ if (this.#hasDispose) {
11124
+ this.#dispose?.(s, k, 'set');
11125
+ }
11126
+ if (this.#hasDisposeAfter) {
11127
+ this.#disposed?.push([s, k, 'set']);
11128
+ }
11129
+ }
11130
+ }
11131
+ else if (!noDisposeOnSet) {
11132
+ if (this.#hasDispose) {
11133
+ this.#dispose?.(oldVal, k, 'set');
11134
+ }
11135
+ if (this.#hasDisposeAfter) {
11136
+ this.#disposed?.push([oldVal, k, 'set']);
11137
+ }
11138
+ }
11139
+ this.#removeItemSize(index);
11140
+ this.#addItemSize(index, size, status);
11141
+ this.#valList[index] = v;
11142
+ if (status) {
11143
+ status.set = 'replace';
11144
+ const oldValue = oldVal && this.#isBackgroundFetch(oldVal)
11145
+ ? oldVal.__staleWhileFetching
11146
+ : oldVal;
11147
+ if (oldValue !== undefined)
11148
+ status.oldValue = oldValue;
11149
+ }
11150
+ }
11151
+ else if (status) {
11152
+ status.set = 'update';
11153
+ }
11154
+ }
11155
+ if (ttl !== 0 && !this.#ttls) {
11156
+ this.#initializeTTLTracking();
11157
+ }
11158
+ if (this.#ttls) {
11159
+ if (!noUpdateTTL) {
11160
+ this.#setItemTTL(index, ttl, start);
11161
+ }
11162
+ if (status)
11163
+ this.#statusTTL(status, index);
11164
+ }
11165
+ if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
11166
+ const dt = this.#disposed;
11167
+ let task;
11168
+ while ((task = dt?.shift())) {
11169
+ this.#disposeAfter?.(...task);
11170
+ }
11171
+ }
11172
+ return this;
11173
+ }
11174
+ /**
11175
+ * Evict the least recently used item, returning its value or
11176
+ * `undefined` if cache is empty.
11177
+ */
11178
+ pop() {
11179
+ try {
11180
+ while (this.#size) {
11181
+ const val = this.#valList[this.#head];
11182
+ this.#evict(true);
11183
+ if (this.#isBackgroundFetch(val)) {
11184
+ if (val.__staleWhileFetching) {
11185
+ return val.__staleWhileFetching;
11186
+ }
11187
+ }
11188
+ else if (val !== undefined) {
11189
+ return val;
11190
+ }
11191
+ }
11192
+ }
11193
+ finally {
11194
+ if (this.#hasDisposeAfter && this.#disposed) {
11195
+ const dt = this.#disposed;
11196
+ let task;
11197
+ while ((task = dt?.shift())) {
11198
+ this.#disposeAfter?.(...task);
11199
+ }
11200
+ }
11201
+ }
11202
+ }
11203
+ #evict(free) {
11204
+ const head = this.#head;
11205
+ const k = this.#keyList[head];
11206
+ const v = this.#valList[head];
11207
+ if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
11208
+ v.__abortController.abort(new Error('evicted'));
11209
+ }
11210
+ else if (this.#hasDispose || this.#hasDisposeAfter) {
11211
+ if (this.#hasDispose) {
11212
+ this.#dispose?.(v, k, 'evict');
11213
+ }
11214
+ if (this.#hasDisposeAfter) {
11215
+ this.#disposed?.push([v, k, 'evict']);
11216
+ }
11217
+ }
11218
+ this.#removeItemSize(head);
11219
+ // if we aren't about to use the index, then null these out
11220
+ if (free) {
11221
+ this.#keyList[head] = undefined;
11222
+ this.#valList[head] = undefined;
11223
+ this.#free.push(head);
11224
+ }
11225
+ if (this.#size === 1) {
11226
+ this.#head = this.#tail = 0;
11227
+ this.#free.length = 0;
11228
+ }
11229
+ else {
11230
+ this.#head = this.#next[head];
11231
+ }
11232
+ this.#keyMap.delete(k);
11233
+ this.#size--;
11234
+ return head;
11235
+ }
11236
+ /**
11237
+ * Check if a key is in the cache, without updating the recency of use.
11238
+ * Will return false if the item is stale, even though it is technically
11239
+ * in the cache.
11240
+ *
11241
+ * Check if a key is in the cache, without updating the recency of
11242
+ * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
11243
+ * to `true` in either the options or the constructor.
11244
+ *
11245
+ * Will return `false` if the item is stale, even though it is technically in
11246
+ * the cache. The difference can be determined (if it matters) by using a
11247
+ * `status` argument, and inspecting the `has` field.
11248
+ *
11249
+ * Will not update item age unless
11250
+ * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
11251
+ */
11252
+ has(k, hasOptions = {}) {
11253
+ const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
11254
+ const index = this.#keyMap.get(k);
11255
+ if (index !== undefined) {
11256
+ const v = this.#valList[index];
11257
+ if (this.#isBackgroundFetch(v) &&
11258
+ v.__staleWhileFetching === undefined) {
11259
+ return false;
11260
+ }
11261
+ if (!this.#isStale(index)) {
11262
+ if (updateAgeOnHas) {
11263
+ this.#updateItemAge(index);
11264
+ }
11265
+ if (status) {
11266
+ status.has = 'hit';
11267
+ this.#statusTTL(status, index);
11268
+ }
11269
+ return true;
11270
+ }
11271
+ else if (status) {
11272
+ status.has = 'stale';
11273
+ this.#statusTTL(status, index);
11274
+ }
11275
+ }
11276
+ else if (status) {
11277
+ status.has = 'miss';
11278
+ }
11279
+ return false;
11280
+ }
11281
+ /**
11282
+ * Like {@link LRUCache#get} but doesn't update recency or delete stale
11283
+ * items.
11284
+ *
11285
+ * Returns `undefined` if the item is stale, unless
11286
+ * {@link LRUCache.OptionsBase.allowStale} is set.
11287
+ */
11288
+ peek(k, peekOptions = {}) {
11289
+ const { allowStale = this.allowStale } = peekOptions;
11290
+ const index = this.#keyMap.get(k);
11291
+ if (index === undefined ||
11292
+ (!allowStale && this.#isStale(index))) {
11293
+ return;
11294
+ }
11295
+ const v = this.#valList[index];
11296
+ // either stale and allowed, or forcing a refresh of non-stale value
11297
+ return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
11298
+ }
11299
+ #backgroundFetch(k, index, options, context) {
11300
+ const v = index === undefined ? undefined : this.#valList[index];
11301
+ if (this.#isBackgroundFetch(v)) {
11302
+ return v;
11303
+ }
11304
+ const ac = new AC();
11305
+ const { signal } = options;
11306
+ // when/if our AC signals, then stop listening to theirs.
11307
+ signal?.addEventListener('abort', () => ac.abort(signal.reason), {
11308
+ signal: ac.signal,
11309
+ });
11310
+ const fetchOpts = {
11311
+ signal: ac.signal,
11312
+ options,
11313
+ context,
11314
+ };
11315
+ const cb = (v, updateCache = false) => {
11316
+ const { aborted } = ac.signal;
11317
+ const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
11318
+ if (options.status) {
11319
+ if (aborted && !updateCache) {
11320
+ options.status.fetchAborted = true;
11321
+ options.status.fetchError = ac.signal.reason;
11322
+ if (ignoreAbort)
11323
+ options.status.fetchAbortIgnored = true;
11324
+ }
11325
+ else {
11326
+ options.status.fetchResolved = true;
11327
+ }
11328
+ }
11329
+ if (aborted && !ignoreAbort && !updateCache) {
11330
+ return fetchFail(ac.signal.reason);
11331
+ }
11332
+ // either we didn't abort, and are still here, or we did, and ignored
11333
+ const bf = p;
11334
+ if (this.#valList[index] === p) {
11335
+ if (v === undefined) {
11336
+ if (bf.__staleWhileFetching) {
11337
+ this.#valList[index] = bf.__staleWhileFetching;
11338
+ }
11339
+ else {
11340
+ this.#delete(k, 'fetch');
11341
+ }
11342
+ }
11343
+ else {
11344
+ if (options.status)
11345
+ options.status.fetchUpdated = true;
11346
+ this.set(k, v, fetchOpts.options);
11347
+ }
11348
+ }
11349
+ return v;
11350
+ };
11351
+ const eb = (er) => {
11352
+ if (options.status) {
11353
+ options.status.fetchRejected = true;
11354
+ options.status.fetchError = er;
11355
+ }
11356
+ return fetchFail(er);
11357
+ };
11358
+ const fetchFail = (er) => {
11359
+ const { aborted } = ac.signal;
11360
+ const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
11361
+ const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
11362
+ const noDelete = allowStale || options.noDeleteOnFetchRejection;
11363
+ const bf = p;
11364
+ if (this.#valList[index] === p) {
11365
+ // if we allow stale on fetch rejections, then we need to ensure that
11366
+ // the stale value is not removed from the cache when the fetch fails.
11367
+ const del = !noDelete || bf.__staleWhileFetching === undefined;
11368
+ if (del) {
11369
+ this.#delete(k, 'fetch');
11370
+ }
11371
+ else if (!allowStaleAborted) {
11372
+ // still replace the *promise* with the stale value,
11373
+ // since we are done with the promise at this point.
11374
+ // leave it untouched if we're still waiting for an
11375
+ // aborted background fetch that hasn't yet returned.
11376
+ this.#valList[index] = bf.__staleWhileFetching;
11377
+ }
11378
+ }
11379
+ if (allowStale) {
11380
+ if (options.status && bf.__staleWhileFetching !== undefined) {
11381
+ options.status.returnedStale = true;
11382
+ }
11383
+ return bf.__staleWhileFetching;
11384
+ }
11385
+ else if (bf.__returned === bf) {
11386
+ throw er;
11387
+ }
11388
+ };
11389
+ const pcall = (res, rej) => {
11390
+ const fmp = this.#fetchMethod?.(k, v, fetchOpts);
11391
+ if (fmp && fmp instanceof Promise) {
11392
+ fmp.then(v => res(v === undefined ? undefined : v), rej);
11393
+ }
11394
+ // ignored, we go until we finish, regardless.
11395
+ // defer check until we are actually aborting,
11396
+ // so fetchMethod can override.
11397
+ ac.signal.addEventListener('abort', () => {
11398
+ if (!options.ignoreFetchAbort ||
11399
+ options.allowStaleOnFetchAbort) {
11400
+ res(undefined);
11401
+ // when it eventually resolves, update the cache.
11402
+ if (options.allowStaleOnFetchAbort) {
11403
+ res = v => cb(v, true);
11404
+ }
11405
+ }
11406
+ });
11407
+ };
11408
+ if (options.status)
11409
+ options.status.fetchDispatched = true;
11410
+ const p = new Promise(pcall).then(cb, eb);
11411
+ const bf = Object.assign(p, {
11412
+ __abortController: ac,
11413
+ __staleWhileFetching: v,
11414
+ __returned: undefined,
11415
+ });
11416
+ if (index === undefined) {
11417
+ // internal, don't expose status.
11418
+ this.set(k, bf, { ...fetchOpts.options, status: undefined });
11419
+ index = this.#keyMap.get(k);
11420
+ }
11421
+ else {
11422
+ this.#valList[index] = bf;
11423
+ }
11424
+ return bf;
11425
+ }
11426
+ #isBackgroundFetch(p) {
11427
+ if (!this.#hasFetchMethod)
11428
+ return false;
11429
+ const b = p;
11430
+ return (!!b &&
11431
+ b instanceof Promise &&
11432
+ b.hasOwnProperty('__staleWhileFetching') &&
11433
+ b.__abortController instanceof AC);
11434
+ }
11435
+ async fetch(k, fetchOptions = {}) {
11436
+ const {
11437
+ // get options
11438
+ allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
11439
+ // set options
11440
+ ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
11441
+ // fetch exclusive options
11442
+ noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
11443
+ if (!this.#hasFetchMethod) {
11444
+ if (status)
11445
+ status.fetch = 'get';
11446
+ return this.get(k, {
11447
+ allowStale,
11448
+ updateAgeOnGet,
11449
+ noDeleteOnStaleGet,
11450
+ status,
11451
+ });
11452
+ }
11453
+ const options = {
11454
+ allowStale,
11455
+ updateAgeOnGet,
11456
+ noDeleteOnStaleGet,
11457
+ ttl,
11458
+ noDisposeOnSet,
11459
+ size,
11460
+ sizeCalculation,
11461
+ noUpdateTTL,
11462
+ noDeleteOnFetchRejection,
11463
+ allowStaleOnFetchRejection,
11464
+ allowStaleOnFetchAbort,
11465
+ ignoreFetchAbort,
11466
+ status,
11467
+ signal,
11468
+ };
11469
+ let index = this.#keyMap.get(k);
11470
+ if (index === undefined) {
11471
+ if (status)
11472
+ status.fetch = 'miss';
11473
+ const p = this.#backgroundFetch(k, index, options, context);
11474
+ return (p.__returned = p);
11475
+ }
11476
+ else {
11477
+ // in cache, maybe already fetching
11478
+ const v = this.#valList[index];
11479
+ if (this.#isBackgroundFetch(v)) {
11480
+ const stale = allowStale && v.__staleWhileFetching !== undefined;
11481
+ if (status) {
11482
+ status.fetch = 'inflight';
11483
+ if (stale)
11484
+ status.returnedStale = true;
11485
+ }
11486
+ return stale ? v.__staleWhileFetching : (v.__returned = v);
11487
+ }
11488
+ // if we force a refresh, that means do NOT serve the cached value,
11489
+ // unless we are already in the process of refreshing the cache.
11490
+ const isStale = this.#isStale(index);
11491
+ if (!forceRefresh && !isStale) {
11492
+ if (status)
11493
+ status.fetch = 'hit';
11494
+ this.#moveToTail(index);
11495
+ if (updateAgeOnGet) {
11496
+ this.#updateItemAge(index);
11497
+ }
11498
+ if (status)
11499
+ this.#statusTTL(status, index);
11500
+ return v;
11501
+ }
11502
+ // ok, it is stale or a forced refresh, and not already fetching.
11503
+ // refresh the cache.
11504
+ const p = this.#backgroundFetch(k, index, options, context);
11505
+ const hasStale = p.__staleWhileFetching !== undefined;
11506
+ const staleVal = hasStale && allowStale;
11507
+ if (status) {
11508
+ status.fetch = isStale ? 'stale' : 'refresh';
11509
+ if (staleVal && isStale)
11510
+ status.returnedStale = true;
11511
+ }
11512
+ return staleVal ? p.__staleWhileFetching : (p.__returned = p);
11513
+ }
11514
+ }
11515
+ async forceFetch(k, fetchOptions = {}) {
11516
+ const v = await this.fetch(k, fetchOptions);
11517
+ if (v === undefined)
11518
+ throw new Error('fetch() returned undefined');
11519
+ return v;
11520
+ }
11521
+ memo(k, memoOptions = {}) {
11522
+ const memoMethod = this.#memoMethod;
11523
+ if (!memoMethod) {
11524
+ throw new Error('no memoMethod provided to constructor');
11525
+ }
11526
+ const { context, forceRefresh, ...options } = memoOptions;
11527
+ const v = this.get(k, options);
11528
+ if (!forceRefresh && v !== undefined)
11529
+ return v;
11530
+ const vv = memoMethod(k, v, {
11531
+ options,
11532
+ context,
11533
+ });
11534
+ this.set(k, vv, options);
11535
+ return vv;
11536
+ }
11537
+ /**
11538
+ * Return a value from the cache. Will update the recency of the cache
11539
+ * entry found.
11540
+ *
11541
+ * If the key is not found, get() will return `undefined`.
11542
+ */
11543
+ get(k, getOptions = {}) {
11544
+ const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
11545
+ const index = this.#keyMap.get(k);
11546
+ if (index !== undefined) {
11547
+ const value = this.#valList[index];
11548
+ const fetching = this.#isBackgroundFetch(value);
11549
+ if (status)
11550
+ this.#statusTTL(status, index);
11551
+ if (this.#isStale(index)) {
11552
+ if (status)
11553
+ status.get = 'stale';
11554
+ // delete only if not an in-flight background fetch
11555
+ if (!fetching) {
11556
+ if (!noDeleteOnStaleGet) {
11557
+ this.#delete(k, 'expire');
11558
+ }
11559
+ if (status && allowStale)
11560
+ status.returnedStale = true;
11561
+ return allowStale ? value : undefined;
11562
+ }
11563
+ else {
11564
+ if (status &&
11565
+ allowStale &&
11566
+ value.__staleWhileFetching !== undefined) {
11567
+ status.returnedStale = true;
11568
+ }
11569
+ return allowStale ? value.__staleWhileFetching : undefined;
11570
+ }
11571
+ }
11572
+ else {
11573
+ if (status)
11574
+ status.get = 'hit';
11575
+ // if we're currently fetching it, we don't actually have it yet
11576
+ // it's not stale, which means this isn't a staleWhileRefetching.
11577
+ // If it's not stale, and fetching, AND has a __staleWhileFetching
11578
+ // value, then that means the user fetched with {forceRefresh:true},
11579
+ // so it's safe to return that value.
11580
+ if (fetching) {
11581
+ return value.__staleWhileFetching;
11582
+ }
11583
+ this.#moveToTail(index);
11584
+ if (updateAgeOnGet) {
11585
+ this.#updateItemAge(index);
11586
+ }
11587
+ return value;
11588
+ }
11589
+ }
11590
+ else if (status) {
11591
+ status.get = 'miss';
11592
+ }
11593
+ }
11594
+ #connect(p, n) {
11595
+ this.#prev[n] = p;
11596
+ this.#next[p] = n;
11597
+ }
11598
+ #moveToTail(index) {
11599
+ // if tail already, nothing to do
11600
+ // if head, move head to next[index]
11601
+ // else
11602
+ // move next[prev[index]] to next[index] (head has no prev)
11603
+ // move prev[next[index]] to prev[index]
11604
+ // prev[index] = tail
11605
+ // next[tail] = index
11606
+ // tail = index
11607
+ if (index !== this.#tail) {
11608
+ if (index === this.#head) {
11609
+ this.#head = this.#next[index];
11610
+ }
11611
+ else {
11612
+ this.#connect(this.#prev[index], this.#next[index]);
11613
+ }
11614
+ this.#connect(this.#tail, index);
11615
+ this.#tail = index;
11616
+ }
11617
+ }
11618
+ /**
11619
+ * Deletes a key out of the cache.
11620
+ *
11621
+ * Returns true if the key was deleted, false otherwise.
11622
+ */
11623
+ delete(k) {
11624
+ return this.#delete(k, 'delete');
11625
+ }
11626
+ #delete(k, reason) {
11627
+ let deleted = false;
11628
+ if (this.#size !== 0) {
11629
+ const index = this.#keyMap.get(k);
11630
+ if (index !== undefined) {
11631
+ deleted = true;
11632
+ if (this.#size === 1) {
11633
+ this.#clear(reason);
11634
+ }
11635
+ else {
11636
+ this.#removeItemSize(index);
11637
+ const v = this.#valList[index];
11638
+ if (this.#isBackgroundFetch(v)) {
11639
+ v.__abortController.abort(new Error('deleted'));
11640
+ }
11641
+ else if (this.#hasDispose || this.#hasDisposeAfter) {
11642
+ if (this.#hasDispose) {
11643
+ this.#dispose?.(v, k, reason);
11644
+ }
11645
+ if (this.#hasDisposeAfter) {
11646
+ this.#disposed?.push([v, k, reason]);
11647
+ }
11648
+ }
11649
+ this.#keyMap.delete(k);
11650
+ this.#keyList[index] = undefined;
11651
+ this.#valList[index] = undefined;
11652
+ if (index === this.#tail) {
11653
+ this.#tail = this.#prev[index];
11654
+ }
11655
+ else if (index === this.#head) {
11656
+ this.#head = this.#next[index];
11657
+ }
11658
+ else {
11659
+ const pi = this.#prev[index];
11660
+ this.#next[pi] = this.#next[index];
11661
+ const ni = this.#next[index];
11662
+ this.#prev[ni] = this.#prev[index];
11663
+ }
11664
+ this.#size--;
11665
+ this.#free.push(index);
11666
+ }
11667
+ }
11668
+ }
11669
+ if (this.#hasDisposeAfter && this.#disposed?.length) {
11670
+ const dt = this.#disposed;
11671
+ let task;
11672
+ while ((task = dt?.shift())) {
11673
+ this.#disposeAfter?.(...task);
11674
+ }
11675
+ }
11676
+ return deleted;
11677
+ }
11678
+ /**
11679
+ * Clear the cache entirely, throwing away all values.
11680
+ */
11681
+ clear() {
11682
+ return this.#clear('delete');
11683
+ }
11684
+ #clear(reason) {
11685
+ for (const index of this.#rindexes({ allowStale: true })) {
11686
+ const v = this.#valList[index];
11687
+ if (this.#isBackgroundFetch(v)) {
11688
+ v.__abortController.abort(new Error('deleted'));
11689
+ }
11690
+ else {
11691
+ const k = this.#keyList[index];
11692
+ if (this.#hasDispose) {
11693
+ this.#dispose?.(v, k, reason);
11694
+ }
11695
+ if (this.#hasDisposeAfter) {
11696
+ this.#disposed?.push([v, k, reason]);
11697
+ }
11698
+ }
11699
+ }
11700
+ this.#keyMap.clear();
11701
+ this.#valList.fill(undefined);
11702
+ this.#keyList.fill(undefined);
11703
+ if (this.#ttls && this.#starts) {
11704
+ this.#ttls.fill(0);
11705
+ this.#starts.fill(0);
11706
+ }
11707
+ if (this.#sizes) {
11708
+ this.#sizes.fill(0);
11709
+ }
11710
+ this.#head = 0;
11711
+ this.#tail = 0;
11712
+ this.#free.length = 0;
11713
+ this.#calculatedSize = 0;
11714
+ this.#size = 0;
11715
+ if (this.#hasDisposeAfter && this.#disposed) {
11716
+ const dt = this.#disposed;
11717
+ let task;
11718
+ while ((task = dt?.shift())) {
11719
+ this.#disposeAfter?.(...task);
11720
+ }
11721
+ }
10920
11722
  }
10921
- }
10922
- };
10923
-
10924
- const del = (self, node) => {
10925
- if (node) {
10926
- const hit = node.value;
10927
- if (self[DISPOSE])
10928
- self[DISPOSE](hit.key, hit.value);
10929
-
10930
- self[LENGTH] -= hit.length;
10931
- self[CACHE].delete(hit.key);
10932
- self[LRU_LIST].removeNode(node);
10933
- }
10934
- };
10935
-
10936
- class Entry {
10937
- constructor (key, value, length, now, maxAge) {
10938
- this.key = key;
10939
- this.value = value;
10940
- this.length = length;
10941
- this.now = now;
10942
- this.maxAge = maxAge || 0;
10943
- }
10944
11723
  }
10945
11724
 
10946
- const forEachStep = (self, fn, node, thisp) => {
10947
- let hit = node.value;
10948
- if (isStale(self, hit)) {
10949
- del(self, node);
10950
- if (!self[ALLOW_STALE])
10951
- hit = undefined;
10952
- }
10953
- if (hit)
10954
- fn.call(thisp, hit.value, hit.key, self);
10955
- };
10956
-
10957
- var lruCache = LRUCache;
10958
-
10959
11725
  var Constants = {};
10960
11726
 
10961
11727
  var Namespaces = {};
@@ -31213,7 +31979,11 @@ const ICE_SERVER_REFRESH_PERIOD = 1000 * 60 * 60 * 6; // 6 hours
31213
31979
  class WebrtcExtension extends EventEmitter {
31214
31980
  constructor(client, clientOptions) {
31215
31981
  super();
31216
- this.ignoredSessions = new lruCache({ max: 10, maxAge: 10 * 60 * 60 * 6 });
31982
+ // sessionId maps to boolean where `true` means the sessionId is ignored
31983
+ this.ignoredSessions = new LRUCache({ max: 10, ttl: 10 * 60 * 60 * 6 });
31984
+ // sessionId maps to a list of sdp ice candidates
31985
+ // hold onto early candidates for 1 minute
31986
+ this.earlyIceCandidates = new LRUCache({ max: 10, ttl: 1000 * 60, ttlAutopurge: true });
31217
31987
  this.pendingSessions = {};
31218
31988
  this.statsArr = [];
31219
31989
  this.throttleSendStatsInterval = 25000;
@@ -31366,10 +32136,20 @@ class WebrtcExtension extends EventEmitter {
31366
32136
  this.webrtcSessions = this.webrtcSessions.filter(s => s.id !== session.id);
31367
32137
  });
31368
32138
  this.webrtcSessions.push(session);
31369
- this.logger.info('emitting sdp media-session (offer)');
32139
+ this.logger.info('emitting sdp media-session (offer');
32140
+ this.applyEarlyIceCandidates(session);
31370
32141
  return this.emit(events.INCOMING_RTCSESSION, session);
31371
32142
  });
31372
32143
  }
32144
+ applyEarlyIceCandidates(session) {
32145
+ const earlyCandidates = this.earlyIceCandidates.get(session.id);
32146
+ if (earlyCandidates) {
32147
+ this.earlyIceCandidates.delete(session.id);
32148
+ for (const candidate of earlyCandidates) {
32149
+ void session.addRemoteIceCandidate(candidate);
32150
+ }
32151
+ }
32152
+ }
31373
32153
  handleGenesysRenegotiate(existingSession, newSdp) {
31374
32154
  return __awaiter$5(this, void 0, void 0, function* () {
31375
32155
  yield existingSession.peerConnection.setRemoteDescription({ sdp: newSdp, type: 'offer' });
@@ -31380,8 +32160,19 @@ class WebrtcExtension extends EventEmitter {
31380
32160
  return __awaiter$5(this, void 0, void 0, function* () {
31381
32161
  const message = iq.genesysWebrtc;
31382
32162
  const params = message.params;
31383
- const session = this.getSessionById(params.sessionId);
31384
- yield session.addRemoteIceCandidate(params.sdp);
32163
+ const session = this.getSessionById(params.sessionId, true);
32164
+ if (session) {
32165
+ yield session.addRemoteIceCandidate(params.sdp);
32166
+ }
32167
+ else {
32168
+ const earlyCandidates = this.earlyIceCandidates.get(params.sessionId);
32169
+ if (earlyCandidates) {
32170
+ this.earlyIceCandidates.set(params.sessionId, [...earlyCandidates, params.sdp]);
32171
+ }
32172
+ else {
32173
+ this.earlyIceCandidates.set(params.sessionId, [params.sdp]);
32174
+ }
32175
+ }
31385
32176
  });
31386
32177
  }
31387
32178
  handleGenesysTerminate(iq) {
@@ -31392,9 +32183,9 @@ class WebrtcExtension extends EventEmitter {
31392
32183
  session.onSessionTerminate(params.reason);
31393
32184
  });
31394
32185
  }
31395
- getSessionById(id) {
32186
+ getSessionById(id, nullIfNotFound = false) {
31396
32187
  const session = this.getAllSessions().find(session => session.id === id);
31397
- if (!session) {
32188
+ if (!session && !nullIfNotFound) {
31398
32189
  const error = new Error('Failed to find session by id');
31399
32190
  this.logger.error(error, { sessionId: id });
31400
32191
  throw error;
@@ -42655,7 +43446,7 @@ class Client extends EventEmitter {
42655
43446
  return Client.version;
42656
43447
  }
42657
43448
  static get version() {
42658
- return '18.0.0';
43449
+ return '18.0.1';
42659
43450
  }
42660
43451
  }
42661
43452