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