@edgeone/nuxt-pages 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +275 -0
  2. package/dist/build/content/server.js +18 -0
  3. package/dist/build/content/static.js +17 -0
  4. package/dist/build/functions/server.js +19 -0
  5. package/dist/build/plugin-context.js +18 -0
  6. package/dist/build/routes.js +18 -0
  7. package/dist/build/templates/nuxt-handler-backup.js +305 -0
  8. package/dist/build/templates/nuxt-handler-monorepo.tmpl-ipx_backup.js +511 -0
  9. package/dist/build/templates/nuxt-handler-monorepo.tmpl.js +243 -0
  10. package/dist/build/templates/nuxt-handler.tmpl.js +212 -0
  11. package/dist/esm-chunks/chunk-5YBUNNZ4.js +81 -0
  12. package/dist/esm-chunks/chunk-6BT4RYQJ.js +43 -0
  13. package/dist/esm-chunks/chunk-6YERJDAJ.js +208 -0
  14. package/dist/esm-chunks/chunk-GX4Z7KQX.js +15065 -0
  15. package/dist/esm-chunks/chunk-HBXUWFGE.js +19 -0
  16. package/dist/esm-chunks/chunk-HY3HNABZ.js +87 -0
  17. package/dist/esm-chunks/chunk-KGYBHZC3.js +1467 -0
  18. package/dist/esm-chunks/chunk-MMMRMLH2.js +132 -0
  19. package/dist/esm-chunks/chunk-NJ4SUJNF.js +5635 -0
  20. package/dist/esm-chunks/chunk-QG7JLDXY.js +127 -0
  21. package/dist/esm-chunks/chunk-RPSYO4VM.js +562 -0
  22. package/dist/esm-chunks/chunk-UOPC2N5A.js +69 -0
  23. package/dist/esm-chunks/chunk-V2LFVP3C.js +838 -0
  24. package/dist/index.js +61 -0
  25. package/dist/run/config.js +17 -0
  26. package/dist/run/constants.js +17 -0
  27. package/dist/run/handlers/cache.cjs +1410 -0
  28. package/dist/run/handlers/nuxt-cache.cjs +200 -0
  29. package/dist/run/handlers/nuxt-server.js +156 -0
  30. package/dist/run/handlers/request-context.cjs +148 -0
  31. package/dist/run/handlers/server.js +77 -0
  32. package/dist/run/handlers/tags-handler.cjs +177 -0
  33. package/dist/run/handlers/tracer.cjs +1004 -0
  34. package/dist/run/handlers/use-cache-handler.js +220 -0
  35. package/dist/run/handlers/wait-until.cjs +123 -0
  36. package/dist/run/headers.js +17 -0
  37. package/dist/run/revalidate.js +34 -0
  38. package/dist/run/storage/regional-blob-store.cjs +64 -0
  39. package/dist/run/storage/request-scoped-in-memory-cache.cjs +1582 -0
  40. package/dist/run/storage/storage.cjs +191 -0
  41. package/dist/shared/blob-types.cjs +37 -0
  42. package/dist/shared/blobkey.js +25 -0
  43. package/dist/shared/cache-types.cjs +33 -0
  44. package/dist/shared/nuxt-cache-types.cjs +18 -0
  45. package/dist/types/options.js +6 -0
  46. package/dist/utils.js +25 -0
  47. package/package.json +58 -0
