@psnext/slingcli 3.3.20260915-1 → 3.3.20260922-1

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.
Files changed (49) hide show
  1. package/node_modules/@types/node/README.md +2 -2
  2. package/node_modules/@types/node/child_process.d.ts +1 -1
  3. package/node_modules/@types/node/crypto.d.ts +20 -8
  4. package/node_modules/@types/node/ffi.d.ts +14 -0
  5. package/node_modules/@types/node/http2.d.ts +3 -2
  6. package/node_modules/@types/node/net.d.ts +16 -4
  7. package/node_modules/@types/node/package.json +2 -17
  8. package/node_modules/@types/node/process.d.ts +1 -0
  9. package/node_modules/@types/node/quic.d.ts +39 -1
  10. package/node_modules/@types/node/stream/iter.d.ts +9 -12
  11. package/node_modules/@types/node/test/reporters.d.ts +1 -0
  12. package/node_modules/@types/node/test.d.ts +72 -0
  13. package/node_modules/@types/node/tls.d.ts +14 -10
  14. package/node_modules/@types/node/vm.d.ts +2 -1
  15. package/node_modules/@types/node/worker_threads.d.ts +4 -1
  16. package/node_modules/ip-address/README.md +97 -97
  17. package/node_modules/ip-address/dist/ipv4.d.ts +5 -2
  18. package/node_modules/ip-address/dist/ipv4.js +8 -4
  19. package/node_modules/ip-address/dist/ipv4.js.map +1 -1
  20. package/node_modules/ip-address/dist/ipv6.js +3 -2
  21. package/node_modules/ip-address/dist/ipv6.js.map +1 -1
  22. package/node_modules/ip-address/package.json +1 -1
  23. package/node_modules/lru-cache/dist/commonjs/react-native/diagnostics-channel-react-native.d.ts.map +1 -0
  24. package/node_modules/lru-cache/dist/commonjs/react-native/diagnostics-channel-react-native.js.map +1 -0
  25. package/node_modules/lru-cache/dist/commonjs/react-native/diagnostics-channel.d.ts +5 -0
  26. package/node_modules/lru-cache/dist/commonjs/react-native/diagnostics-channel.js +7 -0
  27. package/node_modules/lru-cache/dist/commonjs/react-native/index.d.ts +1400 -0
  28. package/node_modules/lru-cache/dist/commonjs/react-native/index.d.ts.map +1 -0
  29. package/node_modules/lru-cache/dist/commonjs/react-native/index.js +1733 -0
  30. package/node_modules/lru-cache/dist/commonjs/react-native/index.js.map +1 -0
  31. package/node_modules/lru-cache/dist/commonjs/react-native/perf.d.ts +12 -0
  32. package/node_modules/lru-cache/dist/commonjs/react-native/perf.d.ts.map +1 -0
  33. package/node_modules/lru-cache/dist/commonjs/react-native/perf.js +10 -0
  34. package/node_modules/lru-cache/dist/commonjs/react-native/perf.js.map +1 -0
  35. package/node_modules/lru-cache/dist/esm/react-native/diagnostics-channel-react-native.d.ts.map +1 -0
  36. package/node_modules/lru-cache/dist/esm/react-native/diagnostics-channel-react-native.js.map +1 -0
  37. package/node_modules/lru-cache/dist/esm/react-native/diagnostics-channel.d.ts +5 -0
  38. package/node_modules/lru-cache/dist/esm/react-native/diagnostics-channel.js +4 -0
  39. package/node_modules/lru-cache/dist/esm/react-native/index.d.ts +1400 -0
  40. package/node_modules/lru-cache/dist/esm/react-native/index.d.ts.map +1 -0
  41. package/node_modules/lru-cache/dist/esm/react-native/index.js +1729 -0
  42. package/node_modules/lru-cache/dist/esm/react-native/index.js.map +1 -0
  43. package/node_modules/lru-cache/dist/esm/react-native/perf.d.ts +12 -0
  44. package/node_modules/lru-cache/dist/esm/react-native/perf.d.ts.map +1 -0
  45. package/node_modules/lru-cache/dist/esm/react-native/perf.js +7 -0
  46. package/node_modules/lru-cache/dist/esm/react-native/perf.js.map +1 -0
  47. package/node_modules/lru-cache/package.json +27 -1
  48. package/package.json +2 -2
  49. package/slingshot/index.js +176 -176
