@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,327 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { LRUCache } from "lru-cache";
|
|
3
|
+
import { getAsyncLocalStorage } from "./async_storage.mjs";
|
|
4
|
+
const CacheSize = {
|
|
5
|
+
KB: 1024,
|
|
6
|
+
MB: 1048576,
|
|
7
|
+
GB: 1073741824
|
|
8
|
+
};
|
|
9
|
+
const CacheTime = {
|
|
10
|
+
SECOND: 1000,
|
|
11
|
+
MINUTE: 60000,
|
|
12
|
+
HOUR: 3600000,
|
|
13
|
+
DAY: 86400000,
|
|
14
|
+
WEEK: 604800000,
|
|
15
|
+
MONTH: 2592000000
|
|
16
|
+
};
|
|
17
|
+
function estimateObjectSize(data) {
|
|
18
|
+
const type = typeof data;
|
|
19
|
+
if ('number' === type) return 8;
|
|
20
|
+
if ('boolean' === type) return 4;
|
|
21
|
+
if ('string' === type) return Math.max(2 * data.length, 1);
|
|
22
|
+
if (null == data) return 1;
|
|
23
|
+
if (ArrayBuffer.isView(data)) return Math.max(data.byteLength, 1);
|
|
24
|
+
if (Array.isArray(data)) return Math.max(data.reduce((acc, item)=>acc + estimateObjectSize(item), 0), 1);
|
|
25
|
+
if (data instanceof Map || data instanceof Set) return 1024;
|
|
26
|
+
if (data instanceof Date) return 8;
|
|
27
|
+
if ('object' === type) return Math.max(Object.entries(data).reduce((acc, [key, value])=>acc + 2 * key.length + estimateObjectSize(value), 0), 1);
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
class MemoryContainer {
|
|
31
|
+
async get(key) {
|
|
32
|
+
return this.lru.get(key);
|
|
33
|
+
}
|
|
34
|
+
async set(key, value, options) {
|
|
35
|
+
if (options?.ttl) this.lru.set(key, value, {
|
|
36
|
+
ttl: 1000 * options.ttl
|
|
37
|
+
});
|
|
38
|
+
else this.lru.set(key, value);
|
|
39
|
+
}
|
|
40
|
+
async has(key) {
|
|
41
|
+
return this.lru.has(key);
|
|
42
|
+
}
|
|
43
|
+
async delete(key) {
|
|
44
|
+
return this.lru.delete(key);
|
|
45
|
+
}
|
|
46
|
+
async clear() {
|
|
47
|
+
this.lru.clear();
|
|
48
|
+
}
|
|
49
|
+
constructor(options){
|
|
50
|
+
this.lru = new LRUCache({
|
|
51
|
+
maxSize: options?.maxSize ?? CacheSize.GB,
|
|
52
|
+
sizeCalculation: estimateObjectSize,
|
|
53
|
+
updateAgeOnGet: true,
|
|
54
|
+
updateAgeOnHas: true
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const isServer = "u" < typeof window;
|
|
59
|
+
const requestCacheMap = new WeakMap();
|
|
60
|
+
const TAG_PREFIX = 'tag:';
|
|
61
|
+
const CACHE_PREFIX = 'modernjs_cache:';
|
|
62
|
+
const ongoingRevalidations = new Map();
|
|
63
|
+
let cache_storage;
|
|
64
|
+
let cacheConfig = {
|
|
65
|
+
maxSize: CacheSize.GB
|
|
66
|
+
};
|
|
67
|
+
function getStorage() {
|
|
68
|
+
if (cache_storage) return cache_storage;
|
|
69
|
+
cache_storage = cacheConfig.container ? cacheConfig.container : new MemoryContainer({
|
|
70
|
+
maxSize: cacheConfig.maxSize
|
|
71
|
+
});
|
|
72
|
+
return cache_storage;
|
|
73
|
+
}
|
|
74
|
+
function configureCache(config) {
|
|
75
|
+
cacheConfig = {
|
|
76
|
+
...cacheConfig,
|
|
77
|
+
...config
|
|
78
|
+
};
|
|
79
|
+
cache_storage = void 0;
|
|
80
|
+
}
|
|
81
|
+
function generateKey(args) {
|
|
82
|
+
return JSON.stringify(args, (_, value)=>{
|
|
83
|
+
if (value && 'object' == typeof value && !Array.isArray(value)) return Object.keys(value).sort().reduce((result, key)=>{
|
|
84
|
+
result[key] = value[key];
|
|
85
|
+
return result;
|
|
86
|
+
}, {});
|
|
87
|
+
return value;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function generateStableFunctionId(fn) {
|
|
91
|
+
const fnString = fn.toString();
|
|
92
|
+
let hash = 0;
|
|
93
|
+
for(let i = 0; i < fnString.length; i++){
|
|
94
|
+
const char = fnString.charCodeAt(i);
|
|
95
|
+
hash = (hash << 5) - hash + char;
|
|
96
|
+
hash &= hash;
|
|
97
|
+
}
|
|
98
|
+
return `fn_${fn.name || 'anonymous'}_${Math.abs(hash).toString(36)}`;
|
|
99
|
+
}
|
|
100
|
+
function cache(fn, options) {
|
|
101
|
+
return async (...args)=>{
|
|
102
|
+
if (isServer && void 0 === options) {
|
|
103
|
+
const storage = await getAsyncLocalStorage();
|
|
104
|
+
const request = storage?.useContext()?.request;
|
|
105
|
+
if (request) {
|
|
106
|
+
let shouldDisableCaching = false;
|
|
107
|
+
if (cacheConfig.unstable_shouldDisable) shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
108
|
+
request
|
|
109
|
+
});
|
|
110
|
+
if (shouldDisableCaching) return fn(...args);
|
|
111
|
+
let requestCache = requestCacheMap.get(request);
|
|
112
|
+
if (!requestCache) {
|
|
113
|
+
requestCache = new Map();
|
|
114
|
+
requestCacheMap.set(request, requestCache);
|
|
115
|
+
}
|
|
116
|
+
let fnCache = requestCache.get(fn);
|
|
117
|
+
if (!fnCache) {
|
|
118
|
+
fnCache = new Map();
|
|
119
|
+
requestCache.set(fn, fnCache);
|
|
120
|
+
}
|
|
121
|
+
const key = generateKey(args);
|
|
122
|
+
if (fnCache.has(key)) return fnCache.get(key);
|
|
123
|
+
const promise = fn(...args);
|
|
124
|
+
fnCache.set(key, promise);
|
|
125
|
+
try {
|
|
126
|
+
const data = await promise;
|
|
127
|
+
return data;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
fnCache.delete(key);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} else if (void 0 !== options) try {
|
|
134
|
+
const { tag, maxAge = 5 * CacheTime.MINUTE, revalidate = 0, customKey, onCache, getKey, unstable_shouldCache } = options;
|
|
135
|
+
let missReason;
|
|
136
|
+
const currentStorage = getStorage();
|
|
137
|
+
const now = Date.now();
|
|
138
|
+
const tags = tag ? Array.isArray(tag) ? tag : [
|
|
139
|
+
tag
|
|
140
|
+
] : [];
|
|
141
|
+
const genKey = getKey ? getKey(...args) : generateKey(args);
|
|
142
|
+
let finalKey;
|
|
143
|
+
if (customKey) finalKey = customKey({
|
|
144
|
+
params: args,
|
|
145
|
+
fn,
|
|
146
|
+
generatedKey: genKey
|
|
147
|
+
});
|
|
148
|
+
else {
|
|
149
|
+
const functionId = generateStableFunctionId(fn);
|
|
150
|
+
finalKey = `${functionId}:${genKey}`;
|
|
151
|
+
}
|
|
152
|
+
const storageKey = `${CACHE_PREFIX}${finalKey}`;
|
|
153
|
+
let shouldDisableCaching = false;
|
|
154
|
+
if (isServer && cacheConfig.unstable_shouldDisable) {
|
|
155
|
+
const asyncStorage = await getAsyncLocalStorage();
|
|
156
|
+
const request = asyncStorage?.useContext()?.request;
|
|
157
|
+
if (request) shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
158
|
+
request
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (shouldDisableCaching) missReason = 1;
|
|
162
|
+
else {
|
|
163
|
+
const cached = await currentStorage.get(storageKey);
|
|
164
|
+
if (cached) try {
|
|
165
|
+
const cacheItem = cached;
|
|
166
|
+
const age = now - cacheItem.timestamp;
|
|
167
|
+
if (age < maxAge) {
|
|
168
|
+
onCache?.({
|
|
169
|
+
status: 'hit',
|
|
170
|
+
key: finalKey,
|
|
171
|
+
params: args,
|
|
172
|
+
result: cacheItem.data
|
|
173
|
+
});
|
|
174
|
+
return cacheItem.data;
|
|
175
|
+
}
|
|
176
|
+
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
177
|
+
onCache?.({
|
|
178
|
+
status: 'stale',
|
|
179
|
+
key: finalKey,
|
|
180
|
+
params: args,
|
|
181
|
+
result: cacheItem.data
|
|
182
|
+
});
|
|
183
|
+
if (!ongoingRevalidations.has(storageKey)) {
|
|
184
|
+
const revalidationPromise = (async ()=>{
|
|
185
|
+
try {
|
|
186
|
+
const newData = await fn(...args);
|
|
187
|
+
let shouldCache = true;
|
|
188
|
+
if (unstable_shouldCache) shouldCache = await unstable_shouldCache({
|
|
189
|
+
params: args,
|
|
190
|
+
result: newData
|
|
191
|
+
});
|
|
192
|
+
if (shouldCache) await setCacheItem(currentStorage, storageKey, newData, tags, maxAge, revalidate);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (isServer) {
|
|
195
|
+
const asyncStorage = await getAsyncLocalStorage();
|
|
196
|
+
asyncStorage?.useContext()?.monitors?.error(error.message);
|
|
197
|
+
} else console.error('Background revalidation failed:', error);
|
|
198
|
+
} finally{
|
|
199
|
+
ongoingRevalidations.delete(storageKey);
|
|
200
|
+
}
|
|
201
|
+
})();
|
|
202
|
+
ongoingRevalidations.set(storageKey, revalidationPromise);
|
|
203
|
+
}
|
|
204
|
+
return cacheItem.data;
|
|
205
|
+
}
|
|
206
|
+
missReason = 3;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.warn('Failed to parse cached data:', error);
|
|
209
|
+
missReason = 4;
|
|
210
|
+
}
|
|
211
|
+
else missReason = 2;
|
|
212
|
+
}
|
|
213
|
+
const data = await fn(...args);
|
|
214
|
+
if (!shouldDisableCaching) {
|
|
215
|
+
let shouldCache = true;
|
|
216
|
+
if (unstable_shouldCache) shouldCache = await unstable_shouldCache({
|
|
217
|
+
params: args,
|
|
218
|
+
result: data
|
|
219
|
+
});
|
|
220
|
+
if (shouldCache) await setCacheItem(currentStorage, storageKey, data, tags, maxAge, revalidate);
|
|
221
|
+
}
|
|
222
|
+
onCache?.({
|
|
223
|
+
status: 'miss',
|
|
224
|
+
key: finalKey,
|
|
225
|
+
params: args,
|
|
226
|
+
result: data,
|
|
227
|
+
reason: missReason
|
|
228
|
+
});
|
|
229
|
+
return data;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.warn('Cache operation failed, falling back to direct execution:', error);
|
|
232
|
+
const data = await fn(...args);
|
|
233
|
+
const { onCache } = options;
|
|
234
|
+
try {
|
|
235
|
+
onCache?.({
|
|
236
|
+
status: 'miss',
|
|
237
|
+
key: 'cache_failed',
|
|
238
|
+
params: args,
|
|
239
|
+
result: data,
|
|
240
|
+
reason: 5
|
|
241
|
+
});
|
|
242
|
+
} catch (callbackError) {
|
|
243
|
+
console.warn('Failed to call onCache callback:', callbackError);
|
|
244
|
+
}
|
|
245
|
+
return data;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
console.warn('The cache function will not work because it runs on the browser and there are no options are provided.');
|
|
249
|
+
return fn(...args);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
async function setCacheItem(storage, storageKey, data, tags, maxAge, revalidate) {
|
|
254
|
+
const newItem = {
|
|
255
|
+
data,
|
|
256
|
+
timestamp: Date.now(),
|
|
257
|
+
tags: tags.length > 0 ? tags : void 0
|
|
258
|
+
};
|
|
259
|
+
const ttl = (maxAge + revalidate) / 1000;
|
|
260
|
+
await storage.set(storageKey, newItem, {
|
|
261
|
+
ttl: ttl > 0 ? ttl : void 0
|
|
262
|
+
});
|
|
263
|
+
await updateTagRelationships(storage, storageKey, tags);
|
|
264
|
+
}
|
|
265
|
+
async function updateTagRelationships(storage, storageKey, tags) {
|
|
266
|
+
for (const tag of tags){
|
|
267
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
268
|
+
const keyList = await storage.get(tagStoreKey);
|
|
269
|
+
const keyArray = keyList || [];
|
|
270
|
+
if (!keyArray.includes(storageKey)) keyArray.push(storageKey);
|
|
271
|
+
await storage.set(tagStoreKey, keyArray);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async function removeKeyFromTags(storage, storageKey, tags) {
|
|
275
|
+
for (const tag of tags){
|
|
276
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
277
|
+
const keyList = await storage.get(tagStoreKey);
|
|
278
|
+
if (keyList) try {
|
|
279
|
+
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
280
|
+
const updatedKeyList = keyArray.filter((key)=>key !== storageKey);
|
|
281
|
+
if (updatedKeyList.length > 0) await storage.set(tagStoreKey, updatedKeyList);
|
|
282
|
+
else await storage.delete(tagStoreKey);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
console.warn(`Failed to process tag key list for tag ${tag}:`, error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function withRequestCache(handler) {
|
|
289
|
+
if (!isServer) return handler;
|
|
290
|
+
return async (req, ...args)=>{
|
|
291
|
+
const storage = await getAsyncLocalStorage();
|
|
292
|
+
return storage.run({
|
|
293
|
+
request: req
|
|
294
|
+
}, ()=>handler(req, ...args));
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
async function revalidateTag(tag) {
|
|
298
|
+
const currentStorage = getStorage();
|
|
299
|
+
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
300
|
+
const keyList = await currentStorage.get(tagStoreKey);
|
|
301
|
+
if (keyList) try {
|
|
302
|
+
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
303
|
+
for (const cacheKey of keyArray){
|
|
304
|
+
const cached = await currentStorage.get(cacheKey);
|
|
305
|
+
if (cached) try {
|
|
306
|
+
const cacheItem = cached;
|
|
307
|
+
if (cacheItem.tags) {
|
|
308
|
+
const otherTags = cacheItem.tags.filter((t)=>t !== tag);
|
|
309
|
+
await removeKeyFromTags(currentStorage, cacheKey, otherTags);
|
|
310
|
+
}
|
|
311
|
+
} catch (error) {
|
|
312
|
+
console.warn('Failed to parse cached data while revalidating:', error);
|
|
313
|
+
}
|
|
314
|
+
await currentStorage.delete(cacheKey);
|
|
315
|
+
}
|
|
316
|
+
await currentStorage.delete(tagStoreKey);
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.warn('Failed to process tag key list:', error);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
async function clearStore() {
|
|
322
|
+
const currentStorage = getStorage();
|
|
323
|
+
await currentStorage.clear();
|
|
324
|
+
cache_storage = void 0;
|
|
325
|
+
ongoingRevalidations.clear();
|
|
326
|
+
}
|
|
327
|
+
export { CacheSize, CacheTime, cache, clearStore, configureCache, generateKey, revalidateTag, withRequestCache };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
function parseQuery(req) {
|
|
3
|
+
const query = {};
|
|
4
|
+
const { url } = req;
|
|
5
|
+
const q = url.split('?')[1];
|
|
6
|
+
if (q) {
|
|
7
|
+
const search = new URLSearchParams(q);
|
|
8
|
+
search.forEach((v, k)=>{
|
|
9
|
+
query[k] = v;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
return query;
|
|
13
|
+
}
|
|
14
|
+
function parseHeaders(request) {
|
|
15
|
+
const headersData = {};
|
|
16
|
+
request.headers.forEach((value, key)=>{
|
|
17
|
+
headersData[key] = value;
|
|
18
|
+
});
|
|
19
|
+
return headersData;
|
|
20
|
+
}
|
|
21
|
+
function getPathname(request) {
|
|
22
|
+
const match = request.url.match(/^https?:\/\/[^/]+(\/[^?]*)/);
|
|
23
|
+
return match ? match[1] : '/';
|
|
24
|
+
}
|
|
25
|
+
function getHost(request) {
|
|
26
|
+
const { headers } = request;
|
|
27
|
+
let host = headers.get('X-Forwarded-Host');
|
|
28
|
+
if (!host) host = headers.get('Host');
|
|
29
|
+
host = host?.split(/\s*,\s*/, 1)[0] || 'undefined';
|
|
30
|
+
return host;
|
|
31
|
+
}
|
|
32
|
+
function parseCookie(req) {
|
|
33
|
+
const _cookie = req.headers.get('Cookie');
|
|
34
|
+
const cookie = {};
|
|
35
|
+
_cookie?.trim().split(';').forEach((item)=>{
|
|
36
|
+
const [k, v] = item.trim().split('=');
|
|
37
|
+
if (k) cookie[k] = v;
|
|
38
|
+
});
|
|
39
|
+
return cookie;
|
|
40
|
+
}
|
|
41
|
+
export { getHost, getPathname, parseCookie, parseHeaders, parseQuery };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare function invariant(value: boolean, message?: string): asserts value;
|
|
2
|
+
export declare function invariant<T>(value: T | null | undefined, message?: string): asserts value is T;
|
|
3
|
+
export declare class AbortedDeferredError extends Error {
|
|
4
|
+
}
|
|
5
|
+
export interface TrackedPromise extends Promise<any> {
|
|
6
|
+
_tracked?: boolean;
|
|
7
|
+
_data?: any;
|
|
8
|
+
_error?: any;
|
|
9
|
+
}
|
|
10
|
+
export declare class DeferredData {
|
|
11
|
+
private pendingKeysSet;
|
|
12
|
+
private controller;
|
|
13
|
+
private abortPromise;
|
|
14
|
+
private unlistenAbortSignal;
|
|
15
|
+
private subscribers;
|
|
16
|
+
__modern_deferred: boolean;
|
|
17
|
+
data: Record<string, unknown>;
|
|
18
|
+
init?: ResponseInit;
|
|
19
|
+
deferredKeys: string[];
|
|
20
|
+
constructor(data: Record<string, unknown>, responseInit?: ResponseInit);
|
|
21
|
+
private trackPromise;
|
|
22
|
+
private onSettle;
|
|
23
|
+
private emit;
|
|
24
|
+
subscribe(fn: (aborted: boolean, settledKey?: string) => void): () => boolean;
|
|
25
|
+
cancel(): void;
|
|
26
|
+
resolveData(signal: AbortSignal): Promise<boolean>;
|
|
27
|
+
get done(): boolean;
|
|
28
|
+
get unwrappedData(): {};
|
|
29
|
+
get pendingKeys(): string[];
|
|
30
|
+
}
|
|
31
|
+
export declare const activeDeferreds: Map<string, DeferredData>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NestedRoute } from '@modern-js/types';
|
|
2
|
+
import { type JSX } from 'react';
|
|
3
|
+
export declare const transformNestedRoutes: (routes: NestedRoute[]) => import("react-router").RouteObject[];
|
|
4
|
+
type DeferredDataComponentType = (props?: {
|
|
5
|
+
nonce?: string;
|
|
6
|
+
}) => JSX.Element | null;
|
|
7
|
+
export declare const renderNestedRoute: (nestedRoute: NestedRoute, options?: {
|
|
8
|
+
parent?: NestedRoute;
|
|
9
|
+
DeferredDataComponent?: DeferredDataComponentType;
|
|
10
|
+
props?: Record<string, any>;
|
|
11
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type PlainObject = {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
};
|
|
4
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
5
|
+
export declare function merge<T extends PlainObject, U extends PlainObject[]>(target: T, ...sources: U): T & UnionToIntersection<U[number]>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Fs from '@modern-js/utils/fs-extra';
|
|
2
|
+
import type { Buffer } from 'buffer';
|
|
3
|
+
import type { Storage } from './storer/storage';
|
|
4
|
+
export declare class FileReader {
|
|
5
|
+
private storage;
|
|
6
|
+
private fs;
|
|
7
|
+
constructor(storage: Storage<Buffer | null>);
|
|
8
|
+
readFile(path: string, encoding?: 'utf-8'): Promise<string | null>;
|
|
9
|
+
readFile(path: string, encoding?: 'buffer'): Promise<Buffer | null>;
|
|
10
|
+
readFileFromSystem(path: string, encoding?: 'utf-8'): Promise<string | null>;
|
|
11
|
+
readFileFromSystem(path: string, encoding?: 'buffer'): Promise<Buffer | null>;
|
|
12
|
+
_readFileFactory(fs: typeof Fs): (path: string, encoding?: "utf-8" | "buffer") => Promise<string | Buffer | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Clear the fileCache entriely.
|
|
15
|
+
*/
|
|
16
|
+
reset(fs?: typeof Fs): Promise<void>;
|
|
17
|
+
private encodingContent;
|
|
18
|
+
}
|
|
19
|
+
export declare const fileReader: FileReader;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RequestPayload } from '@modern-js/types';
|
|
2
|
+
import type { LoaderContext } from './createLoaderCtx';
|
|
3
|
+
interface Get<P extends Record<string, unknown>> {
|
|
4
|
+
<Key extends keyof P>(key: Key): P[Key];
|
|
5
|
+
<T>(key: LoaderContext<T>): T;
|
|
6
|
+
}
|
|
7
|
+
interface Set<P extends Record<string, unknown>> {
|
|
8
|
+
<Key extends keyof P>(key: Key, value: P[Key]): void;
|
|
9
|
+
<T>(key: LoaderContext<T>, value: T): void;
|
|
10
|
+
}
|
|
11
|
+
export type RequestContext<P extends Record<string, unknown> = {}> = {
|
|
12
|
+
get: Get<P & RequestPayload>;
|
|
13
|
+
set: Set<P & RequestPayload>;
|
|
14
|
+
};
|
|
15
|
+
export declare function createRequestContext<P extends Record<string, unknown>>(context?: Map<string | symbol, unknown>): RequestContext<P>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Reporter } from '@modern-js/types';
|
|
2
|
+
export type { LoaderContext } from './createLoaderCtx';
|
|
3
|
+
export { createRequestContext, type RequestContext, } from './createRequestCtx';
|
|
4
|
+
export declare const reporterCtx: import("./createLoaderCtx").LoaderContext<Reporter>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../server/nestedRoutes';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const DEFAULT_UNSAFE_SSR_HEADERS: readonly ["authorization", "proxy-authorization", "cookie", "set-cookie", "x-api-key", "x-auth-token", "x-csrf-token", "x-xsrf-token", "x-forwarded-client-cert", "cf-access-jwt-assertion"];
|
|
2
|
+
type SanitizeSSRPayloadOptions = {
|
|
3
|
+
unsafeHeaders?: string[];
|
|
4
|
+
treatRootAsHeaders?: boolean;
|
|
5
|
+
};
|
|
6
|
+
type SanitizeSSRPayloadResult<T> = {
|
|
7
|
+
payload: T;
|
|
8
|
+
removed: string[];
|
|
9
|
+
};
|
|
10
|
+
export declare const sanitizeSSRPayload: <T>(payload: T, options?: SanitizeSSRPayloadOptions) => SanitizeSSRPayloadResult<T>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const serializeJson: (data: any) => string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Container } from '@modern-js/types';
|
|
2
|
+
interface MemoryContainerOptions {
|
|
3
|
+
/** The maximum size of the cache, unit(MB). The default of value is 256. */
|
|
4
|
+
max?: number;
|
|
5
|
+
maxAge?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* MemoryContainer, it use lur-cache as cahe layer.
|
|
9
|
+
* It has a Time to Live, by default as 1 hour.
|
|
10
|
+
*/
|
|
11
|
+
export declare class MemoryContainer<K extends string, V extends {}> implements Container<K, V> {
|
|
12
|
+
private static BYTE;
|
|
13
|
+
private static KB;
|
|
14
|
+
private static MB;
|
|
15
|
+
private static ms;
|
|
16
|
+
private static second;
|
|
17
|
+
private static minute;
|
|
18
|
+
private static hour;
|
|
19
|
+
private cache;
|
|
20
|
+
constructor({ max, maxAge }?: MemoryContainerOptions);
|
|
21
|
+
get(key: K): Promise<V | undefined>;
|
|
22
|
+
set(key: K, value: V): Promise<this>;
|
|
23
|
+
has(key: K): Promise<boolean>;
|
|
24
|
+
delete(key: K): Promise<boolean>;
|
|
25
|
+
forEach(callbackFn: (v: V, k: K, container: this) => void): void;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Container } from '@modern-js/types';
|
|
2
|
+
export declare class Storage<V = unknown> {
|
|
3
|
+
private namespace;
|
|
4
|
+
private container;
|
|
5
|
+
constructor(namespace: string, container: Container<string, V>);
|
|
6
|
+
keys?(): Promise<string[]>;
|
|
7
|
+
values?(): Promise<V[]>;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a specified element from the container. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Container.
|
|
10
|
+
* @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
|
|
11
|
+
*/
|
|
12
|
+
get(key: string): Promise<V | undefined>;
|
|
13
|
+
/**
|
|
14
|
+
* Adds a new element with a specified key and value to the storage. If an element with the same key already exists, the element will be updated.
|
|
15
|
+
*/
|
|
16
|
+
set(key: string, value: V): Promise<this>;
|
|
17
|
+
/**
|
|
18
|
+
* @returns boolean indicating whether an element with the specified key exists or not.
|
|
19
|
+
*/
|
|
20
|
+
has(key: string): Promise<boolean>;
|
|
21
|
+
delete(key: string): Promise<boolean>;
|
|
22
|
+
clear?(): Promise<void>;
|
|
23
|
+
forEach?(fallbackFn: (v: V, k: string, storage: this) => void): void;
|
|
24
|
+
private computedUniqueKey;
|
|
25
|
+
private checkIsOwnkey;
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const parsedJSONFromElement: (id: string) => any;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from 'react-router';
|
|
2
|
+
export declare const DEFERRED_SYMBOL: unique symbol;
|
|
3
|
+
/** @deprecated Please use Response.json instead. */
|
|
4
|
+
export declare const json: (data: any, init?: number | ResponseInit) => Response;
|
|
5
|
+
/** @deprecated defer is deprecated, You don't need to use it. */
|
|
6
|
+
export declare const defer: (data: any, init?: number | ResponseInit) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'react-router';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './nestedRoutes';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const time: () => () => number;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Monitors } from '@modern-js/types';
|
|
2
|
+
import type { IncomingHttpHeaders } from 'http';
|
|
3
|
+
declare const storage: {
|
|
4
|
+
run: <O>(context: {
|
|
5
|
+
monitors?: Monitors;
|
|
6
|
+
headers?: IncomingHttpHeaders;
|
|
7
|
+
request?: Request;
|
|
8
|
+
responseProxy?: {
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
status: number;
|
|
11
|
+
};
|
|
12
|
+
activeDeferreds?: Map<string, unknown>;
|
|
13
|
+
serverPayload?: unknown;
|
|
14
|
+
}, cb: () => O | Promise<O>) => Promise<O>;
|
|
15
|
+
useContext: () => {
|
|
16
|
+
monitors?: Monitors;
|
|
17
|
+
headers?: IncomingHttpHeaders;
|
|
18
|
+
request?: Request;
|
|
19
|
+
responseProxy?: {
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
status: number;
|
|
22
|
+
};
|
|
23
|
+
activeDeferreds?: Map<string, unknown>;
|
|
24
|
+
serverPayload?: unknown;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
type Storage = typeof storage;
|
|
28
|
+
export { type Storage, storage };
|
|
29
|
+
export declare const getAsyncLocalStorage: () => Promise<Storage>;
|