@bleedingdev/modern-js-prod-server 3.2.0-ultramodern.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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/cjs/apply.js +97 -0
- package/dist/cjs/index.js +89 -0
- package/dist/cjs/libs/contractGateAutopilot.js +36 -0
- package/dist/cjs/libs/loadConfig.js +72 -0
- package/dist/cjs/libs/metrics.js +41 -0
- package/dist/cjs/libs/render/index.js +125 -0
- package/dist/cjs/libs/render/ssr.js +118 -0
- package/dist/cjs/libs/render/utils.js +72 -0
- package/dist/cjs/libs/runtimeFallbackWorkerLane.js +167 -0
- package/dist/cjs/libs/telemetry.js +87 -0
- package/dist/cjs/netlify.js +56 -0
- package/dist/cjs/server/index.js +579 -0
- package/dist/cjs/server/modernServer.js +472 -0
- package/dist/cjs/server/modernServerSplit.js +38 -0
- package/dist/cjs/types.js +18 -0
- package/dist/cjs/utils.js +38 -0
- package/dist/esm/apply.mjs +63 -0
- package/dist/esm/index.mjs +26 -0
- package/dist/esm/libs/contractGateAutopilot.mjs +1 -0
- package/dist/esm/libs/loadConfig.mjs +22 -0
- package/dist/esm/libs/metrics.mjs +7 -0
- package/dist/esm/libs/render/index.mjs +81 -0
- package/dist/esm/libs/render/ssr.mjs +73 -0
- package/dist/esm/libs/render/utils.mjs +35 -0
- package/dist/esm/libs/runtimeFallbackWorkerLane.mjs +130 -0
- package/dist/esm/libs/telemetry.mjs +1 -0
- package/dist/esm/netlify.mjs +22 -0
- package/dist/esm/rslib-runtime.mjs +18 -0
- package/dist/esm/server/index.mjs +535 -0
- package/dist/esm/server/modernServer.mjs +419 -0
- package/dist/esm/server/modernServerSplit.mjs +4 -0
- package/dist/esm/types.mjs +0 -0
- package/dist/esm/utils.mjs +4 -0
- package/dist/esm-node/apply.mjs +64 -0
- package/dist/esm-node/index.mjs +27 -0
- package/dist/esm-node/libs/contractGateAutopilot.mjs +2 -0
- package/dist/esm-node/libs/loadConfig.mjs +23 -0
- package/dist/esm-node/libs/metrics.mjs +8 -0
- package/dist/esm-node/libs/render/index.mjs +82 -0
- package/dist/esm-node/libs/render/ssr.mjs +75 -0
- package/dist/esm-node/libs/render/utils.mjs +36 -0
- package/dist/esm-node/libs/runtimeFallbackWorkerLane.mjs +131 -0
- package/dist/esm-node/libs/telemetry.mjs +2 -0
- package/dist/esm-node/netlify.mjs +23 -0
- package/dist/esm-node/rslib-runtime.mjs +19 -0
- package/dist/esm-node/server/index.mjs +536 -0
- package/dist/esm-node/server/modernServer.mjs +421 -0
- package/dist/esm-node/server/modernServerSplit.mjs +5 -0
- package/dist/esm-node/types.mjs +1 -0
- package/dist/esm-node/utils.mjs +5 -0
- package/dist/types/apply.d.ts +6 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/libs/metrics.d.ts +8 -0
- package/dist/types/libs/telemetry.d.ts +2 -0
- package/dist/types/netlify.d.ts +3 -0
- package/dist/types/types.d.ts +15 -0
- package/package.json +79 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +7 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"ignore-styles" (module) {
|
|
4
|
+
module.exports = require("ignore-styles");
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
var __webpack_module_cache__ = {};
|
|
8
|
+
function __webpack_require__(moduleId) {
|
|
9
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
10
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
11
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
12
|
+
exports: {}
|
|
13
|
+
};
|
|
14
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
15
|
+
return module.exports;
|
|
16
|
+
}
|
|
17
|
+
(()=>{
|
|
18
|
+
__webpack_require__.n = (module)=>{
|
|
19
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
20
|
+
__webpack_require__.d(getter, {
|
|
21
|
+
a: getter
|
|
22
|
+
});
|
|
23
|
+
return getter;
|
|
24
|
+
};
|
|
25
|
+
})();
|
|
26
|
+
(()=>{
|
|
27
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
28
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: definition[key]
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
})();
|
|
34
|
+
(()=>{
|
|
35
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
36
|
+
})();
|
|
37
|
+
(()=>{
|
|
38
|
+
__webpack_require__.r = (exports1)=>{
|
|
39
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
40
|
+
value: 'Module'
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
43
|
+
value: true
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
})();
|
|
47
|
+
var __webpack_exports__ = {};
|
|
48
|
+
(()=>{
|
|
49
|
+
__webpack_require__.r(__webpack_exports__);
|
|
50
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
51
|
+
ModernServer: ()=>ModernServer
|
|
52
|
+
});
|
|
53
|
+
const time_namespaceObject = require("@modern-js/runtime-utils/time");
|
|
54
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
55
|
+
const external_http_namespaceObject = require("http");
|
|
56
|
+
const external_path_namespaceObject = require("path");
|
|
57
|
+
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
58
|
+
const external_constants_namespaceObject = require("../constants");
|
|
59
|
+
const context_namespaceObject = require("../libs/context");
|
|
60
|
+
const hook_api_namespaceObject = require("../libs/hook-api");
|
|
61
|
+
const mfCache_namespaceObject = require("../libs/mfCache");
|
|
62
|
+
const proxy_namespaceObject = require("../libs/proxy");
|
|
63
|
+
const index_js_namespaceObject = require("../libs/render/index.js");
|
|
64
|
+
const reader_namespaceObject = require("../libs/render/reader");
|
|
65
|
+
const route_namespaceObject = require("../libs/route");
|
|
66
|
+
const serveFile_namespaceObject = require("../libs/serveFile");
|
|
67
|
+
const external_utils_js_namespaceObject = require("../utils.js");
|
|
68
|
+
const SERVER_DIR = './server';
|
|
69
|
+
class ModernServer {
|
|
70
|
+
async onInit(runner, app) {
|
|
71
|
+
this.runner = runner;
|
|
72
|
+
const { distDir, conf } = this;
|
|
73
|
+
this.initReader();
|
|
74
|
+
(0, external_utils_js_namespaceObject.debug)('final server conf', this.conf);
|
|
75
|
+
if (conf.bff?.proxy) {
|
|
76
|
+
const { handlers, handleUpgrade } = (0, proxy_namespaceObject.createProxyHandler)(conf.bff.proxy);
|
|
77
|
+
app && handleUpgrade(app);
|
|
78
|
+
handlers.forEach((handler)=>{
|
|
79
|
+
this.addHandler(handler);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
app?.on('close', ()=>{
|
|
83
|
+
this.reader.close();
|
|
84
|
+
});
|
|
85
|
+
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
86
|
+
this.router.reset(usageRoutes);
|
|
87
|
+
this.warmupSSRBundle();
|
|
88
|
+
await this.prepareFrameHandler();
|
|
89
|
+
await this.prepareLoaderHandler(usageRoutes, distDir);
|
|
90
|
+
this.routeRenderHandler = this.getRenderHandler();
|
|
91
|
+
await this.setupBeforeProdMiddleware();
|
|
92
|
+
this.addHandler(this.setupMfAssetCacheMiddleware());
|
|
93
|
+
this.addHandler(this.setupStaticMiddleware(this.conf.output?.assetPrefix));
|
|
94
|
+
this.addHandler(serveFile_namespaceObject.faviconFallbackHandler);
|
|
95
|
+
this.addHandler(this.routeHandler.bind(this));
|
|
96
|
+
this.compose();
|
|
97
|
+
}
|
|
98
|
+
getRenderHandler() {
|
|
99
|
+
const { distDir, staticGenerate, conf, metaName } = this;
|
|
100
|
+
const ssrConfig = this.conf.server?.ssr;
|
|
101
|
+
const forceCSR = 'object' == typeof ssrConfig ? ssrConfig.forceCSR : false;
|
|
102
|
+
return (0, index_js_namespaceObject.createRenderHandler)({
|
|
103
|
+
distDir,
|
|
104
|
+
staticGenerate,
|
|
105
|
+
forceCSR,
|
|
106
|
+
conf: this.conf,
|
|
107
|
+
nonce: conf.security?.nonce,
|
|
108
|
+
metaName
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
onRepack(_) {}
|
|
112
|
+
getRequestHandler() {
|
|
113
|
+
return this.requestHandler.bind(this);
|
|
114
|
+
}
|
|
115
|
+
async render(req, res, url) {
|
|
116
|
+
req.logger = req.logger || this.logger;
|
|
117
|
+
req.metrics = req.metrics || this.metrics;
|
|
118
|
+
const context = (0, context_namespaceObject.createContext)(req, res, {
|
|
119
|
+
metaName: this.metaName
|
|
120
|
+
});
|
|
121
|
+
const matched = this.router.match(url || context.path);
|
|
122
|
+
if (!matched) return null;
|
|
123
|
+
res.statusCode = 200;
|
|
124
|
+
const route = matched.generate(context.url);
|
|
125
|
+
const result = await this.handleWeb(context, route);
|
|
126
|
+
if (!result) return null;
|
|
127
|
+
if (result.contentStream) return result.contentStream;
|
|
128
|
+
return result.content.toString();
|
|
129
|
+
}
|
|
130
|
+
async createHTTPServer(handler) {
|
|
131
|
+
return (0, external_http_namespaceObject.createServer)(handler);
|
|
132
|
+
}
|
|
133
|
+
initReader() {
|
|
134
|
+
this.reader.init();
|
|
135
|
+
}
|
|
136
|
+
async onServerChange({ filepath }) {
|
|
137
|
+
const { pwd } = this;
|
|
138
|
+
const { api, server } = external_constants_namespaceObject.AGGRED_DIR;
|
|
139
|
+
const apiPath = external_path_default().normalize(external_path_default().join(pwd, api));
|
|
140
|
+
const serverPath = external_path_default().normalize(external_path_default().join(pwd, server));
|
|
141
|
+
const onlyApi = filepath.startsWith(apiPath);
|
|
142
|
+
const onlyWeb = filepath.startsWith(serverPath);
|
|
143
|
+
await this.prepareFrameHandler({
|
|
144
|
+
onlyWeb,
|
|
145
|
+
onlyApi
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
getRoutes() {
|
|
149
|
+
if (this.presetRoutes) return this.presetRoutes;
|
|
150
|
+
const file = external_path_default().join(this.distDir, utils_namespaceObject.ROUTE_SPEC_FILE);
|
|
151
|
+
if (utils_namespaceObject.fs.existsSync(file)) {
|
|
152
|
+
const content = utils_namespaceObject.fs.readJSONSync(file);
|
|
153
|
+
return content.routes;
|
|
154
|
+
}
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
addHandler(handler) {
|
|
158
|
+
this.handlers.push(handler);
|
|
159
|
+
}
|
|
160
|
+
render404(context) {
|
|
161
|
+
context.error(external_constants_namespaceObject.ERROR_DIGEST.ENOTF, '404 Not Found');
|
|
162
|
+
this.renderErrorPage(context, 404);
|
|
163
|
+
}
|
|
164
|
+
async prepareLoaderHandler(specs, distDir) {
|
|
165
|
+
const { runner } = this;
|
|
166
|
+
const handler = await runner.prepareLoaderHandler({
|
|
167
|
+
serverRoutes: specs,
|
|
168
|
+
distDir
|
|
169
|
+
}, {
|
|
170
|
+
onLast: ()=>null
|
|
171
|
+
});
|
|
172
|
+
this.loaderHandler = handler;
|
|
173
|
+
}
|
|
174
|
+
async prepareFrameHandler(options) {
|
|
175
|
+
const { workDir, runner } = this;
|
|
176
|
+
const { onlyApi, onlyWeb } = options || {};
|
|
177
|
+
const { getMiddlewares, ...collector } = (0, external_utils_js_namespaceObject.createMiddlewareCollecter)();
|
|
178
|
+
await runner.gather(collector);
|
|
179
|
+
const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
|
|
180
|
+
const serverDir = external_path_default().join(workDir, SERVER_DIR);
|
|
181
|
+
if (await utils_namespaceObject.fs.pathExists(external_path_default().join(serverDir)) && !onlyApi) {
|
|
182
|
+
const webExtension = (0, external_utils_js_namespaceObject.mergeExtension)(pluginWebExt);
|
|
183
|
+
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
184
|
+
}
|
|
185
|
+
if (!onlyWeb) {
|
|
186
|
+
const apiExtension = (0, external_utils_js_namespaceObject.mergeExtension)(pluginAPIExt);
|
|
187
|
+
this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async prepareWebHandler(extension) {
|
|
191
|
+
const { workDir, runner } = this;
|
|
192
|
+
const handler = await runner.prepareWebServer({
|
|
193
|
+
pwd: workDir,
|
|
194
|
+
config: extension
|
|
195
|
+
}, {
|
|
196
|
+
onLast: ()=>null
|
|
197
|
+
});
|
|
198
|
+
return handler;
|
|
199
|
+
}
|
|
200
|
+
async prepareAPIHandler(extension) {
|
|
201
|
+
const { workDir, runner, conf } = this;
|
|
202
|
+
const { bff } = conf;
|
|
203
|
+
const prefix = bff?.prefix || '/api';
|
|
204
|
+
const webOnly = await (0, utils_namespaceObject.isWebOnly)();
|
|
205
|
+
if (webOnly && 'development' === process.env.NODE_ENV) return (req, res)=>{
|
|
206
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
207
|
+
res.end(JSON.stringify(''));
|
|
208
|
+
};
|
|
209
|
+
return runner.prepareApiServer({
|
|
210
|
+
pwd: workDir,
|
|
211
|
+
config: extension,
|
|
212
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
213
|
+
httpMethodDecider: bff?.httpMethodDecider,
|
|
214
|
+
render: this.render.bind(this)
|
|
215
|
+
}, {
|
|
216
|
+
onLast: ()=>null
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
filterRoutes(routes) {
|
|
220
|
+
return routes;
|
|
221
|
+
}
|
|
222
|
+
async setupBeforeProdMiddleware() {
|
|
223
|
+
const { conf, runner } = this;
|
|
224
|
+
const preMiddleware = await runner.beforeProdServer(conf);
|
|
225
|
+
preMiddleware.flat().forEach((mid)=>{
|
|
226
|
+
this.addHandler(mid);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
setupStaticMiddleware(prefix) {
|
|
230
|
+
const staticPathRegExp = (0, external_utils_js_namespaceObject.getStaticReg)(this.conf.output, this.conf.html, prefix);
|
|
231
|
+
return (0, serveFile_namespaceObject.createStaticFileHandler)([
|
|
232
|
+
{
|
|
233
|
+
path: staticPathRegExp,
|
|
234
|
+
target: this.distDir
|
|
235
|
+
}
|
|
236
|
+
], prefix);
|
|
237
|
+
}
|
|
238
|
+
setupMfAssetCacheMiddleware() {
|
|
239
|
+
return async (context, next)=>{
|
|
240
|
+
const headers = (0, mfCache_namespaceObject.resolveMfAssetCacheHeaders)(context.url, context.query);
|
|
241
|
+
if (headers) for (const [key, value] of Object.entries(headers))context.res.setHeader(key, value);
|
|
242
|
+
await next();
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
async handleAPI(context) {
|
|
246
|
+
const { req, res } = context;
|
|
247
|
+
if (!this.frameAPIHandler) throw new Error('can not found api handler');
|
|
248
|
+
await this.frameAPIHandler(req, res);
|
|
249
|
+
}
|
|
250
|
+
async handleWeb(context, route) {
|
|
251
|
+
const { res } = context;
|
|
252
|
+
if (this.loaderHandler) {
|
|
253
|
+
await this.loaderHandler(context);
|
|
254
|
+
if (this.isSend(res)) return null;
|
|
255
|
+
}
|
|
256
|
+
context.setParams(route.params);
|
|
257
|
+
context.setServerData('router', {
|
|
258
|
+
baseUrl: route.urlPath,
|
|
259
|
+
params: route.params
|
|
260
|
+
});
|
|
261
|
+
if (route.responseHeaders) Object.keys(route.responseHeaders).forEach((key)=>{
|
|
262
|
+
const value = route.responseHeaders[key];
|
|
263
|
+
if (value) context.res.setHeader(key, value);
|
|
264
|
+
});
|
|
265
|
+
const renderResult = await this.routeRenderHandler({
|
|
266
|
+
ctx: context,
|
|
267
|
+
route,
|
|
268
|
+
runner: this.runner
|
|
269
|
+
});
|
|
270
|
+
if (!renderResult) {
|
|
271
|
+
this.render404(context);
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
if (renderResult.redirect) {
|
|
275
|
+
this.redirect(res, renderResult.content, renderResult.statusCode);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
if (this.isSend(res)) return null;
|
|
279
|
+
res.set('content-type', renderResult.contentType);
|
|
280
|
+
return renderResult;
|
|
281
|
+
}
|
|
282
|
+
async proxy() {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
warmupSSRBundle() {
|
|
286
|
+
const { distDir } = this;
|
|
287
|
+
const bundles = this.router.getBundles();
|
|
288
|
+
bundles.forEach((bundle)=>{
|
|
289
|
+
const filepath = external_path_default().join(distDir, bundle);
|
|
290
|
+
if (utils_namespaceObject.fs.existsSync(filepath)) require(filepath);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
createContext(req, res, options = {}) {
|
|
294
|
+
return (0, context_namespaceObject.createContext)(req, res, options);
|
|
295
|
+
}
|
|
296
|
+
async routeHandler(context) {
|
|
297
|
+
const { res, reporter } = context;
|
|
298
|
+
const matched = this.router.match(context.path);
|
|
299
|
+
if (!matched) return void this.render404(context);
|
|
300
|
+
await reporter.init({
|
|
301
|
+
match: matched
|
|
302
|
+
});
|
|
303
|
+
const end = (0, time_namespaceObject.time)();
|
|
304
|
+
res.on('finish', ()=>{
|
|
305
|
+
const cost = end();
|
|
306
|
+
reporter.reportTiming(external_constants_namespaceObject.ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
|
|
307
|
+
});
|
|
308
|
+
let route = matched.generate(context.url);
|
|
309
|
+
if (route.isApi) return void await this.handleAPI(context);
|
|
310
|
+
if (route.entryName && this.runMode === external_constants_namespaceObject.RUN_MODE.FULL) {
|
|
311
|
+
const afterMatchContext = (0, hook_api_namespaceObject.createAfterMatchContext)(context, route.entryName);
|
|
312
|
+
const end = (0, time_namespaceObject.time)();
|
|
313
|
+
await this.runner.afterMatch(afterMatchContext, {
|
|
314
|
+
onLast: external_utils_js_namespaceObject.noop
|
|
315
|
+
});
|
|
316
|
+
const cost = end();
|
|
317
|
+
cost && reporter.reportTiming(external_constants_namespaceObject.ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
|
|
318
|
+
if (this.isSend(res)) return;
|
|
319
|
+
const { current, url, status } = afterMatchContext.router;
|
|
320
|
+
if (url) return void this.redirect(res, url, status);
|
|
321
|
+
if (route.entryName !== current) {
|
|
322
|
+
const matched = this.router.matchEntry(current);
|
|
323
|
+
if (!matched) return void this.render404(context);
|
|
324
|
+
route = matched.generate(context.url);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (this.frameWebHandler) {
|
|
328
|
+
res.locals = res.locals || {};
|
|
329
|
+
const middlewareContext = (0, hook_api_namespaceObject.createMiddlewareContext)(context);
|
|
330
|
+
const end = (0, time_namespaceObject.time)();
|
|
331
|
+
await this.frameWebHandler(middlewareContext);
|
|
332
|
+
const cost = end();
|
|
333
|
+
cost && reporter.reportTiming(external_constants_namespaceObject.ServerReportTimings.SERVER_MIDDLEWARE, cost);
|
|
334
|
+
res.locals = {
|
|
335
|
+
...res.locals,
|
|
336
|
+
...middlewareContext.response.locals
|
|
337
|
+
};
|
|
338
|
+
if (this.isSend(res)) return;
|
|
339
|
+
}
|
|
340
|
+
const renderResult = await this.handleWeb(context, route);
|
|
341
|
+
if (!renderResult) return;
|
|
342
|
+
const { contentStream: responseStream } = renderResult;
|
|
343
|
+
let { content: response } = renderResult;
|
|
344
|
+
if (route.entryName && responseStream) return void responseStream.pipe(res);
|
|
345
|
+
if (route.entryName && this.runMode === external_constants_namespaceObject.RUN_MODE.FULL) {
|
|
346
|
+
const afterRenderContext = (0, hook_api_namespaceObject.createAfterRenderContext)(context, response.toString());
|
|
347
|
+
const end = (0, time_namespaceObject.time)();
|
|
348
|
+
await this.runner.afterRender(afterRenderContext, {
|
|
349
|
+
onLast: external_utils_js_namespaceObject.noop
|
|
350
|
+
});
|
|
351
|
+
const cost = end();
|
|
352
|
+
cost && reporter.reportTiming(external_constants_namespaceObject.ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost);
|
|
353
|
+
if (this.isSend(res)) return;
|
|
354
|
+
response = afterRenderContext.template.get();
|
|
355
|
+
}
|
|
356
|
+
res.end(response);
|
|
357
|
+
}
|
|
358
|
+
isSend(res) {
|
|
359
|
+
if (res.modernFlushedHeaders) {
|
|
360
|
+
if (res.writableFinished) return true;
|
|
361
|
+
} else if (res.headersSent) return true;
|
|
362
|
+
if (res.getHeader('Location') && (0, external_utils_js_namespaceObject.isRedirect)(res.statusCode)) {
|
|
363
|
+
res.end();
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
compose() {
|
|
369
|
+
const { handlers } = this;
|
|
370
|
+
if (!Array.isArray(handlers)) throw new TypeError('Middleware stack must be an array!');
|
|
371
|
+
for (const fn of handlers)if ('function' != typeof fn) throw new TypeError('Middleware must be composed of functions!');
|
|
372
|
+
this._handler = (context, next)=>{
|
|
373
|
+
let i = 0;
|
|
374
|
+
const dispatch = (error)=>{
|
|
375
|
+
if (error) return this.onError(context, error);
|
|
376
|
+
const handler = handlers[i++];
|
|
377
|
+
if (!handler) return next();
|
|
378
|
+
try {
|
|
379
|
+
const result = handler(context, dispatch);
|
|
380
|
+
if ((0, utils_namespaceObject.isPromise)(result)) return result.catch(onError);
|
|
381
|
+
} catch (e) {
|
|
382
|
+
return onError(e);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
const onError = (err)=>{
|
|
386
|
+
this.onError(context, err);
|
|
387
|
+
};
|
|
388
|
+
return dispatch();
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
requestHandler(req, res, next = ()=>{}) {
|
|
392
|
+
res.statusCode = 200;
|
|
393
|
+
req.logger = req.logger || this.logger;
|
|
394
|
+
req.metrics = req.metrics || this.metrics;
|
|
395
|
+
let context;
|
|
396
|
+
try {
|
|
397
|
+
context = this.createContext(req, res, {
|
|
398
|
+
metaName: this.metaName
|
|
399
|
+
});
|
|
400
|
+
} catch (e) {
|
|
401
|
+
this.logger.error(e);
|
|
402
|
+
res.statusCode = 500;
|
|
403
|
+
res.setHeader('content-type', utils_namespaceObject.mime.contentType('html'));
|
|
404
|
+
return res.end((0, external_utils_js_namespaceObject.createErrorDocument)(500, external_constants_namespaceObject.ERROR_PAGE_TEXT["500"]));
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
return this._handler(context, next);
|
|
408
|
+
} catch (err) {
|
|
409
|
+
return this.onError(context, err);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
redirect(res, url, status = 302) {
|
|
413
|
+
res.set('Location', url);
|
|
414
|
+
res.statusCode = status;
|
|
415
|
+
res.end();
|
|
416
|
+
}
|
|
417
|
+
onError(context, err) {
|
|
418
|
+
context.error(external_constants_namespaceObject.ERROR_DIGEST.EINTER, err);
|
|
419
|
+
this.renderErrorPage(context, 500);
|
|
420
|
+
}
|
|
421
|
+
async renderErrorPage(context, status) {
|
|
422
|
+
const { res } = context;
|
|
423
|
+
context.status = status;
|
|
424
|
+
res.set('content-type', utils_namespaceObject.mime.contentType('html'));
|
|
425
|
+
const statusPage = `/${status}`;
|
|
426
|
+
const customErrorPage = "/_error";
|
|
427
|
+
const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
|
|
428
|
+
if (matched) {
|
|
429
|
+
const route = matched.generate(context.url);
|
|
430
|
+
const { entryName } = route;
|
|
431
|
+
if (entryName === status.toString() || '_error' === entryName) try {
|
|
432
|
+
const file = await this.routeRenderHandler({
|
|
433
|
+
route,
|
|
434
|
+
ctx: context,
|
|
435
|
+
runner: this.runner
|
|
436
|
+
});
|
|
437
|
+
if (file) return void context.res.end(file.content);
|
|
438
|
+
} catch (e) {}
|
|
439
|
+
}
|
|
440
|
+
const text = external_constants_namespaceObject.ERROR_PAGE_TEXT[status] || external_constants_namespaceObject.ERROR_PAGE_TEXT["500"];
|
|
441
|
+
context.res.end((0, external_utils_js_namespaceObject.createErrorDocument)(status, text));
|
|
442
|
+
}
|
|
443
|
+
constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }){
|
|
444
|
+
this.handlers = [];
|
|
445
|
+
this.reader = reader_namespaceObject;
|
|
446
|
+
this.loaderHandler = null;
|
|
447
|
+
this.frameWebHandler = null;
|
|
448
|
+
this.frameAPIHandler = null;
|
|
449
|
+
__webpack_require__("ignore-styles");
|
|
450
|
+
this.pwd = pwd;
|
|
451
|
+
this.distDir = external_path_default().resolve(pwd, config.output.path || 'dist');
|
|
452
|
+
this.workDir = this.distDir;
|
|
453
|
+
this.conf = config;
|
|
454
|
+
(0, external_utils_js_namespaceObject.debug)('server conf', this.conf);
|
|
455
|
+
this.logger = logger;
|
|
456
|
+
this.metrics = metrics;
|
|
457
|
+
this.router = new route_namespaceObject.RouteMatchManager();
|
|
458
|
+
this.presetRoutes = routes;
|
|
459
|
+
this.proxyTarget = proxyTarget;
|
|
460
|
+
this.staticGenerate = staticGenerate || false;
|
|
461
|
+
this.runMode = runMode || external_constants_namespaceObject.RUN_MODE.FULL;
|
|
462
|
+
this.metaName = appContext?.metaName;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
})();
|
|
466
|
+
exports.ModernServer = __webpack_exports__.ModernServer;
|
|
467
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
468
|
+
"ModernServer"
|
|
469
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
470
|
+
Object.defineProperty(exports, '__esModule', {
|
|
471
|
+
value: true
|
|
472
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
createProdServer: ()=>createProdServer
|
|
28
|
+
});
|
|
29
|
+
const createProdServer = ()=>{
|
|
30
|
+
throw new Error('createProdServer from modernServerSplit is not available in this source runtime.');
|
|
31
|
+
};
|
|
32
|
+
exports.createProdServer = __webpack_exports__.createProdServer;
|
|
33
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
34
|
+
"createProdServer"
|
|
35
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
36
|
+
Object.defineProperty(exports, '__esModule', {
|
|
37
|
+
value: true
|
|
38
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
debug: ()=>debug
|
|
28
|
+
});
|
|
29
|
+
const debug = (...args)=>{
|
|
30
|
+
if (process.env.MODERN_DEBUG || process.env.DEBUG) console.log('[prod-server]', ...args);
|
|
31
|
+
};
|
|
32
|
+
exports.debug = __webpack_exports__.debug;
|
|
33
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
34
|
+
"debug"
|
|
35
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
36
|
+
Object.defineProperty(exports, '__esModule', {
|
|
37
|
+
value: true
|
|
38
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ErrorDigest, createDefaultPlugins, createErrorHtml, faviconPlugin, injectConfigMiddlewarePlugin, onError, renderPlugin } from "@modern-js/server-core";
|
|
2
|
+
import { injectNodeSeverPlugin, injectResourcePlugin, injectRscManifestPlugin, loadCacheConfig, serverStaticPlugin } from "@modern-js/server-core/node";
|
|
3
|
+
import { createLogger, isProd, logger } from "@modern-js/utils";
|
|
4
|
+
function getLogger(_) {
|
|
5
|
+
if (process.env.DEBUG || 'production' === process.env.NODE_ENV) return createLogger({
|
|
6
|
+
level: process.env.MODERN_SERVER_LOG_LEVEL || 'verbose'
|
|
7
|
+
});
|
|
8
|
+
return createLogger();
|
|
9
|
+
}
|
|
10
|
+
async function applyPlugins(serverBase, options, nodeServer) {
|
|
11
|
+
const { pwd, appContext, config, logger: optLogger, serverConfig } = options;
|
|
12
|
+
const enableRsc = config.server?.rsc ?? serverConfig?.server?.rsc ?? false;
|
|
13
|
+
const serverErrorHandler = options.serverConfig?.onError;
|
|
14
|
+
const loadCachePwd = isProd() ? pwd : appContext.appDirectory || pwd;
|
|
15
|
+
const cacheConfig = await loadCacheConfig(loadCachePwd);
|
|
16
|
+
serverBase.notFound((c)=>{
|
|
17
|
+
const monitors = c.get('monitors');
|
|
18
|
+
onError(ErrorDigest.ENOTF, '404 not found', monitors, c.req.raw);
|
|
19
|
+
return c.html(createErrorHtml(404), 404);
|
|
20
|
+
});
|
|
21
|
+
serverBase.onError(async (err, c)=>{
|
|
22
|
+
const monitors = c.get('monitors');
|
|
23
|
+
onError(ErrorDigest.EINTER, err, monitors, c.req.raw);
|
|
24
|
+
if (serverErrorHandler) try {
|
|
25
|
+
const result = await serverErrorHandler(err, c);
|
|
26
|
+
if (result instanceof Response) return result;
|
|
27
|
+
} catch (configError) {
|
|
28
|
+
logger.error(`Error in serverConfig.onError handler: ${configError}`);
|
|
29
|
+
}
|
|
30
|
+
const bffPrefix = config.bff?.prefix || '/api';
|
|
31
|
+
const bffPrefixList = Array.isArray(bffPrefix) ? bffPrefix : [
|
|
32
|
+
bffPrefix
|
|
33
|
+
];
|
|
34
|
+
const isApiPath = bffPrefixList.some((prefix)=>c.req.path.startsWith(prefix));
|
|
35
|
+
if (isApiPath) return c.json({
|
|
36
|
+
message: err?.message || '[BFF] Internal Server Error'
|
|
37
|
+
}, err?.status || 500);
|
|
38
|
+
return c.html(createErrorHtml(500), 500);
|
|
39
|
+
});
|
|
40
|
+
const loggerOptions = config.server.logger;
|
|
41
|
+
const { middlewares, renderMiddlewares } = options.serverConfig || {};
|
|
42
|
+
const plugins = [
|
|
43
|
+
...nodeServer ? [
|
|
44
|
+
injectNodeSeverPlugin({
|
|
45
|
+
nodeServer
|
|
46
|
+
})
|
|
47
|
+
] : [],
|
|
48
|
+
...createDefaultPlugins({
|
|
49
|
+
cacheConfig,
|
|
50
|
+
staticGenerate: options.staticGenerate,
|
|
51
|
+
logger: false === loggerOptions ? false : optLogger || getLogger(loggerOptions)
|
|
52
|
+
}),
|
|
53
|
+
injectConfigMiddlewarePlugin(middlewares, renderMiddlewares),
|
|
54
|
+
...options.plugins || [],
|
|
55
|
+
injectResourcePlugin(),
|
|
56
|
+
injectRscManifestPlugin(enableRsc),
|
|
57
|
+
serverStaticPlugin(),
|
|
58
|
+
faviconPlugin(),
|
|
59
|
+
renderPlugin()
|
|
60
|
+
];
|
|
61
|
+
serverBase.addPlugins(plugins);
|
|
62
|
+
}
|
|
63
|
+
export { applyPlugins };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createServerBase } from "@modern-js/server-core";
|
|
2
|
+
import { createNodeServer, loadServerCliConfig, loadServerEnv, loadServerPlugins, loadServerRuntimeConfig } from "@modern-js/server-core/node";
|
|
3
|
+
import { applyPlugins } from "./apply.mjs";
|
|
4
|
+
const createProdServer = async (options)=>{
|
|
5
|
+
await loadServerEnv(options);
|
|
6
|
+
const serverBaseOptions = options;
|
|
7
|
+
const serverCliConfig = 'production' === process.env.NODE_ENV ? loadServerCliConfig(options.pwd, options.config) : options.config;
|
|
8
|
+
if (serverCliConfig) serverBaseOptions.config = serverCliConfig;
|
|
9
|
+
const serverRuntimeConfig = await loadServerRuntimeConfig(options.serverConfigPath);
|
|
10
|
+
if (serverRuntimeConfig) {
|
|
11
|
+
serverBaseOptions.serverConfig = serverRuntimeConfig;
|
|
12
|
+
serverBaseOptions.plugins = [
|
|
13
|
+
...serverRuntimeConfig.plugins || [],
|
|
14
|
+
...options.plugins || []
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
const server = createServerBase(serverBaseOptions);
|
|
18
|
+
const nodeServer = await createNodeServer(server.handle.bind(server));
|
|
19
|
+
await applyPlugins(server, options, nodeServer);
|
|
20
|
+
await server.init();
|
|
21
|
+
return nodeServer;
|
|
22
|
+
};
|
|
23
|
+
const src = createProdServer;
|
|
24
|
+
export { TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, createOtlpTelemetryExporter, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, hasEnabledTelemetryExporters } from "./libs/telemetry.mjs";
|
|
25
|
+
export default src;
|
|
26
|
+
export { applyPlugins, createProdServer, loadServerPlugins };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ContractGateAutopilot } from "@modern-js/server-core";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { compatibleRequire, fs, lodash } from "@modern-js/utils";
|
|
2
|
+
import { parse } from "flatted";
|
|
3
|
+
import path from "path";
|
|
4
|
+
const getServerConfigPath = (distDirectory, serverConfigFile)=>{
|
|
5
|
+
const fileName = serverConfigFile || 'server.js';
|
|
6
|
+
return path.join(distDirectory, fileName);
|
|
7
|
+
};
|
|
8
|
+
const requireConfig = (serverConfigPath)=>{
|
|
9
|
+
if (fs.pathExistsSync(serverConfigPath)) return compatibleRequire(serverConfigPath) || {};
|
|
10
|
+
return {};
|
|
11
|
+
};
|
|
12
|
+
const loadConfig = ({ cliConfig, serverConfig, resolvedConfigPath })=>{
|
|
13
|
+
let outputConfig = {};
|
|
14
|
+
if (fs.pathExistsSync(resolvedConfigPath)) try {
|
|
15
|
+
const content = fs.readFileSync(resolvedConfigPath, 'utf8');
|
|
16
|
+
outputConfig = parse(content) || {};
|
|
17
|
+
} catch (_error) {
|
|
18
|
+
outputConfig = {};
|
|
19
|
+
}
|
|
20
|
+
return lodash.merge({}, outputConfig, serverConfig || {}, cliConfig || {});
|
|
21
|
+
};
|
|
22
|
+
export { getServerConfigPath, loadConfig, requireConfig };
|