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