@modern-js/prod-server 2.48.6 → 2.49.1

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.
Files changed (174) hide show
  1. package/README.md +20 -16
  2. package/dist/cjs/{libs/route/route.js → error.js} +16 -17
  3. package/dist/cjs/index.js +18 -24
  4. package/dist/cjs/init.js +71 -0
  5. package/dist/cjs/{type.js → types.js} +2 -2
  6. package/dist/esm/error.js +15 -0
  7. package/dist/esm/index.js +53 -19
  8. package/dist/esm/init.js +73 -0
  9. package/dist/esm-node/error.js +14 -0
  10. package/dist/esm-node/index.js +16 -17
  11. package/dist/esm-node/init.js +47 -0
  12. package/dist/types/error.d.ts +6 -0
  13. package/dist/types/index.d.ts +8 -11
  14. package/dist/types/init.d.ts +4 -0
  15. package/dist/types/types.d.ts +14 -0
  16. package/package.json +23 -64
  17. package/dist/cjs/constants.js +0 -72
  18. package/dist/cjs/libs/context/context.js +0 -236
  19. package/dist/cjs/libs/context/index.js +0 -31
  20. package/dist/cjs/libs/hook-api/afterRenderForStream.js +0 -34
  21. package/dist/cjs/libs/hook-api/base.js +0 -96
  22. package/dist/cjs/libs/hook-api/index.js +0 -85
  23. package/dist/cjs/libs/hook-api/index.worker.js +0 -116
  24. package/dist/cjs/libs/hook-api/route.js +0 -44
  25. package/dist/cjs/libs/hook-api/template.js +0 -72
  26. package/dist/cjs/libs/hook-api/templateForStream.js +0 -52
  27. package/dist/cjs/libs/loadConfig.js +0 -70
  28. package/dist/cjs/libs/metrics.js +0 -33
  29. package/dist/cjs/libs/preload/flushServerHeader.js +0 -49
  30. package/dist/cjs/libs/preload/index.js +0 -24
  31. package/dist/cjs/libs/preload/parseLinks.js +0 -124
  32. package/dist/cjs/libs/preload/shouldFlushServerHeader.js +0 -52
  33. package/dist/cjs/libs/preload/transformLinks2String.js +0 -150
  34. package/dist/cjs/libs/proxy.js +0 -99
  35. package/dist/cjs/libs/render/index.js +0 -109
  36. package/dist/cjs/libs/render/measure.js +0 -74
  37. package/dist/cjs/libs/render/ssr.js +0 -130
  38. package/dist/cjs/libs/render/ssrCache/cacheMod.js +0 -51
  39. package/dist/cjs/libs/render/ssrCache/index.js +0 -90
  40. package/dist/cjs/libs/render/ssrCache/manager.js +0 -120
  41. package/dist/cjs/libs/render/static.js +0 -72
  42. package/dist/cjs/libs/render/type.js +0 -33
  43. package/dist/cjs/libs/render/utils.js +0 -41
  44. package/dist/cjs/libs/reporter.js +0 -39
  45. package/dist/cjs/libs/route/index.js +0 -83
  46. package/dist/cjs/libs/route/matcher.js +0 -113
  47. package/dist/cjs/libs/serveFile.js +0 -84
  48. package/dist/cjs/libs/serverTiming.js +0 -41
  49. package/dist/cjs/renderHtml.js +0 -65
  50. package/dist/cjs/server/index.js +0 -223
  51. package/dist/cjs/server/modernServer.js +0 -522
  52. package/dist/cjs/server/modernServerSplit.js +0 -67
  53. package/dist/cjs/utils.js +0 -164
  54. package/dist/cjs/workerServer.js +0 -260
  55. package/dist/esm/constants.js +0 -43
  56. package/dist/esm/libs/context/context.js +0 -260
  57. package/dist/esm/libs/context/index.js +0 -8
  58. package/dist/esm/libs/hook-api/afterRenderForStream.js +0 -33
  59. package/dist/esm/libs/hook-api/base.js +0 -73
  60. package/dist/esm/libs/hook-api/index.js +0 -55
  61. package/dist/esm/libs/hook-api/index.worker.js +0 -99
  62. package/dist/esm/libs/hook-api/route.js +0 -26
  63. package/dist/esm/libs/hook-api/template.js +0 -56
  64. package/dist/esm/libs/hook-api/templateForStream.js +0 -35
  65. package/dist/esm/libs/loadConfig.js +0 -36
  66. package/dist/esm/libs/metrics.js +0 -9
  67. package/dist/esm/libs/preload/flushServerHeader.js +0 -50
  68. package/dist/esm/libs/preload/index.js +0 -2
  69. package/dist/esm/libs/preload/parseLinks.js +0 -172
  70. package/dist/esm/libs/preload/shouldFlushServerHeader.js +0 -27
  71. package/dist/esm/libs/preload/transformLinks2String.js +0 -140
  72. package/dist/esm/libs/proxy.js +0 -160
  73. package/dist/esm/libs/render/index.js +0 -175
  74. package/dist/esm/libs/render/measure.js +0 -69
  75. package/dist/esm/libs/render/ssr.js +0 -133
  76. package/dist/esm/libs/render/ssrCache/cacheMod.js +0 -25
  77. package/dist/esm/libs/render/ssrCache/index.js +0 -170
  78. package/dist/esm/libs/render/ssrCache/manager.js +0 -178
  79. package/dist/esm/libs/render/static.js +0 -100
  80. package/dist/esm/libs/render/type.js +0 -9
  81. package/dist/esm/libs/render/utils.js +0 -16
  82. package/dist/esm/libs/reporter.js +0 -15
  83. package/dist/esm/libs/route/index.js +0 -82
  84. package/dist/esm/libs/route/matcher.js +0 -92
  85. package/dist/esm/libs/route/route.js +0 -17
  86. package/dist/esm/libs/serveFile.js +0 -68
  87. package/dist/esm/libs/serverTiming.js +0 -22
  88. package/dist/esm/renderHtml.js +0 -133
  89. package/dist/esm/server/index.js +0 -350
  90. package/dist/esm/server/modernServer.js +0 -884
  91. package/dist/esm/server/modernServerSplit.js +0 -113
  92. package/dist/esm/utils.js +0 -103
  93. package/dist/esm/workerServer.js +0 -352
  94. package/dist/esm-node/constants.js +0 -43
  95. package/dist/esm-node/libs/context/context.js +0 -202
  96. package/dist/esm-node/libs/context/index.js +0 -6
  97. package/dist/esm-node/libs/hook-api/afterRenderForStream.js +0 -10
  98. package/dist/esm-node/libs/hook-api/base.js +0 -61
  99. package/dist/esm-node/libs/hook-api/index.js +0 -57
  100. package/dist/esm-node/libs/hook-api/index.worker.js +0 -89
  101. package/dist/esm-node/libs/hook-api/route.js +0 -20
  102. package/dist/esm-node/libs/hook-api/template.js +0 -47
  103. package/dist/esm-node/libs/hook-api/templateForStream.js +0 -28
  104. package/dist/esm-node/libs/loadConfig.js +0 -34
  105. package/dist/esm-node/libs/metrics.js +0 -9
  106. package/dist/esm-node/libs/preload/flushServerHeader.js +0 -25
  107. package/dist/esm-node/libs/preload/index.js +0 -2
  108. package/dist/esm-node/libs/preload/parseLinks.js +0 -90
  109. package/dist/esm-node/libs/preload/shouldFlushServerHeader.js +0 -27
  110. package/dist/esm-node/libs/preload/transformLinks2String.js +0 -126
  111. package/dist/esm-node/libs/proxy.js +0 -74
  112. package/dist/esm-node/libs/render/index.js +0 -75
  113. package/dist/esm-node/libs/render/measure.js +0 -49
  114. package/dist/esm-node/libs/render/ssr.js +0 -96
  115. package/dist/esm-node/libs/render/ssrCache/cacheMod.js +0 -17
  116. package/dist/esm-node/libs/render/ssrCache/index.js +0 -66
  117. package/dist/esm-node/libs/render/ssrCache/manager.js +0 -96
  118. package/dist/esm-node/libs/render/static.js +0 -38
  119. package/dist/esm-node/libs/render/type.js +0 -9
  120. package/dist/esm-node/libs/render/utils.js +0 -16
  121. package/dist/esm-node/libs/reporter.js +0 -15
  122. package/dist/esm-node/libs/route/index.js +0 -58
  123. package/dist/esm-node/libs/route/matcher.js +0 -89
  124. package/dist/esm-node/libs/route/route.js +0 -16
  125. package/dist/esm-node/libs/serveFile.js +0 -49
  126. package/dist/esm-node/libs/serverTiming.js +0 -17
  127. package/dist/esm-node/renderHtml.js +0 -73
  128. package/dist/esm-node/server/index.js +0 -189
  129. package/dist/esm-node/server/modernServer.js +0 -488
  130. package/dist/esm-node/server/modernServerSplit.js +0 -43
  131. package/dist/esm-node/utils.js +0 -132
  132. package/dist/esm-node/workerServer.js +0 -235
  133. package/dist/types/constants.d.ts +0 -30
  134. package/dist/types/libs/context/context.d.ts +0 -62
  135. package/dist/types/libs/context/index.d.ts +0 -7
  136. package/dist/types/libs/hook-api/afterRenderForStream.d.ts +0 -4
  137. package/dist/types/libs/hook-api/base.d.ts +0 -53
  138. package/dist/types/libs/hook-api/index.d.ts +0 -6
  139. package/dist/types/libs/hook-api/index.worker.d.ts +0 -19
  140. package/dist/types/libs/hook-api/route.d.ts +0 -9
  141. package/dist/types/libs/hook-api/template.d.ts +0 -22
  142. package/dist/types/libs/hook-api/templateForStream.d.ts +0 -8
  143. package/dist/types/libs/loadConfig.d.ts +0 -13
  144. package/dist/types/libs/metrics.d.ts +0 -3
  145. package/dist/types/libs/preload/flushServerHeader.d.ts +0 -14
  146. package/dist/types/libs/preload/index.d.ts +0 -2
  147. package/dist/types/libs/preload/parseLinks.d.ts +0 -13
  148. package/dist/types/libs/preload/shouldFlushServerHeader.d.ts +0 -3
  149. package/dist/types/libs/preload/transformLinks2String.d.ts +0 -3
  150. package/dist/types/libs/proxy.d.ts +0 -13
  151. package/dist/types/libs/render/index.d.ts +0 -21
  152. package/dist/types/libs/render/measure.d.ts +0 -10
  153. package/dist/types/libs/render/ssr.d.ts +0 -12
  154. package/dist/types/libs/render/ssrCache/cacheMod.d.ts +0 -8
  155. package/dist/types/libs/render/ssrCache/index.d.ts +0 -6
  156. package/dist/types/libs/render/ssrCache/manager.d.ts +0 -20
  157. package/dist/types/libs/render/static.d.ts +0 -3
  158. package/dist/types/libs/render/type.d.ts +0 -36
  159. package/dist/types/libs/render/utils.d.ts +0 -5
  160. package/dist/types/libs/reporter.d.ts +0 -2
  161. package/dist/types/libs/route/index.d.ts +0 -15
  162. package/dist/types/libs/route/matcher.d.ts +0 -15
  163. package/dist/types/libs/route/route.d.ts +0 -14
  164. package/dist/types/libs/serveFile.d.ts +0 -9
  165. package/dist/types/libs/serverTiming.d.ts +0 -13
  166. package/dist/types/renderHtml.d.ts +0 -23
  167. package/dist/types/server/index.d.ts +0 -55
  168. package/dist/types/server/modernServer.d.ts +0 -71
  169. package/dist/types/server/modernServerSplit.d.ts +0 -2
  170. package/dist/types/type.d.ts +0 -78
  171. package/dist/types/utils.d.ts +0 -24
  172. package/dist/types/workerServer.d.ts +0 -59
  173. /package/dist/esm/{type.js → types.js} +0 -0
  174. /package/dist/esm-node/{type.js → types.js} +0 -0
