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