@@ -0,0 +1,1733 @@
1
+ "use strict";
2
+ /**
3
+ * @module LRUCache
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LRUCache = void 0;
7
+ const diagnostics_channel_js_1 = require("./diagnostics-channel.js");
8
+ const perf_js_1 = require("./perf.js");
9
+ const hasSubscribers = () => diagnostics_channel_js_1.metrics.hasSubscribers || diagnostics_channel_js_1.tracing.hasSubscribers;
10
+ const warned = new Set();
11
+ /* c8 ignore start */
12
+ const PROCESS = (typeof process === 'object' && !!process ?
13
+ process
14
+ : {});
15
+ /* c8 ignore stop */
16
+ const emitWarning = (msg, type, code, fn) => {
17
+ if (typeof PROCESS.emitWarning === 'function') {
18
+ PROCESS.emitWarning(msg, type, code, fn);
19
+ }
20
+ else {
21
+ //oxlint-disable-next-line no-console
22
+ console.error(`[${code}] ${type}: ${msg}`);
23
+ }
24
+ };
25
+ const shouldWarn = (code) => !warned.has(code);
26
+ const TYPE = Symbol('type');
27
+ const isPosInt = (n) => !!n && n === Math.floor(n) && n > 0 && isFinite(n);
28
+ // This is a little bit ridiculous, tbh.
29
+ // The maximum array length is 2^32-1 or thereabouts on most JS impls.
30
+ // And well before that point, you're caching the entire world, I mean,
31
+ // that's ~32GB of just integers for the next/prev links, plus whatever
32
+ // else to hold that many keys and values. Just filling the memory with
33
+ // zeroes at init time is brutal when you get that big.
34
+ // But why not be complete?
35
+ // Maybe in the future, these limits will have expanded.
36
+ /* c8 ignore start */
37
+ const getUintArray = (max) => !isPosInt(max) ? null
38
+ : max <= Math.pow(2, 8) ? Uint8Array
39
+ : max <= Math.pow(2, 16) ? Uint16Array
40
+ : max <= Math.pow(2, 32) ? Uint32Array
41
+ : max <= Number.MAX_SAFE_INTEGER ? ZeroArray
42
+ : null;
43
+ /* c8 ignore stop */
44
+ class ZeroArray extends Array {
45
+ constructor(size) {
46
+ super(size);
47
+ this.fill(0);
48
+ }
49
+ }
50
+ class Stack {
51
+ /* c8 ignore start - not sure why this is showing up uncovered?? */
52
+ heap;
53
+ /* c8 ignore stop */
54
+ length;
55
+ // private constructor
56
+ static #constructing = false;
57
+ static create(max) {
58
+ const HeapCls = getUintArray(max);
59
+ if (!HeapCls)
60
+ return [];
61
+ Stack.#constructing = true;
62
+ const s = new Stack(max, HeapCls);
63
+ Stack.#constructing = false;
64
+ return s;
65
+ }
66
+ constructor(max, HeapCls) {
67
+ /* c8 ignore start */
68
+ if (!Stack.#constructing) {
69
+ throw new TypeError('instantiate Stack using Stack.create(n)');
70
+ }
71
+ /* c8 ignore stop */
72
+ this.heap = new HeapCls(max);
73
+ this.length = 0;
74
+ }
75
+ push(n) {
76
+ this.heap[this.length++] = n;
77
+ }
78
+ pop() {
79
+ return this.heap[--this.length];
80
+ }
81
+ }
82
+ /**
83
+ * Default export, the thing you're using this module to get.
84
+ *
85
+ * The `K` and `V` types define the key and value types, respectively. The
86
+ * optional `FC` type defines the type of the `context` object passed to
87
+ * `cache.fetch()` and `cache.memo()`.
88
+ *
89
+ * Keys and values **must not** be `null` or `undefined`.
90
+ *
91
+ * All properties from the options object (with the exception of `max`,
92
+ * `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
93
+ * added as normal public members. (The listed options are read-only getters.)
94
+ *
95
+ * Changing any of these will alter the defaults for subsequent method calls.
96
+ */
97
+ class LRUCache {
98
+ // options that cannot be changed without disaster
99
+ #max;
100
+ #maxSize;
101
+ #dispose;
102
+ #onInsert;
103
+ #disposeAfter;
104
+ #fetchMethod;
105
+ #memoMethod;
106
+ #perf;
107
+ /**
108
+ * {@link LRUCache.OptionsBase.perf}
109
+ */
110
+ get perf() {
111
+ return this.#perf;
112
+ }
113
+ /**
114
+ * {@link LRUCache.OptionsBase.ttl}
115
+ */
116
+ ttl;
117
+ /**
118
+ * {@link LRUCache.OptionsBase.ttlResolution}
119
+ */
120
+ ttlResolution;
121
+ /**
122
+ * {@link LRUCache.OptionsBase.ttlAutopurge}
123
+ */
124
+ ttlAutopurge;
125
+ /**
126
+ * {@link LRUCache.OptionsBase.updateAgeOnGet}
127
+ */
128
+ updateAgeOnGet;
129
+ /**
130
+ * {@link LRUCache.OptionsBase.updateAgeOnHas}
131
+ */
132
+ updateAgeOnHas;
133
+ /**
134
+ * {@link LRUCache.OptionsBase.allowStale}
135
+ */
136
+ allowStale;
137
+ /**
138
+ * {@link LRUCache.OptionsBase.noDisposeOnSet}
139
+ */
140
+ noDisposeOnSet;
141
+ /**
142
+ * {@link LRUCache.OptionsBase.noUpdateTTL}
143
+ */
144
+ noUpdateTTL;
145
+ /**
146
+ * {@link LRUCache.OptionsBase.maxEntrySize}
147
+ */
148
+ maxEntrySize;
149
+ /**
150
+ * {@link LRUCache.OptionsBase.sizeCalculation}
151
+ */
152
+ sizeCalculation;
153
+ /**
154
+ * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
155
+ */
156
+ noDeleteOnFetchRejection;
157
+ /**
158
+ * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
159
+ */
160
+ noDeleteOnStaleGet;
161
+ /**
162
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
163
+ */
164
+ allowStaleOnFetchAbort;
165
+ /**
166
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
167
+ */
168
+ allowStaleOnFetchRejection;
169
+ /**
170
+ * {@link LRUCache.OptionsBase.ignoreFetchAbort}
171
+ */
172
+ ignoreFetchAbort;
173
+ /** {@link LRUCache.OptionsBase.backgroundFetchSize} */
174
+ backgroundFetchSize;
175
+ // computed properties
176
+ #size;
177
+ #calculatedSize;
178
+ #keyMap;
179
+ #keyList;
180
+ #valList;
181
+ #next;
182
+ #prev;
183
+ #head;
184
+ #tail;
185
+ #free;
186
+ #disposed;
187
+ #sizes;
188
+ #starts;
189
+ #ttls;
190
+ #autopurgeTimers;
191
+ #hasDispose;
192
+ #hasFetchMethod;
193
+ #hasDisposeAfter;
194
+ #hasOnInsert;
195
+ /**
196
+ * Do not call this method unless you need to inspect the
197
+ * inner workings of the cache. If anything returned by this
198
+ * object is modified in any way, strange breakage may occur.
199
+ *
200
+ * These fields are private for a reason!
201
+ *
202
+ * @internal
203
+ */
204
+ static unsafeExposeInternals(c) {
205
+ return {
206
+ // properties
207
+ starts: c.#starts,
208
+ ttls: c.#ttls,
209
+ autopurgeTimers: c.#autopurgeTimers,
210
+ sizes: c.#sizes,
211
+ keyMap: c.#keyMap,
212
+ keyList: c.#keyList,
213
+ valList: c.#valList,
214
+ next: c.#next,
215
+ prev: c.#prev,
216
+ get head() {
217
+ return c.#head;
218
+ },
219
+ get tail() {
220
+ return c.#tail;
221
+ },
222
+ free: c.#free,
223
+ // methods
224
+ isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
225
+ backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
226
+ moveToTail: (index) => c.#moveToTail(index),
227
+ indexes: (options) => c.#indexes(options),
228
+ rindexes: (options) => c.#rindexes(options),
229
+ isStale: (index) => c.#isStale(index),
230
+ };
231
+ }
232
+ // Protected read-only members
233
+ /**
234
+ * {@link LRUCache.OptionsBase.max} (read-only)
235
+ */
236
+ get max() {
237
+ return this.#max;
238
+ }
239
+ /**
240
+ * {@link LRUCache.OptionsBase.maxSize} (read-only)
241
+ */
242
+ get maxSize() {
243
+ return this.#maxSize;
244
+ }
245
+ /**
246
+ * The total computed size of items in the cache (read-only)
247
+ */
248
+ get calculatedSize() {
249
+ return this.#calculatedSize;
250
+ }
251
+ /**
252
+ * The number of items stored in the cache (read-only)
253
+ */
254
+ get size() {
255
+ return this.#size;
256
+ }
257
+ /**
258
+ * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
259
+ */
260
+ get fetchMethod() {
261
+ return this.#fetchMethod;
262
+ }
263
+ get memoMethod() {
264
+ return this.#memoMethod;
265
+ }
266
+ /**
267
+ * {@link LRUCache.OptionsBase.dispose} (read-only)
268
+ */
269
+ get dispose() {
270
+ return this.#dispose;
271
+ }
272
+ /**
273
+ * {@link LRUCache.OptionsBase.onInsert} (read-only)
274
+ */
275
+ get onInsert() {
276
+ return this.#onInsert;
277
+ }
278
+ /**
279
+ * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
280
+ */
281
+ get disposeAfter() {
282
+ return this.#disposeAfter;
283
+ }
284
+ constructor(options) {
285
+ const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, onInsert, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, backgroundFetchSize = 1, perf, } = options;
286
+ this.backgroundFetchSize = backgroundFetchSize;
287
+ if (perf !== undefined) {
288
+ if (typeof perf?.now !== 'function') {
289
+ throw new TypeError('perf option must have a now() method if specified');
290
+ }
291
+ }
292
+ this.#perf = perf ?? perf_js_1.defaultPerf;
293
+ if (max !== 0 && !isPosInt(max)) {
294
+ throw new TypeError('max option must be a nonnegative integer');
295
+ }
296
+ const UintArray = max ? getUintArray(max) : Array;
297
+ if (!UintArray) {
298
+ throw new Error('invalid max value: ' + max);
299
+ }
300
+ this.#max = max;
301
+ this.#maxSize = maxSize;
302
+ this.maxEntrySize = maxEntrySize || this.#maxSize;
303
+ this.sizeCalculation = sizeCalculation;
304
+ if (this.sizeCalculation) {
305
+ if (!this.#maxSize && !this.maxEntrySize) {
306
+ throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
307
+ }
308
+ if (typeof this.sizeCalculation !== 'function') {
309
+ throw new TypeError('sizeCalculation set to non-function');
310
+ }
311
+ }
312
+ if (memoMethod !== undefined && typeof memoMethod !== 'function') {
313
+ throw new TypeError('memoMethod must be a function if defined');
314
+ }
315
+ this.#memoMethod = memoMethod;
316
+ if (fetchMethod !== undefined && typeof fetchMethod !== 'function') {
317
+ throw new TypeError('fetchMethod must be a function if specified');
318
+ }
319
+ this.#fetchMethod = fetchMethod;
320
+ this.#hasFetchMethod = !!fetchMethod;
321
+ this.#keyMap = new Map();
322
+ this.#keyList = Array.from({ length: max }).fill(undefined);
323
+ this.#valList = Array.from({ length: max }).fill(undefined);
324
+ this.#next = new UintArray(max);
325
+ this.#prev = new UintArray(max);
326
+ this.#head = 0;
327
+ this.#tail = 0;
328
+ this.#free = Stack.create(max);
329
+ this.#size = 0;
330
+ this.#calculatedSize = 0;
331
+ if (typeof dispose === 'function') {
332
+ this.#dispose = dispose;
333
+ }
334
+ if (typeof onInsert === 'function') {
335
+ this.#onInsert = onInsert;
336
+ }
337
+ if (typeof disposeAfter === 'function') {
338
+ this.#disposeAfter = disposeAfter;
339
+ this.#disposed = [];
340
+ }
341
+ else {
342
+ this.#disposeAfter = undefined;
343
+ this.#disposed = undefined;
344
+ }
345
+ this.#hasDispose = !!this.#dispose;
346
+ this.#hasOnInsert = !!this.#onInsert;
347
+ this.#hasDisposeAfter = !!this.#disposeAfter;
348
+ this.noDisposeOnSet = !!noDisposeOnSet;
349
+ this.noUpdateTTL = !!noUpdateTTL;
350
+ this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
351
+ this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
352
+ this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
353
+ this.ignoreFetchAbort = !!ignoreFetchAbort;
354
+ // NB: maxEntrySize is set to maxSize if it's set
355
+ if (this.maxEntrySize !== 0) {
356
+ if (this.#maxSize !== 0) {
357
+ if (!isPosInt(this.#maxSize)) {
358
+ throw new TypeError('maxSize must be a positive integer if specified');
359
+ }
360
+ }
361
+ if (!isPosInt(this.maxEntrySize)) {
362
+ throw new TypeError('maxEntrySize must be a positive integer if specified');
363
+ }
364
+ this.#initializeSizeTracking();
365
+ }
366
+ this.allowStale = !!allowStale;
367
+ this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
368
+ this.updateAgeOnGet = !!updateAgeOnGet;
369
+ this.updateAgeOnHas = !!updateAgeOnHas;
370
+ this.ttlResolution =
371
+ isPosInt(ttlResolution) || ttlResolution === 0 ? ttlResolution : 1;
372
+ this.ttlAutopurge = !!ttlAutopurge;
373
+ this.ttl = ttl || 0;
374
+ if (this.ttl) {
375
+ if (!isPosInt(this.ttl)) {
376
+ throw new TypeError('ttl must be a positive integer if specified');
377
+ }
378
+ this.#initializeTTLTracking();
379
+ }
380
+ // do not allow completely unbounded caches
381
+ if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
382
+ throw new TypeError('At least one of max, maxSize, or ttl is required');
383
+ }
384
+ if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
385
+ const code = 'LRU_CACHE_UNBOUNDED';
386
+ if (shouldWarn(code)) {
387
+ warned.add(code);
388
+ const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
389
+ 'result in unbounded memory consumption.';
390
+ emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
391
+ }
392
+ }
393
+ }
394
+ /**
395
+ * Return the number of ms left in the item's TTL. If item is not in cache,
396
+ * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
397
+ */
398
+ getRemainingTTL(key) {
399
+ return this.#keyMap.has(key) ? Infinity : 0;
400
+ }
401
+ #initializeTTLTracking() {
402
+ const ttls = new ZeroArray(this.#max);
403
+ const starts = new ZeroArray(this.#max);
404
+ this.#ttls = ttls;
405
+ this.#starts = starts;
406
+ const purgeTimers = this.ttlAutopurge ?
407
+ Array.from({
408
+ length: this.#max,
409
+ })
410
+ : undefined;
411
+ this.#autopurgeTimers = purgeTimers;
412
+ this.#setItemTTL = (index, ttl, start = this.#perf.now()) => {
413
+ starts[index] = ttl !== 0 ? start : 0;
414
+ ttls[index] = ttl;
415
+ setPurgeTimer(index, ttl);
416
+ };
417
+ this.#updateItemAge = index => {
418
+ starts[index] = ttls[index] !== 0 ? this.#perf.now() : 0;
419
+ setPurgeTimer(index, ttls[index]);
420
+ };
421
+ // clear out the purge timer if we're setting TTL to 0, and
422
+ // previously had a ttl purge timer running, so it doesn't
423
+ // fire unnecessarily. Don't need to do this if we're not doing
424
+ // autopurge.
425
+ const setPurgeTimer = !this.ttlAutopurge ?
426
+ () => { }
427
+ : (index, ttl) => {
428
+ if (purgeTimers?.[index]) {
429
+ clearTimeout(purgeTimers[index]);
430
+ purgeTimers[index] = undefined;
431
+ }
432
+ if (ttl && ttl !== 0 && purgeTimers) {
433
+ const t = setTimeout(() => {
434
+ if (this.#isStale(index)) {
435
+ this.#delete(this.#keyList[index], 'expire');
436
+ purgeTimers[index] = undefined;
437
+ }
438
+ else {
439
+ setPurgeTimer(index, getRemainingTTL(index));
440
+ }
441
+ }, ttl + 1);
442
+ // unref() not supported on all platforms
443
+ /* c8 ignore start */
444
+ if (t.unref) {
445
+ t.unref();
446
+ }
447
+ /* c8 ignore stop */
448
+ purgeTimers[index] = t;
449
+ }
450
+ };
451
+ this.#statusTTL = (status, index) => {
452
+ if (ttls[index]) {
453
+ const ttl = ttls[index];
454
+ const start = starts[index];
455
+ /* c8 ignore start */
456
+ if (!ttl || !start) {
457
+ return;
458
+ }
459
+ /* c8 ignore stop */
460
+ status.ttl = ttl;
461
+ status.start = start;
462
+ status.now = cachedNow || getNow();
463
+ const age = status.now - start;
464
+ status.remainingTTL = ttl - age;
465
+ }
466
+ };
467
+ // debounce calls to perf.now() to 1s so we're not hitting
468
+ // that costly call repeatedly.
469
+ let cachedNow = 0;
470
+ const getNow = () => {
471
+ const n = this.#perf.now();
472
+ if (this.ttlResolution > 0) {
473
+ cachedNow = n;
474
+ const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
475
+ // not available on all platforms
476
+ /* c8 ignore start */
477
+ if (t.unref) {
478
+ t.unref();
479
+ }
480
+ /* c8 ignore stop */
481
+ }
482
+ return n;
483
+ };
484
+ this.getRemainingTTL = key => {
485
+ const index = this.#keyMap.get(key);
486
+ if (index === undefined) {
487
+ return 0;
488
+ }
489
+ return getRemainingTTL(index);
490
+ };
491
+ const getRemainingTTL = (index) => {
492
+ const ttl = ttls[index];
493
+ const start = starts[index];
494
+ if (!ttl || !start) {
495
+ return Infinity;
496
+ }
497
+ const age = (cachedNow || getNow()) - start;
498
+ return ttl - age;
499
+ };
500
+ this.#isStale = index => {
501
+ const s = starts[index];
502
+ const t = ttls[index];
503
+ return !!t && !!s && (cachedNow || getNow()) - s > t;
504
+ };
505
+ }
506
+ // conditionally set private methods related to TTL
507
+ #updateItemAge = () => { };
508
+ #statusTTL = () => { };
509
+ #setItemTTL = () => { };
510
+ /* c8 ignore stop */
511
+ #isStale = () => false;
512
+ #initializeSizeTracking() {
513
+ const sizes = new ZeroArray(this.#max);
514
+ this.#calculatedSize = 0;
515
+ this.#sizes = sizes;
516
+ this.#removeItemSize = index => {
517
+ this.#calculatedSize -= sizes[index];
518
+ sizes[index] = 0;
519
+ };
520
+ this.#requireSize = (k, v, size, sizeCalculation) => {
521
+ if (!isPosInt(size)) {
522
+ // provisionally accept background fetches.
523
+ // actual value size will be checked when they return.
524
+ if (this.#isBackgroundFetch(v)) {
525
+ // NB: this cannot occur if v.__staleWhileFetching is set,
526
+ // because in that case, it would take on the size of the
527
+ // existing entry that it temporarily replaces.
528
+ return this.backgroundFetchSize;
529
+ }
530
+ if (sizeCalculation) {
531
+ if (typeof sizeCalculation !== 'function') {
532
+ throw new TypeError('sizeCalculation must be a function');
533
+ }
534
+ size = sizeCalculation(v, k);
535
+ if (!isPosInt(size)) {
536
+ throw new TypeError('sizeCalculation return invalid (expect positive integer)');
537
+ }
538
+ }
539
+ else {
540
+ throw new TypeError('invalid size value (must be positive integer). ' +
541
+ 'When maxSize or maxEntrySize is used, sizeCalculation ' +
542
+ 'or size must be set.');
543
+ }
544
+ }
545
+ return size;
546
+ };
547
+ this.#addItemSize = (index, size, status) => {
548
+ sizes[index] = size;
549
+ if (this.#maxSize) {
550
+ const maxSize = this.#maxSize - sizes[index];
551
+ while (this.#calculatedSize > maxSize) {
552
+ this.#evict(true);
553
+ }
554
+ }
555
+ this.#calculatedSize += sizes[index];
556
+ if (status) {
557
+ status.entrySize = size;
558
+ status.totalCalculatedSize = this.#calculatedSize;
559
+ }
560
+ };
561
+ }
562
+ #removeItemSize = _i => { };
563
+ #addItemSize = (_i, _s, _st) => { };
564
+ #requireSize = (_k, _v, size, sizeCalculation) => {
565
+ if (size || sizeCalculation) {
566
+ throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
567
+ }
568
+ return 0;
569
+ };
570
+ *#indexes({ allowStale = this.allowStale } = {}) {
571
+ if (this.#size) {
572
+ for (let i = this.#tail; this.#isValidIndex(i);) {
573
+ if (allowStale || !this.#isStale(i)) {
574
+ yield i;
575
+ }
576
+ if (i === this.#head) {
577
+ break;
578
+ }
579
+ else {
580
+ i = this.#prev[i];
581
+ }
582
+ }
583
+ }
584
+ }
585
+ *#rindexes({ allowStale = this.allowStale } = {}) {
586
+ if (this.#size) {
587
+ for (let i = this.#head; this.#isValidIndex(i);) {
588
+ if (allowStale || !this.#isStale(i)) {
589
+ yield i;
590
+ }
591
+ if (i === this.#tail) {
592
+ break;
593
+ }
594
+ else {
595
+ i = this.#next[i];
596
+ }
597
+ }
598
+ }
599
+ }
600
+ #isValidIndex(index) {
601
+ return (index !== undefined &&
602
+ this.#keyMap.get(this.#keyList[index]) === index);
603
+ }
604
+ /**
605
+ * Return a generator yielding `[key, value]` pairs,
606
+ * in order from most recently used to least recently used.
607
+ */
608
+ *entries() {
609
+ for (const i of this.#indexes()) {
610
+ if (this.#valList[i] !== undefined &&
611
+ this.#keyList[i] !== undefined &&
612
+ !this.#isBackgroundFetch(this.#valList[i])) {
613
+ yield [this.#keyList[i], this.#valList[i]];
614
+ }
615
+ }
616
+ }
617
+ /**
618
+ * Inverse order version of {@link LRUCache.entries}
619
+ *
620
+ * Return a generator yielding `[key, value]` pairs,
621
+ * in order from least recently used to most recently used.
622
+ */
623
+ *rentries() {
624
+ for (const i of this.#rindexes()) {
625
+ if (this.#valList[i] !== undefined &&
626
+ this.#keyList[i] !== undefined &&
627
+ !this.#isBackgroundFetch(this.#valList[i])) {
628
+ yield [this.#keyList[i], this.#valList[i]];
629
+ }
630
+ }
631
+ }
632
+ /**
633
+ * Return a generator yielding the keys in the cache,
634
+ * in order from most recently used to least recently used.
635
+ */
636
+ *keys() {
637
+ for (const i of this.#indexes()) {
638
+ const k = this.#keyList[i];
639
+ if (k !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
640
+ yield k;
641
+ }
642
+ }
643
+ }
644
+ /**
645
+ * Inverse order version of {@link LRUCache.keys}
646
+ *
647
+ * Return a generator yielding the keys in the cache,
648
+ * in order from least recently used to most recently used.
649
+ */
650
+ *rkeys() {
651
+ for (const i of this.#rindexes()) {
652
+ const k = this.#keyList[i];
653
+ if (k !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
654
+ yield k;
655
+ }
656
+ }
657
+ }
658
+ /**
659
+ * Return a generator yielding the values in the cache,
660
+ * in order from most recently used to least recently used.
661
+ */
662
+ *values() {
663
+ for (const i of this.#indexes()) {
664
+ const v = this.#valList[i];
665
+ if (v !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
666
+ yield this.#valList[i];
667
+ }
668
+ }
669
+ }
670
+ /**
671
+ * Inverse order version of {@link LRUCache.values}
672
+ *
673
+ * Return a generator yielding the values in the cache,
674
+ * in order from least recently used to most recently used.
675
+ */
676
+ *rvalues() {
677
+ for (const i of this.#rindexes()) {
678
+ const v = this.#valList[i];
679
+ if (v !== undefined && !this.#isBackgroundFetch(this.#valList[i])) {
680
+ yield this.#valList[i];
681
+ }
682
+ }
683
+ }
684
+ /**
685
+ * Iterating over the cache itself yields the same results as
686
+ * {@link LRUCache.entries}
687
+ */
688
+ [Symbol.iterator]() {
689
+ return this.entries();
690
+ }
691
+ /**
692
+ * A String value that is used in the creation of the default string
693
+ * description of an object. Called by the built-in method
694
+ * `Object.prototype.toString`.
695
+ */
696
+ [Symbol.toStringTag] = 'LRUCache';
697
+ /**
698
+ * Find a value for which the supplied fn method returns a truthy value,
699
+ * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
700
+ */
701
+ find(fn, getOptions = {}) {
702
+ for (const i of this.#indexes()) {
703
+ const v = this.#valList[i];
704
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
705
+ if (value === undefined)
706
+ continue;
707
+ if (fn(value, this.#keyList[i], this)) {
708
+ return this.#get(this.#keyList[i], getOptions);
709
+ }
710
+ }
711
+ }
712
+ /**
713
+ * Call the supplied function on each item in the cache, in order from most
714
+ * recently used to least recently used.
715
+ *
716
+ * `fn` is called as `fn(value, key, cache)`.
717
+ *
718
+ * If `thisp` is provided, function will be called in the `this`-context of
719
+ * the provided object, or the cache if no `thisp` object is provided.
720
+ *
721
+ * Does not update age or recenty of use, or iterate over stale values.
722
+ */
723
+ forEach(fn, thisp = this) {
724
+ for (const i of this.#indexes()) {
725
+ const v = this.#valList[i];
726
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
727
+ if (value === undefined)
728
+ continue;
729
+ fn.call(thisp, value, this.#keyList[i], this);
730
+ }
731
+ }
732
+ /**
733
+ * The same as {@link LRUCache.forEach} but items are iterated over in
734
+ * reverse order. (ie, less recently used items are iterated over first.)
735
+ */
736
+ rforEach(fn, thisp = this) {
737
+ for (const i of this.#rindexes()) {
738
+ const v = this.#valList[i];
739
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
740
+ if (value === undefined)
741
+ continue;
742
+ fn.call(thisp, value, this.#keyList[i], this);
743
+ }
744
+ }
745
+ /**
746
+ * Delete any stale entries. Returns true if anything was removed,
747
+ * false otherwise.
748
+ */
749
+ purgeStale() {
750
+ let deleted = false;
751
+ for (const i of this.#rindexes({ allowStale: true })) {
752
+ if (this.#isStale(i)) {
753
+ this.#delete(this.#keyList[i], 'expire');
754
+ deleted = true;
755
+ }
756
+ }
757
+ return deleted;
758
+ }
759
+ /**
760
+ * Get the extended info about a given entry, to get its value, size, and
761
+ * TTL info simultaneously. Returns `undefined` if the key is not present.
762
+ *
763
+ * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
764
+ * serialization, the `start` value is always the current timestamp, and the
765
+ * `ttl` is a calculated remaining time to live (negative if expired).
766
+ *
767
+ * Always returns stale values, if their info is found in the cache, so be
768
+ * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
769
+ * if relevant.
770
+ */
771
+ info(key) {
772
+ const i = this.#keyMap.get(key);
773
+ if (i === undefined)
774
+ return undefined;
775
+ const v = this.#valList[i];
776
+ /* c8 ignore start - this isn't tested for the info function,
777
+ * but it's the same logic as found in other places. */
778
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
779
+ if (value === undefined)
780
+ return undefined;
781
+ /* c8 ignore stop */
782
+ const entry = { value };
783
+ if (this.#ttls && this.#starts) {
784
+ const ttl = this.#ttls[i];
785
+ const start = this.#starts[i];
786
+ if (ttl && start) {
787
+ const remain = ttl - (this.#perf.now() - start);
788
+ entry.ttl = remain;
789
+ entry.start = Date.now();
790
+ }
791
+ }
792
+ if (this.#sizes) {
793
+ entry.size = this.#sizes[i];
794
+ }
795
+ return entry;
796
+ }
797
+ /**
798
+ * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
799
+ * passed to {@link LRUCache#load}.
800
+ *
801
+ * The `start` fields are calculated relative to a portable `Date.now()`
802
+ * timestamp, even if `performance.now()` is available.
803
+ *
804
+ * Stale entries are always included in the `dump`, even if
805
+ * {@link LRUCache.OptionsBase.allowStale} is false.
806
+ *
807
+ * Note: this returns an actual array, not a generator, so it can be more
808
+ * easily passed around.
809
+ */
810
+ dump() {
811
+ const arr = [];
812
+ for (const i of this.#indexes({ allowStale: true })) {
813
+ const key = this.#keyList[i];
814
+ const v = this.#valList[i];
815
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
816
+ if (value === undefined || key === undefined)
817
+ continue;
818
+ const entry = { value };
819
+ if (this.#ttls && this.#starts) {
820
+ entry.ttl = this.#ttls[i];
821
+ // always dump the start relative to a portable timestamp
822
+ // it's ok for this to be a bit slow, it's a rare operation.
823
+ const age = this.#perf.now() - this.#starts[i];
824
+ entry.start = Math.floor(Date.now() - age);
825
+ }
826
+ if (this.#sizes) {
827
+ entry.size = this.#sizes[i];
828
+ }
829
+ arr.unshift([key, entry]);
830
+ }
831
+ return arr;
832
+ }
833
+ /**
834
+ * Reset the cache and load in the items in entries in the order listed.
835
+ *
836
+ * The shape of the resulting cache may be different if the same options are
837
+ * not used in both caches.
838
+ *
839
+ * The `start` fields are assumed to be calculated relative to a portable
840
+ * `Date.now()` timestamp, even if `performance.now()` is available.
841
+ */
842
+ load(arr) {
843
+ this.clear();
844
+ for (const [key, entry] of arr) {
845
+ if (entry.start) {
846
+ // entry.start is a portable timestamp, but we may be using
847
+ // node's performance.now(), so calculate the offset, so that
848
+ // we get the intended remaining TTL, no matter how long it's
849
+ // been on ice.
850
+ //
851
+ // it's ok for this to be a bit slow, it's a rare operation.
852
+ const age = Date.now() - entry.start;
853
+ entry.start = this.#perf.now() - age;
854
+ }
855
+ this.#set(key, entry.value, entry);
856
+ }
857
+ }
858
+ /**
859
+ * Add a value to the cache.
860
+ *
861
+ * Note: if `undefined` is specified as a value, this is an alias for
862
+ * {@link LRUCache#delete}
863
+ *
864
+ * Fields on the {@link LRUCache.SetOptions} options param will override
865
+ * their corresponding values in the constructor options for the scope
866
+ * of this single `set()` operation.
867
+ *
868
+ * If `start` is provided, then that will set the effective start
869
+ * time for the TTL calculation. Note that this must be a previous
870
+ * value of `performance.now()` if supported, or a previous value of
871
+ * `Date.now()` if not.
872
+ *
873
+ * Options object may also include `size`, which will prevent
874
+ * calling the `sizeCalculation` function and just use the specified
875
+ * number if it is a positive integer, and `noDisposeOnSet` which
876
+ * will prevent calling a `dispose` function in the case of
877
+ * overwrites.
878
+ *
879
+ * If the `size` (or return value of `sizeCalculation`) for a given
880
+ * entry is greater than `maxEntrySize`, then the item will not be
881
+ * added to the cache.
882
+ *
883
+ * Will update the recency of the entry.
884
+ *
885
+ * If the value is `undefined`, then this is an alias for
886
+ * `cache.delete(key)`. `undefined` is never stored in the cache.
887
+ */
888
+ set(k, v, setOptions = {}) {
889
+ const { status = diagnostics_channel_js_1.metrics.hasSubscribers ? {} : undefined } = setOptions;
890
+ setOptions.status = status;
891
+ if (status) {
892
+ status.op = 'set';
893
+ status.key = k;
894
+ if (v !== undefined)
895
+ status.value = v;
896
+ status.cache = this;
897
+ }
898
+ const result = this.#set(k, v, setOptions);
899
+ if (status && diagnostics_channel_js_1.metrics.hasSubscribers) {
900
+ diagnostics_channel_js_1.metrics.publish(status);
901
+ }
902
+ return result;
903
+ }
904
+ #set(k, v, setOptions, bf) {
905
+ const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
906
+ const isBF = this.#isBackgroundFetch(v);
907
+ if (v === undefined) {
908
+ if (status)
909
+ status.set = 'deleted';
910
+ this.delete(k);
911
+ return this;
912
+ }
913
+ let { noUpdateTTL = this.noUpdateTTL } = setOptions;
914
+ if (status && !isBF)
915
+ status.value = v;
916
+ const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation, status);
917
+ // if the item doesn't fit, don't do anything
918
+ // NB: maxEntrySize set to maxSize by default
919
+ if (this.maxEntrySize && size > this.maxEntrySize) {
920
+ // have to delete, in case something is there already.
921
+ this.#delete(k, 'set');
922
+ if (status) {
923
+ status.set = 'miss';
924
+ status.maxEntrySizeExceeded = true;
925
+ }
926
+ return this;
927
+ }
928
+ let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
929
+ if (index === undefined) {
930
+ // addition
931
+ index = (this.#size === 0 ? this.#tail
932
+ : this.#free.length !== 0 ? this.#free.pop()
933
+ : this.#size === this.#max ? this.#evict(false)
934
+ : this.#size);
935
+ this.#keyList[index] = k;
936
+ this.#valList[index] = v;
937
+ this.#keyMap.set(k, index);
938
+ this.#next[this.#tail] = index;
939
+ this.#prev[index] = this.#tail;
940
+ this.#tail = index;
941
+ this.#size++;
942
+ this.#addItemSize(index, size, status);
943
+ if (status)
944
+ status.set = 'add';
945
+ noUpdateTTL = false;
946
+ if (this.#hasOnInsert && !isBF) {
947
+ this.#onInsert?.(v, k, 'add');
948
+ }
949
+ }
950
+ else {
951
+ // update
952
+ // might be updating a background fetch!
953
+ this.#moveToTail(index);
954
+ const oldVal = this.#valList[index];
955
+ if (v !== oldVal) {
956
+ if (!noDisposeOnSet) {
957
+ if (this.#isBackgroundFetch(oldVal)) {
958
+ if (oldVal !== bf) {
959
+ // setting over a background fetch, not merely resolving it.
960
+ oldVal.__abortController.abort(new Error('replaced'));
961
+ }
962
+ const { __staleWhileFetching: s } = oldVal;
963
+ if (s !== undefined && s !== v) {
964
+ if (this.#hasDispose) {
965
+ this.#dispose?.(s, k, 'set');
966
+ }
967
+ if (this.#hasDisposeAfter) {
968
+ this.#disposed?.push([s, k, 'set']);
969
+ }
970
+ }
971
+ }
972
+ else {
973
+ if (this.#hasDispose) {
974
+ this.#dispose?.(oldVal, k, 'set');
975
+ }
976
+ if (this.#hasDisposeAfter) {
977
+ this.#disposed?.push([oldVal, k, 'set']);
978
+ }
979
+ }
980
+ }
981
+ this.#removeItemSize(index);
982
+ this.#addItemSize(index, size, status);
983
+ this.#valList[index] = v;
984
+ if (!isBF) {
985
+ const oldValue = oldVal && this.#isBackgroundFetch(oldVal) ?
986
+ oldVal.__staleWhileFetching
987
+ : oldVal;
988
+ const setType = oldValue === undefined ? 'add'
989
+ : v !== oldValue ? 'replace'
990
+ : 'update';
991
+ if (status) {
992
+ status.set = setType;
993
+ if (oldValue !== undefined)
994
+ status.oldValue = oldValue;
995
+ }
996
+ if (this.#hasOnInsert) {
997
+ this.onInsert?.(v, k, setType);
998
+ }
999
+ }
1000
+ }
1001
+ else if (!isBF) {
1002
+ if (status) {
1003
+ status.set = 'update';
1004
+ }
1005
+ if (this.#hasOnInsert) {
1006
+ this.onInsert?.(v, k, 'update');
1007
+ }
1008
+ }
1009
+ }
1010
+ if (ttl !== 0 && !this.#ttls) {
1011
+ this.#initializeTTLTracking();
1012
+ }
1013
+ if (this.#ttls) {
1014
+ if (!noUpdateTTL) {
1015
+ this.#setItemTTL(index, ttl, start);
1016
+ }
1017
+ if (status)
1018
+ this.#statusTTL(status, index);
1019
+ }
1020
+ if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
1021
+ const dt = this.#disposed;
1022
+ let task;
1023
+ while ((task = dt?.shift())) {
1024
+ this.#disposeAfter?.(...task);
1025
+ }
1026
+ }
1027
+ return this;
1028
+ }
1029
+ /**
1030
+ * Evict the least recently used item, returning its value or
1031
+ * `undefined` if cache is empty.
1032
+ */
1033
+ pop() {
1034
+ try {
1035
+ while (this.#size) {
1036
+ const val = this.#valList[this.#head];
1037
+ this.#evict(true);
1038
+ if (this.#isBackgroundFetch(val)) {
1039
+ if (val.__staleWhileFetching) {
1040
+ return val.__staleWhileFetching;
1041
+ }
1042
+ }
1043
+ else if (val !== undefined) {
1044
+ return val;
1045
+ }
1046
+ }
1047
+ }
1048
+ finally {
1049
+ if (this.#hasDisposeAfter && this.#disposed) {
1050
+ const dt = this.#disposed;
1051
+ let task;
1052
+ while ((task = dt?.shift())) {
1053
+ this.#disposeAfter?.(...task);
1054
+ }
1055
+ }
1056
+ }
1057
+ }
1058
+ #evict(free) {
1059
+ const head = this.#head;
1060
+ const k = this.#keyList[head];
1061
+ const v = this.#valList[head];
1062
+ const isBF = this.#isBackgroundFetch(v);
1063
+ if (isBF) {
1064
+ v.__abortController.abort(new Error('evicted'));
1065
+ }
1066
+ const oldValue = isBF ? v.__staleWhileFetching : v;
1067
+ if ((this.#hasDispose || this.#hasDisposeAfter) &&
1068
+ oldValue !== undefined) {
1069
+ if (this.#hasDispose) {
1070
+ this.#dispose?.(oldValue, k, 'evict');
1071
+ }
1072
+ if (this.#hasDisposeAfter) {
1073
+ this.#disposed?.push([oldValue, k, 'evict']);
1074
+ }
1075
+ }
1076
+ this.#removeItemSize(head);
1077
+ if (this.#autopurgeTimers?.[head]) {
1078
+ clearTimeout(this.#autopurgeTimers[head]);
1079
+ this.#autopurgeTimers[head] = undefined;
1080
+ }
1081
+ // if we aren't about to use the index, then null these out
1082
+ if (free) {
1083
+ this.#keyList[head] = undefined;
1084
+ this.#valList[head] = undefined;
1085
+ this.#free.push(head);
1086
+ }
1087
+ if (this.#size === 1) {
1088
+ this.#head = this.#tail = 0;
1089
+ this.#free.length = 0;
1090
+ }
1091
+ else {
1092
+ this.#head = this.#next[head];
1093
+ }
1094
+ this.#keyMap.delete(k);
1095
+ this.#size--;
1096
+ return head;
1097
+ }
1098
+ /**
1099
+ * Check if a key is in the cache, without updating the recency of use.
1100
+ * Will return false if the item is stale, even though it is technically
1101
+ * in the cache.
1102
+ *
1103
+ * Check if a key is in the cache, without updating the recency of
1104
+ * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
1105
+ * to `true` in either the options or the constructor.
1106
+ *
1107
+ * Will return `false` if the item is stale, even though it is technically in
1108
+ * the cache. The difference can be determined (if it matters) by using a
1109
+ * `status` argument, and inspecting the `has` field.
1110
+ *
1111
+ * Will not update item age unless
1112
+ * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
1113
+ */
1114
+ has(k, hasOptions = {}) {
1115
+ const { status = diagnostics_channel_js_1.metrics.hasSubscribers ? {} : undefined } = hasOptions;
1116
+ hasOptions.status = status;
1117
+ if (status) {
1118
+ status.op = 'has';
1119
+ status.key = k;
1120
+ status.cache = this;
1121
+ }
1122
+ const result = this.#has(k, hasOptions);
1123
+ if (diagnostics_channel_js_1.metrics.hasSubscribers)
1124
+ diagnostics_channel_js_1.metrics.publish(status);
1125
+ return result;
1126
+ }
1127
+ #has(k, hasOptions = {}) {
1128
+ const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
1129
+ const index = this.#keyMap.get(k);
1130
+ if (index !== undefined) {
1131
+ const v = this.#valList[index];
1132
+ if (this.#isBackgroundFetch(v) &&
1133
+ v.__staleWhileFetching === undefined) {
1134
+ return false;
1135
+ }
1136
+ if (!this.#isStale(index)) {
1137
+ if (updateAgeOnHas) {
1138
+ this.#updateItemAge(index);
1139
+ }
1140
+ if (status) {
1141
+ status.has = 'hit';
1142
+ this.#statusTTL(status, index);
1143
+ }
1144
+ return true;
1145
+ }
1146
+ else if (status) {
1147
+ status.has = 'stale';
1148
+ this.#statusTTL(status, index);
1149
+ }
1150
+ }
1151
+ else if (status) {
1152
+ status.has = 'miss';
1153
+ }
1154
+ return false;
1155
+ }
1156
+ /**
1157
+ * Like {@link LRUCache#get} but doesn't update recency or delete stale
1158
+ * items.
1159
+ *
1160
+ * Returns `undefined` if the item is stale, unless
1161
+ * {@link LRUCache.OptionsBase.allowStale} is set.
1162
+ */
1163
+ peek(k, peekOptions = {}) {
1164
+ const { status = hasSubscribers() ? {} : undefined } = peekOptions;
1165
+ if (status) {
1166
+ status.op = 'peek';
1167
+ status.key = k;
1168
+ status.cache = this;
1169
+ }
1170
+ peekOptions.status = status;
1171
+ const result = this.#peek(k, peekOptions);
1172
+ if (diagnostics_channel_js_1.metrics.hasSubscribers) {
1173
+ diagnostics_channel_js_1.metrics.publish(status);
1174
+ }
1175
+ return result;
1176
+ }
1177
+ #peek(k, peekOptions) {
1178
+ const { status, allowStale = this.allowStale } = peekOptions;
1179
+ const index = this.#keyMap.get(k);
1180
+ if (index === undefined || (!allowStale && this.#isStale(index))) {
1181
+ if (status)
1182
+ status.peek = index === undefined ? 'miss' : 'stale';
1183
+ return undefined;
1184
+ }
1185
+ const v = this.#valList[index];
1186
+ const val = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
1187
+ if (status) {
1188
+ if (val !== undefined) {
1189
+ status.peek = 'hit';
1190
+ status.value = val;
1191
+ }
1192
+ else {
1193
+ status.peek = 'miss';
1194
+ }
1195
+ }
1196
+ return val;
1197
+ }
1198
+ #backgroundFetch(k, index, options, context) {
1199
+ const v = index === undefined ? undefined : this.#valList[index];
1200
+ if (this.#isBackgroundFetch(v)) {
1201
+ return v;
1202
+ }
1203
+ const ac = new AbortController();
1204
+ const { signal } = options;
1205
+ // when/if our AC signals, then stop listening to theirs.
1206
+ signal?.addEventListener('abort', () => ac.abort(signal.reason), {
1207
+ signal: ac.signal,
1208
+ });
1209
+ const fetchOpts = {
1210
+ signal: ac.signal,
1211
+ options,
1212
+ context,
1213
+ };
1214
+ const cb = (v, updateCache = false) => {
1215
+ const { aborted } = ac.signal;
1216
+ const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
1217
+ const proceed = options.ignoreFetchAbort ||
1218
+ !!(options.allowStaleOnFetchAbort && v !== undefined);
1219
+ if (options.status) {
1220
+ if (aborted && !updateCache) {
1221
+ options.status.fetchAborted = true;
1222
+ options.status.fetchError = ac.signal.reason;
1223
+ if (ignoreAbort)
1224
+ options.status.fetchAbortIgnored = true;
1225
+ }
1226
+ else {
1227
+ options.status.fetchResolved = true;
1228
+ }
1229
+ }
1230
+ if (aborted && !ignoreAbort && !updateCache) {
1231
+ return fetchFail(ac.signal.reason, proceed);
1232
+ }
1233
+ // either we didn't abort, and are still here, or we did, and ignored
1234
+ const bf = p;
1235
+ // if nothing else has been written there but we're set to update the
1236
+ // cache and ignore the abort, or if it's still pending on this specific
1237
+ // background request, then write it to the cache.
1238
+ const vl = this.#valList[index];
1239
+ if (vl === p || (vl === undefined && ignoreAbort && updateCache)) {
1240
+ if (v === undefined) {
1241
+ if (bf.__staleWhileFetching !== undefined) {
1242
+ this.#valList[index] = bf.__staleWhileFetching;
1243
+ }
1244
+ else {
1245
+ this.#delete(k, 'fetch');
1246
+ }
1247
+ }
1248
+ else {
1249
+ if (options.status)
1250
+ options.status.fetchUpdated = true;
1251
+ this.#set(k, v, fetchOpts.options, bf);
1252
+ }
1253
+ }
1254
+ return v;
1255
+ };
1256
+ const eb = (er) => {
1257
+ if (options.status) {
1258
+ options.status.fetchRejected = true;
1259
+ options.status.fetchError = er;
1260
+ }
1261
+ // do not pass go, do not collect $200
1262
+ return fetchFail(er, false);
1263
+ };
1264
+ const fetchFail = (er, proceed) => {
1265
+ const { aborted } = ac.signal;
1266
+ const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
1267
+ const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
1268
+ const noDelete = allowStale || options.noDeleteOnFetchRejection;
1269
+ const bf = p;
1270
+ if (this.#valList[index] === p) {
1271
+ // if we allow stale on fetch rejections, then we need to ensure that
1272
+ // the stale value is not removed from the cache when the fetch fails.
1273
+ const del = !noDelete || (!proceed && bf.__staleWhileFetching === undefined);
1274
+ if (del) {
1275
+ this.#delete(k, 'fetch');
1276
+ }
1277
+ else if (!allowStaleAborted) {
1278
+ // still replace the *promise* with the stale value,
1279
+ // since we are done with the promise at this point.
1280
+ // leave it untouched if we're still waiting for an
1281
+ // aborted background fetch that hasn't yet returned.
1282
+ this.#valList[index] = bf.__staleWhileFetching;
1283
+ }
1284
+ }
1285
+ if (allowStale) {
1286
+ if (options.status && bf.__staleWhileFetching !== undefined) {
1287
+ options.status.returnedStale = true;
1288
+ }
1289
+ return bf.__staleWhileFetching;
1290
+ }
1291
+ else if (bf.__returned === bf) {
1292
+ throw er;
1293
+ }
1294
+ };
1295
+ const pcall = (res, rej) => {
1296
+ const fmp = this.#fetchMethod?.(k, v, fetchOpts);
1297
+ // ignored, we go until we finish, regardless.
1298
+ // defer check until we are actually aborting,
1299
+ // so fetchMethod can override.
1300
+ ac.signal.addEventListener('abort', () => {
1301
+ if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
1302
+ res(undefined);
1303
+ // when it eventually resolves, update the cache.
1304
+ if (options.allowStaleOnFetchAbort) {
1305
+ res = v => cb(v, true);
1306
+ }
1307
+ }
1308
+ });
1309
+ if (fmp && fmp instanceof Promise) {
1310
+ fmp.then(v => res(v === undefined ? undefined : v), rej);
1311
+ }
1312
+ else if (fmp !== undefined) {
1313
+ res(fmp);
1314
+ }
1315
+ };
1316
+ if (options.status)
1317
+ options.status.fetchDispatched = true;
1318
+ const p = new Promise(pcall).then(cb, eb);
1319
+ const bf = Object.assign(p, {
1320
+ __abortController: ac,
1321
+ __staleWhileFetching: v,
1322
+ __returned: undefined,
1323
+ });
1324
+ if (index === undefined) {
1325
+ // internal, don't expose status.
1326
+ this.#set(k, bf, { ...fetchOpts.options, status: undefined });
1327
+ index = this.#keyMap.get(k);
1328
+ }
1329
+ else {
1330
+ // do not call #set, because we do not want to adjust its place
1331
+ // in the lru queue, as it has not yet been "used". Also, we don't
1332
+ // need to worry about evicting for size, because a background fetch
1333
+ // over a stale value is treated as the same size as its stale value.
1334
+ this.#valList[index] = bf;
1335
+ }
1336
+ return bf;
1337
+ }
1338
+ #isBackgroundFetch(p) {
1339
+ if (!this.#hasFetchMethod)
1340
+ return false;
1341
+ const b = p;
1342
+ return (!!b &&
1343
+ b instanceof Promise &&
1344
+ b.hasOwnProperty('__staleWhileFetching') &&
1345
+ b.__abortController instanceof AbortController);
1346
+ }
1347
+ fetch(k, fetchOptions = {}) {
1348
+ const ths = diagnostics_channel_js_1.tracing.hasSubscribers;
1349
+ const { status = hasSubscribers() ? {} : undefined } = fetchOptions;
1350
+ fetchOptions.status = status;
1351
+ if (status && fetchOptions.context) {
1352
+ status.context = fetchOptions.context;
1353
+ }
1354
+ const p = this.#fetch(k, fetchOptions);
1355
+ if (status && ths) {
1356
+ status.trace = true;
1357
+ diagnostics_channel_js_1.tracing.tracePromise(() => p, status).catch(() => { });
1358
+ }
1359
+ return p;
1360
+ }
1361
+ async #fetch(k, fetchOptions = {}) {
1362
+ const {
1363
+ // get options
1364
+ allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
1365
+ // set options
1366
+ ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
1367
+ // fetch exclusive options
1368
+ noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
1369
+ if (status) {
1370
+ status.op = 'fetch';
1371
+ status.key = k;
1372
+ if (forceRefresh)
1373
+ status.forceRefresh = true;
1374
+ status.cache = this;
1375
+ }
1376
+ if (!this.#hasFetchMethod) {
1377
+ if (status)
1378
+ status.fetch = 'get';
1379
+ return this.#get(k, {
1380
+ allowStale,
1381
+ updateAgeOnGet,
1382
+ noDeleteOnStaleGet,
1383
+ status,
1384
+ });
1385
+ }
1386
+ const options = {
1387
+ allowStale,
1388
+ updateAgeOnGet,
1389
+ noDeleteOnStaleGet,
1390
+ ttl,
1391
+ noDisposeOnSet,
1392
+ size,
1393
+ sizeCalculation,
1394
+ noUpdateTTL,
1395
+ noDeleteOnFetchRejection,
1396
+ allowStaleOnFetchRejection,
1397
+ allowStaleOnFetchAbort,
1398
+ ignoreFetchAbort,
1399
+ status,
1400
+ signal,
1401
+ };
1402
+ let index = this.#keyMap.get(k);
1403
+ if (index === undefined) {
1404
+ if (status)
1405
+ status.fetch = 'miss';
1406
+ const p = this.#backgroundFetch(k, index, options, context);
1407
+ return (p.__returned = p);
1408
+ }
1409
+ else {
1410
+ // in cache, maybe already fetching
1411
+ const v = this.#valList[index];
1412
+ if (this.#isBackgroundFetch(v)) {
1413
+ const stale = allowStale && v.__staleWhileFetching !== undefined;
1414
+ if (status) {
1415
+ status.fetch = 'inflight';
1416
+ if (stale)
1417
+ status.returnedStale = true;
1418
+ }
1419
+ return stale ? v.__staleWhileFetching : (v.__returned = v);
1420
+ }
1421
+ // if we force a refresh, that means do NOT serve the cached value,
1422
+ // unless we are already in the process of refreshing the cache.
1423
+ const isStale = this.#isStale(index);
1424
+ if (!forceRefresh && !isStale) {
1425
+ if (status)
1426
+ status.fetch = 'hit';
1427
+ this.#moveToTail(index);
1428
+ if (updateAgeOnGet) {
1429
+ this.#updateItemAge(index);
1430
+ }
1431
+ if (status)
1432
+ this.#statusTTL(status, index);
1433
+ return v;
1434
+ }
1435
+ // ok, it is stale or a forced refresh, and not already fetching.
1436
+ // refresh the cache.
1437
+ const p = this.#backgroundFetch(k, index, options, context);
1438
+ const hasStale = p.__staleWhileFetching !== undefined;
1439
+ const staleVal = hasStale && allowStale;
1440
+ if (status) {
1441
+ status.fetch = isStale ? 'stale' : 'refresh';
1442
+ if (staleVal && isStale)
1443
+ status.returnedStale = true;
1444
+ }
1445
+ return staleVal ? p.__staleWhileFetching : (p.__returned = p);
1446
+ }
1447
+ }
1448
+ forceFetch(k, fetchOptions = {}) {
1449
+ const ths = diagnostics_channel_js_1.tracing.hasSubscribers;
1450
+ const { status = hasSubscribers() ? {} : undefined } = fetchOptions;
1451
+ fetchOptions.status = status;
1452
+ if (status && fetchOptions.context) {
1453
+ status.context = fetchOptions.context;
1454
+ }
1455
+ const p = this.#forceFetch(k, fetchOptions);
1456
+ if (status && ths) {
1457
+ status.trace = true;
1458
+ diagnostics_channel_js_1.tracing.tracePromise(() => p, status).catch(() => { });
1459
+ }
1460
+ return p;
1461
+ }
1462
+ async #forceFetch(k, fetchOptions = {}) {
1463
+ const v = await this.#fetch(k, fetchOptions);
1464
+ if (v === undefined)
1465
+ throw new Error('fetch() returned undefined');
1466
+ return v;
1467
+ }
1468
+ memo(k, memoOptions = {}) {
1469
+ const { status = diagnostics_channel_js_1.metrics.hasSubscribers ? {} : undefined } = memoOptions;
1470
+ memoOptions.status = status;
1471
+ if (status) {
1472
+ status.op = 'memo';
1473
+ status.key = k;
1474
+ if (memoOptions.context) {
1475
+ status.context = memoOptions.context;
1476
+ }
1477
+ status.cache = this;
1478
+ }
1479
+ const result = this.#memo(k, memoOptions);
1480
+ if (status)
1481
+ status.value = result;
1482
+ if (diagnostics_channel_js_1.metrics.hasSubscribers)
1483
+ diagnostics_channel_js_1.metrics.publish(status);
1484
+ return result;
1485
+ }
1486
+ #memo(k, memoOptions = {}) {
1487
+ const memoMethod = this.#memoMethod;
1488
+ if (!memoMethod) {
1489
+ throw new Error('no memoMethod provided to constructor');
1490
+ }
1491
+ const { context, status, forceRefresh, ...options } = memoOptions;
1492
+ if (status && forceRefresh)
1493
+ status.forceRefresh = true;
1494
+ const v = this.#get(k, options);
1495
+ const refresh = forceRefresh || v === undefined;
1496
+ if (status) {
1497
+ status.memo = refresh ? 'miss' : 'hit';
1498
+ if (!refresh)
1499
+ status.value = v;
1500
+ }
1501
+ if (!refresh)
1502
+ return v;
1503
+ const vv = memoMethod(k, v, {
1504
+ options,
1505
+ context,
1506
+ });
1507
+ if (status)
1508
+ status.value = vv;
1509
+ this.#set(k, vv, options);
1510
+ return vv;
1511
+ }
1512
+ /**
1513
+ * Return a value from the cache. Will update the recency of the cache
1514
+ * entry found.
1515
+ *
1516
+ * If the key is not found, get() will return `undefined`.
1517
+ */
1518
+ get(k, getOptions = {}) {
1519
+ const { status = diagnostics_channel_js_1.metrics.hasSubscribers ? {} : undefined } = getOptions;
1520
+ getOptions.status = status;
1521
+ if (status) {
1522
+ status.op = 'get';
1523
+ status.key = k;
1524
+ status.cache = this;
1525
+ }
1526
+ const result = this.#get(k, getOptions);
1527
+ if (status) {
1528
+ if (result !== undefined)
1529
+ status.value = result;
1530
+ if (diagnostics_channel_js_1.metrics.hasSubscribers)
1531
+ diagnostics_channel_js_1.metrics.publish(status);
1532
+ }
1533
+ return result;
1534
+ }
1535
+ #get(k, getOptions = {}) {
1536
+ const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
1537
+ const index = this.#keyMap.get(k);
1538
+ if (index === undefined) {
1539
+ if (status)
1540
+ status.get = 'miss';
1541
+ return undefined;
1542
+ }
1543
+ const value = this.#valList[index];
1544
+ const fetching = this.#isBackgroundFetch(value);
1545
+ if (status)
1546
+ this.#statusTTL(status, index);
1547
+ if (this.#isStale(index)) {
1548
+ // delete only if not an in-flight background fetch
1549
+ if (!fetching) {
1550
+ if (!noDeleteOnStaleGet) {
1551
+ this.#delete(k, 'expire');
1552
+ }
1553
+ if (status)
1554
+ status.get = 'stale';
1555
+ if (allowStale) {
1556
+ if (status)
1557
+ status.returnedStale = true;
1558
+ return value;
1559
+ }
1560
+ return undefined;
1561
+ }
1562
+ if (status)
1563
+ status.get = 'stale-fetching';
1564
+ if (allowStale && value.__staleWhileFetching !== undefined) {
1565
+ if (status)
1566
+ status.returnedStale = true;
1567
+ return value.__staleWhileFetching;
1568
+ }
1569
+ return undefined;
1570
+ }
1571
+ // not stale
1572
+ if (status)
1573
+ status.get = fetching ? 'fetching' : 'hit';
1574
+ // if we're currently fetching it, we don't actually have it yet
1575
+ // it's not stale, which means this isn't a staleWhileRefetching.
1576
+ // If it's not stale, and fetching, AND has a __staleWhileFetching
1577
+ // value, then that means the user fetched with {forceRefresh:true},
1578
+ // so it's safe to return that value.
1579
+ this.#moveToTail(index);
1580
+ if (updateAgeOnGet) {
1581
+ this.#updateItemAge(index);
1582
+ }
1583
+ return fetching ? value.__staleWhileFetching : value;
1584
+ }
1585
+ #connect(p, n) {
1586
+ this.#prev[n] = p;
1587
+ this.#next[p] = n;
1588
+ }
1589
+ #moveToTail(index) {
1590
+ // if tail already, nothing to do
1591
+ // if head, move head to next[index]
1592
+ // else
1593
+ // move next[prev[index]] to next[index] (head has no prev)
1594
+ // move prev[next[index]] to prev[index]
1595
+ // prev[index] = tail
1596
+ // next[tail] = index
1597
+ // tail = index
1598
+ if (index !== this.#tail) {
1599
+ if (index === this.#head) {
1600
+ this.#head = this.#next[index];
1601
+ }
1602
+ else {
1603
+ this.#connect(this.#prev[index], this.#next[index]);
1604
+ }
1605
+ this.#connect(this.#tail, index);
1606
+ this.#tail = index;
1607
+ }
1608
+ }
1609
+ /**
1610
+ * Deletes a key out of the cache.
1611
+ *
1612
+ * Returns true if the key was deleted, false otherwise.
1613
+ */
1614
+ delete(k) {
1615
+ return this.#delete(k, 'delete');
1616
+ }
1617
+ #delete(k, reason) {
1618
+ if (diagnostics_channel_js_1.metrics.hasSubscribers) {
1619
+ diagnostics_channel_js_1.metrics.publish({
1620
+ op: 'delete',
1621
+ delete: reason,
1622
+ key: k,
1623
+ cache: this,
1624
+ });
1625
+ }
1626
+ let deleted = false;
1627
+ if (this.#size !== 0) {
1628
+ const index = this.#keyMap.get(k);
1629
+ if (index !== undefined) {
1630
+ if (this.#autopurgeTimers?.[index]) {
1631
+ clearTimeout(this.#autopurgeTimers[index]);
1632
+ this.#autopurgeTimers[index] = undefined;
1633
+ }
1634
+ deleted = true;
1635
+ if (this.#size === 1) {
1636
+ this.#clear(reason);
1637
+ }
1638
+ else {
1639
+ this.#removeItemSize(index);
1640
+ const v = this.#valList[index];
1641
+ if (this.#isBackgroundFetch(v)) {
1642
+ v.__abortController.abort(new Error('deleted'));
1643
+ }
1644
+ else if (this.#hasDispose || this.#hasDisposeAfter) {
1645
+ if (this.#hasDispose) {
1646
+ this.#dispose?.(v, k, reason);
1647
+ }
1648
+ if (this.#hasDisposeAfter) {
1649
+ this.#disposed?.push([v, k, reason]);
1650
+ }
1651
+ }
1652
+ this.#keyMap.delete(k);
1653
+ this.#keyList[index] = undefined;
1654
+ this.#valList[index] = undefined;
1655
+ if (index === this.#tail) {
1656
+ this.#tail = this.#prev[index];
1657
+ }
1658
+ else if (index === this.#head) {
1659
+ this.#head = this.#next[index];
1660
+ }
1661
+ else {
1662
+ const pi = this.#prev[index];
1663
+ this.#next[pi] = this.#next[index];
1664
+ const ni = this.#next[index];
1665
+ this.#prev[ni] = this.#prev[index];
1666
+ }
1667
+ this.#size--;
1668
+ this.#free.push(index);
1669
+ }
1670
+ }
1671
+ }
1672
+ if (this.#hasDisposeAfter && this.#disposed?.length) {
1673
+ const dt = this.#disposed;
1674
+ let task;
1675
+ while ((task = dt?.shift())) {
1676
+ this.#disposeAfter?.(...task);
1677
+ }
1678
+ }
1679
+ return deleted;
1680
+ }
1681
+ /**
1682
+ * Clear the cache entirely, throwing away all values.
1683
+ */
1684
+ clear() {
1685
+ return this.#clear('delete');
1686
+ }
1687
+ #clear(reason) {
1688
+ for (const index of this.#rindexes({ allowStale: true })) {
1689
+ const v = this.#valList[index];
1690
+ if (this.#isBackgroundFetch(v)) {
1691
+ v.__abortController.abort(new Error('deleted'));
1692
+ }
1693
+ else {
1694
+ const k = this.#keyList[index];
1695
+ if (this.#hasDispose) {
1696
+ this.#dispose?.(v, k, reason);
1697
+ }
1698
+ if (this.#hasDisposeAfter) {
1699
+ this.#disposed?.push([v, k, reason]);
1700
+ }
1701
+ }
1702
+ }
1703
+ this.#keyMap.clear();
1704
+ void this.#valList.fill(undefined);
1705
+ this.#keyList.fill(undefined);
1706
+ if (this.#ttls && this.#starts) {
1707
+ this.#ttls.fill(0);
1708
+ this.#starts.fill(0);
1709
+ for (const t of this.#autopurgeTimers ?? []) {
1710
+ if (t !== undefined)
1711
+ clearTimeout(t);
1712
+ }
1713
+ this.#autopurgeTimers?.fill(undefined);
1714
+ }
1715
+ if (this.#sizes) {
1716
+ this.#sizes.fill(0);
1717
+ }
1718
+ this.#head = 0;
1719
+ this.#tail = 0;
1720
+ this.#free.length = 0;
1721
+ this.#calculatedSize = 0;
1722
+ this.#size = 0;
1723
+ if (this.#hasDisposeAfter && this.#disposed) {
1724
+ const dt = this.#disposed;
1725
+ let task;
1726
+ while ((task = dt?.shift())) {
1727
+ this.#disposeAfter?.(...task);
1728
+ }
1729
+ }
1730
+ }
1731
+ }
1732
+ exports.LRUCache = LRUCache;
1733
+ //# sourceMappingURL=index.js.map