@modern-js/prod-server 2.48.6 → 2.49.1
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/README.md +20 -16
- package/dist/cjs/{libs/route/route.js → error.js} +16 -17
- package/dist/cjs/index.js +18 -24
- package/dist/cjs/init.js +71 -0
- package/dist/cjs/{type.js → types.js} +2 -2
- package/dist/esm/error.js +15 -0
- package/dist/esm/index.js +53 -19
- package/dist/esm/init.js +73 -0
- package/dist/esm-node/error.js +14 -0
- package/dist/esm-node/index.js +16 -17
- package/dist/esm-node/init.js +47 -0
- package/dist/types/error.d.ts +6 -0
- package/dist/types/index.d.ts +8 -11
- package/dist/types/init.d.ts +4 -0
- package/dist/types/types.d.ts +14 -0
- package/package.json +23 -64
- package/dist/cjs/constants.js +0 -72
- package/dist/cjs/libs/context/context.js +0 -236
- package/dist/cjs/libs/context/index.js +0 -31
- package/dist/cjs/libs/hook-api/afterRenderForStream.js +0 -34
- package/dist/cjs/libs/hook-api/base.js +0 -96
- package/dist/cjs/libs/hook-api/index.js +0 -85
- package/dist/cjs/libs/hook-api/index.worker.js +0 -116
- package/dist/cjs/libs/hook-api/route.js +0 -44
- package/dist/cjs/libs/hook-api/template.js +0 -72
- package/dist/cjs/libs/hook-api/templateForStream.js +0 -52
- package/dist/cjs/libs/loadConfig.js +0 -70
- package/dist/cjs/libs/metrics.js +0 -33
- package/dist/cjs/libs/preload/flushServerHeader.js +0 -49
- package/dist/cjs/libs/preload/index.js +0 -24
- package/dist/cjs/libs/preload/parseLinks.js +0 -124
- package/dist/cjs/libs/preload/shouldFlushServerHeader.js +0 -52
- package/dist/cjs/libs/preload/transformLinks2String.js +0 -150
- package/dist/cjs/libs/proxy.js +0 -99
- package/dist/cjs/libs/render/index.js +0 -109
- package/dist/cjs/libs/render/measure.js +0 -74
- package/dist/cjs/libs/render/ssr.js +0 -130
- package/dist/cjs/libs/render/ssrCache/cacheMod.js +0 -51
- package/dist/cjs/libs/render/ssrCache/index.js +0 -90
- package/dist/cjs/libs/render/ssrCache/manager.js +0 -120
- package/dist/cjs/libs/render/static.js +0 -72
- package/dist/cjs/libs/render/type.js +0 -33
- package/dist/cjs/libs/render/utils.js +0 -41
- package/dist/cjs/libs/reporter.js +0 -39
- package/dist/cjs/libs/route/index.js +0 -83
- package/dist/cjs/libs/route/matcher.js +0 -113
- package/dist/cjs/libs/serveFile.js +0 -84
- package/dist/cjs/libs/serverTiming.js +0 -41
- package/dist/cjs/renderHtml.js +0 -65
- package/dist/cjs/server/index.js +0 -223
- package/dist/cjs/server/modernServer.js +0 -522
- package/dist/cjs/server/modernServerSplit.js +0 -67
- package/dist/cjs/utils.js +0 -164
- package/dist/cjs/workerServer.js +0 -260
- package/dist/esm/constants.js +0 -43
- package/dist/esm/libs/context/context.js +0 -260
- package/dist/esm/libs/context/index.js +0 -8
- package/dist/esm/libs/hook-api/afterRenderForStream.js +0 -33
- package/dist/esm/libs/hook-api/base.js +0 -73
- package/dist/esm/libs/hook-api/index.js +0 -55
- package/dist/esm/libs/hook-api/index.worker.js +0 -99
- package/dist/esm/libs/hook-api/route.js +0 -26
- package/dist/esm/libs/hook-api/template.js +0 -56
- package/dist/esm/libs/hook-api/templateForStream.js +0 -35
- package/dist/esm/libs/loadConfig.js +0 -36
- package/dist/esm/libs/metrics.js +0 -9
- package/dist/esm/libs/preload/flushServerHeader.js +0 -50
- package/dist/esm/libs/preload/index.js +0 -2
- package/dist/esm/libs/preload/parseLinks.js +0 -172
- package/dist/esm/libs/preload/shouldFlushServerHeader.js +0 -27
- package/dist/esm/libs/preload/transformLinks2String.js +0 -140
- package/dist/esm/libs/proxy.js +0 -160
- package/dist/esm/libs/render/index.js +0 -175
- package/dist/esm/libs/render/measure.js +0 -69
- package/dist/esm/libs/render/ssr.js +0 -133
- package/dist/esm/libs/render/ssrCache/cacheMod.js +0 -25
- package/dist/esm/libs/render/ssrCache/index.js +0 -170
- package/dist/esm/libs/render/ssrCache/manager.js +0 -178
- package/dist/esm/libs/render/static.js +0 -100
- package/dist/esm/libs/render/type.js +0 -9
- package/dist/esm/libs/render/utils.js +0 -16
- package/dist/esm/libs/reporter.js +0 -15
- package/dist/esm/libs/route/index.js +0 -82
- package/dist/esm/libs/route/matcher.js +0 -92
- package/dist/esm/libs/route/route.js +0 -17
- package/dist/esm/libs/serveFile.js +0 -68
- package/dist/esm/libs/serverTiming.js +0 -22
- package/dist/esm/renderHtml.js +0 -133
- package/dist/esm/server/index.js +0 -350
- package/dist/esm/server/modernServer.js +0 -884
- package/dist/esm/server/modernServerSplit.js +0 -113
- package/dist/esm/utils.js +0 -103
- package/dist/esm/workerServer.js +0 -352
- package/dist/esm-node/constants.js +0 -43
- package/dist/esm-node/libs/context/context.js +0 -202
- package/dist/esm-node/libs/context/index.js +0 -6
- package/dist/esm-node/libs/hook-api/afterRenderForStream.js +0 -10
- package/dist/esm-node/libs/hook-api/base.js +0 -61
- package/dist/esm-node/libs/hook-api/index.js +0 -57
- package/dist/esm-node/libs/hook-api/index.worker.js +0 -89
- package/dist/esm-node/libs/hook-api/route.js +0 -20
- package/dist/esm-node/libs/hook-api/template.js +0 -47
- package/dist/esm-node/libs/hook-api/templateForStream.js +0 -28
- package/dist/esm-node/libs/loadConfig.js +0 -34
- package/dist/esm-node/libs/metrics.js +0 -9
- package/dist/esm-node/libs/preload/flushServerHeader.js +0 -25
- package/dist/esm-node/libs/preload/index.js +0 -2
- package/dist/esm-node/libs/preload/parseLinks.js +0 -90
- package/dist/esm-node/libs/preload/shouldFlushServerHeader.js +0 -27
- package/dist/esm-node/libs/preload/transformLinks2String.js +0 -126
- package/dist/esm-node/libs/proxy.js +0 -74
- package/dist/esm-node/libs/render/index.js +0 -75
- package/dist/esm-node/libs/render/measure.js +0 -49
- package/dist/esm-node/libs/render/ssr.js +0 -96
- package/dist/esm-node/libs/render/ssrCache/cacheMod.js +0 -17
- package/dist/esm-node/libs/render/ssrCache/index.js +0 -66
- package/dist/esm-node/libs/render/ssrCache/manager.js +0 -96
- package/dist/esm-node/libs/render/static.js +0 -38
- package/dist/esm-node/libs/render/type.js +0 -9
- package/dist/esm-node/libs/render/utils.js +0 -16
- package/dist/esm-node/libs/reporter.js +0 -15
- package/dist/esm-node/libs/route/index.js +0 -58
- package/dist/esm-node/libs/route/matcher.js +0 -89
- package/dist/esm-node/libs/route/route.js +0 -16
- package/dist/esm-node/libs/serveFile.js +0 -49
- package/dist/esm-node/libs/serverTiming.js +0 -17
- package/dist/esm-node/renderHtml.js +0 -73
- package/dist/esm-node/server/index.js +0 -189
- package/dist/esm-node/server/modernServer.js +0 -488
- package/dist/esm-node/server/modernServerSplit.js +0 -43
- package/dist/esm-node/utils.js +0 -132
- package/dist/esm-node/workerServer.js +0 -235
- package/dist/types/constants.d.ts +0 -30
- package/dist/types/libs/context/context.d.ts +0 -62
- package/dist/types/libs/context/index.d.ts +0 -7
- package/dist/types/libs/hook-api/afterRenderForStream.d.ts +0 -4
- package/dist/types/libs/hook-api/base.d.ts +0 -53
- package/dist/types/libs/hook-api/index.d.ts +0 -6
- package/dist/types/libs/hook-api/index.worker.d.ts +0 -19
- package/dist/types/libs/hook-api/route.d.ts +0 -9
- package/dist/types/libs/hook-api/template.d.ts +0 -22
- package/dist/types/libs/hook-api/templateForStream.d.ts +0 -8
- package/dist/types/libs/loadConfig.d.ts +0 -13
- package/dist/types/libs/metrics.d.ts +0 -3
- package/dist/types/libs/preload/flushServerHeader.d.ts +0 -14
- package/dist/types/libs/preload/index.d.ts +0 -2
- package/dist/types/libs/preload/parseLinks.d.ts +0 -13
- package/dist/types/libs/preload/shouldFlushServerHeader.d.ts +0 -3
- package/dist/types/libs/preload/transformLinks2String.d.ts +0 -3
- package/dist/types/libs/proxy.d.ts +0 -13
- package/dist/types/libs/render/index.d.ts +0 -21
- package/dist/types/libs/render/measure.d.ts +0 -10
- package/dist/types/libs/render/ssr.d.ts +0 -12
- package/dist/types/libs/render/ssrCache/cacheMod.d.ts +0 -8
- package/dist/types/libs/render/ssrCache/index.d.ts +0 -6
- package/dist/types/libs/render/ssrCache/manager.d.ts +0 -20
- package/dist/types/libs/render/static.d.ts +0 -3
- package/dist/types/libs/render/type.d.ts +0 -36
- package/dist/types/libs/render/utils.d.ts +0 -5
- package/dist/types/libs/reporter.d.ts +0 -2
- package/dist/types/libs/route/index.d.ts +0 -15
- package/dist/types/libs/route/matcher.d.ts +0 -15
- package/dist/types/libs/route/route.d.ts +0 -14
- package/dist/types/libs/serveFile.d.ts +0 -9
- package/dist/types/libs/serverTiming.d.ts +0 -13
- package/dist/types/renderHtml.d.ts +0 -23
- package/dist/types/server/index.d.ts +0 -55
- package/dist/types/server/modernServer.d.ts +0 -71
- package/dist/types/server/modernServerSplit.d.ts +0 -2
- package/dist/types/type.d.ts +0 -78
- package/dist/types/utils.d.ts +0 -24
- package/dist/types/workerServer.d.ts +0 -59
- /package/dist/esm/{type.js → types.js} +0 -0
- /package/dist/esm-node/{type.js → types.js} +0 -0
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
function transformToRegExp(input) {
|
|
2
|
-
if (typeof input === "string") {
|
|
3
|
-
return new RegExp(input);
|
|
4
|
-
}
|
|
5
|
-
return input;
|
|
6
|
-
}
|
|
7
|
-
function shouldFlushServerHeader(serverConf, userAgent, disablePreload) {
|
|
8
|
-
const { ssr: ssrConf } = serverConf || {};
|
|
9
|
-
if (disablePreload) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
if (typeof ssrConf === "object" && ssrConf.preload) {
|
|
13
|
-
if (typeof ssrConf.preload === "object") {
|
|
14
|
-
const { userAgentFilter } = ssrConf.preload;
|
|
15
|
-
if (userAgentFilter && userAgent) {
|
|
16
|
-
return !transformToRegExp(userAgentFilter).test(userAgent);
|
|
17
|
-
}
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
shouldFlushServerHeader,
|
|
26
|
-
transformToRegExp
|
|
27
|
-
};
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { transformToRegExp } from "./shouldFlushServerHeader";
|
|
2
|
-
function transformLinks2String(links, preload) {
|
|
3
|
-
if (typeof preload === "boolean") {
|
|
4
|
-
return transformLinkToString(dedup(links));
|
|
5
|
-
}
|
|
6
|
-
const { include, exclude, attributes } = preload;
|
|
7
|
-
const resolveLinks = transformLinkToString(addAttributes(dedup(removeExclude(addInclude(links, include), exclude)), attributes));
|
|
8
|
-
return resolveLinks;
|
|
9
|
-
}
|
|
10
|
-
function addInclude(links, include) {
|
|
11
|
-
const images = [
|
|
12
|
-
"gif",
|
|
13
|
-
"jpg",
|
|
14
|
-
"jpeg",
|
|
15
|
-
"png",
|
|
16
|
-
"webp",
|
|
17
|
-
"bmp",
|
|
18
|
-
"tiff",
|
|
19
|
-
"anpg",
|
|
20
|
-
"ico"
|
|
21
|
-
];
|
|
22
|
-
const videos = [
|
|
23
|
-
"mp4",
|
|
24
|
-
"webm",
|
|
25
|
-
"ogm",
|
|
26
|
-
"ogv",
|
|
27
|
-
"ogg"
|
|
28
|
-
];
|
|
29
|
-
const fonts = [
|
|
30
|
-
"woff",
|
|
31
|
-
"woff2",
|
|
32
|
-
"eot",
|
|
33
|
-
"ttf",
|
|
34
|
-
"otf"
|
|
35
|
-
];
|
|
36
|
-
const includes = (include === null || include === void 0 ? void 0 : include.map((item) => {
|
|
37
|
-
if (typeof item === "string") {
|
|
38
|
-
const type = (() => {
|
|
39
|
-
if (item.endsWith(".js")) {
|
|
40
|
-
return "script";
|
|
41
|
-
}
|
|
42
|
-
if (item.endsWith(".css")) {
|
|
43
|
-
return "style";
|
|
44
|
-
}
|
|
45
|
-
if (images.some((image) => item.endsWith(`.${image}`))) {
|
|
46
|
-
return "image";
|
|
47
|
-
}
|
|
48
|
-
if (videos.some((video) => item.endsWith(`.${video}`))) {
|
|
49
|
-
return "video";
|
|
50
|
-
}
|
|
51
|
-
if (fonts.some((font) => item.endsWith(`.${font}`))) {
|
|
52
|
-
return "font";
|
|
53
|
-
}
|
|
54
|
-
})();
|
|
55
|
-
return {
|
|
56
|
-
uri: item,
|
|
57
|
-
as: type
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
uri: item.url,
|
|
62
|
-
as: item.as,
|
|
63
|
-
rel: item.rel
|
|
64
|
-
};
|
|
65
|
-
})) || [];
|
|
66
|
-
return links.concat(includes);
|
|
67
|
-
}
|
|
68
|
-
function removeExclude(links, exclude) {
|
|
69
|
-
return exclude ? links.filter(({ uri }) => !transformToRegExp(exclude).test(uri)) : links;
|
|
70
|
-
}
|
|
71
|
-
function addAttributes(links, attributes) {
|
|
72
|
-
const parseAttributes = (_attributes) => {
|
|
73
|
-
return Object.entries(_attributes || {}).reduce((results, [key, value]) => {
|
|
74
|
-
if (typeof value === "boolean") {
|
|
75
|
-
value && results.push(`; ${key}`);
|
|
76
|
-
return results;
|
|
77
|
-
}
|
|
78
|
-
results.push(`; ${key}=${value}`);
|
|
79
|
-
return results;
|
|
80
|
-
}, []).join("");
|
|
81
|
-
};
|
|
82
|
-
return links.map((link) => {
|
|
83
|
-
const { as } = link;
|
|
84
|
-
if (as) {
|
|
85
|
-
const attributesStr = (() => {
|
|
86
|
-
const { style, script, image, font } = attributes || {};
|
|
87
|
-
switch (as) {
|
|
88
|
-
case "script":
|
|
89
|
-
return parseAttributes(script);
|
|
90
|
-
case "style":
|
|
91
|
-
return parseAttributes(style);
|
|
92
|
-
case "image":
|
|
93
|
-
return parseAttributes(image);
|
|
94
|
-
case "font":
|
|
95
|
-
return parseAttributes(font);
|
|
96
|
-
default:
|
|
97
|
-
return "";
|
|
98
|
-
}
|
|
99
|
-
})();
|
|
100
|
-
return {
|
|
101
|
-
...link,
|
|
102
|
-
rest: attributesStr
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
return link;
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
function dedup(links) {
|
|
109
|
-
const set = /* @__PURE__ */ new Set();
|
|
110
|
-
return links.filter(({ uri }) => {
|
|
111
|
-
if (set.has(uri)) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
set.add(uri);
|
|
115
|
-
return true;
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
function transformLinkToString(links) {
|
|
119
|
-
return links.map(({ uri, as, rel: originalRel, rest }) => {
|
|
120
|
-
const rel = originalRel || "preload";
|
|
121
|
-
return as ? `<${uri}>; rel=${rel}; as=${as}${rest || ""}` : `<${uri}>; rel=${rel}${rest || ""}`;
|
|
122
|
-
}).join(", ");
|
|
123
|
-
}
|
|
124
|
-
export {
|
|
125
|
-
transformLinks2String
|
|
126
|
-
};
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { HttpProxyMiddleware } from "http-proxy-middleware/dist/http-proxy-middleware";
|
|
2
|
-
import { debug } from "../utils";
|
|
3
|
-
function formatProxyOptions(proxyOptions) {
|
|
4
|
-
const ret = [];
|
|
5
|
-
if (Array.isArray(proxyOptions)) {
|
|
6
|
-
ret.push(...proxyOptions);
|
|
7
|
-
} else if ("target" in proxyOptions) {
|
|
8
|
-
ret.push(proxyOptions);
|
|
9
|
-
} else {
|
|
10
|
-
for (const [context, options] of Object.entries(proxyOptions)) {
|
|
11
|
-
const opts = {
|
|
12
|
-
context,
|
|
13
|
-
changeOrigin: true,
|
|
14
|
-
logLevel: "warn"
|
|
15
|
-
};
|
|
16
|
-
if (typeof options === "string") {
|
|
17
|
-
opts.target = options;
|
|
18
|
-
} else {
|
|
19
|
-
Object.assign(opts, options);
|
|
20
|
-
}
|
|
21
|
-
ret.push(opts);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
const handleError = (err, _req, _res, _target) => {
|
|
25
|
-
console.error(err);
|
|
26
|
-
};
|
|
27
|
-
for (const opts of ret) {
|
|
28
|
-
var _opts;
|
|
29
|
-
var _onError;
|
|
30
|
-
(_onError = (_opts = opts).onError) !== null && _onError !== void 0 ? _onError : _opts.onError = handleError;
|
|
31
|
-
}
|
|
32
|
-
return ret;
|
|
33
|
-
}
|
|
34
|
-
const createProxyHandler = (proxyOptions) => {
|
|
35
|
-
debug("createProxyHandler", proxyOptions);
|
|
36
|
-
const formattedOptionsList = formatProxyOptions(proxyOptions);
|
|
37
|
-
const proxies = [];
|
|
38
|
-
const handlers = [];
|
|
39
|
-
for (const opts of formattedOptionsList) {
|
|
40
|
-
const proxy = new HttpProxyMiddleware(opts.context, opts);
|
|
41
|
-
const handler = async (ctx, next) => {
|
|
42
|
-
const { req, res } = ctx;
|
|
43
|
-
const bypassUrl = typeof opts.bypass === "function" ? opts.bypass(req, res, opts) : null;
|
|
44
|
-
if (typeof bypassUrl === "boolean") {
|
|
45
|
-
ctx.status = 404;
|
|
46
|
-
next();
|
|
47
|
-
} else if (typeof bypassUrl === "string") {
|
|
48
|
-
ctx.url = bypassUrl;
|
|
49
|
-
next();
|
|
50
|
-
} else {
|
|
51
|
-
proxy.middleware(req, res, next);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
proxies.push(proxy);
|
|
55
|
-
handlers.push(handler);
|
|
56
|
-
}
|
|
57
|
-
const handleUpgrade = (server) => {
|
|
58
|
-
for (const proxy of proxies) {
|
|
59
|
-
const raw = proxy;
|
|
60
|
-
if (raw.proxyOptions.ws === true && !raw.wsInternalSubscribed) {
|
|
61
|
-
server.on("upgrade", raw.handleUpgrade);
|
|
62
|
-
raw.wsInternalSubscribed = true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
handlers,
|
|
68
|
-
handleUpgrade
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
export {
|
|
72
|
-
createProxyHandler,
|
|
73
|
-
formatProxyOptions
|
|
74
|
-
};
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { cutNameByHyphen, mime } from "@modern-js/utils";
|
|
3
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
4
|
-
import { ERROR_DIGEST } from "../../constants";
|
|
5
|
-
import { shouldFlushServerHeader } from "../preload/shouldFlushServerHeader";
|
|
6
|
-
import { handleDirectory } from "./static";
|
|
7
|
-
import * as ssr from "./ssr";
|
|
8
|
-
import { injectServerData } from "./utils";
|
|
9
|
-
const calcFallback = (metaName) => `x-${cutNameByHyphen(metaName)}-ssr-fallback`;
|
|
10
|
-
const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, ssrRender, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
|
|
11
|
-
if (ctx.resHasHandled()) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
const { entryPath, urlPath } = route;
|
|
15
|
-
const entry = path.join(distDir, entryPath);
|
|
16
|
-
if (!route.isSPA) {
|
|
17
|
-
const result = await handleDirectory(ctx, entry, urlPath);
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
const templatePath = entry;
|
|
21
|
-
const content = await fileReader.readFile(templatePath);
|
|
22
|
-
if (!content) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
const queryCSR = ctx.query.csr;
|
|
26
|
-
const headerFallback = ctx.headers[calcFallback(metaName)];
|
|
27
|
-
const useCSR = forceCSR && (queryCSR || headerFallback);
|
|
28
|
-
if (route.isSSR && !useCSR) {
|
|
29
|
-
try {
|
|
30
|
-
const userAgent = ctx.getReqHeader("User-Agent");
|
|
31
|
-
const disablePreload = Boolean(ctx.headers[`x-${cutNameByHyphen(metaName)}-disable-preload`]);
|
|
32
|
-
if (shouldFlushServerHeader(conf.server, userAgent, disablePreload)) {
|
|
33
|
-
const { flushServerHeader } = await import("../preload");
|
|
34
|
-
flushServerHeader({
|
|
35
|
-
serverConf: conf.server,
|
|
36
|
-
ctx,
|
|
37
|
-
distDir,
|
|
38
|
-
template: content.toString(),
|
|
39
|
-
headers: {
|
|
40
|
-
"Content-Type": mime.contentType(path.extname(templatePath))
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
const ssrRenderOptions = {
|
|
45
|
-
distDir,
|
|
46
|
-
route,
|
|
47
|
-
template: content.toString(),
|
|
48
|
-
staticGenerate,
|
|
49
|
-
nonce
|
|
50
|
-
};
|
|
51
|
-
const result = await (ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : ssr.render(ctx, ssrRenderOptions, runner));
|
|
52
|
-
return result;
|
|
53
|
-
} catch (err) {
|
|
54
|
-
ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
55
|
-
ctx.res.set(calcFallback(metaName), "1");
|
|
56
|
-
await runner.handleSSRFallback({
|
|
57
|
-
ctx,
|
|
58
|
-
type: "error"
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
} else if (route.isSSR && useCSR) {
|
|
62
|
-
const fallbackType = queryCSR ? "query" : "header";
|
|
63
|
-
await runner.handleSSRFallback({
|
|
64
|
-
ctx,
|
|
65
|
-
type: fallbackType
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
content: route.entryName ? injectServerData(content.toString(), ctx) : content,
|
|
70
|
-
contentType: mime.contentType(path.extname(templatePath))
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
export {
|
|
74
|
-
createRenderHandler
|
|
75
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
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, void 0, {
|
|
7
|
-
...tags,
|
|
8
|
-
pathname,
|
|
9
|
-
entry
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
const emitCounter = (name, counter, tags = {}) => {
|
|
13
|
-
metrics.emitCounter(name, counter, void 0, {
|
|
14
|
-
...tags,
|
|
15
|
-
pathname,
|
|
16
|
-
entry
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
return {
|
|
20
|
-
emitTimer,
|
|
21
|
-
emitCounter
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
const createLogger = (serverContext, logger) => {
|
|
25
|
-
const request = serverContext.request || {};
|
|
26
|
-
const { headers = {}, pathname = "" } = request;
|
|
27
|
-
const debug = (message, ...args) => {
|
|
28
|
-
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
29
|
-
};
|
|
30
|
-
const info = (message, ...args) => {
|
|
31
|
-
logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
|
|
32
|
-
};
|
|
33
|
-
const error = (message, e) => {
|
|
34
|
-
if (!e) {
|
|
35
|
-
e = message;
|
|
36
|
-
message = "";
|
|
37
|
-
}
|
|
38
|
-
logger.error(`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, pathname, headersWithoutCookie(headers));
|
|
39
|
-
};
|
|
40
|
-
return {
|
|
41
|
-
error,
|
|
42
|
-
info,
|
|
43
|
-
debug
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
export {
|
|
47
|
-
createLogger,
|
|
48
|
-
createMetrics
|
|
49
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs, mime, LOADABLE_STATS_FILE, ROUTE_MANIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
|
|
3
|
-
import * as isbot from "isbot";
|
|
4
|
-
import { createAfterStreamingRenderContext } from "../hook-api";
|
|
5
|
-
import { afterRenderInjectableStream } from "../hook-api/afterRenderForStream";
|
|
6
|
-
import { createLogger, createMetrics } from "./measure";
|
|
7
|
-
import { injectServerDataStream, injectServerData } from "./utils";
|
|
8
|
-
import { ssrCache } from "./ssrCache";
|
|
9
|
-
const render = async (ctx, renderOptions, runner) => {
|
|
10
|
-
var _ctx_res;
|
|
11
|
-
const { distDir, route, template, staticGenerate, enableUnsafeCtx = false, nonce } = renderOptions;
|
|
12
|
-
const { urlPath, bundle, entryName } = route;
|
|
13
|
-
const bundleJS = path.join(distDir, bundle);
|
|
14
|
-
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
15
|
-
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
16
|
-
const routesManifestUri = path.join(distDir, ROUTE_MANIFEST_FILE);
|
|
17
|
-
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
|
|
18
|
-
const isSpider = isbot.default(ctx.headers["user-agent"] || null);
|
|
19
|
-
const context = {
|
|
20
|
-
request: {
|
|
21
|
-
baseUrl: urlPath,
|
|
22
|
-
params: ctx.params,
|
|
23
|
-
pathname: ctx.path,
|
|
24
|
-
host: ctx.host,
|
|
25
|
-
query: ctx.query,
|
|
26
|
-
url: ctx.href,
|
|
27
|
-
headers: ctx.headers
|
|
28
|
-
},
|
|
29
|
-
response: {
|
|
30
|
-
setHeader: (key, value) => {
|
|
31
|
-
return ctx.res.setHeader(key, value);
|
|
32
|
-
},
|
|
33
|
-
status: (code) => {
|
|
34
|
-
ctx.res.statusCode = code;
|
|
35
|
-
},
|
|
36
|
-
locals: ((_ctx_res = ctx.res) === null || _ctx_res === void 0 ? void 0 : _ctx_res.locals) || {}
|
|
37
|
-
},
|
|
38
|
-
redirection: {},
|
|
39
|
-
template,
|
|
40
|
-
loadableStats,
|
|
41
|
-
routeManifest,
|
|
42
|
-
entryName,
|
|
43
|
-
staticGenerate,
|
|
44
|
-
logger: void 0,
|
|
45
|
-
metrics: void 0,
|
|
46
|
-
reporter: ctx.reporter,
|
|
47
|
-
serverTiming: ctx.serverTiming,
|
|
48
|
-
req: ctx.req,
|
|
49
|
-
res: ctx.res,
|
|
50
|
-
enableUnsafeCtx,
|
|
51
|
-
isSpider,
|
|
52
|
-
nonce
|
|
53
|
-
};
|
|
54
|
-
context.logger = createLogger(context, ctx.logger);
|
|
55
|
-
context.metrics = createMetrics(context, ctx.metrics);
|
|
56
|
-
runner.extendSSRContext(context);
|
|
57
|
-
const bundleJSContent = await Promise.resolve(require(bundleJS));
|
|
58
|
-
const serverRender = bundleJSContent[SERVER_RENDER_FUNCTION_NAME];
|
|
59
|
-
const { data: content, status: cacheStatus } = await ssrCache(ctx.req, serverRender, context);
|
|
60
|
-
const { url, status = 302 } = context.redirection;
|
|
61
|
-
if (url) {
|
|
62
|
-
return {
|
|
63
|
-
content: url,
|
|
64
|
-
contentType: "",
|
|
65
|
-
statusCode: status,
|
|
66
|
-
redirect: true
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
const headers = {};
|
|
70
|
-
cacheStatus && (headers["x-render-cache"] = cacheStatus);
|
|
71
|
-
if (typeof content === "string") {
|
|
72
|
-
return {
|
|
73
|
-
content: injectServerData(content, ctx),
|
|
74
|
-
contentType: mime.contentType("html"),
|
|
75
|
-
headers
|
|
76
|
-
};
|
|
77
|
-
} else {
|
|
78
|
-
let contentStream = injectServerDataStream(content, ctx);
|
|
79
|
-
const afterStreamingRenderContext = createAfterStreamingRenderContext(ctx, route);
|
|
80
|
-
contentStream = contentStream.pipe(afterRenderInjectableStream((chunk) => {
|
|
81
|
-
const context2 = afterStreamingRenderContext(chunk);
|
|
82
|
-
return runner.afterStreamingRender(context2, {
|
|
83
|
-
onLast: ({ chunk: chunk2 }) => chunk2
|
|
84
|
-
});
|
|
85
|
-
}));
|
|
86
|
-
return {
|
|
87
|
-
content: "",
|
|
88
|
-
contentStream,
|
|
89
|
-
contentType: mime.contentType("html"),
|
|
90
|
-
headers
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
export {
|
|
95
|
-
render
|
|
96
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
|
|
3
|
-
const CACHE_FILENAME = "cache";
|
|
4
|
-
class ServerCacheMod {
|
|
5
|
-
loadServerCacheMod(pwd = process.cwd()) {
|
|
6
|
-
const serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
|
|
7
|
-
const mod = requireExistModule(serverCacheFilepath, {
|
|
8
|
-
interop: false
|
|
9
|
-
});
|
|
10
|
-
this.customContainer = mod === null || mod === void 0 ? void 0 : mod.customContainer;
|
|
11
|
-
this.cacheOption = mod === null || mod === void 0 ? void 0 : mod.cacheOption;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
const cacheMod = new ServerCacheMod();
|
|
15
|
-
export {
|
|
16
|
-
cacheMod
|
|
17
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Transform } from "stream";
|
|
2
|
-
import { createMemoryStorage } from "@modern-js/runtime-utils/storer";
|
|
3
|
-
import { cacheMod } from "./cacheMod";
|
|
4
|
-
import { CacheManager } from "./manager";
|
|
5
|
-
const cacheStorage = createMemoryStorage("__ssr__cache");
|
|
6
|
-
async function ssrCache(req, render, ssrContext) {
|
|
7
|
-
const { customContainer, cacheOption } = cacheMod;
|
|
8
|
-
const cacheControl = await matchCacheControl(req, cacheOption);
|
|
9
|
-
const cacheManager = new CacheManager(customContainer ? customContainer : cacheStorage);
|
|
10
|
-
if (cacheControl) {
|
|
11
|
-
return cacheManager.getCacheResult(req, cacheControl, render, ssrContext);
|
|
12
|
-
} else {
|
|
13
|
-
const renderResult = await render(ssrContext);
|
|
14
|
-
if (!renderResult) {
|
|
15
|
-
return {
|
|
16
|
-
data: ""
|
|
17
|
-
};
|
|
18
|
-
} else if (typeof renderResult === "string") {
|
|
19
|
-
return {
|
|
20
|
-
data: renderResult
|
|
21
|
-
};
|
|
22
|
-
} else {
|
|
23
|
-
const stream = new Transform({
|
|
24
|
-
write(chunk, _, callback) {
|
|
25
|
-
this.push(chunk);
|
|
26
|
-
callback();
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
const data = await renderResult(stream);
|
|
30
|
-
return {
|
|
31
|
-
data
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
async function matchCacheControl(req, cacheOption) {
|
|
37
|
-
if (!cacheOption) {
|
|
38
|
-
return void 0;
|
|
39
|
-
} else if (isCacheControl(cacheOption)) {
|
|
40
|
-
return cacheOption;
|
|
41
|
-
} else if (isCacheOptionProvider(cacheOption)) {
|
|
42
|
-
return cacheOption(req);
|
|
43
|
-
} else {
|
|
44
|
-
const url = req.url;
|
|
45
|
-
const options = Object.entries(cacheOption);
|
|
46
|
-
for (const [key, option] of options) {
|
|
47
|
-
if (key === "*" || new RegExp(key).test(url)) {
|
|
48
|
-
if (typeof option === "function") {
|
|
49
|
-
return option(req);
|
|
50
|
-
} else {
|
|
51
|
-
return option;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return void 0;
|
|
56
|
-
}
|
|
57
|
-
function isCacheOptionProvider(option) {
|
|
58
|
-
return typeof option === "function";
|
|
59
|
-
}
|
|
60
|
-
function isCacheControl(option) {
|
|
61
|
-
return typeof option === "object" && option !== null && "maxAge" in option;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export {
|
|
65
|
-
ssrCache
|
|
66
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { Transform } from "stream";
|
|
2
|
-
import { normalizePathname } from "@modern-js/runtime-utils/url";
|
|
3
|
-
class CacheManager {
|
|
4
|
-
async getCacheResult(req, cacheControl, render, ssrContext) {
|
|
5
|
-
const key = this.computedKey(req, cacheControl);
|
|
6
|
-
const value = await this.container.get(key);
|
|
7
|
-
const { maxAge, staleWhileRevalidate } = cacheControl;
|
|
8
|
-
const ttl = maxAge + staleWhileRevalidate;
|
|
9
|
-
if (value) {
|
|
10
|
-
const cache = JSON.parse(value);
|
|
11
|
-
const interval = Date.now() - cache.cursor;
|
|
12
|
-
if (interval <= maxAge) {
|
|
13
|
-
return {
|
|
14
|
-
data: cache.val,
|
|
15
|
-
status: "hit"
|
|
16
|
-
};
|
|
17
|
-
} else if (interval <= staleWhileRevalidate + maxAge) {
|
|
18
|
-
this.processCache(key, render, ssrContext, ttl);
|
|
19
|
-
return {
|
|
20
|
-
data: cache.val,
|
|
21
|
-
status: "stale"
|
|
22
|
-
};
|
|
23
|
-
} else {
|
|
24
|
-
return this.processCache(key, render, ssrContext, ttl, "expired");
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
return this.processCache(key, render, ssrContext, ttl, "miss");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async processCache(key, render, ssrContext, ttl, status) {
|
|
31
|
-
const renderResult = await render(ssrContext);
|
|
32
|
-
if (!renderResult) {
|
|
33
|
-
return {
|
|
34
|
-
data: ""
|
|
35
|
-
};
|
|
36
|
-
} else if (typeof renderResult === "string") {
|
|
37
|
-
const current = Date.now();
|
|
38
|
-
const cache = {
|
|
39
|
-
val: renderResult,
|
|
40
|
-
cursor: current
|
|
41
|
-
};
|
|
42
|
-
await this.container.set(key, JSON.stringify(cache), {
|
|
43
|
-
ttl
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
data: renderResult,
|
|
47
|
-
status
|
|
48
|
-
};
|
|
49
|
-
} else {
|
|
50
|
-
let html = "";
|
|
51
|
-
const stream = new Transform({
|
|
52
|
-
write(chunk, _, callback) {
|
|
53
|
-
html += chunk.toString();
|
|
54
|
-
this.push(chunk);
|
|
55
|
-
callback();
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
stream.on("close", () => {
|
|
59
|
-
const current = Date.now();
|
|
60
|
-
const cache = {
|
|
61
|
-
val: html,
|
|
62
|
-
cursor: current
|
|
63
|
-
};
|
|
64
|
-
this.container.set(key, JSON.stringify(cache), {
|
|
65
|
-
ttl
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
const readable = await renderResult(stream);
|
|
69
|
-
return {
|
|
70
|
-
data: readable,
|
|
71
|
-
status
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
computedKey(req, cacheControl) {
|
|
76
|
-
const { url } = req;
|
|
77
|
-
const [pathname] = url.split("?");
|
|
78
|
-
const { customKey } = cacheControl;
|
|
79
|
-
const defaultKey = normalizePathname(pathname);
|
|
80
|
-
if (customKey) {
|
|
81
|
-
if (typeof customKey === "string") {
|
|
82
|
-
return customKey;
|
|
83
|
-
} else {
|
|
84
|
-
return customKey(defaultKey);
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
return defaultKey;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
constructor(container) {
|
|
91
|
-
this.container = container;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
export {
|
|
95
|
-
CacheManager
|
|
96
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { mime } from "@modern-js/utils";
|
|
3
|
-
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
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 fileReader.readFile(filepath);
|
|
8
|
-
let contentType = mime.contentType(path.extname(filepath) || "");
|
|
9
|
-
if (!content) {
|
|
10
|
-
if (pathname.endsWith("/")) {
|
|
11
|
-
content = await fileReader.readFile(`${filepath}index.html`);
|
|
12
|
-
} else if (!pathname.includes(".")) {
|
|
13
|
-
content = await fileReader.readFile(`${filepath}.html`);
|
|
14
|
-
if (!content) {
|
|
15
|
-
content = await fileReader.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
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
var RenderLevel;
|
|
2
|
-
(function(RenderLevel2) {
|
|
3
|
-
RenderLevel2[RenderLevel2["CLIENT_RENDER"] = 0] = "CLIENT_RENDER";
|
|
4
|
-
RenderLevel2[RenderLevel2["SERVER_PREFETCH"] = 1] = "SERVER_PREFETCH";
|
|
5
|
-
RenderLevel2[RenderLevel2["SERVER_RENDER"] = 2] = "SERVER_RENDER";
|
|
6
|
-
})(RenderLevel || (RenderLevel = {}));
|
|
7
|
-
export {
|
|
8
|
-
RenderLevel
|
|
9
|
-
};
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { TemplateAPI } from "../hook-api/template";
|
|
2
|
-
import { templateInjectableStream } from "../hook-api/templateForStream";
|
|
3
|
-
const injectServerData = (content, context) => {
|
|
4
|
-
const template = new TemplateAPI(content);
|
|
5
|
-
template.prependHead(`<script type="application/json" id="__MODERN_SERVER_DATA__">${JSON.stringify(context.serverData)}</script>`);
|
|
6
|
-
return template.get();
|
|
7
|
-
};
|
|
8
|
-
const injectServerDataStream = (content, context) => {
|
|
9
|
-
return content.pipe(templateInjectableStream({
|
|
10
|
-
prependHead: `<script type="application/json" id="__MODERN_SERVER_DATA__">${JSON.stringify(context.serverData)}</script>`
|
|
11
|
-
}));
|
|
12
|
-
};
|
|
13
|
-
export {
|
|
14
|
-
injectServerData,
|
|
15
|
-
injectServerDataStream
|
|
16
|
-
};
|