@modern-js/runtime-utils 2.69.5 → 3.0.0-alpha.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/dist/cjs/browser/deferreds.js +141 -145
- package/dist/cjs/browser/index.js +62 -21
- package/dist/cjs/browser/nestedRoutes.js +121 -147
- package/dist/cjs/merge.js +51 -44
- package/dist/cjs/node/fileReader.js +88 -97
- package/dist/cjs/node/index.js +87 -33
- package/dist/cjs/node/loaderContext/createLoaderCtx.js +43 -36
- package/dist/cjs/node/loaderContext/createRequestCtx.js +43 -41
- package/dist/cjs/node/loaderContext/index.js +38 -29
- package/dist/cjs/node/serialize.js +46 -40
- package/dist/cjs/node/storer/container.js +62 -58
- package/dist/cjs/node/storer/index.js +39 -31
- package/dist/cjs/node/storer/storage.js +82 -92
- package/dist/cjs/node/stream.js +89 -91
- package/dist/cjs/parsed.js +40 -38
- package/dist/cjs/router.js +88 -19
- package/dist/cjs/rsc.js +58 -0
- package/dist/cjs/server/index.js +55 -19
- package/dist/cjs/server/nestedRoutes.js +43 -39
- package/dist/cjs/time.js +53 -47
- package/dist/cjs/universal/async_storage.js +53 -37
- package/dist/cjs/universal/async_storage.server.js +63 -74
- package/dist/cjs/universal/cache.js +330 -381
- package/dist/cjs/universal/request.js +73 -65
- package/dist/cjs/url.js +33 -28
- package/dist/esm/browser/deferreds.mjs +118 -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/node/index.js → esm/node/index.mjs} +2 -5
- package/dist/esm/node/loaderContext/createLoaderCtx.mjs +14 -0
- package/dist/esm/node/loaderContext/createRequestCtx.mjs +16 -0
- package/dist/{esm-node/node/loaderContext/index.js → esm/node/loaderContext/index.mjs} +1 -4
- package/dist/esm/node/serialize.mjs +5 -0
- package/dist/esm/node/storer/container.mjs +38 -0
- package/dist/{esm-node/node/storer/index.js → esm/node/storer/index.mjs} +2 -6
- package/dist/esm/node/storer/storage.mjs +53 -0
- package/dist/esm/node/stream.mjs +68 -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/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 +118 -0
- package/dist/esm-node/browser/index.mjs +2 -0
- package/dist/esm-node/browser/nestedRoutes.mjs +104 -0
- package/dist/esm-node/merge.mjs +26 -0
- package/dist/esm-node/node/fileReader.mjs +47 -0
- package/dist/esm-node/node/index.mjs +5 -0
- package/dist/esm-node/node/loaderContext/createLoaderCtx.mjs +14 -0
- package/dist/esm-node/node/loaderContext/createRequestCtx.mjs +16 -0
- package/dist/esm-node/node/loaderContext/index.mjs +4 -0
- package/dist/esm-node/node/serialize.mjs +5 -0
- package/dist/esm-node/node/storer/container.mjs +38 -0
- package/dist/esm-node/node/storer/index.mjs +7 -0
- package/dist/esm-node/node/storer/storage.mjs +53 -0
- package/dist/esm-node/node/stream.mjs +68 -0
- package/dist/esm-node/parsed.mjs +12 -0
- package/dist/esm-node/router.mjs +18 -0
- package/dist/esm-node/rsc.mjs +1 -0
- package/dist/esm-node/server/index.mjs +1 -0
- package/dist/esm-node/server/nestedRoutes.mjs +15 -0
- package/dist/esm-node/time.mjs +27 -0
- package/dist/esm-node/universal/async_storage.mjs +24 -0
- package/dist/esm-node/universal/async_storage.server.mjs +32 -0
- package/dist/esm-node/universal/cache.mjs +326 -0
- package/dist/esm-node/universal/request.mjs +40 -0
- package/dist/esm-node/url.mjs +5 -0
- package/dist/types/browser/nestedRoutes.d.ts +2 -1
- package/dist/types/node/stream.d.ts +1 -1
- package/dist/types/router.d.ts +6 -1
- package/dist/types/rsc.d.ts +1 -0
- package/dist/types/universal/async_storage.d.ts +1 -1
- package/dist/types/universal/async_storage.server.d.ts +1 -1
- package/package.json +36 -46
- package/rslib.config.mts +27 -0
- package/rstest.config.ts +26 -0
- package/dist/cjs/node/router.js +0 -22
- package/dist/cjs/remixRouter.js +0 -43
- package/dist/esm/browser/deferreds.js +0 -227
- package/dist/esm/browser/index.js +0 -2
- package/dist/esm/browser/nestedRoutes.js +0 -177
- package/dist/esm/merge.js +0 -33
- package/dist/esm/node/fileReader.js +0 -140
- package/dist/esm/node/index.js +0 -8
- package/dist/esm/node/loaderContext/createLoaderCtx.js +0 -24
- package/dist/esm/node/loaderContext/createRequestCtx.js +0 -24
- package/dist/esm/node/loaderContext/index.js +0 -7
- package/dist/esm/node/router.js +0 -1
- package/dist/esm/node/serialize.js +0 -9
- package/dist/esm/node/storer/container.js +0 -94
- package/dist/esm/node/storer/index.js +0 -11
- package/dist/esm/node/storer/storage.js +0 -135
- package/dist/esm/node/stream.js +0 -87
- package/dist/esm/parsed.js +0 -20
- package/dist/esm/remixRouter.js +0 -7
- package/dist/esm/router.js +0 -1
- package/dist/esm/server/index.js +0 -1
- package/dist/esm/server/nestedRoutes.js +0 -23
- package/dist/esm/time.js +0 -32
- package/dist/esm/universal/async_storage.js +0 -18
- package/dist/esm/universal/async_storage.server.js +0 -43
- package/dist/esm/universal/cache.js +0 -1032
- package/dist/esm/universal/request.js +0 -51
- package/dist/esm/url.js +0 -10
- package/dist/esm-node/browser/deferreds.js +0 -138
- package/dist/esm-node/browser/nestedRoutes.js +0 -142
- package/dist/esm-node/merge.js +0 -29
- package/dist/esm-node/node/fileReader.js +0 -68
- package/dist/esm-node/node/loaderContext/createLoaderCtx.js +0 -19
- package/dist/esm-node/node/loaderContext/createRequestCtx.js +0 -24
- package/dist/esm-node/node/router.js +0 -1
- package/dist/esm-node/node/serialize.js +0 -9
- package/dist/esm-node/node/storer/container.js +0 -44
- package/dist/esm-node/node/storer/storage.js +0 -73
- package/dist/esm-node/node/stream.js +0 -80
- package/dist/esm-node/parsed.js +0 -20
- package/dist/esm-node/remixRouter.js +0 -7
- package/dist/esm-node/router.js +0 -1
- package/dist/esm-node/server/nestedRoutes.js +0 -21
- package/dist/esm-node/time.js +0 -31
- package/dist/esm-node/universal/async_storage.js +0 -18
- package/dist/esm-node/universal/async_storage.server.js +0 -45
- package/dist/esm-node/universal/cache.js +0 -401
- package/dist/esm-node/universal/request.js +0 -50
- package/dist/esm-node/url.js +0 -10
- package/dist/types/node/router.d.ts +0 -1
- package/dist/types/remixRouter.d.ts +0 -2
- /package/dist/{esm-node/browser/index.js → esm/browser/index.mjs} +0 -0
- /package/dist/{esm-node/server/index.js → esm/server/index.mjs} +0 -0
package/dist/esm-node/parsed.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const parsedJSONFromElement = (id) => {
|
|
2
|
-
const elements = document.querySelectorAll(`#${id}`);
|
|
3
|
-
if (elements.length === 0) {
|
|
4
|
-
return void 0;
|
|
5
|
-
}
|
|
6
|
-
const element = elements[elements.length - 1];
|
|
7
|
-
if (element) {
|
|
8
|
-
try {
|
|
9
|
-
const parsed = JSON.parse((element === null || element === void 0 ? void 0 : element.textContent) || "");
|
|
10
|
-
return parsed;
|
|
11
|
-
} catch (e) {
|
|
12
|
-
console.error(`parse ${id} error`, e);
|
|
13
|
-
return void 0;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return void 0;
|
|
17
|
-
};
|
|
18
|
-
export {
|
|
19
|
-
parsedJSONFromElement
|
|
20
|
-
};
|
package/dist/esm-node/router.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "react-router-dom";
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
function sortByUrlPath(entries) {
|
|
2
|
-
entries.sort(function(a, b) {
|
|
3
|
-
const length1 = a.urlPath.length;
|
|
4
|
-
const length2 = b.urlPath.length;
|
|
5
|
-
if (length1 < length2) {
|
|
6
|
-
return 1;
|
|
7
|
-
}
|
|
8
|
-
if (length1 > length2) {
|
|
9
|
-
return -1;
|
|
10
|
-
}
|
|
11
|
-
return 0;
|
|
12
|
-
});
|
|
13
|
-
return entries;
|
|
14
|
-
}
|
|
15
|
-
const matchEntry = (pathname, entries) => {
|
|
16
|
-
sortByUrlPath(entries);
|
|
17
|
-
return entries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
18
|
-
};
|
|
19
|
-
export {
|
|
20
|
-
matchEntry
|
|
21
|
-
};
|
package/dist/esm-node/time.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
function processHrtime(previousTimestamp) {
|
|
2
|
-
const now = (/* @__PURE__ */ new Date()).getTime();
|
|
3
|
-
const clocktime = now * 1e-3;
|
|
4
|
-
let seconds = Math.floor(clocktime);
|
|
5
|
-
let nanoseconds = Math.floor(clocktime % 1 * 1e9);
|
|
6
|
-
if (previousTimestamp) {
|
|
7
|
-
seconds -= previousTimestamp[0];
|
|
8
|
-
nanoseconds -= previousTimestamp[1];
|
|
9
|
-
if (nanoseconds < 0) {
|
|
10
|
-
seconds--;
|
|
11
|
-
nanoseconds += 1e9;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return [
|
|
15
|
-
seconds,
|
|
16
|
-
nanoseconds
|
|
17
|
-
];
|
|
18
|
-
}
|
|
19
|
-
const getLatency = (hrtime) => {
|
|
20
|
-
const [s, ns] = processHrtime(hrtime);
|
|
21
|
-
return s * 1e3 + ns / 1e6;
|
|
22
|
-
};
|
|
23
|
-
const time = () => {
|
|
24
|
-
const hrtime = processHrtime();
|
|
25
|
-
return () => {
|
|
26
|
-
return getLatency(hrtime);
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
export {
|
|
30
|
-
time
|
|
31
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
2
|
-
const getAsyncLocalStorage = () => {
|
|
3
|
-
if (isBrowser) {
|
|
4
|
-
console.error("You should not get async storage in browser");
|
|
5
|
-
return null;
|
|
6
|
-
} else {
|
|
7
|
-
try {
|
|
8
|
-
const serverStorage = require("./async_storage.server");
|
|
9
|
-
return serverStorage.getAsyncLocalStorage();
|
|
10
|
-
} catch (err) {
|
|
11
|
-
console.error("Failed to load server async storage", err);
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export {
|
|
17
|
-
getAsyncLocalStorage
|
|
18
|
-
};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import * as ah from "async_hooks";
|
|
2
|
-
const createStorage = () => {
|
|
3
|
-
let storage2;
|
|
4
|
-
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
5
|
-
storage2 = new ah.AsyncLocalStorage();
|
|
6
|
-
}
|
|
7
|
-
const run = (context, cb) => {
|
|
8
|
-
if (!storage2) {
|
|
9
|
-
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
10
|
-
`);
|
|
11
|
-
}
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
13
|
-
storage2.run(context, () => {
|
|
14
|
-
try {
|
|
15
|
-
return resolve(cb());
|
|
16
|
-
} catch (error) {
|
|
17
|
-
return reject(error);
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
const useContext = () => {
|
|
23
|
-
if (!storage2) {
|
|
24
|
-
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
25
|
-
`);
|
|
26
|
-
}
|
|
27
|
-
const context = storage2 === null || storage2 === void 0 ? void 0 : storage2.getStore();
|
|
28
|
-
if (!context) {
|
|
29
|
-
throw new Error(`Can't call useContext out of scope, make sure @modern-js/runtime-utils is a single version in node_modules`);
|
|
30
|
-
}
|
|
31
|
-
return context;
|
|
32
|
-
};
|
|
33
|
-
return {
|
|
34
|
-
run,
|
|
35
|
-
useContext
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
const storage = createStorage();
|
|
39
|
-
const getAsyncLocalStorage = () => {
|
|
40
|
-
return storage;
|
|
41
|
-
};
|
|
42
|
-
export {
|
|
43
|
-
getAsyncLocalStorage,
|
|
44
|
-
storage
|
|
45
|
-
};
|
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import { LRUCache } from "lru-cache";
|
|
2
|
-
import { getAsyncLocalStorage } from "./async_storage";
|
|
3
|
-
const CacheSize = {
|
|
4
|
-
KB: 1024,
|
|
5
|
-
MB: 1024 * 1024,
|
|
6
|
-
GB: 1024 * 1024 * 1024
|
|
7
|
-
};
|
|
8
|
-
const CacheTime = {
|
|
9
|
-
SECOND: 1e3,
|
|
10
|
-
MINUTE: 60 * 1e3,
|
|
11
|
-
HOUR: 60 * 60 * 1e3,
|
|
12
|
-
DAY: 24 * 60 * 60 * 1e3,
|
|
13
|
-
WEEK: 7 * 24 * 60 * 60 * 1e3,
|
|
14
|
-
MONTH: 30 * 24 * 60 * 60 * 1e3
|
|
15
|
-
};
|
|
16
|
-
function estimateObjectSize(data) {
|
|
17
|
-
const type = typeof data;
|
|
18
|
-
if (type === "number")
|
|
19
|
-
return 8;
|
|
20
|
-
if (type === "boolean")
|
|
21
|
-
return 4;
|
|
22
|
-
if (type === "string")
|
|
23
|
-
return Math.max(data.length * 2, 1);
|
|
24
|
-
if (data === null || data === void 0)
|
|
25
|
-
return 1;
|
|
26
|
-
if (ArrayBuffer.isView(data)) {
|
|
27
|
-
return Math.max(data.byteLength, 1);
|
|
28
|
-
}
|
|
29
|
-
if (Array.isArray(data)) {
|
|
30
|
-
return Math.max(data.reduce((acc, item) => acc + estimateObjectSize(item), 0), 1);
|
|
31
|
-
}
|
|
32
|
-
if (data instanceof Map || data instanceof Set) {
|
|
33
|
-
return 1024;
|
|
34
|
-
}
|
|
35
|
-
if (data instanceof Date) {
|
|
36
|
-
return 8;
|
|
37
|
-
}
|
|
38
|
-
if (type === "object") {
|
|
39
|
-
return Math.max(Object.entries(data).reduce((acc, [key, value]) => acc + key.length * 2 + estimateObjectSize(value), 0), 1);
|
|
40
|
-
}
|
|
41
|
-
return 1;
|
|
42
|
-
}
|
|
43
|
-
class MemoryContainer {
|
|
44
|
-
async get(key) {
|
|
45
|
-
return this.lru.get(key);
|
|
46
|
-
}
|
|
47
|
-
async set(key, value, options) {
|
|
48
|
-
if (options === null || options === void 0 ? void 0 : options.ttl) {
|
|
49
|
-
this.lru.set(key, value, {
|
|
50
|
-
ttl: options.ttl * 1e3
|
|
51
|
-
});
|
|
52
|
-
} else {
|
|
53
|
-
this.lru.set(key, value);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async has(key) {
|
|
57
|
-
return this.lru.has(key);
|
|
58
|
-
}
|
|
59
|
-
async delete(key) {
|
|
60
|
-
return this.lru.delete(key);
|
|
61
|
-
}
|
|
62
|
-
async clear() {
|
|
63
|
-
this.lru.clear();
|
|
64
|
-
}
|
|
65
|
-
constructor(options) {
|
|
66
|
-
var _options_maxSize;
|
|
67
|
-
this.lru = new LRUCache({
|
|
68
|
-
maxSize: (_options_maxSize = options === null || options === void 0 ? void 0 : options.maxSize) !== null && _options_maxSize !== void 0 ? _options_maxSize : CacheSize.GB,
|
|
69
|
-
sizeCalculation: estimateObjectSize,
|
|
70
|
-
updateAgeOnGet: true,
|
|
71
|
-
updateAgeOnHas: true
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const isServer = typeof window === "undefined";
|
|
76
|
-
const requestCacheMap = /* @__PURE__ */ new WeakMap();
|
|
77
|
-
const TAG_PREFIX = "tag:";
|
|
78
|
-
const CACHE_PREFIX = "modernjs_cache:";
|
|
79
|
-
const ongoingRevalidations = /* @__PURE__ */ new Map();
|
|
80
|
-
let storage;
|
|
81
|
-
let cacheConfig = {
|
|
82
|
-
maxSize: CacheSize.GB
|
|
83
|
-
};
|
|
84
|
-
function getStorage() {
|
|
85
|
-
if (storage) {
|
|
86
|
-
return storage;
|
|
87
|
-
}
|
|
88
|
-
if (cacheConfig.container) {
|
|
89
|
-
storage = cacheConfig.container;
|
|
90
|
-
} else {
|
|
91
|
-
storage = new MemoryContainer({
|
|
92
|
-
maxSize: cacheConfig.maxSize
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
return storage;
|
|
96
|
-
}
|
|
97
|
-
function configureCache(config) {
|
|
98
|
-
cacheConfig = {
|
|
99
|
-
...cacheConfig,
|
|
100
|
-
...config
|
|
101
|
-
};
|
|
102
|
-
storage = void 0;
|
|
103
|
-
}
|
|
104
|
-
function generateKey(args) {
|
|
105
|
-
return JSON.stringify(args, (_, value) => {
|
|
106
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
107
|
-
return Object.keys(value).sort().reduce((result, key) => {
|
|
108
|
-
result[key] = value[key];
|
|
109
|
-
return result;
|
|
110
|
-
}, {});
|
|
111
|
-
}
|
|
112
|
-
return value;
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function generateStableFunctionId(fn) {
|
|
116
|
-
const fnString = fn.toString();
|
|
117
|
-
let hash = 0;
|
|
118
|
-
for (let i = 0; i < fnString.length; i++) {
|
|
119
|
-
const char = fnString.charCodeAt(i);
|
|
120
|
-
hash = (hash << 5) - hash + char;
|
|
121
|
-
hash = hash & hash;
|
|
122
|
-
}
|
|
123
|
-
return `fn_${fn.name || "anonymous"}_${Math.abs(hash).toString(36)}`;
|
|
124
|
-
}
|
|
125
|
-
function cache(fn, options) {
|
|
126
|
-
return async (...args) => {
|
|
127
|
-
if (isServer && typeof options === "undefined") {
|
|
128
|
-
var _storage_useContext;
|
|
129
|
-
const storage2 = getAsyncLocalStorage();
|
|
130
|
-
const request = storage2 === null || storage2 === void 0 ? void 0 : (_storage_useContext = storage2.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
|
|
131
|
-
if (request) {
|
|
132
|
-
let shouldDisableCaching = false;
|
|
133
|
-
if (cacheConfig.unstable_shouldDisable) {
|
|
134
|
-
shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
135
|
-
request
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
if (shouldDisableCaching) {
|
|
139
|
-
return fn(...args);
|
|
140
|
-
}
|
|
141
|
-
let requestCache = requestCacheMap.get(request);
|
|
142
|
-
if (!requestCache) {
|
|
143
|
-
requestCache = /* @__PURE__ */ new Map();
|
|
144
|
-
requestCacheMap.set(request, requestCache);
|
|
145
|
-
}
|
|
146
|
-
let fnCache = requestCache.get(fn);
|
|
147
|
-
if (!fnCache) {
|
|
148
|
-
fnCache = /* @__PURE__ */ new Map();
|
|
149
|
-
requestCache.set(fn, fnCache);
|
|
150
|
-
}
|
|
151
|
-
const key = generateKey(args);
|
|
152
|
-
if (fnCache.has(key)) {
|
|
153
|
-
return fnCache.get(key);
|
|
154
|
-
}
|
|
155
|
-
const promise = fn(...args);
|
|
156
|
-
fnCache.set(key, promise);
|
|
157
|
-
try {
|
|
158
|
-
const data = await promise;
|
|
159
|
-
return data;
|
|
160
|
-
} catch (error) {
|
|
161
|
-
fnCache.delete(key);
|
|
162
|
-
throw error;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
} else if (typeof options !== "undefined") {
|
|
166
|
-
try {
|
|
167
|
-
const { tag, maxAge = CacheTime.MINUTE * 5, revalidate = 0, customKey, onCache, getKey, unstable_shouldCache } = options;
|
|
168
|
-
let missReason;
|
|
169
|
-
const currentStorage = getStorage();
|
|
170
|
-
const now = Date.now();
|
|
171
|
-
const tags = tag ? Array.isArray(tag) ? tag : [
|
|
172
|
-
tag
|
|
173
|
-
] : [];
|
|
174
|
-
const genKey = getKey ? getKey(...args) : generateKey(args);
|
|
175
|
-
let finalKey;
|
|
176
|
-
if (customKey) {
|
|
177
|
-
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
|
-
var _asyncStorage_useContext;
|
|
190
|
-
const asyncStorage = getAsyncLocalStorage();
|
|
191
|
-
const request = asyncStorage === null || asyncStorage === void 0 ? void 0 : (_asyncStorage_useContext = asyncStorage.useContext()) === null || _asyncStorage_useContext === void 0 ? void 0 : _asyncStorage_useContext.request;
|
|
192
|
-
if (request) {
|
|
193
|
-
shouldDisableCaching = await cacheConfig.unstable_shouldDisable({
|
|
194
|
-
request
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (!shouldDisableCaching) {
|
|
199
|
-
const cached = await currentStorage.get(storageKey);
|
|
200
|
-
if (cached) {
|
|
201
|
-
try {
|
|
202
|
-
const cacheItem = cached;
|
|
203
|
-
const age = now - cacheItem.timestamp;
|
|
204
|
-
if (age < maxAge) {
|
|
205
|
-
onCache === null || onCache === void 0 ? void 0 : onCache({
|
|
206
|
-
status: "hit",
|
|
207
|
-
key: finalKey,
|
|
208
|
-
params: args,
|
|
209
|
-
result: cacheItem.data
|
|
210
|
-
});
|
|
211
|
-
return cacheItem.data;
|
|
212
|
-
}
|
|
213
|
-
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
214
|
-
onCache === null || onCache === void 0 ? void 0 : onCache({
|
|
215
|
-
status: "stale",
|
|
216
|
-
key: finalKey,
|
|
217
|
-
params: args,
|
|
218
|
-
result: cacheItem.data
|
|
219
|
-
});
|
|
220
|
-
if (!ongoingRevalidations.has(storageKey)) {
|
|
221
|
-
const revalidationPromise = (async () => {
|
|
222
|
-
try {
|
|
223
|
-
const newData = await fn(...args);
|
|
224
|
-
let shouldCache = true;
|
|
225
|
-
if (unstable_shouldCache) {
|
|
226
|
-
shouldCache = await unstable_shouldCache({
|
|
227
|
-
params: args,
|
|
228
|
-
result: newData
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
if (shouldCache) {
|
|
232
|
-
await setCacheItem(currentStorage, storageKey, newData, tags, maxAge, revalidate);
|
|
233
|
-
}
|
|
234
|
-
} catch (error) {
|
|
235
|
-
if (isServer) {
|
|
236
|
-
var _asyncStorage_useContext_monitors, _asyncStorage_useContext2;
|
|
237
|
-
const asyncStorage = getAsyncLocalStorage();
|
|
238
|
-
asyncStorage === null || asyncStorage === void 0 ? void 0 : (_asyncStorage_useContext2 = asyncStorage.useContext()) === null || _asyncStorage_useContext2 === void 0 ? void 0 : (_asyncStorage_useContext_monitors = _asyncStorage_useContext2.monitors) === null || _asyncStorage_useContext_monitors === void 0 ? void 0 : _asyncStorage_useContext_monitors.error(error.message);
|
|
239
|
-
} else {
|
|
240
|
-
console.error("Background revalidation failed:", error);
|
|
241
|
-
}
|
|
242
|
-
} finally {
|
|
243
|
-
ongoingRevalidations.delete(storageKey);
|
|
244
|
-
}
|
|
245
|
-
})();
|
|
246
|
-
ongoingRevalidations.set(storageKey, revalidationPromise);
|
|
247
|
-
}
|
|
248
|
-
return cacheItem.data;
|
|
249
|
-
}
|
|
250
|
-
missReason = 3;
|
|
251
|
-
} catch (error) {
|
|
252
|
-
console.warn("Failed to parse cached data:", error);
|
|
253
|
-
missReason = 4;
|
|
254
|
-
}
|
|
255
|
-
} else {
|
|
256
|
-
missReason = 2;
|
|
257
|
-
}
|
|
258
|
-
} else {
|
|
259
|
-
missReason = 1;
|
|
260
|
-
}
|
|
261
|
-
const data = await fn(...args);
|
|
262
|
-
if (!shouldDisableCaching) {
|
|
263
|
-
let shouldCache = true;
|
|
264
|
-
if (unstable_shouldCache) {
|
|
265
|
-
shouldCache = await unstable_shouldCache({
|
|
266
|
-
params: args,
|
|
267
|
-
result: data
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
if (shouldCache) {
|
|
271
|
-
await setCacheItem(currentStorage, storageKey, data, tags, maxAge, revalidate);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
onCache === null || onCache === void 0 ? void 0 : onCache({
|
|
275
|
-
status: "miss",
|
|
276
|
-
key: finalKey,
|
|
277
|
-
params: args,
|
|
278
|
-
result: data,
|
|
279
|
-
reason: missReason
|
|
280
|
-
});
|
|
281
|
-
return data;
|
|
282
|
-
} catch (error) {
|
|
283
|
-
console.warn("Cache operation failed, falling back to direct execution:", error);
|
|
284
|
-
const data = await fn(...args);
|
|
285
|
-
const { onCache } = options;
|
|
286
|
-
try {
|
|
287
|
-
onCache === null || onCache === void 0 ? void 0 : onCache({
|
|
288
|
-
status: "miss",
|
|
289
|
-
key: "cache_failed",
|
|
290
|
-
params: args,
|
|
291
|
-
result: data,
|
|
292
|
-
reason: 5
|
|
293
|
-
});
|
|
294
|
-
} catch (callbackError) {
|
|
295
|
-
console.warn("Failed to call onCache callback:", callbackError);
|
|
296
|
-
}
|
|
297
|
-
return data;
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
|
|
301
|
-
return fn(...args);
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
async function setCacheItem(storage2, storageKey, data, tags, maxAge, revalidate) {
|
|
306
|
-
const newItem = {
|
|
307
|
-
data,
|
|
308
|
-
timestamp: Date.now(),
|
|
309
|
-
tags: tags.length > 0 ? tags : void 0
|
|
310
|
-
};
|
|
311
|
-
const ttl = (maxAge + revalidate) / 1e3;
|
|
312
|
-
await storage2.set(storageKey, newItem, {
|
|
313
|
-
ttl: ttl > 0 ? ttl : void 0
|
|
314
|
-
});
|
|
315
|
-
await updateTagRelationships(storage2, storageKey, tags);
|
|
316
|
-
}
|
|
317
|
-
async function updateTagRelationships(storage2, storageKey, tags) {
|
|
318
|
-
for (const tag of tags) {
|
|
319
|
-
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
320
|
-
const keyList = await storage2.get(tagStoreKey);
|
|
321
|
-
const keyArray = keyList || [];
|
|
322
|
-
if (!keyArray.includes(storageKey)) {
|
|
323
|
-
keyArray.push(storageKey);
|
|
324
|
-
}
|
|
325
|
-
await storage2.set(tagStoreKey, keyArray);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
async function removeKeyFromTags(storage2, storageKey, tags) {
|
|
329
|
-
for (const tag of tags) {
|
|
330
|
-
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
331
|
-
const keyList = await storage2.get(tagStoreKey);
|
|
332
|
-
if (keyList) {
|
|
333
|
-
try {
|
|
334
|
-
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
335
|
-
const updatedKeyList = keyArray.filter((key) => key !== storageKey);
|
|
336
|
-
if (updatedKeyList.length > 0) {
|
|
337
|
-
await storage2.set(tagStoreKey, updatedKeyList);
|
|
338
|
-
} else {
|
|
339
|
-
await storage2.delete(tagStoreKey);
|
|
340
|
-
}
|
|
341
|
-
} catch (error) {
|
|
342
|
-
console.warn(`Failed to process tag key list for tag ${tag}:`, error);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
function withRequestCache(handler) {
|
|
348
|
-
if (!isServer) {
|
|
349
|
-
return handler;
|
|
350
|
-
}
|
|
351
|
-
return async (req, ...args) => {
|
|
352
|
-
const storage2 = getAsyncLocalStorage();
|
|
353
|
-
return storage2.run({
|
|
354
|
-
request: req
|
|
355
|
-
}, () => handler(req, ...args));
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
async function revalidateTag(tag) {
|
|
359
|
-
const currentStorage = getStorage();
|
|
360
|
-
const tagStoreKey = `${TAG_PREFIX}${tag}`;
|
|
361
|
-
const keyList = await currentStorage.get(tagStoreKey);
|
|
362
|
-
if (keyList) {
|
|
363
|
-
try {
|
|
364
|
-
const keyArray = Array.isArray(keyList) ? keyList : [];
|
|
365
|
-
for (const cacheKey of keyArray) {
|
|
366
|
-
const cached = await currentStorage.get(cacheKey);
|
|
367
|
-
if (cached) {
|
|
368
|
-
try {
|
|
369
|
-
const cacheItem = cached;
|
|
370
|
-
if (cacheItem.tags) {
|
|
371
|
-
const otherTags = cacheItem.tags.filter((t) => t !== tag);
|
|
372
|
-
await removeKeyFromTags(currentStorage, cacheKey, otherTags);
|
|
373
|
-
}
|
|
374
|
-
} catch (error) {
|
|
375
|
-
console.warn("Failed to parse cached data while revalidating:", error);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
await currentStorage.delete(cacheKey);
|
|
379
|
-
}
|
|
380
|
-
await currentStorage.delete(tagStoreKey);
|
|
381
|
-
} catch (error) {
|
|
382
|
-
console.warn("Failed to process tag key list:", error);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
async function clearStore() {
|
|
387
|
-
const currentStorage = getStorage();
|
|
388
|
-
await currentStorage.clear();
|
|
389
|
-
storage = void 0;
|
|
390
|
-
ongoingRevalidations.clear();
|
|
391
|
-
}
|
|
392
|
-
export {
|
|
393
|
-
CacheSize,
|
|
394
|
-
CacheTime,
|
|
395
|
-
cache,
|
|
396
|
-
clearStore,
|
|
397
|
-
configureCache,
|
|
398
|
-
generateKey,
|
|
399
|
-
revalidateTag,
|
|
400
|
-
withRequestCache
|
|
401
|
-
};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
function parseQuery(req) {
|
|
2
|
-
const query = {};
|
|
3
|
-
const { url } = req;
|
|
4
|
-
const q = url.split("?")[1];
|
|
5
|
-
if (q) {
|
|
6
|
-
const search = new URLSearchParams(q);
|
|
7
|
-
search.forEach((v, k) => {
|
|
8
|
-
query[k] = v;
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
return query;
|
|
12
|
-
}
|
|
13
|
-
function parseHeaders(request) {
|
|
14
|
-
const headersData = {};
|
|
15
|
-
request.headers.forEach((value, key) => {
|
|
16
|
-
headersData[key] = value;
|
|
17
|
-
});
|
|
18
|
-
return headersData;
|
|
19
|
-
}
|
|
20
|
-
function getPathname(request) {
|
|
21
|
-
const match = request.url.match(/^https?:\/\/[^/]+(\/[^?]*)/);
|
|
22
|
-
return match ? match[1] : "/";
|
|
23
|
-
}
|
|
24
|
-
function getHost(request) {
|
|
25
|
-
const { headers } = request;
|
|
26
|
-
let host = headers.get("X-Forwarded-Host");
|
|
27
|
-
if (!host) {
|
|
28
|
-
host = headers.get("Host");
|
|
29
|
-
}
|
|
30
|
-
host = (host === null || host === void 0 ? void 0 : host.split(/\s*,\s*/, 1)[0]) || "undefined";
|
|
31
|
-
return host;
|
|
32
|
-
}
|
|
33
|
-
function parseCookie(req) {
|
|
34
|
-
const _cookie = req.headers.get("Cookie");
|
|
35
|
-
const cookie = {};
|
|
36
|
-
_cookie === null || _cookie === void 0 ? void 0 : _cookie.trim().split(";").forEach((item) => {
|
|
37
|
-
const [k, v] = item.trim().split("=");
|
|
38
|
-
if (k) {
|
|
39
|
-
cookie[k] = v;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
return cookie;
|
|
43
|
-
}
|
|
44
|
-
export {
|
|
45
|
-
getHost,
|
|
46
|
-
getPathname,
|
|
47
|
-
parseCookie,
|
|
48
|
-
parseHeaders,
|
|
49
|
-
parseQuery
|
|
50
|
-
};
|
package/dist/esm-node/url.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from 'react-router-dom/server';
|
|
File without changes
|
|
File without changes
|