@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.6
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 +139 -0
- package/dist/js/modern/constants.js +32 -25
- package/dist/js/modern/index.js +11 -6
- package/dist/js/modern/libs/context/context.js +52 -84
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +44 -35
- package/dist/js/modern/libs/hook-api/route.js +6 -7
- package/dist/js/modern/libs/hook-api/template.js +20 -34
- package/dist/js/modern/libs/loadConfig.js +45 -24
- package/dist/js/modern/libs/metrics.js +3 -4
- package/dist/js/modern/libs/proxy.js +68 -37
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
- package/dist/js/modern/libs/render/cache/index.js +88 -54
- package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
- package/dist/js/modern/libs/render/cache/spr.js +133 -117
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -39
- package/dist/js/modern/libs/render/index.js +76 -56
- package/dist/js/modern/libs/render/measure.js +38 -27
- package/dist/js/modern/libs/render/reader.js +65 -62
- package/dist/js/modern/libs/render/ssr.js +50 -32
- package/dist/js/modern/libs/render/static.js +50 -33
- package/dist/js/modern/libs/render/type.js +9 -6
- package/dist/js/modern/libs/route/index.js +8 -15
- package/dist/js/modern/libs/route/matcher.js +20 -34
- package/dist/js/modern/libs/route/route.js +9 -18
- package/dist/js/modern/libs/serve-file.js +33 -20
- package/dist/js/modern/server/index.js +150 -145
- package/dist/js/modern/server/modern-server-split.js +46 -12
- package/dist/js/modern/server/modern-server.js +384 -422
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -43
- package/dist/js/modern/worker-server.js +34 -14
- package/dist/js/node/constants.js +53 -30
- package/dist/js/node/index.js +37 -57
- package/dist/js/node/libs/context/context.js +83 -94
- package/dist/js/node/libs/context/index.js +28 -13
- package/dist/js/node/libs/hook-api/index.js +76 -48
- package/dist/js/node/libs/hook-api/route.js +26 -11
- package/dist/js/node/libs/hook-api/template.js +41 -39
- package/dist/js/node/libs/loadConfig.js +73 -35
- package/dist/js/node/libs/metrics.js +25 -9
- package/dist/js/node/libs/proxy.js +89 -44
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -56
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -128
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
- package/dist/js/node/libs/render/cache/index.js +110 -64
- package/dist/js/node/libs/render/cache/page-caches/index.js +54 -14
- package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -12
- package/dist/js/node/libs/render/cache/spr.js +161 -129
- package/dist/js/node/libs/render/cache/type.js +15 -5
- package/dist/js/node/libs/render/cache/util.js +99 -45
- package/dist/js/node/libs/render/index.js +106 -67
- package/dist/js/node/libs/render/measure.js +58 -31
- package/dist/js/node/libs/render/reader.js +95 -70
- package/dist/js/node/libs/render/ssr.js +80 -47
- package/dist/js/node/libs/render/static.js +79 -40
- package/dist/js/node/libs/render/type.js +31 -12
- package/dist/js/node/libs/route/index.js +31 -26
- package/dist/js/node/libs/route/matcher.js +40 -41
- package/dist/js/node/libs/route/route.js +29 -22
- package/dist/js/node/libs/serve-file.js +66 -32
- package/dist/js/node/server/index.js +168 -160
- package/dist/js/node/server/modern-server-split.js +72 -22
- package/dist/js/node/server/modern-server.js +403 -445
- package/dist/js/node/type.js +15 -3
- package/dist/js/node/utils.js +85 -52
- package/dist/js/node/worker-server.js +57 -21
- package/dist/js/treeshaking/constants.js +26 -25
- package/dist/js/treeshaking/index.js +10 -10
- package/dist/js/treeshaking/libs/context/context.js +268 -237
- package/dist/js/treeshaking/libs/context/index.js +3 -3
- package/dist/js/treeshaking/libs/hook-api/index.js +267 -143
- package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
- package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
- package/dist/js/treeshaking/libs/loadConfig.js +80 -37
- package/dist/js/treeshaking/libs/metrics.js +4 -10
- package/dist/js/treeshaking/libs/proxy.js +240 -76
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
- package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
- package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
- package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
- package/dist/js/treeshaking/libs/render/index.js +228 -95
- package/dist/js/treeshaking/libs/render/measure.js +142 -57
- package/dist/js/treeshaking/libs/render/reader.js +325 -177
- package/dist/js/treeshaking/libs/render/ssr.js +220 -95
- package/dist/js/treeshaking/libs/render/static.js +210 -78
- package/dist/js/treeshaking/libs/render/type.js +7 -6
- package/dist/js/treeshaking/libs/route/index.js +125 -89
- package/dist/js/treeshaking/libs/route/matcher.js +132 -107
- package/dist/js/treeshaking/libs/route/route.js +40 -26
- package/dist/js/treeshaking/libs/serve-file.js +177 -68
- package/dist/js/treeshaking/server/index.js +493 -327
- package/dist/js/treeshaking/server/modern-server-split.js +352 -144
- package/dist/js/treeshaking/server/modern-server.js +1048 -909
- package/dist/js/treeshaking/type.js +1 -1
- package/dist/js/treeshaking/utils.js +138 -81
- package/dist/js/treeshaking/worker-server.js +176 -55
- package/dist/types/index.d.ts +2 -0
- package/dist/types/libs/context/context.d.ts +4 -1
- package/dist/types/libs/loadConfig.d.ts +1 -0
- package/dist/types/libs/render/cache/index.d.ts +2 -0
- package/dist/types/libs/render/cache/spr.d.ts +2 -0
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/worker-server.d.ts +1 -2
- package/package.json +7 -14
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -37
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -46
- package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
- package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,33 +1,89 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __objRest = (source, exclude) => {
|
|
18
|
+
var target = {};
|
|
19
|
+
for (var prop in source)
|
|
20
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
21
|
+
target[prop] = source[prop];
|
|
22
|
+
if (source != null && __getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
24
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
import { createServer } from "http";
|
|
50
|
+
import path from "path";
|
|
51
|
+
import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
|
52
|
+
import {
|
|
53
|
+
RouteMatchManager
|
|
54
|
+
} from "../libs/route";
|
|
13
55
|
import { createRenderHandler } from "../libs/render";
|
|
14
|
-
import {
|
|
15
|
-
|
|
56
|
+
import {
|
|
57
|
+
createStaticFileHandler,
|
|
58
|
+
faviconFallbackHandler
|
|
59
|
+
} from "../libs/serve-file";
|
|
60
|
+
import {
|
|
61
|
+
createErrorDocument,
|
|
62
|
+
createMiddlewareCollecter,
|
|
63
|
+
getStaticReg,
|
|
64
|
+
mergeExtension,
|
|
65
|
+
noop,
|
|
66
|
+
debug,
|
|
67
|
+
isRedirect
|
|
68
|
+
} from "../utils";
|
|
16
69
|
import * as reader from "../libs/render/reader";
|
|
17
70
|
import { createProxyHandler } from "../libs/proxy";
|
|
18
71
|
import { createContext } from "../libs/context";
|
|
19
72
|
import { templateInjectableStream } from "../libs/hook-api/template";
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
73
|
+
import {
|
|
74
|
+
AGGRED_DIR,
|
|
75
|
+
ERROR_DIGEST,
|
|
76
|
+
ERROR_PAGE_TEXT,
|
|
77
|
+
RUN_MODE
|
|
78
|
+
} from "../constants";
|
|
79
|
+
import {
|
|
80
|
+
createAfterMatchContext,
|
|
81
|
+
createAfterRenderContext,
|
|
82
|
+
createMiddlewareContext
|
|
83
|
+
} from "../libs/hook-api";
|
|
84
|
+
const API_DIR = "./api";
|
|
85
|
+
const SERVER_DIR = "./server";
|
|
86
|
+
class ModernServer {
|
|
31
87
|
constructor({
|
|
32
88
|
pwd,
|
|
33
89
|
config,
|
|
@@ -38,33 +94,18 @@ export class ModernServer {
|
|
|
38
94
|
runMode,
|
|
39
95
|
proxyTarget
|
|
40
96
|
}) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
_defineProperty(this, "runner", void 0);
|
|
49
|
-
_defineProperty(this, "logger", void 0);
|
|
50
|
-
_defineProperty(this, "metrics", void 0);
|
|
51
|
-
_defineProperty(this, "runMode", void 0);
|
|
52
|
-
_defineProperty(this, "reader", reader);
|
|
53
|
-
_defineProperty(this, "proxyTarget", void 0);
|
|
54
|
-
_defineProperty(this, "staticFileHandler", void 0);
|
|
55
|
-
_defineProperty(this, "routeRenderHandler", void 0);
|
|
56
|
-
_defineProperty(this, "beforeRouteHandler", null);
|
|
57
|
-
_defineProperty(this, "frameWebHandler", null);
|
|
58
|
-
_defineProperty(this, "frameAPIHandler", null);
|
|
59
|
-
_defineProperty(this, "proxyHandler", null);
|
|
60
|
-
_defineProperty(this, "_handler", void 0);
|
|
61
|
-
_defineProperty(this, "staticGenerate", void 0);
|
|
62
|
-
require('ignore-styles');
|
|
97
|
+
this.handlers = [];
|
|
98
|
+
this.reader = reader;
|
|
99
|
+
this.beforeRouteHandler = null;
|
|
100
|
+
this.frameWebHandler = null;
|
|
101
|
+
this.frameAPIHandler = null;
|
|
102
|
+
this.proxyHandler = null;
|
|
103
|
+
require("ignore-styles");
|
|
63
104
|
this.pwd = pwd;
|
|
64
|
-
this.distDir = path.join(pwd, config.output.path ||
|
|
105
|
+
this.distDir = path.join(pwd, config.output.path || "dist");
|
|
65
106
|
this.workDir = this.distDir;
|
|
66
107
|
this.conf = config;
|
|
67
|
-
debug(
|
|
108
|
+
debug("server conf", this.conf);
|
|
68
109
|
this.logger = logger;
|
|
69
110
|
this.metrics = metrics;
|
|
70
111
|
this.router = new RouteMatchManager();
|
|
@@ -72,133 +113,104 @@ export class ModernServer {
|
|
|
72
113
|
this.proxyTarget = proxyTarget;
|
|
73
114
|
this.staticGenerate = staticGenerate || false;
|
|
74
115
|
this.runMode = runMode || RUN_MODE.FULL;
|
|
75
|
-
// process.env.BUILD_TYPE = `${this.staticGenerate ? 'ssg' : 'ssr'}`;
|
|
76
116
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
this.addHandler(handler);
|
|
117
|
+
onInit(runner, app) {
|
|
118
|
+
return __async(this, null, function* () {
|
|
119
|
+
var _a;
|
|
120
|
+
this.runner = runner;
|
|
121
|
+
const { distDir, staticGenerate, conf } = this;
|
|
122
|
+
debug("final server conf", this.conf);
|
|
123
|
+
this.proxyHandler = createProxyHandler((_a = conf.bff) == null ? void 0 : _a.proxy);
|
|
124
|
+
if (this.proxyHandler) {
|
|
125
|
+
this.proxyHandler.forEach((handler) => {
|
|
126
|
+
this.addHandler(handler);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
this.reader.init();
|
|
130
|
+
app.on("close", () => {
|
|
131
|
+
this.reader.close();
|
|
93
132
|
});
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
133
|
+
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
134
|
+
this.router.reset(usageRoutes);
|
|
135
|
+
this.warmupSSRBundle();
|
|
136
|
+
yield this.prepareFrameHandler();
|
|
137
|
+
yield this.prepareBeforeRouteHandler(usageRoutes, distDir);
|
|
138
|
+
const staticPathRegExp = getStaticReg(
|
|
139
|
+
this.conf.output || {},
|
|
140
|
+
this.conf.html
|
|
141
|
+
);
|
|
142
|
+
this.staticFileHandler = createStaticFileHandler(
|
|
143
|
+
[
|
|
144
|
+
{
|
|
145
|
+
path: staticPathRegExp,
|
|
146
|
+
target: distDir
|
|
147
|
+
}
|
|
148
|
+
],
|
|
149
|
+
this.conf.output
|
|
150
|
+
);
|
|
151
|
+
this.routeRenderHandler = createRenderHandler({
|
|
152
|
+
distDir,
|
|
153
|
+
staticGenerate
|
|
154
|
+
});
|
|
155
|
+
yield this.setupBeforeProdMiddleware();
|
|
156
|
+
this.addHandler(this.staticFileHandler);
|
|
157
|
+
this.addHandler(faviconFallbackHandler);
|
|
158
|
+
this.addBeforeRouteHandler();
|
|
159
|
+
this.addHandler(this.routeHandler.bind(this));
|
|
160
|
+
this.compose();
|
|
121
161
|
});
|
|
122
|
-
await this.setupBeforeProdMiddleware();
|
|
123
|
-
this.addHandler(this.staticFileHandler);
|
|
124
|
-
|
|
125
|
-
// execute after staticFileHandler, can rename to staticFallbackHandler if needed.
|
|
126
|
-
this.addHandler(faviconFallbackHandler);
|
|
127
|
-
this.addBeforeRouteHandler();
|
|
128
|
-
this.addHandler(this.routeHandler.bind(this));
|
|
129
|
-
|
|
130
|
-
// compose middlewares to http handler
|
|
131
|
-
this.compose();
|
|
132
162
|
}
|
|
133
|
-
|
|
134
|
-
// server ready
|
|
135
163
|
onRepack(_) {
|
|
136
|
-
// empty
|
|
137
164
|
}
|
|
138
165
|
addBeforeRouteHandler() {
|
|
139
|
-
this.addHandler(
|
|
166
|
+
this.addHandler((context, next) => __async(this, null, function* () {
|
|
140
167
|
if (this.beforeRouteHandler) {
|
|
141
|
-
|
|
168
|
+
yield this.beforeRouteHandler(context);
|
|
142
169
|
if (this.isSend(context.res)) {
|
|
143
170
|
return;
|
|
144
171
|
}
|
|
145
172
|
}
|
|
146
|
-
// eslint-disable-next-line consistent-return
|
|
147
173
|
return next();
|
|
148
|
-
});
|
|
174
|
+
}));
|
|
149
175
|
}
|
|
150
|
-
onServerChange({
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const {
|
|
154
|
-
pwd
|
|
155
|
-
} = this;
|
|
156
|
-
const {
|
|
157
|
-
api,
|
|
158
|
-
server
|
|
159
|
-
} = AGGRED_DIR;
|
|
176
|
+
onServerChange({ filepath }) {
|
|
177
|
+
const { pwd } = this;
|
|
178
|
+
const { api, server } = AGGRED_DIR;
|
|
160
179
|
const apiPath = path.normalize(path.join(pwd, api));
|
|
161
180
|
const serverPath = path.normalize(path.join(pwd, server));
|
|
162
181
|
const onlyApi = filepath.startsWith(apiPath);
|
|
163
182
|
const onlyWeb = filepath.startsWith(serverPath);
|
|
164
|
-
this.prepareFrameHandler({
|
|
165
|
-
onlyWeb,
|
|
166
|
-
onlyApi
|
|
167
|
-
});
|
|
183
|
+
this.prepareFrameHandler({ onlyWeb, onlyApi });
|
|
168
184
|
}
|
|
169
|
-
|
|
170
|
-
// exposed requestHandler
|
|
171
185
|
getRequestHandler() {
|
|
172
186
|
return this.requestHandler.bind(this);
|
|
173
187
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
render(req, res, url) {
|
|
189
|
+
return __async(this, null, function* () {
|
|
190
|
+
req.logger = this.logger;
|
|
191
|
+
req.metrics = this.metrics;
|
|
192
|
+
const context = createContext(req, res);
|
|
193
|
+
const matched = this.router.match(url || context.path);
|
|
194
|
+
if (!matched) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
const route = matched.generate(context.url);
|
|
198
|
+
const result = yield this.handleWeb(context, route);
|
|
199
|
+
if (!result) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
return result.content.toString();
|
|
203
|
+
});
|
|
188
204
|
}
|
|
189
|
-
|
|
190
|
-
return
|
|
205
|
+
createHTTPServer(handler) {
|
|
206
|
+
return __async(this, null, function* () {
|
|
207
|
+
return createServer(handler);
|
|
208
|
+
});
|
|
191
209
|
}
|
|
192
|
-
|
|
193
|
-
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
194
|
-
// get routes info
|
|
195
210
|
getRoutes() {
|
|
196
|
-
// Preferred to use preset routes
|
|
197
211
|
if (this.presetRoutes) {
|
|
198
212
|
return this.presetRoutes;
|
|
199
213
|
}
|
|
200
|
-
|
|
201
|
-
// read routes from spec file
|
|
202
214
|
const file = path.join(this.distDir, ROUTE_SPEC_FILE);
|
|
203
215
|
if (fs.existsSync(file)) {
|
|
204
216
|
const content = fs.readJSONSync(file);
|
|
@@ -206,301 +218,249 @@ export class ModernServer {
|
|
|
206
218
|
}
|
|
207
219
|
return [];
|
|
208
220
|
}
|
|
209
|
-
|
|
210
|
-
// add promisify request handler to server
|
|
211
|
-
// handler should do not do more things after invoke next
|
|
212
221
|
addHandler(handler) {
|
|
213
|
-
|
|
214
|
-
this.handlers.push(handler);
|
|
215
|
-
} else {
|
|
216
|
-
this.handlers.push(util.promisify(handler));
|
|
217
|
-
}
|
|
222
|
+
this.handlers.push(handler);
|
|
218
223
|
}
|
|
219
|
-
|
|
220
|
-
// return 404 page
|
|
221
224
|
render404(context) {
|
|
222
|
-
context.error(ERROR_DIGEST.ENOTF,
|
|
225
|
+
context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
|
|
223
226
|
this.renderErrorPage(context, 404);
|
|
224
227
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
runner
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
228
|
+
prepareBeforeRouteHandler(specs, distDir) {
|
|
229
|
+
return __async(this, null, function* () {
|
|
230
|
+
const { runner } = this;
|
|
231
|
+
const handler = yield runner.preparebeforeRouteHandler(
|
|
232
|
+
{
|
|
233
|
+
serverRoutes: specs,
|
|
234
|
+
distDir
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
onLast: () => null
|
|
238
|
+
}
|
|
239
|
+
);
|
|
240
|
+
this.beforeRouteHandler = handler;
|
|
234
241
|
});
|
|
235
|
-
this.beforeRouteHandler = handler;
|
|
236
242
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
runner
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
await runner.gather(collector);
|
|
256
|
-
const {
|
|
257
|
-
api: pluginAPIExt,
|
|
258
|
-
web: pluginWebExt
|
|
259
|
-
} = getMiddlewares();
|
|
260
|
-
const apiDir = path.join(workDir, API_DIR);
|
|
261
|
-
const serverDir = path.join(workDir, SERVER_DIR);
|
|
262
|
-
|
|
263
|
-
// get api or web server handler from server-framework plugin
|
|
264
|
-
if ((await fs.pathExists(path.join(serverDir))) && !onlyApi) {
|
|
265
|
-
const webExtension = mergeExtension(pluginWebExt);
|
|
266
|
-
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
267
|
-
}
|
|
268
|
-
if (fs.existsSync(apiDir) && !onlyWeb) {
|
|
269
|
-
const apiExtension = mergeExtension(pluginAPIExt);
|
|
270
|
-
this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
|
|
271
|
-
}
|
|
243
|
+
prepareFrameHandler(options) {
|
|
244
|
+
return __async(this, null, function* () {
|
|
245
|
+
const { workDir, runner } = this;
|
|
246
|
+
const { onlyApi, onlyWeb } = options || {};
|
|
247
|
+
const _a = createMiddlewareCollecter(), { getMiddlewares } = _a, collector = __objRest(_a, ["getMiddlewares"]);
|
|
248
|
+
yield runner.gather(collector);
|
|
249
|
+
const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
|
|
250
|
+
const apiDir = path.join(workDir, API_DIR);
|
|
251
|
+
const serverDir = path.join(workDir, SERVER_DIR);
|
|
252
|
+
if ((yield fs.pathExists(path.join(serverDir))) && !onlyApi) {
|
|
253
|
+
const webExtension = mergeExtension(pluginWebExt);
|
|
254
|
+
this.frameWebHandler = yield this.prepareWebHandler(webExtension);
|
|
255
|
+
}
|
|
256
|
+
if (fs.existsSync(apiDir) && !onlyWeb) {
|
|
257
|
+
const apiExtension = mergeExtension(pluginAPIExt);
|
|
258
|
+
this.frameAPIHandler = yield this.prepareAPIHandler(apiExtension);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
272
261
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
workDir,
|
|
276
|
-
runner
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
262
|
+
prepareWebHandler(extension) {
|
|
263
|
+
return __async(this, null, function* () {
|
|
264
|
+
const { workDir, runner } = this;
|
|
265
|
+
const handler = yield runner.prepareWebServer(
|
|
266
|
+
{
|
|
267
|
+
pwd: workDir,
|
|
268
|
+
config: extension
|
|
269
|
+
},
|
|
270
|
+
{ onLast: () => null }
|
|
271
|
+
);
|
|
272
|
+
return handler;
|
|
283
273
|
});
|
|
284
|
-
return handler;
|
|
285
274
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
workDir,
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
prefix: Array.isArray(prefix) ? prefix[0] : prefix
|
|
300
|
-
}, {
|
|
301
|
-
onLast: () => null
|
|
275
|
+
prepareAPIHandler(extension) {
|
|
276
|
+
return __async(this, null, function* () {
|
|
277
|
+
const { workDir, runner, conf } = this;
|
|
278
|
+
const { bff } = conf;
|
|
279
|
+
const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
|
|
280
|
+
return runner.prepareApiServer(
|
|
281
|
+
{
|
|
282
|
+
pwd: workDir,
|
|
283
|
+
config: extension,
|
|
284
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix
|
|
285
|
+
},
|
|
286
|
+
{ onLast: () => null }
|
|
287
|
+
);
|
|
302
288
|
});
|
|
303
289
|
}
|
|
304
290
|
filterRoutes(routes) {
|
|
305
291
|
return routes;
|
|
306
292
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
conf,
|
|
310
|
-
runner
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
this.addHandler(mid);
|
|
293
|
+
setupBeforeProdMiddleware() {
|
|
294
|
+
return __async(this, null, function* () {
|
|
295
|
+
const { conf, runner } = this;
|
|
296
|
+
const preMiddleware = yield runner.beforeProdServer(conf);
|
|
297
|
+
preMiddleware.flat().forEach((mid) => {
|
|
298
|
+
this.addHandler(mid);
|
|
299
|
+
});
|
|
315
300
|
});
|
|
316
301
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
req,
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
}
|
|
325
|
-
await this.frameAPIHandler(req, res);
|
|
302
|
+
handleAPI(context) {
|
|
303
|
+
return __async(this, null, function* () {
|
|
304
|
+
const { req, res } = context;
|
|
305
|
+
if (!this.frameAPIHandler) {
|
|
306
|
+
throw new Error("can not found api handler");
|
|
307
|
+
}
|
|
308
|
+
yield this.frameAPIHandler(req, res);
|
|
309
|
+
});
|
|
326
310
|
}
|
|
327
|
-
|
|
328
|
-
return this
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
311
|
+
handleWeb(context, route) {
|
|
312
|
+
return __async(this, null, function* () {
|
|
313
|
+
return this.routeRenderHandler({
|
|
314
|
+
ctx: context,
|
|
315
|
+
route,
|
|
316
|
+
runner: this.runner
|
|
317
|
+
});
|
|
332
318
|
});
|
|
333
319
|
}
|
|
334
|
-
|
|
335
|
-
return null
|
|
320
|
+
proxy() {
|
|
321
|
+
return __async(this, null, function* () {
|
|
322
|
+
return null;
|
|
323
|
+
});
|
|
336
324
|
}
|
|
337
|
-
|
|
338
|
-
// warmup ssr function
|
|
339
325
|
warmupSSRBundle() {
|
|
340
|
-
const {
|
|
341
|
-
distDir
|
|
342
|
-
} = this;
|
|
326
|
+
const { distDir } = this;
|
|
343
327
|
const bundles = this.router.getBundles();
|
|
344
|
-
bundles.forEach(bundle => {
|
|
328
|
+
bundles.forEach((bundle) => {
|
|
345
329
|
const filepath = path.join(distDir, bundle);
|
|
346
|
-
// if error, just throw and let process die
|
|
347
330
|
require(filepath);
|
|
348
331
|
});
|
|
349
332
|
}
|
|
350
333
|
createContext(req, res, options = {}) {
|
|
351
334
|
return createContext(req, res, options);
|
|
352
335
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const {
|
|
358
|
-
res
|
|
359
|
-
} = context;
|
|
360
|
-
|
|
361
|
-
// match routes in the route spec
|
|
362
|
-
const matched = this.router.match(context.path);
|
|
363
|
-
if (!matched) {
|
|
364
|
-
this.render404(context);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// route is api service
|
|
369
|
-
let route = matched.generate(context.url);
|
|
370
|
-
if (route.isApi) {
|
|
371
|
-
await this.handleAPI(context);
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
const afterMatchContext = createAfterMatchContext(context, route.entryName);
|
|
375
|
-
|
|
376
|
-
// only full mode run server hook
|
|
377
|
-
if (this.runMode === RUN_MODE.FULL) {
|
|
378
|
-
await this.runner.afterMatch(afterMatchContext, {
|
|
379
|
-
onLast: noop
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
if (this.isSend(res)) {
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
const {
|
|
386
|
-
current,
|
|
387
|
-
url,
|
|
388
|
-
status
|
|
389
|
-
} = afterMatchContext.router;
|
|
390
|
-
// redirect to another url
|
|
391
|
-
if (url) {
|
|
392
|
-
this.redirect(res, url, status);
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// rewrite to another entry
|
|
397
|
-
if (route.entryName !== current) {
|
|
398
|
-
const matched = this.router.matchEntry(current);
|
|
336
|
+
routeHandler(context) {
|
|
337
|
+
return __async(this, null, function* () {
|
|
338
|
+
const { res } = context;
|
|
339
|
+
const matched = this.router.match(context.path);
|
|
399
340
|
if (!matched) {
|
|
400
341
|
this.render404(context);
|
|
401
342
|
return;
|
|
402
343
|
}
|
|
403
|
-
route = matched.generate(context.url);
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
context.res.setHeader(key, value);
|
|
344
|
+
let route = matched.generate(context.url);
|
|
345
|
+
if (route.isApi) {
|
|
346
|
+
yield this.handleAPI(context);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const afterMatchContext = createAfterMatchContext(context, route.entryName);
|
|
350
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
351
|
+
yield this.runner.afterMatch(afterMatchContext, { onLast: noop });
|
|
352
|
+
}
|
|
353
|
+
if (this.isSend(res)) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const { current, url, status } = afterMatchContext.router;
|
|
357
|
+
if (url) {
|
|
358
|
+
this.redirect(res, url, status);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (route.entryName !== current) {
|
|
362
|
+
const matched2 = this.router.matchEntry(current);
|
|
363
|
+
if (!matched2) {
|
|
364
|
+
this.render404(context);
|
|
365
|
+
return;
|
|
426
366
|
}
|
|
367
|
+
route = matched2.generate(context.url);
|
|
368
|
+
}
|
|
369
|
+
context.setParams(route.params);
|
|
370
|
+
context.setServerData("router", {
|
|
371
|
+
baseUrl: route.urlPath,
|
|
372
|
+
params: route.params
|
|
427
373
|
});
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
res.setHeader('content-type', renderResult.contentType);
|
|
444
|
-
const {
|
|
445
|
-
contentStream
|
|
446
|
-
} = renderResult;
|
|
447
|
-
if (contentStream) {
|
|
448
|
-
contentStream.pipe(templateInjectableStream({
|
|
449
|
-
prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>` : undefined
|
|
450
|
-
})).pipe(res);
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
let response = renderResult.content;
|
|
454
|
-
if (route.entryName) {
|
|
455
|
-
const afterRenderContext = createAfterRenderContext(context, response.toString());
|
|
456
|
-
|
|
457
|
-
// only full mode run server hook
|
|
458
|
-
// FIXME: how to run server hook in streaming
|
|
459
|
-
if (this.runMode === RUN_MODE.FULL) {
|
|
460
|
-
await this.runner.afterRender(afterRenderContext, {
|
|
461
|
-
onLast: noop
|
|
374
|
+
if (this.frameWebHandler) {
|
|
375
|
+
res.locals = res.locals || {};
|
|
376
|
+
const middlewareContext = createMiddlewareContext(context);
|
|
377
|
+
yield this.frameWebHandler(middlewareContext);
|
|
378
|
+
res.locals = __spreadValues(__spreadValues({}, res.locals), middlewareContext.response.locals);
|
|
379
|
+
}
|
|
380
|
+
if (this.isSend(res)) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (route.responseHeaders) {
|
|
384
|
+
Object.keys(route.responseHeaders).forEach((key) => {
|
|
385
|
+
const value = route.responseHeaders[key];
|
|
386
|
+
if (value) {
|
|
387
|
+
context.res.setHeader(key, value);
|
|
388
|
+
}
|
|
462
389
|
});
|
|
463
390
|
}
|
|
391
|
+
const renderResult = yield this.handleWeb(context, route);
|
|
392
|
+
if (!renderResult) {
|
|
393
|
+
this.render404(context);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (renderResult.redirect) {
|
|
397
|
+
this.redirect(
|
|
398
|
+
res,
|
|
399
|
+
renderResult.content,
|
|
400
|
+
renderResult.statusCode
|
|
401
|
+
);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
464
404
|
if (this.isSend(res)) {
|
|
465
405
|
return;
|
|
466
406
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
407
|
+
res.setHeader("content-type", renderResult.contentType);
|
|
408
|
+
const { contentStream } = renderResult;
|
|
409
|
+
if (contentStream) {
|
|
410
|
+
contentStream.pipe(
|
|
411
|
+
templateInjectableStream({
|
|
412
|
+
prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
|
|
413
|
+
context.serverData
|
|
414
|
+
)}<\/script>` : void 0
|
|
415
|
+
})
|
|
416
|
+
).pipe(res);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
let response = renderResult.content;
|
|
420
|
+
if (route.entryName) {
|
|
421
|
+
const afterRenderContext = createAfterRenderContext(
|
|
422
|
+
context,
|
|
423
|
+
response.toString()
|
|
424
|
+
);
|
|
425
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
426
|
+
yield this.runner.afterRender(afterRenderContext, { onLast: noop });
|
|
427
|
+
}
|
|
428
|
+
if (this.isSend(res)) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
afterRenderContext.template.prependHead(
|
|
432
|
+
`<script>window._SERVER_DATA=${JSON.stringify(
|
|
433
|
+
context.serverData
|
|
434
|
+
)}<\/script>`
|
|
435
|
+
);
|
|
436
|
+
response = afterRenderContext.template.get();
|
|
437
|
+
}
|
|
438
|
+
res.end(response);
|
|
439
|
+
});
|
|
476
440
|
}
|
|
477
441
|
isSend(res) {
|
|
478
442
|
if (res.headersSent) {
|
|
479
443
|
return true;
|
|
480
444
|
}
|
|
481
|
-
if (res.getHeader(
|
|
445
|
+
if (res.getHeader("Location") && isRedirect(res.statusCode)) {
|
|
482
446
|
res.end();
|
|
483
447
|
return true;
|
|
484
448
|
}
|
|
485
449
|
return false;
|
|
486
450
|
}
|
|
487
|
-
|
|
488
|
-
// compose handlers and create the final handler
|
|
489
451
|
compose() {
|
|
490
|
-
const {
|
|
491
|
-
handlers
|
|
492
|
-
} = this;
|
|
452
|
+
const { handlers } = this;
|
|
493
453
|
if (!Array.isArray(handlers)) {
|
|
494
|
-
throw new TypeError(
|
|
454
|
+
throw new TypeError("Middleware stack must be an array!");
|
|
495
455
|
}
|
|
496
456
|
for (const fn of handlers) {
|
|
497
|
-
if (typeof fn !==
|
|
498
|
-
throw new TypeError(
|
|
457
|
+
if (typeof fn !== "function") {
|
|
458
|
+
throw new TypeError("Middleware must be composed of functions!");
|
|
499
459
|
}
|
|
500
460
|
}
|
|
501
461
|
this._handler = (context, next) => {
|
|
502
462
|
let i = 0;
|
|
503
|
-
const dispatch = error => {
|
|
463
|
+
const dispatch = (error) => {
|
|
504
464
|
if (error) {
|
|
505
465
|
return this.onError(context, error);
|
|
506
466
|
}
|
|
@@ -508,16 +468,22 @@ export class ModernServer {
|
|
|
508
468
|
if (!handler) {
|
|
509
469
|
return next();
|
|
510
470
|
}
|
|
511
|
-
|
|
471
|
+
try {
|
|
472
|
+
const result = handler(context, dispatch);
|
|
473
|
+
if (isPromise(result)) {
|
|
474
|
+
return result.catch(onError);
|
|
475
|
+
}
|
|
476
|
+
} catch (e) {
|
|
477
|
+
return onError(e);
|
|
478
|
+
}
|
|
512
479
|
};
|
|
513
|
-
const onError = err => {
|
|
480
|
+
const onError = (err) => {
|
|
514
481
|
this.onError(context, err);
|
|
515
482
|
};
|
|
516
483
|
return dispatch();
|
|
517
484
|
};
|
|
518
485
|
}
|
|
519
486
|
requestHandler(req, res, next = () => {
|
|
520
|
-
// empty
|
|
521
487
|
}) {
|
|
522
488
|
res.statusCode = 200;
|
|
523
489
|
req.logger = this.logger;
|
|
@@ -528,7 +494,7 @@ export class ModernServer {
|
|
|
528
494
|
} catch (e) {
|
|
529
495
|
this.logger.error(e);
|
|
530
496
|
res.statusCode = 500;
|
|
531
|
-
res.setHeader(
|
|
497
|
+
res.setHeader("content-type", mime.contentType("html"));
|
|
532
498
|
return res.end(createErrorDocument(500, ERROR_PAGE_TEXT[500]));
|
|
533
499
|
}
|
|
534
500
|
try {
|
|
@@ -538,7 +504,7 @@ export class ModernServer {
|
|
|
538
504
|
}
|
|
539
505
|
}
|
|
540
506
|
redirect(res, url, status = 302) {
|
|
541
|
-
res.setHeader(
|
|
507
|
+
res.setHeader("Location", url);
|
|
542
508
|
res.statusCode = status;
|
|
543
509
|
res.end();
|
|
544
510
|
}
|
|
@@ -546,41 +512,37 @@ export class ModernServer {
|
|
|
546
512
|
context.error(ERROR_DIGEST.EINTER, err);
|
|
547
513
|
this.renderErrorPage(context, 500);
|
|
548
514
|
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
res
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
});
|
|
573
|
-
if (file) {
|
|
574
|
-
context.res.end(file.content);
|
|
575
|
-
return;
|
|
515
|
+
renderErrorPage(context, status) {
|
|
516
|
+
return __async(this, null, function* () {
|
|
517
|
+
const { res } = context;
|
|
518
|
+
context.status = status;
|
|
519
|
+
res.setHeader("content-type", mime.contentType("html"));
|
|
520
|
+
const statusPage = `/${status}`;
|
|
521
|
+
const customErrorPage = `/_error`;
|
|
522
|
+
const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
|
|
523
|
+
if (matched) {
|
|
524
|
+
const route = matched.generate(context.url);
|
|
525
|
+
const { entryName } = route;
|
|
526
|
+
if (entryName === status.toString() || entryName === "_error") {
|
|
527
|
+
try {
|
|
528
|
+
const file = yield this.routeRenderHandler({
|
|
529
|
+
route,
|
|
530
|
+
ctx: context,
|
|
531
|
+
runner: this.runner
|
|
532
|
+
});
|
|
533
|
+
if (file) {
|
|
534
|
+
context.res.end(file.content);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
} catch (e) {
|
|
576
538
|
}
|
|
577
|
-
} catch (e) {
|
|
578
|
-
// just catch error when the rendering error occurred in the custom error page.
|
|
579
539
|
}
|
|
580
540
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
541
|
+
const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT[500];
|
|
542
|
+
context.res.end(createErrorDocument(status, text));
|
|
543
|
+
});
|
|
584
544
|
}
|
|
585
545
|
}
|
|
586
|
-
|
|
546
|
+
export {
|
|
547
|
+
ModernServer
|
|
548
|
+
};
|