@@ -0,0 +1,1467 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+
8
+ // node_modules/lru-cache/dist/esm/index.js
9
+ var perf = typeof performance === "object" && performance && typeof performance.now === "function" ? performance : Date;
10
+ var warned = /* @__PURE__ */ new Set();
11
+ var PROCESS = typeof process === "object" && !!process ? process : {};
12
+ var emitWarning = (msg, type, code, fn) => {
13
+ typeof PROCESS.emitWarning === "function" ? PROCESS.emitWarning(msg, type, code, fn) : console.error(`[${code}] ${type}: ${msg}`);
14
+ };
15
+ var AC = globalThis.AbortController;
16
+ var AS = globalThis.AbortSignal;
17
+ if (typeof AC === "undefined") {
18
+ AS = class AbortSignal {
19
+ onabort;
20
+ _onabort = [];
21
+ reason;
22
+ aborted = false;
23
+ addEventListener(_, fn) {
24
+ this._onabort.push(fn);
25
+ }
26
+ };
27
+ AC = class AbortController {
28
+ constructor() {
29
+ warnACPolyfill();
30
+ }
31
+ signal = new AS();
32
+ abort(reason) {
33
+ if (this.signal.aborted)
34
+ return;
35
+ this.signal.reason = reason;
36
+ this.signal.aborted = true;
37
+ for (const fn of this.signal._onabort) {
38
+ fn(reason);
39
+ }
40
+ this.signal.onabort?.(reason);
41
+ }
42
+ };
43
+ let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== "1";
44
+ const warnACPolyfill = () => {
45
+ if (!printACPolyfillWarning)
46
+ return;
47
+ printACPolyfillWarning = false;
48
+ emitWarning("AbortController is not defined. If using lru-cache in node 14, load an AbortController polyfill from the `node-abort-controller` package. A minimal polyfill is provided for use by LRUCache.fetch(), but it should not be relied upon in other contexts (eg, passing it to other APIs that use AbortController/AbortSignal might have undesirable effects). You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.", "NO_ABORT_CONTROLLER", "ENOTSUP", warnACPolyfill);
49
+ };
50
+ }
51
+ var shouldWarn = (code) => !warned.has(code);
52
+ var TYPE = Symbol("type");
53
+ var isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
54
+ var getUintArray = (max) => !isPosInt(max) ? null : max <= Math.pow(2, 8) ? Uint8Array : max <= Math.pow(2, 16) ? Uint16Array : max <= Math.pow(2, 32) ? Uint32Array : max <= Number.MAX_SAFE_INTEGER ? ZeroArray : null;
55
+ var ZeroArray = class extends Array {
56
+ constructor(size) {
57
+ super(size);
58
+ this.fill(0);
59
+ }
60
+ };
61
+ var Stack = class _Stack {
62
+ heap;
63
+ length;
64
+ // private constructor
65
+ static #constructing = false;
66
+ static create(max) {
67
+ const HeapCls = getUintArray(max);
68
+ if (!HeapCls)
69
+ return [];
70
+ _Stack.#constructing = true;
71
+ const s = new _Stack(max, HeapCls);
72
+ _Stack.#constructing = false;
73
+ return s;
74
+ }
75
+ constructor(max, HeapCls) {
76
+ if (!_Stack.#constructing) {
77
+ throw new TypeError("instantiate Stack using Stack.create(n)");
78
+ }
79
+ this.heap = new HeapCls(max);
80
+ this.length = 0;
81
+ }
82
+ push(n) {
83
+ this.heap[this.length++] = n;
84
+ }
85
+ pop() {
86
+ return this.heap[--this.length];
87
+ }
88
+ };
89
+ var LRUCache = class _LRUCache {
90
+ // options that cannot be changed without disaster
91
+ #max;
92
+ #maxSize;
93
+ #dispose;
94
+ #disposeAfter;
95
+ #fetchMethod;
96
+ #memoMethod;
97
+ /**
98
+ * {@link LRUCache.OptionsBase.ttl}
99
+ */
100
+ ttl;
101
+ /**
102
+ * {@link LRUCache.OptionsBase.ttlResolution}
103
+ */
104
+ ttlResolution;
105
+ /**
106
+ * {@link LRUCache.OptionsBase.ttlAutopurge}
107
+ */
108
+ ttlAutopurge;
109
+ /**
110
+ * {@link LRUCache.OptionsBase.updateAgeOnGet}
111
+ */
112
+ updateAgeOnGet;
113
+ /**
114
+ * {@link LRUCache.OptionsBase.updateAgeOnHas}
115
+ */
116
+ updateAgeOnHas;
117
+ /**
118
+ * {@link LRUCache.OptionsBase.allowStale}
119
+ */
120
+ allowStale;
121
+ /**
122
+ * {@link LRUCache.OptionsBase.noDisposeOnSet}
123
+ */
124
+ noDisposeOnSet;
125
+ /**
126
+ * {@link LRUCache.OptionsBase.noUpdateTTL}
127
+ */
128
+ noUpdateTTL;
129
+ /**
130
+ * {@link LRUCache.OptionsBase.maxEntrySize}
131
+ */
132
+ maxEntrySize;
133
+ /**
134
+ * {@link LRUCache.OptionsBase.sizeCalculation}
135
+ */
136
+ sizeCalculation;
137
+ /**
138
+ * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
139
+ */
140
+ noDeleteOnFetchRejection;
141
+ /**
142
+ * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
143
+ */
144
+ noDeleteOnStaleGet;
145
+ /**
146
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
147
+ */
148
+ allowStaleOnFetchAbort;
149
+ /**
150
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
151
+ */
152
+ allowStaleOnFetchRejection;
153
+ /**
154
+ * {@link LRUCache.OptionsBase.ignoreFetchAbort}
155
+ */
156
+ ignoreFetchAbort;
157
+ // computed properties
158
+ #size;
159
+ #calculatedSize;
160
+ #keyMap;
161
+ #keyList;
162
+ #valList;
163
+ #next;
164
+ #prev;
165
+ #head;
166
+ #tail;
167
+ #free;
168
+ #disposed;
169
+ #sizes;
170
+ #starts;
171
+ #ttls;
172
+ #hasDispose;
173
+ #hasFetchMethod;
174
+ #hasDisposeAfter;
175
+ /**
176
+ * Do not call this method unless you need to inspect the
177
+ * inner workings of the cache. If anything returned by this
178
+ * object is modified in any way, strange breakage may occur.
179
+ *
180
+ * These fields are private for a reason!
181
+ *
182
+ * @internal
183
+ */
184
+ static unsafeExposeInternals(c) {
185
+ return {
186
+ // properties
187
+ starts: c.#starts,
188
+ ttls: c.#ttls,
189
+ sizes: c.#sizes,
190
+ keyMap: c.#keyMap,
191
+ keyList: c.#keyList,
192
+ valList: c.#valList,
193
+ next: c.#next,
194
+ prev: c.#prev,
195
+ get head() {
196
+ return c.#head;
197
+ },
198
+ get tail() {
199
+ return c.#tail;
200
+ },
201
+ free: c.#free,
202
+ // methods
203
+ isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
204
+ backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
205
+ moveToTail: (index) => c.#moveToTail(index),
206
+ indexes: (options) => c.#indexes(options),
207
+ rindexes: (options) => c.#rindexes(options),
208
+ isStale: (index) => c.#isStale(index)
209
+ };
210
+ }
211
+ // Protected read-only members
212
+ /**
213
+ * {@link LRUCache.OptionsBase.max} (read-only)
214
+ */
215
+ get max() {
216
+ return this.#max;
217
+ }
218
+ /**
219
+ * {@link LRUCache.OptionsBase.maxSize} (read-only)
220
+ */
221
+ get maxSize() {
222
+ return this.#maxSize;
223
+ }
224
+ /**
225
+ * The total computed size of items in the cache (read-only)
226
+ */
227
+ get calculatedSize() {
228
+ return this.#calculatedSize;
229
+ }
230
+ /**
231
+ * The number of items stored in the cache (read-only)
232
+ */
233
+ get size() {
234
+ return this.#size;
235
+ }
236
+ /**
237
+ * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
238
+ */
239
+ get fetchMethod() {
240
+ return this.#fetchMethod;
241
+ }
242
+ get memoMethod() {
243
+ return this.#memoMethod;
244
+ }
245
+ /**
246
+ * {@link LRUCache.OptionsBase.dispose} (read-only)
247
+ */
248
+ get dispose() {
249
+ return this.#dispose;
250
+ }
251
+ /**
252
+ * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
253
+ */
254
+ get disposeAfter() {
255
+ return this.#disposeAfter;
256
+ }
257
+ constructor(options) {
258
+ 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;
259
+ if (max !== 0 && !isPosInt(max)) {
260
+ throw new TypeError("max option must be a nonnegative integer");
261
+ }
262
+ const UintArray = max ? getUintArray(max) : Array;
263
+ if (!UintArray) {
264
+ throw new Error("invalid max value: " + max);
265
+ }
266
+ this.#max = max;
267
+ this.#maxSize = maxSize;
268
+ this.maxEntrySize = maxEntrySize || this.#maxSize;
269
+ this.sizeCalculation = sizeCalculation;
270
+ if (this.sizeCalculation) {
271
+ if (!this.#maxSize && !this.maxEntrySize) {
272
+ throw new TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");
273
+ }
274
+ if (typeof this.sizeCalculation !== "function") {
275
+ throw new TypeError("sizeCalculation set to non-function");
276
+ }
277
+ }
278
+ if (memoMethod !== void 0 && typeof memoMethod !== "function") {
279
+ throw new TypeError("memoMethod must be a function if defined");
280
+ }
281
+ this.#memoMethod = memoMethod;
282
+ if (fetchMethod !== void 0 && typeof fetchMethod !== "function") {
283
+ throw new TypeError("fetchMethod must be a function if specified");
284
+ }
285
+ this.#fetchMethod = fetchMethod;
286
+ this.#hasFetchMethod = !!fetchMethod;
287
+ this.#keyMap = /* @__PURE__ */ new Map();
288
+ this.#keyList = new Array(max).fill(void 0);
289
+ this.#valList = new Array(max).fill(void 0);
290
+ this.#next = new UintArray(max);
291
+ this.#prev = new UintArray(max);
292
+ this.#head = 0;
293
+ this.#tail = 0;
294
+ this.#free = Stack.create(max);
295
+ this.#size = 0;
296
+ this.#calculatedSize = 0;
297
+ if (typeof dispose === "function") {
298
+ this.#dispose = dispose;
299
+ }
300
+ if (typeof disposeAfter === "function") {
301
+ this.#disposeAfter = disposeAfter;
302
+ this.#disposed = [];
303
+ } else {
304
+ this.#disposeAfter = void 0;
305
+ this.#disposed = void 0;
306
+ }
307
+ this.#hasDispose = !!this.#dispose;
308
+ this.#hasDisposeAfter = !!this.#disposeAfter;
309
+ this.noDisposeOnSet = !!noDisposeOnSet;
310
+ this.noUpdateTTL = !!noUpdateTTL;
311
+ this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
312
+ this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
313
+ this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
314
+ this.ignoreFetchAbort = !!ignoreFetchAbort;
315
+ if (this.maxEntrySize !== 0) {
316
+ if (this.#maxSize !== 0) {
317
+ if (!isPosInt(this.#maxSize)) {
318
+ throw new TypeError("maxSize must be a positive integer if specified");
319
+ }
320
+ }
321
+ if (!isPosInt(this.maxEntrySize)) {
322
+ throw new TypeError("maxEntrySize must be a positive integer if specified");
323
+ }
324
+ this.#initializeSizeTracking();
325
+ }
326
+ this.allowStale = !!allowStale;
327
+ this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
328
+ this.updateAgeOnGet = !!updateAgeOnGet;
329
+ this.updateAgeOnHas = !!updateAgeOnHas;
330
+ this.ttlResolution = isPosInt(ttlResolution) || ttlResolution === 0 ? ttlResolution : 1;
331
+ this.ttlAutopurge = !!ttlAutopurge;
332
+ this.ttl = ttl || 0;
333
+ if (this.ttl) {
334
+ if (!isPosInt(this.ttl)) {
335
+ throw new TypeError("ttl must be a positive integer if specified");
336
+ }
337
+ this.#initializeTTLTracking();
338
+ }
339
+ if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
340
+ throw new TypeError("At least one of max, maxSize, or ttl is required");
341
+ }
342
+ if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
343
+ const code = "LRU_CACHE_UNBOUNDED";
344
+ if (shouldWarn(code)) {
345
+ warned.add(code);
346
+ const msg = "TTL caching without ttlAutopurge, max, or maxSize can result in unbounded memory consumption.";
347
+ emitWarning(msg, "UnboundedCacheWarning", code, _LRUCache);
348
+ }
349
+ }
350
+ }
351
+ /**
352
+ * Return the number of ms left in the item's TTL. If item is not in cache,
353
+ * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
354
+ */
355
+ getRemainingTTL(key) {
356
+ return this.#keyMap.has(key) ? Infinity : 0;
357
+ }
358
+ #initializeTTLTracking() {
359
+ const ttls = new ZeroArray(this.#max);
360
+ const starts = new ZeroArray(this.#max);
361
+ this.#ttls = ttls;
362
+ this.#starts = starts;
363
+ this.#setItemTTL = (index, ttl, start = perf.now()) => {
364
+ starts[index] = ttl !== 0 ? start : 0;
365
+ ttls[index] = ttl;
366
+ if (ttl !== 0 && this.ttlAutopurge) {
367
+ const t = setTimeout(() => {
368
+ if (this.#isStale(index)) {
369
+ this.#delete(this.#keyList[index], "expire");
370
+ }
371
+ }, ttl + 1);
372
+ if (t.unref) {
373
+ t.unref();
374
+ }
375
+ }
376
+ };
377
+ this.#updateItemAge = (index) => {
378
+ starts[index] = ttls[index] !== 0 ? perf.now() : 0;
379
+ };
380
+ this.#statusTTL = (status, index) => {
381
+ if (ttls[index]) {
382
+ const ttl = ttls[index];
383
+ const start = starts[index];
384
+ if (!ttl || !start)
385
+ return;
386
+ status.ttl = ttl;
387
+ status.start = start;
388
+ status.now = cachedNow || getNow();
389
+ const age = status.now - start;
390
+ status.remainingTTL = ttl - age;
391
+ }
392
+ };
393
+ let cachedNow = 0;
394
+ const getNow = () => {
395
+ const n = perf.now();
396
+ if (this.ttlResolution > 0) {
397
+ cachedNow = n;
398
+ const t = setTimeout(() => cachedNow = 0, this.ttlResolution);
399
+ if (t.unref) {
400
+ t.unref();
401
+ }
402
+ }
403
+ return n;
404
+ };
405
+ this.getRemainingTTL = (key) => {
406
+ const index = this.#keyMap.get(key);
407
+ if (index === void 0) {
408
+ return 0;
409
+ }
410
+ const ttl = ttls[index];
411
+ const start = starts[index];
412
+ if (!ttl || !start) {
413
+ return Infinity;
414
+ }
415
+ const age = (cachedNow || getNow()) - start;
416
+ return ttl - age;
417
+ };
418
+ this.#isStale = (index) => {
419
+ const s = starts[index];
420
+ const t = ttls[index];
421
+ return !!t && !!s && (cachedNow || getNow()) - s > t;
422
+ };
423
+ }
424
+ // conditionally set private methods related to TTL
425
+ #updateItemAge = () => {
426
+ };
427
+ #statusTTL = () => {
428
+ };
429
+ #setItemTTL = () => {
430
+ };
431
+ /* c8 ignore stop */
432
+ #isStale = () => false;
433
+ #initializeSizeTracking() {
434
+ const sizes = new ZeroArray(this.#max);
435
+ this.#calculatedSize = 0;
436
+ this.#sizes = sizes;
437
+ this.#removeItemSize = (index) => {
438
+ this.#calculatedSize -= sizes[index];
439
+ sizes[index] = 0;
440
+ };
441
+ this.#requireSize = (k, v, size, sizeCalculation) => {
442
+ if (this.#isBackgroundFetch(v)) {
443
+ return 0;
444
+ }
445
+ if (!isPosInt(size)) {
446
+ if (sizeCalculation) {
447
+ if (typeof sizeCalculation !== "function") {
448
+ throw new TypeError("sizeCalculation must be a function");
449
+ }
450
+ size = sizeCalculation(v, k);
451
+ if (!isPosInt(size)) {
452
+ throw new TypeError("sizeCalculation return invalid (expect positive integer)");
453
+ }
454
+ } else {
455
+ throw new TypeError("invalid size value (must be positive integer). When maxSize or maxEntrySize is used, sizeCalculation or size must be set.");
456
+ }
457
+ }
458
+ return size;
459
+ };
460
+ this.#addItemSize = (index, size, status) => {
461
+ sizes[index] = size;
462
+ if (this.#maxSize) {
463
+ const maxSize = this.#maxSize - sizes[index];
464
+ while (this.#calculatedSize > maxSize) {
465
+ this.#evict(true);
466
+ }
467
+ }
468
+ this.#calculatedSize += sizes[index];
469
+ if (status) {
470
+ status.entrySize = size;
471
+ status.totalCalculatedSize = this.#calculatedSize;
472
+ }
473
+ };
474
+ }
475
+ #removeItemSize = (_i) => {
476
+ };
477
+ #addItemSize = (_i, _s, _st) => {
478
+ };
479
+ #requireSize = (_k, _v, size, sizeCalculation) => {
480
+ if (size || sizeCalculation) {
481
+ throw new TypeError("cannot set size without setting maxSize or maxEntrySize on cache");
482
+ }
483
+ return 0;
484
+ };
485
+ *#indexes({ allowStale = this.allowStale } = {}) {
486
+ if (this.#size) {
487
+ for (let i = this.#tail; true; ) {
488
+ if (!this.#isValidIndex(i)) {
489
+ break;
490
+ }
491
+ if (allowStale || !this.#isStale(i)) {
492
+ yield i;
493
+ }
494
+ if (i === this.#head) {
495
+ break;
496
+ } else {
497
+ i = this.#prev[i];
498
+ }
499
+ }
500
+ }
501
+ }
502
+ *#rindexes({ allowStale = this.allowStale } = {}) {
503
+ if (this.#size) {
504
+ for (let i = this.#head; true; ) {
505
+ if (!this.#isValidIndex(i)) {
506
+ break;
507
+ }
508
+ if (allowStale || !this.#isStale(i)) {
509
+ yield i;
510
+ }
511
+ if (i === this.#tail) {
512
+ break;
513
+ } else {
514
+ i = this.#next[i];
515
+ }
516
+ }
517
+ }
518
+ }
519
+ #isValidIndex(index) {
520
+ return index !== void 0 && this.#keyMap.get(this.#keyList[index]) === index;
521
+ }
522
+ /**
523
+ * Return a generator yielding `[key, value]` pairs,
524
+ * in order from most recently used to least recently used.
525
+ */
526
+ *entries() {
527
+ for (const i of this.#indexes()) {
528
+ if (this.#valList[i] !== void 0 && this.#keyList[i] !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
529
+ yield [this.#keyList[i], this.#valList[i]];
530
+ }
531
+ }
532
+ }
533
+ /**
534
+ * Inverse order version of {@link LRUCache.entries}
535
+ *
536
+ * Return a generator yielding `[key, value]` pairs,
537
+ * in order from least recently used to most recently used.
538
+ */
539
+ *rentries() {
540
+ for (const i of this.#rindexes()) {
541
+ if (this.#valList[i] !== void 0 && this.#keyList[i] !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
542
+ yield [this.#keyList[i], this.#valList[i]];
543
+ }
544
+ }
545
+ }
546
+ /**
547
+ * Return a generator yielding the keys in the cache,
548
+ * in order from most recently used to least recently used.
549
+ */
550
+ *keys() {
551
+ for (const i of this.#indexes()) {
552
+ const k = this.#keyList[i];
553
+ if (k !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
554
+ yield k;
555
+ }
556
+ }
557
+ }
558
+ /**
559
+ * Inverse order version of {@link LRUCache.keys}
560
+ *
561
+ * Return a generator yielding the keys in the cache,
562
+ * in order from least recently used to most recently used.
563
+ */
564
+ *rkeys() {
565
+ for (const i of this.#rindexes()) {
566
+ const k = this.#keyList[i];
567
+ if (k !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
568
+ yield k;
569
+ }
570
+ }
571
+ }
572
+ /**
573
+ * Return a generator yielding the values in the cache,
574
+ * in order from most recently used to least recently used.
575
+ */
576
+ *values() {
577
+ for (const i of this.#indexes()) {
578
+ const v = this.#valList[i];
579
+ if (v !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
580
+ yield this.#valList[i];
581
+ }
582
+ }
583
+ }
584
+ /**
585
+ * Inverse order version of {@link LRUCache.values}
586
+ *
587
+ * Return a generator yielding the values in the cache,
588
+ * in order from least recently used to most recently used.
589
+ */
590
+ *rvalues() {
591
+ for (const i of this.#rindexes()) {
592
+ const v = this.#valList[i];
593
+ if (v !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
594
+ yield this.#valList[i];
595
+ }
596
+ }
597
+ }
598
+ /**
599
+ * Iterating over the cache itself yields the same results as
600
+ * {@link LRUCache.entries}
601
+ */
602
+ [Symbol.iterator]() {
603
+ return this.entries();
604
+ }
605
+ /**
606
+ * A String value that is used in the creation of the default string
607
+ * description of an object. Called by the built-in method
608
+ * `Object.prototype.toString`.
609
+ */
610
+ [Symbol.toStringTag] = "LRUCache";
611
+ /**
612
+ * Find a value for which the supplied fn method returns a truthy value,
613
+ * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
614
+ */
615
+ find(fn, getOptions = {}) {
616
+ for (const i of this.#indexes()) {
617
+ const v = this.#valList[i];
618
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
619
+ if (value === void 0)
620
+ continue;
621
+ if (fn(value, this.#keyList[i], this)) {
622
+ return this.get(this.#keyList[i], getOptions);
623
+ }
624
+ }
625
+ }
626
+ /**
627
+ * Call the supplied function on each item in the cache, in order from most
628
+ * recently used to least recently used.
629
+ *
630
+ * `fn` is called as `fn(value, key, cache)`.
631
+ *
632
+ * If `thisp` is provided, function will be called in the `this`-context of
633
+ * the provided object, or the cache if no `thisp` object is provided.
634
+ *
635
+ * Does not update age or recenty of use, or iterate over stale values.
636
+ */
637
+ forEach(fn, thisp = this) {
638
+ for (const i of this.#indexes()) {
639
+ const v = this.#valList[i];
640
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
641
+ if (value === void 0)
642
+ continue;
643
+ fn.call(thisp, value, this.#keyList[i], this);
644
+ }
645
+ }
646
+ /**
647
+ * The same as {@link LRUCache.forEach} but items are iterated over in
648
+ * reverse order. (ie, less recently used items are iterated over first.)
649
+ */
650
+ rforEach(fn, thisp = this) {
651
+ for (const i of this.#rindexes()) {
652
+ const v = this.#valList[i];
653
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
654
+ if (value === void 0)
655
+ continue;
656
+ fn.call(thisp, value, this.#keyList[i], this);
657
+ }
658
+ }
659
+ /**
660
+ * Delete any stale entries. Returns true if anything was removed,
661
+ * false otherwise.
662
+ */
663
+ purgeStale() {
664
+ let deleted = false;
665
+ for (const i of this.#rindexes({ allowStale: true })) {
666
+ if (this.#isStale(i)) {
667
+ this.#delete(this.#keyList[i], "expire");
668
+ deleted = true;
669
+ }
670
+ }
671
+ return deleted;
672
+ }
673
+ /**
674
+ * Get the extended info about a given entry, to get its value, size, and
675
+ * TTL info simultaneously. Returns `undefined` if the key is not present.
676
+ *
677
+ * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
678
+ * serialization, the `start` value is always the current timestamp, and the
679
+ * `ttl` is a calculated remaining time to live (negative if expired).
680
+ *
681
+ * Always returns stale values, if their info is found in the cache, so be
682
+ * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
683
+ * if relevant.
684
+ */
685
+ info(key) {
686
+ const i = this.#keyMap.get(key);
687
+ if (i === void 0)
688
+ return void 0;
689
+ const v = this.#valList[i];
690
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
691
+ if (value === void 0)
692
+ return void 0;
693
+ const entry = { value };
694
+ if (this.#ttls && this.#starts) {
695
+ const ttl = this.#ttls[i];
696
+ const start = this.#starts[i];
697
+ if (ttl && start) {
698
+ const remain = ttl - (perf.now() - start);
699
+ entry.ttl = remain;
700
+ entry.start = Date.now();
701
+ }
702
+ }
703
+ if (this.#sizes) {
704
+ entry.size = this.#sizes[i];
705
+ }
706
+ return entry;
707
+ }
708
+ /**
709
+ * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
710
+ * passed to {@link LRLUCache#load}.
711
+ *
712
+ * The `start` fields are calculated relative to a portable `Date.now()`
713
+ * timestamp, even if `performance.now()` is available.
714
+ *
715
+ * Stale entries are always included in the `dump`, even if
716
+ * {@link LRUCache.OptionsBase.allowStale} is false.
717
+ *
718
+ * Note: this returns an actual array, not a generator, so it can be more
719
+ * easily passed around.
720
+ */
721
+ dump() {
722
+ const arr = [];
723
+ for (const i of this.#indexes({ allowStale: true })) {
724
+ const key = this.#keyList[i];
725
+ const v = this.#valList[i];
726
+ const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
727
+ if (value === void 0 || key === void 0)
728
+ continue;
729
+ const entry = { value };
730
+ if (this.#ttls && this.#starts) {
731
+ entry.ttl = this.#ttls[i];
732
+ const age = perf.now() - this.#starts[i];
733
+ entry.start = Math.floor(Date.now() - age);
734
+ }
735
+ if (this.#sizes) {
736
+ entry.size = this.#sizes[i];
737
+ }
738
+ arr.unshift([key, entry]);
739
+ }
740
+ return arr;
741
+ }
742
+ /**
743
+ * Reset the cache and load in the items in entries in the order listed.
744
+ *
745
+ * The shape of the resulting cache may be different if the same options are
746
+ * not used in both caches.
747
+ *
748
+ * The `start` fields are assumed to be calculated relative to a portable
749
+ * `Date.now()` timestamp, even if `performance.now()` is available.
750
+ */
751
+ load(arr) {
752
+ this.clear();
753
+ for (const [key, entry] of arr) {
754
+ if (entry.start) {
755
+ const age = Date.now() - entry.start;
756
+ entry.start = perf.now() - age;
757
+ }
758
+ this.set(key, entry.value, entry);
759
+ }
760
+ }
761
+ /**
762
+ * Add a value to the cache.
763
+ *
764
+ * Note: if `undefined` is specified as a value, this is an alias for
765
+ * {@link LRUCache#delete}
766
+ *
767
+ * Fields on the {@link LRUCache.SetOptions} options param will override
768
+ * their corresponding values in the constructor options for the scope
769
+ * of this single `set()` operation.
770
+ *
771
+ * If `start` is provided, then that will set the effective start
772
+ * time for the TTL calculation. Note that this must be a previous
773
+ * value of `performance.now()` if supported, or a previous value of
774
+ * `Date.now()` if not.
775
+ *
776
+ * Options object may also include `size`, which will prevent
777
+ * calling the `sizeCalculation` function and just use the specified
778
+ * number if it is a positive integer, and `noDisposeOnSet` which
779
+ * will prevent calling a `dispose` function in the case of
780
+ * overwrites.
781
+ *
782
+ * If the `size` (or return value of `sizeCalculation`) for a given
783
+ * entry is greater than `maxEntrySize`, then the item will not be
784
+ * added to the cache.
785
+ *
786
+ * Will update the recency of the entry.
787
+ *
788
+ * If the value is `undefined`, then this is an alias for
789
+ * `cache.delete(key)`. `undefined` is never stored in the cache.
790
+ */
791
+ set(k, v, setOptions = {}) {
792
+ if (v === void 0) {
793
+ this.delete(k);
794
+ return this;
795
+ }
796
+ const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status } = setOptions;
797
+ let { noUpdateTTL = this.noUpdateTTL } = setOptions;
798
+ const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
799
+ if (this.maxEntrySize && size > this.maxEntrySize) {
800
+ if (status) {
801
+ status.set = "miss";
802
+ status.maxEntrySizeExceeded = true;
803
+ }
804
+ this.#delete(k, "set");
805
+ return this;
806
+ }
807
+ let index = this.#size === 0 ? void 0 : this.#keyMap.get(k);
808
+ if (index === void 0) {
809
+ index = this.#size === 0 ? this.#tail : this.#free.length !== 0 ? this.#free.pop() : this.#size === this.#max ? this.#evict(false) : this.#size;
810
+ this.#keyList[index] = k;
811
+ this.#valList[index] = v;
812
+ this.#keyMap.set(k, index);
813
+ this.#next[this.#tail] = index;
814
+ this.#prev[index] = this.#tail;
815
+ this.#tail = index;
816
+ this.#size++;
817
+ this.#addItemSize(index, size, status);
818
+ if (status)
819
+ status.set = "add";
820
+ noUpdateTTL = false;
821
+ } else {
822
+ this.#moveToTail(index);
823
+ const oldVal = this.#valList[index];
824
+ if (v !== oldVal) {
825
+ if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
826
+ oldVal.__abortController.abort(new Error("replaced"));
827
+ const { __staleWhileFetching: s } = oldVal;
828
+ if (s !== void 0 && !noDisposeOnSet) {
829
+ if (this.#hasDispose) {
830
+ this.#dispose?.(s, k, "set");
831
+ }
832
+ if (this.#hasDisposeAfter) {
833
+ this.#disposed?.push([s, k, "set"]);
834
+ }
835
+ }
836
+ } else if (!noDisposeOnSet) {
837
+ if (this.#hasDispose) {
838
+ this.#dispose?.(oldVal, k, "set");
839
+ }
840
+ if (this.#hasDisposeAfter) {
841
+ this.#disposed?.push([oldVal, k, "set"]);
842
+ }
843
+ }
844
+ this.#removeItemSize(index);
845
+ this.#addItemSize(index, size, status);
846
+ this.#valList[index] = v;
847
+ if (status) {
848
+ status.set = "replace";
849
+ const oldValue = oldVal && this.#isBackgroundFetch(oldVal) ? oldVal.__staleWhileFetching : oldVal;
850
+ if (oldValue !== void 0)
851
+ status.oldValue = oldValue;
852
+ }
853
+ } else if (status) {
854
+ status.set = "update";
855
+ }
856
+ }
857
+ if (ttl !== 0 && !this.#ttls) {
858
+ this.#initializeTTLTracking();
859
+ }
860
+ if (this.#ttls) {
861
+ if (!noUpdateTTL) {
862
+ this.#setItemTTL(index, ttl, start);
863
+ }
864
+ if (status)
865
+ this.#statusTTL(status, index);
866
+ }
867
+ if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
868
+ const dt = this.#disposed;
869
+ let task;
870
+ while (task = dt?.shift()) {
871
+ this.#disposeAfter?.(...task);
872
+ }
873
+ }
874
+ return this;
875
+ }
876
+ /**
877
+ * Evict the least recently used item, returning its value or
878
+ * `undefined` if cache is empty.
879
+ */
880
+ pop() {
881
+ try {
882
+ while (this.#size) {
883
+ const val = this.#valList[this.#head];
884
+ this.#evict(true);
885
+ if (this.#isBackgroundFetch(val)) {
886
+ if (val.__staleWhileFetching) {
887
+ return val.__staleWhileFetching;
888
+ }
889
+ } else if (val !== void 0) {
890
+ return val;
891
+ }
892
+ }
893
+ } finally {
894
+ if (this.#hasDisposeAfter && this.#disposed) {
895
+ const dt = this.#disposed;
896
+ let task;
897
+ while (task = dt?.shift()) {
898
+ this.#disposeAfter?.(...task);
899
+ }
900
+ }
901
+ }
902
+ }
903
+ #evict(free) {
904
+ const head = this.#head;
905
+ const k = this.#keyList[head];
906
+ const v = this.#valList[head];
907
+ if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
908
+ v.__abortController.abort(new Error("evicted"));
909
+ } else if (this.#hasDispose || this.#hasDisposeAfter) {
910
+ if (this.#hasDispose) {
911
+ this.#dispose?.(v, k, "evict");
912
+ }
913
+ if (this.#hasDisposeAfter) {
914
+ this.#disposed?.push([v, k, "evict"]);
915
+ }
916
+ }
917
+ this.#removeItemSize(head);
918
+ if (free) {
919
+ this.#keyList[head] = void 0;
920
+ this.#valList[head] = void 0;
921
+ this.#free.push(head);
922
+ }
923
+ if (this.#size === 1) {
924
+ this.#head = this.#tail = 0;
925
+ this.#free.length = 0;
926
+ } else {
927
+ this.#head = this.#next[head];
928
+ }
929
+ this.#keyMap.delete(k);
930
+ this.#size--;
931
+ return head;
932
+ }
933
+ /**
934
+ * Check if a key is in the cache, without updating the recency of use.
935
+ * Will return false if the item is stale, even though it is technically
936
+ * in the cache.
937
+ *
938
+ * Check if a key is in the cache, without updating the recency of
939
+ * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
940
+ * to `true` in either the options or the constructor.
941
+ *
942
+ * Will return `false` if the item is stale, even though it is technically in
943
+ * the cache. The difference can be determined (if it matters) by using a
944
+ * `status` argument, and inspecting the `has` field.
945
+ *
946
+ * Will not update item age unless
947
+ * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
948
+ */
949
+ has(k, hasOptions = {}) {
950
+ const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
951
+ const index = this.#keyMap.get(k);
952
+ if (index !== void 0) {
953
+ const v = this.#valList[index];
954
+ if (this.#isBackgroundFetch(v) && v.__staleWhileFetching === void 0) {
955
+ return false;
956
+ }
957
+ if (!this.#isStale(index)) {
958
+ if (updateAgeOnHas) {
959
+ this.#updateItemAge(index);
960
+ }
961
+ if (status) {
962
+ status.has = "hit";
963
+ this.#statusTTL(status, index);
964
+ }
965
+ return true;
966
+ } else if (status) {
967
+ status.has = "stale";
968
+ this.#statusTTL(status, index);
969
+ }
970
+ } else if (status) {
971
+ status.has = "miss";
972
+ }
973
+ return false;
974
+ }
975
+ /**
976
+ * Like {@link LRUCache#get} but doesn't update recency or delete stale
977
+ * items.
978
+ *
979
+ * Returns `undefined` if the item is stale, unless
980
+ * {@link LRUCache.OptionsBase.allowStale} is set.
981
+ */
982
+ peek(k, peekOptions = {}) {
983
+ const { allowStale = this.allowStale } = peekOptions;
984
+ const index = this.#keyMap.get(k);
985
+ if (index === void 0 || !allowStale && this.#isStale(index)) {
986
+ return;
987
+ }
988
+ const v = this.#valList[index];
989
+ return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
990
+ }
991
+ #backgroundFetch(k, index, options, context) {
992
+ const v = index === void 0 ? void 0 : this.#valList[index];
993
+ if (this.#isBackgroundFetch(v)) {
994
+ return v;
995
+ }
996
+ const ac = new AC();
997
+ const { signal } = options;
998
+ signal?.addEventListener("abort", () => ac.abort(signal.reason), {
999
+ signal: ac.signal
1000
+ });
1001
+ const fetchOpts = {
1002
+ signal: ac.signal,
1003
+ options,
1004
+ context
1005
+ };
1006
+ const cb = (v2, updateCache = false) => {
1007
+ const { aborted } = ac.signal;
1008
+ const ignoreAbort = options.ignoreFetchAbort && v2 !== void 0;
1009
+ if (options.status) {
1010
+ if (aborted && !updateCache) {
1011
+ options.status.fetchAborted = true;
1012
+ options.status.fetchError = ac.signal.reason;
1013
+ if (ignoreAbort)
1014
+ options.status.fetchAbortIgnored = true;
1015
+ } else {
1016
+ options.status.fetchResolved = true;
1017
+ }
1018
+ }
1019
+ if (aborted && !ignoreAbort && !updateCache) {
1020
+ return fetchFail(ac.signal.reason);
1021
+ }
1022
+ const bf2 = p;
1023
+ if (this.#valList[index] === p) {
1024
+ if (v2 === void 0) {
1025
+ if (bf2.__staleWhileFetching) {
1026
+ this.#valList[index] = bf2.__staleWhileFetching;
1027
+ } else {
1028
+ this.#delete(k, "fetch");
1029
+ }
1030
+ } else {
1031
+ if (options.status)
1032
+ options.status.fetchUpdated = true;
1033
+ this.set(k, v2, fetchOpts.options);
1034
+ }
1035
+ }
1036
+ return v2;
1037
+ };
1038
+ const eb = (er) => {
1039
+ if (options.status) {
1040
+ options.status.fetchRejected = true;
1041
+ options.status.fetchError = er;
1042
+ }
1043
+ return fetchFail(er);
1044
+ };
1045
+ const fetchFail = (er) => {
1046
+ const { aborted } = ac.signal;
1047
+ const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
1048
+ const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
1049
+ const noDelete = allowStale || options.noDeleteOnFetchRejection;
1050
+ const bf2 = p;
1051
+ if (this.#valList[index] === p) {
1052
+ const del = !noDelete || bf2.__staleWhileFetching === void 0;
1053
+ if (del) {
1054
+ this.#delete(k, "fetch");
1055
+ } else if (!allowStaleAborted) {
1056
+ this.#valList[index] = bf2.__staleWhileFetching;
1057
+ }
1058
+ }
1059
+ if (allowStale) {
1060
+ if (options.status && bf2.__staleWhileFetching !== void 0) {
1061
+ options.status.returnedStale = true;
1062
+ }
1063
+ return bf2.__staleWhileFetching;
1064
+ } else if (bf2.__returned === bf2) {
1065
+ throw er;
1066
+ }
1067
+ };
1068
+ const pcall = (res, rej) => {
1069
+ const fmp = this.#fetchMethod?.(k, v, fetchOpts);
1070
+ if (fmp && fmp instanceof Promise) {
1071
+ fmp.then((v2) => res(v2 === void 0 ? void 0 : v2), rej);
1072
+ }
1073
+ ac.signal.addEventListener("abort", () => {
1074
+ if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
1075
+ res(void 0);
1076
+ if (options.allowStaleOnFetchAbort) {
1077
+ res = (v2) => cb(v2, true);
1078
+ }
1079
+ }
1080
+ });
1081
+ };
1082
+ if (options.status)
1083
+ options.status.fetchDispatched = true;
1084
+ const p = new Promise(pcall).then(cb, eb);
1085
+ const bf = Object.assign(p, {
1086
+ __abortController: ac,
1087
+ __staleWhileFetching: v,
1088
+ __returned: void 0
1089
+ });
1090
+ if (index === void 0) {
1091
+ this.set(k, bf, { ...fetchOpts.options, status: void 0 });
1092
+ index = this.#keyMap.get(k);
1093
+ } else {
1094
+ this.#valList[index] = bf;
1095
+ }
1096
+ return bf;
1097
+ }
1098
+ #isBackgroundFetch(p) {
1099
+ if (!this.#hasFetchMethod)
1100
+ return false;
1101
+ const b = p;
1102
+ return !!b && b instanceof Promise && b.hasOwnProperty("__staleWhileFetching") && b.__abortController instanceof AC;
1103
+ }
1104
+ async fetch(k, fetchOptions = {}) {
1105
+ const {
1106
+ // get options
1107
+ allowStale = this.allowStale,
1108
+ updateAgeOnGet = this.updateAgeOnGet,
1109
+ noDeleteOnStaleGet = this.noDeleteOnStaleGet,
1110
+ // set options
1111
+ ttl = this.ttl,
1112
+ noDisposeOnSet = this.noDisposeOnSet,
1113
+ size = 0,
1114
+ sizeCalculation = this.sizeCalculation,
1115
+ noUpdateTTL = this.noUpdateTTL,
1116
+ // fetch exclusive options
1117
+ noDeleteOnFetchRejection = this.noDeleteOnFetchRejection,
1118
+ allowStaleOnFetchRejection = this.allowStaleOnFetchRejection,
1119
+ ignoreFetchAbort = this.ignoreFetchAbort,
1120
+ allowStaleOnFetchAbort = this.allowStaleOnFetchAbort,
1121
+ context,
1122
+ forceRefresh = false,
1123
+ status,
1124
+ signal
1125
+ } = fetchOptions;
1126
+ if (!this.#hasFetchMethod) {
1127
+ if (status)
1128
+ status.fetch = "get";
1129
+ return this.get(k, {
1130
+ allowStale,
1131
+ updateAgeOnGet,
1132
+ noDeleteOnStaleGet,
1133
+ status
1134
+ });
1135
+ }
1136
+ const options = {
1137
+ allowStale,
1138
+ updateAgeOnGet,
1139
+ noDeleteOnStaleGet,
1140
+ ttl,
1141
+ noDisposeOnSet,
1142
+ size,
1143
+ sizeCalculation,
1144
+ noUpdateTTL,
1145
+ noDeleteOnFetchRejection,
1146
+ allowStaleOnFetchRejection,
1147
+ allowStaleOnFetchAbort,
1148
+ ignoreFetchAbort,
1149
+ status,
1150
+ signal
1151
+ };
1152
+ let index = this.#keyMap.get(k);
1153
+ if (index === void 0) {
1154
+ if (status)
1155
+ status.fetch = "miss";
1156
+ const p = this.#backgroundFetch(k, index, options, context);
1157
+ return p.__returned = p;
1158
+ } else {
1159
+ const v = this.#valList[index];
1160
+ if (this.#isBackgroundFetch(v)) {
1161
+ const stale = allowStale && v.__staleWhileFetching !== void 0;
1162
+ if (status) {
1163
+ status.fetch = "inflight";
1164
+ if (stale)
1165
+ status.returnedStale = true;
1166
+ }
1167
+ return stale ? v.__staleWhileFetching : v.__returned = v;
1168
+ }
1169
+ const isStale = this.#isStale(index);
1170
+ if (!forceRefresh && !isStale) {
1171
+ if (status)
1172
+ status.fetch = "hit";
1173
+ this.#moveToTail(index);
1174
+ if (updateAgeOnGet) {
1175
+ this.#updateItemAge(index);
1176
+ }
1177
+ if (status)
1178
+ this.#statusTTL(status, index);
1179
+ return v;
1180
+ }
1181
+ const p = this.#backgroundFetch(k, index, options, context);
1182
+ const hasStale = p.__staleWhileFetching !== void 0;
1183
+ const staleVal = hasStale && allowStale;
1184
+ if (status) {
1185
+ status.fetch = isStale ? "stale" : "refresh";
1186
+ if (staleVal && isStale)
1187
+ status.returnedStale = true;
1188
+ }
1189
+ return staleVal ? p.__staleWhileFetching : p.__returned = p;
1190
+ }
1191
+ }
1192
+ async forceFetch(k, fetchOptions = {}) {
1193
+ const v = await this.fetch(k, fetchOptions);
1194
+ if (v === void 0)
1195
+ throw new Error("fetch() returned undefined");
1196
+ return v;
1197
+ }
1198
+ memo(k, memoOptions = {}) {
1199
+ const memoMethod = this.#memoMethod;
1200
+ if (!memoMethod) {
1201
+ throw new Error("no memoMethod provided to constructor");
1202
+ }
1203
+ const { context, forceRefresh, ...options } = memoOptions;
1204
+ const v = this.get(k, options);
1205
+ if (!forceRefresh && v !== void 0)
1206
+ return v;
1207
+ const vv = memoMethod(k, v, {
1208
+ options,
1209
+ context
1210
+ });
1211
+ this.set(k, vv, options);
1212
+ return vv;
1213
+ }
1214
+ /**
1215
+ * Return a value from the cache. Will update the recency of the cache
1216
+ * entry found.
1217
+ *
1218
+ * If the key is not found, get() will return `undefined`.
1219
+ */
1220
+ get(k, getOptions = {}) {
1221
+ const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions;
1222
+ const index = this.#keyMap.get(k);
1223
+ if (index !== void 0) {
1224
+ const value = this.#valList[index];
1225
+ const fetching = this.#isBackgroundFetch(value);
1226
+ if (status)
1227
+ this.#statusTTL(status, index);
1228
+ if (this.#isStale(index)) {
1229
+ if (status)
1230
+ status.get = "stale";
1231
+ if (!fetching) {
1232
+ if (!noDeleteOnStaleGet) {
1233
+ this.#delete(k, "expire");
1234
+ }
1235
+ if (status && allowStale)
1236
+ status.returnedStale = true;
1237
+ return allowStale ? value : void 0;
1238
+ } else {
1239
+ if (status && allowStale && value.__staleWhileFetching !== void 0) {
1240
+ status.returnedStale = true;
1241
+ }
1242
+ return allowStale ? value.__staleWhileFetching : void 0;
1243
+ }
1244
+ } else {
1245
+ if (status)
1246
+ status.get = "hit";
1247
+ if (fetching) {
1248
+ return value.__staleWhileFetching;
1249
+ }
1250
+ this.#moveToTail(index);
1251
+ if (updateAgeOnGet) {
1252
+ this.#updateItemAge(index);
1253
+ }
1254
+ return value;
1255
+ }
1256
+ } else if (status) {
1257
+ status.get = "miss";
1258
+ }
1259
+ }
1260
+ #connect(p, n) {
1261
+ this.#prev[n] = p;
1262
+ this.#next[p] = n;
1263
+ }
1264
+ #moveToTail(index) {
1265
+ if (index !== this.#tail) {
1266
+ if (index === this.#head) {
1267
+ this.#head = this.#next[index];
1268
+ } else {
1269
+ this.#connect(this.#prev[index], this.#next[index]);
1270
+ }
1271
+ this.#connect(this.#tail, index);
1272
+ this.#tail = index;
1273
+ }
1274
+ }
1275
+ /**
1276
+ * Deletes a key out of the cache.
1277
+ *
1278
+ * Returns true if the key was deleted, false otherwise.
1279
+ */
1280
+ delete(k) {
1281
+ return this.#delete(k, "delete");
1282
+ }
1283
+ #delete(k, reason) {
1284
+ let deleted = false;
1285
+ if (this.#size !== 0) {
1286
+ const index = this.#keyMap.get(k);
1287
+ if (index !== void 0) {
1288
+ deleted = true;
1289
+ if (this.#size === 1) {
1290
+ this.#clear(reason);
1291
+ } else {
1292
+ this.#removeItemSize(index);
1293
+ const v = this.#valList[index];
1294
+ if (this.#isBackgroundFetch(v)) {
1295
+ v.__abortController.abort(new Error("deleted"));
1296
+ } else if (this.#hasDispose || this.#hasDisposeAfter) {
1297
+ if (this.#hasDispose) {
1298
+ this.#dispose?.(v, k, reason);
1299
+ }
1300
+ if (this.#hasDisposeAfter) {
1301
+ this.#disposed?.push([v, k, reason]);
1302
+ }
1303
+ }
1304
+ this.#keyMap.delete(k);
1305
+ this.#keyList[index] = void 0;
1306
+ this.#valList[index] = void 0;
1307
+ if (index === this.#tail) {
1308
+ this.#tail = this.#prev[index];
1309
+ } else if (index === this.#head) {
1310
+ this.#head = this.#next[index];
1311
+ } else {
1312
+ const pi = this.#prev[index];
1313
+ this.#next[pi] = this.#next[index];
1314
+ const ni = this.#next[index];
1315
+ this.#prev[ni] = this.#prev[index];
1316
+ }
1317
+ this.#size--;
1318
+ this.#free.push(index);
1319
+ }
1320
+ }
1321
+ }
1322
+ if (this.#hasDisposeAfter && this.#disposed?.length) {
1323
+ const dt = this.#disposed;
1324
+ let task;
1325
+ while (task = dt?.shift()) {
1326
+ this.#disposeAfter?.(...task);
1327
+ }
1328
+ }
1329
+ return deleted;
1330
+ }
1331
+ /**
1332
+ * Clear the cache entirely, throwing away all values.
1333
+ */
1334
+ clear() {
1335
+ return this.#clear("delete");
1336
+ }
1337
+ #clear(reason) {
1338
+ for (const index of this.#rindexes({ allowStale: true })) {
1339
+ const v = this.#valList[index];
1340
+ if (this.#isBackgroundFetch(v)) {
1341
+ v.__abortController.abort(new Error("deleted"));
1342
+ } else {
1343
+ const k = this.#keyList[index];
1344
+ if (this.#hasDispose) {
1345
+ this.#dispose?.(v, k, reason);
1346
+ }
1347
+ if (this.#hasDisposeAfter) {
1348
+ this.#disposed?.push([v, k, reason]);
1349
+ }
1350
+ }
1351
+ }
1352
+ this.#keyMap.clear();
1353
+ this.#valList.fill(void 0);
1354
+ this.#keyList.fill(void 0);
1355
+ if (this.#ttls && this.#starts) {
1356
+ this.#ttls.fill(0);
1357
+ this.#starts.fill(0);
1358
+ }
1359
+ if (this.#sizes) {
1360
+ this.#sizes.fill(0);
1361
+ }
1362
+ this.#head = 0;
1363
+ this.#tail = 0;
1364
+ this.#free.length = 0;
1365
+ this.#calculatedSize = 0;
1366
+ this.#size = 0;
1367
+ if (this.#hasDisposeAfter && this.#disposed) {
1368
+ const dt = this.#disposed;
1369
+ let task;
1370
+ while (task = dt?.shift()) {
1371
+ this.#disposeAfter?.(...task);
1372
+ }
1373
+ }
1374
+ }
1375
+ };
1376
+
1377
+ // src/run/handlers/request-context.cts
1378
+ import { AsyncLocalStorage } from "node:async_hooks";
1379
+
1380
+ // node_modules/@netlify/functions/dist/internal.js
1381
+ import { env } from "process";
1382
+ var systemLogTag = "__nfSystemLog";
1383
+ var serializeError = (error) => {
1384
+ const cause = error?.cause instanceof Error ? serializeError(error.cause) : error.cause;
1385
+ return {
1386
+ error: error.message,
1387
+ error_cause: cause,
1388
+ error_stack: error.stack
1389
+ };
1390
+ };
1391
+ var SystemLogger = class _SystemLogger {
1392
+ fields;
1393
+ logLevel;
1394
+ constructor(fields = {}, logLevel = 2) {
1395
+ this.fields = fields;
1396
+ this.logLevel = logLevel;
1397
+ }
1398
+ doLog(logger, message) {
1399
+ if (env.NETLIFY_DEV && !env.NETLIFY_ENABLE_SYSTEM_LOGGING) {
1400
+ return;
1401
+ }
1402
+ logger(systemLogTag, JSON.stringify({ msg: message, fields: this.fields }));
1403
+ }
1404
+ log(message) {
1405
+ if (this.logLevel > 2) {
1406
+ return;
1407
+ }
1408
+ this.doLog(console.log, message);
1409
+ }
1410
+ debug(message) {
1411
+ if (this.logLevel > 1) {
1412
+ return;
1413
+ }
1414
+ this.doLog(console.debug, message);
1415
+ }
1416
+ error(message) {
1417
+ if (this.logLevel > 3) {
1418
+ return;
1419
+ }
1420
+ this.doLog(console.error, message);
1421
+ }
1422
+ withLogLevel(level) {
1423
+ return new _SystemLogger(this.fields, level);
1424
+ }
1425
+ withFields(fields) {
1426
+ return new _SystemLogger(
1427
+ {
1428
+ ...this.fields,
1429
+ ...fields
1430
+ },
1431
+ this.logLevel
1432
+ );
1433
+ }
1434
+ withError(error) {
1435
+ const fields = error instanceof Error ? serializeError(error) : { error };
1436
+ return this.withFields(fields);
1437
+ }
1438
+ };
1439
+ var systemLogger = new SystemLogger();
1440
+
1441
+ // src/run/handlers/request-context.cts
1442
+ var REQUEST_CONTEXT_GLOBAL_KEY = Symbol.for("nf-request-context-async-local-storage");
1443
+ var REQUEST_COUNTER_KEY = Symbol.for("nf-request-counter");
1444
+ var extendedGlobalThis = globalThis;
1445
+ var requestContextAsyncLocalStorage;
1446
+ function getRequestContextAsyncLocalStorage() {
1447
+ if (requestContextAsyncLocalStorage) {
1448
+ return requestContextAsyncLocalStorage;
1449
+ }
1450
+ if (extendedGlobalThis[REQUEST_CONTEXT_GLOBAL_KEY]) {
1451
+ return extendedGlobalThis[REQUEST_CONTEXT_GLOBAL_KEY];
1452
+ }
1453
+ const storage = new AsyncLocalStorage();
1454
+ requestContextAsyncLocalStorage = storage;
1455
+ extendedGlobalThis[REQUEST_CONTEXT_GLOBAL_KEY] = storage;
1456
+ return storage;
1457
+ }
1458
+ var getRequestContext = () => getRequestContextAsyncLocalStorage().getStore();
1459
+ function getLogger() {
1460
+ return getRequestContext()?.logger ?? systemLogger;
1461
+ }
1462
+
1463
+ export {
1464
+ LRUCache,
1465
+ getRequestContext,
1466
+ getLogger
1467
+ };