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