@modern-js/prod-server 2.4.1-beta.0 → 2.5.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 +18 -3
- package/dist/cjs/constants.js +62 -0
- package/dist/cjs/index.js +44 -0
- package/dist/cjs/libs/context/context.js +189 -0
- package/dist/cjs/libs/context/index.js +30 -0
- package/dist/cjs/libs/hook-api/index.js +151 -0
- package/dist/cjs/libs/hook-api/route.js +43 -0
- package/dist/cjs/libs/hook-api/template.js +97 -0
- package/dist/cjs/libs/loadConfig.js +76 -0
- package/dist/cjs/libs/logger.js +122 -0
- package/dist/cjs/libs/metrics.js +34 -0
- package/dist/cjs/libs/proxy.js +81 -0
- package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +93 -0
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +210 -0
- package/dist/cjs/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/cjs/libs/render/cache/index.js +97 -0
- package/dist/cjs/libs/render/cache/page-caches/index.js +33 -0
- package/dist/cjs/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/cjs/libs/render/cache/spr.js +242 -0
- package/dist/cjs/libs/render/cache/type.js +15 -0
- package/dist/cjs/libs/render/cache/util.js +116 -0
- package/dist/cjs/libs/render/index.js +93 -0
- package/dist/cjs/libs/render/measure.js +75 -0
- package/dist/cjs/libs/render/reader.js +118 -0
- package/dist/cjs/libs/render/ssr.js +103 -0
- package/dist/cjs/libs/render/static.js +67 -0
- package/dist/cjs/libs/render/type.js +32 -0
- package/dist/cjs/libs/route/index.js +78 -0
- package/dist/cjs/libs/route/matcher.js +106 -0
- package/dist/cjs/libs/route/route.js +39 -0
- package/dist/cjs/libs/serve-file.js +77 -0
- package/dist/cjs/server/index.js +169 -0
- package/dist/cjs/server/modern-server-split.js +66 -0
- package/dist/cjs/server/modern-server.js +491 -0
- package/dist/cjs/type.js +15 -0
- package/dist/cjs/utils.js +152 -0
- package/dist/cjs/worker-server.js +93 -0
- package/dist/esm/constants.js +29 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/libs/context/context.js +274 -0
- package/dist/esm/libs/context/index.js +5 -0
- package/dist/esm/libs/hook-api/index.js +281 -0
- package/dist/esm/libs/hook-api/route.js +68 -0
- package/dist/esm/libs/hook-api/template.js +127 -0
- package/dist/esm/libs/loadConfig.js +82 -0
- package/dist/esm/libs/logger.js +205 -0
- package/dist/esm/libs/metrics.js +6 -0
- package/dist/esm/libs/proxy.js +244 -0
- package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/esm/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/esm/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/esm/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/esm/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/esm/libs/render/cache/index.js +346 -0
- package/dist/esm/libs/render/cache/page-caches/index.js +154 -0
- package/dist/esm/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/esm/libs/render/cache/spr.js +492 -0
- package/dist/esm/libs/render/cache/type.js +1 -0
- package/dist/esm/libs/render/cache/util.js +280 -0
- package/dist/esm/libs/render/index.js +234 -0
- package/dist/esm/libs/render/measure.js +146 -0
- package/dist/esm/libs/render/reader.js +339 -0
- package/dist/esm/libs/render/ssr.js +223 -0
- package/dist/esm/libs/render/static.js +216 -0
- package/dist/esm/libs/render/type.js +7 -0
- package/dist/esm/libs/route/index.js +130 -0
- package/dist/esm/libs/route/matcher.js +143 -0
- package/dist/esm/libs/route/route.js +40 -0
- package/dist/esm/libs/serve-file.js +184 -0
- package/dist/esm/server/index.js +505 -0
- package/dist/esm/server/modern-server-split.js +360 -0
- package/dist/esm/server/modern-server.js +1090 -0
- package/dist/esm/type.js +1 -0
- package/dist/esm/utils.js +147 -0
- package/dist/esm/worker-server.js +233 -0
- package/dist/esm-node/constants.js +35 -0
- package/dist/esm-node/index.js +18 -0
- package/dist/esm-node/libs/context/context.js +160 -0
- package/dist/esm-node/libs/context/index.js +6 -0
- package/dist/esm-node/libs/hook-api/index.js +119 -0
- package/dist/esm-node/libs/hook-api/route.js +20 -0
- package/dist/esm-node/libs/hook-api/template.js +73 -0
- package/dist/esm-node/libs/loadConfig.js +45 -0
- package/dist/esm-node/libs/logger.js +98 -0
- package/dist/esm-node/libs/metrics.js +11 -0
- package/dist/esm-node/libs/proxy.js +57 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +83 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +210 -0
- package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/esm-node/libs/render/cache/index.js +76 -0
- package/dist/esm-node/libs/render/cache/page-caches/index.js +10 -0
- package/dist/esm-node/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/esm-node/libs/render/cache/spr.js +220 -0
- package/dist/esm-node/libs/render/cache/type.js +0 -0
- package/dist/esm-node/libs/render/cache/util.js +80 -0
- package/dist/esm-node/libs/render/index.js +64 -0
- package/dist/esm-node/libs/render/measure.js +51 -0
- package/dist/esm-node/libs/render/reader.js +85 -0
- package/dist/esm-node/libs/render/ssr.js +80 -0
- package/dist/esm-node/libs/render/static.js +38 -0
- package/dist/esm-node/libs/render/type.js +9 -0
- package/dist/esm-node/libs/route/index.js +54 -0
- package/dist/esm-node/libs/route/matcher.js +87 -0
- package/dist/esm-node/libs/route/route.js +16 -0
- package/dist/esm-node/libs/serve-file.js +47 -0
- package/dist/esm-node/server/index.js +156 -0
- package/dist/esm-node/server/modern-server-split.js +43 -0
- package/dist/esm-node/server/modern-server.js +484 -0
- package/dist/esm-node/type.js +0 -0
- package/dist/esm-node/utils.js +120 -0
- package/dist/esm-node/worker-server.js +69 -0
- package/dist/js/modern/libs/render/index.js +4 -2
- package/dist/js/modern/server/modern-server.js +14 -8
- package/dist/js/node/libs/render/index.js +4 -2
- package/dist/js/node/server/modern-server.js +14 -8
- package/dist/js/treeshaking/libs/render/index.js +4 -3
- package/dist/js/treeshaking/server/modern-server.js +14 -8
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +3 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +17 -18
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import LRUCache from "lru-cache";
|
|
3
|
+
import { mime } from "@modern-js/utils";
|
|
4
|
+
import {
|
|
5
|
+
cacheAddition,
|
|
6
|
+
connectFactor,
|
|
7
|
+
fname,
|
|
8
|
+
maybeSync,
|
|
9
|
+
namespaceHash,
|
|
10
|
+
valueFactory,
|
|
11
|
+
withCoalescedInvoke
|
|
12
|
+
} from "./util";
|
|
13
|
+
import { createPageCaches } from "./page-caches";
|
|
14
|
+
const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
15
|
+
const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
|
|
16
|
+
const BASE_LEVEL = 0;
|
|
17
|
+
const QUERY_LEVEL = 1;
|
|
18
|
+
const HEADER_LEVEL = 2;
|
|
19
|
+
const QUERY_HEADER_LEVEL = 3;
|
|
20
|
+
class CacheManager {
|
|
21
|
+
constructor(cacheOptions) {
|
|
22
|
+
this.find = (() => {
|
|
23
|
+
{
|
|
24
|
+
const _this = this;
|
|
25
|
+
return {
|
|
26
|
+
[fname(BASE_LEVEL)](context, cacheKey) {
|
|
27
|
+
return _this.md5(cacheKey);
|
|
28
|
+
},
|
|
29
|
+
[fname(QUERY_LEVEL)](context, cacheKey, data) {
|
|
30
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
31
|
+
if (!queryFactor) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return _this.md5(connectFactor(cacheKey, queryFactor));
|
|
35
|
+
},
|
|
36
|
+
[fname(HEADER_LEVEL)](context, cacheKey, data) {
|
|
37
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
38
|
+
if (!headerFactor) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return _this.md5(connectFactor(cacheKey, headerFactor));
|
|
42
|
+
},
|
|
43
|
+
[fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
44
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
45
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
46
|
+
if (!queryFactor || !headerFactor) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
})();
|
|
54
|
+
this.cacheOptions = cacheOptions;
|
|
55
|
+
this.cache = new LRUCache({
|
|
56
|
+
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
57
|
+
length(n) {
|
|
58
|
+
const len = n.caches.keys().reduce((total, cur) => {
|
|
59
|
+
var _a;
|
|
60
|
+
return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
|
|
61
|
+
}, 1);
|
|
62
|
+
return len;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
md5(content) {
|
|
67
|
+
const md5 = crypto.createHash("md5");
|
|
68
|
+
return md5.update(content).digest("hex");
|
|
69
|
+
}
|
|
70
|
+
generateRequestKey(context) {
|
|
71
|
+
const { pathname, entry } = context;
|
|
72
|
+
return this.md5(`${pathname}_${entry}`);
|
|
73
|
+
}
|
|
74
|
+
replaceValue(value, matcher) {
|
|
75
|
+
let final = value;
|
|
76
|
+
Object.keys(matcher).some((replacer) => {
|
|
77
|
+
const reg = new RegExp(matcher[replacer]);
|
|
78
|
+
if (reg.test(value)) {
|
|
79
|
+
final = replacer;
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
});
|
|
84
|
+
return final;
|
|
85
|
+
}
|
|
86
|
+
factor(keys, obj, matches = {}) {
|
|
87
|
+
keys.sort();
|
|
88
|
+
const getValue = valueFactory(obj);
|
|
89
|
+
const factorAry = keys.reduce((ary, key) => {
|
|
90
|
+
let value = getValue(key) || "";
|
|
91
|
+
const matcher = matches[key];
|
|
92
|
+
if (matcher) {
|
|
93
|
+
value = this.replaceValue(value, matcher);
|
|
94
|
+
}
|
|
95
|
+
return ary.concat([key, value]);
|
|
96
|
+
}, []);
|
|
97
|
+
return factorAry.join(",");
|
|
98
|
+
}
|
|
99
|
+
queryFactor(context, data) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
|
|
102
|
+
const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
|
|
103
|
+
if (!queryKeys || queryKeys.length === 0) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const requestQuery = context.query;
|
|
107
|
+
const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
108
|
+
return queryFactor;
|
|
109
|
+
}
|
|
110
|
+
headerFactor(context, data) {
|
|
111
|
+
var _a, _b;
|
|
112
|
+
const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
|
|
113
|
+
const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
|
|
114
|
+
if (!headerKeys || headerKeys.length === 0) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const requestHeader = context.headers;
|
|
118
|
+
const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
119
|
+
return headerFactor;
|
|
120
|
+
}
|
|
121
|
+
async best(context, cacheKey, data) {
|
|
122
|
+
const { level } = data;
|
|
123
|
+
const cacheHash = this.find[fname(level)](context, cacheKey, data);
|
|
124
|
+
if (!cacheHash) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
return data.caches.get(cacheHash);
|
|
128
|
+
}
|
|
129
|
+
createCacheContent(config, caches) {
|
|
130
|
+
return {
|
|
131
|
+
level: config.level,
|
|
132
|
+
interval: config.interval,
|
|
133
|
+
includes: config.includes || null,
|
|
134
|
+
limit: config.staleLimit,
|
|
135
|
+
matches: config.matches || null,
|
|
136
|
+
caches
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async get(context) {
|
|
140
|
+
const cacheKey = this.generateRequestKey(context);
|
|
141
|
+
const data = this.cache.get(cacheKey);
|
|
142
|
+
if (!data) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const dest = await this.best(context, cacheKey, data);
|
|
146
|
+
if (!dest) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const { expireTime, limitTime, html, cacheHash } = dest;
|
|
150
|
+
const isStale = Date.now() - expireTime > 0;
|
|
151
|
+
const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
152
|
+
return {
|
|
153
|
+
content: html || "",
|
|
154
|
+
contentType: mime.contentType("html"),
|
|
155
|
+
isStale,
|
|
156
|
+
isGarbage,
|
|
157
|
+
hash: cacheHash
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
async set(context, html, cacheConfig, sync = false) {
|
|
161
|
+
if (!cacheConfig) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
const cacheKey = this.generateRequestKey(context);
|
|
165
|
+
let data = this.cache.get(cacheKey);
|
|
166
|
+
if (!data) {
|
|
167
|
+
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
168
|
+
data = this.createCacheContent(cacheConfig, caches);
|
|
169
|
+
}
|
|
170
|
+
const cacheHash = this.find[fname(cacheConfig.level)](
|
|
171
|
+
context,
|
|
172
|
+
cacheKey,
|
|
173
|
+
data
|
|
174
|
+
);
|
|
175
|
+
if (!cacheHash) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const cacheSyncOrAsync = async () => {
|
|
179
|
+
const next = data;
|
|
180
|
+
const limit = cacheConfig.staleLimit;
|
|
181
|
+
const storeHTML = cacheAddition(html, cacheHash);
|
|
182
|
+
const size = storeHTML.length;
|
|
183
|
+
await next.caches.set(cacheHash, {
|
|
184
|
+
expireTime: Date.now() + cacheConfig.interval * 1e3,
|
|
185
|
+
limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
|
|
186
|
+
cacheHash,
|
|
187
|
+
html: storeHTML,
|
|
188
|
+
size
|
|
189
|
+
});
|
|
190
|
+
this.cache.set(cacheKey, next);
|
|
191
|
+
return true;
|
|
192
|
+
};
|
|
193
|
+
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
|
|
194
|
+
null,
|
|
195
|
+
namespaceHash("stream", cacheHash),
|
|
196
|
+
[]
|
|
197
|
+
);
|
|
198
|
+
return maybeSync(doCache)(sync);
|
|
199
|
+
}
|
|
200
|
+
async del(context, cacheHash) {
|
|
201
|
+
const cacheKey = this.generateRequestKey(context);
|
|
202
|
+
const data = this.cache.get(cacheKey);
|
|
203
|
+
data == null ? void 0 : data.caches.del(cacheHash);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
let manager;
|
|
207
|
+
function createCache() {
|
|
208
|
+
if (manager) {
|
|
209
|
+
return manager;
|
|
210
|
+
}
|
|
211
|
+
manager = new CacheManager({ max: 0 });
|
|
212
|
+
return manager;
|
|
213
|
+
}
|
|
214
|
+
function destroyCache() {
|
|
215
|
+
manager = null;
|
|
216
|
+
}
|
|
217
|
+
export {
|
|
218
|
+
createCache,
|
|
219
|
+
destroyCache
|
|
220
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import url from "url";
|
|
2
|
+
function namespaceHash(namespace, hash) {
|
|
3
|
+
return `${namespace}/${hash}`;
|
|
4
|
+
}
|
|
5
|
+
function fname(lv) {
|
|
6
|
+
return `f${lv}`;
|
|
7
|
+
}
|
|
8
|
+
function connectFactor(...args) {
|
|
9
|
+
return args.join("-");
|
|
10
|
+
}
|
|
11
|
+
function valueFactory(obj) {
|
|
12
|
+
if (obj instanceof url.URLSearchParams) {
|
|
13
|
+
return function(key) {
|
|
14
|
+
return obj.get(key);
|
|
15
|
+
};
|
|
16
|
+
} else {
|
|
17
|
+
return function(key) {
|
|
18
|
+
const value = obj[key];
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
return value.join(",");
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function getTime([s, ns]) {
|
|
27
|
+
return Math.floor(s * 1e3 + ns / 1e6);
|
|
28
|
+
}
|
|
29
|
+
const RE_START_IN_HEAD = /<head>/;
|
|
30
|
+
function cacheAddition(html, hash) {
|
|
31
|
+
const additionHtml = html.replace(
|
|
32
|
+
RE_START_IN_HEAD,
|
|
33
|
+
`<head><meta name="x-moden-spr" content="${hash}">`
|
|
34
|
+
);
|
|
35
|
+
return additionHtml;
|
|
36
|
+
}
|
|
37
|
+
const globalInvokeCache = /* @__PURE__ */ new Map();
|
|
38
|
+
function withCoalescedInvoke(func) {
|
|
39
|
+
return async function(key, args) {
|
|
40
|
+
const entry = globalInvokeCache.get(key);
|
|
41
|
+
if (entry) {
|
|
42
|
+
return entry.then((res) => ({
|
|
43
|
+
isOrigin: false,
|
|
44
|
+
value: res.value
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
function __wrapper() {
|
|
48
|
+
return func(...args);
|
|
49
|
+
}
|
|
50
|
+
const future = __wrapper().then((res) => {
|
|
51
|
+
globalInvokeCache.delete(key);
|
|
52
|
+
return { isOrigin: true, value: res };
|
|
53
|
+
}).catch((err) => {
|
|
54
|
+
globalInvokeCache.delete(key);
|
|
55
|
+
throw err;
|
|
56
|
+
});
|
|
57
|
+
globalInvokeCache.set(key, future);
|
|
58
|
+
return future;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function maybeSync(fn) {
|
|
62
|
+
return (sync) => {
|
|
63
|
+
if (sync) {
|
|
64
|
+
return fn();
|
|
65
|
+
} else {
|
|
66
|
+
fn();
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
cacheAddition,
|
|
73
|
+
connectFactor,
|
|
74
|
+
fname,
|
|
75
|
+
getTime,
|
|
76
|
+
maybeSync,
|
|
77
|
+
namespaceHash,
|
|
78
|
+
valueFactory,
|
|
79
|
+
withCoalescedInvoke
|
|
80
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, mime } from "@modern-js/utils";
|
|
3
|
+
import { ERROR_DIGEST } from "../../constants";
|
|
4
|
+
import { handleDirectory } from "./static";
|
|
5
|
+
import { readFile } from "./reader";
|
|
6
|
+
import * as ssr from "./ssr";
|
|
7
|
+
const createRenderHandler = ({
|
|
8
|
+
distDir,
|
|
9
|
+
staticGenerate,
|
|
10
|
+
forceCSR
|
|
11
|
+
}) => async function render({
|
|
12
|
+
ctx,
|
|
13
|
+
route,
|
|
14
|
+
runner
|
|
15
|
+
}) {
|
|
16
|
+
if (ctx.resHasHandled()) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const { entryPath, urlPath } = route;
|
|
20
|
+
const entry = path.join(distDir, entryPath);
|
|
21
|
+
if (!route.isSPA) {
|
|
22
|
+
const result = await handleDirectory(ctx, entry, urlPath);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
const templatePath = entry;
|
|
26
|
+
if (!fs.existsSync(templatePath)) {
|
|
27
|
+
throw new Error(`Could not find template file: ${templatePath}`);
|
|
28
|
+
}
|
|
29
|
+
const content = await readFile(templatePath);
|
|
30
|
+
if (!content) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const useCSR = forceCSR && ctx.query.csr;
|
|
34
|
+
if (route.isSSR && !useCSR) {
|
|
35
|
+
try {
|
|
36
|
+
const result = await ssr.render(
|
|
37
|
+
ctx,
|
|
38
|
+
{
|
|
39
|
+
distDir,
|
|
40
|
+
entryName: route.entryName,
|
|
41
|
+
urlPath: route.urlPath,
|
|
42
|
+
bundle: route.bundle,
|
|
43
|
+
template: content.toString(),
|
|
44
|
+
staticGenerate
|
|
45
|
+
},
|
|
46
|
+
runner
|
|
47
|
+
);
|
|
48
|
+
return result;
|
|
49
|
+
} catch (err) {
|
|
50
|
+
ctx.error(
|
|
51
|
+
ERROR_DIGEST.ERENDER,
|
|
52
|
+
err.stack || err.message
|
|
53
|
+
);
|
|
54
|
+
ctx.res.setHeader("x-modern-ssr-fallback", "1");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
content,
|
|
59
|
+
contentType: mime.contentType(path.extname(templatePath))
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
createRenderHandler
|
|
64
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { headersWithoutCookie } from "../../utils";
|
|
2
|
+
const createMetrics = (context, metrics) => {
|
|
3
|
+
const { entryName: entry, request } = context;
|
|
4
|
+
const { pathname = "" } = request || {};
|
|
5
|
+
const emitTimer = (name, cost, tags = {}) => {
|
|
6
|
+
metrics.emitTimer(name, cost, {
|
|
7
|
+
...tags,
|
|
8
|
+
pathname,
|
|
9
|
+
entry
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
const emitCounter = (name, counter, tags = {}) => {
|
|
13
|
+
metrics.emitCounter(name, counter, {
|
|
14
|
+
...tags,
|
|
15
|
+
pathname,
|
|
16
|
+
entry
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
return { emitTimer, emitCounter };
|
|
20
|
+
};
|
|
21
|
+
const createLogger = (serverContext, logger) => {
|
|
22
|
+
const request = serverContext.request || {};
|
|
23
|
+
const { headers = {}, pathname = "" } = request;
|
|
24
|
+
const debug = (message, ...args) => {
|
|
25
|
+
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
26
|
+
};
|
|
27
|
+
const info = (message, ...args) => {
|
|
28
|
+
logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
|
|
29
|
+
};
|
|
30
|
+
const error = (message, e) => {
|
|
31
|
+
if (!e) {
|
|
32
|
+
e = message;
|
|
33
|
+
message = "";
|
|
34
|
+
}
|
|
35
|
+
logger.error(
|
|
36
|
+
`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`,
|
|
37
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
38
|
+
pathname,
|
|
39
|
+
headersWithoutCookie(headers)
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
error,
|
|
44
|
+
info,
|
|
45
|
+
debug
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
createLogger,
|
|
50
|
+
createMetrics
|
|
51
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fs } from "@modern-js/utils";
|
|
2
|
+
import LRU from "lru-cache";
|
|
3
|
+
const Byte = 1;
|
|
4
|
+
const KB = 1024 * Byte;
|
|
5
|
+
const MB = 1024 * KB;
|
|
6
|
+
const getContentLength = (cache) => cache.content.length;
|
|
7
|
+
const createCacheItem = async (filepath, mtime) => {
|
|
8
|
+
const content = await fs.readFile(filepath);
|
|
9
|
+
return {
|
|
10
|
+
content,
|
|
11
|
+
mtime
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
class LruReader {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.cache = new LRU({
|
|
17
|
+
max: 256 * MB,
|
|
18
|
+
length: getContentLength,
|
|
19
|
+
maxAge: 5 * 60 * 5e3
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
init() {
|
|
23
|
+
}
|
|
24
|
+
close() {
|
|
25
|
+
}
|
|
26
|
+
async read(filepath) {
|
|
27
|
+
if (this.cache.has(filepath)) {
|
|
28
|
+
const { content } = this.cache.get(filepath);
|
|
29
|
+
return { content };
|
|
30
|
+
}
|
|
31
|
+
if (!fs.existsSync(filepath)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const stat = fs.statSync(filepath);
|
|
35
|
+
if (stat.isDirectory()) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (stat.size > 20 * MB) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const item = await createCacheItem(filepath, stat.mtime);
|
|
42
|
+
this.cache.set(filepath, item);
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
update() {
|
|
46
|
+
const { cache } = this;
|
|
47
|
+
const files = cache.keys();
|
|
48
|
+
for (const filepath of files) {
|
|
49
|
+
if (!fs.existsSync(filepath)) {
|
|
50
|
+
cache.del(filepath);
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const item = cache.get(filepath);
|
|
54
|
+
const stat = fs.statSync(filepath);
|
|
55
|
+
const { mtime } = stat;
|
|
56
|
+
if (item.mtime < mtime) {
|
|
57
|
+
cache.del(filepath);
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
cache.del(filepath);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const reader = new LruReader();
|
|
66
|
+
const readFile = async (filepath) => {
|
|
67
|
+
const file = await reader.read(filepath);
|
|
68
|
+
return file == null ? void 0 : file.content;
|
|
69
|
+
};
|
|
70
|
+
const updateFile = () => {
|
|
71
|
+
reader.update();
|
|
72
|
+
};
|
|
73
|
+
const init = () => {
|
|
74
|
+
reader.init();
|
|
75
|
+
};
|
|
76
|
+
const close = () => {
|
|
77
|
+
reader.close();
|
|
78
|
+
};
|
|
79
|
+
export {
|
|
80
|
+
LruReader,
|
|
81
|
+
close,
|
|
82
|
+
init,
|
|
83
|
+
readFile,
|
|
84
|
+
updateFile
|
|
85
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import {
|
|
3
|
+
fs,
|
|
4
|
+
LOADABLE_STATS_FILE,
|
|
5
|
+
mime,
|
|
6
|
+
ROUTE_MINIFEST_FILE,
|
|
7
|
+
SERVER_RENDER_FUNCTION_NAME
|
|
8
|
+
} from "@modern-js/utils";
|
|
9
|
+
import cookie from "cookie";
|
|
10
|
+
import cache from "./cache";
|
|
11
|
+
import { createLogger, createMetrics } from "./measure";
|
|
12
|
+
const render = async (ctx, renderOptions, runner) => {
|
|
13
|
+
var _a;
|
|
14
|
+
const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
|
|
15
|
+
const bundleJS = path.join(distDir, bundle);
|
|
16
|
+
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
17
|
+
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
18
|
+
const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
|
|
19
|
+
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
|
|
20
|
+
const context = {
|
|
21
|
+
request: {
|
|
22
|
+
baseUrl: urlPath,
|
|
23
|
+
params: ctx.params,
|
|
24
|
+
pathname: ctx.path,
|
|
25
|
+
host: ctx.host,
|
|
26
|
+
query: ctx.query,
|
|
27
|
+
url: ctx.href,
|
|
28
|
+
cookieMap: cookie.parse(ctx.headers.cookie || ""),
|
|
29
|
+
headers: ctx.headers
|
|
30
|
+
},
|
|
31
|
+
response: {
|
|
32
|
+
setHeader: (key, value) => {
|
|
33
|
+
return ctx.res.setHeader(key, value);
|
|
34
|
+
},
|
|
35
|
+
status: (code) => {
|
|
36
|
+
ctx.res.statusCode = code;
|
|
37
|
+
},
|
|
38
|
+
locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
|
|
39
|
+
},
|
|
40
|
+
redirection: {},
|
|
41
|
+
template,
|
|
42
|
+
loadableStats,
|
|
43
|
+
routeManifest,
|
|
44
|
+
entryName,
|
|
45
|
+
staticGenerate,
|
|
46
|
+
logger: void 0,
|
|
47
|
+
metrics: void 0,
|
|
48
|
+
req: ctx.req,
|
|
49
|
+
res: ctx.res
|
|
50
|
+
};
|
|
51
|
+
context.logger = createLogger(context, ctx.logger);
|
|
52
|
+
context.metrics = createMetrics(context, ctx.metrics);
|
|
53
|
+
runner.extendSSRContext(context);
|
|
54
|
+
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
55
|
+
const content = await cache(serverRender, ctx)(context);
|
|
56
|
+
const { url, status = 302 } = context.redirection;
|
|
57
|
+
if (url) {
|
|
58
|
+
return {
|
|
59
|
+
content: url,
|
|
60
|
+
contentType: "",
|
|
61
|
+
statusCode: status,
|
|
62
|
+
redirect: true
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (typeof content === "string") {
|
|
66
|
+
return {
|
|
67
|
+
content,
|
|
68
|
+
contentType: mime.contentType("html")
|
|
69
|
+
};
|
|
70
|
+
} else {
|
|
71
|
+
return {
|
|
72
|
+
content: "",
|
|
73
|
+
contentStream: content,
|
|
74
|
+
contentType: mime.contentType("html")
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
export {
|
|
79
|
+
render
|
|
80
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { mime } from "@modern-js/utils";
|
|
3
|
+
import { readFile } from "./reader";
|
|
4
|
+
async function handleDirectory(ctx, entryPath, urlPath) {
|
|
5
|
+
const { path: pathname } = ctx;
|
|
6
|
+
const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
|
|
7
|
+
let content = await readFile(filepath);
|
|
8
|
+
let contentType = mime.contentType(path.extname(filepath) || "");
|
|
9
|
+
if (!content) {
|
|
10
|
+
if (pathname.endsWith("/")) {
|
|
11
|
+
content = await readFile(`${filepath}index.html`);
|
|
12
|
+
} else if (!pathname.includes(".")) {
|
|
13
|
+
content = await readFile(`${filepath}.html`);
|
|
14
|
+
if (!content) {
|
|
15
|
+
content = await readFile(`${filepath}/index.html`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (content) {
|
|
19
|
+
contentType = mime.contentType("html");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (!content) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
content,
|
|
27
|
+
contentType: contentType || ""
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const trimLeft = (str, prefix) => {
|
|
31
|
+
if (str.startsWith(prefix)) {
|
|
32
|
+
return str.substring(prefix.length);
|
|
33
|
+
}
|
|
34
|
+
return str;
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
handleDirectory
|
|
38
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
var RenderLevel = /* @__PURE__ */ ((RenderLevel2) => {
|
|
2
|
+
RenderLevel2[RenderLevel2["CLIENT_RENDER"] = 0] = "CLIENT_RENDER";
|
|
3
|
+
RenderLevel2[RenderLevel2["SERVER_PREFETCH"] = 1] = "SERVER_PREFETCH";
|
|
4
|
+
RenderLevel2[RenderLevel2["SERVER_RENDER"] = 2] = "SERVER_RENDER";
|
|
5
|
+
return RenderLevel2;
|
|
6
|
+
})(RenderLevel || {});
|
|
7
|
+
export {
|
|
8
|
+
RenderLevel
|
|
9
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { RouteMatcher } from "./matcher";
|
|
2
|
+
class RouteMatchManager {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.specs = [];
|
|
5
|
+
this.matchers = [];
|
|
6
|
+
}
|
|
7
|
+
filter(pathname) {
|
|
8
|
+
return this.matchers.reduce((matches, matcher) => {
|
|
9
|
+
if (matcher.matchUrlPath(pathname)) {
|
|
10
|
+
matches.push(matcher);
|
|
11
|
+
}
|
|
12
|
+
return matches;
|
|
13
|
+
}, []);
|
|
14
|
+
}
|
|
15
|
+
best(pathname, matches) {
|
|
16
|
+
let best;
|
|
17
|
+
let matchedLen = 0;
|
|
18
|
+
for (const match of matches) {
|
|
19
|
+
const len = match.matchLength(pathname);
|
|
20
|
+
if (len === null) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (len > matchedLen) {
|
|
24
|
+
best = match;
|
|
25
|
+
matchedLen = len;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return best;
|
|
29
|
+
}
|
|
30
|
+
reset(specs) {
|
|
31
|
+
this.specs = specs;
|
|
32
|
+
const matchers = specs.reduce((ms, spec) => {
|
|
33
|
+
ms.push(new RouteMatcher(spec));
|
|
34
|
+
return ms;
|
|
35
|
+
}, []);
|
|
36
|
+
this.matchers = matchers;
|
|
37
|
+
}
|
|
38
|
+
match(pathname) {
|
|
39
|
+
const matches = this.filter(pathname);
|
|
40
|
+
const best = this.best(pathname, matches);
|
|
41
|
+
return best;
|
|
42
|
+
}
|
|
43
|
+
matchEntry(entryname) {
|
|
44
|
+
return this.matchers.find((matcher) => matcher.matchEntry(entryname));
|
|
45
|
+
}
|
|
46
|
+
getBundles() {
|
|
47
|
+
const bundles = this.specs.filter((route) => route.isSSR).map((route) => route.bundle);
|
|
48
|
+
return bundles;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
RouteMatchManager,
|
|
53
|
+
RouteMatcher
|
|
54
|
+
};
|