@@ -1,522 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var modernServer_exports = {};
30
- __export(modernServer_exports, {
31
- ModernServer: () => ModernServer
32
- });
33
- module.exports = __toCommonJS(modernServer_exports);
34
- var import_http = require("http");
35
- var import_path = __toESM(require("path"));
36
- var import_utils = require("@modern-js/utils");
37
- var import_time = require("@modern-js/runtime-utils/time");
38
- var import_route = require("../libs/route");
39
- var import_render = require("../libs/render");
40
- var import_serveFile = require("../libs/serveFile");
41
- var import_utils2 = require("../utils");
42
- var import_proxy = require("../libs/proxy");
43
- var import_context = require("../libs/context");
44
- var import_constants = require("../constants");
45
- var import_hook_api = require("../libs/hook-api");
46
- var import_cacheMod = require("../libs/render/ssrCache/cacheMod");
47
- const SERVER_DIR = "./server";
48
- class ModernServer {
49
- // server prepare
50
- async onInit(runner, app) {
51
- var _conf_bff, _this_conf_output;
52
- this.runner = runner;
53
- const { distDir, conf } = this;
54
- (0, import_utils2.debug)("final server conf", this.conf);
55
- if ((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy) {
56
- const { handlers, handleUpgrade } = (0, import_proxy.createProxyHandler)(conf.bff.proxy);
57
- app && handleUpgrade(app);
58
- handlers.forEach((handler) => {
59
- this.addHandler(handler);
60
- });
61
- }
62
- const usageRoutes = this.filterRoutes(this.getRoutes());
63
- this.router.reset(usageRoutes);
64
- this.warmupSSRBundle();
65
- import_cacheMod.cacheMod.loadServerCacheMod((0, import_utils.isProd)() ? distDir : this.pwd);
66
- await this.prepareFrameHandler();
67
- await this.prepareLoaderHandler(usageRoutes, distDir);
68
- this.routeRenderHandler = this.getRenderHandler();
69
- await this.setupBeforeProdMiddleware();
70
- this.addHandler(this.setupStaticMiddleware((_this_conf_output = this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
71
- this.addHandler(import_serveFile.faviconFallbackHandler);
72
- this.addHandler(this.routeHandler.bind(this));
73
- this.compose();
74
- }
75
- getRenderHandler() {
76
- var _this_conf_server, _conf_security;
77
- const { distDir, staticGenerate, conf, metaName } = this;
78
- const ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
79
- const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
80
- return (0, import_render.createRenderHandler)({
81
- distDir,
82
- staticGenerate,
83
- forceCSR,
84
- conf: this.conf,
85
- nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
86
- metaName
87
- });
88
- }
89
- // server ready
90
- onRepack(_) {
91
- }
92
- // exposed requestHandler
93
- getRequestHandler() {
94
- return this.requestHandler.bind(this);
95
- }
96
- async render(req, res, url) {
97
- req.logger = req.logger || this.logger;
98
- req.metrics = req.metrics || this.metrics;
99
- const context = (0, import_context.createContext)(req, res, {
100
- metaName: this.metaName
101
- });
102
- const matched = this.router.match(url || context.path);
103
- if (!matched) {
104
- return null;
105
- }
106
- res.statusCode = 200;
107
- const route = matched.generate(context.url);
108
- const result = await this.handleWeb(context, route);
109
- if (!result) {
110
- return null;
111
- }
112
- if (result.contentStream) {
113
- return result.contentStream;
114
- }
115
- return result.content.toString();
116
- }
117
- async createHTTPServer(handler) {
118
- return (0, import_http.createServer)(handler);
119
- }
120
- /* —————————————————————— function will be overwrite —————————————————————— */
121
- async onServerChange({ filepath }) {
122
- const { pwd } = this;
123
- const { api, server } = import_constants.AGGRED_DIR;
124
- const apiPath = import_path.default.normalize(import_path.default.join(pwd, api));
125
- const serverPath = import_path.default.normalize(import_path.default.join(pwd, server));
126
- const onlyApi = filepath.startsWith(apiPath);
127
- const onlyWeb = filepath.startsWith(serverPath);
128
- await this.prepareFrameHandler({
129
- onlyWeb,
130
- onlyApi
131
- });
132
- }
133
- // get routes info
134
- getRoutes() {
135
- if (this.presetRoutes) {
136
- return this.presetRoutes;
137
- }
138
- const file = import_path.default.join(this.distDir, import_utils.ROUTE_SPEC_FILE);
139
- if (import_utils.fs.existsSync(file)) {
140
- const content = import_utils.fs.readJSONSync(file);
141
- return content.routes;
142
- }
143
- return [];
144
- }
145
- // add promisify request handler to server
146
- // handler should do not do more things after invoke next
147
- addHandler(handler) {
148
- this.handlers.push(handler);
149
- }
150
- // return 404 page
151
- render404(context) {
152
- context.error(import_constants.ERROR_DIGEST.ENOTF, "404 Not Found");
153
- this.renderErrorPage(context, 404);
154
- }
155
- async prepareLoaderHandler(specs, distDir) {
156
- const { runner } = this;
157
- const handler = await runner.prepareLoaderHandler({
158
- serverRoutes: specs,
159
- distDir
160
- }, {
161
- onLast: () => null
162
- });
163
- this.loaderHandler = handler;
164
- }
165
- // gather frame extension and get framework handler
166
- async prepareFrameHandler(options) {
167
- const { workDir, runner } = this;
168
- const { onlyApi, onlyWeb } = options || {};
169
- const { getMiddlewares, ...collector } = (0, import_utils2.createMiddlewareCollecter)();
170
- await runner.gather(collector);
171
- const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
172
- const serverDir = import_path.default.join(workDir, SERVER_DIR);
173
- if (await import_utils.fs.pathExists(import_path.default.join(serverDir)) && !onlyApi) {
174
- const webExtension = (0, import_utils2.mergeExtension)(pluginWebExt);
175
- this.frameWebHandler = await this.prepareWebHandler(webExtension);
176
- }
177
- if (!onlyWeb) {
178
- const apiExtension = (0, import_utils2.mergeExtension)(pluginAPIExt);
179
- this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
180
- }
181
- }
182
- async prepareWebHandler(extension) {
183
- const { workDir, runner } = this;
184
- const handler = await runner.prepareWebServer({
185
- pwd: workDir,
186
- config: extension
187
- }, {
188
- onLast: () => null
189
- });
190
- return handler;
191
- }
192
- async prepareAPIHandler(extension) {
193
- const { workDir, runner, conf } = this;
194
- const { bff } = conf;
195
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
196
- const webOnly = await (0, import_utils.isWebOnly)();
197
- if (webOnly && process.env.NODE_ENV === "development") {
198
- return (req, res) => {
199
- res.setHeader("Content-Type", "text/plain");
200
- res.end(JSON.stringify(""));
201
- };
202
- }
203
- return runner.prepareApiServer({
204
- pwd: workDir,
205
- config: extension,
206
- prefix: Array.isArray(prefix) ? prefix[0] : prefix,
207
- httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
208
- render: this.render.bind(this)
209
- }, {
210
- onLast: () => null
211
- });
212
- }
213
- filterRoutes(routes) {
214
- return routes;
215
- }
216
- async setupBeforeProdMiddleware() {
217
- const { conf, runner } = this;
218
- const preMiddleware = await runner.beforeProdServer(conf);
219
- preMiddleware.flat().forEach((mid) => {
220
- this.addHandler(mid);
221
- });
222
- }
223
- setupStaticMiddleware(prefix) {
224
- const staticPathRegExp = (0, import_utils2.getStaticReg)(this.conf.output, this.conf.html, prefix);
225
- return (0, import_serveFile.createStaticFileHandler)([
226
- {
227
- path: staticPathRegExp,
228
- target: this.distDir
229
- }
230
- ], prefix);
231
- }
232
- async handleAPI(context) {
233
- const { req, res } = context;
234
- if (!this.frameAPIHandler) {
235
- throw new Error("can not found api handler");
236
- }
237
- await this.frameAPIHandler(req, res);
238
- }
239
- async handleWeb(context, route) {
240
- const { res } = context;
241
- if (this.loaderHandler) {
242
- await this.loaderHandler(context);
243
- if (this.isSend(res)) {
244
- return null;
245
- }
246
- }
247
- context.setParams(route.params);
248
- context.setServerData("router", {
249
- baseUrl: route.urlPath,
250
- params: route.params
251
- });
252
- if (route.responseHeaders) {
253
- Object.keys(route.responseHeaders).forEach((key) => {
254
- const value = route.responseHeaders[key];
255
- if (value) {
256
- context.res.setHeader(key, value);
257
- }
258
- });
259
- }
260
- const renderResult = await this.routeRenderHandler({
261
- ctx: context,
262
- route,
263
- runner: this.runner
264
- });
265
- if (!renderResult) {
266
- this.render404(context);
267
- return null;
268
- }
269
- if (renderResult.redirect) {
270
- this.redirect(res, renderResult.content, renderResult.statusCode);
271
- return null;
272
- }
273
- if (this.isSend(res)) {
274
- return null;
275
- }
276
- res.set("content-type", renderResult.contentType);
277
- return renderResult;
278
- }
279
- async proxy() {
280
- return null;
281
- }
282
- // warmup ssr function
283
- warmupSSRBundle() {
284
- const { distDir } = this;
285
- const bundles = this.router.getBundles();
286
- bundles.forEach((bundle) => {
287
- const filepath = import_path.default.join(distDir, bundle);
288
- if (import_utils.fs.existsSync(filepath)) {
289
- require(filepath);
290
- }
291
- });
292
- }
293
- createContext(req, res, options = {}) {
294
- return (0, import_context.createContext)(req, res, options);
295
- }
296
- /* —————————————————————— private function —————————————————————— */
297
- // handler route.json, include api / csr / ssr
298
- async routeHandler(context) {
299
- const { res, reporter } = context;
300
- const matched = this.router.match(context.path);
301
- if (!matched) {
302
- this.render404(context);
303
- return;
304
- }
305
- await reporter.init({
306
- match: matched
307
- });
308
- const end = (0, import_time.time)();
309
- res.on("finish", () => {
310
- const cost = end();
311
- reporter.reportTiming(import_constants.ServerReportTimings.SERVER_HANDLE_REQUEST, cost);
312
- });
313
- let route = matched.generate(context.url);
314
- if (route.isApi) {
315
- await this.handleAPI(context);
316
- return;
317
- }
318
- if (route.entryName && this.runMode === import_constants.RUN_MODE.FULL) {
319
- const afterMatchContext = (0, import_hook_api.createAfterMatchContext)(context, route.entryName);
320
- const end2 = (0, import_time.time)();
321
- await this.runner.afterMatch(afterMatchContext, {
322
- onLast: import_utils2.noop
323
- });
324
- const cost = end2();
325
- cost && reporter.reportTiming(import_constants.ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
326
- if (this.isSend(res)) {
327
- return;
328
- }
329
- const { current, url, status } = afterMatchContext.router;
330
- if (url) {
331
- this.redirect(res, url, status);
332
- return;
333
- }
334
- if (route.entryName !== current) {
335
- const matched2 = this.router.matchEntry(current);
336
- if (!matched2) {
337
- this.render404(context);
338
- return;
339
- }
340
- route = matched2.generate(context.url);
341
- }
342
- }
343
- if (this.frameWebHandler) {
344
- res.locals = res.locals || {};
345
- const middlewareContext = (0, import_hook_api.createMiddlewareContext)(context);
346
- const end2 = (0, import_time.time)();
347
- await this.frameWebHandler(middlewareContext);
348
- const cost = end2();
349
- cost && reporter.reportTiming(import_constants.ServerReportTimings.SERVER_MIDDLEWARE, cost);
350
- res.locals = {
351
- ...res.locals,
352
- ...middlewareContext.response.locals
353
- };
354
- if (this.isSend(res)) {
355
- return;
356
- }
357
- }
358
- const renderResult = await this.handleWeb(context, route);
359
- if (!renderResult) {
360
- return;
361
- }
362
- const { contentStream: responseStream, headers } = renderResult;
363
- if (headers) {
364
- for (const name in headers) {
365
- const value = headers[name];
366
- res.setHeader(name, value);
367
- }
368
- }
369
- let { content: response } = renderResult;
370
- if (route.entryName && responseStream) {
371
- responseStream.pipe(res);
372
- return;
373
- }
374
- if (route.entryName && this.runMode === import_constants.RUN_MODE.FULL) {
375
- const afterRenderContext = (0, import_hook_api.createAfterRenderContext)(context, route, response.toString());
376
- const end2 = (0, import_time.time)();
377
- await this.runner.afterRender(afterRenderContext, {
378
- onLast: import_utils2.noop
379
- });
380
- const cost = end2();
381
- cost && reporter.reportTiming(import_constants.ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost);
382
- if (this.isSend(res)) {
383
- return;
384
- }
385
- response = afterRenderContext.template.get();
386
- }
387
- res.end(response);
388
- }
389
- isSend(res) {
390
- if (res.modernFlushedHeaders) {
391
- if (res.writableFinished) {
392
- return true;
393
- }
394
- } else if (res.headersSent) {
395
- return true;
396
- }
397
- if (res.getHeader("Location") && (0, import_utils2.isRedirect)(res.statusCode)) {
398
- res.end();
399
- return true;
400
- }
401
- return false;
402
- }
403
- // compose handlers and create the final handler
404
- compose() {
405
- const { handlers } = this;
406
- if (!Array.isArray(handlers)) {
407
- throw new TypeError("Middleware stack must be an array!");
408
- }
409
- for (const fn of handlers) {
410
- if (typeof fn !== "function") {
411
- throw new TypeError("Middleware must be composed of functions!");
412
- }
413
- }
414
- this._handler = (context, next) => {
415
- let i = 0;
416
- const dispatch = (error) => {
417
- if (error) {
418
- return this.onError(context, error);
419
- }
420
- const handler = handlers[i++];
421
- if (!handler) {
422
- return next();
423
- }
424
- try {
425
- const result = handler(context, dispatch);
426
- if ((0, import_utils.isPromise)(result)) {
427
- return result.catch(onError);
428
- }
429
- } catch (e) {
430
- return onError(e);
431
- }
432
- };
433
- const onError = (err) => {
434
- this.onError(context, err);
435
- };
436
- return dispatch();
437
- };
438
- }
439
- requestHandler(req, res, next = () => {
440
- }) {
441
- res.statusCode = 200;
442
- req.logger = req.logger || this.logger;
443
- req.metrics = req.metrics || this.metrics;
444
- let context;
445
- try {
446
- context = this.createContext(req, res, {
447
- metaName: this.metaName
448
- });
449
- } catch (e) {
450
- this.logger.error(e);
451
- res.statusCode = 500;
452
- res.setHeader("content-type", import_utils.mime.contentType("html"));
453
- return res.end((0, import_utils2.createErrorDocument)(500, import_constants.ERROR_PAGE_TEXT[500]));
454
- }
455
- try {
456
- return this._handler(context, next);
457
- } catch (err) {
458
- return this.onError(context, err);
459
- }
460
- }
461
- redirect(res, url, status = 302) {
462
- res.set("Location", url);
463
- res.statusCode = status;
464
- res.end();
465
- }
466
- onError(context, err) {
467
- context.error(import_constants.ERROR_DIGEST.EINTER, err);
468
- this.renderErrorPage(context, 500);
469
- }
470
- async renderErrorPage(context, status) {
471
- const { res } = context;
472
- context.status = status;
473
- res.set("content-type", import_utils.mime.contentType("html"));
474
- const statusPage = `/${status}`;
475
- const customErrorPage = `/_error`;
476
- const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
477
- if (matched) {
478
- const route = matched.generate(context.url);
479
- const { entryName } = route;
480
- if (entryName === status.toString() || entryName === "_error") {
481
- try {
482
- const file = await this.routeRenderHandler({
483
- route,
484
- ctx: context,
485
- runner: this.runner
486
- });
487
- if (file) {
488
- context.res.end(file.content);
489
- return;
490
- }
491
- } catch (e) {
492
- }
493
- }
494
- }
495
- const text = import_constants.ERROR_PAGE_TEXT[status] || import_constants.ERROR_PAGE_TEXT[500];
496
- context.res.end((0, import_utils2.createErrorDocument)(status, text));
497
- }
498
- constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }) {
499
- this.handlers = [];
500
- this.loaderHandler = null;
501
- this.frameWebHandler = null;
502
- this.frameAPIHandler = null;
503
- require("ignore-styles");
504
- this.pwd = pwd;
505
- this.distDir = import_path.default.resolve(pwd, config.output.path || "dist");
506
- this.workDir = this.distDir;
507
- this.conf = config;
508
- (0, import_utils2.debug)("server conf", this.conf);
509
- this.logger = logger;
510
- this.metrics = metrics;
511
- this.router = new import_route.RouteMatchManager();
512
- this.presetRoutes = routes;
513
- this.proxyTarget = proxyTarget;
514
- this.staticGenerate = staticGenerate || false;
515
- this.runMode = runMode || import_constants.RUN_MODE.FULL;
516
- this.metaName = appContext === null || appContext === void 0 ? void 0 : appContext.metaName;
517
- }
518
- }
519
- // Annotate the CommonJS export names for ESM import in node:
520
- 0 && (module.exports = {
521
- ModernServer
522
- });
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var modernServerSplit_exports = {};
20
- __export(modernServerSplit_exports, {
21
- createProdServer: () => createProdServer
22
- });
23
- module.exports = __toCommonJS(modernServerSplit_exports);
24
- var import_modernServer = require("./modernServer");
25
- class ModernSSRServer extends import_modernServer.ModernServer {
26
- prepareAPIHandler(_) {
27
- return null;
28
- }
29
- async handleAPI(context) {
30
- return this.render404(context);
31
- }
32
- }
33
- class ModernAPIServer extends import_modernServer.ModernServer {
34
- prepareWebHandler(_) {
35
- return null;
36
- }
37
- filterRoutes(routes) {
38
- return routes.filter((route) => route.isApi);
39
- }
40
- }
41
- class ModernWebServer extends import_modernServer.ModernServer {
42
- async warmupSSRBundle() {
43
- return null;
44
- }
45
- async handleAPI(context) {
46
- return this.render404(context);
47
- }
48
- async handleWeb(context, route) {
49
- route.isSSR = false;
50
- return super.handleWeb(context, route);
51
- }
52
- }
53
- const createProdServer = (options) => {
54
- if (options.apiOnly) {
55
- return new ModernAPIServer(options);
56
- } else if (options.ssrOnly) {
57
- return new ModernSSRServer(options);
58
- } else if (options.webOnly) {
59
- return new ModernWebServer(options);
60
- } else {
61
- return new import_modernServer.ModernServer(options);
62
- }
63
- };
64
- // Annotate the CommonJS export names for ESM import in node:
65
- 0 && (module.exports = {
66
- createProdServer
67
- });