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