@modern-js/runtime-utils 2.65.0 → 2.65.2
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/nestedRoutes.js +11 -11
- package/dist/cjs/node/index.js +3 -6
- package/dist/cjs/node/storer/container.js +8 -15
- package/dist/cjs/universal/async_storage.js +31 -0
- package/dist/cjs/{node/storage.js → universal/async_storage.server.js} +18 -12
- package/dist/cjs/universal/cache.js +247 -0
- package/dist/esm/browser/nestedRoutes.js +12 -12
- package/dist/esm/node/index.js +2 -4
- package/dist/esm/node/storer/container.js +8 -5
- package/dist/esm/universal/async_storage.js +7 -0
- package/dist/esm/{node/storage.js → universal/async_storage.server.js} +13 -8
- package/dist/esm/universal/cache.js +355 -0
- package/dist/esm-node/browser/nestedRoutes.js +11 -11
- package/dist/esm-node/node/index.js +2 -4
- package/dist/esm-node/node/storer/container.js +8 -5
- package/dist/esm-node/universal/async_storage.js +7 -0
- package/dist/esm-node/{node/storage.js → universal/async_storage.server.js} +13 -8
- package/dist/esm-node/universal/cache.js +216 -0
- package/dist/types/browser/nestedRoutes.d.ts +2 -3
- package/dist/types/node/index.d.ts +1 -4
- package/dist/types/node/loaderContext/index.d.ts +1 -13
- package/dist/types/node/storer/container.d.ts +1 -1
- package/dist/types/node/storer/index.d.ts +1 -1
- package/dist/types/server/nestedRoutes.d.ts +1 -2
- package/dist/types/universal/async_storage.d.ts +2 -0
- package/dist/types/universal/async_storage.server.d.ts +18 -0
- package/dist/types/universal/cache.d.ts +28 -0
- package/package.json +14 -8
- package/dist/types/node/storage.d.ts +0 -5
|
@@ -27,12 +27,11 @@ var import_constants = require("@modern-js/utils/universal/constants");
|
|
|
27
27
|
var import_react = require("react");
|
|
28
28
|
var import_react_router_dom = require("react-router-dom");
|
|
29
29
|
var import_time = require("../time");
|
|
30
|
-
|
|
30
|
+
var import_async_storage = require("../universal/async_storage");
|
|
31
|
+
const transformNestedRoutes = (routes) => {
|
|
31
32
|
const routeElements = [];
|
|
32
33
|
for (const route of routes) {
|
|
33
|
-
const routeElement = renderNestedRoute(route
|
|
34
|
-
reporter
|
|
35
|
-
});
|
|
34
|
+
const routeElement = renderNestedRoute(route);
|
|
36
35
|
routeElements.push(routeElement);
|
|
37
36
|
}
|
|
38
37
|
return (0, import_react_router_dom.createRoutesFromElements)(routeElements);
|
|
@@ -40,12 +39,12 @@ const transformNestedRoutes = (routes, reporter) => {
|
|
|
40
39
|
const renderNestedRoute = (nestedRoute, options = {}) => {
|
|
41
40
|
const { children, index, id, component, isRoot, lazyImport, config, handle } = nestedRoute;
|
|
42
41
|
const Component = component;
|
|
43
|
-
const { parent, props = {}
|
|
42
|
+
const { parent, props = {} } = options;
|
|
44
43
|
const routeProps = {
|
|
45
44
|
caseSensitive: nestedRoute.caseSensitive,
|
|
46
45
|
path: nestedRoute.path,
|
|
47
46
|
id: nestedRoute.id,
|
|
48
|
-
loader: createLoader(nestedRoute
|
|
47
|
+
loader: createLoader(nestedRoute),
|
|
49
48
|
action: nestedRoute.action,
|
|
50
49
|
hasErrorBoundary: nestedRoute.hasErrorBoundary,
|
|
51
50
|
shouldRevalidate: nestedRoute.shouldRevalidate,
|
|
@@ -98,8 +97,7 @@ const renderNestedRoute = (nestedRoute, options = {}) => {
|
|
|
98
97
|
}
|
|
99
98
|
const childElements = children === null || children === void 0 ? void 0 : children.map((childRoute) => {
|
|
100
99
|
return renderNestedRoute(childRoute, {
|
|
101
|
-
parent: nestedRoute
|
|
102
|
-
reporter
|
|
100
|
+
parent: nestedRoute
|
|
103
101
|
});
|
|
104
102
|
});
|
|
105
103
|
const routeElement = index ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router_dom.Route, {
|
|
@@ -112,7 +110,7 @@ const renderNestedRoute = (nestedRoute, options = {}) => {
|
|
|
112
110
|
}, id);
|
|
113
111
|
return routeElement;
|
|
114
112
|
};
|
|
115
|
-
function createLoader(route
|
|
113
|
+
function createLoader(route) {
|
|
116
114
|
const { loader } = route;
|
|
117
115
|
if (loader) {
|
|
118
116
|
return async (args) => {
|
|
@@ -122,8 +120,10 @@ function createLoader(route, reporter) {
|
|
|
122
120
|
const end = (0, import_time.time)();
|
|
123
121
|
const res = await loader(args);
|
|
124
122
|
const cost = end();
|
|
125
|
-
if (typeof document === "undefined"
|
|
126
|
-
|
|
123
|
+
if (typeof document === "undefined") {
|
|
124
|
+
var _storage_useContext_monitors;
|
|
125
|
+
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
126
|
+
storage === null || storage === void 0 ? void 0 : (_storage_useContext_monitors = storage.useContext().monitors) === null || _storage_useContext_monitors === void 0 ? void 0 : _storage_useContext_monitors.timing(`${import_constants.LOADER_REPORTER_NAME}-${route.id}`, cost);
|
|
127
127
|
}
|
|
128
128
|
return res;
|
|
129
129
|
};
|
package/dist/cjs/node/index.js
CHANGED
|
@@ -19,21 +19,18 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
|
|
|
19
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
20
|
var node_exports = {};
|
|
21
21
|
__export(node_exports, {
|
|
22
|
-
run: () => run,
|
|
23
22
|
serializeJson: () => import_serialize.serializeJson,
|
|
24
|
-
|
|
23
|
+
storage: () => import_async_storage.storage
|
|
25
24
|
});
|
|
26
25
|
module.exports = __toCommonJS(node_exports);
|
|
27
|
-
var
|
|
26
|
+
var import_async_storage = require("../universal/async_storage.server");
|
|
28
27
|
var import_serialize = require("./serialize");
|
|
29
28
|
__reExport(node_exports, require("./loaderContext"), module.exports);
|
|
30
29
|
__reExport(node_exports, require("./stream"), module.exports);
|
|
31
|
-
const { run, useContext: useHeaders } = (0, import_storage.createStorage)();
|
|
32
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
31
|
0 && (module.exports = {
|
|
34
|
-
run,
|
|
35
32
|
serializeJson,
|
|
36
|
-
|
|
33
|
+
storage,
|
|
37
34
|
...require("./loaderContext"),
|
|
38
35
|
...require("./stream")
|
|
39
36
|
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,21 +15,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var container_exports = {};
|
|
30
20
|
__export(container_exports, {
|
|
31
21
|
MemoryContainer: () => MemoryContainer
|
|
32
22
|
});
|
|
33
23
|
module.exports = __toCommonJS(container_exports);
|
|
34
|
-
var import_lru_cache =
|
|
24
|
+
var import_lru_cache = require("lru-cache");
|
|
35
25
|
class MemoryContainer {
|
|
36
26
|
async get(key) {
|
|
37
27
|
return this.cache.get(key);
|
|
@@ -46,7 +36,7 @@ class MemoryContainer {
|
|
|
46
36
|
async delete(key) {
|
|
47
37
|
const exist = await this.has(key);
|
|
48
38
|
if (exist) {
|
|
49
|
-
this.cache.
|
|
39
|
+
this.cache.delete(key);
|
|
50
40
|
}
|
|
51
41
|
return exist;
|
|
52
42
|
}
|
|
@@ -56,9 +46,12 @@ class MemoryContainer {
|
|
|
56
46
|
});
|
|
57
47
|
}
|
|
58
48
|
constructor({ max, maxAge } = {}) {
|
|
59
|
-
this.cache = new import_lru_cache.
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
this.cache = new import_lru_cache.LRUCache({
|
|
50
|
+
maxSize: (max || 256) * MemoryContainer.MB,
|
|
51
|
+
ttl: maxAge || MemoryContainer.hour,
|
|
52
|
+
sizeCalculation: (value, key) => {
|
|
53
|
+
return JSON.stringify(value).length;
|
|
54
|
+
}
|
|
62
55
|
});
|
|
63
56
|
}
|
|
64
57
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var async_storage_exports = {};
|
|
20
|
+
__export(async_storage_exports, {
|
|
21
|
+
getAsyncLocalStorage: () => getAsyncLocalStorage
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(async_storage_exports);
|
|
24
|
+
const getAsyncLocalStorage = () => {
|
|
25
|
+
console.error("You should not get async storage in browser");
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
getAsyncLocalStorage
|
|
31
|
+
});
|
|
@@ -26,24 +26,25 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
mod
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var
|
|
30
|
-
__export(
|
|
31
|
-
|
|
29
|
+
var async_storage_server_exports = {};
|
|
30
|
+
__export(async_storage_server_exports, {
|
|
31
|
+
getAsyncLocalStorage: () => getAsyncLocalStorage,
|
|
32
|
+
storage: () => storage
|
|
32
33
|
});
|
|
33
|
-
module.exports = __toCommonJS(
|
|
34
|
+
module.exports = __toCommonJS(async_storage_server_exports);
|
|
34
35
|
var ah = __toESM(require("async_hooks"));
|
|
35
36
|
const createStorage = () => {
|
|
36
|
-
let
|
|
37
|
+
let storage2;
|
|
37
38
|
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
38
|
-
|
|
39
|
+
storage2 = new ah.AsyncLocalStorage();
|
|
39
40
|
}
|
|
40
41
|
const run = (context, cb) => {
|
|
41
|
-
if (!
|
|
42
|
+
if (!storage2) {
|
|
42
43
|
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
43
44
|
`);
|
|
44
45
|
}
|
|
45
46
|
return new Promise((resolve, reject) => {
|
|
46
|
-
|
|
47
|
+
storage2.run(context, () => {
|
|
47
48
|
try {
|
|
48
49
|
return resolve(cb());
|
|
49
50
|
} catch (error) {
|
|
@@ -53,13 +54,13 @@ const createStorage = () => {
|
|
|
53
54
|
});
|
|
54
55
|
};
|
|
55
56
|
const useContext = () => {
|
|
56
|
-
if (!
|
|
57
|
+
if (!storage2) {
|
|
57
58
|
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
58
59
|
`);
|
|
59
60
|
}
|
|
60
|
-
const context =
|
|
61
|
+
const context = storage2 === null || storage2 === void 0 ? void 0 : storage2.getStore();
|
|
61
62
|
if (!context) {
|
|
62
|
-
throw new Error(`Can't call useContext out of scope, make sure @modern-js/utils is a single version in node_modules`);
|
|
63
|
+
throw new Error(`Can't call useContext out of scope, make sure @modern-js/runtime-utils is a single version in node_modules`);
|
|
63
64
|
}
|
|
64
65
|
return context;
|
|
65
66
|
};
|
|
@@ -68,7 +69,12 @@ const createStorage = () => {
|
|
|
68
69
|
useContext
|
|
69
70
|
};
|
|
70
71
|
};
|
|
72
|
+
const storage = createStorage();
|
|
73
|
+
const getAsyncLocalStorage = () => {
|
|
74
|
+
return storage;
|
|
75
|
+
};
|
|
71
76
|
// Annotate the CommonJS export names for ESM import in node:
|
|
72
77
|
0 && (module.exports = {
|
|
73
|
-
|
|
78
|
+
getAsyncLocalStorage,
|
|
79
|
+
storage
|
|
74
80
|
});
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var cache_exports = {};
|
|
20
|
+
__export(cache_exports, {
|
|
21
|
+
CacheSize: () => CacheSize,
|
|
22
|
+
CacheTime: () => CacheTime,
|
|
23
|
+
cache: () => cache,
|
|
24
|
+
clearStore: () => clearStore,
|
|
25
|
+
configureCache: () => configureCache,
|
|
26
|
+
generateKey: () => generateKey,
|
|
27
|
+
revalidateTag: () => revalidateTag,
|
|
28
|
+
withRequestCache: () => withRequestCache
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(cache_exports);
|
|
31
|
+
var import_lru_cache = require("lru-cache");
|
|
32
|
+
var import_async_storage = require("./async_storage");
|
|
33
|
+
const CacheSize = {
|
|
34
|
+
KB: 1024,
|
|
35
|
+
MB: 1024 * 1024,
|
|
36
|
+
GB: 1024 * 1024 * 1024
|
|
37
|
+
};
|
|
38
|
+
const CacheTime = {
|
|
39
|
+
SECOND: 1e3,
|
|
40
|
+
MINUTE: 60 * 1e3,
|
|
41
|
+
HOUR: 60 * 60 * 1e3,
|
|
42
|
+
DAY: 24 * 60 * 60 * 1e3,
|
|
43
|
+
WEEK: 7 * 24 * 60 * 60 * 1e3,
|
|
44
|
+
MONTH: 30 * 24 * 60 * 60 * 1e3
|
|
45
|
+
};
|
|
46
|
+
const isServer = typeof window === "undefined";
|
|
47
|
+
const requestCacheMap = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
let lruCache;
|
|
49
|
+
let cacheConfig = {
|
|
50
|
+
maxSize: CacheSize.GB
|
|
51
|
+
};
|
|
52
|
+
const tagFnMap = /* @__PURE__ */ new Map();
|
|
53
|
+
function addTagFnRelation(tag, fn) {
|
|
54
|
+
let fns = tagFnMap.get(tag);
|
|
55
|
+
if (!fns) {
|
|
56
|
+
fns = /* @__PURE__ */ new Set();
|
|
57
|
+
tagFnMap.set(tag, fns);
|
|
58
|
+
}
|
|
59
|
+
fns.add(fn);
|
|
60
|
+
}
|
|
61
|
+
function configureCache(config) {
|
|
62
|
+
cacheConfig = {
|
|
63
|
+
...cacheConfig,
|
|
64
|
+
...config
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getLRUCache() {
|
|
68
|
+
if (!lruCache) {
|
|
69
|
+
lruCache = new import_lru_cache.LRUCache({
|
|
70
|
+
maxSize: cacheConfig.maxSize,
|
|
71
|
+
sizeCalculation: (value) => {
|
|
72
|
+
if (!value.size) {
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
let size = 0;
|
|
76
|
+
for (const [k, item] of value.entries()) {
|
|
77
|
+
size += k.length * 2;
|
|
78
|
+
size += estimateObjectSize(item.data);
|
|
79
|
+
size += 8;
|
|
80
|
+
}
|
|
81
|
+
return size;
|
|
82
|
+
},
|
|
83
|
+
updateAgeOnGet: true,
|
|
84
|
+
updateAgeOnHas: true
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return lruCache;
|
|
88
|
+
}
|
|
89
|
+
function estimateObjectSize(data) {
|
|
90
|
+
const type = typeof data;
|
|
91
|
+
if (type === "number")
|
|
92
|
+
return 8;
|
|
93
|
+
if (type === "boolean")
|
|
94
|
+
return 4;
|
|
95
|
+
if (type === "string")
|
|
96
|
+
return Math.max(data.length * 2, 1);
|
|
97
|
+
if (data === null || data === void 0)
|
|
98
|
+
return 1;
|
|
99
|
+
if (ArrayBuffer.isView(data)) {
|
|
100
|
+
return Math.max(data.byteLength, 1);
|
|
101
|
+
}
|
|
102
|
+
if (Array.isArray(data)) {
|
|
103
|
+
return Math.max(data.reduce((acc, item) => acc + estimateObjectSize(item), 0), 1);
|
|
104
|
+
}
|
|
105
|
+
if (data instanceof Map || data instanceof Set) {
|
|
106
|
+
return 1024;
|
|
107
|
+
}
|
|
108
|
+
if (data instanceof Date) {
|
|
109
|
+
return 8;
|
|
110
|
+
}
|
|
111
|
+
if (type === "object") {
|
|
112
|
+
return Math.max(Object.entries(data).reduce((acc, [key, value]) => acc + key.length * 2 + estimateObjectSize(value), 0), 1);
|
|
113
|
+
}
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
116
|
+
function generateKey(args) {
|
|
117
|
+
return JSON.stringify(args, (_, value) => {
|
|
118
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
119
|
+
return Object.keys(value).sort().reduce((result, key) => {
|
|
120
|
+
result[key] = value[key];
|
|
121
|
+
return result;
|
|
122
|
+
}, {});
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function cache(fn, options) {
|
|
128
|
+
const { tag = "default", maxAge = CacheTime.MINUTE * 5, revalidate = 0 } = options || {};
|
|
129
|
+
const store = getLRUCache();
|
|
130
|
+
const tags = Array.isArray(tag) ? tag : [
|
|
131
|
+
tag
|
|
132
|
+
];
|
|
133
|
+
tags.forEach((t) => addTagFnRelation(t, fn));
|
|
134
|
+
return async (...args) => {
|
|
135
|
+
if (isServer && typeof options === "undefined") {
|
|
136
|
+
var _storage_useContext;
|
|
137
|
+
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
138
|
+
const request = storage === null || storage === void 0 ? void 0 : (_storage_useContext = storage.useContext()) === null || _storage_useContext === void 0 ? void 0 : _storage_useContext.request;
|
|
139
|
+
if (request) {
|
|
140
|
+
let requestCache = requestCacheMap.get(request);
|
|
141
|
+
if (!requestCache) {
|
|
142
|
+
requestCache = /* @__PURE__ */ new Map();
|
|
143
|
+
requestCacheMap.set(request, requestCache);
|
|
144
|
+
}
|
|
145
|
+
const key = generateKey(args);
|
|
146
|
+
if (requestCache.has(key)) {
|
|
147
|
+
return requestCache.get(key);
|
|
148
|
+
}
|
|
149
|
+
const promise = fn(...args);
|
|
150
|
+
requestCache.set(key, promise);
|
|
151
|
+
try {
|
|
152
|
+
const data = await promise;
|
|
153
|
+
return data;
|
|
154
|
+
} catch (error) {
|
|
155
|
+
requestCache.delete(key);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} else if (typeof options !== "undefined") {
|
|
160
|
+
let tagCache = store.get(fn);
|
|
161
|
+
if (!tagCache) {
|
|
162
|
+
tagCache = /* @__PURE__ */ new Map();
|
|
163
|
+
}
|
|
164
|
+
const key = generateKey(args);
|
|
165
|
+
const cached = tagCache.get(key);
|
|
166
|
+
const now = Date.now();
|
|
167
|
+
if (cached) {
|
|
168
|
+
const age = now - cached.timestamp;
|
|
169
|
+
if (age < maxAge) {
|
|
170
|
+
return cached.data;
|
|
171
|
+
}
|
|
172
|
+
if (revalidate > 0 && age < maxAge + revalidate) {
|
|
173
|
+
if (!cached.isRevalidating) {
|
|
174
|
+
cached.isRevalidating = true;
|
|
175
|
+
Promise.resolve().then(async () => {
|
|
176
|
+
try {
|
|
177
|
+
const newData = await fn(...args);
|
|
178
|
+
tagCache.set(key, {
|
|
179
|
+
data: newData,
|
|
180
|
+
timestamp: Date.now(),
|
|
181
|
+
isRevalidating: false
|
|
182
|
+
});
|
|
183
|
+
store.set(fn, tagCache);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
cached.isRevalidating = false;
|
|
186
|
+
if (isServer) {
|
|
187
|
+
var _storage_useContext_monitors, _storage_useContext2;
|
|
188
|
+
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
189
|
+
storage === null || storage === void 0 ? void 0 : (_storage_useContext2 = storage.useContext()) === null || _storage_useContext2 === void 0 ? void 0 : (_storage_useContext_monitors = _storage_useContext2.monitors) === null || _storage_useContext_monitors === void 0 ? void 0 : _storage_useContext_monitors.error(error.message);
|
|
190
|
+
} else {
|
|
191
|
+
console.error("Background revalidation failed:", error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return cached.data;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const data = await fn(...args);
|
|
200
|
+
tagCache.set(key, {
|
|
201
|
+
data,
|
|
202
|
+
timestamp: now,
|
|
203
|
+
isRevalidating: false
|
|
204
|
+
});
|
|
205
|
+
store.set(fn, tagCache);
|
|
206
|
+
return data;
|
|
207
|
+
} else {
|
|
208
|
+
console.warn("The cache function will not work because it runs on the browser and there are no options are provided.");
|
|
209
|
+
return fn(...args);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function withRequestCache(handler) {
|
|
214
|
+
if (!isServer) {
|
|
215
|
+
return handler;
|
|
216
|
+
}
|
|
217
|
+
return async (req, ...args) => {
|
|
218
|
+
const storage = (0, import_async_storage.getAsyncLocalStorage)();
|
|
219
|
+
return storage.run({
|
|
220
|
+
request: req
|
|
221
|
+
}, () => handler(req, ...args));
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function revalidateTag(tag) {
|
|
225
|
+
const fns = tagFnMap.get(tag);
|
|
226
|
+
if (fns) {
|
|
227
|
+
fns.forEach((fn) => {
|
|
228
|
+
lruCache === null || lruCache === void 0 ? void 0 : lruCache.delete(fn);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function clearStore() {
|
|
233
|
+
lruCache === null || lruCache === void 0 ? void 0 : lruCache.clear();
|
|
234
|
+
lruCache = void 0;
|
|
235
|
+
tagFnMap.clear();
|
|
236
|
+
}
|
|
237
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
238
|
+
0 && (module.exports = {
|
|
239
|
+
CacheSize,
|
|
240
|
+
CacheTime,
|
|
241
|
+
cache,
|
|
242
|
+
clearStore,
|
|
243
|
+
configureCache,
|
|
244
|
+
generateKey,
|
|
245
|
+
revalidateTag,
|
|
246
|
+
withRequestCache
|
|
247
|
+
});
|
|
@@ -8,15 +8,14 @@ import { LOADER_REPORTER_NAME } from "@modern-js/utils/universal/constants";
|
|
|
8
8
|
import { Suspense } from "react";
|
|
9
9
|
import { Outlet, Route, createRoutesFromElements } from "react-router-dom";
|
|
10
10
|
import { time } from "../time";
|
|
11
|
-
|
|
11
|
+
import { getAsyncLocalStorage } from "../universal/async_storage";
|
|
12
|
+
var transformNestedRoutes = function(routes) {
|
|
12
13
|
var routeElements = [];
|
|
13
14
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
14
15
|
try {
|
|
15
16
|
for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
16
17
|
var route = _step.value;
|
|
17
|
-
var routeElement = renderNestedRoute(route
|
|
18
|
-
reporter
|
|
19
|
-
});
|
|
18
|
+
var routeElement = renderNestedRoute(route);
|
|
20
19
|
routeElements.push(routeElement);
|
|
21
20
|
}
|
|
22
21
|
} catch (err) {
|
|
@@ -39,12 +38,12 @@ var renderNestedRoute = function(nestedRoute) {
|
|
|
39
38
|
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
40
39
|
var children = nestedRoute.children, index = nestedRoute.index, id = nestedRoute.id, component = nestedRoute.component, isRoot = nestedRoute.isRoot, lazyImport = nestedRoute.lazyImport, config = nestedRoute.config, handle = nestedRoute.handle;
|
|
41
40
|
var Component = component;
|
|
42
|
-
var parent = options.parent, _options_props = options.props, props = _options_props === void 0 ? {} : _options_props
|
|
41
|
+
var parent = options.parent, _options_props = options.props, props = _options_props === void 0 ? {} : _options_props;
|
|
43
42
|
var routeProps = {
|
|
44
43
|
caseSensitive: nestedRoute.caseSensitive,
|
|
45
44
|
path: nestedRoute.path,
|
|
46
45
|
id: nestedRoute.id,
|
|
47
|
-
loader: createLoader(nestedRoute
|
|
46
|
+
loader: createLoader(nestedRoute),
|
|
48
47
|
action: nestedRoute.action,
|
|
49
48
|
hasErrorBoundary: nestedRoute.hasErrorBoundary,
|
|
50
49
|
shouldRevalidate: nestedRoute.shouldRevalidate,
|
|
@@ -92,8 +91,7 @@ var renderNestedRoute = function(nestedRoute) {
|
|
|
92
91
|
}
|
|
93
92
|
var childElements = children === null || children === void 0 ? void 0 : children.map(function(childRoute) {
|
|
94
93
|
return renderNestedRoute(childRoute, {
|
|
95
|
-
parent: nestedRoute
|
|
96
|
-
reporter
|
|
94
|
+
parent: nestedRoute
|
|
97
95
|
});
|
|
98
96
|
});
|
|
99
97
|
var routeElement = index ? /* @__PURE__ */ _jsx(Route, _object_spread_props(_object_spread({}, routeProps), {
|
|
@@ -104,12 +102,12 @@ var renderNestedRoute = function(nestedRoute) {
|
|
|
104
102
|
}), id);
|
|
105
103
|
return routeElement;
|
|
106
104
|
};
|
|
107
|
-
function createLoader(route
|
|
105
|
+
function createLoader(route) {
|
|
108
106
|
var loader = route.loader;
|
|
109
107
|
if (loader) {
|
|
110
108
|
return function() {
|
|
111
109
|
var _ref = _async_to_generator(function(args) {
|
|
112
|
-
var end, res, cost;
|
|
110
|
+
var end, res, cost, _storage_useContext_monitors, storage;
|
|
113
111
|
return _ts_generator(this, function(_state) {
|
|
114
112
|
switch (_state.label) {
|
|
115
113
|
case 0:
|
|
@@ -124,8 +122,10 @@ function createLoader(route, reporter) {
|
|
|
124
122
|
case 1:
|
|
125
123
|
res = _state.sent();
|
|
126
124
|
cost = end();
|
|
127
|
-
if (typeof document === "undefined"
|
|
128
|
-
|
|
125
|
+
if (typeof document === "undefined") {
|
|
126
|
+
;
|
|
127
|
+
storage = getAsyncLocalStorage();
|
|
128
|
+
storage === null || storage === void 0 ? void 0 : (_storage_useContext_monitors = storage.useContext().monitors) === null || _storage_useContext_monitors === void 0 ? void 0 : _storage_useContext_monitors.timing("".concat(LOADER_REPORTER_NAME, "-").concat(route.id), cost);
|
|
129
129
|
}
|
|
130
130
|
return [
|
|
131
131
|
2,
|
package/dist/esm/node/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
var _createStorage = createStorage(), run = _createStorage.run, useHeaders = _createStorage.useContext;
|
|
1
|
+
import { storage } from "../universal/async_storage.server";
|
|
3
2
|
import { serializeJson } from "./serialize";
|
|
4
3
|
export * from "./loaderContext";
|
|
5
4
|
export * from "./stream";
|
|
6
5
|
export {
|
|
7
|
-
run,
|
|
8
6
|
serializeJson,
|
|
9
|
-
|
|
7
|
+
storage
|
|
10
8
|
};
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
3
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import
|
|
4
|
+
import { LRUCache } from "lru-cache";
|
|
5
5
|
var MemoryContainer = /* @__PURE__ */ function() {
|
|
6
6
|
"use strict";
|
|
7
7
|
function MemoryContainer2() {
|
|
8
8
|
var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, max = _ref.max, maxAge = _ref.maxAge;
|
|
9
9
|
_class_call_check(this, MemoryContainer2);
|
|
10
|
-
this.cache = new
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
this.cache = new LRUCache({
|
|
11
|
+
maxSize: (max || 256) * MemoryContainer2.MB,
|
|
12
|
+
ttl: maxAge || MemoryContainer2.hour,
|
|
13
|
+
sizeCalculation: function(value, key) {
|
|
14
|
+
return JSON.stringify(value).length;
|
|
15
|
+
}
|
|
13
16
|
});
|
|
14
17
|
}
|
|
15
18
|
var _proto = MemoryContainer2.prototype;
|
|
@@ -61,7 +64,7 @@ var MemoryContainer = /* @__PURE__ */ function() {
|
|
|
61
64
|
case 1:
|
|
62
65
|
exist = _state.sent();
|
|
63
66
|
if (exist) {
|
|
64
|
-
_this.cache.
|
|
67
|
+
_this.cache.delete(key);
|
|
65
68
|
}
|
|
66
69
|
return [
|
|
67
70
|
2,
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import * as ah from "async_hooks";
|
|
2
2
|
var createStorage = function() {
|
|
3
|
-
var
|
|
3
|
+
var storage2;
|
|
4
4
|
if (typeof ah.AsyncLocalStorage !== "undefined") {
|
|
5
|
-
|
|
5
|
+
storage2 = new ah.AsyncLocalStorage();
|
|
6
6
|
}
|
|
7
7
|
var run = function(context, cb) {
|
|
8
|
-
if (!
|
|
8
|
+
if (!storage2) {
|
|
9
9
|
throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
|
|
10
10
|
}
|
|
11
11
|
return new Promise(function(resolve, reject) {
|
|
12
|
-
|
|
12
|
+
storage2.run(context, function() {
|
|
13
13
|
try {
|
|
14
14
|
return resolve(cb());
|
|
15
15
|
} catch (error) {
|
|
@@ -19,12 +19,12 @@ var createStorage = function() {
|
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
21
|
var useContext = function() {
|
|
22
|
-
if (!
|
|
22
|
+
if (!storage2) {
|
|
23
23
|
throw new Error("Unable to use async_hook, please confirm the node version >= 12.17\n ");
|
|
24
24
|
}
|
|
25
|
-
var context =
|
|
25
|
+
var context = storage2 === null || storage2 === void 0 ? void 0 : storage2.getStore();
|
|
26
26
|
if (!context) {
|
|
27
|
-
throw new Error("Can't call useContext out of scope, make sure @modern-js/utils is a single version in node_modules");
|
|
27
|
+
throw new Error("Can't call useContext out of scope, make sure @modern-js/runtime-utils is a single version in node_modules");
|
|
28
28
|
}
|
|
29
29
|
return context;
|
|
30
30
|
};
|
|
@@ -33,6 +33,11 @@ var createStorage = function() {
|
|
|
33
33
|
useContext
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
|
+
var storage = createStorage();
|
|
37
|
+
var getAsyncLocalStorage = function() {
|
|
38
|
+
return storage;
|
|
39
|
+
};
|
|
36
40
|
export {
|
|
37
|
-
|
|
41
|
+
getAsyncLocalStorage,
|
|
42
|
+
storage
|
|
38
43
|
};
|