@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.4

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 (130) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +42 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +88 -54
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +144 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +377 -417
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +45 -30
  40. package/dist/js/node/index.js +31 -57
  41. package/dist/js/node/libs/context/context.js +79 -94
  42. package/dist/js/node/libs/context/index.js +23 -13
  43. package/dist/js/node/libs/hook-api/index.js +69 -48
  44. package/dist/js/node/libs/hook-api/route.js +22 -11
  45. package/dist/js/node/libs/hook-api/template.js +36 -39
  46. package/dist/js/node/libs/loadConfig.js +69 -35
  47. package/dist/js/node/libs/metrics.js +21 -9
  48. package/dist/js/node/libs/proxy.js +86 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +132 -70
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +268 -218
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +61 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +54 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +101 -119
  54. package/dist/js/node/libs/render/cache/index.js +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +50 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +29 -12
  57. package/dist/js/node/libs/render/cache/spr.js +156 -129
  58. package/dist/js/node/libs/render/cache/type.js +0 -5
  59. package/dist/js/node/libs/render/cache/util.js +88 -45
  60. package/dist/js/node/libs/render/index.js +102 -67
  61. package/dist/js/node/libs/render/measure.js +55 -31
  62. package/dist/js/node/libs/render/reader.js +87 -70
  63. package/dist/js/node/libs/render/ssr.js +76 -47
  64. package/dist/js/node/libs/render/static.js +75 -40
  65. package/dist/js/node/libs/render/type.js +27 -12
  66. package/dist/js/node/libs/route/index.js +26 -26
  67. package/dist/js/node/libs/route/matcher.js +36 -41
  68. package/dist/js/node/libs/route/route.js +25 -22
  69. package/dist/js/node/libs/serve-file.js +61 -32
  70. package/dist/js/node/server/index.js +160 -160
  71. package/dist/js/node/server/modern-server-split.js +68 -22
  72. package/dist/js/node/server/modern-server.js +395 -441
  73. package/dist/js/node/type.js +0 -3
  74. package/dist/js/node/utils.js +74 -52
  75. package/dist/js/node/worker-server.js +53 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +265 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +468 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1046 -909
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,33 +1,90 @@
1
- const _excluded = ["getMiddlewares"];
2
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
5
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
7
- /* eslint-disable max-lines */
8
- import { createServer } from 'http';
9
- import util from 'util';
10
- import path from 'path';
11
- import { fs, mime, ROUTE_SPEC_FILE } from '@modern-js/utils';
12
- import { RouteMatchManager } from "../libs/route";
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 util from "util";
51
+ import path from "path";
52
+ import { fs, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
53
+ import {
54
+ RouteMatchManager
55
+ } from "../libs/route";
13
56
  import { createRenderHandler } from "../libs/render";
14
- import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serve-file";
15
- import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExtension, noop, debug, isRedirect } from "../utils";
57
+ import {
58
+ createStaticFileHandler,
59
+ faviconFallbackHandler
60
+ } from "../libs/serve-file";
61
+ import {
62
+ createErrorDocument,
63
+ createMiddlewareCollecter,
64
+ getStaticReg,
65
+ mergeExtension,
66
+ noop,
67
+ debug,
68
+ isRedirect
69
+ } from "../utils";
16
70
  import * as reader from "../libs/render/reader";
17
71
  import { createProxyHandler } from "../libs/proxy";
18
72
  import { createContext } from "../libs/context";
19
73
  import { templateInjectableStream } from "../libs/hook-api/template";
