@bleedingdev/modern-js-runtime-utils 3.2.0-ultramodern.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/browser/deferreds.js +161 -0
- package/dist/cjs/browser/index.js +65 -0
- package/dist/cjs/browser/nestedRoutes.js +141 -0
- package/dist/cjs/merge.js +60 -0
- package/dist/cjs/node/fileReader.js +94 -0
- package/dist/cjs/node/index.js +99 -0
- package/dist/cjs/node/loaderContext/createLoaderCtx.js +51 -0
- package/dist/cjs/node/loaderContext/createRequestCtx.js +50 -0
- package/dist/cjs/node/loaderContext/index.js +41 -0
- package/dist/cjs/node/nestedRoutes.js +58 -0
- package/dist/cjs/node/sanitize.js +103 -0
- package/dist/cjs/node/serialize.js +49 -0
- package/dist/cjs/node/storer/container.js +72 -0
- package/dist/cjs/node/storer/index.js +44 -0
- package/dist/cjs/node/storer/storage.js +87 -0
- package/dist/cjs/parsed.js +46 -0
- package/dist/cjs/router.js +91 -0
- package/dist/cjs/rsc.js +58 -0
- package/dist/cjs/server/index.js +58 -0
- package/dist/cjs/server/nestedRoutes.js +49 -0
- package/dist/cjs/time.js +61 -0
- package/dist/cjs/universal/async_storage.js +58 -0
- package/dist/cjs/universal/async_storage.server.js +69 -0
- package/dist/cjs/universal/cache.js +381 -0
- package/dist/cjs/universal/request.js +86 -0
- package/dist/cjs/url.js +39 -0
- package/dist/esm/browser/deferreds.mjs +118 -0
- package/dist/esm/browser/index.mjs +2 -0
- package/dist/esm/browser/nestedRoutes.mjs +104 -0
- package/dist/esm/merge.mjs +26 -0
- package/dist/esm/node/fileReader.mjs +47 -0
- package/dist/esm/node/index.mjs +5 -0
- package/dist/esm/node/loaderContext/createLoaderCtx.mjs +14 -0
- package/dist/esm/node/loaderContext/createRequestCtx.mjs +16 -0
- package/dist/esm/node/loaderContext/index.mjs +4 -0
- package/dist/esm/node/nestedRoutes.mjs +1 -0
- package/dist/esm/node/sanitize.mjs +66 -0
- package/dist/esm/node/serialize.mjs +5 -0
- package/dist/esm/node/storer/container.mjs +38 -0
- package/dist/esm/node/storer/index.mjs +7 -0
- package/dist/esm/node/storer/storage.mjs +53 -0
- package/dist/esm/parsed.mjs +12 -0
- package/dist/esm/router.mjs +18 -0
- package/dist/esm/rsc.mjs +1 -0
- package/dist/esm/server/index.mjs +1 -0
- package/dist/esm/server/nestedRoutes.mjs +15 -0
- package/dist/esm/time.mjs +27 -0
- package/dist/esm/universal/async_storage.mjs +7 -0
- package/dist/esm/universal/async_storage.server.mjs +32 -0
- package/dist/esm/universal/cache.mjs +326 -0
- package/dist/esm/universal/request.mjs +40 -0
- package/dist/esm/url.mjs +5 -0
- package/dist/esm-node/browser/deferreds.mjs +119 -0
- package/dist/esm-node/browser/index.mjs +3 -0
- package/dist/esm-node/browser/nestedRoutes.mjs +105 -0
- package/dist/esm-node/merge.mjs +27 -0
- package/dist/esm-node/node/fileReader.mjs +48 -0
- package/dist/esm-node/node/index.mjs +6 -0
- package/dist/esm-node/node/loaderContext/createLoaderCtx.mjs +15 -0
- package/dist/esm-node/node/loaderContext/createRequestCtx.mjs +17 -0
- package/dist/esm-node/node/loaderContext/index.mjs +5 -0
- package/dist/esm-node/node/nestedRoutes.mjs +2 -0
- package/dist/esm-node/node/sanitize.mjs +67 -0
- package/dist/esm-node/node/serialize.mjs +6 -0
- package/dist/esm-node/node/storer/container.mjs +39 -0
- package/dist/esm-node/node/storer/index.mjs +8 -0
- package/dist/esm-node/node/storer/storage.mjs +54 -0
- package/dist/esm-node/parsed.mjs +13 -0
- package/dist/esm-node/router.mjs +19 -0
- package/dist/esm-node/rsc.mjs +2 -0
- package/dist/esm-node/server/index.mjs +2 -0
- package/dist/esm-node/server/nestedRoutes.mjs +16 -0
- package/dist/esm-node/time.mjs +28 -0
- package/dist/esm-node/universal/async_storage.mjs +25 -0
- package/dist/esm-node/universal/async_storage.server.mjs +33 -0
- package/dist/esm-node/universal/cache.mjs +327 -0
- package/dist/esm-node/universal/request.mjs +41 -0
- package/dist/esm-node/url.mjs +6 -0
- package/dist/types/browser/deferreds.d.ts +31 -0
- package/dist/types/browser/index.d.ts +2 -0
- package/dist/types/browser/nestedRoutes.d.ts +12 -0
- package/dist/types/merge.d.ts +6 -0
- package/dist/types/node/fileReader.d.ts +19 -0
- package/dist/types/node/index.d.ts +8 -0
- package/dist/types/node/loaderContext/createLoaderCtx.d.ts +7 -0
- package/dist/types/node/loaderContext/createRequestCtx.d.ts +16 -0
- package/dist/types/node/loaderContext/index.d.ts +4 -0
- package/dist/types/node/nestedRoutes.d.ts +1 -0
- package/dist/types/node/sanitize.d.ts +11 -0
- package/dist/types/node/serialize.d.ts +1 -0
- package/dist/types/node/storer/container.d.ts +27 -0
- package/dist/types/node/storer/index.d.ts +3 -0
- package/dist/types/node/storer/storage.d.ts +26 -0
- package/dist/types/parsed.d.ts +1 -0
- package/dist/types/router.d.ts +6 -0
- package/dist/types/rsc.d.ts +1 -0
- package/dist/types/server/index.d.ts +1 -0
- package/dist/types/server/nestedRoutes.d.ts +2 -0
- package/dist/types/time.d.ts +1 -0
- package/dist/types/universal/async_storage.d.ts +2 -0
- package/dist/types/universal/async_storage.server.d.ts +29 -0
- package/dist/types/universal/cache.d.ts +68 -0
- package/dist/types/universal/request.d.ts +17 -0
- package/dist/types/url.d.ts +11 -0
- package/package.json +197 -0
- package/rslib.config.mts +28 -0
- package/rstest.config.mts +26 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
CacheSize: ()=>CacheSize,
|
|
28
|
+
CacheTime: ()=>CacheTime,
|
|
29
|
+
cache: ()=>cache,
|
|
30
|
+
clearStore: ()=>clearStore,
|
|
31
|
+
configureCache: ()=>configureCache,
|
|
32
|
+
generateKey: ()=>generateKey,
|
|
33
|
+
revalidateTag: ()=>revalidateTag,
|
|
34
|
+
withRequestCache: ()=>withRequestCache
|
|
35
|
+
});
|
|
36
|
+
const external_lru_cache_namespaceObject = require("lru-cache");
|
|
37
|
+
const external_async_storage_js_namespaceObject = require("./async_storage.js");
|
|
38
|
+
const CacheSize = {
|
|
39
|
+
KB: 1024,
|
|
40
|
+
MB: 1048576,
|
|
41
|
+
GB: 1073741824
|
|
42
|
+
};
|
|
43
|
+
const CacheTime = {
|
|
44
|
+
SECOND: 1000,
|
|
45
|
+
MINUTE: 60000,
|
|
46
|
+
HOUR: 3600000,
|
|
47
|
+
DAY: 86400000,
|
|
48
|
+
WEEK: 604800000,
|
|
49
|
+
MONTH: 2592000000
|
|
50
|
+
};
|
|
51
|
+
function estimateObjectSize(data) {
|
|
52
|
+
const type = typeof data;
|
|
53
|
+
if ('number' === type) return 8;
|
|
54
|
+
if ('boolean' === type) return 4;
|
|
55
|
+
if ('string' === type) return Math.max(2 * data.length, 1);
|
|
56
|
+
if (null == data) return 1;
|
|
57
|
+
if (ArrayBuffer.isView(data)) return Math.max(data.byteLength, 1);
|
|
58
|
+
if (Array.isArray(data)) return Math.max(data.reduce((acc, item)=>acc + estimateObjectSize(item), 0), 1);
|
|
59
|
+
if (data instanceof Map || data instanceof Set) return 1024;
|
|
60
|
+
if (data instanceof Date) return 8;
|
|
61
|
+
if ('object' === type) return Math.max(Object.entries(data).reduce((acc, [key, value])=>acc + 2 * key.length + estimateObjectSize(value), 0), 1);
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
class MemoryContainer {
|
|
65
|
+
async get(key) {
|
|
66
|
+
return this.lru.get(key);
|
|
67
|
+
}
|
|
68
|
+
async set(key, value, options) {
|
|
69
|
+
if (options?.ttl) this.lru.set(key, value, {
|
|
70
|
+
ttl: 1000 * options.ttl
|
|
71
|
+
});
|
|
72
|
+
else this.lru.set(key, value);
|
|
73
|
+
}
|
|
74
|
+
async has(key) {
|
|
75
|
+
return this.lru.has(key);
|
|
76
|
+
}
|
|
77
|
+
async delete(key) {
|
|
78
|
+
return this.lru.delete(key);
|
|
79
|
+
}
|
|
80
|
+
async clear() {
|
|
81
|
+
this.lru.clear();
|
|
82
|
+
}
|
|
83
|
+
constructor(options){
|
|
84
|
+
this.lru = new external_lru_cache_namespaceObject.LRUCache({
|
|
85
|
+
maxSize: options?.maxSize ?? CacheSize.GB,
|
|
86
|
+
sizeCalculation: estimateObjectSize,
|
|
87
|
+
updateAgeOnGet: true,
|
|
88
|
+
updateAgeOnHas: true
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const isServer = "u" < typeof window;
|
|
93
|
+
const requestCacheMap = new WeakMap();
|
|
94
|
+
const TAG_PREFIX = 'tag:';
|
|
95
|
+
const CACHE_PREFIX = 'modernjs_cache:';
|
|
96
|
+
const ongoingRevalidations = new Map();
|
|
97
|
+
let cache_storage;
|
|
98
|
+
let cacheConfig = {
|
|
99
|
+
maxSize: CacheSize.GB
|
|
100
|
+
};
|
|
101
|
+
function getStorage() {
|
|
102
|
+
if (cache_storage) return cache_storage;
|
|
103
|
+
cache_storage = cacheConfig.container ? cacheConfig.container : new MemoryContainer({
|
|
104
|
+
maxSize: cacheConfig.maxSize
|
|
105
|
+
});
|
|
106
|
+
return cache_storage;
|
|
107
|
+
}
|
|
108
|
+
function configureCache(config) {
|
|
109
|
+
cacheConfig = {
|
|
110
|
+
...cacheConfig,
|
|
111
|
+
...config
|
|
112
|
+
};
|
|
113
|
+
cache_storage = void 0;
|
|
114
|
+
}
|
|
115
|
+
function generateKey(args) {
|
|
116
|
+
return JSON.stringify(args, (_, value)=>{
|
|
117
|
+
if (value && 'object' == typeof value && !Array.isArray(value)) return Object.keys(value).sort().reduce((result, key)=>{
|
|
118
|
+
result[key] = value[key];
|
|
119
|
+
return result;
|
|
120
|
+
}, {});
|
|
121
|
+
return value;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function generateStableFunctionId(fn) {
|
|
125
|
+
const fnString = fn.toString();
|
|
126
|
+
let hash = 0;
|
|
127
|
+
for(let i = 0; i < fnString.length; i++){
|
|
128
|
+
const char = fnString.charCodeAt(i);
|
|
129
|
+
hash = (hash << 5) - hash + char;
|
|
130
|
+
hash &= hash;
|
|
131
|
+
}
|
|
132
|
+
return `fn_${fn.name || 'anonymous'}_${Math.abs(hash).toString(36)}`;
|
|
133
|
+
}
|
|
134
|
+
function cache(fn, options) {
|
|
135
|
+
return async (...args)=>{
|
|
136
|
+
if (isServer && void 0 === options) {
|
|
137
|
+
const storage = await (0, external_async_storage_js_namespaceObject.getAsyncLocalStorage)();
|
|
138
|
+
const request = storage?.useContext()?.request;
|
|
139
|
+
if (request) {
|
|
140
|
+
let shouldDisableCaching = false;
|
|
141
|
+
if (cacheConfig.unstable_shouldDisable) shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
142
|
+
request
|
|
143
|
+
});
|
|
144
|
+
if (shouldDisableCaching) return fn(...args);
|
|
145
|
+
let requestCache = requestCacheMap.get(request);
|
|
146
|
+
if (!requestCache) {
|
|
147
|
+
requestCache = new Map();
|
|
148
|
+
requestCacheMap.set(request, requestCache);
|
|
149
|
+
}
|
|
150
|
+
let fnCache = requestCache.get(fn);
|
|
151
|
+
if (!fnCache) {
|
|
152
|
+
fnCache = new Map();
|
|
153
|
+
requestCache.set(fn, fnCache);
|
|
154
|
+
}
|
|
155
|
+
const key = generateKey(args);
|
|
156
|
+
if (fnCache.has(key)) return fnCache.get(key);
|
|
157
|
+
const promise = fn(...args);
|
|
158
|
+
fnCache.set(key, promise);
|
|
159
|
+
try {
|
|
160
|
+
const data = await promise;
|
|
161
|
+
return data;
|
|
162
|
+
} catch (error) {
|
|
163
|
+
fnCache.delete(key);
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} else if (void 0 !== options) try {
|
|
168
|
+
const { tag, maxAge = 5 * CacheTime.MINUTE, revalidate = 0, customKey, onCache, getKey, unstable_shouldCache } = options;
|
|
169
|
+
let missReason;
|
|
170
|
+
const currentStorage = getStorage();
|
|
171
|
+
const now = Date.now();
|
|
172
|
+
const tags = tag ? Array.isArray(tag) ? tag : [
|
|
173
|
+
tag
|
|
174
|
+
] : [];
|
|
175
|
+
const genKey = getKey ? getKey(...args) : generateKey(args);
|
|
176
|
+
let finalKey;
|
|
177
|
+
if (customKey) finalKey = customKey({
|
|
178
|
+
params: args,
|
|
179
|
+
fn,
|
|
180
|
+
generatedKey: genKey
|
|
181
|
+
});
|
|
182
|
+
else {
|
|
183
|
+
const functionId = generateStableFunctionId(fn);
|
|
184
|
+
finalKey = `${functionId}:${genKey}`;
|
|
185
|
+
}
|
|
186
|
+
const storageKey = `${CACHE_PREFIX}${finalKey}`;
|
|
187
|
+
let shouldDisableCaching = false;
|
|
188
|
+
if (isServer && cacheConfig.unstable_shouldDisable) {
|
|
189
|
+
const asyncStorage = await (0, external_async_storage_js_namespaceObject.getAsyncLocalStorage)();
|
|
190
|
+
const request = asyncStorage?.useContext()?.request;
|
|
191
|
+
if (request) shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
192
|
+
request
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
if (shouldDisableCaching) missReason = 1;
|
|
196
|
+
else {
|
|
197
|
+
const cached = await currentStorage.get(storageKey);
|
|
198
|
+
if (cached) try {
|
|
199
|
+
const cacheItem = cached;
|
|
200
|
+
const age = now - cacheItem.timestamp;
|
|
201
|
+
if (age < maxAge) {
|
|
202
|
+
onCache?.({
|
|
203
|
+
status: 'hit',
|
|
204
|
+
key: finalKey,
|
|
205
|
+
params: args,
|
|
206
|
+
result: cacheItem.data
|
|
207
|
+
});
|
|
208
|
+
return cacheItem.data;
|
|
209
|
+
}
|
|
210
|
+
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
211
|
+
onCache?.({
|
|
212
|
+
status: 'stale',
|
|
213
|
+
key: finalKey,
|
|
214
|
+
params: args,
|
|
215
|
+
result: cacheItem.data
|
|
216
|
+
});
|
|
217
|
+
if (!ongoingRevalidations.has(storageKey)) {
|
|
218
|
+
const revalidationPromise = (async ()=>{
|
|
219
|
+
try {
|
|
220
|
+
const newData = await fn(...args);
|
|
221
|
+
let shouldCache = true;
|
|
222
|
+
if (unstable_shouldCache) shouldCache = await unstable_shouldCache({
|
|
223
|
+
params: args,
|
|
224
|
+
result: newData
|
|
225
|
+
});
|
|
226
|
+
if (shouldCache) await setCacheItem(currentStorage, storageKey, newData, tags, maxAge, revalidate);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (isServer) {
|
|
229
|
+
const asyncStorage = await (0, external_async_storage_js_namespaceObject.getAsyncLocalStorage)();
|
|
230
|
+
asyncStorage?.useContext()?.monitors?.error(error.message);
|
|
231
|
+
} else console.error('Background revalidation failed:', error);
|
|
232
|
+
} finally{
|
|
233
|
+
ongoingRevalidations.delete(storageKey);
|
|
234
|
+
}
|
|
235
|
+
})();
|
|
236
|
+
ongoingRevalidations.set(storageKey, revalidationPromise);
|
|
237
|
+
}
|
|
238
|
+
return cacheItem.data;
|
|
239
|
+
}
|
|
240
|
+
missReason = 3;
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.warn('Failed to parse cached data:', error);
|
|
243
|
+
missReason = 4;
|
|
244
|
+
}
|
|
245
|
+
else missReason = 2;
|
|
246
|
+
}
|
|
247
|
+
const data = await fn(...args);
|
|
248
|
+
if (!shouldDisableCaching) {
|
|
249
|
+
let shouldCache = true;
|
|
250
|
+
if (unstable_shouldCache) shouldCache = await unstable_shouldCache({
|
|
251
|
+
params: args,
|
|
252
|
+
result: data
|
|
253
|
+
});
|
|
254
|
+
if (shouldCache) await setCacheItem(currentStorage, storageKey, data, tags, maxAge, revalidate);
|
|
255
|
+
}
|
|
256
|
+
onCache?.({
|
|
257
|
+
status: 'miss',
|
|
258
|
+
key: finalKey,
|
|
259
|
+
params: args,
|
|
260
|
+
result: data,
|
|
261
|
+
reason: missReason
|
|
262
|
+
});
|
|
263
|
+
return data;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.warn('Cache operation failed, falling back to direct execution:', error);
|
|
266
|
+
const data = await fn(...args);
|
|
267
|
+
const { onCache } = options;
|
|
268
|
+
try {
|
|
269
|
+
onCache?.({
|
|
270
|
+
status: 'miss',
|
|
271
|
+
key: 'cache_failed',
|
|
272
|
+
params: args,
|
|
273
|
+
result: data,
|
|
274
|
+
reason: 5
|
|
275
|
+
});
|
|
276
|
+
} catch (callbackError) {
|
|
277
|
+
console.warn('Failed to call onCache callback:', callbackError);
|
|
278
|
+
}
|
|
279
|
+
return data;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
console.warn('The cache function will not work because it runs on the browser and there are no options are provided.');
|
|
283
|
+
return fn(...args);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
async function setCacheItem(storage, storageKey, data, tags, maxAge, revalidate) {
|
|
288
|
+
const newItem = {
|
|
289
|
+
data,
|
|
290
|
+
timestamp: Date.now(),
|
|
291
|
+
tags: tags.length > 0 ? tags : void 0
|
|
292
|
+
};
|
|
293
|
+
const ttl = (maxAge + revalidate) / 1000;
|
|
294
|
+
await storage.set(storageKey, newItem, {
|
|
295
|
+
ttl: ttl > 0 ? ttl : void 0
|
|
296
|
+
});
|
|
297
|
+
await updateTagRelationships(storage, storageKey, tags);
|
|
298
|
+
}
|
|
299
|
+
async function updateTagRelationships(storage, storageKey, tags) {
|
|
300
|
+
for (const tag of tags){
|
|
301
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
302
|
+
const keyList = await storage.get(tagStoreKey);
|
|
303
|
+
const keyArray = keyList || [];
|
|
304
|
+
if (!keyArray.includes(storageKey)) keyArray.push(storageKey);
|
|
305
|
+
await storage.set(tagStoreKey, keyArray);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async function removeKeyFromTags(storage, storageKey, tags) {
|
|
309
|
+
for (const tag of tags){
|
|
310
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
311
|
+
const keyList = await storage.get(tagStoreKey);
|
|
312
|
+
if (keyList) try {
|
|
313
|
+
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
314
|
+
const updatedKeyList = keyArray.filter((key)=>key !== storageKey);
|
|
315
|
+
if (updatedKeyList.length > 0) await storage.set(tagStoreKey, updatedKeyList);
|
|
316
|
+
else await storage.delete(tagStoreKey);
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.warn(`Failed to process tag key list for tag ${tag}:`, error);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function withRequestCache(handler) {
|
|
323
|
+
if (!isServer) return handler;
|
|
324
|
+
return async (req, ...args)=>{
|
|
325
|
+
const storage = await (0, external_async_storage_js_namespaceObject.getAsyncLocalStorage)();
|
|
326
|
+
return storage.run({
|
|
327
|
+
request: req
|
|
328
|
+
}, ()=>handler(req, ...args));
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
async function revalidateTag(tag) {
|
|
332
|
+
const currentStorage = getStorage();
|
|
333
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
334
|
+
const keyList = await currentStorage.get(tagStoreKey);
|
|
335
|
+
if (keyList) try {
|
|
336
|
+
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
337
|
+
for (const cacheKey of keyArray){
|
|
338
|
+
const cached = await currentStorage.get(cacheKey);
|
|
339
|
+
if (cached) try {
|
|
340
|
+
const cacheItem = cached;
|
|
341
|
+
if (cacheItem.tags) {
|
|
342
|
+
const otherTags = cacheItem.tags.filter((t)=>t !== tag);
|
|
343
|
+
await removeKeyFromTags(currentStorage, cacheKey, otherTags);
|
|
344
|
+
}
|
|
345
|
+
} catch (error) {
|
|
346
|
+
console.warn('Failed to parse cached data while revalidating:', error);
|
|
347
|
+
}
|
|
348
|
+
await currentStorage.delete(cacheKey);
|
|
349
|
+
}
|
|
350
|
+
await currentStorage.delete(tagStoreKey);
|
|
351
|
+
} catch (error) {
|
|
352
|
+
console.warn('Failed to process tag key list:', error);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
async function clearStore() {
|
|
356
|
+
const currentStorage = getStorage();
|
|
357
|
+
await currentStorage.clear();
|
|
358
|
+
cache_storage = void 0;
|
|
359
|
+
ongoingRevalidations.clear();
|
|
360
|
+
}
|
|
361
|
+
exports.CacheSize = __webpack_exports__.CacheSize;
|
|
362
|
+
exports.CacheTime = __webpack_exports__.CacheTime;
|
|
363
|
+
exports.cache = __webpack_exports__.cache;
|
|
364
|
+
exports.clearStore = __webpack_exports__.clearStore;
|
|
365
|
+
exports.configureCache = __webpack_exports__.configureCache;
|
|
366
|
+
exports.generateKey = __webpack_exports__.generateKey;
|
|
367
|
+
exports.revalidateTag = __webpack_exports__.revalidateTag;
|
|
368
|
+
exports.withRequestCache = __webpack_exports__.withRequestCache;
|
|
369
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
370
|
+
"CacheSize",
|
|
371
|
+
"CacheTime",
|
|
372
|
+
"cache",
|
|
373
|
+
"clearStore",
|
|
374
|
+
"configureCache",
|
|
375
|
+
"generateKey",
|
|
376
|
+
"revalidateTag",
|
|
377
|
+
"withRequestCache"
|
|
378
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
379
|
+
Object.defineProperty(exports, '__esModule', {
|
|
380
|
+
value: true
|
|
381
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
getHost: ()=>getHost,
|
|
28
|
+
getPathname: ()=>getPathname,
|
|
29
|
+
parseCookie: ()=>parseCookie,
|
|
30
|
+
parseHeaders: ()=>parseHeaders,
|
|
31
|
+
parseQuery: ()=>parseQuery
|
|
32
|
+
});
|
|
33
|
+
function parseQuery(req) {
|
|
34
|
+
const query = {};
|
|
35
|
+
const { url } = req;
|
|
36
|
+
const q = url.split('?')[1];
|
|
37
|
+
if (q) {
|
|
38
|
+
const search = new URLSearchParams(q);
|
|
39
|
+
search.forEach((v, k)=>{
|
|
40
|
+
query[k] = v;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return query;
|
|
44
|
+
}
|
|
45
|
+
function parseHeaders(request) {
|
|
46
|
+
const headersData = {};
|
|
47
|
+
request.headers.forEach((value, key)=>{
|
|
48
|
+
headersData[key] = value;
|
|
49
|
+
});
|
|
50
|
+
return headersData;
|
|
51
|
+
}
|
|
52
|
+
function getPathname(request) {
|
|
53
|
+
const match = request.url.match(/^https?:\/\/[^/]+(\/[^?]*)/);
|
|
54
|
+
return match ? match[1] : '/';
|
|
55
|
+
}
|
|
56
|
+
function getHost(request) {
|
|
57
|
+
const { headers } = request;
|
|
58
|
+
let host = headers.get('X-Forwarded-Host');
|
|
59
|
+
if (!host) host = headers.get('Host');
|
|
60
|
+
host = host?.split(/\s*,\s*/, 1)[0] || 'undefined';
|
|
61
|
+
return host;
|
|
62
|
+
}
|
|
63
|
+
function parseCookie(req) {
|
|
64
|
+
const _cookie = req.headers.get('Cookie');
|
|
65
|
+
const cookie = {};
|
|
66
|
+
_cookie?.trim().split(';').forEach((item)=>{
|
|
67
|
+
const [k, v] = item.trim().split('=');
|
|
68
|
+
if (k) cookie[k] = v;
|
|
69
|
+
});
|
|
70
|
+
return cookie;
|
|
71
|
+
}
|
|
72
|
+
exports.getHost = __webpack_exports__.getHost;
|
|
73
|
+
exports.getPathname = __webpack_exports__.getPathname;
|
|
74
|
+
exports.parseCookie = __webpack_exports__.parseCookie;
|
|
75
|
+
exports.parseHeaders = __webpack_exports__.parseHeaders;
|
|
76
|
+
exports.parseQuery = __webpack_exports__.parseQuery;
|
|
77
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
78
|
+
"getHost",
|
|
79
|
+
"getPathname",
|
|
80
|
+
"parseCookie",
|
|
81
|
+
"parseHeaders",
|
|
82
|
+
"parseQuery"
|
|
83
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
84
|
+
Object.defineProperty(exports, '__esModule', {
|
|
85
|
+
value: true
|
|
86
|
+
});
|
package/dist/cjs/url.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
normalizePathname: ()=>normalizePathname
|
|
28
|
+
});
|
|
29
|
+
function normalizePathname(pathname) {
|
|
30
|
+
const normalized = '/' + pathname.replace(/^\/+|\/+$/g, '');
|
|
31
|
+
return normalized;
|
|
32
|
+
}
|
|
33
|
+
exports.normalizePathname = __webpack_exports__.normalizePathname;
|
|
34
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
35
|
+
"normalizePathname"
|
|
36
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
37
|
+
Object.defineProperty(exports, '__esModule', {
|
|
38
|
+
value: true
|
|
39
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
function invariant(value, message) {
|
|
2
|
+
if (false === value || null == value) throw new Error(message);
|
|
3
|
+
}
|
|
4
|
+
class AbortedDeferredError extends Error {
|
|
5
|
+
}
|
|
6
|
+
function isTrackedPromise(value) {
|
|
7
|
+
return value instanceof Promise && true === value._tracked;
|
|
8
|
+
}
|
|
9
|
+
function unwrapTrackedPromise(value) {
|
|
10
|
+
if (!isTrackedPromise(value)) return value;
|
|
11
|
+
if (value._error) throw value._error;
|
|
12
|
+
return value._data;
|
|
13
|
+
}
|
|
14
|
+
class DeferredData {
|
|
15
|
+
trackPromise(key, value) {
|
|
16
|
+
if (!(value instanceof Promise)) return value;
|
|
17
|
+
this.deferredKeys.push(key);
|
|
18
|
+
this.pendingKeysSet.add(key);
|
|
19
|
+
const promise = Promise.race([
|
|
20
|
+
value,
|
|
21
|
+
this.abortPromise
|
|
22
|
+
]).then((data)=>this.onSettle(promise, key, void 0, data), (error)=>this.onSettle(promise, key, error));
|
|
23
|
+
promise.catch(()=>{});
|
|
24
|
+
Object.defineProperty(promise, '_tracked', {
|
|
25
|
+
get: ()=>true
|
|
26
|
+
});
|
|
27
|
+
return promise;
|
|
28
|
+
}
|
|
29
|
+
onSettle(promise, key, error, data) {
|
|
30
|
+
if (this.controller.signal.aborted && error instanceof AbortedDeferredError) {
|
|
31
|
+
this.unlistenAbortSignal();
|
|
32
|
+
Object.defineProperty(promise, '_error', {
|
|
33
|
+
get: ()=>error
|
|
34
|
+
});
|
|
35
|
+
return Promise.reject(error);
|
|
36
|
+
}
|
|
37
|
+
this.pendingKeysSet.delete(key);
|
|
38
|
+
if (this.done) this.unlistenAbortSignal();
|
|
39
|
+
if (void 0 === error && void 0 === data) {
|
|
40
|
+
const undefinedError = new Error(`Deferred data for key "${key}" resolved/rejected with \`undefined\`, you must resolve/reject with a value or \`null\`.`);
|
|
41
|
+
Object.defineProperty(promise, '_error', {
|
|
42
|
+
get: ()=>undefinedError
|
|
43
|
+
});
|
|
44
|
+
this.emit(false, key);
|
|
45
|
+
return Promise.reject(undefinedError);
|
|
46
|
+
}
|
|
47
|
+
if (void 0 === data) {
|
|
48
|
+
Object.defineProperty(promise, '_error', {
|
|
49
|
+
get: ()=>error
|
|
50
|
+
});
|
|
51
|
+
this.emit(false, key);
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
}
|
|
54
|
+
Object.defineProperty(promise, '_data', {
|
|
55
|
+
get: ()=>data
|
|
56
|
+
});
|
|
57
|
+
this.emit(false, key);
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
emit(aborted, settledKey) {
|
|
61
|
+
this.subscribers.forEach((subscriber)=>subscriber(aborted, settledKey));
|
|
62
|
+
}
|
|
63
|
+
subscribe(fn) {
|
|
64
|
+
this.subscribers.add(fn);
|
|
65
|
+
return ()=>this.subscribers.delete(fn);
|
|
66
|
+
}
|
|
67
|
+
cancel() {
|
|
68
|
+
this.controller.abort();
|
|
69
|
+
this.pendingKeysSet.forEach((v, k)=>this.pendingKeysSet.delete(k));
|
|
70
|
+
this.emit(true);
|
|
71
|
+
}
|
|
72
|
+
async resolveData(signal) {
|
|
73
|
+
let aborted = false;
|
|
74
|
+
if (!this.done) {
|
|
75
|
+
const onAbort = ()=>this.cancel();
|
|
76
|
+
signal.addEventListener('abort', onAbort);
|
|
77
|
+
aborted = await new Promise((resolve)=>{
|
|
78
|
+
this.subscribe((aborted)=>{
|
|
79
|
+
signal.removeEventListener('abort', onAbort);
|
|
80
|
+
if (aborted || this.done) resolve(aborted);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return aborted;
|
|
85
|
+
}
|
|
86
|
+
get done() {
|
|
87
|
+
return 0 === this.pendingKeysSet.size;
|
|
88
|
+
}
|
|
89
|
+
get unwrappedData() {
|
|
90
|
+
invariant(null !== this.data && this.done, 'Can only unwrap data on initialized and settled deferreds');
|
|
91
|
+
return Object.entries(this.data).reduce((acc, [key, value])=>Object.assign(acc, {
|
|
92
|
+
[key]: unwrapTrackedPromise(value)
|
|
93
|
+
}), {});
|
|
94
|
+
}
|
|
95
|
+
get pendingKeys() {
|
|
96
|
+
return Array.from(this.pendingKeysSet);
|
|
97
|
+
}
|
|
98
|
+
constructor(data, responseInit){
|
|
99
|
+
this.pendingKeysSet = new Set();
|
|
100
|
+
this.subscribers = new Set();
|
|
101
|
+
this.__modern_deferred = true;
|
|
102
|
+
this.deferredKeys = [];
|
|
103
|
+
invariant(data && 'object' == typeof data && !Array.isArray(data), 'defer() only accepts plain objects');
|
|
104
|
+
let reject;
|
|
105
|
+
this.abortPromise = new Promise((_, r)=>reject = r);
|
|
106
|
+
this.controller = new AbortController();
|
|
107
|
+
const onAbort = ()=>reject(new AbortedDeferredError('Deferred data aborted'));
|
|
108
|
+
this.unlistenAbortSignal = ()=>this.controller.signal.removeEventListener('abort', onAbort);
|
|
109
|
+
this.controller.signal.addEventListener('abort', onAbort);
|
|
110
|
+
this.data = Object.entries(data).reduce((acc, [key, value])=>Object.assign(acc, {
|
|
111
|
+
[key]: this.trackPromise(key, value)
|
|
112
|
+
}), {});
|
|
113
|
+
if (this.done) this.unlistenAbortSignal();
|
|
114
|
+
this.init = responseInit;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const activeDeferreds = new Map();
|
|
118
|
+
export { AbortedDeferredError, DeferredData, activeDeferreds, invariant };
|