@modern-js/prod-server 2.8.0 → 2.9.1-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/CHANGELOG.md +9 -0
- package/dist/cjs/server/index.js +8 -6
- package/dist/esm/server/index.js +71 -18
- package/dist/esm-node/server/index.js +9 -6
- package/dist/js/modern/constants.js +35 -0
- package/dist/js/modern/index.js +18 -0
- package/dist/js/modern/libs/context/context.js +160 -0
- package/dist/js/modern/libs/context/index.js +6 -0
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +20 -0
- package/dist/js/modern/libs/hook-api/template.js +73 -0
- package/dist/js/modern/libs/loadConfig.js +62 -0
- package/dist/js/modern/libs/logger.js +111 -0
- package/dist/js/modern/libs/metrics.js +11 -0
- package/dist/js/modern/libs/proxy.js +92 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +114 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +254 -0
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/js/modern/libs/render/cache/index.js +115 -0
- package/dist/js/modern/libs/render/cache/page-caches/index.js +32 -0
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/js/modern/libs/render/cache/spr.js +248 -0
- package/dist/js/modern/libs/render/cache/type.js +0 -0
- package/dist/js/modern/libs/render/cache/util.js +102 -0
- package/dist/js/modern/libs/render/index.js +86 -0
- package/dist/js/modern/libs/render/measure.js +68 -0
- package/dist/js/modern/libs/render/reader.js +107 -0
- package/dist/js/modern/libs/render/ssr.js +100 -0
- package/dist/js/modern/libs/render/static.js +60 -0
- package/dist/js/modern/libs/render/type.js +9 -0
- package/dist/js/modern/libs/route/index.js +54 -0
- package/dist/js/modern/libs/route/matcher.js +87 -0
- package/dist/js/modern/libs/route/route.js +16 -0
- package/dist/js/modern/libs/serve-file.js +67 -0
- package/dist/js/modern/server/index.js +208 -0
- package/dist/js/modern/server/modern-server-split.js +74 -0
- package/dist/js/modern/server/modern-server.js +554 -0
- package/dist/js/modern/type.js +0 -0
- package/dist/js/modern/utils.js +136 -0
- package/dist/js/modern/worker-server.js +89 -0
- package/dist/js/node/constants.js +62 -0
- package/dist/js/node/index.js +44 -0
- package/dist/js/node/libs/context/context.js +189 -0
- package/dist/js/node/libs/context/index.js +30 -0
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +43 -0
- package/dist/js/node/libs/hook-api/template.js +97 -0
- package/dist/js/node/libs/loadConfig.js +91 -0
- package/dist/js/node/libs/logger.js +133 -0
- package/dist/js/node/libs/metrics.js +34 -0
- package/dist/js/node/libs/proxy.js +114 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +115 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +245 -0
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/js/node/libs/render/cache/index.js +134 -0
- package/dist/js/node/libs/render/cache/page-caches/index.js +55 -0
- package/dist/js/node/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/js/node/libs/render/cache/spr.js +270 -0
- package/dist/js/node/libs/render/cache/type.js +15 -0
- package/dist/js/node/libs/render/cache/util.js +138 -0
- package/dist/js/node/libs/render/index.js +115 -0
- package/dist/js/node/libs/render/measure.js +90 -0
- package/dist/js/node/libs/render/reader.js +140 -0
- package/dist/js/node/libs/render/ssr.js +123 -0
- package/dist/js/node/libs/render/static.js +89 -0
- package/dist/js/node/libs/render/type.js +32 -0
- package/dist/js/node/libs/route/index.js +78 -0
- package/dist/js/node/libs/route/matcher.js +106 -0
- package/dist/js/node/libs/route/route.js +39 -0
- package/dist/js/node/libs/serve-file.js +97 -0
- package/dist/js/node/server/index.js +219 -0
- package/dist/js/node/server/modern-server-split.js +97 -0
- package/dist/js/node/server/modern-server.js +559 -0
- package/dist/js/node/type.js +15 -0
- package/dist/js/node/utils.js +166 -0
- package/dist/js/node/worker-server.js +113 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/logger.js +205 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +234 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +143 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server-split.js +360 -0
- package/dist/js/treeshaking/server/modern-server.js +1089 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +233 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -9
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var lru_exports = {};
|
|
25
|
+
__export(lru_exports, {
|
|
26
|
+
LRUCaches: () => LRUCaches
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(lru_exports);
|
|
29
|
+
var import_lru_cache = __toESM(require("lru-cache"));
|
|
30
|
+
class LRUCaches {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.max = options.max;
|
|
33
|
+
this.caches = new import_lru_cache.default(this.max);
|
|
34
|
+
}
|
|
35
|
+
init() {
|
|
36
|
+
return Promise.resolve();
|
|
37
|
+
}
|
|
38
|
+
keys() {
|
|
39
|
+
return this.caches.keys();
|
|
40
|
+
}
|
|
41
|
+
get(key) {
|
|
42
|
+
return Promise.resolve(this.caches.get(key) || null);
|
|
43
|
+
}
|
|
44
|
+
peek(key) {
|
|
45
|
+
return this.caches.peek(key) || null;
|
|
46
|
+
}
|
|
47
|
+
set(key, cache) {
|
|
48
|
+
this.caches.set(key, cache);
|
|
49
|
+
return Promise.resolve();
|
|
50
|
+
}
|
|
51
|
+
del(key) {
|
|
52
|
+
this.caches.del(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
56
|
+
0 && (module.exports = {
|
|
57
|
+
LRUCaches
|
|
58
|
+
});
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var spr_exports = {};
|
|
45
|
+
__export(spr_exports, {
|
|
46
|
+
createCache: () => createCache,
|
|
47
|
+
destroyCache: () => destroyCache
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(spr_exports);
|
|
50
|
+
var import_crypto = __toESM(require("crypto"));
|
|
51
|
+
var import_lru_cache = __toESM(require("lru-cache"));
|
|
52
|
+
var import_utils = require("@modern-js/utils");
|
|
53
|
+
var import_util = require("./util");
|
|
54
|
+
var import_page_caches = require("./page-caches");
|
|
55
|
+
const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
56
|
+
const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
|
|
57
|
+
const BASE_LEVEL = 0;
|
|
58
|
+
const QUERY_LEVEL = 1;
|
|
59
|
+
const HEADER_LEVEL = 2;
|
|
60
|
+
const QUERY_HEADER_LEVEL = 3;
|
|
61
|
+
class CacheManager {
|
|
62
|
+
constructor(cacheOptions) {
|
|
63
|
+
this.find = (() => {
|
|
64
|
+
{
|
|
65
|
+
const _this = this;
|
|
66
|
+
return {
|
|
67
|
+
[(0, import_util.fname)(BASE_LEVEL)](context, cacheKey) {
|
|
68
|
+
return _this.md5(cacheKey);
|
|
69
|
+
},
|
|
70
|
+
[(0, import_util.fname)(QUERY_LEVEL)](context, cacheKey, data) {
|
|
71
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
72
|
+
if (!queryFactor) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return _this.md5((0, import_util.connectFactor)(cacheKey, queryFactor));
|
|
76
|
+
},
|
|
77
|
+
[(0, import_util.fname)(HEADER_LEVEL)](context, cacheKey, data) {
|
|
78
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
79
|
+
if (!headerFactor) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return _this.md5((0, import_util.connectFactor)(cacheKey, headerFactor));
|
|
83
|
+
},
|
|
84
|
+
[(0, import_util.fname)(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
85
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
86
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
87
|
+
if (!queryFactor || !headerFactor) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return _this.md5((0, import_util.connectFactor)(cacheKey, headerFactor, queryFactor));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
})();
|
|
95
|
+
this.cacheOptions = cacheOptions;
|
|
96
|
+
this.cache = new import_lru_cache.default({
|
|
97
|
+
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
98
|
+
length(n) {
|
|
99
|
+
const len = n.caches.keys().reduce((total, cur) => {
|
|
100
|
+
var _a;
|
|
101
|
+
return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
|
|
102
|
+
}, 1);
|
|
103
|
+
return len;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
md5(content) {
|
|
108
|
+
const md5 = import_crypto.default.createHash("md5");
|
|
109
|
+
return md5.update(content).digest("hex");
|
|
110
|
+
}
|
|
111
|
+
generateRequestKey(context) {
|
|
112
|
+
const { pathname, entry } = context;
|
|
113
|
+
return this.md5(`${pathname}_${entry}`);
|
|
114
|
+
}
|
|
115
|
+
replaceValue(value, matcher) {
|
|
116
|
+
let final = value;
|
|
117
|
+
Object.keys(matcher).some((replacer) => {
|
|
118
|
+
const reg = new RegExp(matcher[replacer]);
|
|
119
|
+
if (reg.test(value)) {
|
|
120
|
+
final = replacer;
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
});
|
|
125
|
+
return final;
|
|
126
|
+
}
|
|
127
|
+
factor(keys, obj, matches = {}) {
|
|
128
|
+
keys.sort();
|
|
129
|
+
const getValue = (0, import_util.valueFactory)(obj);
|
|
130
|
+
const factorAry = keys.reduce((ary, key) => {
|
|
131
|
+
let value = getValue(key) || "";
|
|
132
|
+
const matcher = matches[key];
|
|
133
|
+
if (matcher) {
|
|
134
|
+
value = this.replaceValue(value, matcher);
|
|
135
|
+
}
|
|
136
|
+
return ary.concat([key, value]);
|
|
137
|
+
}, []);
|
|
138
|
+
return factorAry.join(",");
|
|
139
|
+
}
|
|
140
|
+
queryFactor(context, data) {
|
|
141
|
+
var _a, _b;
|
|
142
|
+
const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
|
|
143
|
+
const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
|
|
144
|
+
if (!queryKeys || queryKeys.length === 0) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const requestQuery = context.query;
|
|
148
|
+
const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
149
|
+
return queryFactor;
|
|
150
|
+
}
|
|
151
|
+
headerFactor(context, data) {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
|
|
154
|
+
const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
|
|
155
|
+
if (!headerKeys || headerKeys.length === 0) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const requestHeader = context.headers;
|
|
159
|
+
const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
160
|
+
return headerFactor;
|
|
161
|
+
}
|
|
162
|
+
best(context, cacheKey, data) {
|
|
163
|
+
return __async(this, null, function* () {
|
|
164
|
+
const { level } = data;
|
|
165
|
+
const cacheHash = this.find[(0, import_util.fname)(level)](context, cacheKey, data);
|
|
166
|
+
if (!cacheHash) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return data.caches.get(cacheHash);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
createCacheContent(config, caches) {
|
|
173
|
+
return {
|
|
174
|
+
level: config.level,
|
|
175
|
+
interval: config.interval,
|
|
176
|
+
includes: config.includes || null,
|
|
177
|
+
limit: config.staleLimit,
|
|
178
|
+
matches: config.matches || null,
|
|
179
|
+
caches
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
get(context) {
|
|
183
|
+
return __async(this, null, function* () {
|
|
184
|
+
const cacheKey = this.generateRequestKey(context);
|
|
185
|
+
const data = this.cache.get(cacheKey);
|
|
186
|
+
if (!data) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const dest = yield this.best(context, cacheKey, data);
|
|
190
|
+
if (!dest) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const { expireTime, limitTime, html, cacheHash } = dest;
|
|
194
|
+
const isStale = Date.now() - expireTime > 0;
|
|
195
|
+
const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
196
|
+
return {
|
|
197
|
+
content: html || "",
|
|
198
|
+
contentType: import_utils.mime.contentType("html"),
|
|
199
|
+
isStale,
|
|
200
|
+
isGarbage,
|
|
201
|
+
hash: cacheHash
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
set(context, html, cacheConfig, sync = false) {
|
|
206
|
+
return __async(this, null, function* () {
|
|
207
|
+
if (!cacheConfig) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
const cacheKey = this.generateRequestKey(context);
|
|
211
|
+
let data = this.cache.get(cacheKey);
|
|
212
|
+
if (!data) {
|
|
213
|
+
const caches = yield (0, import_page_caches.createPageCaches)(MAX_CACHE_EACH_REQ);
|
|
214
|
+
data = this.createCacheContent(cacheConfig, caches);
|
|
215
|
+
}
|
|
216
|
+
const cacheHash = this.find[(0, import_util.fname)(cacheConfig.level)](
|
|
217
|
+
context,
|
|
218
|
+
cacheKey,
|
|
219
|
+
data
|
|
220
|
+
);
|
|
221
|
+
if (!cacheHash) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
const cacheSyncOrAsync = () => __async(this, null, function* () {
|
|
225
|
+
const next = data;
|
|
226
|
+
const limit = cacheConfig.staleLimit;
|
|
227
|
+
const storeHTML = (0, import_util.cacheAddition)(html, cacheHash);
|
|
228
|
+
const size = storeHTML.length;
|
|
229
|
+
yield next.caches.set(cacheHash, {
|
|
230
|
+
expireTime: Date.now() + cacheConfig.interval * 1e3,
|
|
231
|
+
limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
|
|
232
|
+
cacheHash,
|
|
233
|
+
html: storeHTML,
|
|
234
|
+
size
|
|
235
|
+
});
|
|
236
|
+
this.cache.set(cacheKey, next);
|
|
237
|
+
return true;
|
|
238
|
+
});
|
|
239
|
+
const doCache = (0, import_util.withCoalescedInvoke)(cacheSyncOrAsync).bind(
|
|
240
|
+
null,
|
|
241
|
+
(0, import_util.namespaceHash)("stream", cacheHash),
|
|
242
|
+
[]
|
|
243
|
+
);
|
|
244
|
+
return (0, import_util.maybeSync)(doCache)(sync);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
del(context, cacheHash) {
|
|
248
|
+
return __async(this, null, function* () {
|
|
249
|
+
const cacheKey = this.generateRequestKey(context);
|
|
250
|
+
const data = this.cache.get(cacheKey);
|
|
251
|
+
data == null ? void 0 : data.caches.del(cacheHash);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
let manager;
|
|
256
|
+
function createCache() {
|
|
257
|
+
if (manager) {
|
|
258
|
+
return manager;
|
|
259
|
+
}
|
|
260
|
+
manager = new CacheManager({ max: 0 });
|
|
261
|
+
return manager;
|
|
262
|
+
}
|
|
263
|
+
function destroyCache() {
|
|
264
|
+
manager = null;
|
|
265
|
+
}
|
|
266
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
267
|
+
0 && (module.exports = {
|
|
268
|
+
createCache,
|
|
269
|
+
destroyCache
|
|
270
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var type_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(type_exports);
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var util_exports = {};
|
|
45
|
+
__export(util_exports, {
|
|
46
|
+
cacheAddition: () => cacheAddition,
|
|
47
|
+
connectFactor: () => connectFactor,
|
|
48
|
+
fname: () => fname,
|
|
49
|
+
getTime: () => getTime,
|
|
50
|
+
maybeSync: () => maybeSync,
|
|
51
|
+
namespaceHash: () => namespaceHash,
|
|
52
|
+
valueFactory: () => valueFactory,
|
|
53
|
+
withCoalescedInvoke: () => withCoalescedInvoke
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(util_exports);
|
|
56
|
+
var import_url = __toESM(require("url"));
|
|
57
|
+
function namespaceHash(namespace, hash) {
|
|
58
|
+
return `${namespace}/${hash}`;
|
|
59
|
+
}
|
|
60
|
+
function fname(lv) {
|
|
61
|
+
return `f${lv}`;
|
|
62
|
+
}
|
|
63
|
+
function connectFactor(...args) {
|
|
64
|
+
return args.join("-");
|
|
65
|
+
}
|
|
66
|
+
function valueFactory(obj) {
|
|
67
|
+
if (obj instanceof import_url.default.URLSearchParams) {
|
|
68
|
+
return function(key) {
|
|
69
|
+
return obj.get(key);
|
|
70
|
+
};
|
|
71
|
+
} else {
|
|
72
|
+
return function(key) {
|
|
73
|
+
const value = obj[key];
|
|
74
|
+
if (Array.isArray(value)) {
|
|
75
|
+
return value.join(",");
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function getTime([s, ns]) {
|
|
82
|
+
return Math.floor(s * 1e3 + ns / 1e6);
|
|
83
|
+
}
|
|
84
|
+
const RE_START_IN_HEAD = /<head>/;
|
|
85
|
+
function cacheAddition(html, hash) {
|
|
86
|
+
const additionHtml = html.replace(
|
|
87
|
+
RE_START_IN_HEAD,
|
|
88
|
+
`<head><meta name="x-moden-spr" content="${hash}">`
|
|
89
|
+
);
|
|
90
|
+
return additionHtml;
|
|
91
|
+
}
|
|
92
|
+
const globalInvokeCache = /* @__PURE__ */ new Map();
|
|
93
|
+
function withCoalescedInvoke(func) {
|
|
94
|
+
return function(key, args) {
|
|
95
|
+
return __async(this, null, function* () {
|
|
96
|
+
const entry = globalInvokeCache.get(key);
|
|
97
|
+
if (entry) {
|
|
98
|
+
return entry.then((res) => ({
|
|
99
|
+
isOrigin: false,
|
|
100
|
+
value: res.value
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
function __wrapper() {
|
|
104
|
+
return func(...args);
|
|
105
|
+
}
|
|
106
|
+
const future = __wrapper().then((res) => {
|
|
107
|
+
globalInvokeCache.delete(key);
|
|
108
|
+
return { isOrigin: true, value: res };
|
|
109
|
+
}).catch((err) => {
|
|
110
|
+
globalInvokeCache.delete(key);
|
|
111
|
+
throw err;
|
|
112
|
+
});
|
|
113
|
+
globalInvokeCache.set(key, future);
|
|
114
|
+
return future;
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function maybeSync(fn) {
|
|
119
|
+
return (sync) => {
|
|
120
|
+
if (sync) {
|
|
121
|
+
return fn();
|
|
122
|
+
} else {
|
|
123
|
+
fn();
|
|
124
|
+
return Promise.resolve();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
cacheAddition,
|
|
131
|
+
connectFactor,
|
|
132
|
+
fname,
|
|
133
|
+
getTime,
|
|
134
|
+
maybeSync,
|
|
135
|
+
namespaceHash,
|
|
136
|
+
valueFactory,
|
|
137
|
+
withCoalescedInvoke
|
|
138
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var render_exports = {};
|
|
45
|
+
__export(render_exports, {
|
|
46
|
+
createRenderHandler: () => createRenderHandler
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(render_exports);
|
|
49
|
+
var import_path = __toESM(require("path"));
|
|
50
|
+
var import_utils = require("@modern-js/utils");
|
|
51
|
+
var import_constants = require("../../constants");
|
|
52
|
+
var import_static = require("./static");
|
|
53
|
+
var import_reader = require("./reader");
|
|
54
|
+
var ssr = __toESM(require("./ssr"));
|
|
55
|
+
const createRenderHandler = ({
|
|
56
|
+
distDir,
|
|
57
|
+
staticGenerate,
|
|
58
|
+
forceCSR
|
|
59
|
+
}) => function render(_0) {
|
|
60
|
+
return __async(this, arguments, function* ({
|
|
61
|
+
ctx,
|
|
62
|
+
route,
|
|
63
|
+
runner
|
|
64
|
+
}) {
|
|
65
|
+
if (ctx.resHasHandled()) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const { entryPath, urlPath } = route;
|
|
69
|
+
const entry = import_path.default.join(distDir, entryPath);
|
|
70
|
+
if (!route.isSPA) {
|
|
71
|
+
const result = yield (0, import_static.handleDirectory)(ctx, entry, urlPath);
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
const templatePath = entry;
|
|
75
|
+
if (!import_utils.fs.existsSync(templatePath)) {
|
|
76
|
+
throw new Error(`Could not find template file: ${templatePath}`);
|
|
77
|
+
}
|
|
78
|
+
const content = yield (0, import_reader.readFile)(templatePath);
|
|
79
|
+
if (!content) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
const useCSR = forceCSR && ctx.query.csr;
|
|
83
|
+
if (route.isSSR && !useCSR) {
|
|
84
|
+
try {
|
|
85
|
+
const result = yield ssr.render(
|
|
86
|
+
ctx,
|
|
87
|
+
{
|
|
88
|
+
distDir,
|
|
89
|
+
entryName: route.entryName,
|
|
90
|
+
urlPath: route.urlPath,
|
|
91
|
+
bundle: route.bundle,
|
|
92
|
+
template: content.toString(),
|
|
93
|
+
staticGenerate
|
|
94
|
+
},
|
|
95
|
+
runner
|
|
96
|
+
);
|
|
97
|
+
return result;
|
|
98
|
+
} catch (err) {
|
|
99
|
+
ctx.error(
|
|
100
|
+
import_constants.ERROR_DIGEST.ERENDER,
|
|
101
|
+
err.stack || err.message
|
|
102
|
+
);
|
|
103
|
+
ctx.res.setHeader("x-modern-ssr-fallback", "1");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
content,
|
|
108
|
+
contentType: import_utils.mime.contentType(import_path.default.extname(templatePath))
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
createRenderHandler
|
|
115
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
35
|
+
var measure_exports = {};
|
|
36
|
+
__export(measure_exports, {
|
|
37
|
+
createLogger: () => createLogger,
|
|
38
|
+
createMetrics: () => createMetrics
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(measure_exports);
|
|
41
|
+
var import_utils = require("../../utils");
|
|
42
|
+
const createMetrics = (context, metrics) => {
|
|
43
|
+
const { entryName: entry, request } = context;
|
|
44
|
+
const { pathname = "" } = request || {};
|
|
45
|
+
const emitTimer = (name, cost, tags = {}) => {
|
|
46
|
+
metrics.emitTimer(name, cost, __spreadProps(__spreadValues({}, tags), {
|
|
47
|
+
pathname,
|
|
48
|
+
entry
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
51
|
+
const emitCounter = (name, counter, tags = {}) => {
|
|
52
|
+
metrics.emitCounter(name, counter, __spreadProps(__spreadValues({}, tags), {
|
|
53
|
+
pathname,
|
|
54
|
+
entry
|
|
55
|
+
}));
|
|
56
|
+
};
|
|
57
|
+
return { emitTimer, emitCounter };
|
|
58
|
+
};
|
|
59
|
+
const createLogger = (serverContext, logger) => {
|
|
60
|
+
const request = serverContext.request || {};
|
|
61
|
+
const { headers = {}, pathname = "" } = request;
|
|
62
|
+
const debug = (message, ...args) => {
|
|
63
|
+
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
64
|
+
};
|
|
65
|
+
const info = (message, ...args) => {
|
|
66
|
+
logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
|
|
67
|
+
};
|
|
68
|
+
const error = (message, e) => {
|
|
69
|
+
if (!e) {
|
|
70
|
+
e = message;
|
|
71
|
+
message = "";
|
|
72
|
+
}
|
|
73
|
+
logger.error(
|
|
74
|
+
`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`,
|
|
75
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
76
|
+
pathname,
|
|
77
|
+
(0, import_utils.headersWithoutCookie)(headers)
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
error,
|
|
82
|
+
info,
|
|
83
|
+
debug
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
createLogger,
|
|
89
|
+
createMetrics
|
|
90
|
+
});
|