@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,86 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import path from "path";
|
|
22
|
+
import { fs, mime } from "@modern-js/utils";
|
|
23
|
+
import { ERROR_DIGEST } from "../../constants";
|
|
24
|
+
import { handleDirectory } from "./static";
|
|
25
|
+
import { readFile } from "./reader";
|
|
26
|
+
import * as ssr from "./ssr";
|
|
27
|
+
const createRenderHandler = ({
|
|
28
|
+
distDir,
|
|
29
|
+
staticGenerate,
|
|
30
|
+
forceCSR
|
|
31
|
+
}) => function render(_0) {
|
|
32
|
+
return __async(this, arguments, function* ({
|
|
33
|
+
ctx,
|
|
34
|
+
route,
|
|
35
|
+
runner
|
|
36
|
+
}) {
|
|
37
|
+
if (ctx.resHasHandled()) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const { entryPath, urlPath } = route;
|
|
41
|
+
const entry = path.join(distDir, entryPath);
|
|
42
|
+
if (!route.isSPA) {
|
|
43
|
+
const result = yield handleDirectory(ctx, entry, urlPath);
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
const templatePath = entry;
|
|
47
|
+
if (!fs.existsSync(templatePath)) {
|
|
48
|
+
throw new Error(`Could not find template file: ${templatePath}`);
|
|
49
|
+
}
|
|
50
|
+
const content = yield readFile(templatePath);
|
|
51
|
+
if (!content) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const useCSR = forceCSR && ctx.query.csr;
|
|
55
|
+
if (route.isSSR && !useCSR) {
|
|
56
|
+
try {
|
|
57
|
+
const result = yield ssr.render(
|
|
58
|
+
ctx,
|
|
59
|
+
{
|
|
60
|
+
distDir,
|
|
61
|
+
entryName: route.entryName,
|
|
62
|
+
urlPath: route.urlPath,
|
|
63
|
+
bundle: route.bundle,
|
|
64
|
+
template: content.toString(),
|
|
65
|
+
staticGenerate
|
|
66
|
+
},
|
|
67
|
+
runner
|
|
68
|
+
);
|
|
69
|
+
return result;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
ctx.error(
|
|
72
|
+
ERROR_DIGEST.ERENDER,
|
|
73
|
+
err.stack || err.message
|
|
74
|
+
);
|
|
75
|
+
ctx.res.setHeader("x-modern-ssr-fallback", "1");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
content,
|
|
80
|
+
contentType: mime.contentType(path.extname(templatePath))
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
export {
|
|
85
|
+
createRenderHandler
|
|
86
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { headersWithoutCookie } from "../../utils";
|
|
21
|
+
const createMetrics = (context, metrics) => {
|
|
22
|
+
const { entryName: entry, request } = context;
|
|
23
|
+
const { pathname = "" } = request || {};
|
|
24
|
+
const emitTimer = (name, cost, tags = {}) => {
|
|
25
|
+
metrics.emitTimer(name, cost, __spreadProps(__spreadValues({}, tags), {
|
|
26
|
+
pathname,
|
|
27
|
+
entry
|
|
28
|
+
}));
|
|
29
|
+
};
|
|
30
|
+
const emitCounter = (name, counter, tags = {}) => {
|
|
31
|
+
metrics.emitCounter(name, counter, __spreadProps(__spreadValues({}, tags), {
|
|
32
|
+
pathname,
|
|
33
|
+
entry
|
|
34
|
+
}));
|
|
35
|
+
};
|
|
36
|
+
return { emitTimer, emitCounter };
|
|
37
|
+
};
|
|
38
|
+
const createLogger = (serverContext, logger) => {
|
|
39
|
+
const request = serverContext.request || {};
|
|
40
|
+
const { headers = {}, pathname = "" } = request;
|
|
41
|
+
const debug = (message, ...args) => {
|
|
42
|
+
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
43
|
+
};
|
|
44
|
+
const info = (message, ...args) => {
|
|
45
|
+
logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
|
|
46
|
+
};
|
|
47
|
+
const error = (message, e) => {
|
|
48
|
+
if (!e) {
|
|
49
|
+
e = message;
|
|
50
|
+
message = "";
|
|
51
|
+
}
|
|
52
|
+
logger.error(
|
|
53
|
+
`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`,
|
|
54
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
55
|
+
pathname,
|
|
56
|
+
headersWithoutCookie(headers)
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
error,
|
|
61
|
+
info,
|
|
62
|
+
debug
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
createLogger,
|
|
67
|
+
createMetrics
|
|
68
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { fs } from "@modern-js/utils";
|
|
22
|
+
import LRU from "lru-cache";
|
|
23
|
+
const Byte = 1;
|
|
24
|
+
const KB = 1024 * Byte;
|
|
25
|
+
const MB = 1024 * KB;
|
|
26
|
+
const getContentLength = (cache) => cache.content.length;
|
|
27
|
+
const createCacheItem = (filepath, mtime) => __async(void 0, null, function* () {
|
|
28
|
+
const content = yield fs.readFile(filepath);
|
|
29
|
+
return {
|
|
30
|
+
content,
|
|
31
|
+
mtime
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
class LruReader {
|
|
35
|
+
constructor() {
|
|
36
|
+
this.cache = new LRU({
|
|
37
|
+
max: 256 * MB,
|
|
38
|
+
length: getContentLength,
|
|
39
|
+
maxAge: 5 * 60 * 5e3
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
init() {
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
}
|
|
46
|
+
read(filepath) {
|
|
47
|
+
return __async(this, null, function* () {
|
|
48
|
+
if (this.cache.has(filepath)) {
|
|
49
|
+
const { content } = this.cache.get(filepath);
|
|
50
|
+
return { content };
|
|
51
|
+
}
|
|
52
|
+
if (!fs.existsSync(filepath)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const stat = fs.statSync(filepath);
|
|
56
|
+
if (stat.isDirectory()) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (stat.size > 20 * MB) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const item = yield createCacheItem(filepath, stat.mtime);
|
|
63
|
+
this.cache.set(filepath, item);
|
|
64
|
+
return item;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
update() {
|
|
68
|
+
const { cache } = this;
|
|
69
|
+
const files = cache.keys();
|
|
70
|
+
for (const filepath of files) {
|
|
71
|
+
if (!fs.existsSync(filepath)) {
|
|
72
|
+
cache.del(filepath);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const item = cache.get(filepath);
|
|
76
|
+
const stat = fs.statSync(filepath);
|
|
77
|
+
const { mtime } = stat;
|
|
78
|
+
if (item.mtime < mtime) {
|
|
79
|
+
cache.del(filepath);
|
|
80
|
+
}
|
|
81
|
+
} catch (e) {
|
|
82
|
+
cache.del(filepath);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const reader = new LruReader();
|
|
88
|
+
const readFile = (filepath) => __async(void 0, null, function* () {
|
|
89
|
+
const file = yield reader.read(filepath);
|
|
90
|
+
return file == null ? void 0 : file.content;
|
|
91
|
+
});
|
|
92
|
+
const updateFile = () => {
|
|
93
|
+
reader.update();
|
|
94
|
+
};
|
|
95
|
+
const init = () => {
|
|
96
|
+
reader.init();
|
|
97
|
+
};
|
|
98
|
+
const close = () => {
|
|
99
|
+
reader.close();
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
LruReader,
|
|
103
|
+
close,
|
|
104
|
+
init,
|
|
105
|
+
readFile,
|
|
106
|
+
updateFile
|
|
107
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import path from "path";
|
|
22
|
+
import {
|
|
23
|
+
fs,
|
|
24
|
+
LOADABLE_STATS_FILE,
|
|
25
|
+
mime,
|
|
26
|
+
ROUTE_MINIFEST_FILE,
|
|
27
|
+
SERVER_RENDER_FUNCTION_NAME
|
|
28
|
+
} from "@modern-js/utils";
|
|
29
|
+
import cookie from "cookie";
|
|
30
|
+
import cache from "./cache";
|
|
31
|
+
import { createLogger, createMetrics } from "./measure";
|
|
32
|
+
const render = (ctx, renderOptions, runner) => __async(void 0, null, function* () {
|
|
33
|
+
var _a;
|
|
34
|
+
const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
|
|
35
|
+
const bundleJS = path.join(distDir, bundle);
|
|
36
|
+
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
37
|
+
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
38
|
+
const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
|
|
39
|
+
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
|
|
40
|
+
const context = {
|
|
41
|
+
request: {
|
|
42
|
+
baseUrl: urlPath,
|
|
43
|
+
params: ctx.params,
|
|
44
|
+
pathname: ctx.path,
|
|
45
|
+
host: ctx.host,
|
|
46
|
+
query: ctx.query,
|
|
47
|
+
url: ctx.href,
|
|
48
|
+
cookieMap: cookie.parse(ctx.headers.cookie || ""),
|
|
49
|
+
headers: ctx.headers
|
|
50
|
+
},
|
|
51
|
+
response: {
|
|
52
|
+
setHeader: (key, value) => {
|
|
53
|
+
return ctx.res.setHeader(key, value);
|
|
54
|
+
},
|
|
55
|
+
status: (code) => {
|
|
56
|
+
ctx.res.statusCode = code;
|
|
57
|
+
},
|
|
58
|
+
locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
|
|
59
|
+
},
|
|
60
|
+
redirection: {},
|
|
61
|
+
template,
|
|
62
|
+
loadableStats,
|
|
63
|
+
routeManifest,
|
|
64
|
+
entryName,
|
|
65
|
+
staticGenerate,
|
|
66
|
+
logger: void 0,
|
|
67
|
+
metrics: void 0,
|
|
68
|
+
req: ctx.req,
|
|
69
|
+
res: ctx.res
|
|
70
|
+
};
|
|
71
|
+
context.logger = createLogger(context, ctx.logger);
|
|
72
|
+
context.metrics = createMetrics(context, ctx.metrics);
|
|
73
|
+
runner.extendSSRContext(context);
|
|
74
|
+
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
75
|
+
const content = yield cache(serverRender, ctx)(context);
|
|
76
|
+
const { url, status = 302 } = context.redirection;
|
|
77
|
+
if (url) {
|
|
78
|
+
return {
|
|
79
|
+
content: url,
|
|
80
|
+
contentType: "",
|
|
81
|
+
statusCode: status,
|
|
82
|
+
redirect: true
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (typeof content === "string") {
|
|
86
|
+
return {
|
|
87
|
+
content,
|
|
88
|
+
contentType: mime.contentType("html")
|
|
89
|
+
};
|
|
90
|
+
} else {
|
|
91
|
+
return {
|
|
92
|
+
content: "",
|
|
93
|
+
contentStream: content,
|
|
94
|
+
contentType: mime.contentType("html")
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
export {
|
|
99
|
+
render
|
|
100
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import path from "path";
|
|
22
|
+
import { mime } from "@modern-js/utils";
|
|
23
|
+
import { readFile } from "./reader";
|
|
24
|
+
function handleDirectory(ctx, entryPath, urlPath) {
|
|
25
|
+
return __async(this, null, function* () {
|
|
26
|
+
const { path: pathname } = ctx;
|
|
27
|
+
const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
|
|
28
|
+
let content = yield readFile(filepath);
|
|
29
|
+
let contentType = mime.contentType(path.extname(filepath) || "");
|
|
30
|
+
if (!content) {
|
|
31
|
+
if (pathname.endsWith("/")) {
|
|
32
|
+
content = yield readFile(`${filepath}index.html`);
|
|
33
|
+
} else if (!pathname.includes(".")) {
|
|
34
|
+
content = yield readFile(`${filepath}.html`);
|
|
35
|
+
if (!content) {
|
|
36
|
+
content = yield readFile(`${filepath}/index.html`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (content) {
|
|
40
|
+
contentType = mime.contentType("html");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!content) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
content,
|
|
48
|
+
contentType: contentType || ""
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const trimLeft = (str, prefix) => {
|
|
53
|
+
if (str.startsWith(prefix)) {
|
|
54
|
+
return str.substring(prefix.length);
|
|
55
|
+
}
|
|
56
|
+
return str;
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
handleDirectory
|
|
60
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
match,
|
|
3
|
+
pathToRegexp,
|
|
4
|
+
compile
|
|
5
|
+
} from "path-to-regexp";
|
|
6
|
+
import { ModernRoute } from "./route";
|
|
7
|
+
const removeTailSlash = (s) => s.replace(/\/+$/, "");
|
|
8
|
+
const removeHtmlSuffix = (url) => {
|
|
9
|
+
if (url.endsWith(".html")) {
|
|
10
|
+
return url.slice(0, -5);
|
|
11
|
+
}
|
|
12
|
+
return url;
|
|
13
|
+
};
|
|
14
|
+
const toPath = (reg, params) => {
|
|
15
|
+
const fn = compile(reg, { encode: encodeURIComponent });
|
|
16
|
+
return fn(params);
|
|
17
|
+
};
|
|
18
|
+
const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
|
|
19
|
+
class RouteMatcher {
|
|
20
|
+
constructor(spec) {
|
|
21
|
+
this.urlPath = "";
|
|
22
|
+
this.spec = spec;
|
|
23
|
+
this.setupUrlPath();
|
|
24
|
+
}
|
|
25
|
+
generate(url) {
|
|
26
|
+
const route = new ModernRoute(this.spec);
|
|
27
|
+
if (this.urlPath) {
|
|
28
|
+
const params = this.parseURLParams(url);
|
|
29
|
+
route.urlPath = toPath(route.urlPath, params);
|
|
30
|
+
route.params = params;
|
|
31
|
+
}
|
|
32
|
+
return route;
|
|
33
|
+
}
|
|
34
|
+
parseURLParams(pathname) {
|
|
35
|
+
if (!this.urlMatcher) {
|
|
36
|
+
return {};
|
|
37
|
+
} else {
|
|
38
|
+
const matchResult = this.urlMatcher(pathname);
|
|
39
|
+
return matchResult.params;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
matchLength(pathname) {
|
|
43
|
+
var _a;
|
|
44
|
+
if (!this.urlReg) {
|
|
45
|
+
return this.urlPath.length;
|
|
46
|
+
} else {
|
|
47
|
+
const result = this.urlReg.exec(pathname);
|
|
48
|
+
return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
matchUrlPath(requestUrl) {
|
|
52
|
+
let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
|
|
53
|
+
urlWithoutSlash = removeHtmlSuffix(urlWithoutSlash);
|
|
54
|
+
if (this.urlMatcher) {
|
|
55
|
+
return Boolean(this.urlMatcher(urlWithoutSlash));
|
|
56
|
+
} else {
|
|
57
|
+
const urlPath = removeHtmlSuffix(this.urlPath);
|
|
58
|
+
if (urlWithoutSlash.startsWith(urlPath)) {
|
|
59
|
+
if (urlPath !== "/" && urlWithoutSlash.length > urlPath.length && !urlWithoutSlash.startsWith(`${urlPath}/`)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
matchEntry(entryName) {
|
|
68
|
+
return this.spec.entryName === entryName;
|
|
69
|
+
}
|
|
70
|
+
setupUrlPath() {
|
|
71
|
+
const { urlPath } = this.spec;
|
|
72
|
+
this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);
|
|
73
|
+
const useReg = regCharsDetector.test(urlPath);
|
|
74
|
+
if (useReg) {
|
|
75
|
+
this.urlMatcher = match(urlPath, {
|
|
76
|
+
end: false,
|
|
77
|
+
decode: decodeURIComponent
|
|
78
|
+
});
|
|
79
|
+
this.urlReg = pathToRegexp(urlPath, [], {
|
|
80
|
+
end: false
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
RouteMatcher
|
|
87
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class ModernRoute {
|
|
2
|
+
constructor(routeSpec) {
|
|
3
|
+
this.params = {};
|
|
4
|
+
this.entryName = routeSpec.entryName || "";
|
|
5
|
+
this.urlPath = routeSpec.urlPath;
|
|
6
|
+
this.entryPath = routeSpec.entryPath || "";
|
|
7
|
+
this.isSSR = routeSpec.isSSR || false;
|
|
8
|
+
this.isSPA = routeSpec.isSPA || false;
|
|
9
|
+
this.isApi = routeSpec.isApi || false;
|
|
10
|
+
this.bundle = routeSpec.bundle || "";
|
|
11
|
+
this.responseHeaders = routeSpec.responseHeaders;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
ModernRoute
|
|
16
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import serve from "serve-static";
|
|
22
|
+
import { isString, isRegExp } from "@modern-js/utils";
|
|
23
|
+
import { useLocalPrefix } from "../utils";
|
|
24
|
+
const removedPrefix = (req, prefix) => {
|
|
25
|
+
if (useLocalPrefix(prefix)) {
|
|
26
|
+
req.url = req.url.slice(prefix.length);
|
|
27
|
+
return () => {
|
|
28
|
+
req.url = prefix + req.url;
|
|
29
|
+
};
|
|
30
|
+
} else {
|
|
31
|
+
return () => {
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const faviconFallbackHandler = (context, next) => {
|
|
36
|
+
if (context.url === "/favicon.ico") {
|
|
37
|
+
context.res.statusCode = 204;
|
|
38
|
+
context.res.end();
|
|
39
|
+
} else {
|
|
40
|
+
next();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const createStaticFileHandler = (rules, output = {}) => (context, next) => __async(void 0, null, function* () {
|
|
44
|
+
const { url: requestUrl, req, res } = context;
|
|
45
|
+
const { assetPrefix = "/" } = output;
|
|
46
|
+
const hitRule = rules.find((item) => {
|
|
47
|
+
if (isString(item.path) && requestUrl.startsWith(item.path)) {
|
|
48
|
+
return true;
|
|
49
|
+
} else if (isRegExp(item.path) && item.path.test(requestUrl)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
});
|
|
54
|
+
if (hitRule) {
|
|
55
|
+
const resume = removedPrefix(req, assetPrefix);
|
|
56
|
+
serve(hitRule.target)(req, res, () => {
|
|
57
|
+
resume();
|
|
58
|
+
next();
|
|
59
|
+
});
|
|
60
|
+
} else {
|
|
61
|
+
return next();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
export {
|
|
65
|
+
createStaticFileHandler,
|
|
66
|
+
faviconFallbackHandler
|
|
67
|
+
};
|