@module-federation/bridge-react 0.16.0 → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/__tests__/bridge.spec.tsx +10 -10
- package/__tests__/createLazyComponent.spec.tsx +209 -0
- package/__tests__/prefetch.spec.ts +156 -0
- package/__tests__/router.spec.tsx +3 -3
- package/__tests__/setupTests.ts +8 -0
- package/dist/{bridge-base-P6pEjY1q.js → bridge-base-BoshEggF.mjs} +1 -1
- package/dist/{bridge-base-BBH982Tz.cjs → bridge-base-UGCwcMnG.js} +1 -1
- package/dist/data-fetch-server-middleware.cjs.js +163 -0
- package/dist/data-fetch-server-middleware.d.ts +15 -0
- package/dist/data-fetch-server-middleware.es.js +164 -0
- package/dist/data-fetch-utils.cjs.js +24 -0
- package/dist/data-fetch-utils.d.ts +81 -0
- package/dist/data-fetch-utils.es.js +26 -0
- package/dist/index-C0fDZB5b.js +45 -0
- package/dist/index-CqxytsLY.mjs +46 -0
- package/dist/index.cjs.js +35 -9
- package/dist/index.d.ts +141 -1
- package/dist/index.es.js +39 -13
- package/dist/index.esm-BCeUd-x9.mjs +418 -0
- package/dist/index.esm-j_1sIRzg.js +417 -0
- package/dist/lazy-load-component-plugin-B80Ud11k.js +521 -0
- package/dist/lazy-load-component-plugin-_UbR2mWQ.mjs +522 -0
- package/dist/lazy-load-component-plugin.cjs.js +6 -0
- package/dist/lazy-load-component-plugin.d.ts +16 -0
- package/dist/lazy-load-component-plugin.es.js +6 -0
- package/dist/lazy-utils.cjs.js +24 -0
- package/dist/lazy-utils.d.ts +149 -0
- package/dist/lazy-utils.es.js +24 -0
- package/dist/plugin.d.ts +13 -4
- package/dist/prefetch-BaKIdUwP.js +1338 -0
- package/dist/prefetch-YpJYjpWC.mjs +1339 -0
- package/dist/router-v5.cjs.js +1 -1
- package/dist/router-v5.d.ts +9 -0
- package/dist/router-v5.es.js +1 -1
- package/dist/router-v6.cjs.js +1 -1
- package/dist/router-v6.d.ts +9 -0
- package/dist/router-v6.es.js +1 -1
- package/dist/router.cjs.js +1 -1
- package/dist/router.d.ts +9 -0
- package/dist/router.es.js +1 -1
- package/dist/utils-C4oPJV34.mjs +2016 -0
- package/dist/utils-iEVlDmyk.js +2015 -0
- package/dist/v18.cjs.js +1 -1
- package/dist/v18.d.ts +9 -0
- package/dist/v18.es.js +1 -1
- package/dist/v19.cjs.js +1 -1
- package/dist/v19.d.ts +9 -0
- package/dist/v19.es.js +1 -1
- package/jest.config.ts +21 -0
- package/package.json +48 -6
- package/project.json +4 -8
- package/src/index.ts +32 -1
- package/src/lazy/AwaitDataFetch.tsx +215 -0
- package/src/lazy/constant.ts +30 -0
- package/src/lazy/createLazyComponent.tsx +411 -0
- package/src/lazy/data-fetch/cache.ts +291 -0
- package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
- package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
- package/src/lazy/data-fetch/index.ts +16 -0
- package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
- package/src/lazy/data-fetch/prefetch.ts +106 -0
- package/src/lazy/data-fetch/runtime-plugin.ts +115 -0
- package/src/lazy/index.ts +35 -0
- package/src/lazy/logger.ts +6 -0
- package/src/lazy/types.ts +75 -0
- package/src/lazy/utils.ts +372 -0
- package/src/lazy/wrapNoSSR.tsx +10 -0
- package/src/plugins/lazy-load-component-plugin.spec.ts +21 -0
- package/src/plugins/lazy-load-component-plugin.ts +57 -0
- package/src/provider/plugin.ts +4 -4
- package/src/remote/component.tsx +3 -3
- package/src/remote/create.tsx +18 -5
- package/tsconfig.json +1 -1
- package/tsconfig.spec.json +26 -0
- package/vite.config.ts +13 -0
- package/vitest.config.ts +6 -1
- package/dist/index-Cv3p6r66.cjs +0 -235
- package/dist/index-D4yt7Udv.js +0 -236
- package/src/.eslintrc.js +0 -9
|
@@ -0,0 +1,2015 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __typeError = (msg) => {
|
|
4
|
+
throw TypeError(msg);
|
|
5
|
+
};
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
9
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
10
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
11
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
12
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
13
|
+
var __privateWrapper = (obj, member, setter, getter) => ({
|
|
14
|
+
set _(value) {
|
|
15
|
+
__privateSet(obj, member, value, setter);
|
|
16
|
+
},
|
|
17
|
+
get _() {
|
|
18
|
+
return __privateGet(obj, member, getter);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
var _a, _constructing, _b, _c, _max, _maxSize, _dispose, _disposeAfter, _fetchMethod, _memoMethod, _size, _calculatedSize, _keyMap, _keyList, _valList, _next, _prev, _head, _tail, _free, _disposed, _sizes, _starts, _ttls, _hasDispose, _hasFetchMethod, _hasDisposeAfter, _LRUCache_instances, initializeTTLTracking_fn, _updateItemAge, _statusTTL, _setItemTTL, _isStale, initializeSizeTracking_fn, _removeItemSize, _addItemSize, _requireSize, indexes_fn, rindexes_fn, isValidIndex_fn, evict_fn, backgroundFetch_fn, isBackgroundFetch_fn, connect_fn, moveToTail_fn, delete_fn, clear_fn;
|
|
22
|
+
const index_esm = require("./index.esm-j_1sIRzg.js");
|
|
23
|
+
const PLUGIN_IDENTIFIER = "[ Module Federation React ]";
|
|
24
|
+
const DOWNGRADE_KEY = "_mfSSRDowngrade";
|
|
25
|
+
const DATA_FETCH_FUNCTION = "_mfDataFetch";
|
|
26
|
+
const FS_HREF = "_mfFSHref";
|
|
27
|
+
const ERROR_TYPE = {
|
|
28
|
+
DATA_FETCH: 1,
|
|
29
|
+
LOAD_REMOTE: 2,
|
|
30
|
+
UNKNOWN: 3
|
|
31
|
+
};
|
|
32
|
+
const WRAP_DATA_FETCH_ID_IDENTIFIER = "wrap_dfip_identifier";
|
|
33
|
+
var MF_DATA_FETCH_TYPE = /* @__PURE__ */ ((MF_DATA_FETCH_TYPE2) => {
|
|
34
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_SERVER"] = 1] = "FETCH_SERVER";
|
|
35
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_CLIENT"] = 2] = "FETCH_CLIENT";
|
|
36
|
+
return MF_DATA_FETCH_TYPE2;
|
|
37
|
+
})(MF_DATA_FETCH_TYPE || {});
|
|
38
|
+
var MF_DATA_FETCH_STATUS = /* @__PURE__ */ ((MF_DATA_FETCH_STATUS2) => {
|
|
39
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADED"] = 1] = "LOADED";
|
|
40
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADING"] = 2] = "LOADING";
|
|
41
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["AWAIT"] = 0] = "AWAIT";
|
|
42
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["ERROR"] = 3] = "ERROR";
|
|
43
|
+
return MF_DATA_FETCH_STATUS2;
|
|
44
|
+
})(MF_DATA_FETCH_STATUS || {});
|
|
45
|
+
const DATA_FETCH_IDENTIFIER = "data";
|
|
46
|
+
const DATA_FETCH_CLIENT_SUFFIX = ".client";
|
|
47
|
+
const DATA_FETCH_QUERY = "x-mf-data-fetch";
|
|
48
|
+
const DATA_FETCH_ERROR_PREFIX = "caught the following error during dataFetch: ";
|
|
49
|
+
const LOAD_REMOTE_ERROR_PREFIX = "caught the following error during loadRemote: ";
|
|
50
|
+
const logger = index_esm.createLogger(PLUGIN_IDENTIFIER);
|
|
51
|
+
const perf = typeof performance === "object" && performance && typeof performance.now === "function" ? performance : Date;
|
|
52
|
+
const warned = /* @__PURE__ */ new Set();
|
|
53
|
+
const PROCESS = typeof process === "object" && !!process ? process : {};
|
|
54
|
+
const emitWarning = (msg, type, code, fn) => {
|
|
55
|
+
typeof PROCESS.emitWarning === "function" ? PROCESS.emitWarning(msg, type, code, fn) : console.error(`[${code}] ${type}: ${msg}`);
|
|
56
|
+
};
|
|
57
|
+
let AC = globalThis.AbortController;
|
|
58
|
+
let AS = globalThis.AbortSignal;
|
|
59
|
+
if (typeof AC === "undefined") {
|
|
60
|
+
AS = class AbortSignal {
|
|
61
|
+
constructor() {
|
|
62
|
+
__publicField(this, "onabort");
|
|
63
|
+
__publicField(this, "_onabort", []);
|
|
64
|
+
__publicField(this, "reason");
|
|
65
|
+
__publicField(this, "aborted", false);
|
|
66
|
+
}
|
|
67
|
+
addEventListener(_, fn) {
|
|
68
|
+
this._onabort.push(fn);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
AC = class AbortController {
|
|
72
|
+
constructor() {
|
|
73
|
+
__publicField(this, "signal", new AS());
|
|
74
|
+
warnACPolyfill();
|
|
75
|
+
}
|
|
76
|
+
abort(reason) {
|
|
77
|
+
var _a2, _b2;
|
|
78
|
+
if (this.signal.aborted)
|
|
79
|
+
return;
|
|
80
|
+
this.signal.reason = reason;
|
|
81
|
+
this.signal.aborted = true;
|
|
82
|
+
for (const fn of this.signal._onabort) {
|
|
83
|
+
fn(reason);
|
|
84
|
+
}
|
|
85
|
+
(_b2 = (_a2 = this.signal).onabort) == null ? void 0 : _b2.call(_a2, reason);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
let printACPolyfillWarning = ((_a = PROCESS.env) == null ? void 0 : _a.LRU_CACHE_IGNORE_AC_WARNING) !== "1";
|
|
89
|
+
const warnACPolyfill = () => {
|
|
90
|
+
if (!printACPolyfillWarning)
|
|
91
|
+
return;
|
|
92
|
+
printACPolyfillWarning = false;
|
|
93
|
+
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);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const shouldWarn = (code) => !warned.has(code);
|
|
97
|
+
const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
|
|
98
|
+
const 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;
|
|
99
|
+
class ZeroArray extends Array {
|
|
100
|
+
constructor(size) {
|
|
101
|
+
super(size);
|
|
102
|
+
this.fill(0);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const _Stack = class _Stack {
|
|
106
|
+
constructor(max, HeapCls) {
|
|
107
|
+
__publicField(this, "heap");
|
|
108
|
+
__publicField(this, "length");
|
|
109
|
+
if (!__privateGet(_Stack, _constructing)) {
|
|
110
|
+
throw new TypeError("instantiate Stack using Stack.create(n)");
|
|
111
|
+
}
|
|
112
|
+
this.heap = new HeapCls(max);
|
|
113
|
+
this.length = 0;
|
|
114
|
+
}
|
|
115
|
+
static create(max) {
|
|
116
|
+
const HeapCls = getUintArray(max);
|
|
117
|
+
if (!HeapCls)
|
|
118
|
+
return [];
|
|
119
|
+
__privateSet(_Stack, _constructing, true);
|
|
120
|
+
const s = new _Stack(max, HeapCls);
|
|
121
|
+
__privateSet(_Stack, _constructing, false);
|
|
122
|
+
return s;
|
|
123
|
+
}
|
|
124
|
+
push(n) {
|
|
125
|
+
this.heap[this.length++] = n;
|
|
126
|
+
}
|
|
127
|
+
pop() {
|
|
128
|
+
return this.heap[--this.length];
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
_constructing = new WeakMap();
|
|
132
|
+
// private constructor
|
|
133
|
+
__privateAdd(_Stack, _constructing, false);
|
|
134
|
+
let Stack = _Stack;
|
|
135
|
+
const _LRUCache = class _LRUCache {
|
|
136
|
+
constructor(options) {
|
|
137
|
+
__privateAdd(this, _LRUCache_instances);
|
|
138
|
+
// options that cannot be changed without disaster
|
|
139
|
+
__privateAdd(this, _max);
|
|
140
|
+
__privateAdd(this, _maxSize);
|
|
141
|
+
__privateAdd(this, _dispose);
|
|
142
|
+
__privateAdd(this, _disposeAfter);
|
|
143
|
+
__privateAdd(this, _fetchMethod);
|
|
144
|
+
__privateAdd(this, _memoMethod);
|
|
145
|
+
/**
|
|
146
|
+
* {@link LRUCache.OptionsBase.ttl}
|
|
147
|
+
*/
|
|
148
|
+
__publicField(this, "ttl");
|
|
149
|
+
/**
|
|
150
|
+
* {@link LRUCache.OptionsBase.ttlResolution}
|
|
151
|
+
*/
|
|
152
|
+
__publicField(this, "ttlResolution");
|
|
153
|
+
/**
|
|
154
|
+
* {@link LRUCache.OptionsBase.ttlAutopurge}
|
|
155
|
+
*/
|
|
156
|
+
__publicField(this, "ttlAutopurge");
|
|
157
|
+
/**
|
|
158
|
+
* {@link LRUCache.OptionsBase.updateAgeOnGet}
|
|
159
|
+
*/
|
|
160
|
+
__publicField(this, "updateAgeOnGet");
|
|
161
|
+
/**
|
|
162
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas}
|
|
163
|
+
*/
|
|
164
|
+
__publicField(this, "updateAgeOnHas");
|
|
165
|
+
/**
|
|
166
|
+
* {@link LRUCache.OptionsBase.allowStale}
|
|
167
|
+
*/
|
|
168
|
+
__publicField(this, "allowStale");
|
|
169
|
+
/**
|
|
170
|
+
* {@link LRUCache.OptionsBase.noDisposeOnSet}
|
|
171
|
+
*/
|
|
172
|
+
__publicField(this, "noDisposeOnSet");
|
|
173
|
+
/**
|
|
174
|
+
* {@link LRUCache.OptionsBase.noUpdateTTL}
|
|
175
|
+
*/
|
|
176
|
+
__publicField(this, "noUpdateTTL");
|
|
177
|
+
/**
|
|
178
|
+
* {@link LRUCache.OptionsBase.maxEntrySize}
|
|
179
|
+
*/
|
|
180
|
+
__publicField(this, "maxEntrySize");
|
|
181
|
+
/**
|
|
182
|
+
* {@link LRUCache.OptionsBase.sizeCalculation}
|
|
183
|
+
*/
|
|
184
|
+
__publicField(this, "sizeCalculation");
|
|
185
|
+
/**
|
|
186
|
+
* {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
|
|
187
|
+
*/
|
|
188
|
+
__publicField(this, "noDeleteOnFetchRejection");
|
|
189
|
+
/**
|
|
190
|
+
* {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
|
|
191
|
+
*/
|
|
192
|
+
__publicField(this, "noDeleteOnStaleGet");
|
|
193
|
+
/**
|
|
194
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
|
|
195
|
+
*/
|
|
196
|
+
__publicField(this, "allowStaleOnFetchAbort");
|
|
197
|
+
/**
|
|
198
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
|
|
199
|
+
*/
|
|
200
|
+
__publicField(this, "allowStaleOnFetchRejection");
|
|
201
|
+
/**
|
|
202
|
+
* {@link LRUCache.OptionsBase.ignoreFetchAbort}
|
|
203
|
+
*/
|
|
204
|
+
__publicField(this, "ignoreFetchAbort");
|
|
205
|
+
// computed properties
|
|
206
|
+
__privateAdd(this, _size);
|
|
207
|
+
__privateAdd(this, _calculatedSize);
|
|
208
|
+
__privateAdd(this, _keyMap);
|
|
209
|
+
__privateAdd(this, _keyList);
|
|
210
|
+
__privateAdd(this, _valList);
|
|
211
|
+
__privateAdd(this, _next);
|
|
212
|
+
__privateAdd(this, _prev);
|
|
213
|
+
__privateAdd(this, _head);
|
|
214
|
+
__privateAdd(this, _tail);
|
|
215
|
+
__privateAdd(this, _free);
|
|
216
|
+
__privateAdd(this, _disposed);
|
|
217
|
+
__privateAdd(this, _sizes);
|
|
218
|
+
__privateAdd(this, _starts);
|
|
219
|
+
__privateAdd(this, _ttls);
|
|
220
|
+
__privateAdd(this, _hasDispose);
|
|
221
|
+
__privateAdd(this, _hasFetchMethod);
|
|
222
|
+
__privateAdd(this, _hasDisposeAfter);
|
|
223
|
+
// conditionally set private methods related to TTL
|
|
224
|
+
__privateAdd(this, _updateItemAge, () => {
|
|
225
|
+
});
|
|
226
|
+
__privateAdd(this, _statusTTL, () => {
|
|
227
|
+
});
|
|
228
|
+
__privateAdd(this, _setItemTTL, () => {
|
|
229
|
+
});
|
|
230
|
+
/* c8 ignore stop */
|
|
231
|
+
__privateAdd(this, _isStale, () => false);
|
|
232
|
+
__privateAdd(this, _removeItemSize, (_i) => {
|
|
233
|
+
});
|
|
234
|
+
__privateAdd(this, _addItemSize, (_i, _s, _st) => {
|
|
235
|
+
});
|
|
236
|
+
__privateAdd(this, _requireSize, (_k, _v, size, sizeCalculation) => {
|
|
237
|
+
if (size || sizeCalculation) {
|
|
238
|
+
throw new TypeError("cannot set size without setting maxSize or maxEntrySize on cache");
|
|
239
|
+
}
|
|
240
|
+
return 0;
|
|
241
|
+
});
|
|
242
|
+
/**
|
|
243
|
+
* A String value that is used in the creation of the default string
|
|
244
|
+
* description of an object. Called by the built-in method
|
|
245
|
+
* `Object.prototype.toString`.
|
|
246
|
+
*/
|
|
247
|
+
__publicField(this, _b, "LRUCache");
|
|
248
|
+
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;
|
|
249
|
+
if (max !== 0 && !isPosInt(max)) {
|
|
250
|
+
throw new TypeError("max option must be a nonnegative integer");
|
|
251
|
+
}
|
|
252
|
+
const UintArray = max ? getUintArray(max) : Array;
|
|
253
|
+
if (!UintArray) {
|
|
254
|
+
throw new Error("invalid max value: " + max);
|
|
255
|
+
}
|
|
256
|
+
__privateSet(this, _max, max);
|
|
257
|
+
__privateSet(this, _maxSize, maxSize);
|
|
258
|
+
this.maxEntrySize = maxEntrySize || __privateGet(this, _maxSize);
|
|
259
|
+
this.sizeCalculation = sizeCalculation;
|
|
260
|
+
if (this.sizeCalculation) {
|
|
261
|
+
if (!__privateGet(this, _maxSize) && !this.maxEntrySize) {
|
|
262
|
+
throw new TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");
|
|
263
|
+
}
|
|
264
|
+
if (typeof this.sizeCalculation !== "function") {
|
|
265
|
+
throw new TypeError("sizeCalculation set to non-function");
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (memoMethod !== void 0 && typeof memoMethod !== "function") {
|
|
269
|
+
throw new TypeError("memoMethod must be a function if defined");
|
|
270
|
+
}
|
|
271
|
+
__privateSet(this, _memoMethod, memoMethod);
|
|
272
|
+
if (fetchMethod !== void 0 && typeof fetchMethod !== "function") {
|
|
273
|
+
throw new TypeError("fetchMethod must be a function if specified");
|
|
274
|
+
}
|
|
275
|
+
__privateSet(this, _fetchMethod, fetchMethod);
|
|
276
|
+
__privateSet(this, _hasFetchMethod, !!fetchMethod);
|
|
277
|
+
__privateSet(this, _keyMap, /* @__PURE__ */ new Map());
|
|
278
|
+
__privateSet(this, _keyList, new Array(max).fill(void 0));
|
|
279
|
+
__privateSet(this, _valList, new Array(max).fill(void 0));
|
|
280
|
+
__privateSet(this, _next, new UintArray(max));
|
|
281
|
+
__privateSet(this, _prev, new UintArray(max));
|
|
282
|
+
__privateSet(this, _head, 0);
|
|
283
|
+
__privateSet(this, _tail, 0);
|
|
284
|
+
__privateSet(this, _free, Stack.create(max));
|
|
285
|
+
__privateSet(this, _size, 0);
|
|
286
|
+
__privateSet(this, _calculatedSize, 0);
|
|
287
|
+
if (typeof dispose === "function") {
|
|
288
|
+
__privateSet(this, _dispose, dispose);
|
|
289
|
+
}
|
|
290
|
+
if (typeof disposeAfter === "function") {
|
|
291
|
+
__privateSet(this, _disposeAfter, disposeAfter);
|
|
292
|
+
__privateSet(this, _disposed, []);
|
|
293
|
+
} else {
|
|
294
|
+
__privateSet(this, _disposeAfter, void 0);
|
|
295
|
+
__privateSet(this, _disposed, void 0);
|
|
296
|
+
}
|
|
297
|
+
__privateSet(this, _hasDispose, !!__privateGet(this, _dispose));
|
|
298
|
+
__privateSet(this, _hasDisposeAfter, !!__privateGet(this, _disposeAfter));
|
|
299
|
+
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
300
|
+
this.noUpdateTTL = !!noUpdateTTL;
|
|
301
|
+
this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
|
|
302
|
+
this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
|
|
303
|
+
this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
|
|
304
|
+
this.ignoreFetchAbort = !!ignoreFetchAbort;
|
|
305
|
+
if (this.maxEntrySize !== 0) {
|
|
306
|
+
if (__privateGet(this, _maxSize) !== 0) {
|
|
307
|
+
if (!isPosInt(__privateGet(this, _maxSize))) {
|
|
308
|
+
throw new TypeError("maxSize must be a positive integer if specified");
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (!isPosInt(this.maxEntrySize)) {
|
|
312
|
+
throw new TypeError("maxEntrySize must be a positive integer if specified");
|
|
313
|
+
}
|
|
314
|
+
__privateMethod(this, _LRUCache_instances, initializeSizeTracking_fn).call(this);
|
|
315
|
+
}
|
|
316
|
+
this.allowStale = !!allowStale;
|
|
317
|
+
this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
|
|
318
|
+
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
319
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
320
|
+
this.ttlResolution = isPosInt(ttlResolution) || ttlResolution === 0 ? ttlResolution : 1;
|
|
321
|
+
this.ttlAutopurge = !!ttlAutopurge;
|
|
322
|
+
this.ttl = ttl || 0;
|
|
323
|
+
if (this.ttl) {
|
|
324
|
+
if (!isPosInt(this.ttl)) {
|
|
325
|
+
throw new TypeError("ttl must be a positive integer if specified");
|
|
326
|
+
}
|
|
327
|
+
__privateMethod(this, _LRUCache_instances, initializeTTLTracking_fn).call(this);
|
|
328
|
+
}
|
|
329
|
+
if (__privateGet(this, _max) === 0 && this.ttl === 0 && __privateGet(this, _maxSize) === 0) {
|
|
330
|
+
throw new TypeError("At least one of max, maxSize, or ttl is required");
|
|
331
|
+
}
|
|
332
|
+
if (!this.ttlAutopurge && !__privateGet(this, _max) && !__privateGet(this, _maxSize)) {
|
|
333
|
+
const code = "LRU_CACHE_UNBOUNDED";
|
|
334
|
+
if (shouldWarn(code)) {
|
|
335
|
+
warned.add(code);
|
|
336
|
+
const msg = "TTL caching without ttlAutopurge, max, or maxSize can result in unbounded memory consumption.";
|
|
337
|
+
emitWarning(msg, "UnboundedCacheWarning", code, _LRUCache);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Do not call this method unless you need to inspect the
|
|
343
|
+
* inner workings of the cache. If anything returned by this
|
|
344
|
+
* object is modified in any way, strange breakage may occur.
|
|
345
|
+
*
|
|
346
|
+
* These fields are private for a reason!
|
|
347
|
+
*
|
|
348
|
+
* @internal
|
|
349
|
+
*/
|
|
350
|
+
static unsafeExposeInternals(c) {
|
|
351
|
+
return {
|
|
352
|
+
// properties
|
|
353
|
+
starts: __privateGet(c, _starts),
|
|
354
|
+
ttls: __privateGet(c, _ttls),
|
|
355
|
+
sizes: __privateGet(c, _sizes),
|
|
356
|
+
keyMap: __privateGet(c, _keyMap),
|
|
357
|
+
keyList: __privateGet(c, _keyList),
|
|
358
|
+
valList: __privateGet(c, _valList),
|
|
359
|
+
next: __privateGet(c, _next),
|
|
360
|
+
prev: __privateGet(c, _prev),
|
|
361
|
+
get head() {
|
|
362
|
+
return __privateGet(c, _head);
|
|
363
|
+
},
|
|
364
|
+
get tail() {
|
|
365
|
+
return __privateGet(c, _tail);
|
|
366
|
+
},
|
|
367
|
+
free: __privateGet(c, _free),
|
|
368
|
+
// methods
|
|
369
|
+
isBackgroundFetch: (p) => {
|
|
370
|
+
var _a2;
|
|
371
|
+
return __privateMethod(_a2 = c, _LRUCache_instances, isBackgroundFetch_fn).call(_a2, p);
|
|
372
|
+
},
|
|
373
|
+
backgroundFetch: (k, index, options, context) => {
|
|
374
|
+
var _a2;
|
|
375
|
+
return __privateMethod(_a2 = c, _LRUCache_instances, backgroundFetch_fn).call(_a2, k, index, options, context);
|
|
376
|
+
},
|
|
377
|
+
moveToTail: (index) => {
|
|
378
|
+
var _a2;
|
|
379
|
+
return __privateMethod(_a2 = c, _LRUCache_instances, moveToTail_fn).call(_a2, index);
|
|
380
|
+
},
|
|
381
|
+
indexes: (options) => {
|
|
382
|
+
var _a2;
|
|
383
|
+
return __privateMethod(_a2 = c, _LRUCache_instances, indexes_fn).call(_a2, options);
|
|
384
|
+
},
|
|
385
|
+
rindexes: (options) => {
|
|
386
|
+
var _a2;
|
|
387
|
+
return __privateMethod(_a2 = c, _LRUCache_instances, rindexes_fn).call(_a2, options);
|
|
388
|
+
},
|
|
389
|
+
isStale: (index) => {
|
|
390
|
+
var _a2;
|
|
391
|
+
return __privateGet(_a2 = c, _isStale).call(_a2, index);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
// Protected read-only members
|
|
396
|
+
/**
|
|
397
|
+
* {@link LRUCache.OptionsBase.max} (read-only)
|
|
398
|
+
*/
|
|
399
|
+
get max() {
|
|
400
|
+
return __privateGet(this, _max);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* {@link LRUCache.OptionsBase.maxSize} (read-only)
|
|
404
|
+
*/
|
|
405
|
+
get maxSize() {
|
|
406
|
+
return __privateGet(this, _maxSize);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* The total computed size of items in the cache (read-only)
|
|
410
|
+
*/
|
|
411
|
+
get calculatedSize() {
|
|
412
|
+
return __privateGet(this, _calculatedSize);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* The number of items stored in the cache (read-only)
|
|
416
|
+
*/
|
|
417
|
+
get size() {
|
|
418
|
+
return __privateGet(this, _size);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* {@link LRUCache.OptionsBase.fetchMethod} (read-only)
|
|
422
|
+
*/
|
|
423
|
+
get fetchMethod() {
|
|
424
|
+
return __privateGet(this, _fetchMethod);
|
|
425
|
+
}
|
|
426
|
+
get memoMethod() {
|
|
427
|
+
return __privateGet(this, _memoMethod);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* {@link LRUCache.OptionsBase.dispose} (read-only)
|
|
431
|
+
*/
|
|
432
|
+
get dispose() {
|
|
433
|
+
return __privateGet(this, _dispose);
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* {@link LRUCache.OptionsBase.disposeAfter} (read-only)
|
|
437
|
+
*/
|
|
438
|
+
get disposeAfter() {
|
|
439
|
+
return __privateGet(this, _disposeAfter);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Return the number of ms left in the item's TTL. If item is not in cache,
|
|
443
|
+
* returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
|
|
444
|
+
*/
|
|
445
|
+
getRemainingTTL(key) {
|
|
446
|
+
return __privateGet(this, _keyMap).has(key) ? Infinity : 0;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
450
|
+
* in order from most recently used to least recently used.
|
|
451
|
+
*/
|
|
452
|
+
*entries() {
|
|
453
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this)) {
|
|
454
|
+
if (__privateGet(this, _valList)[i] !== void 0 && __privateGet(this, _keyList)[i] !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
455
|
+
yield [__privateGet(this, _keyList)[i], __privateGet(this, _valList)[i]];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Inverse order version of {@link LRUCache.entries}
|
|
461
|
+
*
|
|
462
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
463
|
+
* in order from least recently used to most recently used.
|
|
464
|
+
*/
|
|
465
|
+
*rentries() {
|
|
466
|
+
for (const i of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this)) {
|
|
467
|
+
if (__privateGet(this, _valList)[i] !== void 0 && __privateGet(this, _keyList)[i] !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
468
|
+
yield [__privateGet(this, _keyList)[i], __privateGet(this, _valList)[i]];
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Return a generator yielding the keys in the cache,
|
|
474
|
+
* in order from most recently used to least recently used.
|
|
475
|
+
*/
|
|
476
|
+
*keys() {
|
|
477
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this)) {
|
|
478
|
+
const k = __privateGet(this, _keyList)[i];
|
|
479
|
+
if (k !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
480
|
+
yield k;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Inverse order version of {@link LRUCache.keys}
|
|
486
|
+
*
|
|
487
|
+
* Return a generator yielding the keys in the cache,
|
|
488
|
+
* in order from least recently used to most recently used.
|
|
489
|
+
*/
|
|
490
|
+
*rkeys() {
|
|
491
|
+
for (const i of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this)) {
|
|
492
|
+
const k = __privateGet(this, _keyList)[i];
|
|
493
|
+
if (k !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
494
|
+
yield k;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Return a generator yielding the values in the cache,
|
|
500
|
+
* in order from most recently used to least recently used.
|
|
501
|
+
*/
|
|
502
|
+
*values() {
|
|
503
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this)) {
|
|
504
|
+
const v = __privateGet(this, _valList)[i];
|
|
505
|
+
if (v !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
506
|
+
yield __privateGet(this, _valList)[i];
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Inverse order version of {@link LRUCache.values}
|
|
512
|
+
*
|
|
513
|
+
* Return a generator yielding the values in the cache,
|
|
514
|
+
* in order from least recently used to most recently used.
|
|
515
|
+
*/
|
|
516
|
+
*rvalues() {
|
|
517
|
+
for (const i of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this)) {
|
|
518
|
+
const v = __privateGet(this, _valList)[i];
|
|
519
|
+
if (v !== void 0 && !__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, __privateGet(this, _valList)[i])) {
|
|
520
|
+
yield __privateGet(this, _valList)[i];
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Iterating over the cache itself yields the same results as
|
|
526
|
+
* {@link LRUCache.entries}
|
|
527
|
+
*/
|
|
528
|
+
[(_c = Symbol.iterator, _b = Symbol.toStringTag, _c)]() {
|
|
529
|
+
return this.entries();
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Find a value for which the supplied fn method returns a truthy value,
|
|
533
|
+
* similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
|
|
534
|
+
*/
|
|
535
|
+
find(fn, getOptions = {}) {
|
|
536
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this)) {
|
|
537
|
+
const v = __privateGet(this, _valList)[i];
|
|
538
|
+
const value = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
539
|
+
if (value === void 0)
|
|
540
|
+
continue;
|
|
541
|
+
if (fn(value, __privateGet(this, _keyList)[i], this)) {
|
|
542
|
+
return this.get(__privateGet(this, _keyList)[i], getOptions);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Call the supplied function on each item in the cache, in order from most
|
|
548
|
+
* recently used to least recently used.
|
|
549
|
+
*
|
|
550
|
+
* `fn` is called as `fn(value, key, cache)`.
|
|
551
|
+
*
|
|
552
|
+
* If `thisp` is provided, function will be called in the `this`-context of
|
|
553
|
+
* the provided object, or the cache if no `thisp` object is provided.
|
|
554
|
+
*
|
|
555
|
+
* Does not update age or recenty of use, or iterate over stale values.
|
|
556
|
+
*/
|
|
557
|
+
forEach(fn, thisp = this) {
|
|
558
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this)) {
|
|
559
|
+
const v = __privateGet(this, _valList)[i];
|
|
560
|
+
const value = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
561
|
+
if (value === void 0)
|
|
562
|
+
continue;
|
|
563
|
+
fn.call(thisp, value, __privateGet(this, _keyList)[i], this);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* The same as {@link LRUCache.forEach} but items are iterated over in
|
|
568
|
+
* reverse order. (ie, less recently used items are iterated over first.)
|
|
569
|
+
*/
|
|
570
|
+
rforEach(fn, thisp = this) {
|
|
571
|
+
for (const i of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this)) {
|
|
572
|
+
const v = __privateGet(this, _valList)[i];
|
|
573
|
+
const value = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
574
|
+
if (value === void 0)
|
|
575
|
+
continue;
|
|
576
|
+
fn.call(thisp, value, __privateGet(this, _keyList)[i], this);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Delete any stale entries. Returns true if anything was removed,
|
|
581
|
+
* false otherwise.
|
|
582
|
+
*/
|
|
583
|
+
purgeStale() {
|
|
584
|
+
let deleted = false;
|
|
585
|
+
for (const i of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this, { allowStale: true })) {
|
|
586
|
+
if (__privateGet(this, _isStale).call(this, i)) {
|
|
587
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, __privateGet(this, _keyList)[i], "expire");
|
|
588
|
+
deleted = true;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return deleted;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Get the extended info about a given entry, to get its value, size, and
|
|
595
|
+
* TTL info simultaneously. Returns `undefined` if the key is not present.
|
|
596
|
+
*
|
|
597
|
+
* Unlike {@link LRUCache#dump}, which is designed to be portable and survive
|
|
598
|
+
* serialization, the `start` value is always the current timestamp, and the
|
|
599
|
+
* `ttl` is a calculated remaining time to live (negative if expired).
|
|
600
|
+
*
|
|
601
|
+
* Always returns stale values, if their info is found in the cache, so be
|
|
602
|
+
* sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
|
|
603
|
+
* if relevant.
|
|
604
|
+
*/
|
|
605
|
+
info(key) {
|
|
606
|
+
const i = __privateGet(this, _keyMap).get(key);
|
|
607
|
+
if (i === void 0)
|
|
608
|
+
return void 0;
|
|
609
|
+
const v = __privateGet(this, _valList)[i];
|
|
610
|
+
const value = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
611
|
+
if (value === void 0)
|
|
612
|
+
return void 0;
|
|
613
|
+
const entry = { value };
|
|
614
|
+
if (__privateGet(this, _ttls) && __privateGet(this, _starts)) {
|
|
615
|
+
const ttl = __privateGet(this, _ttls)[i];
|
|
616
|
+
const start = __privateGet(this, _starts)[i];
|
|
617
|
+
if (ttl && start) {
|
|
618
|
+
const remain = ttl - (perf.now() - start);
|
|
619
|
+
entry.ttl = remain;
|
|
620
|
+
entry.start = Date.now();
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (__privateGet(this, _sizes)) {
|
|
624
|
+
entry.size = __privateGet(this, _sizes)[i];
|
|
625
|
+
}
|
|
626
|
+
return entry;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Return an array of [key, {@link LRUCache.Entry}] tuples which can be
|
|
630
|
+
* passed to {@link LRLUCache#load}.
|
|
631
|
+
*
|
|
632
|
+
* The `start` fields are calculated relative to a portable `Date.now()`
|
|
633
|
+
* timestamp, even if `performance.now()` is available.
|
|
634
|
+
*
|
|
635
|
+
* Stale entries are always included in the `dump`, even if
|
|
636
|
+
* {@link LRUCache.OptionsBase.allowStale} is false.
|
|
637
|
+
*
|
|
638
|
+
* Note: this returns an actual array, not a generator, so it can be more
|
|
639
|
+
* easily passed around.
|
|
640
|
+
*/
|
|
641
|
+
dump() {
|
|
642
|
+
const arr = [];
|
|
643
|
+
for (const i of __privateMethod(this, _LRUCache_instances, indexes_fn).call(this, { allowStale: true })) {
|
|
644
|
+
const key = __privateGet(this, _keyList)[i];
|
|
645
|
+
const v = __privateGet(this, _valList)[i];
|
|
646
|
+
const value = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
647
|
+
if (value === void 0 || key === void 0)
|
|
648
|
+
continue;
|
|
649
|
+
const entry = { value };
|
|
650
|
+
if (__privateGet(this, _ttls) && __privateGet(this, _starts)) {
|
|
651
|
+
entry.ttl = __privateGet(this, _ttls)[i];
|
|
652
|
+
const age = perf.now() - __privateGet(this, _starts)[i];
|
|
653
|
+
entry.start = Math.floor(Date.now() - age);
|
|
654
|
+
}
|
|
655
|
+
if (__privateGet(this, _sizes)) {
|
|
656
|
+
entry.size = __privateGet(this, _sizes)[i];
|
|
657
|
+
}
|
|
658
|
+
arr.unshift([key, entry]);
|
|
659
|
+
}
|
|
660
|
+
return arr;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Reset the cache and load in the items in entries in the order listed.
|
|
664
|
+
*
|
|
665
|
+
* The shape of the resulting cache may be different if the same options are
|
|
666
|
+
* not used in both caches.
|
|
667
|
+
*
|
|
668
|
+
* The `start` fields are assumed to be calculated relative to a portable
|
|
669
|
+
* `Date.now()` timestamp, even if `performance.now()` is available.
|
|
670
|
+
*/
|
|
671
|
+
load(arr) {
|
|
672
|
+
this.clear();
|
|
673
|
+
for (const [key, entry] of arr) {
|
|
674
|
+
if (entry.start) {
|
|
675
|
+
const age = Date.now() - entry.start;
|
|
676
|
+
entry.start = perf.now() - age;
|
|
677
|
+
}
|
|
678
|
+
this.set(key, entry.value, entry);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Add a value to the cache.
|
|
683
|
+
*
|
|
684
|
+
* Note: if `undefined` is specified as a value, this is an alias for
|
|
685
|
+
* {@link LRUCache#delete}
|
|
686
|
+
*
|
|
687
|
+
* Fields on the {@link LRUCache.SetOptions} options param will override
|
|
688
|
+
* their corresponding values in the constructor options for the scope
|
|
689
|
+
* of this single `set()` operation.
|
|
690
|
+
*
|
|
691
|
+
* If `start` is provided, then that will set the effective start
|
|
692
|
+
* time for the TTL calculation. Note that this must be a previous
|
|
693
|
+
* value of `performance.now()` if supported, or a previous value of
|
|
694
|
+
* `Date.now()` if not.
|
|
695
|
+
*
|
|
696
|
+
* Options object may also include `size`, which will prevent
|
|
697
|
+
* calling the `sizeCalculation` function and just use the specified
|
|
698
|
+
* number if it is a positive integer, and `noDisposeOnSet` which
|
|
699
|
+
* will prevent calling a `dispose` function in the case of
|
|
700
|
+
* overwrites.
|
|
701
|
+
*
|
|
702
|
+
* If the `size` (or return value of `sizeCalculation`) for a given
|
|
703
|
+
* entry is greater than `maxEntrySize`, then the item will not be
|
|
704
|
+
* added to the cache.
|
|
705
|
+
*
|
|
706
|
+
* Will update the recency of the entry.
|
|
707
|
+
*
|
|
708
|
+
* If the value is `undefined`, then this is an alias for
|
|
709
|
+
* `cache.delete(key)`. `undefined` is never stored in the cache.
|
|
710
|
+
*/
|
|
711
|
+
set(k, v, setOptions = {}) {
|
|
712
|
+
var _a2, _b2, _c2, _d, _e;
|
|
713
|
+
if (v === void 0) {
|
|
714
|
+
this.delete(k);
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status } = setOptions;
|
|
718
|
+
let { noUpdateTTL = this.noUpdateTTL } = setOptions;
|
|
719
|
+
const size = __privateGet(this, _requireSize).call(this, k, v, setOptions.size || 0, sizeCalculation);
|
|
720
|
+
if (this.maxEntrySize && size > this.maxEntrySize) {
|
|
721
|
+
if (status) {
|
|
722
|
+
status.set = "miss";
|
|
723
|
+
status.maxEntrySizeExceeded = true;
|
|
724
|
+
}
|
|
725
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, k, "set");
|
|
726
|
+
return this;
|
|
727
|
+
}
|
|
728
|
+
let index = __privateGet(this, _size) === 0 ? void 0 : __privateGet(this, _keyMap).get(k);
|
|
729
|
+
if (index === void 0) {
|
|
730
|
+
index = __privateGet(this, _size) === 0 ? __privateGet(this, _tail) : __privateGet(this, _free).length !== 0 ? __privateGet(this, _free).pop() : __privateGet(this, _size) === __privateGet(this, _max) ? __privateMethod(this, _LRUCache_instances, evict_fn).call(this, false) : __privateGet(this, _size);
|
|
731
|
+
__privateGet(this, _keyList)[index] = k;
|
|
732
|
+
__privateGet(this, _valList)[index] = v;
|
|
733
|
+
__privateGet(this, _keyMap).set(k, index);
|
|
734
|
+
__privateGet(this, _next)[__privateGet(this, _tail)] = index;
|
|
735
|
+
__privateGet(this, _prev)[index] = __privateGet(this, _tail);
|
|
736
|
+
__privateSet(this, _tail, index);
|
|
737
|
+
__privateWrapper(this, _size)._++;
|
|
738
|
+
__privateGet(this, _addItemSize).call(this, index, size, status);
|
|
739
|
+
if (status)
|
|
740
|
+
status.set = "add";
|
|
741
|
+
noUpdateTTL = false;
|
|
742
|
+
} else {
|
|
743
|
+
__privateMethod(this, _LRUCache_instances, moveToTail_fn).call(this, index);
|
|
744
|
+
const oldVal = __privateGet(this, _valList)[index];
|
|
745
|
+
if (v !== oldVal) {
|
|
746
|
+
if (__privateGet(this, _hasFetchMethod) && __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, oldVal)) {
|
|
747
|
+
oldVal.__abortController.abort(new Error("replaced"));
|
|
748
|
+
const { __staleWhileFetching: s } = oldVal;
|
|
749
|
+
if (s !== void 0 && !noDisposeOnSet) {
|
|
750
|
+
if (__privateGet(this, _hasDispose)) {
|
|
751
|
+
(_a2 = __privateGet(this, _dispose)) == null ? void 0 : _a2.call(this, s, k, "set");
|
|
752
|
+
}
|
|
753
|
+
if (__privateGet(this, _hasDisposeAfter)) {
|
|
754
|
+
(_b2 = __privateGet(this, _disposed)) == null ? void 0 : _b2.push([s, k, "set"]);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
} else if (!noDisposeOnSet) {
|
|
758
|
+
if (__privateGet(this, _hasDispose)) {
|
|
759
|
+
(_c2 = __privateGet(this, _dispose)) == null ? void 0 : _c2.call(this, oldVal, k, "set");
|
|
760
|
+
}
|
|
761
|
+
if (__privateGet(this, _hasDisposeAfter)) {
|
|
762
|
+
(_d = __privateGet(this, _disposed)) == null ? void 0 : _d.push([oldVal, k, "set"]);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
__privateGet(this, _removeItemSize).call(this, index);
|
|
766
|
+
__privateGet(this, _addItemSize).call(this, index, size, status);
|
|
767
|
+
__privateGet(this, _valList)[index] = v;
|
|
768
|
+
if (status) {
|
|
769
|
+
status.set = "replace";
|
|
770
|
+
const oldValue = oldVal && __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, oldVal) ? oldVal.__staleWhileFetching : oldVal;
|
|
771
|
+
if (oldValue !== void 0)
|
|
772
|
+
status.oldValue = oldValue;
|
|
773
|
+
}
|
|
774
|
+
} else if (status) {
|
|
775
|
+
status.set = "update";
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (ttl !== 0 && !__privateGet(this, _ttls)) {
|
|
779
|
+
__privateMethod(this, _LRUCache_instances, initializeTTLTracking_fn).call(this);
|
|
780
|
+
}
|
|
781
|
+
if (__privateGet(this, _ttls)) {
|
|
782
|
+
if (!noUpdateTTL) {
|
|
783
|
+
__privateGet(this, _setItemTTL).call(this, index, ttl, start);
|
|
784
|
+
}
|
|
785
|
+
if (status)
|
|
786
|
+
__privateGet(this, _statusTTL).call(this, status, index);
|
|
787
|
+
}
|
|
788
|
+
if (!noDisposeOnSet && __privateGet(this, _hasDisposeAfter) && __privateGet(this, _disposed)) {
|
|
789
|
+
const dt = __privateGet(this, _disposed);
|
|
790
|
+
let task;
|
|
791
|
+
while (task = dt == null ? void 0 : dt.shift()) {
|
|
792
|
+
(_e = __privateGet(this, _disposeAfter)) == null ? void 0 : _e.call(this, ...task);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
return this;
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Evict the least recently used item, returning its value or
|
|
799
|
+
* `undefined` if cache is empty.
|
|
800
|
+
*/
|
|
801
|
+
pop() {
|
|
802
|
+
var _a2;
|
|
803
|
+
try {
|
|
804
|
+
while (__privateGet(this, _size)) {
|
|
805
|
+
const val = __privateGet(this, _valList)[__privateGet(this, _head)];
|
|
806
|
+
__privateMethod(this, _LRUCache_instances, evict_fn).call(this, true);
|
|
807
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, val)) {
|
|
808
|
+
if (val.__staleWhileFetching) {
|
|
809
|
+
return val.__staleWhileFetching;
|
|
810
|
+
}
|
|
811
|
+
} else if (val !== void 0) {
|
|
812
|
+
return val;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
} finally {
|
|
816
|
+
if (__privateGet(this, _hasDisposeAfter) && __privateGet(this, _disposed)) {
|
|
817
|
+
const dt = __privateGet(this, _disposed);
|
|
818
|
+
let task;
|
|
819
|
+
while (task = dt == null ? void 0 : dt.shift()) {
|
|
820
|
+
(_a2 = __privateGet(this, _disposeAfter)) == null ? void 0 : _a2.call(this, ...task);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Check if a key is in the cache, without updating the recency of use.
|
|
827
|
+
* Will return false if the item is stale, even though it is technically
|
|
828
|
+
* in the cache.
|
|
829
|
+
*
|
|
830
|
+
* Check if a key is in the cache, without updating the recency of
|
|
831
|
+
* use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
|
|
832
|
+
* to `true` in either the options or the constructor.
|
|
833
|
+
*
|
|
834
|
+
* Will return `false` if the item is stale, even though it is technically in
|
|
835
|
+
* the cache. The difference can be determined (if it matters) by using a
|
|
836
|
+
* `status` argument, and inspecting the `has` field.
|
|
837
|
+
*
|
|
838
|
+
* Will not update item age unless
|
|
839
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
|
|
840
|
+
*/
|
|
841
|
+
has(k, hasOptions = {}) {
|
|
842
|
+
const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
|
|
843
|
+
const index = __privateGet(this, _keyMap).get(k);
|
|
844
|
+
if (index !== void 0) {
|
|
845
|
+
const v = __privateGet(this, _valList)[index];
|
|
846
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) && v.__staleWhileFetching === void 0) {
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
if (!__privateGet(this, _isStale).call(this, index)) {
|
|
850
|
+
if (updateAgeOnHas) {
|
|
851
|
+
__privateGet(this, _updateItemAge).call(this, index);
|
|
852
|
+
}
|
|
853
|
+
if (status) {
|
|
854
|
+
status.has = "hit";
|
|
855
|
+
__privateGet(this, _statusTTL).call(this, status, index);
|
|
856
|
+
}
|
|
857
|
+
return true;
|
|
858
|
+
} else if (status) {
|
|
859
|
+
status.has = "stale";
|
|
860
|
+
__privateGet(this, _statusTTL).call(this, status, index);
|
|
861
|
+
}
|
|
862
|
+
} else if (status) {
|
|
863
|
+
status.has = "miss";
|
|
864
|
+
}
|
|
865
|
+
return false;
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Like {@link LRUCache#get} but doesn't update recency or delete stale
|
|
869
|
+
* items.
|
|
870
|
+
*
|
|
871
|
+
* Returns `undefined` if the item is stale, unless
|
|
872
|
+
* {@link LRUCache.OptionsBase.allowStale} is set.
|
|
873
|
+
*/
|
|
874
|
+
peek(k, peekOptions = {}) {
|
|
875
|
+
const { allowStale = this.allowStale } = peekOptions;
|
|
876
|
+
const index = __privateGet(this, _keyMap).get(k);
|
|
877
|
+
if (index === void 0 || !allowStale && __privateGet(this, _isStale).call(this, index)) {
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
const v = __privateGet(this, _valList)[index];
|
|
881
|
+
return __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v) ? v.__staleWhileFetching : v;
|
|
882
|
+
}
|
|
883
|
+
async fetch(k, fetchOptions = {}) {
|
|
884
|
+
const {
|
|
885
|
+
// get options
|
|
886
|
+
allowStale = this.allowStale,
|
|
887
|
+
updateAgeOnGet = this.updateAgeOnGet,
|
|
888
|
+
noDeleteOnStaleGet = this.noDeleteOnStaleGet,
|
|
889
|
+
// set options
|
|
890
|
+
ttl = this.ttl,
|
|
891
|
+
noDisposeOnSet = this.noDisposeOnSet,
|
|
892
|
+
size = 0,
|
|
893
|
+
sizeCalculation = this.sizeCalculation,
|
|
894
|
+
noUpdateTTL = this.noUpdateTTL,
|
|
895
|
+
// fetch exclusive options
|
|
896
|
+
noDeleteOnFetchRejection = this.noDeleteOnFetchRejection,
|
|
897
|
+
allowStaleOnFetchRejection = this.allowStaleOnFetchRejection,
|
|
898
|
+
ignoreFetchAbort = this.ignoreFetchAbort,
|
|
899
|
+
allowStaleOnFetchAbort = this.allowStaleOnFetchAbort,
|
|
900
|
+
context,
|
|
901
|
+
forceRefresh = false,
|
|
902
|
+
status,
|
|
903
|
+
signal
|
|
904
|
+
} = fetchOptions;
|
|
905
|
+
if (!__privateGet(this, _hasFetchMethod)) {
|
|
906
|
+
if (status)
|
|
907
|
+
status.fetch = "get";
|
|
908
|
+
return this.get(k, {
|
|
909
|
+
allowStale,
|
|
910
|
+
updateAgeOnGet,
|
|
911
|
+
noDeleteOnStaleGet,
|
|
912
|
+
status
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
const options = {
|
|
916
|
+
allowStale,
|
|
917
|
+
updateAgeOnGet,
|
|
918
|
+
noDeleteOnStaleGet,
|
|
919
|
+
ttl,
|
|
920
|
+
noDisposeOnSet,
|
|
921
|
+
size,
|
|
922
|
+
sizeCalculation,
|
|
923
|
+
noUpdateTTL,
|
|
924
|
+
noDeleteOnFetchRejection,
|
|
925
|
+
allowStaleOnFetchRejection,
|
|
926
|
+
allowStaleOnFetchAbort,
|
|
927
|
+
ignoreFetchAbort,
|
|
928
|
+
status,
|
|
929
|
+
signal
|
|
930
|
+
};
|
|
931
|
+
let index = __privateGet(this, _keyMap).get(k);
|
|
932
|
+
if (index === void 0) {
|
|
933
|
+
if (status)
|
|
934
|
+
status.fetch = "miss";
|
|
935
|
+
const p = __privateMethod(this, _LRUCache_instances, backgroundFetch_fn).call(this, k, index, options, context);
|
|
936
|
+
return p.__returned = p;
|
|
937
|
+
} else {
|
|
938
|
+
const v = __privateGet(this, _valList)[index];
|
|
939
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
940
|
+
const stale = allowStale && v.__staleWhileFetching !== void 0;
|
|
941
|
+
if (status) {
|
|
942
|
+
status.fetch = "inflight";
|
|
943
|
+
if (stale)
|
|
944
|
+
status.returnedStale = true;
|
|
945
|
+
}
|
|
946
|
+
return stale ? v.__staleWhileFetching : v.__returned = v;
|
|
947
|
+
}
|
|
948
|
+
const isStale = __privateGet(this, _isStale).call(this, index);
|
|
949
|
+
if (!forceRefresh && !isStale) {
|
|
950
|
+
if (status)
|
|
951
|
+
status.fetch = "hit";
|
|
952
|
+
__privateMethod(this, _LRUCache_instances, moveToTail_fn).call(this, index);
|
|
953
|
+
if (updateAgeOnGet) {
|
|
954
|
+
__privateGet(this, _updateItemAge).call(this, index);
|
|
955
|
+
}
|
|
956
|
+
if (status)
|
|
957
|
+
__privateGet(this, _statusTTL).call(this, status, index);
|
|
958
|
+
return v;
|
|
959
|
+
}
|
|
960
|
+
const p = __privateMethod(this, _LRUCache_instances, backgroundFetch_fn).call(this, k, index, options, context);
|
|
961
|
+
const hasStale = p.__staleWhileFetching !== void 0;
|
|
962
|
+
const staleVal = hasStale && allowStale;
|
|
963
|
+
if (status) {
|
|
964
|
+
status.fetch = isStale ? "stale" : "refresh";
|
|
965
|
+
if (staleVal && isStale)
|
|
966
|
+
status.returnedStale = true;
|
|
967
|
+
}
|
|
968
|
+
return staleVal ? p.__staleWhileFetching : p.__returned = p;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
async forceFetch(k, fetchOptions = {}) {
|
|
972
|
+
const v = await this.fetch(k, fetchOptions);
|
|
973
|
+
if (v === void 0)
|
|
974
|
+
throw new Error("fetch() returned undefined");
|
|
975
|
+
return v;
|
|
976
|
+
}
|
|
977
|
+
memo(k, memoOptions = {}) {
|
|
978
|
+
const memoMethod = __privateGet(this, _memoMethod);
|
|
979
|
+
if (!memoMethod) {
|
|
980
|
+
throw new Error("no memoMethod provided to constructor");
|
|
981
|
+
}
|
|
982
|
+
const { context, forceRefresh, ...options } = memoOptions;
|
|
983
|
+
const v = this.get(k, options);
|
|
984
|
+
if (!forceRefresh && v !== void 0)
|
|
985
|
+
return v;
|
|
986
|
+
const vv = memoMethod(k, v, {
|
|
987
|
+
options,
|
|
988
|
+
context
|
|
989
|
+
});
|
|
990
|
+
this.set(k, vv, options);
|
|
991
|
+
return vv;
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Return a value from the cache. Will update the recency of the cache
|
|
995
|
+
* entry found.
|
|
996
|
+
*
|
|
997
|
+
* If the key is not found, get() will return `undefined`.
|
|
998
|
+
*/
|
|
999
|
+
get(k, getOptions = {}) {
|
|
1000
|
+
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions;
|
|
1001
|
+
const index = __privateGet(this, _keyMap).get(k);
|
|
1002
|
+
if (index !== void 0) {
|
|
1003
|
+
const value = __privateGet(this, _valList)[index];
|
|
1004
|
+
const fetching = __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, value);
|
|
1005
|
+
if (status)
|
|
1006
|
+
__privateGet(this, _statusTTL).call(this, status, index);
|
|
1007
|
+
if (__privateGet(this, _isStale).call(this, index)) {
|
|
1008
|
+
if (status)
|
|
1009
|
+
status.get = "stale";
|
|
1010
|
+
if (!fetching) {
|
|
1011
|
+
if (!noDeleteOnStaleGet) {
|
|
1012
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, k, "expire");
|
|
1013
|
+
}
|
|
1014
|
+
if (status && allowStale)
|
|
1015
|
+
status.returnedStale = true;
|
|
1016
|
+
return allowStale ? value : void 0;
|
|
1017
|
+
} else {
|
|
1018
|
+
if (status && allowStale && value.__staleWhileFetching !== void 0) {
|
|
1019
|
+
status.returnedStale = true;
|
|
1020
|
+
}
|
|
1021
|
+
return allowStale ? value.__staleWhileFetching : void 0;
|
|
1022
|
+
}
|
|
1023
|
+
} else {
|
|
1024
|
+
if (status)
|
|
1025
|
+
status.get = "hit";
|
|
1026
|
+
if (fetching) {
|
|
1027
|
+
return value.__staleWhileFetching;
|
|
1028
|
+
}
|
|
1029
|
+
__privateMethod(this, _LRUCache_instances, moveToTail_fn).call(this, index);
|
|
1030
|
+
if (updateAgeOnGet) {
|
|
1031
|
+
__privateGet(this, _updateItemAge).call(this, index);
|
|
1032
|
+
}
|
|
1033
|
+
return value;
|
|
1034
|
+
}
|
|
1035
|
+
} else if (status) {
|
|
1036
|
+
status.get = "miss";
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Deletes a key out of the cache.
|
|
1041
|
+
*
|
|
1042
|
+
* Returns true if the key was deleted, false otherwise.
|
|
1043
|
+
*/
|
|
1044
|
+
delete(k) {
|
|
1045
|
+
return __privateMethod(this, _LRUCache_instances, delete_fn).call(this, k, "delete");
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Clear the cache entirely, throwing away all values.
|
|
1049
|
+
*/
|
|
1050
|
+
clear() {
|
|
1051
|
+
return __privateMethod(this, _LRUCache_instances, clear_fn).call(this, "delete");
|
|
1052
|
+
}
|
|
1053
|
+
};
|
|
1054
|
+
_max = new WeakMap();
|
|
1055
|
+
_maxSize = new WeakMap();
|
|
1056
|
+
_dispose = new WeakMap();
|
|
1057
|
+
_disposeAfter = new WeakMap();
|
|
1058
|
+
_fetchMethod = new WeakMap();
|
|
1059
|
+
_memoMethod = new WeakMap();
|
|
1060
|
+
_size = new WeakMap();
|
|
1061
|
+
_calculatedSize = new WeakMap();
|
|
1062
|
+
_keyMap = new WeakMap();
|
|
1063
|
+
_keyList = new WeakMap();
|
|
1064
|
+
_valList = new WeakMap();
|
|
1065
|
+
_next = new WeakMap();
|
|
1066
|
+
_prev = new WeakMap();
|
|
1067
|
+
_head = new WeakMap();
|
|
1068
|
+
_tail = new WeakMap();
|
|
1069
|
+
_free = new WeakMap();
|
|
1070
|
+
_disposed = new WeakMap();
|
|
1071
|
+
_sizes = new WeakMap();
|
|
1072
|
+
_starts = new WeakMap();
|
|
1073
|
+
_ttls = new WeakMap();
|
|
1074
|
+
_hasDispose = new WeakMap();
|
|
1075
|
+
_hasFetchMethod = new WeakMap();
|
|
1076
|
+
_hasDisposeAfter = new WeakMap();
|
|
1077
|
+
_LRUCache_instances = new WeakSet();
|
|
1078
|
+
initializeTTLTracking_fn = function() {
|
|
1079
|
+
const ttls = new ZeroArray(__privateGet(this, _max));
|
|
1080
|
+
const starts = new ZeroArray(__privateGet(this, _max));
|
|
1081
|
+
__privateSet(this, _ttls, ttls);
|
|
1082
|
+
__privateSet(this, _starts, starts);
|
|
1083
|
+
__privateSet(this, _setItemTTL, (index, ttl, start = perf.now()) => {
|
|
1084
|
+
starts[index] = ttl !== 0 ? start : 0;
|
|
1085
|
+
ttls[index] = ttl;
|
|
1086
|
+
if (ttl !== 0 && this.ttlAutopurge) {
|
|
1087
|
+
const t = setTimeout(() => {
|
|
1088
|
+
if (__privateGet(this, _isStale).call(this, index)) {
|
|
1089
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, __privateGet(this, _keyList)[index], "expire");
|
|
1090
|
+
}
|
|
1091
|
+
}, ttl + 1);
|
|
1092
|
+
if (t.unref) {
|
|
1093
|
+
t.unref();
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
__privateSet(this, _updateItemAge, (index) => {
|
|
1098
|
+
starts[index] = ttls[index] !== 0 ? perf.now() : 0;
|
|
1099
|
+
});
|
|
1100
|
+
__privateSet(this, _statusTTL, (status, index) => {
|
|
1101
|
+
if (ttls[index]) {
|
|
1102
|
+
const ttl = ttls[index];
|
|
1103
|
+
const start = starts[index];
|
|
1104
|
+
if (!ttl || !start)
|
|
1105
|
+
return;
|
|
1106
|
+
status.ttl = ttl;
|
|
1107
|
+
status.start = start;
|
|
1108
|
+
status.now = cachedNow || getNow();
|
|
1109
|
+
const age = status.now - start;
|
|
1110
|
+
status.remainingTTL = ttl - age;
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
let cachedNow = 0;
|
|
1114
|
+
const getNow = () => {
|
|
1115
|
+
const n = perf.now();
|
|
1116
|
+
if (this.ttlResolution > 0) {
|
|
1117
|
+
cachedNow = n;
|
|
1118
|
+
const t = setTimeout(() => cachedNow = 0, this.ttlResolution);
|
|
1119
|
+
if (t.unref) {
|
|
1120
|
+
t.unref();
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
return n;
|
|
1124
|
+
};
|
|
1125
|
+
this.getRemainingTTL = (key) => {
|
|
1126
|
+
const index = __privateGet(this, _keyMap).get(key);
|
|
1127
|
+
if (index === void 0) {
|
|
1128
|
+
return 0;
|
|
1129
|
+
}
|
|
1130
|
+
const ttl = ttls[index];
|
|
1131
|
+
const start = starts[index];
|
|
1132
|
+
if (!ttl || !start) {
|
|
1133
|
+
return Infinity;
|
|
1134
|
+
}
|
|
1135
|
+
const age = (cachedNow || getNow()) - start;
|
|
1136
|
+
return ttl - age;
|
|
1137
|
+
};
|
|
1138
|
+
__privateSet(this, _isStale, (index) => {
|
|
1139
|
+
const s = starts[index];
|
|
1140
|
+
const t = ttls[index];
|
|
1141
|
+
return !!t && !!s && (cachedNow || getNow()) - s > t;
|
|
1142
|
+
});
|
|
1143
|
+
};
|
|
1144
|
+
_updateItemAge = new WeakMap();
|
|
1145
|
+
_statusTTL = new WeakMap();
|
|
1146
|
+
_setItemTTL = new WeakMap();
|
|
1147
|
+
_isStale = new WeakMap();
|
|
1148
|
+
initializeSizeTracking_fn = function() {
|
|
1149
|
+
const sizes = new ZeroArray(__privateGet(this, _max));
|
|
1150
|
+
__privateSet(this, _calculatedSize, 0);
|
|
1151
|
+
__privateSet(this, _sizes, sizes);
|
|
1152
|
+
__privateSet(this, _removeItemSize, (index) => {
|
|
1153
|
+
__privateSet(this, _calculatedSize, __privateGet(this, _calculatedSize) - sizes[index]);
|
|
1154
|
+
sizes[index] = 0;
|
|
1155
|
+
});
|
|
1156
|
+
__privateSet(this, _requireSize, (k, v, size, sizeCalculation) => {
|
|
1157
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
1158
|
+
return 0;
|
|
1159
|
+
}
|
|
1160
|
+
if (!isPosInt(size)) {
|
|
1161
|
+
if (sizeCalculation) {
|
|
1162
|
+
if (typeof sizeCalculation !== "function") {
|
|
1163
|
+
throw new TypeError("sizeCalculation must be a function");
|
|
1164
|
+
}
|
|
1165
|
+
size = sizeCalculation(v, k);
|
|
1166
|
+
if (!isPosInt(size)) {
|
|
1167
|
+
throw new TypeError("sizeCalculation return invalid (expect positive integer)");
|
|
1168
|
+
}
|
|
1169
|
+
} else {
|
|
1170
|
+
throw new TypeError("invalid size value (must be positive integer). When maxSize or maxEntrySize is used, sizeCalculation or size must be set.");
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
return size;
|
|
1174
|
+
});
|
|
1175
|
+
__privateSet(this, _addItemSize, (index, size, status) => {
|
|
1176
|
+
sizes[index] = size;
|
|
1177
|
+
if (__privateGet(this, _maxSize)) {
|
|
1178
|
+
const maxSize = __privateGet(this, _maxSize) - sizes[index];
|
|
1179
|
+
while (__privateGet(this, _calculatedSize) > maxSize) {
|
|
1180
|
+
__privateMethod(this, _LRUCache_instances, evict_fn).call(this, true);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
__privateSet(this, _calculatedSize, __privateGet(this, _calculatedSize) + sizes[index]);
|
|
1184
|
+
if (status) {
|
|
1185
|
+
status.entrySize = size;
|
|
1186
|
+
status.totalCalculatedSize = __privateGet(this, _calculatedSize);
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
};
|
|
1190
|
+
_removeItemSize = new WeakMap();
|
|
1191
|
+
_addItemSize = new WeakMap();
|
|
1192
|
+
_requireSize = new WeakMap();
|
|
1193
|
+
indexes_fn = function* ({ allowStale = this.allowStale } = {}) {
|
|
1194
|
+
if (__privateGet(this, _size)) {
|
|
1195
|
+
for (let i = __privateGet(this, _tail); true; ) {
|
|
1196
|
+
if (!__privateMethod(this, _LRUCache_instances, isValidIndex_fn).call(this, i)) {
|
|
1197
|
+
break;
|
|
1198
|
+
}
|
|
1199
|
+
if (allowStale || !__privateGet(this, _isStale).call(this, i)) {
|
|
1200
|
+
yield i;
|
|
1201
|
+
}
|
|
1202
|
+
if (i === __privateGet(this, _head)) {
|
|
1203
|
+
break;
|
|
1204
|
+
} else {
|
|
1205
|
+
i = __privateGet(this, _prev)[i];
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
rindexes_fn = function* ({ allowStale = this.allowStale } = {}) {
|
|
1211
|
+
if (__privateGet(this, _size)) {
|
|
1212
|
+
for (let i = __privateGet(this, _head); true; ) {
|
|
1213
|
+
if (!__privateMethod(this, _LRUCache_instances, isValidIndex_fn).call(this, i)) {
|
|
1214
|
+
break;
|
|
1215
|
+
}
|
|
1216
|
+
if (allowStale || !__privateGet(this, _isStale).call(this, i)) {
|
|
1217
|
+
yield i;
|
|
1218
|
+
}
|
|
1219
|
+
if (i === __privateGet(this, _tail)) {
|
|
1220
|
+
break;
|
|
1221
|
+
} else {
|
|
1222
|
+
i = __privateGet(this, _next)[i];
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
};
|
|
1227
|
+
isValidIndex_fn = function(index) {
|
|
1228
|
+
return index !== void 0 && __privateGet(this, _keyMap).get(__privateGet(this, _keyList)[index]) === index;
|
|
1229
|
+
};
|
|
1230
|
+
evict_fn = function(free) {
|
|
1231
|
+
var _a2, _b2;
|
|
1232
|
+
const head = __privateGet(this, _head);
|
|
1233
|
+
const k = __privateGet(this, _keyList)[head];
|
|
1234
|
+
const v = __privateGet(this, _valList)[head];
|
|
1235
|
+
if (__privateGet(this, _hasFetchMethod) && __privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
1236
|
+
v.__abortController.abort(new Error("evicted"));
|
|
1237
|
+
} else if (__privateGet(this, _hasDispose) || __privateGet(this, _hasDisposeAfter)) {
|
|
1238
|
+
if (__privateGet(this, _hasDispose)) {
|
|
1239
|
+
(_a2 = __privateGet(this, _dispose)) == null ? void 0 : _a2.call(this, v, k, "evict");
|
|
1240
|
+
}
|
|
1241
|
+
if (__privateGet(this, _hasDisposeAfter)) {
|
|
1242
|
+
(_b2 = __privateGet(this, _disposed)) == null ? void 0 : _b2.push([v, k, "evict"]);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
__privateGet(this, _removeItemSize).call(this, head);
|
|
1246
|
+
if (free) {
|
|
1247
|
+
__privateGet(this, _keyList)[head] = void 0;
|
|
1248
|
+
__privateGet(this, _valList)[head] = void 0;
|
|
1249
|
+
__privateGet(this, _free).push(head);
|
|
1250
|
+
}
|
|
1251
|
+
if (__privateGet(this, _size) === 1) {
|
|
1252
|
+
__privateSet(this, _head, __privateSet(this, _tail, 0));
|
|
1253
|
+
__privateGet(this, _free).length = 0;
|
|
1254
|
+
} else {
|
|
1255
|
+
__privateSet(this, _head, __privateGet(this, _next)[head]);
|
|
1256
|
+
}
|
|
1257
|
+
__privateGet(this, _keyMap).delete(k);
|
|
1258
|
+
__privateWrapper(this, _size)._--;
|
|
1259
|
+
return head;
|
|
1260
|
+
};
|
|
1261
|
+
backgroundFetch_fn = function(k, index, options, context) {
|
|
1262
|
+
const v = index === void 0 ? void 0 : __privateGet(this, _valList)[index];
|
|
1263
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
1264
|
+
return v;
|
|
1265
|
+
}
|
|
1266
|
+
const ac = new AC();
|
|
1267
|
+
const { signal } = options;
|
|
1268
|
+
signal == null ? void 0 : signal.addEventListener("abort", () => ac.abort(signal.reason), {
|
|
1269
|
+
signal: ac.signal
|
|
1270
|
+
});
|
|
1271
|
+
const fetchOpts = {
|
|
1272
|
+
signal: ac.signal,
|
|
1273
|
+
options,
|
|
1274
|
+
context
|
|
1275
|
+
};
|
|
1276
|
+
const cb = (v2, updateCache = false) => {
|
|
1277
|
+
const { aborted } = ac.signal;
|
|
1278
|
+
const ignoreAbort = options.ignoreFetchAbort && v2 !== void 0;
|
|
1279
|
+
if (options.status) {
|
|
1280
|
+
if (aborted && !updateCache) {
|
|
1281
|
+
options.status.fetchAborted = true;
|
|
1282
|
+
options.status.fetchError = ac.signal.reason;
|
|
1283
|
+
if (ignoreAbort)
|
|
1284
|
+
options.status.fetchAbortIgnored = true;
|
|
1285
|
+
} else {
|
|
1286
|
+
options.status.fetchResolved = true;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
if (aborted && !ignoreAbort && !updateCache) {
|
|
1290
|
+
return fetchFail(ac.signal.reason);
|
|
1291
|
+
}
|
|
1292
|
+
const bf2 = p;
|
|
1293
|
+
if (__privateGet(this, _valList)[index] === p) {
|
|
1294
|
+
if (v2 === void 0) {
|
|
1295
|
+
if (bf2.__staleWhileFetching) {
|
|
1296
|
+
__privateGet(this, _valList)[index] = bf2.__staleWhileFetching;
|
|
1297
|
+
} else {
|
|
1298
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, k, "fetch");
|
|
1299
|
+
}
|
|
1300
|
+
} else {
|
|
1301
|
+
if (options.status)
|
|
1302
|
+
options.status.fetchUpdated = true;
|
|
1303
|
+
this.set(k, v2, fetchOpts.options);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
return v2;
|
|
1307
|
+
};
|
|
1308
|
+
const eb = (er) => {
|
|
1309
|
+
if (options.status) {
|
|
1310
|
+
options.status.fetchRejected = true;
|
|
1311
|
+
options.status.fetchError = er;
|
|
1312
|
+
}
|
|
1313
|
+
return fetchFail(er);
|
|
1314
|
+
};
|
|
1315
|
+
const fetchFail = (er) => {
|
|
1316
|
+
const { aborted } = ac.signal;
|
|
1317
|
+
const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
|
|
1318
|
+
const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
|
|
1319
|
+
const noDelete = allowStale || options.noDeleteOnFetchRejection;
|
|
1320
|
+
const bf2 = p;
|
|
1321
|
+
if (__privateGet(this, _valList)[index] === p) {
|
|
1322
|
+
const del = !noDelete || bf2.__staleWhileFetching === void 0;
|
|
1323
|
+
if (del) {
|
|
1324
|
+
__privateMethod(this, _LRUCache_instances, delete_fn).call(this, k, "fetch");
|
|
1325
|
+
} else if (!allowStaleAborted) {
|
|
1326
|
+
__privateGet(this, _valList)[index] = bf2.__staleWhileFetching;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
if (allowStale) {
|
|
1330
|
+
if (options.status && bf2.__staleWhileFetching !== void 0) {
|
|
1331
|
+
options.status.returnedStale = true;
|
|
1332
|
+
}
|
|
1333
|
+
return bf2.__staleWhileFetching;
|
|
1334
|
+
} else if (bf2.__returned === bf2) {
|
|
1335
|
+
throw er;
|
|
1336
|
+
}
|
|
1337
|
+
};
|
|
1338
|
+
const pcall = (res, rej) => {
|
|
1339
|
+
var _a2;
|
|
1340
|
+
const fmp = (_a2 = __privateGet(this, _fetchMethod)) == null ? void 0 : _a2.call(this, k, v, fetchOpts);
|
|
1341
|
+
if (fmp && fmp instanceof Promise) {
|
|
1342
|
+
fmp.then((v2) => res(v2 === void 0 ? void 0 : v2), rej);
|
|
1343
|
+
}
|
|
1344
|
+
ac.signal.addEventListener("abort", () => {
|
|
1345
|
+
if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
|
|
1346
|
+
res(void 0);
|
|
1347
|
+
if (options.allowStaleOnFetchAbort) {
|
|
1348
|
+
res = (v2) => cb(v2, true);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1352
|
+
};
|
|
1353
|
+
if (options.status)
|
|
1354
|
+
options.status.fetchDispatched = true;
|
|
1355
|
+
const p = new Promise(pcall).then(cb, eb);
|
|
1356
|
+
const bf = Object.assign(p, {
|
|
1357
|
+
__abortController: ac,
|
|
1358
|
+
__staleWhileFetching: v,
|
|
1359
|
+
__returned: void 0
|
|
1360
|
+
});
|
|
1361
|
+
if (index === void 0) {
|
|
1362
|
+
this.set(k, bf, { ...fetchOpts.options, status: void 0 });
|
|
1363
|
+
index = __privateGet(this, _keyMap).get(k);
|
|
1364
|
+
} else {
|
|
1365
|
+
__privateGet(this, _valList)[index] = bf;
|
|
1366
|
+
}
|
|
1367
|
+
return bf;
|
|
1368
|
+
};
|
|
1369
|
+
isBackgroundFetch_fn = function(p) {
|
|
1370
|
+
if (!__privateGet(this, _hasFetchMethod))
|
|
1371
|
+
return false;
|
|
1372
|
+
const b = p;
|
|
1373
|
+
return !!b && b instanceof Promise && b.hasOwnProperty("__staleWhileFetching") && b.__abortController instanceof AC;
|
|
1374
|
+
};
|
|
1375
|
+
connect_fn = function(p, n) {
|
|
1376
|
+
__privateGet(this, _prev)[n] = p;
|
|
1377
|
+
__privateGet(this, _next)[p] = n;
|
|
1378
|
+
};
|
|
1379
|
+
moveToTail_fn = function(index) {
|
|
1380
|
+
if (index !== __privateGet(this, _tail)) {
|
|
1381
|
+
if (index === __privateGet(this, _head)) {
|
|
1382
|
+
__privateSet(this, _head, __privateGet(this, _next)[index]);
|
|
1383
|
+
} else {
|
|
1384
|
+
__privateMethod(this, _LRUCache_instances, connect_fn).call(this, __privateGet(this, _prev)[index], __privateGet(this, _next)[index]);
|
|
1385
|
+
}
|
|
1386
|
+
__privateMethod(this, _LRUCache_instances, connect_fn).call(this, __privateGet(this, _tail), index);
|
|
1387
|
+
__privateSet(this, _tail, index);
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
delete_fn = function(k, reason) {
|
|
1391
|
+
var _a2, _b2, _c2, _d;
|
|
1392
|
+
let deleted = false;
|
|
1393
|
+
if (__privateGet(this, _size) !== 0) {
|
|
1394
|
+
const index = __privateGet(this, _keyMap).get(k);
|
|
1395
|
+
if (index !== void 0) {
|
|
1396
|
+
deleted = true;
|
|
1397
|
+
if (__privateGet(this, _size) === 1) {
|
|
1398
|
+
__privateMethod(this, _LRUCache_instances, clear_fn).call(this, reason);
|
|
1399
|
+
} else {
|
|
1400
|
+
__privateGet(this, _removeItemSize).call(this, index);
|
|
1401
|
+
const v = __privateGet(this, _valList)[index];
|
|
1402
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
1403
|
+
v.__abortController.abort(new Error("deleted"));
|
|
1404
|
+
} else if (__privateGet(this, _hasDispose) || __privateGet(this, _hasDisposeAfter)) {
|
|
1405
|
+
if (__privateGet(this, _hasDispose)) {
|
|
1406
|
+
(_a2 = __privateGet(this, _dispose)) == null ? void 0 : _a2.call(this, v, k, reason);
|
|
1407
|
+
}
|
|
1408
|
+
if (__privateGet(this, _hasDisposeAfter)) {
|
|
1409
|
+
(_b2 = __privateGet(this, _disposed)) == null ? void 0 : _b2.push([v, k, reason]);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
__privateGet(this, _keyMap).delete(k);
|
|
1413
|
+
__privateGet(this, _keyList)[index] = void 0;
|
|
1414
|
+
__privateGet(this, _valList)[index] = void 0;
|
|
1415
|
+
if (index === __privateGet(this, _tail)) {
|
|
1416
|
+
__privateSet(this, _tail, __privateGet(this, _prev)[index]);
|
|
1417
|
+
} else if (index === __privateGet(this, _head)) {
|
|
1418
|
+
__privateSet(this, _head, __privateGet(this, _next)[index]);
|
|
1419
|
+
} else {
|
|
1420
|
+
const pi = __privateGet(this, _prev)[index];
|
|
1421
|
+
__privateGet(this, _next)[pi] = __privateGet(this, _next)[index];
|
|
1422
|
+
const ni = __privateGet(this, _next)[index];
|
|
1423
|
+
__privateGet(this, _prev)[ni] = __privateGet(this, _prev)[index];
|
|
1424
|
+
}
|
|
1425
|
+
__privateWrapper(this, _size)._--;
|
|
1426
|
+
__privateGet(this, _free).push(index);
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
if (__privateGet(this, _hasDisposeAfter) && ((_c2 = __privateGet(this, _disposed)) == null ? void 0 : _c2.length)) {
|
|
1431
|
+
const dt = __privateGet(this, _disposed);
|
|
1432
|
+
let task;
|
|
1433
|
+
while (task = dt == null ? void 0 : dt.shift()) {
|
|
1434
|
+
(_d = __privateGet(this, _disposeAfter)) == null ? void 0 : _d.call(this, ...task);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return deleted;
|
|
1438
|
+
};
|
|
1439
|
+
clear_fn = function(reason) {
|
|
1440
|
+
var _a2, _b2, _c2;
|
|
1441
|
+
for (const index of __privateMethod(this, _LRUCache_instances, rindexes_fn).call(this, { allowStale: true })) {
|
|
1442
|
+
const v = __privateGet(this, _valList)[index];
|
|
1443
|
+
if (__privateMethod(this, _LRUCache_instances, isBackgroundFetch_fn).call(this, v)) {
|
|
1444
|
+
v.__abortController.abort(new Error("deleted"));
|
|
1445
|
+
} else {
|
|
1446
|
+
const k = __privateGet(this, _keyList)[index];
|
|
1447
|
+
if (__privateGet(this, _hasDispose)) {
|
|
1448
|
+
(_a2 = __privateGet(this, _dispose)) == null ? void 0 : _a2.call(this, v, k, reason);
|
|
1449
|
+
}
|
|
1450
|
+
if (__privateGet(this, _hasDisposeAfter)) {
|
|
1451
|
+
(_b2 = __privateGet(this, _disposed)) == null ? void 0 : _b2.push([v, k, reason]);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
__privateGet(this, _keyMap).clear();
|
|
1456
|
+
__privateGet(this, _valList).fill(void 0);
|
|
1457
|
+
__privateGet(this, _keyList).fill(void 0);
|
|
1458
|
+
if (__privateGet(this, _ttls) && __privateGet(this, _starts)) {
|
|
1459
|
+
__privateGet(this, _ttls).fill(0);
|
|
1460
|
+
__privateGet(this, _starts).fill(0);
|
|
1461
|
+
}
|
|
1462
|
+
if (__privateGet(this, _sizes)) {
|
|
1463
|
+
__privateGet(this, _sizes).fill(0);
|
|
1464
|
+
}
|
|
1465
|
+
__privateSet(this, _head, 0);
|
|
1466
|
+
__privateSet(this, _tail, 0);
|
|
1467
|
+
__privateGet(this, _free).length = 0;
|
|
1468
|
+
__privateSet(this, _calculatedSize, 0);
|
|
1469
|
+
__privateSet(this, _size, 0);
|
|
1470
|
+
if (__privateGet(this, _hasDisposeAfter) && __privateGet(this, _disposed)) {
|
|
1471
|
+
const dt = __privateGet(this, _disposed);
|
|
1472
|
+
let task;
|
|
1473
|
+
while (task = dt == null ? void 0 : dt.shift()) {
|
|
1474
|
+
(_c2 = __privateGet(this, _disposeAfter)) == null ? void 0 : _c2.call(this, ...task);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
};
|
|
1478
|
+
let LRUCache = _LRUCache;
|
|
1479
|
+
const CacheSize = {
|
|
1480
|
+
KB: 1024,
|
|
1481
|
+
MB: 1024 * 1024,
|
|
1482
|
+
GB: 1024 * 1024 * 1024
|
|
1483
|
+
};
|
|
1484
|
+
const CacheTime = {
|
|
1485
|
+
SECOND: 1e3,
|
|
1486
|
+
MINUTE: 60 * 1e3,
|
|
1487
|
+
HOUR: 60 * 60 * 1e3,
|
|
1488
|
+
DAY: 24 * 60 * 60 * 1e3,
|
|
1489
|
+
WEEK: 7 * 24 * 60 * 60 * 1e3,
|
|
1490
|
+
MONTH: 30 * 24 * 60 * 60 * 1e3
|
|
1491
|
+
};
|
|
1492
|
+
function getTagKeyMap() {
|
|
1493
|
+
const dataFetchCache = getDataFetchCache();
|
|
1494
|
+
if (!dataFetchCache || !dataFetchCache.tagKeyMap) {
|
|
1495
|
+
const tagKeyMap = /* @__PURE__ */ new Map();
|
|
1496
|
+
globalThis.__MF_DATA_FETCH_CACHE__ || (globalThis.__MF_DATA_FETCH_CACHE__ = {});
|
|
1497
|
+
globalThis.__MF_DATA_FETCH_CACHE__.tagKeyMap = tagKeyMap;
|
|
1498
|
+
return tagKeyMap;
|
|
1499
|
+
}
|
|
1500
|
+
return dataFetchCache.tagKeyMap;
|
|
1501
|
+
}
|
|
1502
|
+
function addTagKeyRelation(tag, key) {
|
|
1503
|
+
const tagKeyMap = getTagKeyMap();
|
|
1504
|
+
let keys = tagKeyMap.get(tag);
|
|
1505
|
+
if (!keys) {
|
|
1506
|
+
keys = /* @__PURE__ */ new Set();
|
|
1507
|
+
tagKeyMap.set(tag, keys);
|
|
1508
|
+
}
|
|
1509
|
+
keys.add(key);
|
|
1510
|
+
}
|
|
1511
|
+
function getCacheConfig() {
|
|
1512
|
+
const dataFetchCache = getDataFetchCache();
|
|
1513
|
+
if (!dataFetchCache || !dataFetchCache.cacheConfig) {
|
|
1514
|
+
const cacheConfig = {
|
|
1515
|
+
maxSize: CacheSize.GB
|
|
1516
|
+
};
|
|
1517
|
+
globalThis.__MF_DATA_FETCH_CACHE__ || (globalThis.__MF_DATA_FETCH_CACHE__ = {});
|
|
1518
|
+
globalThis.__MF_DATA_FETCH_CACHE__.cacheConfig = cacheConfig;
|
|
1519
|
+
return cacheConfig;
|
|
1520
|
+
}
|
|
1521
|
+
return dataFetchCache.cacheConfig;
|
|
1522
|
+
}
|
|
1523
|
+
function configureCache(config) {
|
|
1524
|
+
const cacheConfig = getCacheConfig();
|
|
1525
|
+
Object.assign(cacheConfig, config);
|
|
1526
|
+
}
|
|
1527
|
+
function getLRUCache() {
|
|
1528
|
+
const dataFetchCache = getDataFetchCache();
|
|
1529
|
+
const cacheConfig = getCacheConfig();
|
|
1530
|
+
if (!dataFetchCache || !dataFetchCache.cacheStore) {
|
|
1531
|
+
const cacheStore = new LRUCache({
|
|
1532
|
+
maxSize: cacheConfig.maxSize ?? CacheSize.GB,
|
|
1533
|
+
sizeCalculation: (value) => {
|
|
1534
|
+
if (!value.size) {
|
|
1535
|
+
return 1;
|
|
1536
|
+
}
|
|
1537
|
+
let size = 0;
|
|
1538
|
+
for (const [k, item] of value.entries()) {
|
|
1539
|
+
size += k.length * 2;
|
|
1540
|
+
size += estimateObjectSize(item.data);
|
|
1541
|
+
size += 8;
|
|
1542
|
+
}
|
|
1543
|
+
return size;
|
|
1544
|
+
},
|
|
1545
|
+
updateAgeOnGet: true,
|
|
1546
|
+
updateAgeOnHas: true
|
|
1547
|
+
});
|
|
1548
|
+
globalThis.__MF_DATA_FETCH_CACHE__ || (globalThis.__MF_DATA_FETCH_CACHE__ = {});
|
|
1549
|
+
globalThis.__MF_DATA_FETCH_CACHE__.cacheStore = cacheStore;
|
|
1550
|
+
return cacheStore;
|
|
1551
|
+
}
|
|
1552
|
+
return dataFetchCache.cacheStore;
|
|
1553
|
+
}
|
|
1554
|
+
function estimateObjectSize(data) {
|
|
1555
|
+
const type = typeof data;
|
|
1556
|
+
if (type === "number") return 8;
|
|
1557
|
+
if (type === "boolean") return 4;
|
|
1558
|
+
if (type === "string") return Math.max(data.length * 2, 1);
|
|
1559
|
+
if (data === null || data === void 0) return 1;
|
|
1560
|
+
if (ArrayBuffer.isView(data)) {
|
|
1561
|
+
return Math.max(data.byteLength, 1);
|
|
1562
|
+
}
|
|
1563
|
+
if (Array.isArray(data)) {
|
|
1564
|
+
return Math.max(
|
|
1565
|
+
data.reduce((acc, item) => acc + estimateObjectSize(item), 0),
|
|
1566
|
+
1
|
|
1567
|
+
);
|
|
1568
|
+
}
|
|
1569
|
+
if (data instanceof Map || data instanceof Set) {
|
|
1570
|
+
return 1024;
|
|
1571
|
+
}
|
|
1572
|
+
if (data instanceof Date) {
|
|
1573
|
+
return 8;
|
|
1574
|
+
}
|
|
1575
|
+
if (type === "object") {
|
|
1576
|
+
return Math.max(
|
|
1577
|
+
Object.entries(data).reduce(
|
|
1578
|
+
(acc, [key, value]) => acc + key.length * 2 + estimateObjectSize(value),
|
|
1579
|
+
0
|
|
1580
|
+
),
|
|
1581
|
+
1
|
|
1582
|
+
);
|
|
1583
|
+
}
|
|
1584
|
+
return 1;
|
|
1585
|
+
}
|
|
1586
|
+
function generateKey(dataFetchOptions) {
|
|
1587
|
+
return JSON.stringify(dataFetchOptions, (_, value) => {
|
|
1588
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
1589
|
+
return Object.keys(value).sort().reduce((result, key) => {
|
|
1590
|
+
result[key] = value[key];
|
|
1591
|
+
return result;
|
|
1592
|
+
}, {});
|
|
1593
|
+
}
|
|
1594
|
+
return value;
|
|
1595
|
+
});
|
|
1596
|
+
}
|
|
1597
|
+
function cache(fn, options) {
|
|
1598
|
+
const {
|
|
1599
|
+
tag = "default",
|
|
1600
|
+
maxAge = CacheTime.MINUTE * 5,
|
|
1601
|
+
revalidate = 0,
|
|
1602
|
+
onCache,
|
|
1603
|
+
getKey
|
|
1604
|
+
} = options || {};
|
|
1605
|
+
const tags = Array.isArray(tag) ? tag : [tag];
|
|
1606
|
+
return async (dataFetchOptions) => {
|
|
1607
|
+
if (dataFetchOptions.isDowngrade || !dataFetchOptions._id) {
|
|
1608
|
+
return fn(dataFetchOptions);
|
|
1609
|
+
}
|
|
1610
|
+
const store = getLRUCache();
|
|
1611
|
+
const now = Date.now();
|
|
1612
|
+
const storeKey = dataFetchOptions._id;
|
|
1613
|
+
const cacheKey = getKey ? getKey(dataFetchOptions) : generateKey(dataFetchOptions);
|
|
1614
|
+
tags.forEach((t) => addTagKeyRelation(t, cacheKey));
|
|
1615
|
+
let cacheStore = store.get(cacheKey);
|
|
1616
|
+
if (!cacheStore) {
|
|
1617
|
+
cacheStore = /* @__PURE__ */ new Map();
|
|
1618
|
+
}
|
|
1619
|
+
const cached = cacheStore.get(storeKey);
|
|
1620
|
+
if (cached) {
|
|
1621
|
+
const age = now - cached.timestamp;
|
|
1622
|
+
if (age < maxAge) {
|
|
1623
|
+
if (onCache) {
|
|
1624
|
+
const useCache = onCache({
|
|
1625
|
+
status: "hit",
|
|
1626
|
+
key: cacheKey,
|
|
1627
|
+
params: dataFetchOptions,
|
|
1628
|
+
result: cached.data
|
|
1629
|
+
});
|
|
1630
|
+
if (!useCache) {
|
|
1631
|
+
return fn(dataFetchOptions);
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return cached.data;
|
|
1635
|
+
}
|
|
1636
|
+
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
1637
|
+
if (onCache) {
|
|
1638
|
+
onCache({
|
|
1639
|
+
status: "stale",
|
|
1640
|
+
key: cacheKey,
|
|
1641
|
+
params: dataFetchOptions,
|
|
1642
|
+
result: cached.data
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
if (!cached.isRevalidating) {
|
|
1646
|
+
cached.isRevalidating = true;
|
|
1647
|
+
Promise.resolve().then(async () => {
|
|
1648
|
+
try {
|
|
1649
|
+
const newData = await fn(dataFetchOptions);
|
|
1650
|
+
cacheStore.set(storeKey, {
|
|
1651
|
+
data: newData,
|
|
1652
|
+
timestamp: Date.now(),
|
|
1653
|
+
isRevalidating: false
|
|
1654
|
+
});
|
|
1655
|
+
store.set(cacheKey, cacheStore);
|
|
1656
|
+
} catch (error) {
|
|
1657
|
+
cached.isRevalidating = false;
|
|
1658
|
+
console.error("Background revalidation failed:", error);
|
|
1659
|
+
}
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
return cached.data;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
const data = await fn(dataFetchOptions);
|
|
1666
|
+
cacheStore.set(storeKey, {
|
|
1667
|
+
data,
|
|
1668
|
+
timestamp: now,
|
|
1669
|
+
isRevalidating: false
|
|
1670
|
+
});
|
|
1671
|
+
store.set(cacheKey, cacheStore);
|
|
1672
|
+
if (onCache) {
|
|
1673
|
+
onCache({
|
|
1674
|
+
status: "miss",
|
|
1675
|
+
key: cacheKey,
|
|
1676
|
+
params: dataFetchOptions,
|
|
1677
|
+
result: data
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
return data;
|
|
1681
|
+
};
|
|
1682
|
+
}
|
|
1683
|
+
function revalidateTag(tag) {
|
|
1684
|
+
const tagKeyMap = getTagKeyMap();
|
|
1685
|
+
const keys = tagKeyMap.get(tag);
|
|
1686
|
+
const lruCache = getLRUCache();
|
|
1687
|
+
if (keys) {
|
|
1688
|
+
keys.forEach((key) => {
|
|
1689
|
+
lruCache == null ? void 0 : lruCache.delete(key);
|
|
1690
|
+
});
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
function clearStore() {
|
|
1694
|
+
var _a2;
|
|
1695
|
+
const lruCache = getLRUCache();
|
|
1696
|
+
const tagKeyMap = getTagKeyMap();
|
|
1697
|
+
lruCache == null ? void 0 : lruCache.clear();
|
|
1698
|
+
(_a2 = globalThis.__MF_DATA_FETCH_CACHE__) == null ? true : delete _a2.cacheStore;
|
|
1699
|
+
tagKeyMap.clear();
|
|
1700
|
+
}
|
|
1701
|
+
const getDataFetchInfo = ({
|
|
1702
|
+
name,
|
|
1703
|
+
alias,
|
|
1704
|
+
id,
|
|
1705
|
+
remoteSnapshot
|
|
1706
|
+
}) => {
|
|
1707
|
+
if (!remoteSnapshot) {
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1710
|
+
if (!("modules" in remoteSnapshot)) {
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
const regex = new RegExp(`^${name}(/[^/].*|)$`);
|
|
1714
|
+
const nameOrAlias = regex.test(id) ? name : alias || name;
|
|
1715
|
+
const expose = id.replace(nameOrAlias, "");
|
|
1716
|
+
let dataFetchName = "";
|
|
1717
|
+
let dataFetchId = "";
|
|
1718
|
+
let dataFetchKey = "";
|
|
1719
|
+
if (expose.startsWith("/")) {
|
|
1720
|
+
dataFetchName = `${expose.slice(1)}.${DATA_FETCH_IDENTIFIER}`;
|
|
1721
|
+
dataFetchId = `${id}.${DATA_FETCH_IDENTIFIER}`;
|
|
1722
|
+
dataFetchKey = `${name}${expose}.${DATA_FETCH_IDENTIFIER}`;
|
|
1723
|
+
} else if (expose === "") {
|
|
1724
|
+
dataFetchName = DATA_FETCH_IDENTIFIER;
|
|
1725
|
+
dataFetchId = `${id}/${DATA_FETCH_IDENTIFIER}`;
|
|
1726
|
+
dataFetchKey = `${name}/${DATA_FETCH_IDENTIFIER}`;
|
|
1727
|
+
} else {
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
if (!dataFetchName || !dataFetchId || !dataFetchKey) {
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
if (!remoteSnapshot.modules.find(
|
|
1734
|
+
(module2) => module2.moduleName === dataFetchName
|
|
1735
|
+
)) {
|
|
1736
|
+
return;
|
|
1737
|
+
}
|
|
1738
|
+
return {
|
|
1739
|
+
dataFetchName,
|
|
1740
|
+
dataFetchId,
|
|
1741
|
+
dataFetchKey
|
|
1742
|
+
};
|
|
1743
|
+
};
|
|
1744
|
+
function initDataFetchMap() {
|
|
1745
|
+
globalThis.__MF_DATA_FETCH_MAP__ || (globalThis.__MF_DATA_FETCH_MAP__ = {});
|
|
1746
|
+
}
|
|
1747
|
+
function getDataFetchItem(id) {
|
|
1748
|
+
var _a2;
|
|
1749
|
+
return (_a2 = globalThis.__MF_DATA_FETCH_MAP__) == null ? void 0 : _a2[id];
|
|
1750
|
+
}
|
|
1751
|
+
function getDataFetchMap() {
|
|
1752
|
+
return globalThis.__MF_DATA_FETCH_MAP__;
|
|
1753
|
+
}
|
|
1754
|
+
function getDataFetchCache() {
|
|
1755
|
+
return globalThis.__MF_DATA_FETCH_CACHE__;
|
|
1756
|
+
}
|
|
1757
|
+
const flushDataFetch = () => {
|
|
1758
|
+
globalThis.__MF_DATA_FETCH_MAP__ = {};
|
|
1759
|
+
globalThis[DOWNGRADE_KEY] = void 0;
|
|
1760
|
+
clearStore();
|
|
1761
|
+
};
|
|
1762
|
+
function setDataFetchItemLoadedStatus(id) {
|
|
1763
|
+
const dataFetchItem = getDataFetchItem(id);
|
|
1764
|
+
if (!dataFetchItem) {
|
|
1765
|
+
return;
|
|
1766
|
+
}
|
|
1767
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
1768
|
+
}
|
|
1769
|
+
const wrapDataFetchId = (id) => {
|
|
1770
|
+
return `${WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${WRAP_DATA_FETCH_ID_IDENTIFIER}`;
|
|
1771
|
+
};
|
|
1772
|
+
const getDataFetchIdWithErrorMsgs = (errMsgs) => {
|
|
1773
|
+
const firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
|
|
1774
|
+
if (firstIdentifierIndex === -1) {
|
|
1775
|
+
return void 0;
|
|
1776
|
+
}
|
|
1777
|
+
const secondIdentifierIndex = errMsgs.indexOf(
|
|
1778
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER,
|
|
1779
|
+
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length
|
|
1780
|
+
);
|
|
1781
|
+
if (secondIdentifierIndex === -1) {
|
|
1782
|
+
return void 0;
|
|
1783
|
+
}
|
|
1784
|
+
return errMsgs.substring(
|
|
1785
|
+
firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length,
|
|
1786
|
+
secondIdentifierIndex
|
|
1787
|
+
);
|
|
1788
|
+
};
|
|
1789
|
+
async function fetchData(id, params, remoteInfo) {
|
|
1790
|
+
var _a2;
|
|
1791
|
+
const callFetchData = async () => {
|
|
1792
|
+
const item = getDataFetchItem(id);
|
|
1793
|
+
if (!item) {
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
const [fetchDataFnArr, ..._rest] = item;
|
|
1797
|
+
const fetchDataFn = await fetchDataFnArr[2];
|
|
1798
|
+
if (!fetchDataFn) {
|
|
1799
|
+
return;
|
|
1800
|
+
}
|
|
1801
|
+
return fetchDataFn({
|
|
1802
|
+
...params,
|
|
1803
|
+
_id: id
|
|
1804
|
+
});
|
|
1805
|
+
};
|
|
1806
|
+
if (index_esm.isBrowserEnv()) {
|
|
1807
|
+
const dataFetchItem = getDataFetchItem(id);
|
|
1808
|
+
if (!dataFetchItem) {
|
|
1809
|
+
throw new Error(`dataFetchItem not found, id: ${id}`);
|
|
1810
|
+
}
|
|
1811
|
+
if ((_a2 = dataFetchItem[1]) == null ? void 0 : _a2[0]) {
|
|
1812
|
+
return dataFetchItem[1][0];
|
|
1813
|
+
}
|
|
1814
|
+
if (isCSROnly()) {
|
|
1815
|
+
logger.debug("==========csr only!");
|
|
1816
|
+
return callFetchData();
|
|
1817
|
+
}
|
|
1818
|
+
if (remoteInfo) {
|
|
1819
|
+
return callDowngrade(id, params, remoteInfo);
|
|
1820
|
+
}
|
|
1821
|
+
const mfDowngrade = getDowngradeTag();
|
|
1822
|
+
if (mfDowngrade && (typeof mfDowngrade === "boolean" || mfDowngrade.includes(id))) {
|
|
1823
|
+
return callDowngrade(id, { ...params, isDowngrade: true });
|
|
1824
|
+
}
|
|
1825
|
+
let res;
|
|
1826
|
+
let rej;
|
|
1827
|
+
const p = new Promise((resolve, reject) => {
|
|
1828
|
+
res = resolve;
|
|
1829
|
+
rej = reject;
|
|
1830
|
+
});
|
|
1831
|
+
dataFetchItem[1] = [p, res, rej];
|
|
1832
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
|
|
1833
|
+
return dataFetchItem[1][0];
|
|
1834
|
+
}
|
|
1835
|
+
return callFetchData();
|
|
1836
|
+
}
|
|
1837
|
+
function getDataFetchMapKey(dataFetchInfo, hostInfo) {
|
|
1838
|
+
if (!dataFetchInfo || !hostInfo) {
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
const { dataFetchKey } = dataFetchInfo;
|
|
1842
|
+
return index_esm.composeKeyWithSeparator(dataFetchKey, hostInfo.name, hostInfo.version);
|
|
1843
|
+
}
|
|
1844
|
+
async function loadDataFetchModule(instance, id) {
|
|
1845
|
+
return instance.loadRemote(id).then((m) => {
|
|
1846
|
+
if (m && typeof m === "object" && "fetchData" in m && typeof m.fetchData === "function") {
|
|
1847
|
+
return m.fetchData;
|
|
1848
|
+
}
|
|
1849
|
+
throw new Error(
|
|
1850
|
+
`fetchData not found in remote ${id}, ${JSON.stringify(m)}`
|
|
1851
|
+
);
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
function isDataLoaderExpose(exposeKey) {
|
|
1855
|
+
return exposeKey.endsWith(DATA_FETCH_IDENTIFIER) || exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX);
|
|
1856
|
+
}
|
|
1857
|
+
function getDowngradeTag() {
|
|
1858
|
+
return globalThis[DOWNGRADE_KEY];
|
|
1859
|
+
}
|
|
1860
|
+
function callAllDowngrade() {
|
|
1861
|
+
const dataFetchMap = getDataFetchMap();
|
|
1862
|
+
if (!dataFetchMap) {
|
|
1863
|
+
return;
|
|
1864
|
+
}
|
|
1865
|
+
Object.keys(dataFetchMap).forEach((key) => {
|
|
1866
|
+
callDowngrade(key);
|
|
1867
|
+
});
|
|
1868
|
+
}
|
|
1869
|
+
async function callDowngrade(id, params, remoteInfo) {
|
|
1870
|
+
const dataFetchMap = getDataFetchMap();
|
|
1871
|
+
if (!dataFetchMap) {
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1874
|
+
const mfDataFetch = dataFetchMap[id];
|
|
1875
|
+
if ((mfDataFetch == null ? void 0 : mfDataFetch[2]) === MF_DATA_FETCH_STATUS.AWAIT) {
|
|
1876
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
|
|
1877
|
+
let promise;
|
|
1878
|
+
let res;
|
|
1879
|
+
let rej;
|
|
1880
|
+
if (mfDataFetch[1]) {
|
|
1881
|
+
promise = mfDataFetch[1][0];
|
|
1882
|
+
res = mfDataFetch[1][1];
|
|
1883
|
+
rej = mfDataFetch[1][2];
|
|
1884
|
+
} else {
|
|
1885
|
+
promise = new Promise((resolve, reject) => {
|
|
1886
|
+
res = resolve;
|
|
1887
|
+
rej = reject;
|
|
1888
|
+
});
|
|
1889
|
+
mfDataFetch[1] = [promise, res, rej];
|
|
1890
|
+
}
|
|
1891
|
+
const dataFetchType = mfDataFetch[0][1];
|
|
1892
|
+
if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT) {
|
|
1893
|
+
try {
|
|
1894
|
+
mfDataFetch[0][0]().then(async (getDataFetchFn) => {
|
|
1895
|
+
return getDataFetchFn({
|
|
1896
|
+
...params,
|
|
1897
|
+
isDowngrade: true,
|
|
1898
|
+
_id: id
|
|
1899
|
+
}).then((data) => {
|
|
1900
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
1901
|
+
res && res(data);
|
|
1902
|
+
});
|
|
1903
|
+
});
|
|
1904
|
+
} catch (e) {
|
|
1905
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
1906
|
+
rej && rej(e);
|
|
1907
|
+
}
|
|
1908
|
+
} else if (dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER) {
|
|
1909
|
+
try {
|
|
1910
|
+
const currentUrl = new URL(window.location.href);
|
|
1911
|
+
currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
|
|
1912
|
+
if (params) {
|
|
1913
|
+
currentUrl.searchParams.set(
|
|
1914
|
+
"params",
|
|
1915
|
+
encodeURIComponent(JSON.stringify(params))
|
|
1916
|
+
);
|
|
1917
|
+
}
|
|
1918
|
+
if (remoteInfo) {
|
|
1919
|
+
currentUrl.searchParams.set(
|
|
1920
|
+
"remoteInfo",
|
|
1921
|
+
encodeURIComponent(JSON.stringify(remoteInfo))
|
|
1922
|
+
);
|
|
1923
|
+
}
|
|
1924
|
+
const fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
|
|
1925
|
+
if (fetchServerQuery && typeof fetchServerQuery === "object") {
|
|
1926
|
+
Object.keys(fetchServerQuery).forEach((key) => {
|
|
1927
|
+
currentUrl.searchParams.set(
|
|
1928
|
+
key,
|
|
1929
|
+
JSON.stringify(fetchServerQuery[key])
|
|
1930
|
+
);
|
|
1931
|
+
});
|
|
1932
|
+
}
|
|
1933
|
+
const fetchUrl = currentUrl.toString();
|
|
1934
|
+
const data = await fetch(fetchUrl).then((res2) => res2.json());
|
|
1935
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
1936
|
+
res && res(data);
|
|
1937
|
+
} catch (e) {
|
|
1938
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
1939
|
+
rej && rej(e);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
return promise;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
function isCSROnly() {
|
|
1946
|
+
return window._SSR_DATA === void 0;
|
|
1947
|
+
}
|
|
1948
|
+
function isServerEnv() {
|
|
1949
|
+
return typeof window === "undefined";
|
|
1950
|
+
}
|
|
1951
|
+
function setSSREnv({
|
|
1952
|
+
fetchServerQuery
|
|
1953
|
+
}) {
|
|
1954
|
+
globalThis.FEDERATION_SSR = true;
|
|
1955
|
+
globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
|
|
1956
|
+
}
|
|
1957
|
+
function getLoadedRemoteInfos(id, instance) {
|
|
1958
|
+
if (!instance) {
|
|
1959
|
+
return;
|
|
1960
|
+
}
|
|
1961
|
+
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
|
|
1962
|
+
if (!name) {
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
const module2 = instance.moduleCache.get(name);
|
|
1966
|
+
if (!module2) {
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(
|
|
1970
|
+
module2.remoteInfo
|
|
1971
|
+
);
|
|
1972
|
+
return {
|
|
1973
|
+
...module2.remoteInfo,
|
|
1974
|
+
snapshot: remoteSnapshot,
|
|
1975
|
+
expose
|
|
1976
|
+
};
|
|
1977
|
+
}
|
|
1978
|
+
exports.CacheSize = CacheSize;
|
|
1979
|
+
exports.CacheTime = CacheTime;
|
|
1980
|
+
exports.DATA_FETCH_CLIENT_SUFFIX = DATA_FETCH_CLIENT_SUFFIX;
|
|
1981
|
+
exports.DATA_FETCH_ERROR_PREFIX = DATA_FETCH_ERROR_PREFIX;
|
|
1982
|
+
exports.DATA_FETCH_FUNCTION = DATA_FETCH_FUNCTION;
|
|
1983
|
+
exports.DATA_FETCH_QUERY = DATA_FETCH_QUERY;
|
|
1984
|
+
exports.DOWNGRADE_KEY = DOWNGRADE_KEY;
|
|
1985
|
+
exports.ERROR_TYPE = ERROR_TYPE;
|
|
1986
|
+
exports.FS_HREF = FS_HREF;
|
|
1987
|
+
exports.LOAD_REMOTE_ERROR_PREFIX = LOAD_REMOTE_ERROR_PREFIX;
|
|
1988
|
+
exports.MF_DATA_FETCH_STATUS = MF_DATA_FETCH_STATUS;
|
|
1989
|
+
exports.MF_DATA_FETCH_TYPE = MF_DATA_FETCH_TYPE;
|
|
1990
|
+
exports.cache = cache;
|
|
1991
|
+
exports.callAllDowngrade = callAllDowngrade;
|
|
1992
|
+
exports.callDowngrade = callDowngrade;
|
|
1993
|
+
exports.clearStore = clearStore;
|
|
1994
|
+
exports.configureCache = configureCache;
|
|
1995
|
+
exports.fetchData = fetchData;
|
|
1996
|
+
exports.flushDataFetch = flushDataFetch;
|
|
1997
|
+
exports.generateKey = generateKey;
|
|
1998
|
+
exports.getDataFetchCache = getDataFetchCache;
|
|
1999
|
+
exports.getDataFetchIdWithErrorMsgs = getDataFetchIdWithErrorMsgs;
|
|
2000
|
+
exports.getDataFetchInfo = getDataFetchInfo;
|
|
2001
|
+
exports.getDataFetchItem = getDataFetchItem;
|
|
2002
|
+
exports.getDataFetchMap = getDataFetchMap;
|
|
2003
|
+
exports.getDataFetchMapKey = getDataFetchMapKey;
|
|
2004
|
+
exports.getDowngradeTag = getDowngradeTag;
|
|
2005
|
+
exports.getLoadedRemoteInfos = getLoadedRemoteInfos;
|
|
2006
|
+
exports.initDataFetchMap = initDataFetchMap;
|
|
2007
|
+
exports.isCSROnly = isCSROnly;
|
|
2008
|
+
exports.isDataLoaderExpose = isDataLoaderExpose;
|
|
2009
|
+
exports.isServerEnv = isServerEnv;
|
|
2010
|
+
exports.loadDataFetchModule = loadDataFetchModule;
|
|
2011
|
+
exports.logger = logger;
|
|
2012
|
+
exports.revalidateTag = revalidateTag;
|
|
2013
|
+
exports.setDataFetchItemLoadedStatus = setDataFetchItemLoadedStatus;
|
|
2014
|
+
exports.setSSREnv = setSSREnv;
|
|
2015
|
+
exports.wrapDataFetchId = wrapDataFetchId;
|