@fortedigital/nextjs-cache-handler
Version:
Next.js cache handlers
667 lines (664 loc) • 27.3 kB
JavaScript
// ../../node_modules/.pnpm/lru-cache@11.5.2/node_modules/lru-cache/dist/esm/node/index.min.js
import { tracingChannel as G, channel as P } from "diagnostics_channel";
var S = P("lru-cache:metrics");
var W = G("lru-cache");
var L = typeof performance == "object" && performance && typeof performance.now == "function" ? performance : Date;
var R = () => S.hasSubscribers || W.hasSubscribers;
var U = /* @__PURE__ */ new Set();
var M = typeof process == "object" && process ? process : {};
var k = (d3, e, t, i) => {
typeof M.emitWarning == "function" ? M.emitWarning(d3, e, t, i) : console.error(`[${t}] ${e}: ${d3}`);
};
var H = (d3) => !U.has(d3);
var T = (d3) => !!d3 && d3 === Math.floor(d3) && d3 > 0 && isFinite(d3);
var j = (d3) => T(d3) ? d3 <= Math.pow(2, 8) ? Uint8Array : d3 <= Math.pow(2, 16) ? Uint16Array : d3 <= Math.pow(2, 32) ? Uint32Array : d3 <= Number.MAX_SAFE_INTEGER ? O : null : null;
var O = class extends Array {
constructor(e) {
super(e), this.fill(0);
}
};
var x = class d {
heap;
length;
static #o = false;
static create(e) {
let t = j(e);
if (!t) return [];
d.#o = true;
let i = new d(e, t);
return d.#o = false, i;
}
constructor(e, t) {
if (!d.#o) throw new TypeError("instantiate Stack using Stack.create(n)");
this.heap = new t(e), this.length = 0;
}
push(e) {
this.heap[this.length++] = e;
}
pop() {
return this.heap[--this.length];
}
};
var I = class d2 {
#o;
#c;
#S;
#O;
#w;
#M;
#I;
#m;
get perf() {
return this.#m;
}
ttl;
ttlResolution;
ttlAutopurge;
updateAgeOnGet;
updateAgeOnHas;
allowStale;
noDisposeOnSet;
noUpdateTTL;
maxEntrySize;
sizeCalculation;
noDeleteOnFetchRejection;
noDeleteOnStaleGet;
allowStaleOnFetchAbort;
allowStaleOnFetchRejection;
ignoreFetchAbort;
backgroundFetchSize;
#n;
#b;
#s;
#i;
#t;
#l;
#u;
#a;
#h;
#y;
#r;
#_;
#F;
#d;
#g;
#T;
#U;
#f;
#D;
static unsafeExposeInternals(e) {
return { starts: e.#F, ttls: e.#d, autopurgeTimers: e.#g, sizes: e.#_, keyMap: e.#s, keyList: e.#i, valList: e.#t, next: e.#l, prev: e.#u, get head() {
return e.#a;
}, get tail() {
return e.#h;
}, free: e.#y, isBackgroundFetch: (t) => e.#e(t), backgroundFetch: (t, i, s, n) => e.#P(t, i, s, n), moveToTail: (t) => e.#L(t), indexes: (t) => e.#A(t), rindexes: (t) => e.#z(t), isStale: (t) => e.#p(t) };
}
get max() {
return this.#o;
}
get maxSize() {
return this.#c;
}
get calculatedSize() {
return this.#b;
}
get size() {
return this.#n;
}
get fetchMethod() {
return this.#M;
}
get memoMethod() {
return this.#I;
}
get dispose() {
return this.#S;
}
get onInsert() {
return this.#O;
}
get disposeAfter() {
return this.#w;
}
constructor(e) {
let { max: t = 0, ttl: i, ttlResolution: s = 1, ttlAutopurge: n, updateAgeOnGet: o, updateAgeOnHas: l, allowStale: h, dispose: r, onInsert: c, disposeAfter: m, noDisposeOnSet: _, noUpdateTTL: u, maxSize: g = 0, maxEntrySize: f = 0, sizeCalculation: y, fetchMethod: a, memoMethod: w, noDeleteOnFetchRejection: F, noDeleteOnStaleGet: b, allowStaleOnFetchRejection: p, allowStaleOnFetchAbort: A, ignoreFetchAbort: z, backgroundFetchSize: C = 1, perf: E } = e;
if (this.backgroundFetchSize = C, E !== void 0 && typeof E?.now != "function") throw new TypeError("perf option must have a now() method if specified");
if (this.#m = E ?? L, t !== 0 && !T(t)) throw new TypeError("max option must be a nonnegative integer");
let v = t ? j(t) : Array;
if (!v) throw new Error("invalid max value: " + t);
if (this.#o = t, this.#c = g, this.maxEntrySize = f || this.#c, this.sizeCalculation = y, this.sizeCalculation) {
if (!this.#c && !this.maxEntrySize) throw new TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");
if (typeof this.sizeCalculation != "function") throw new TypeError("sizeCalculation set to non-function");
}
if (w !== void 0 && typeof w != "function") throw new TypeError("memoMethod must be a function if defined");
if (this.#I = w, a !== void 0 && typeof a != "function") throw new TypeError("fetchMethod must be a function if specified");
if (this.#M = a, this.#U = !!a, this.#s = /* @__PURE__ */ new Map(), this.#i = Array.from({ length: t }).fill(void 0), this.#t = Array.from({ length: t }).fill(void 0), this.#l = new v(t), this.#u = new v(t), this.#a = 0, this.#h = 0, this.#y = x.create(t), this.#n = 0, this.#b = 0, typeof r == "function" && (this.#S = r), typeof c == "function" && (this.#O = c), typeof m == "function" ? (this.#w = m, this.#r = []) : (this.#w = void 0, this.#r = void 0), this.#T = !!this.#S, this.#D = !!this.#O, this.#f = !!this.#w, this.noDisposeOnSet = !!_, this.noUpdateTTL = !!u, this.noDeleteOnFetchRejection = !!F, this.allowStaleOnFetchRejection = !!p, this.allowStaleOnFetchAbort = !!A, this.ignoreFetchAbort = !!z, this.maxEntrySize !== 0) {
if (this.#c !== 0 && !T(this.#c)) throw new TypeError("maxSize must be a positive integer if specified");
if (!T(this.maxEntrySize)) throw new TypeError("maxEntrySize must be a positive integer if specified");
this.#X();
}
if (this.allowStale = !!h, this.noDeleteOnStaleGet = !!b, this.updateAgeOnGet = !!o, this.updateAgeOnHas = !!l, this.ttlResolution = T(s) || s === 0 ? s : 1, this.ttlAutopurge = !!n, this.ttl = i || 0, this.ttl) {
if (!T(this.ttl)) throw new TypeError("ttl must be a positive integer if specified");
this.#k();
}
if (this.#o === 0 && this.ttl === 0 && this.#c === 0) throw new TypeError("At least one of max, maxSize, or ttl is required");
if (!this.ttlAutopurge && !this.#o && !this.#c) {
let D = "LRU_CACHE_UNBOUNDED";
H(D) && (U.add(D), k("TTL caching without ttlAutopurge, max, or maxSize can result in unbounded memory consumption.", "UnboundedCacheWarning", D, d2));
}
}
getRemainingTTL(e) {
return this.#s.has(e) ? 1 / 0 : 0;
}
#k() {
let e = new O(this.#o), t = new O(this.#o);
this.#d = e, this.#F = t;
let i = this.ttlAutopurge ? Array.from({ length: this.#o }) : void 0;
this.#g = i, this.#H = (h, r, c = this.#m.now()) => {
t[h] = r !== 0 ? c : 0, e[h] = r, s(h, r);
}, this.#R = (h) => {
t[h] = e[h] !== 0 ? this.#m.now() : 0, s(h, e[h]);
};
let s = this.ttlAutopurge ? (h, r) => {
if (i?.[h] && (clearTimeout(i[h]), i[h] = void 0), r && r !== 0 && i) {
let c = setTimeout(() => {
this.#p(h) ? (this.#E(this.#i[h], "expire"), i[h] = void 0) : s(h, l(h));
}, r + 1);
c.unref && c.unref(), i[h] = c;
}
} : () => {
};
this.#v = (h, r) => {
if (e[r]) {
let c = e[r], m = t[r];
if (!c || !m) return;
h.ttl = c, h.start = m, h.now = n || o();
let _ = h.now - m;
h.remainingTTL = c - _;
}
};
let n = 0, o = () => {
let h = this.#m.now();
if (this.ttlResolution > 0) {
n = h;
let r = setTimeout(() => n = 0, this.ttlResolution);
r.unref && r.unref();
}
return h;
};
this.getRemainingTTL = (h) => {
let r = this.#s.get(h);
return r === void 0 ? 0 : l(r);
};
let l = (h) => {
let r = e[h], c = t[h];
if (!r || !c) return 1 / 0;
let m = (n || o()) - c;
return r - m;
};
this.#p = (h) => {
let r = t[h], c = e[h];
return !!c && !!r && (n || o()) - r > c;
};
}
#R = () => {
};
#v = () => {
};
#H = () => {
};
#p = () => false;
#X() {
let e = new O(this.#o);
this.#b = 0, this.#_ = e, this.#x = (t) => {
this.#b -= e[t], e[t] = 0;
}, this.#N = (t, i, s, n) => {
if (!T(s)) {
if (this.#e(i)) return this.backgroundFetchSize;
if (n) {
if (typeof n != "function") throw new TypeError("sizeCalculation must be a function");
if (s = n(i, t), !T(s)) throw new TypeError("sizeCalculation return invalid (expect positive integer)");
} else throw new TypeError("invalid size value (must be positive integer). When maxSize or maxEntrySize is used, sizeCalculation or size must be set.");
}
return s;
}, this.#j = (t, i, s) => {
if (e[t] = i, this.#c) {
let n = this.#c - e[t];
for (; this.#b > n; ) this.#G(true);
}
this.#b += e[t], s && (s.entrySize = i, s.totalCalculatedSize = this.#b);
};
}
#x = (e) => {
};
#j = (e, t, i) => {
};
#N = (e, t, i, s) => {
if (i || s) throw new TypeError("cannot set size without setting maxSize or maxEntrySize on cache");
return 0;
};
*#A({ allowStale: e = this.allowStale } = {}) {
if (this.#n) for (let t = this.#h; this.#V(t) && ((e || !this.#p(t)) && (yield t), t !== this.#a); ) t = this.#u[t];
}
*#z({ allowStale: e = this.allowStale } = {}) {
if (this.#n) for (let t = this.#a; this.#V(t) && ((e || !this.#p(t)) && (yield t), t !== this.#h); ) t = this.#l[t];
}
#V(e) {
return e !== void 0 && this.#s.get(this.#i[e]) === e;
}
*entries() {
for (let e of this.#A()) this.#t[e] !== void 0 && this.#i[e] !== void 0 && !this.#e(this.#t[e]) && (yield [this.#i[e], this.#t[e]]);
}
*rentries() {
for (let e of this.#z()) this.#t[e] !== void 0 && this.#i[e] !== void 0 && !this.#e(this.#t[e]) && (yield [this.#i[e], this.#t[e]]);
}
*keys() {
for (let e of this.#A()) {
let t = this.#i[e];
t !== void 0 && !this.#e(this.#t[e]) && (yield t);
}
}
*rkeys() {
for (let e of this.#z()) {
let t = this.#i[e];
t !== void 0 && !this.#e(this.#t[e]) && (yield t);
}
}
*values() {
for (let e of this.#A()) this.#t[e] !== void 0 && !this.#e(this.#t[e]) && (yield this.#t[e]);
}
*rvalues() {
for (let e of this.#z()) this.#t[e] !== void 0 && !this.#e(this.#t[e]) && (yield this.#t[e]);
}
[Symbol.iterator]() {
return this.entries();
}
[Symbol.toStringTag] = "LRUCache";
find(e, t = {}) {
for (let i of this.#A()) {
let s = this.#t[i], n = this.#e(s) ? s.__staleWhileFetching : s;
if (n !== void 0 && e(n, this.#i[i], this)) return this.#C(this.#i[i], t);
}
}
forEach(e, t = this) {
for (let i of this.#A()) {
let s = this.#t[i], n = this.#e(s) ? s.__staleWhileFetching : s;
n !== void 0 && e.call(t, n, this.#i[i], this);
}
}
rforEach(e, t = this) {
for (let i of this.#z()) {
let s = this.#t[i], n = this.#e(s) ? s.__staleWhileFetching : s;
n !== void 0 && e.call(t, n, this.#i[i], this);
}
}
purgeStale() {
let e = false;
for (let t of this.#z({ allowStale: true })) this.#p(t) && (this.#E(this.#i[t], "expire"), e = true);
return e;
}
info(e) {
let t = this.#s.get(e);
if (t === void 0) return;
let i = this.#t[t], s = this.#e(i) ? i.__staleWhileFetching : i;
if (s === void 0) return;
let n = { value: s };
if (this.#d && this.#F) {
let o = this.#d[t], l = this.#F[t];
if (o && l) {
let h = o - (this.#m.now() - l);
n.ttl = h, n.start = Date.now();
}
}
return this.#_ && (n.size = this.#_[t]), n;
}
dump() {
let e = [];
for (let t of this.#A({ allowStale: true })) {
let i = this.#i[t], s = this.#t[t], n = this.#e(s) ? s.__staleWhileFetching : s;
if (n === void 0 || i === void 0) continue;
let o = { value: n };
if (this.#d && this.#F) {
o.ttl = this.#d[t];
let l = this.#m.now() - this.#F[t];
o.start = Math.floor(Date.now() - l);
}
this.#_ && (o.size = this.#_[t]), e.unshift([i, o]);
}
return e;
}
load(e) {
this.clear();
for (let [t, i] of e) {
if (i.start) {
let s = Date.now() - i.start;
i.start = this.#m.now() - s;
}
this.#W(t, i.value, i);
}
}
set(e, t, i = {}) {
let { status: s = S.hasSubscribers ? {} : void 0 } = i;
i.status = s, s && (s.op = "set", s.key = e, t !== void 0 && (s.value = t), s.cache = this);
let n = this.#W(e, t, i);
return s && S.hasSubscribers && S.publish(s), n;
}
#W(e, t, i, s) {
let { ttl: n = this.ttl, start: o, noDisposeOnSet: l = this.noDisposeOnSet, sizeCalculation: h = this.sizeCalculation, status: r } = i, c = this.#e(t);
if (t === void 0) return r && (r.set = "deleted"), this.delete(e), this;
let { noUpdateTTL: m = this.noUpdateTTL } = i;
r && !c && (r.value = t);
let _ = this.#N(e, t, i.size || 0, h, r);
if (this.maxEntrySize && _ > this.maxEntrySize) return this.#E(e, "set"), r && (r.set = "miss", r.maxEntrySizeExceeded = true), this;
let u = this.#n === 0 ? void 0 : this.#s.get(e);
if (u === void 0) u = this.#n === 0 ? this.#h : this.#y.length !== 0 ? this.#y.pop() : this.#n === this.#o ? this.#G(false) : this.#n, this.#i[u] = e, this.#t[u] = t, this.#s.set(e, u), this.#l[this.#h] = u, this.#u[u] = this.#h, this.#h = u, this.#n++, this.#j(u, _, r), r && (r.set = "add"), m = false, this.#D && !c && this.#O?.(t, e, "add");
else {
this.#L(u);
let g = this.#t[u];
if (t !== g) {
if (!l) if (this.#e(g)) {
g !== s && g.__abortController.abort(new Error("replaced"));
let { __staleWhileFetching: f } = g;
f !== void 0 && f !== t && (this.#T && this.#S?.(f, e, "set"), this.#f && this.#r?.push([f, e, "set"]));
} else this.#T && this.#S?.(g, e, "set"), this.#f && this.#r?.push([g, e, "set"]);
if (this.#x(u), this.#j(u, _, r), this.#t[u] = t, !c) {
let f = g && this.#e(g) ? g.__staleWhileFetching : g, y = f === void 0 ? "add" : t !== f ? "replace" : "update";
r && (r.set = y, f !== void 0 && (r.oldValue = f)), this.#D && this.onInsert?.(t, e, y);
}
} else c || (r && (r.set = "update"), this.#D && this.onInsert?.(t, e, "update"));
}
if (n !== 0 && !this.#d && this.#k(), this.#d && (m || this.#H(u, n, o), r && this.#v(r, u)), !l && this.#f && this.#r) {
let g = this.#r, f;
for (; f = g?.shift(); ) this.#w?.(...f);
}
return this;
}
pop() {
try {
for (; this.#n; ) {
let e = this.#t[this.#a];
if (this.#G(true), this.#e(e)) {
if (e.__staleWhileFetching) return e.__staleWhileFetching;
} else if (e !== void 0) return e;
}
} finally {
if (this.#f && this.#r) {
let e = this.#r, t;
for (; t = e?.shift(); ) this.#w?.(...t);
}
}
}
#G(e) {
let t = this.#a, i = this.#i[t], s = this.#t[t], n = this.#e(s);
n && s.__abortController.abort(new Error("evicted"));
let o = n ? s.__staleWhileFetching : s;
return (this.#T || this.#f) && o !== void 0 && (this.#T && this.#S?.(o, i, "evict"), this.#f && this.#r?.push([o, i, "evict"])), this.#x(t), this.#g?.[t] && (clearTimeout(this.#g[t]), this.#g[t] = void 0), e && (this.#i[t] = void 0, this.#t[t] = void 0, this.#y.push(t)), this.#n === 1 ? (this.#a = this.#h = 0, this.#y.length = 0) : this.#a = this.#l[t], this.#s.delete(i), this.#n--, t;
}
has(e, t = {}) {
let { status: i = S.hasSubscribers ? {} : void 0 } = t;
t.status = i, i && (i.op = "has", i.key = e, i.cache = this);
let s = this.#Y(e, t);
return S.hasSubscribers && S.publish(i), s;
}
#Y(e, t = {}) {
let { updateAgeOnHas: i = this.updateAgeOnHas, status: s } = t, n = this.#s.get(e);
if (n !== void 0) {
let o = this.#t[n];
if (this.#e(o) && o.__staleWhileFetching === void 0) return false;
if (this.#p(n)) s && (s.has = "stale", this.#v(s, n));
else return i && this.#R(n), s && (s.has = "hit", this.#v(s, n)), true;
} else s && (s.has = "miss");
return false;
}
peek(e, t = {}) {
let { status: i = R() ? {} : void 0 } = t;
i && (i.op = "peek", i.key = e, i.cache = this), t.status = i;
let s = this.#J(e, t);
return S.hasSubscribers && S.publish(i), s;
}
#J(e, t) {
let { status: i, allowStale: s = this.allowStale } = t, n = this.#s.get(e);
if (n === void 0 || !s && this.#p(n)) {
i && (i.peek = n === void 0 ? "miss" : "stale");
return;
}
let o = this.#t[n], l = this.#e(o) ? o.__staleWhileFetching : o;
return i && (l !== void 0 ? (i.peek = "hit", i.value = l) : i.peek = "miss"), l;
}
#P(e, t, i, s) {
let n = t === void 0 ? void 0 : this.#t[t];
if (this.#e(n)) return n;
let o = new AbortController(), { signal: l } = i;
l?.addEventListener("abort", () => o.abort(l.reason), { signal: o.signal });
let h = { signal: o.signal, options: i, context: s }, r = (f, y = false) => {
let { aborted: a } = o.signal, w = i.ignoreFetchAbort && f !== void 0, F = i.ignoreFetchAbort || !!(i.allowStaleOnFetchAbort && f !== void 0);
if (i.status && (a && !y ? (i.status.fetchAborted = true, i.status.fetchError = o.signal.reason, w && (i.status.fetchAbortIgnored = true)) : i.status.fetchResolved = true), a && !w && !y) return m(o.signal.reason, F);
let b = u, p = this.#t[t];
return (p === u || p === void 0 && w && y) && (f === void 0 ? b.__staleWhileFetching !== void 0 ? this.#t[t] = b.__staleWhileFetching : this.#E(e, "fetch") : (i.status && (i.status.fetchUpdated = true), this.#W(e, f, h.options, b))), f;
}, c = (f) => (i.status && (i.status.fetchRejected = true, i.status.fetchError = f), m(f, false)), m = (f, y) => {
let { aborted: a } = o.signal, w = a && i.allowStaleOnFetchAbort, F = w || i.allowStaleOnFetchRejection, b = F || i.noDeleteOnFetchRejection, p = u;
if (this.#t[t] === u && (!b || !y && p.__staleWhileFetching === void 0 ? this.#E(e, "fetch") : w || (this.#t[t] = p.__staleWhileFetching)), F) return i.status && p.__staleWhileFetching !== void 0 && (i.status.returnedStale = true), p.__staleWhileFetching;
if (p.__returned === p) throw f;
}, _ = (f, y) => {
let a = this.#M?.(e, n, h);
o.signal.addEventListener("abort", () => {
(!i.ignoreFetchAbort || i.allowStaleOnFetchAbort) && (f(void 0), i.allowStaleOnFetchAbort && (f = (w) => r(w, true)));
}), a && a instanceof Promise ? a.then((w) => f(w === void 0 ? void 0 : w), y) : a !== void 0 && f(a);
};
i.status && (i.status.fetchDispatched = true);
let u = new Promise(_).then(r, c), g = Object.assign(u, { __abortController: o, __staleWhileFetching: n, __returned: void 0 });
return t === void 0 ? (this.#W(e, g, { ...h.options, status: void 0 }), t = this.#s.get(e)) : this.#t[t] = g, g;
}
#e(e) {
if (!this.#U) return false;
let t = e;
return !!t && t instanceof Promise && t.hasOwnProperty("__staleWhileFetching") && t.__abortController instanceof AbortController;
}
fetch(e, t = {}) {
let i = W.hasSubscribers, { status: s = R() ? {} : void 0 } = t;
t.status = s, s && t.context && (s.context = t.context);
let n = this.#B(e, t);
return s && i && (s.trace = true, W.tracePromise(() => n, s).catch(() => {
})), n;
}
async #B(e, t = {}) {
let { allowStale: i = this.allowStale, updateAgeOnGet: s = this.updateAgeOnGet, noDeleteOnStaleGet: n = this.noDeleteOnStaleGet, ttl: o = this.ttl, noDisposeOnSet: l = this.noDisposeOnSet, size: h = 0, sizeCalculation: r = this.sizeCalculation, noUpdateTTL: c = this.noUpdateTTL, noDeleteOnFetchRejection: m = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection: _ = this.allowStaleOnFetchRejection, ignoreFetchAbort: u = this.ignoreFetchAbort, allowStaleOnFetchAbort: g = this.allowStaleOnFetchAbort, context: f, forceRefresh: y = false, status: a, signal: w } = t;
if (a && (a.op = "fetch", a.key = e, y && (a.forceRefresh = true), a.cache = this), !this.#U) return a && (a.fetch = "get"), this.#C(e, { allowStale: i, updateAgeOnGet: s, noDeleteOnStaleGet: n, status: a });
let F = { allowStale: i, updateAgeOnGet: s, noDeleteOnStaleGet: n, ttl: o, noDisposeOnSet: l, size: h, sizeCalculation: r, noUpdateTTL: c, noDeleteOnFetchRejection: m, allowStaleOnFetchRejection: _, allowStaleOnFetchAbort: g, ignoreFetchAbort: u, status: a, signal: w }, b = this.#s.get(e);
if (b === void 0) {
a && (a.fetch = "miss");
let p = this.#P(e, b, F, f);
return p.__returned = p;
} else {
let p = this.#t[b];
if (this.#e(p)) {
let v = i && p.__staleWhileFetching !== void 0;
return a && (a.fetch = "inflight", v && (a.returnedStale = true)), v ? p.__staleWhileFetching : p.__returned = p;
}
let A = this.#p(b);
if (!y && !A) return a && (a.fetch = "hit"), this.#L(b), s && this.#R(b), a && this.#v(a, b), p;
let z = this.#P(e, b, F, f), E = z.__staleWhileFetching !== void 0 && i;
return a && (a.fetch = A ? "stale" : "refresh", E && A && (a.returnedStale = true)), E ? z.__staleWhileFetching : z.__returned = z;
}
}
forceFetch(e, t = {}) {
let i = W.hasSubscribers, { status: s = R() ? {} : void 0 } = t;
t.status = s, s && t.context && (s.context = t.context);
let n = this.#K(e, t);
return s && i && (s.trace = true, W.tracePromise(() => n, s).catch(() => {
})), n;
}
async #K(e, t = {}) {
let i = await this.#B(e, t);
if (i === void 0) throw new Error("fetch() returned undefined");
return i;
}
memo(e, t = {}) {
let { status: i = S.hasSubscribers ? {} : void 0 } = t;
t.status = i, i && (i.op = "memo", i.key = e, t.context && (i.context = t.context), i.cache = this);
let s = this.#Q(e, t);
return i && (i.value = s), S.hasSubscribers && S.publish(i), s;
}
#Q(e, t = {}) {
let i = this.#I;
if (!i) throw new Error("no memoMethod provided to constructor");
let { context: s, status: n, forceRefresh: o, ...l } = t;
n && o && (n.forceRefresh = true);
let h = this.#C(e, l), r = o || h === void 0;
if (n && (n.memo = r ? "miss" : "hit", r || (n.value = h)), !r) return h;
let c = i(e, h, { options: l, context: s });
return n && (n.value = c), this.#W(e, c, l), c;
}
get(e, t = {}) {
let { status: i = S.hasSubscribers ? {} : void 0 } = t;
t.status = i, i && (i.op = "get", i.key = e, i.cache = this);
let s = this.#C(e, t);
return i && (s !== void 0 && (i.value = s), S.hasSubscribers && S.publish(i)), s;
}
#C(e, t = {}) {
let { allowStale: i = this.allowStale, updateAgeOnGet: s = this.updateAgeOnGet, noDeleteOnStaleGet: n = this.noDeleteOnStaleGet, status: o } = t, l = this.#s.get(e);
if (l === void 0) {
o && (o.get = "miss");
return;
}
let h = this.#t[l], r = this.#e(h);
return o && this.#v(o, l), this.#p(l) ? r ? (o && (o.get = "stale-fetching"), i && h.__staleWhileFetching !== void 0 ? (o && (o.returnedStale = true), h.__staleWhileFetching) : void 0) : (n || this.#E(e, "expire"), o && (o.get = "stale"), i ? (o && (o.returnedStale = true), h) : void 0) : (o && (o.get = r ? "fetching" : "hit"), this.#L(l), s && this.#R(l), r ? h.__staleWhileFetching : h);
}
#$(e, t) {
this.#u[t] = e, this.#l[e] = t;
}
#L(e) {
e !== this.#h && (e === this.#a ? this.#a = this.#l[e] : this.#$(this.#u[e], this.#l[e]), this.#$(this.#h, e), this.#h = e);
}
delete(e) {
return this.#E(e, "delete");
}
#E(e, t) {
S.hasSubscribers && S.publish({ op: "delete", delete: t, key: e, cache: this });
let i = false;
if (this.#n !== 0) {
let s = this.#s.get(e);
if (s !== void 0) if (this.#g?.[s] && (clearTimeout(this.#g[s]), this.#g[s] = void 0), i = true, this.#n === 1) this.#q(t);
else {
this.#x(s);
let n = this.#t[s];
if (this.#e(n) ? n.__abortController.abort(new Error("deleted")) : (this.#T || this.#f) && (this.#T && this.#S?.(n, e, t), this.#f && this.#r?.push([n, e, t])), this.#s.delete(e), this.#i[s] = void 0, this.#t[s] = void 0, s === this.#h) this.#h = this.#u[s];
else if (s === this.#a) this.#a = this.#l[s];
else {
let o = this.#u[s];
this.#l[o] = this.#l[s];
let l = this.#l[s];
this.#u[l] = this.#u[s];
}
this.#n--, this.#y.push(s);
}
}
if (this.#f && this.#r?.length) {
let s = this.#r, n;
for (; n = s?.shift(); ) this.#w?.(...n);
}
return i;
}
clear() {
return this.#q("delete");
}
#q(e) {
for (let t of this.#z({ allowStale: true })) {
let i = this.#t[t];
if (this.#e(i)) i.__abortController.abort(new Error("deleted"));
else {
let s = this.#i[t];
this.#T && this.#S?.(i, s, e), this.#f && this.#r?.push([i, s, e]);
}
}
if (this.#s.clear(), this.#t.fill(void 0), this.#i.fill(void 0), this.#d && this.#F) {
this.#d.fill(0), this.#F.fill(0);
for (let t of this.#g ?? []) t !== void 0 && clearTimeout(t);
this.#g?.fill(void 0);
}
if (this.#_ && this.#_.fill(0), this.#a = 0, this.#h = 0, this.#y.length = 0, this.#b = 0, this.#n = 0, this.#f && this.#r) {
let t = this.#r, i;
for (; i = t?.shift(); ) this.#w?.(...i);
}
}
};
// src/helpers/const.ts
var NEXT_CACHE_IMPLICIT_TAG_ID = "_N_T_";
var MAX_INT32 = 2 ** 31 - 1;
// src/handlers/local-lru.ts
var MAX_ITEMS_NUMBER = 1e3;
var MAX_ITEM_SIZE_BYTES = 100 * 1024 * 1024;
var DEFAULT_OPTIONS = {
maxItemsNumber: MAX_ITEMS_NUMBER,
maxItemSizeBytes: MAX_ITEM_SIZE_BYTES
};
function createConfiguredCache(calculateSizeCallback, {
maxItemsNumber = MAX_ITEMS_NUMBER,
maxItemSizeBytes = MAX_ITEM_SIZE_BYTES
} = DEFAULT_OPTIONS) {
return new I({
max: maxItemsNumber,
maxSize: maxItemSizeBytes,
sizeCalculation: calculateSizeCallback
});
}
function createHandler({
...lruOptions
} = {}) {
const lruCacheStore = createConfiguredCache(
(v) => JSON.stringify(v).length,
lruOptions
);
const revalidatedTags = /* @__PURE__ */ new Map();
return {
name: "local-lru",
get(key, { implicitTags }) {
const cacheValue = lruCacheStore.get(key);
if (!cacheValue) {
return Promise.resolve(null);
}
const sanitizedImplicitTags = implicitTags;
const combinedTags = /* @__PURE__ */ new Set([
...cacheValue.tags,
...sanitizedImplicitTags
]);
if (combinedTags.size === 0) {
return Promise.resolve(cacheValue);
}
for (const tag of combinedTags) {
const revalidationTime = revalidatedTags.get(tag);
if (revalidationTime && revalidationTime > cacheValue.lastModified) {
lruCacheStore.delete(key);
return Promise.resolve(null);
}
}
return Promise.resolve(cacheValue);
},
set(key, cacheHandlerValue, ctx) {
if (ctx?.setOnlyIfNotExists && lruCacheStore.has(key)) {
return Promise.resolve();
}
lruCacheStore.set(key, cacheHandlerValue);
return Promise.resolve();
},
revalidateTag(tag) {
for (const [key, { tags }] of lruCacheStore.entries()) {
if (tags.includes(tag)) {
lruCacheStore.delete(key);
}
}
if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {
revalidatedTags.set(tag, Date.now());
}
return Promise.resolve();
},
delete(key) {
lruCacheStore.delete(key);
return Promise.resolve();
}
};
}
export {
createConfiguredCache,
createHandler as default
};