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