@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,421 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import { time } from "@modern-js/runtime-utils/time";
|
|
4
|
+
import { ROUTE_SPEC_FILE, fs, isPromise, isWebOnly, mime } from "@modern-js/utils";
|
|
5
|
+
import { createServer } from "http";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { AGGRED_DIR, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE, ServerReportTimings } from "../constants";
|
|
8
|
+
import { createContext } from "../libs/context";
|
|
9
|
+
import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "../libs/hook-api";
|
|
10
|
+
import { resolveMfAssetCacheHeaders } from "../libs/mfCache";
|
|
11
|
+
import { createProxyHandler } from "../libs/proxy";
|
|
12
|
+
import { createRenderHandler } from "../libs/render/index.mjs";
|
|
13
|
+
import * as __rspack_external__libs_render_reader_11828727 from "../libs/render/reader";
|
|
14
|
+
import { RouteMatchManager } from "../libs/route";
|
|
15
|
+
import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serveFile";
|
|
16
|
+
import { createErrorDocument, createMiddlewareCollecter, debug, getStaticReg, isRedirect, mergeExtension, noop } from "../utils.mjs";
|
|
17
|
+
import { __webpack_require__ } from "../rslib-runtime.mjs";
|
|
18
|
+
import * as __rspack_external_ignore_styles_9f5737c8 from "ignore-styles";
|
|
19
|
+
__webpack_require__.add({
|
|
20
|
+
"ignore-styles" (module) {
|
|
21
|
+
module.exports = __rspack_external_ignore_styles_9f5737c8;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const SERVER_DIR = './server';
|
|
25
|
+
class ModernServer {
|
|
26
|
+
async onInit(runner, app) {
|
|
27
|
+
this.runner = runner;
|
|
28
|
+
const { distDir, conf } = this;
|
|
29
|
+
this.initReader();
|
|
30
|
+
debug('final server conf', this.conf);
|
|
31
|
+
if (conf.bff?.proxy) {
|
|
32
|
+
const { handlers, handleUpgrade } = createProxyHandler(conf.bff.proxy);
|
|
33
|
+
app && handleUpgrade(app);
|
|
34
|
+
handlers.forEach((handler)=>{
|
|
35
|
+
this.addHandler(handler);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
app?.on('close', ()=>{
|
|
39
|
+
this.reader.close();
|
|
40
|
+
});
|
|
41
|
+
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
42
|
+
this.router.reset(usageRoutes);
|
|
43
|
+
this.warmupSSRBundle();
|
|
44
|
+
await this.prepareFrameHandler();
|
|
45
|
+
await this.prepareLoaderHandler(usageRoutes, distDir);
|
|
46
|
+
this.routeRenderHandler = this.getRenderHandler();
|
|
47
|
+
await this.setupBeforeProdMiddleware();
|
|
48
|
+
this.addHandler(this.setupMfAssetCacheMiddleware());
|
|
49
|
+
this.addHandler(this.setupStaticMiddleware(this.conf.output?.assetPrefix));
|
|
50
|
+
this.addHandler(faviconFallbackHandler);
|
|
51
|
+
this.addHandler(this.routeHandler.bind(this));
|
|
52
|
+
this.compose();
|
|
53
|
+
}
|
|
54
|
+
getRenderHandler() {
|
|
55
|
+
const { distDir, staticGenerate, conf, metaName } = this;
|
|
56
|
+
const ssrConfig = this.conf.server?.ssr;
|
|
57
|
+
const forceCSR = 'object' == typeof ssrConfig ? ssrConfig.forceCSR : false;
|
|
58
|
+
return createRenderHandler({
|
|
59
|
+
distDir,
|
|
60
|
+
staticGenerate,
|
|
61
|
+
forceCSR,
|
|
62
|
+
conf: this.conf,
|
|
63
|
+
nonce: conf.security?.nonce,
|
|
64
|
+
metaName
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
onRepack(_) {}
|
|
68
|
+
getRequestHandler() {
|
|
69
|
+
return this.requestHandler.bind(this);
|
|
70
|
+
}
|
|
71
|
+
async render(req, res, url) {
|
|
72
|
+
req.logger = req.logger || this.logger;
|
|
73
|
+
req.metrics = req.metrics || this.metrics;
|
|
74
|
+
const context = createContext(req, res, {
|
|
75
|
+
metaName: this.metaName
|
|
76
|
+
});
|
|
77
|
+
const matched = this.router.match(url || context.path);
|
|
78
|
+
if (!matched) return null;
|
|
79
|
+
res.statusCode = 200;
|
|
80
|
+
const route = matched.generate(context.url);
|
|
81
|
+
const result = await this.handleWeb(context, route);
|
|
82
|
+
if (!result) return null;
|
|
83
|
+
if (result.contentStream) return result.contentStream;
|
|
84
|
+
return result.content.toString();
|
|
85
|
+
}
|
|
86
|
+
async createHTTPServer(handler) {
|
|
87
|
+
return createServer(handler);
|
|
88
|
+
}
|
|
89
|
+
initReader() {
|
|
90
|
+
this.reader.init();
|
|
91
|
+
}
|
|
92
|
+
async onServerChange({ filepath }) {
|
|
93
|
+
const { pwd } = this;
|
|
94
|
+
const { api, server } = AGGRED_DIR;
|
|
95
|
+
const apiPath = path.normalize(path.join(pwd, api));
|
|
96
|
+
const serverPath = path.normalize(path.join(pwd, server));
|
|
97
|
+
const onlyApi = filepath.startsWith(apiPath);
|
|
98
|
+
const onlyWeb = filepath.startsWith(serverPath);
|
|
99
|
+
await this.prepareFrameHandler({
|
|
100
|
+
onlyWeb,
|
|
101
|
+
onlyApi
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
getRoutes() {
|
|
105
|
+
if (this.presetRoutes) return this.presetRoutes;
|
|
106
|
+
const file = path.join(this.distDir, ROUTE_SPEC_FILE);
|
|
107
|
+
if (fs.existsSync(file)) {
|
|
108
|
+
const content = fs.readJSONSync(file);
|
|
109
|
+
return content.routes;
|
|
110
|
+
}
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
addHandler(handler) {
|
|
114
|
+
this.handlers.push(handler);
|
|
115
|
+
}
|
|
116
|
+
render404(context) {
|
|
117
|
+
context.error(ERROR_DIGEST.ENOTF, '404 Not Found');
|
|
118
|
+
this.renderErrorPage(context, 404);
|
|
119
|
+
}
|
|
120
|
+
async prepareLoaderHandler(specs, distDir) {
|
|
121
|
+
const { runner } = this;
|
|
122
|
+
const handler = await runner.prepareLoaderHandler({
|
|
123
|
+
serverRoutes: specs,
|
|
124
|
+
distDir
|
|
125
|
+
}, {
|
|
126
|
+
onLast: ()=>null
|
|
127
|
+
});
|
|
128
|
+
this.loaderHandler = handler;
|
|
129
|
+
}
|
|
130
|
+
async prepareFrameHandler(options) {
|
|
131
|
+
const { workDir, runner } = this;
|
|
132
|
+
const { onlyApi, onlyWeb } = options || {};
|
|
133
|
+
const { getMiddlewares, ...collector } = createMiddlewareCollecter();
|
|
134
|
+
await runner.gather(collector);
|
|
135
|
+
const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
|
|
136
|
+
const serverDir = path.join(workDir, SERVER_DIR);
|
|
137
|
+
if (await fs.pathExists(path.join(serverDir)) && !onlyApi) {
|
|
138
|
+
const webExtension = mergeExtension(pluginWebExt);
|
|
139
|
+
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
140
|
+
}
|
|
141
|
+
if (!onlyWeb) {
|
|
142
|
+
const apiExtension = mergeExtension(pluginAPIExt);
|
|
143
|
+
this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async prepareWebHandler(extension) {
|
|
147
|
+
const { workDir, runner } = this;
|
|
148
|
+
const handler = await runner.prepareWebServer({
|
|
149
|
+
pwd: workDir,
|
|
150
|
+
config: extension
|
|
151
|
+
}, {
|
|
152
|
+
onLast: ()=>null
|
|
153
|
+
});
|
|
154
|
+
return handler;
|
|
155
|
+
}
|
|
156
|
+
async prepareAPIHandler(extension) {
|
|
157
|
+
const { workDir, runner, conf } = this;
|
|
158
|
+
const { bff } = conf;
|
|
159
|
+
const prefix = bff?.prefix || '/api';
|
|
160
|
+
const webOnly = await isWebOnly();
|
|
161
|
+
if (webOnly && 'development' === process.env.NODE_ENV) return (req, res)=>{
|
|
162
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
163
|
+
res.end(JSON.stringify(''));
|
|
164
|
+
};
|
|
165
|
+
return runner.prepareApiServer({
|
|
166
|
+
pwd: workDir,
|
|
167
|
+
config: extension,
|
|
168
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
169
|
+
httpMethodDecider: bff?.httpMethodDecider,
|
|
170
|
+
render: this.render.bind(this)
|
|
171
|
+
}, {
|
|
172
|
+
onLast: ()=>null
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
filterRoutes(routes) {
|
|
176
|
+
return routes;
|
|
177
|
+
}
|
|
178
|
+
async setupBeforeProdMiddleware() {
|
|
179
|
+
const { conf, runner } = this;
|
|
180
|
+
const preMiddleware = await runner.beforeProdServer(conf);
|
|
181
|
+
preMiddleware.flat().forEach((mid)=>{
|
|
182
|
+
this.addHandler(mid);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
setupStaticMiddleware(prefix) {
|
|
186
|
+
const staticPathRegExp = getStaticReg(this.conf.output, this.conf.html, prefix);
|
|
187
|
+
return createStaticFileHandler([
|
|
188
|
+
{
|
|
189
|
+
path: staticPathRegExp,
|
|
190
|
+
target: this.distDir
|
|
191
|
+
}
|
|
192
|
+
], prefix);
|
|
193
|
+
}
|
|
194
|
+
setupMfAssetCacheMiddleware() {
|
|
195
|
+
return async (context, next)=>{
|
|
196
|
+
const headers = resolveMfAssetCacheHeaders(context.url, context.query);
|
|
197
|
+
if (headers) for (const [key, value] of Object.entries(headers))context.res.setHeader(key, value);
|
|
198
|
+
await next();
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
async handleAPI(context) {
|
|
202
|
+
const { req, res } = context;
|
|
203
|
+
if (!this.frameAPIHandler) throw new Error('can not found api handler');
|
|
204
|
+
await this.frameAPIHandler(req, res);
|
|
205
|
+
}
|
|
206
|
+
async handleWeb(context, route) {
|
|
207
|
+
const { res } = context;
|
|
208
|
+
if (this.loaderHandler) {
|
|
209
|
+
await this.loaderHandler(context);
|
|
210
|
+
if (this.isSend(res)) return null;
|
|
211
|
+
}
|
|
212
|
+
context.setParams(route.params);
|
|
213
|
+
context.setServerData('router', {
|
|
214
|
+
baseUrl: route.urlPath,
|
|
215
|
+
params: route.params
|
|
216
|
+
});
|
|
217
|
+
if (route.responseHeaders) Object.keys(route.responseHeaders).forEach((key)=>{
|
|
218
|
+
const value = route.responseHeaders[key];
|
|
219
|
+
if (value) context.res.setHeader(key, value);
|
|
220
|
+
});
|
|
221
|
+
const renderResult = await this.routeRenderHandler({
|
|
222
|
+
ctx: context,
|
|
223
|
+
route,
|
|
224
|
+
runner: this.runner
|
|
225
|
+
});
|
|
226
|
+
if (!renderResult) {
|
|
227
|
+
this.render404(context);
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
if (renderResult.redirect) {
|
|
231
|
+
this.redirect(res, renderResult.content, renderResult.statusCode);
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
if (this.isSend(res)) return null;
|
|
235
|
+
res.set('content-type', renderResult.contentType);
|
|
236
|
+
return renderResult;
|
|
237
|
+
}
|
|
238
|
+
async proxy() {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
warmupSSRBundle() {
|
|
242
|
+
const { distDir } = this;
|
|
243
|
+
const bundles = this.router.getBundles();
|
|
244
|
+
bundles.forEach((bundle)=>{
|
|
245
|
+
const filepath = path.join(distDir, bundle);
|
|
246
|
+
if (fs.existsSync(filepath)) require(filepath);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
createContext(req, res, options = {}) {
|
|
250
|
+
return createContext(req, res, options);
|
|
251
|
+
}
|
|
252
|
+
async routeHandler(context) {
|
|
253
|
+
const { res, reporter } = context;
|
|
254
|
+
const matched = this.router.match(context.path);
|
|
255
|
+
if (!matched) return void this.render404(context);
|
|
256
|
+
await reporter.init({
|
|
257
|
+
match: matched
|
|
258
|
+
});
|
|
259
|
+
const end = time();
|
|
260
|
+
res.on('finish', ()=>{
|
|
261
|
+
const cost = end();
|
|
262
|
+
reporter.reportTiming(ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
|
|
263
|
+
});
|
|
264
|
+
let route = matched.generate(context.url);
|
|
265
|
+
if (route.isApi) return void await this.handleAPI(context);
|
|
266
|
+
if (route.entryName && this.runMode === RUN_MODE.FULL) {
|
|
267
|
+
const afterMatchContext = createAfterMatchContext(context, route.entryName);
|
|
268
|
+
const end = time();
|
|
269
|
+
await this.runner.afterMatch(afterMatchContext, {
|
|
270
|
+
onLast: noop
|
|
271
|
+
});
|
|
272
|
+
const cost = end();
|
|
273
|
+
cost && reporter.reportTiming(ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
|
|
274
|
+
if (this.isSend(res)) return;
|
|
275
|
+
const { current, url, status } = afterMatchContext.router;
|
|
276
|
+
if (url) return void this.redirect(res, url, status);
|
|
277
|
+
if (route.entryName !== current) {
|
|
278
|
+
const matched = this.router.matchEntry(current);
|
|
279
|
+
if (!matched) return void this.render404(context);
|
|
280
|
+
route = matched.generate(context.url);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (this.frameWebHandler) {
|
|
284
|
+
res.locals = res.locals || {};
|
|
285
|
+
const middlewareContext = createMiddlewareContext(context);
|
|
286
|
+
const end = time();
|
|
287
|
+
await this.frameWebHandler(middlewareContext);
|
|
288
|
+
const cost = end();
|
|
289
|
+
cost && reporter.reportTiming(ServerReportTimings.SERVER_MIDDLEWARE, cost);
|
|
290
|
+
res.locals = {
|
|
291
|
+
...res.locals,
|
|
292
|
+
...middlewareContext.response.locals
|
|
293
|
+
};
|
|
294
|
+
if (this.isSend(res)) return;
|
|
295
|
+
}
|
|
296
|
+
const renderResult = await this.handleWeb(context, route);
|
|
297
|
+
if (!renderResult) return;
|
|
298
|
+
const { contentStream: responseStream } = renderResult;
|
|
299
|
+
let { content: response } = renderResult;
|
|
300
|
+
if (route.entryName && responseStream) return void responseStream.pipe(res);
|
|
301
|
+
if (route.entryName && this.runMode === RUN_MODE.FULL) {
|
|
302
|
+
const afterRenderContext = createAfterRenderContext(context, response.toString());
|
|
303
|
+
const end = time();
|
|
304
|
+
await this.runner.afterRender(afterRenderContext, {
|
|
305
|
+
onLast: noop
|
|
306
|
+
});
|
|
307
|
+
const cost = end();
|
|
308
|
+
cost && reporter.reportTiming(ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost);
|
|
309
|
+
if (this.isSend(res)) return;
|
|
310
|
+
response = afterRenderContext.template.get();
|
|
311
|
+
}
|
|
312
|
+
res.end(response);
|
|
313
|
+
}
|
|
314
|
+
isSend(res) {
|
|
315
|
+
if (res.modernFlushedHeaders) {
|
|
316
|
+
if (res.writableFinished) return true;
|
|
317
|
+
} else if (res.headersSent) return true;
|
|
318
|
+
if (res.getHeader('Location') && isRedirect(res.statusCode)) {
|
|
319
|
+
res.end();
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
compose() {
|
|
325
|
+
const { handlers } = this;
|
|
326
|
+
if (!Array.isArray(handlers)) throw new TypeError('Middleware stack must be an array!');
|
|
327
|
+
for (const fn of handlers)if ('function' != typeof fn) throw new TypeError('Middleware must be composed of functions!');
|
|
328
|
+
this._handler = (context, next)=>{
|
|
329
|
+
let i = 0;
|
|
330
|
+
const dispatch = (error)=>{
|
|
331
|
+
if (error) return this.onError(context, error);
|
|
332
|
+
const handler = handlers[i++];
|
|
333
|
+
if (!handler) return next();
|
|
334
|
+
try {
|
|
335
|
+
const result = handler(context, dispatch);
|
|
336
|
+
if (isPromise(result)) return result.catch(onError);
|
|
337
|
+
} catch (e) {
|
|
338
|
+
return onError(e);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
const onError = (err)=>{
|
|
342
|
+
this.onError(context, err);
|
|
343
|
+
};
|
|
344
|
+
return dispatch();
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
requestHandler(req, res, next = ()=>{}) {
|
|
348
|
+
res.statusCode = 200;
|
|
349
|
+
req.logger = req.logger || this.logger;
|
|
350
|
+
req.metrics = req.metrics || this.metrics;
|
|
351
|
+
let context;
|
|
352
|
+
try {
|
|
353
|
+
context = this.createContext(req, res, {
|
|
354
|
+
metaName: this.metaName
|
|
355
|
+
});
|
|
356
|
+
} catch (e) {
|
|
357
|
+
this.logger.error(e);
|
|
358
|
+
res.statusCode = 500;
|
|
359
|
+
res.setHeader('content-type', mime.contentType('html'));
|
|
360
|
+
return res.end(createErrorDocument(500, ERROR_PAGE_TEXT["500"]));
|
|
361
|
+
}
|
|
362
|
+
try {
|
|
363
|
+
return this._handler(context, next);
|
|
364
|
+
} catch (err) {
|
|
365
|
+
return this.onError(context, err);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
redirect(res, url, status = 302) {
|
|
369
|
+
res.set('Location', url);
|
|
370
|
+
res.statusCode = status;
|
|
371
|
+
res.end();
|
|
372
|
+
}
|
|
373
|
+
onError(context, err) {
|
|
374
|
+
context.error(ERROR_DIGEST.EINTER, err);
|
|
375
|
+
this.renderErrorPage(context, 500);
|
|
376
|
+
}
|
|
377
|
+
async renderErrorPage(context, status) {
|
|
378
|
+
const { res } = context;
|
|
379
|
+
context.status = status;
|
|
380
|
+
res.set('content-type', mime.contentType('html'));
|
|
381
|
+
const statusPage = `/${status}`;
|
|
382
|
+
const customErrorPage = "/_error";
|
|
383
|
+
const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
|
|
384
|
+
if (matched) {
|
|
385
|
+
const route = matched.generate(context.url);
|
|
386
|
+
const { entryName } = route;
|
|
387
|
+
if (entryName === status.toString() || '_error' === entryName) try {
|
|
388
|
+
const file = await this.routeRenderHandler({
|
|
389
|
+
route,
|
|
390
|
+
ctx: context,
|
|
391
|
+
runner: this.runner
|
|
392
|
+
});
|
|
393
|
+
if (file) return void context.res.end(file.content);
|
|
394
|
+
} catch (e) {}
|
|
395
|
+
}
|
|
396
|
+
const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT["500"];
|
|
397
|
+
context.res.end(createErrorDocument(status, text));
|
|
398
|
+
}
|
|
399
|
+
constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }){
|
|
400
|
+
this.handlers = [];
|
|
401
|
+
this.reader = __rspack_external__libs_render_reader_11828727;
|
|
402
|
+
this.loaderHandler = null;
|
|
403
|
+
this.frameWebHandler = null;
|
|
404
|
+
this.frameAPIHandler = null;
|
|
405
|
+
__webpack_require__("ignore-styles");
|
|
406
|
+
this.pwd = pwd;
|
|
407
|
+
this.distDir = path.resolve(pwd, config.output.path || 'dist');
|
|
408
|
+
this.workDir = this.distDir;
|
|
409
|
+
this.conf = config;
|
|
410
|
+
debug('server conf', this.conf);
|
|
411
|
+
this.logger = logger;
|
|
412
|
+
this.metrics = metrics;
|
|
413
|
+
this.router = new RouteMatchManager();
|
|
414
|
+
this.presetRoutes = routes;
|
|
415
|
+
this.proxyTarget = proxyTarget;
|
|
416
|
+
this.staticGenerate = staticGenerate || false;
|
|
417
|
+
this.runMode = runMode || RUN_MODE.FULL;
|
|
418
|
+
this.metaName = appContext?.metaName;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
export { ModernServer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Server as NodeServer } from 'node:http';
|
|
2
|
+
import type { Http2SecureServer } from 'node:http2';
|
|
3
|
+
import { type ServerBase } from '@modern-js/server-core';
|
|
4
|
+
import type { ProdServerOptions } from './types';
|
|
5
|
+
export type ApplyPlugins = typeof applyPlugins;
|
|
6
|
+
export declare function applyPlugins(serverBase: ServerBase, options: ProdServerOptions, nodeServer?: NodeServer | Http2SecureServer): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { loadServerPlugins } from '@modern-js/server-core/node';
|
|
2
|
+
import type { ProdServerOptions } from './types';
|
|
3
|
+
export type { ServerPlugin } from '@modern-js/server-core';
|
|
4
|
+
export { type ApplyPlugins, applyPlugins } from './apply';
|
|
5
|
+
export type { TelemetryCanaryDecision, TelemetryQueueStats, TelemetrySloAlert, } from './libs/telemetry';
|
|
6
|
+
export { createOtlpTelemetryExporter, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, hasEnabledTelemetryExporters, TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, } from './libs/telemetry';
|
|
7
|
+
export type { BaseEnv, ProdServerOptions } from './types';
|
|
8
|
+
export { loadServerPlugins };
|
|
9
|
+
export declare const createProdServer: (options: ProdServerOptions) => Promise<(import("node:http").Server<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse> | import("node:https").Server<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse> | import("node:http2").Http2SecureServer<typeof import("node:http").IncomingMessage, typeof import("node:http").ServerResponse, typeof import("node:http2").Http2ServerRequest, typeof import("node:http2").Http2ServerResponse>) & {
|
|
10
|
+
getRequestListener: () => ReturnType<(handler: import("@modern-js/server-core/dist/types/types").RequestHandler) => (req: import("@modern-js/types/server").NodeRequest, res: import("@modern-js/types/server").NodeResponse) => Promise<void>>;
|
|
11
|
+
getRequestHandler: () => import("@modern-js/server-core/dist/types/types").RequestHandler;
|
|
12
|
+
}>;
|
|
13
|
+
export default createProdServer;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type MetricTags = Record<string, unknown>;
|
|
2
|
+
type Metrics = {
|
|
3
|
+
emitCounter: (name: string, value: number, prefix?: string, labels?: MetricTags) => void;
|
|
4
|
+
emitTimer: (name: string, value: number, prefix?: string, labels?: MetricTags) => void;
|
|
5
|
+
gauges: (labels: MetricTags, value: number, name: string, prefix?: string) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare const metrics: Metrics;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export type { RuntimeFallbackSignalAuthConfig, RuntimeFallbackSignalRuntimeState, RuntimeFallbackSignalTrustContext, RuntimeFallbackSignalTrustPolicy, RuntimeSignalError, TelemetryCanaryDecision, TelemetryCanaryStatusSnapshot, TelemetryQueueStats, TelemetrySloAlert, } from '@modern-js/server-core';
|
|
2
|
+
export { createOtlpTelemetryExporter, createRuntimeFallbackSignalRuntimeState, createRuntimeSignalError, createTelemetryAwareMetrics, createVictoriaMetricsTelemetryExporter, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, DEFAULT_RUNTIME_STATUS_ENDPOINT, enforceRuntimeFallbackSignalAuthToken, enforceRuntimeFallbackSignalTrustPolicy, getRuntimeSignalErrorStatusCode, hasEnabledTelemetryExporters, normalizeRuntimeFallbackSignalAuthConfig, normalizeRuntimeFallbackTrustPolicy, parseRuntimeFallbackSignalPayloadFromRawBody, resolveRuntimeFallbackSignalEndpoint, TelemetryCanaryOrchestrator, TelemetryRegistry, TelemetryStartupHealthError, } from '@modern-js/server-core';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CreateDefaultPluginsOptions, ServerBaseOptions, ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import type { Reporter } from '@modern-js/types';
|
|
3
|
+
import type { Logger } from '@modern-js/utils';
|
|
4
|
+
interface ProdServerExtraOptions {
|
|
5
|
+
serverConfigPath: string;
|
|
6
|
+
plugins?: ServerPlugin[];
|
|
7
|
+
}
|
|
8
|
+
export type ProdServerOptions = ServerBaseOptions & ProdServerExtraOptions & CreateDefaultPluginsOptions;
|
|
9
|
+
export type BaseEnv = {
|
|
10
|
+
Variables: {
|
|
11
|
+
logger: Logger;
|
|
12
|
+
reporter: Reporter;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bleedingdev/modern-js-prod-server",
|
|
3
|
+
"description": "A Progressive React Framework for modern web development.",
|
|
4
|
+
"homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/BleedingDev/ultramodern.js/issues"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/BleedingDev/ultramodern.js.git",
|
|
11
|
+
"directory": "packages/server/prod-server"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"framework",
|
|
17
|
+
"modern",
|
|
18
|
+
"modern.js"
|
|
19
|
+
],
|
|
20
|
+
"version": "3.2.0-ultramodern.0",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"main": "./dist/cjs/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"node": {
|
|
27
|
+
"import": "./dist/esm-node/index.mjs",
|
|
28
|
+
"require": "./dist/cjs/index.js"
|
|
29
|
+
},
|
|
30
|
+
"default": "./dist/cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./netlify": {
|
|
33
|
+
"types": "./dist/types/netlify.d.ts",
|
|
34
|
+
"node": {
|
|
35
|
+
"import": "./dist/esm-node/netlify.mjs",
|
|
36
|
+
"require": "./dist/cjs/netlify.js"
|
|
37
|
+
},
|
|
38
|
+
"default": "./dist/cjs/netlify.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"*": {
|
|
43
|
+
".": [
|
|
44
|
+
"./dist/types/index.d.ts"
|
|
45
|
+
],
|
|
46
|
+
"netlify": [
|
|
47
|
+
"./dist/types/netlify.d.ts"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@swc/helpers": "^0.5.21",
|
|
53
|
+
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.2.0-ultramodern.0",
|
|
54
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.0",
|
|
55
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@rslib/core": "0.21.5",
|
|
59
|
+
"@types/merge-deep": "^3.0.3",
|
|
60
|
+
"@types/node": "^25.8.0",
|
|
61
|
+
"@typescript/native-preview": "7.0.0-dev.20260516.1",
|
|
62
|
+
"lint-staged": "~17.0.5",
|
|
63
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.0",
|
|
64
|
+
"@scripts/rstest-config": "2.66.0"
|
|
65
|
+
},
|
|
66
|
+
"sideEffects": [],
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=20"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public",
|
|
72
|
+
"registry": "https://registry.npmjs.org/"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "rslib build",
|
|
76
|
+
"dev": "rslib build --watch",
|
|
77
|
+
"test": "rstest --passWithNoTests"
|
|
78
|
+
}
|
|
79
|
+
}
|
package/rslib.config.mts
ADDED