@modern-js/prod-server 2.6.0 → 2.6.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/libs/{serve-file.js → serveFile.js} +3 -3
- package/dist/cjs/server/index.js +6 -4
- package/dist/cjs/server/{modern-server.js → modernServer.js} +8 -9
- package/dist/cjs/server/{modern-server-split.js → modernServerSplit.js} +8 -8
- package/dist/cjs/{worker-server.js → workerServer.js} +3 -3
- package/dist/esm/index.js +1 -1
- package/dist/esm/server/index.js +5 -3
- package/dist/esm/server/{modern-server.js → modernServer.js} +4 -5
- package/dist/esm/server/modernServerSplit.js +360 -0
- package/dist/esm-node/index.js +1 -1
- package/dist/esm-node/server/index.js +5 -3
- package/dist/esm-node/server/{modern-server.js → modernServer.js} +3 -4
- package/dist/esm-node/server/{modern-server-split.js → modernServerSplit.js} +1 -1
- package/dist/js/modern/constants.js +35 -0
- package/dist/js/modern/index.js +18 -0
- package/dist/js/modern/libs/context/context.js +160 -0
- package/dist/js/modern/libs/context/index.js +6 -0
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +20 -0
- package/dist/js/modern/libs/hook-api/template.js +73 -0
- package/dist/js/modern/libs/loadConfig.js +62 -0
- package/dist/js/modern/libs/logger.js +111 -0
- package/dist/js/modern/libs/metrics.js +11 -0
- package/dist/js/modern/libs/proxy.js +92 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +114 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +254 -0
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/js/modern/libs/render/cache/index.js +115 -0
- package/dist/js/modern/libs/render/cache/page-caches/index.js +32 -0
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/js/modern/libs/render/cache/spr.js +248 -0
- package/dist/js/modern/libs/render/cache/type.js +0 -0
- package/dist/js/modern/libs/render/cache/util.js +102 -0
- package/dist/js/modern/libs/render/index.js +86 -0
- package/dist/js/modern/libs/render/measure.js +68 -0
- package/dist/js/modern/libs/render/reader.js +107 -0
- package/dist/js/modern/libs/render/ssr.js +100 -0
- package/dist/js/modern/libs/render/static.js +60 -0
- package/dist/js/modern/libs/render/type.js +9 -0
- package/dist/js/modern/libs/route/index.js +54 -0
- package/dist/js/modern/libs/route/matcher.js +87 -0
- package/dist/js/modern/libs/route/route.js +16 -0
- package/dist/js/modern/libs/serve-file.js +67 -0
- package/dist/js/modern/server/index.js +208 -0
- package/dist/js/modern/server/modern-server-split.js +74 -0
- package/dist/js/modern/server/modern-server.js +554 -0
- package/dist/js/modern/type.js +0 -0
- package/dist/js/modern/utils.js +136 -0
- package/dist/js/modern/worker-server.js +89 -0
- package/dist/js/node/constants.js +62 -0
- package/dist/js/node/index.js +44 -0
- package/dist/js/node/libs/context/context.js +189 -0
- package/dist/js/node/libs/context/index.js +30 -0
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +43 -0
- package/dist/js/node/libs/hook-api/template.js +97 -0
- package/dist/js/node/libs/loadConfig.js +91 -0
- package/dist/js/node/libs/logger.js +133 -0
- package/dist/js/node/libs/metrics.js +34 -0
- package/dist/js/node/libs/proxy.js +114 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +115 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +245 -0
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/js/node/libs/render/cache/index.js +134 -0
- package/dist/js/node/libs/render/cache/page-caches/index.js +55 -0
- package/dist/js/node/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/js/node/libs/render/cache/spr.js +270 -0
- package/dist/js/node/libs/render/cache/type.js +15 -0
- package/dist/js/node/libs/render/cache/util.js +138 -0
- package/dist/js/node/libs/render/index.js +115 -0
- package/dist/js/node/libs/render/measure.js +90 -0
- package/dist/js/node/libs/render/reader.js +140 -0
- package/dist/js/node/libs/render/ssr.js +123 -0
- package/dist/js/node/libs/render/static.js +89 -0
- package/dist/js/node/libs/render/type.js +32 -0
- package/dist/js/node/libs/route/index.js +78 -0
- package/dist/js/node/libs/route/matcher.js +106 -0
- package/dist/js/node/libs/route/route.js +39 -0
- package/dist/js/node/libs/serve-file.js +97 -0
- package/dist/js/node/server/index.js +219 -0
- package/dist/js/node/server/modern-server-split.js +97 -0
- package/dist/js/node/server/modern-server.js +559 -0
- package/dist/js/node/type.js +15 -0
- package/dist/js/node/utils.js +166 -0
- package/dist/js/node/worker-server.js +113 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/logger.js +205 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +234 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +143 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server.js +1089 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +233 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/type.d.ts +8 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +14 -14
- /package/dist/esm/libs/{serve-file.js → serveFile.js} +0 -0
- /package/dist/esm/{worker-server.js → workerServer.js} +0 -0
- /package/dist/esm-node/libs/{serve-file.js → serveFile.js} +0 -0
- /package/dist/esm-node/{worker-server.js → workerServer.js} +0 -0
- /package/dist/{esm → js/treeshaking}/server/modern-server-split.js +0 -0
- /package/dist/types/libs/{serve-file.d.ts → serveFile.d.ts} +0 -0
- /package/dist/types/server/{modern-server.d.ts → modernServer.d.ts} +0 -0
- /package/dist/types/server/{modern-server-split.d.ts → modernServerSplit.d.ts} +0 -0
- /package/dist/types/{worker-server.d.ts → workerServer.d.ts} +0 -0
|
@@ -0,0 +1,559 @@
|
|
|
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
|
|
73
|
+
});
|
|
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";
|
|
90
|
+
class ModernServer {
|
|
91
|
+
constructor({
|
|
92
|
+
pwd,
|
|
93
|
+
config,
|
|
94
|
+
routes,
|
|
95
|
+
staticGenerate,
|
|
96
|
+
logger,
|
|
97
|
+
metrics,
|
|
98
|
+
runMode,
|
|
99
|
+
proxyTarget
|
|
100
|
+
}) {
|
|
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");
|
|
108
|
+
this.pwd = pwd;
|
|
109
|
+
this.distDir = import_path.default.join(pwd, config.output.path || "dist");
|
|
110
|
+
this.workDir = this.distDir;
|
|
111
|
+
this.conf = config;
|
|
112
|
+
(0, import_utils2.debug)("server conf", this.conf);
|
|
113
|
+
this.logger = logger;
|
|
114
|
+
this.metrics = metrics;
|
|
115
|
+
this.router = new import_route.RouteMatchManager();
|
|
116
|
+
this.presetRoutes = routes;
|
|
117
|
+
this.proxyTarget = proxyTarget;
|
|
118
|
+
this.staticGenerate = staticGenerate || false;
|
|
119
|
+
this.runMode = runMode || import_constants.RUN_MODE.FULL;
|
|
120
|
+
}
|
|
121
|
+
onInit(runner, app) {
|
|
122
|
+
return __async(this, null, function* () {
|
|
123
|
+
var _a, _b;
|
|
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();
|
|
136
|
+
});
|
|
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
|
+
const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
|
|
156
|
+
const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
|
|
157
|
+
this.routeRenderHandler = (0, import_render.createRenderHandler)({
|
|
158
|
+
distDir,
|
|
159
|
+
staticGenerate,
|
|
160
|
+
forceCSR
|
|
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();
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
onRepack(_) {
|
|
171
|
+
}
|
|
172
|
+
addBeforeRouteHandler() {
|
|
173
|
+
this.addHandler((context, next) => __async(this, null, function* () {
|
|
174
|
+
if (this.beforeRouteHandler) {
|
|
175
|
+
yield this.beforeRouteHandler(context);
|
|
176
|
+
if (this.isSend(context.res)) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return next();
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
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));
|
|
188
|
+
const onlyApi = filepath.startsWith(apiPath);
|
|
189
|
+
const onlyWeb = filepath.startsWith(serverPath);
|
|
190
|
+
this.prepareFrameHandler({ onlyWeb, onlyApi });
|
|
191
|
+
}
|
|
192
|
+
getRequestHandler() {
|
|
193
|
+
return this.requestHandler.bind(this);
|
|
194
|
+
}
|
|
195
|
+
render(req, res, url) {
|
|
196
|
+
return __async(this, null, function* () {
|
|
197
|
+
req.logger = req.logger || this.logger;
|
|
198
|
+
req.metrics = 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
|
+
});
|
|
211
|
+
}
|
|
212
|
+
createHTTPServer(handler) {
|
|
213
|
+
return __async(this, null, function* () {
|
|
214
|
+
return (0, import_http.createServer)(handler);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
getRoutes() {
|
|
218
|
+
if (this.presetRoutes) {
|
|
219
|
+
return this.presetRoutes;
|
|
220
|
+
}
|
|
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);
|
|
224
|
+
return content.routes;
|
|
225
|
+
}
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
addHandler(handler) {
|
|
229
|
+
this.handlers.push(handler);
|
|
230
|
+
}
|
|
231
|
+
render404(context) {
|
|
232
|
+
context.error(import_constants.ERROR_DIGEST.ENOTF, "404 Not Found");
|
|
233
|
+
this.renderErrorPage(context, 404);
|
|
234
|
+
}
|
|
235
|
+
prepareBeforeRouteHandler(specs, distDir) {
|
|
236
|
+
return __async(this, null, function* () {
|
|
237
|
+
const { runner } = this;
|
|
238
|
+
const handler = yield runner.preparebeforeRouteHandler(
|
|
239
|
+
{
|
|
240
|
+
serverRoutes: specs,
|
|
241
|
+
distDir
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
onLast: () => null
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
this.beforeRouteHandler = handler;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
prepareFrameHandler(options) {
|
|
251
|
+
return __async(this, null, function* () {
|
|
252
|
+
const { workDir, runner } = this;
|
|
253
|
+
const { onlyApi, onlyWeb } = options || {};
|
|
254
|
+
const _a = (0, import_utils2.createMiddlewareCollecter)(), { getMiddlewares } = _a, collector = __objRest(_a, ["getMiddlewares"]);
|
|
255
|
+
yield runner.gather(collector);
|
|
256
|
+
const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
|
|
257
|
+
const apiDir = import_path.default.join(workDir, API_DIR);
|
|
258
|
+
const serverDir = import_path.default.join(workDir, SERVER_DIR);
|
|
259
|
+
if ((yield import_utils.fs.pathExists(import_path.default.join(serverDir))) && !onlyApi) {
|
|
260
|
+
const webExtension = (0, import_utils2.mergeExtension)(pluginWebExt);
|
|
261
|
+
this.frameWebHandler = yield this.prepareWebHandler(webExtension);
|
|
262
|
+
}
|
|
263
|
+
if (import_utils.fs.existsSync(apiDir) && !onlyWeb) {
|
|
264
|
+
const apiExtension = (0, import_utils2.mergeExtension)(pluginAPIExt);
|
|
265
|
+
this.frameAPIHandler = yield this.prepareAPIHandler(apiExtension);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
prepareWebHandler(extension) {
|
|
270
|
+
return __async(this, null, function* () {
|
|
271
|
+
const { workDir, runner } = this;
|
|
272
|
+
const handler = yield runner.prepareWebServer(
|
|
273
|
+
{
|
|
274
|
+
pwd: workDir,
|
|
275
|
+
config: extension
|
|
276
|
+
},
|
|
277
|
+
{ onLast: () => null }
|
|
278
|
+
);
|
|
279
|
+
return handler;
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
prepareAPIHandler(extension) {
|
|
283
|
+
return __async(this, null, function* () {
|
|
284
|
+
const { workDir, runner, conf } = this;
|
|
285
|
+
const { bff } = conf;
|
|
286
|
+
const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
|
|
287
|
+
return runner.prepareApiServer(
|
|
288
|
+
{
|
|
289
|
+
pwd: workDir,
|
|
290
|
+
config: extension,
|
|
291
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
292
|
+
render: this.render.bind(this)
|
|
293
|
+
},
|
|
294
|
+
{ onLast: () => null }
|
|
295
|
+
);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
filterRoutes(routes) {
|
|
299
|
+
return routes;
|
|
300
|
+
}
|
|
301
|
+
setupBeforeProdMiddleware() {
|
|
302
|
+
return __async(this, null, function* () {
|
|
303
|
+
const { conf, runner } = this;
|
|
304
|
+
const preMiddleware = yield runner.beforeProdServer(conf);
|
|
305
|
+
preMiddleware.flat().forEach((mid) => {
|
|
306
|
+
this.addHandler(mid);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
handleAPI(context) {
|
|
311
|
+
return __async(this, null, function* () {
|
|
312
|
+
const { req, res } = context;
|
|
313
|
+
if (!this.frameAPIHandler) {
|
|
314
|
+
throw new Error("can not found api handler");
|
|
315
|
+
}
|
|
316
|
+
yield this.frameAPIHandler(req, res);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
handleWeb(context, route) {
|
|
320
|
+
return __async(this, null, function* () {
|
|
321
|
+
return this.routeRenderHandler({
|
|
322
|
+
ctx: context,
|
|
323
|
+
route,
|
|
324
|
+
runner: this.runner
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
proxy() {
|
|
329
|
+
return __async(this, null, function* () {
|
|
330
|
+
return null;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
warmupSSRBundle() {
|
|
334
|
+
const { distDir } = this;
|
|
335
|
+
const bundles = this.router.getBundles();
|
|
336
|
+
bundles.forEach((bundle) => {
|
|
337
|
+
const filepath = import_path.default.join(distDir, bundle);
|
|
338
|
+
if (import_utils.fs.existsSync(filepath)) {
|
|
339
|
+
require(filepath);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
createContext(req, res, options = {}) {
|
|
344
|
+
return (0, import_context.createContext)(req, res, options);
|
|
345
|
+
}
|
|
346
|
+
routeHandler(context) {
|
|
347
|
+
return __async(this, null, function* () {
|
|
348
|
+
const { res } = context;
|
|
349
|
+
const matched = this.router.match(context.path);
|
|
350
|
+
if (!matched) {
|
|
351
|
+
this.render404(context);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
let route = matched.generate(context.url);
|
|
355
|
+
if (route.isApi) {
|
|
356
|
+
yield this.handleAPI(context);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const afterMatchContext = (0, import_hook_api.createAfterMatchContext)(context, route.entryName);
|
|
360
|
+
if (this.runMode === import_constants.RUN_MODE.FULL) {
|
|
361
|
+
yield this.runner.afterMatch(afterMatchContext, { onLast: import_utils2.noop });
|
|
362
|
+
}
|
|
363
|
+
if (this.isSend(res)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const { current, url, status } = afterMatchContext.router;
|
|
367
|
+
if (url) {
|
|
368
|
+
this.redirect(res, url, status);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (route.entryName !== current) {
|
|
372
|
+
const matched2 = this.router.matchEntry(current);
|
|
373
|
+
if (!matched2) {
|
|
374
|
+
this.render404(context);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
route = matched2.generate(context.url);
|
|
378
|
+
}
|
|
379
|
+
context.setParams(route.params);
|
|
380
|
+
context.setServerData("router", {
|
|
381
|
+
baseUrl: route.urlPath,
|
|
382
|
+
params: route.params
|
|
383
|
+
});
|
|
384
|
+
if (this.frameWebHandler) {
|
|
385
|
+
res.locals = res.locals || {};
|
|
386
|
+
const middlewareContext = (0, import_hook_api.createMiddlewareContext)(context);
|
|
387
|
+
yield this.frameWebHandler(middlewareContext);
|
|
388
|
+
res.locals = __spreadValues(__spreadValues({}, res.locals), middlewareContext.response.locals);
|
|
389
|
+
}
|
|
390
|
+
if (this.isSend(res)) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (route.responseHeaders) {
|
|
394
|
+
Object.keys(route.responseHeaders).forEach((key) => {
|
|
395
|
+
const value = route.responseHeaders[key];
|
|
396
|
+
if (value) {
|
|
397
|
+
context.res.setHeader(key, value);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const renderResult = yield this.handleWeb(context, route);
|
|
402
|
+
if (!renderResult) {
|
|
403
|
+
this.render404(context);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (renderResult.redirect) {
|
|
407
|
+
this.redirect(
|
|
408
|
+
res,
|
|
409
|
+
renderResult.content,
|
|
410
|
+
renderResult.statusCode
|
|
411
|
+
);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (this.isSend(res)) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
res.setHeader("content-type", renderResult.contentType);
|
|
418
|
+
const { contentStream } = renderResult;
|
|
419
|
+
if (contentStream) {
|
|
420
|
+
contentStream.pipe(
|
|
421
|
+
(0, import_template.templateInjectableStream)({
|
|
422
|
+
prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
|
|
423
|
+
context.serverData
|
|
424
|
+
)}<\/script>` : void 0
|
|
425
|
+
})
|
|
426
|
+
).pipe(res);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
let response = renderResult.content;
|
|
430
|
+
if (route.entryName) {
|
|
431
|
+
const afterRenderContext = (0, import_hook_api.createAfterRenderContext)(
|
|
432
|
+
context,
|
|
433
|
+
response.toString()
|
|
434
|
+
);
|
|
435
|
+
if (this.runMode === import_constants.RUN_MODE.FULL) {
|
|
436
|
+
yield this.runner.afterRender(afterRenderContext, { onLast: import_utils2.noop });
|
|
437
|
+
}
|
|
438
|
+
if (this.isSend(res)) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
afterRenderContext.template.prependHead(
|
|
442
|
+
`<script>window._SERVER_DATA=${JSON.stringify(
|
|
443
|
+
context.serverData
|
|
444
|
+
)}<\/script>`
|
|
445
|
+
);
|
|
446
|
+
response = afterRenderContext.template.get();
|
|
447
|
+
}
|
|
448
|
+
res.end(response);
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
isSend(res) {
|
|
452
|
+
if (res.headersSent) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
if (res.getHeader("Location") && (0, import_utils2.isRedirect)(res.statusCode)) {
|
|
456
|
+
res.end();
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
compose() {
|
|
462
|
+
const { handlers } = this;
|
|
463
|
+
if (!Array.isArray(handlers)) {
|
|
464
|
+
throw new TypeError("Middleware stack must be an array!");
|
|
465
|
+
}
|
|
466
|
+
for (const fn of handlers) {
|
|
467
|
+
if (typeof fn !== "function") {
|
|
468
|
+
throw new TypeError("Middleware must be composed of functions!");
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
this._handler = (context, next) => {
|
|
472
|
+
let i = 0;
|
|
473
|
+
const dispatch = (error) => {
|
|
474
|
+
if (error) {
|
|
475
|
+
return this.onError(context, error);
|
|
476
|
+
}
|
|
477
|
+
const handler = handlers[i++];
|
|
478
|
+
if (!handler) {
|
|
479
|
+
return next();
|
|
480
|
+
}
|
|
481
|
+
try {
|
|
482
|
+
const result = handler(context, dispatch);
|
|
483
|
+
if ((0, import_utils.isPromise)(result)) {
|
|
484
|
+
return result.catch(onError);
|
|
485
|
+
}
|
|
486
|
+
} catch (e) {
|
|
487
|
+
return onError(e);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
const onError = (err) => {
|
|
491
|
+
this.onError(context, err);
|
|
492
|
+
};
|
|
493
|
+
return dispatch();
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
requestHandler(req, res, next = () => {
|
|
497
|
+
}) {
|
|
498
|
+
res.statusCode = 200;
|
|
499
|
+
req.logger = req.logger || this.logger;
|
|
500
|
+
req.metrics = req.metrics || this.metrics;
|
|
501
|
+
let context;
|
|
502
|
+
try {
|
|
503
|
+
context = this.createContext(req, res);
|
|
504
|
+
} catch (e) {
|
|
505
|
+
this.logger.error(e);
|
|
506
|
+
res.statusCode = 500;
|
|
507
|
+
res.setHeader("content-type", import_utils.mime.contentType("html"));
|
|
508
|
+
return res.end((0, import_utils2.createErrorDocument)(500, import_constants.ERROR_PAGE_TEXT[500]));
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
return this._handler(context, next);
|
|
512
|
+
} catch (err) {
|
|
513
|
+
return this.onError(context, err);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
redirect(res, url, status = 302) {
|
|
517
|
+
res.setHeader("Location", url);
|
|
518
|
+
res.statusCode = status;
|
|
519
|
+
res.end();
|
|
520
|
+
}
|
|
521
|
+
onError(context, err) {
|
|
522
|
+
context.error(import_constants.ERROR_DIGEST.EINTER, err);
|
|
523
|
+
this.renderErrorPage(context, 500);
|
|
524
|
+
}
|
|
525
|
+
renderErrorPage(context, status) {
|
|
526
|
+
return __async(this, null, function* () {
|
|
527
|
+
const { res } = context;
|
|
528
|
+
context.status = status;
|
|
529
|
+
res.setHeader("content-type", import_utils.mime.contentType("html"));
|
|
530
|
+
const statusPage = `/${status}`;
|
|
531
|
+
const customErrorPage = `/_error`;
|
|
532
|
+
const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
|
|
533
|
+
if (matched) {
|
|
534
|
+
const route = matched.generate(context.url);
|
|
535
|
+
const { entryName } = route;
|
|
536
|
+
if (entryName === status.toString() || entryName === "_error") {
|
|
537
|
+
try {
|
|
538
|
+
const file = yield this.routeRenderHandler({
|
|
539
|
+
route,
|
|
540
|
+
ctx: context,
|
|
541
|
+
runner: this.runner
|
|
542
|
+
});
|
|
543
|
+
if (file) {
|
|
544
|
+
context.res.end(file.content);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
} catch (e) {
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const text = import_constants.ERROR_PAGE_TEXT[status] || import_constants.ERROR_PAGE_TEXT[500];
|
|
552
|
+
context.res.end((0, import_utils2.createErrorDocument)(status, text));
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
557
|
+
0 && (module.exports = {
|
|
558
|
+
ModernServer
|
|
559
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var type_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(type_exports);
|