20
- import { AGGRED_DIR, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE } from "../constants";
21
- import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "../libs/hook-api";
22
- const API_DIR = './api';
23
- const SERVER_DIR = './server';
24
- export class ModernServer {
25
- // appDirectory
26
-
27
- // product dist dir
28
-
29
- // work on src or dist
30
-
74
+ import {
75
+ AGGRED_DIR,
76
+ ERROR_DIGEST,
77
+ ERROR_PAGE_TEXT,
78
+ RUN_MODE
79
+ } from "../constants";
80
+ import {
81
+ createAfterMatchContext,
82
+ createAfterRenderContext,
83
+ createMiddlewareContext
84
+ } from "../libs/hook-api";
85
+ const API_DIR = "./api";
86
+ const SERVER_DIR = "./server";
87
+ class ModernServer {
31
88
  constructor({
32
89
  pwd,
33
90
  config,
@@ -38,33 +95,18 @@ export class ModernServer {
38
95
  runMode,
39
96
  proxyTarget
40
97
  }) {
41
- _defineProperty(this, "pwd", void 0);
42
- _defineProperty(this, "distDir", void 0);
43
- _defineProperty(this, "workDir", void 0);
44
- _defineProperty(this, "router", void 0);
45
- _defineProperty(this, "conf", void 0);
46
- _defineProperty(this, "handlers", []);
47
- _defineProperty(this, "presetRoutes", void 0);
48
- _defineProperty(this, "runner", void 0);
49
- _defineProperty(this, "logger", void 0);
50
- _defineProperty(this, "metrics", void 0);
51
- _defineProperty(this, "runMode", void 0);
52
- _defineProperty(this, "reader", reader);
53
- _defineProperty(this, "proxyTarget", void 0);
54
- _defineProperty(this, "staticFileHandler", void 0);
55
- _defineProperty(this, "routeRenderHandler", void 0);
56
- _defineProperty(this, "beforeRouteHandler", null);
57
- _defineProperty(this, "frameWebHandler", null);
58
- _defineProperty(this, "frameAPIHandler", null);
59
- _defineProperty(this, "proxyHandler", null);
60
- _defineProperty(this, "_handler", void 0);
61
- _defineProperty(this, "staticGenerate", void 0);
62
- require('ignore-styles');
98
+ this.handlers = [];
99
+ this.reader = reader;
100
+ this.beforeRouteHandler = null;
101
+ this.frameWebHandler = null;
102
+ this.frameAPIHandler = null;
103
+ this.proxyHandler = null;
104
+ require("ignore-styles");
63
105
  this.pwd = pwd;
64
- this.distDir = path.join(pwd, config.output.path || 'dist');
106
+ this.distDir = path.join(pwd, config.output.path || "dist");
65
107
  this.workDir = this.distDir;
66
108
  this.conf = config;
67
- debug('server conf', this.conf);
109
+ debug("server conf", this.conf);
68
110
  this.logger = logger;
69
111
  this.metrics = metrics;
70
112
  this.router = new RouteMatchManager();
@@ -72,133 +114,104 @@ export class ModernServer {
72
114
  this.proxyTarget = proxyTarget;
73
115
  this.staticGenerate = staticGenerate || false;
74
116
  this.runMode = runMode || RUN_MODE.FULL;
75
- // process.env.BUILD_TYPE = `${this.staticGenerate ? 'ssg' : 'ssr'}`;
76
117
  }
77
-
78
- // server prepare
79
- async onInit(runner, app) {
80
- var _conf$bff;
81
- this.runner = runner;
82
- const {
83
- distDir,
84
- staticGenerate,
85
- conf
86
- } = this;
87
- debug('final server conf', this.conf);
88
- // proxy handler, each proxy has own handler
89
- this.proxyHandler = createProxyHandler((_conf$bff = conf.bff) === null || _conf$bff === void 0 ? void 0 : _conf$bff.proxy);
90
- if (this.proxyHandler) {
91
- this.proxyHandler.forEach(handler => {
92
- this.addHandler(handler);
118
+ onInit(runner, app) {
119
+ return __async(this, null, function* () {
120
+ var _a;
121
+ this.runner = runner;
122
+ const { distDir, staticGenerate, conf } = this;
123
+ debug("final server conf", this.conf);
124
+ this.proxyHandler = createProxyHandler((_a = conf.bff) == null ? void 0 : _a.proxy);
125
+ if (this.proxyHandler) {
126
+ this.proxyHandler.forEach((handler) => {
127
+ this.addHandler(handler);
128
+ });
129
+ }
130
+ this.reader.init();
131
+ app.on("close", () => {
132
+ this.reader.close();
93
133
  });
94
- }
95
-
96
- // start file reader
97
- this.reader.init();
98
- app.on('close', () => {
99
- this.reader.close();
100
- });
101
-
102
- // use preset routes priority
103
- const usageRoutes = this.filterRoutes(this.getRoutes());
104
- this.router.reset(usageRoutes);
105
-
106
- // warmup ssr bundle in production env
107
- this.warmupSSRBundle();
108
- await this.prepareFrameHandler();
109
- await this.prepareBeforeRouteHandler(usageRoutes, distDir);
110
-
111
- // Only work when without setting `assetPrefix`.
112
- // Setting `assetPrefix` means these resources should be uploaded to CDN.
113
- const staticPathRegExp = getStaticReg(this.conf.output || {}, this.conf.html);
114
- this.staticFileHandler = createStaticFileHandler([{
115
- path: staticPathRegExp,
116
- target: distDir
117
- }], this.conf.output);
118
- this.routeRenderHandler = createRenderHandler({
119
- distDir,
120
- staticGenerate
134
+ const usageRoutes = this.filterRoutes(this.getRoutes());
135
+ this.router.reset(usageRoutes);
136
+ this.warmupSSRBundle();
137
+ yield this.prepareFrameHandler();
138
+ yield this.prepareBeforeRouteHandler(usageRoutes, distDir);
139
+ const staticPathRegExp = getStaticReg(
140
+ this.conf.output || {},
141
+ this.conf.html
142
+ );
143
+ this.staticFileHandler = createStaticFileHandler(
144
+ [
145
+ {
146
+ path: staticPathRegExp,
147
+ target: distDir
148
+ }
149
+ ],
150
+ this.conf.output
151
+ );
152
+ this.routeRenderHandler = createRenderHandler({
153
+ distDir,
154
+ staticGenerate
155
+ });
156
+ yield this.setupBeforeProdMiddleware();
157
+ this.addHandler(this.staticFileHandler);
158
+ this.addHandler(faviconFallbackHandler);
159
+ this.addBeforeRouteHandler();
160
+ this.addHandler(this.routeHandler.bind(this));
161
+ this.compose();
121
162
  });
122
- await this.setupBeforeProdMiddleware();
123
- this.addHandler(this.staticFileHandler);
124
-
125
- // execute after staticFileHandler, can rename to staticFallbackHandler if needed.
126
- this.addHandler(faviconFallbackHandler);
127
- this.addBeforeRouteHandler();
128
- this.addHandler(this.routeHandler.bind(this));
129
-
130
- // compose middlewares to http handler
131
- this.compose();
132
163
  }
133
-
134
- // server ready
135
164
  onRepack(_) {
136
- // empty
137
165
  }
138
166
  addBeforeRouteHandler() {
139
- this.addHandler(async (context, next) => {
167
+ this.addHandler((context, next) => __async(this, null, function* () {
140
168
  if (this.beforeRouteHandler) {
141
- await this.beforeRouteHandler(context);
169
+ yield this.beforeRouteHandler(context);
142
170
  if (this.isSend(context.res)) {
143
171
  return;
144
172
  }
145
173
  }
146
- // eslint-disable-next-line consistent-return
147
174
  return next();
148
- });
175
+ }));
149
176
  }
150
- onServerChange({
151
- filepath
152
- }) {
153
- const {
154
- pwd
155
- } = this;
156
- const {
157
- api,
158
- server
159
- } = AGGRED_DIR;
177
+ onServerChange({ filepath }) {
178
+ const { pwd } = this;
179
+ const { api, server } = AGGRED_DIR;
160
180
  const apiPath = path.normalize(path.join(pwd, api));
161
181
  const serverPath = path.normalize(path.join(pwd, server));
162
182
  const onlyApi = filepath.startsWith(apiPath);
163
183
  const onlyWeb = filepath.startsWith(serverPath);
164
- this.prepareFrameHandler({
165
- onlyWeb,
166
- onlyApi
167
- });
184
+ this.prepareFrameHandler({ onlyWeb, onlyApi });
168
185
  }
169
-
170
- // exposed requestHandler
171
186
  getRequestHandler() {
172
187
  return this.requestHandler.bind(this);
173
188
  }
174
- async render(req, res, url) {
175
- req.logger = this.logger;
176
- req.metrics = this.metrics;
177
- const context = createContext(req, res);
178
- const matched = this.router.match(url || context.path);
179
- if (!matched) {
180
- return null;
181
- }
182
- const route = matched.generate(context.url);
183
- const result = await this.handleWeb(context, route);
184
- if (!result) {
185
- return null;
186
- }
187
- return result.content.toString();
189
+ render(req, res, url) {
190
+ return __async(this, null, function* () {
191
+ req.logger = this.logger;
192
+ req.metrics = this.metrics;
193
+ const context = createContext(req, res);
194
+ const matched = this.router.match(url || context.path);
195
+ if (!matched) {
196
+ return null;
197
+ }
198
+ const route = matched.generate(context.url);
199
+ const result = yield this.handleWeb(context, route);
200
+ if (!result) {
201
+ return null;
202
+ }
203
+ return result.content.toString();
204
+ });
188
205
  }
189
- async createHTTPServer(handler) {
190
- return createServer(handler);
206
+ createHTTPServer(handler) {
207
+ return __async(this, null, function* () {
208
+ return createServer(handler);
209
+ });
191
210
  }
192
-
193
- /* —————————————————————— function will be overwrite —————————————————————— */
194
- // get routes info
195
211
  getRoutes() {
196
- // Preferred to use preset routes
197
212
  if (this.presetRoutes) {
198
213
  return this.presetRoutes;
199
214
  }
200
-
201
- // read routes from spec file
202
215
  const file = path.join(this.distDir, ROUTE_SPEC_FILE);
203
216
  if (fs.existsSync(file)) {
204
217
  const content = fs.readJSONSync(file);
@@ -206,301 +219,253 @@ export class ModernServer {
206
219
  }
207
220
  return [];
208
221
  }
209
-
210
- // add promisify request handler to server
211
- // handler should do not do more things after invoke next
212
222
  addHandler(handler) {
213
- if (handler[Symbol.toStringTag] === 'AsyncFunction') {
223
+ if (handler[Symbol.toStringTag] === "AsyncFunction") {
214
224
  this.handlers.push(handler);
215
225
  } else {
216
226
  this.handlers.push(util.promisify(handler));
217
227
  }
218
228
  }
219
-
220
- // return 404 page
221
229
  render404(context) {
222
- context.error(ERROR_DIGEST.ENOTF, '404 Not Found');
230
+ context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
223
231
  this.renderErrorPage(context, 404);
224
232
  }
225
- async prepareBeforeRouteHandler(specs, distDir) {
226
- const {
227
- runner
228
- } = this;
229
- const handler = await runner.preparebeforeRouteHandler({
230
- serverRoutes: specs,
231
- distDir
232
- }, {
233
- onLast: () => null
233
+ prepareBeforeRouteHandler(specs, distDir) {
234
+ return __async(this, null, function* () {
235
+ const { runner } = this;
236
+ const handler = yield runner.preparebeforeRouteHandler(
237
+ {
238
+ serverRoutes: specs,
239
+ distDir
240
+ },
241
+ {
242
+ onLast: () => null
243
+ }
244
+ );
245
+ this.beforeRouteHandler = handler;
234
246
  });
235
- this.beforeRouteHandler = handler;
236
247
  }
237
-
238
- // gather frame extension and get framework handler
239
- async prepareFrameHandler(options) {
240
- const {
241
- workDir,
242
- runner
243
- } = this;
244
- const {
245
- onlyApi,
246
- onlyWeb
247
- } = options || {};
248
-
249
- // server hook, gather plugin inject
250
- const _createMiddlewareColl = createMiddlewareCollecter(),
251
- {
252
- getMiddlewares
253
- } = _createMiddlewareColl,
254
- collector = _objectWithoutProperties(_createMiddlewareColl, _excluded);
255
- await runner.gather(collector);
256
- const {
257
- api: pluginAPIExt,
258
- web: pluginWebExt
259
- } = getMiddlewares();
260
- const apiDir = path.join(workDir, API_DIR);
261
- const serverDir = path.join(workDir, SERVER_DIR);
262
-
263
- // get api or web server handler from server-framework plugin
264
- if ((await fs.pathExists(path.join(serverDir))) && !onlyApi) {
265
- const webExtension = mergeExtension(pluginWebExt);
266
- this.frameWebHandler = await this.prepareWebHandler(webExtension);
267
- }
268
- if (fs.existsSync(apiDir) && !onlyWeb) {
269
- const apiExtension = mergeExtension(pluginAPIExt);
270
- this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
271
- }
248
+ prepareFrameHandler(options) {
249
+ return __async(this, null, function* () {
250
+ const { workDir, runner } = this;
251
+ const { onlyApi, onlyWeb } = options || {};
252
+ const _a = createMiddlewareCollecter(), { getMiddlewares } = _a, collector = __objRest(_a, ["getMiddlewares"]);
253
+ yield runner.gather(collector);
254
+ const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
255
+ const apiDir = path.join(workDir, API_DIR);
256
+ const serverDir = path.join(workDir, SERVER_DIR);
257
+ if ((yield fs.pathExists(path.join(serverDir))) && !onlyApi) {
258
+ const webExtension = mergeExtension(pluginWebExt);
259
+ this.frameWebHandler = yield this.prepareWebHandler(webExtension);
260
+ }
261
+ if (fs.existsSync(apiDir) && !onlyWeb) {
262
+ const apiExtension = mergeExtension(pluginAPIExt);
263
+ this.frameAPIHandler = yield this.prepareAPIHandler(apiExtension);
264
+ }
265
+ });
272
266
  }
273
- async prepareWebHandler(extension) {
274
- const {
275
- workDir,
276
- runner
277
- } = this;
278
- const handler = await runner.prepareWebServer({
279
- pwd: workDir,
280
- config: extension
281
- }, {
282
- onLast: () => null
267
+ prepareWebHandler(extension) {
268
+ return __async(this, null, function* () {
269
+ const { workDir, runner } = this;
270
+ const handler = yield runner.prepareWebServer(
271
+ {
272
+ pwd: workDir,
273
+ config: extension
274
+ },
275
+ { onLast: () => null }
276
+ );
277
+ return handler;
283
278
  });
284
- return handler;
285
279
  }
286
- async prepareAPIHandler(extension) {
287
- const {
288
- workDir,
289
- runner,
290
- conf
291
- } = this;
292
- const {
293
- bff
294
- } = conf;
295
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
296
- return runner.prepareApiServer({
297
- pwd: workDir,
298
- config: extension,
299
- prefix: Array.isArray(prefix) ? prefix[0] : prefix
300
- }, {
301
- onLast: () => null
280
+ prepareAPIHandler(extension) {
281
+ return __async(this, null, function* () {
282
+ const { workDir, runner, conf } = this;
283
+ const { bff } = conf;
284
+ const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
285
+ return runner.prepareApiServer(
286
+ {
287
+ pwd: workDir,
288
+ config: extension,
289
+ prefix: Array.isArray(prefix) ? prefix[0] : prefix
290
+ },
291
+ { onLast: () => null }
292
+ );
302
293
  });
303
294
  }
304
295
  filterRoutes(routes) {
305
296
  return routes;
306
297
  }
307
- async setupBeforeProdMiddleware() {
308
- const {
309
- conf,
310
- runner
311
- } = this;
312
- const preMiddleware = await runner.beforeProdServer(conf);
313
- preMiddleware.flat().forEach(mid => {
314
- this.addHandler(mid);
298
+ setupBeforeProdMiddleware() {
299
+ return __async(this, null, function* () {
300
+ const { conf, runner } = this;
301
+ const preMiddleware = yield runner.beforeProdServer(conf);
302
+ preMiddleware.flat().forEach((mid) => {
303
+ this.addHandler(mid);
304
+ });
315
305
  });
316
306
  }
317
- async handleAPI(context) {
318
- const {
319
- req,
320
- res
321
- } = context;
322
- if (!this.frameAPIHandler) {
323
- throw new Error('can not found api handler');
324
- }
325
- await this.frameAPIHandler(req, res);
307
+ handleAPI(context) {
308
+ return __async(this, null, function* () {
309
+ const { req, res } = context;
310
+ if (!this.frameAPIHandler) {
311
+ throw new Error("can not found api handler");
312
+ }
313
+ yield this.frameAPIHandler(req, res);
314
+ });
326
315
  }
327
- async handleWeb(context, route) {
328
- return this.routeRenderHandler({
329
- ctx: context,
330
- route,
331
- runner: this.runner
316
+ handleWeb(context, route) {
317
+ return __async(this, null, function* () {
318
+ return this.routeRenderHandler({
319
+ ctx: context,
320
+ route,
321
+ runner: this.runner
322
+ });
332
323
  });
333
324
  }
334
- async proxy() {
335
- return null;
325
+ proxy() {
326
+ return __async(this, null, function* () {
327
+ return null;
328
+ });
336
329
  }
337
-
338
- // warmup ssr function
339
330
  warmupSSRBundle() {
340
- const {
341
- distDir
342
- } = this;
331
+ const { distDir } = this;
343
332
  const bundles = this.router.getBundles();
344
- bundles.forEach(bundle => {
333
+ bundles.forEach((bundle) => {
345
334
  const filepath = path.join(distDir, bundle);
346
- // if error, just throw and let process die
347
335
  require(filepath);
348
336
  });
349
337
  }
350
338
  createContext(req, res, options = {}) {
351
339
  return createContext(req, res, options);
352
340
  }
353
-
354
- /* —————————————————————— private function —————————————————————— */
355
- // handler route.json, include api / csr / ssr
356
- async routeHandler(context) {
357
- const {
358
- res
359
- } = context;
360
-
361
- // match routes in the route spec
362
- const matched = this.router.match(context.path);
363
- if (!matched) {
364
- this.render404(context);
365
- return;
366
- }
367
-
368
- // route is api service
369
- let route = matched.generate(context.url);
370
- if (route.isApi) {
371
- await this.handleAPI(context);
372
- return;
373
- }
374
- const afterMatchContext = createAfterMatchContext(context, route.entryName);
375
-
376
- // only full mode run server hook
377
- if (this.runMode === RUN_MODE.FULL) {
378
- await this.runner.afterMatch(afterMatchContext, {
379
- onLast: noop
380
- });
381
- }
382
- if (this.isSend(res)) {
383
- return;
384
- }
385
- const {
386
- current,
387
- url,
388
- status
389
- } = afterMatchContext.router;
390
- // redirect to another url
391
- if (url) {
392
- this.redirect(res, url, status);
393
- return;
394
- }
395
-
396
- // rewrite to another entry
397
- if (route.entryName !== current) {
398
- const matched = this.router.matchEntry(current);
341
+ routeHandler(context) {
342
+ return __async(this, null, function* () {
343
+ const { res } = context;
344
+ const matched = this.router.match(context.path);
399
345
  if (!matched) {
400
346
  this.render404(context);
401
347
  return;
402
348
  }
403
- route = matched.generate(context.url);
404
- }
405
- context.setParams(route.params);
406
- context.setServerData('router', {
407
- baseUrl: route.urlPath,
408
- params: route.params
409
- });
410
- if (this.frameWebHandler) {
411
- res.locals = res.locals || {};
412
- const middlewareContext = createMiddlewareContext(context);
413
- await this.frameWebHandler(middlewareContext);
414
- res.locals = _objectSpread(_objectSpread({}, res.locals), middlewareContext.response.locals);
415
- }
416
-
417
- // frameWebHandler has process request
418
- if (this.isSend(res)) {
419
- return;
420
- }
421
- if (route.responseHeaders) {
422
- Object.keys(route.responseHeaders).forEach(key => {
423
- const value = route.responseHeaders[key];
424
- if (value) {
425
- context.res.setHeader(key, value);
349
+ let route = matched.generate(context.url);
350
+ if (route.isApi) {
351
+ yield this.handleAPI(context);
352
+ return;
353
+ }
354
+ const afterMatchContext = createAfterMatchContext(context, route.entryName);
355
+ if (this.runMode === RUN_MODE.FULL) {
356
+ yield this.runner.afterMatch(afterMatchContext, { onLast: noop });
357
+ }
358
+ if (this.isSend(res)) {
359
+ return;
360
+ }
361
+ const { current, url, status } = afterMatchContext.router;
362
+ if (url) {
363
+ this.redirect(res, url, status);
364
+ return;
365
+ }
366
+ if (route.entryName !== current) {
367
+ const matched2 = this.router.matchEntry(current);
368
+ if (!matched2) {
369
+ this.render404(context);
370
+ return;
426
371
  }
372
+ route = matched2.generate(context.url);
373
+ }
374
+ context.setParams(route.params);
375
+ context.setServerData("router", {
376
+ baseUrl: route.urlPath,
377
+ params: route.params
427
378
  });
428
- }
429
- const renderResult = await this.handleWeb(context, route);
430
- if (!renderResult) {
431
- this.render404(context);
432
- return;
433
- }
434
-
435
- // React Router navigation
436
- if (renderResult.redirect) {
437
- this.redirect(res, renderResult.content, renderResult.statusCode);
438
- return;
439
- }
440
- if (this.isSend(res)) {
441
- return;
442
- }
443
- res.setHeader('content-type', renderResult.contentType);
444
- const {
445
- contentStream
446
- } = renderResult;
447
- if (contentStream) {
448
- contentStream.pipe(templateInjectableStream({
449
- prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>` : undefined
450
- })).pipe(res);
451
- return;
452
- }
453
- let response = renderResult.content;
454
- if (route.entryName) {
455
- const afterRenderContext = createAfterRenderContext(context, response.toString());
456
-
457
- // only full mode run server hook
458
- // FIXME: how to run server hook in streaming
459
- if (this.runMode === RUN_MODE.FULL) {
460
- await this.runner.afterRender(afterRenderContext, {
461
- onLast: noop
379
+ if (this.frameWebHandler) {
380
+ res.locals = res.locals || {};
381
+ const middlewareContext = createMiddlewareContext(context);
382
+ yield this.frameWebHandler(middlewareContext);
383
+ res.locals = __spreadValues(__spreadValues({}, res.locals), middlewareContext.response.locals);
384
+ }
385
+ if (this.isSend(res)) {
386
+ return;
387
+ }
388
+ if (route.responseHeaders) {
389
+ Object.keys(route.responseHeaders).forEach((key) => {
390
+ const value = route.responseHeaders[key];
391
+ if (value) {
392
+ context.res.setHeader(key, value);
393
+ }
462
394
  });
463
395
  }
396
+ const renderResult = yield this.handleWeb(context, route);
397
+ if (!renderResult) {
398
+ this.render404(context);
399
+ return;
400
+ }
401
+ if (renderResult.redirect) {
402
+ this.redirect(
403
+ res,
404
+ renderResult.content,
405
+ renderResult.statusCode
406
+ );
407
+ return;
408
+ }
464
409
  if (this.isSend(res)) {
465
410
  return;
466
411
  }
467
-
468
- // It will inject _SERVER_DATA twice, when SSG mode.
469
- // The first time was in ssg html created, the seoncd time was in prod-server start.
470
- // but the second wound causes route error.
471
- // To ensure that the second injection fails, the _SERVER_DATA inject at the front of head,
472
- afterRenderContext.template.prependHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
473
- response = afterRenderContext.template.get();
474
- }
475
- res.end(response);
412
+ res.setHeader("content-type", renderResult.contentType);
413
+ const { contentStream } = renderResult;
414
+ if (contentStream) {
415
+ contentStream.pipe(
416
+ templateInjectableStream({
417
+ prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
418
+ context.serverData
419
+ )}<\/script>` : void 0
420
+ })
421
+ ).pipe(res);
422
+ return;
423
+ }
424
+ let response = renderResult.content;
425
+ if (route.entryName) {
426
+ const afterRenderContext = createAfterRenderContext(
427
+ context,
428
+ response.toString()
429
+ );
430
+ if (this.runMode === RUN_MODE.FULL) {
431
+ yield this.runner.afterRender(afterRenderContext, { onLast: noop });
432
+ }
433
+ if (this.isSend(res)) {
434
+ return;
435
+ }
436
+ afterRenderContext.template.prependHead(
437
+ `<script>window._SERVER_DATA=${JSON.stringify(
438
+ context.serverData
439
+ )}<\/script>`
440
+ );
441
+ response = afterRenderContext.template.get();
442
+ }
443
+ res.end(response);
444
+ });
476
445
  }
477
446
  isSend(res) {
478
447
  if (res.headersSent) {
479
448
  return true;
480
449
  }
481
- if (res.getHeader('Location') && isRedirect(res.statusCode)) {
450
+ if (res.getHeader("Location") && isRedirect(res.statusCode)) {
482
451
  res.end();
483
452
  return true;
484
453
  }
485
454
  return false;
486
455
  }
487
-
488
- // compose handlers and create the final handler
489
456
  compose() {
490
- const {
491
- handlers
492
- } = this;
457
+ const { handlers } = this;
493
458
  if (!Array.isArray(handlers)) {
494
- throw new TypeError('Middleware stack must be an array!');
459
+ throw new TypeError("Middleware stack must be an array!");
495
460
  }
496
461
  for (const fn of handlers) {
497
- if (typeof fn !== 'function') {
498
- throw new TypeError('Middleware must be composed of functions!');
462
+ if (typeof fn !== "function") {
463
+ throw new TypeError("Middleware must be composed of functions!");
499
464
  }
500
465
  }
501
466
  this._handler = (context, next) => {
502
467
  let i = 0;
503
- const dispatch = error => {
468
+ const dispatch = (error) => {
504
469
  if (error) {
505
470
  return this.onError(context, error);
506
471
  }
@@ -510,14 +475,13 @@ export class ModernServer {
510
475
  }
511
476
  return handler(context, dispatch).catch(onError);
512
477
  };
513
- const onError = err => {
478
+ const onError = (err) => {
514
479
  this.onError(context, err);
515
480
  };
516
481
  return dispatch();
517
482
  };
518
483
  }
519
484
  requestHandler(req, res, next = () => {
520
- // empty
521
485
  }) {
522
486
  res.statusCode = 200;
523
487
  req.logger = this.logger;
@@ -528,7 +492,7 @@ export class ModernServer {
528
492
  } catch (e) {
529
493
  this.logger.error(e);
530
494
  res.statusCode = 500;
531
- res.setHeader('content-type', mime.contentType('html'));
495
+ res.setHeader("content-type", mime.contentType("html"));
532
496
  return res.end(createErrorDocument(500, ERROR_PAGE_TEXT[500]));
533
497
  }
534
498
  try {
@@ -538,7 +502,7 @@ export class ModernServer {
538
502
  }
539
503
  }
540
504
  redirect(res, url, status = 302) {
541
- res.setHeader('Location', url);
505
+ res.setHeader("Location", url);
542
506
  res.statusCode = status;
543
507
  res.end();
544
508
  }
@@ -546,41 +510,37 @@ export class ModernServer {
546
510
  context.error(ERROR_DIGEST.EINTER, err);
547
511
  this.renderErrorPage(context, 500);
548
512
  }
549
- async renderErrorPage(context, status) {
550
- const {
551
- res
552
- } = context;
553
- context.status = status;
554
- res.setHeader('content-type', mime.contentType('html'));
555
- const statusPage = `/${status}`;
556
- const customErrorPage = `/_error`;
557
- const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
558
- // if no custom status page find
559
-
560
- if (matched) {
561
- const route = matched.generate(context.url);
562
- const {
563
- entryName
564
- } = route;
565
- // check entryName, avoid matched '/' route
566
- if (entryName === status.toString() || entryName === '_error') {
567
- try {
568
- const file = await this.routeRenderHandler({
569
- route,
570
- ctx: context,
571
- runner: this.runner
572
- });
573
- if (file) {
574
- context.res.end(file.content);
575
- return;
513
+ renderErrorPage(context, status) {
514
+ return __async(this, null, function* () {
515
+ const { res } = context;
516
+ context.status = status;
517
+ res.setHeader("content-type", mime.contentType("html"));
518
+ const statusPage = `/${status}`;
519
+ const customErrorPage = `/_error`;
520
+ const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
521
+ if (matched) {
522
+ const route = matched.generate(context.url);
523
+ const { entryName } = route;
524
+ if (entryName === status.toString() || entryName === "_error") {
525
+ try {
526
+ const file = yield this.routeRenderHandler({
527
+ route,
528
+ ctx: context,
529
+ runner: this.runner
530
+ });
531
+ if (file) {
532
+ context.res.end(file.content);
533
+ return;
534
+ }
535
+ } catch (e) {
576
536
  }
577
- } catch (e) {
578
- // just catch error when the rendering error occurred in the custom error page.
579
537
  }
580
538
  }
581
- }
582
- const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT[500];
583
- context.res.end(createErrorDocument(status, text));
539
+ const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT[500];
540
+ context.res.end(createErrorDocument(status, text));
541
+ });
584
542
  }
585
543
  }
586
- /* eslint-enable max-lines */
544
+ export {
545
+ ModernServer
546
+ };