@modern-js/prod-server 2.5.1-alpha.3 → 2.6.1-alpha.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 (55) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/index.js +2 -2
  3. package/dist/cjs/libs/context/context.js +13 -0
  4. package/dist/cjs/libs/hook-api/index.js +4 -0
  5. package/dist/cjs/libs/loadConfig.js +5 -0
  6. package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +4 -0
  7. package/dist/cjs/libs/render/cache/page-caches/lru.js +4 -0
  8. package/dist/cjs/libs/render/cache/spr.js +5 -0
  9. package/dist/cjs/libs/render/cache/util.js +4 -0
  10. package/dist/cjs/libs/render/index.js +4 -0
  11. package/dist/cjs/libs/render/reader.js +9 -0
  12. package/dist/cjs/libs/render/ssr.js +16 -4
  13. package/dist/cjs/libs/render/static.js +4 -0
  14. package/dist/cjs/libs/route/index.js +4 -0
  15. package/dist/cjs/libs/route/matcher.js +4 -0
  16. package/dist/cjs/libs/{serve-file.js → serveFile.js} +29 -22
  17. package/dist/cjs/server/index.js +32 -2
  18. package/dist/cjs/server/{modern-server.js → modernServer.js} +26 -11
  19. package/dist/cjs/server/{modern-server-split.js → modernServerSplit.js} +8 -8
  20. package/dist/cjs/{worker-server.js → workerServer.js} +3 -3
  21. package/dist/esm/index.js +1 -1
  22. package/dist/esm/libs/context/context.js +15 -6
  23. package/dist/esm/libs/render/cache/spr.js +1 -0
  24. package/dist/esm/libs/render/ssr.js +5 -5
  25. package/dist/esm/libs/route/index.js +4 -0
  26. package/dist/esm/libs/route/matcher.js +4 -0
  27. package/dist/esm/libs/{serve-file.js → serveFile.js} +2 -1
  28. package/dist/esm/server/index.js +29 -4
  29. package/dist/esm/server/{modern-server.js → modernServer.js} +20 -8
  30. package/dist/esm/server/{modern-server-split.js → modernServerSplit.js} +1 -1
  31. package/dist/esm-node/index.js +1 -1
  32. package/dist/esm-node/libs/context/context.js +9 -0
  33. package/dist/esm-node/libs/loadConfig.js +1 -0
  34. package/dist/esm-node/libs/render/cache/spr.js +1 -0
  35. package/dist/esm-node/libs/render/reader.js +5 -0
  36. package/dist/esm-node/libs/render/ssr.js +12 -4
  37. package/dist/esm-node/libs/route/index.js +4 -0
  38. package/dist/esm-node/libs/route/matcher.js +4 -0
  39. package/dist/esm-node/libs/serveFile.js +50 -0
  40. package/dist/esm-node/server/index.js +27 -1
  41. package/dist/esm-node/server/{modern-server.js → modernServer.js} +17 -6
  42. package/dist/esm-node/server/{modern-server-split.js → modernServerSplit.js} +1 -1
  43. package/dist/types/index.d.ts +1 -1
  44. package/dist/types/libs/context/context.d.ts +1 -1
  45. package/dist/types/libs/render/ssr.d.ts +1 -0
  46. package/dist/types/type.d.ts +1 -1
  47. package/dist/types/utils.d.ts +1 -1
  48. package/package.json +12 -12
  49. package/dist/esm-node/libs/serve-file.js +0 -47
  50. /package/dist/esm/{worker-server.js → workerServer.js} +0 -0
  51. /package/dist/esm-node/{worker-server.js → workerServer.js} +0 -0
  52. /package/dist/types/libs/{serve-file.d.ts → serveFile.d.ts} +0 -0
  53. /package/dist/types/server/{modern-server.d.ts → modernServer.d.ts} +0 -0
  54. /package/dist/types/server/{modern-server-split.d.ts → modernServerSplit.d.ts} +0 -0
  55. /package/dist/types/{worker-server.d.ts → workerServer.d.ts} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @modern-js/prod-server
2
2
 
3
+ ## 2.6.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 36164a2: fix: env.PORT should be respected when using listenOptions
8
+ fix: 当使用 listenOptions 时,env.PORT 应该被支持
9
+ - 49fa0b1: fix: remove header info from SSR ctx to avoid security issues, reserved a switch
10
+ fix: 移除 SSR 上下文中的 header 信息,避免造成安全问题,预留一个字段开启
11
+ - Updated dependencies [e1f799e]
12
+ - Updated dependencies [7915ab3]
13
+ - Updated dependencies [0fe658a]
14
+ - @modern-js/utils@2.6.0
15
+ - @modern-js/server-core@2.6.0
16
+
3
17
  ## 2.5.0
4
18
 
5
19
  ### Patch Changes
package/dist/cjs/index.js CHANGED
@@ -18,14 +18,14 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var src_exports = {};
20
20
  __export(src_exports, {
21
- ModernServer: () => import_modern_server.ModernServer,
21
+ ModernServer: () => import_modernServer.ModernServer,
22
22
  Server: () => import_server.Server,
23
23
  createProxyHandler: () => import_proxy.createProxyHandler,
24
24
  default: () => src_default
25
25
  });
26
26
  module.exports = __toCommonJS(src_exports);
27
27
  var import_server = require("./server");
28
- var import_modern_server = require("./server/modern-server");
28
+ var import_modernServer = require("./server/modernServer");
29
29
  var import_proxy = require("./libs/proxy");
30
30
  __reExport(src_exports, require("./type"), module.exports);
31
31
  __reExport(src_exports, require("./constants"), module.exports);
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -34,6 +38,9 @@ var import_fresh = __toESM(require("fresh"));
34
38
  var import_utils = require("../../utils");
35
39
  class ModernServerContext {
36
40
  constructor(req, res, options) {
41
+ /**
42
+ * url params
43
+ */
37
44
  this.params = {};
38
45
  this.options = {};
39
46
  this.req = req;
@@ -56,6 +63,7 @@ class ModernServerContext {
56
63
  this.send(body);
57
64
  };
58
65
  }
66
+ // compat express res.send, only support etag now
59
67
  send(body) {
60
68
  try {
61
69
  const generateETag = !this.res.getHeader("ETag") && this.options.etag;
@@ -105,6 +113,7 @@ class ModernServerContext {
105
113
  }
106
114
  return false;
107
115
  }
116
+ /* request property */
108
117
  get headers() {
109
118
  return this.req.headers;
110
119
  }
@@ -165,12 +174,16 @@ class ModernServerContext {
165
174
  const str = this.querystring;
166
175
  return import_querystring.default.parse(str);
167
176
  }
177
+ /* response property */
168
178
  get status() {
169
179
  return this.res.statusCode;
170
180
  }
171
181
  set status(statusCode) {
172
182
  this.res.statusCode = statusCode;
173
183
  }
184
+ /**
185
+ * 判断链接是否已经关闭
186
+ */
174
187
  resHasHandled() {
175
188
  return this.res.writableEnded;
176
189
  }
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -53,6 +57,7 @@ const loadConfig = ({
53
57
  {
54
58
  ...resolvedConfig,
55
59
  plugins: []
60
+ // filter cli plugins
56
61
  },
57
62
  serverConfig,
58
63
  cliConfig
@@ -13,6 +13,10 @@ var __copyProps = (to, from, except, desc) => {
13
13
  return to;
14
14
  };
15
15
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
16
20
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
17
21
  mod
18
22
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -75,6 +79,7 @@ class CacheManager {
75
79
  this.cacheOptions = cacheOptions;
76
80
  this.cache = new import_lru_cache.default({
77
81
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
82
+ // 默认存 100M,最大 600M
78
83
  length(n) {
79
84
  const len = n.caches.keys().reduce((total, cur) => {
80
85
  var _a;
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -44,11 +48,13 @@ const createCacheItem = async (filepath, mtime) => {
44
48
  };
45
49
  };
46
50
  class LruReader {
51
+ // private timer?: NodeJS.Timeout;
47
52
  constructor() {
48
53
  this.cache = new import_lru_cache.default({
49
54
  max: 256 * MB,
50
55
  length: getContentLength,
51
56
  maxAge: 5 * 60 * 5e3
57
+ // 60s
52
58
  });
53
59
  }
54
60
  init() {
@@ -93,6 +99,9 @@ class LruReader {
93
99
  }
94
100
  }
95
101
  }
102
+ // private timeTask() {
103
+ // this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
104
+ // }
96
105
  }
97
106
  const reader = new LruReader();
98
107
  const readFile = async (filepath) => {
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -28,12 +32,19 @@ __export(ssr_exports, {
28
32
  module.exports = __toCommonJS(ssr_exports);
29
33
  var import_path = __toESM(require("path"));
30
34
  var import_utils = require("@modern-js/utils");
31
- var import_cookie = __toESM(require("cookie"));
32
35
  var import_cache = __toESM(require("./cache"));
33
36
  var import_measure = require("./measure");
34
37
  const render = async (ctx, renderOptions, runner) => {
35
38
  var _a;
36
- const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
39
+ const {
40
+ urlPath,
41
+ bundle,
42
+ distDir,
43
+ template,
44
+ entryName,
45
+ staticGenerate,
46
+ enableUnsafeCtx = false
47
+ } = renderOptions;
37
48
  const bundleJS = import_path.default.join(distDir, bundle);
38
49
  const loadableUri = import_path.default.join(distDir, import_utils.LOADABLE_STATS_FILE);
39
50
  const loadableStats = import_utils.fs.existsSync(loadableUri) ? require(loadableUri) : "";
@@ -47,7 +58,6 @@ const render = async (ctx, renderOptions, runner) => {
47
58
  host: ctx.host,
48
59
  query: ctx.query,
49
60
  url: ctx.href,
50
- cookieMap: import_cookie.default.parse(ctx.headers.cookie || ""),
51
61
  headers: ctx.headers
52
62
  },
53
63
  response: {
@@ -63,12 +73,14 @@ const render = async (ctx, renderOptions, runner) => {
63
73
  template,
64
74
  loadableStats,
65
75
  routeManifest,
76
+ // for streaming ssr
66
77
  entryName,
67
78
  staticGenerate,
68
79
  logger: void 0,
69
80
  metrics: void 0,
70
81
  req: ctx.req,
71
- res: ctx.res
82
+ res: ctx.res,
83
+ enableUnsafeCtx
72
84
  };
73
85
  context.logger = (0, import_measure.createLogger)(context, ctx.logger);
74
86
  context.metrics = (0, import_measure.createMetrics)(context, ctx.metrics);
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -27,6 +27,7 @@ class RouteMatchManager {
27
27
  this.specs = [];
28
28
  this.matchers = [];
29
29
  }
30
+ // get all routes matches pathname
30
31
  filter(pathname) {
31
32
  return this.matchers.reduce((matches, matcher) => {
32
33
  if (matcher.matchUrlPath(pathname)) {
@@ -35,6 +36,7 @@ class RouteMatchManager {
35
36
  return matches;
36
37
  }, []);
37
38
  }
39
+ // get best match from a set of matches
38
40
  best(pathname, matches) {
39
41
  let best;
40
42
  let matchedLen = 0;
@@ -50,6 +52,7 @@ class RouteMatchManager {
50
52
  }
51
53
  return best;
52
54
  }
55
+ // reset routes matcher
53
56
  reset(specs) {
54
57
  this.specs = specs;
55
58
  const matchers = specs.reduce((ms, spec) => {
@@ -58,6 +61,7 @@ class RouteMatchManager {
58
61
  }, []);
59
62
  this.matchers = matchers;
60
63
  }
64
+ // get best match from all matcher in manager
61
65
  match(pathname) {
62
66
  const matches = this.filter(pathname);
63
67
  const best = this.best(pathname, matches);
@@ -40,6 +40,7 @@ class RouteMatcher {
40
40
  this.spec = spec;
41
41
  this.setupUrlPath();
42
42
  }
43
+ // generate modern route object
43
44
  generate(url) {
44
45
  const route = new import_route.ModernRoute(this.spec);
45
46
  if (this.urlPath) {
@@ -57,6 +58,7 @@ class RouteMatcher {
57
58
  return matchResult.params;
58
59
  }
59
60
  }
61
+ // get match url length
60
62
  matchLength(pathname) {
61
63
  var _a;
62
64
  if (!this.urlReg) {
@@ -66,6 +68,7 @@ class RouteMatcher {
66
68
  return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
67
69
  }
68
70
  }
71
+ // if match url path
69
72
  matchUrlPath(requestUrl) {
70
73
  let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
71
74
  urlWithoutSlash = removeHtmlSuffix(urlWithoutSlash);
@@ -85,6 +88,7 @@ class RouteMatcher {
85
88
  matchEntry(entryName) {
86
89
  return this.spec.entryName === entryName;
87
90
  }
91
+ // compiler urlPath to regexp if necessary
88
92
  setupUrlPath() {
89
93
  const { urlPath } = this.spec;
90
94
  this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);
@@ -17,16 +17,20 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
23
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var serve_file_exports = {};
25
- __export(serve_file_exports, {
28
+ var serveFile_exports = {};
29
+ __export(serveFile_exports, {
26
30
  createStaticFileHandler: () => createStaticFileHandler,
27
31
  faviconFallbackHandler: () => faviconFallbackHandler
28
32
  });
29
- module.exports = __toCommonJS(serve_file_exports);
33
+ module.exports = __toCommonJS(serveFile_exports);
30
34
  var import_serve_static = __toESM(require("serve-static"));
31
35
  var import_utils = require("@modern-js/utils");
32
36
  var import_utils2 = require("../utils");
@@ -49,27 +53,30 @@ const faviconFallbackHandler = (context, next) => {
49
53
  next();
50
54
  }
51
55
  };
52
- const createStaticFileHandler = (rules, output = {}) => async (context, next) => {
53
- const { url: requestUrl, req, res } = context;
54
- const { assetPrefix = "/" } = output;
55
- const hitRule = rules.find((item) => {
56
- if ((0, import_utils.isString)(item.path) && requestUrl.startsWith(item.path)) {
57
- return true;
58
- } else if ((0, import_utils.isRegExp)(item.path) && item.path.test(requestUrl)) {
59
- return true;
60
- }
61
- return false;
62
- });
63
- if (hitRule) {
64
- const resume = removedPrefix(req, assetPrefix);
65
- (0, import_serve_static.default)(hitRule.target)(req, res, () => {
66
- resume();
67
- next();
56
+ const createStaticFileHandler = (rules, output = {}) => (
57
+ // eslint-disable-next-line consistent-return
58
+ async (context, next) => {
59
+ const { url: requestUrl, req, res } = context;
60
+ const { assetPrefix = "/" } = output;
61
+ const hitRule = rules.find((item) => {
62
+ if ((0, import_utils.isString)(item.path) && requestUrl.startsWith(item.path)) {
63
+ return true;
64
+ } else if ((0, import_utils.isRegExp)(item.path) && item.path.test(requestUrl)) {
65
+ return true;
66
+ }
67
+ return false;
68
68
  });
69
- } else {
70
- return next();
69
+ if (hitRule) {
70
+ const resume = removedPrefix(req, assetPrefix);
71
+ (0, import_serve_static.default)(hitRule.target)(req, res, () => {
72
+ resume();
73
+ next();
74
+ });
75
+ } else {
76
+ return next();
77
+ }
71
78
  }
72
- };
79
+ );
73
80
  // Annotate the CommonJS export names for ESM import in node:
74
81
  0 && (module.exports = {
75
82
  createStaticFileHandler,
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -33,10 +37,10 @@ var import_server_core = require("@modern-js/server-core");
33
37
  var import_metrics = require("../libs/metrics");
34
38
  var import_loadConfig = require("../libs/loadConfig");
35
39
  var import_utils2 = require("../utils");
36
- var import_modern_server_split = require("./modern-server-split");
40
+ var import_modernServerSplit = require("./modernServerSplit");
37
41
  class Server {
38
42
  constructor(options) {
39
- this.serverImpl = import_modern_server_split.createProdServer;
43
+ this.serverImpl = import_modernServerSplit.createProdServer;
40
44
  options.logger = options.logger || new import_utils.Logger({
41
45
  level: "warn"
42
46
  });
@@ -44,6 +48,20 @@ class Server {
44
48
  this.options = options;
45
49
  this.serverConfig = {};
46
50
  }
51
+ /**
52
+ * 初始化顺序
53
+ * - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
54
+ * - 获取 server runtime config
55
+ * - 设置 context
56
+ * - 创建 hooksRunner
57
+ * - 合并插件,内置插件和 serverConfig 中配置的插件
58
+ * - 执行 config hook
59
+ * - 获取最终的配置
60
+ * - 设置配置到 context
61
+ * - 初始化 server
62
+ * - 执行 prepare hook
63
+ * - 执行 server init
64
+ */
47
65
  async init() {
48
66
  const { options } = this;
49
67
  this.loadServerEnv(options);
@@ -58,6 +76,11 @@ class Server {
58
76
  await this.server.onInit(this.runner, this.app);
59
77
  return this;
60
78
  }
79
+ /**
80
+ * Execute config hooks
81
+ * @param runner
82
+ * @param options
83
+ */
61
84
  runConfigHook(runner, serverConfig) {
62
85
  const newServerConfig = runner.config(serverConfig || {});
63
86
  return newServerConfig;
@@ -75,6 +98,10 @@ class Server {
75
98
  const serverConfig = (0, import_loadConfig.requireConfig)(serverConfigPath);
76
99
  this.serverConfig = serverConfig;
77
100
  }
101
+ /**
102
+ *
103
+ * merge cliConfig and serverConfig
104
+ */
78
105
  async initConfig(runner, options) {
79
106
  const { pwd, config } = options;
80
107
  const { serverConfig } = this;
@@ -98,6 +125,9 @@ class Server {
98
125
  listener == null ? void 0 : listener();
99
126
  };
100
127
  if (typeof options === "object") {
128
+ if (process.env.PORT) {
129
+ Object.assign(options, { port: process.env.PORT });
130
+ }
101
131
  this.app.listen(options, callback);
102
132
  } else {
103
133
  this.app.listen(process.env.PORT || options || 8080, callback);
@@ -17,21 +17,25 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
23
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var modern_server_exports = {};
25
- __export(modern_server_exports, {
28
+ var modernServer_exports = {};
29
+ __export(modernServer_exports, {
26
30
  ModernServer: () => ModernServer
27
31
  });
28
- module.exports = __toCommonJS(modern_server_exports);
32
+ module.exports = __toCommonJS(modernServer_exports);
29
33
  var import_http = require("http");
30
34
  var import_path = __toESM(require("path"));
31
35
  var import_utils = require("@modern-js/utils");
32
36
  var import_route = require("../libs/route");
33
37
  var import_render = require("../libs/render");
34
- var import_serve_file = require("../libs/serve-file");
38
+ var import_serveFile = require("../libs/serveFile");
35
39
  var import_utils2 = require("../utils");
36
40
  var reader = __toESM(require("../libs/render/reader"));
37
41
  var import_proxy = require("../libs/proxy");
@@ -39,7 +43,6 @@ var import_context = require("../libs/context");
39
43
  var import_template = require("../libs/hook-api/template");
40
44
  var import_constants = require("../constants");
41
45
  var import_hook_api = require("../libs/hook-api");
42
- const API_DIR = "./api";
43
46
  const SERVER_DIR = "./server";
44
47
  class ModernServer {
45
48
  constructor({
@@ -72,6 +75,7 @@ class ModernServer {
72
75
  this.staticGenerate = staticGenerate || false;
73
76
  this.runMode = runMode || import_constants.RUN_MODE.FULL;
74
77
  }
78
+ // server prepare
75
79
  async onInit(runner, app) {
76
80
  var _a, _b;
77
81
  this.runner = runner;
@@ -96,7 +100,7 @@ class ModernServer {
96
100
  this.conf.output || {},
97
101
  this.conf.html
98
102
  );
99
- this.staticFileHandler = (0, import_serve_file.createStaticFileHandler)(
103
+ this.staticFileHandler = (0, import_serveFile.createStaticFileHandler)(
100
104
  [
101
105
  {
102
106
  path: staticPathRegExp,
@@ -114,11 +118,12 @@ class ModernServer {
114
118
  });
115
119
  await this.setupBeforeProdMiddleware();
116
120
  this.addHandler(this.staticFileHandler);
117
- this.addHandler(import_serve_file.faviconFallbackHandler);
121
+ this.addHandler(import_serveFile.faviconFallbackHandler);
118
122
  this.addBeforeRouteHandler();
119
123
  this.addHandler(this.routeHandler.bind(this));
120
124
  this.compose();
121
125
  }
126
+ // server ready
122
127
  onRepack(_) {
123
128
  }
124
129
  addBeforeRouteHandler() {
@@ -141,6 +146,7 @@ class ModernServer {
141
146
  const onlyWeb = filepath.startsWith(serverPath);
142
147
  this.prepareFrameHandler({ onlyWeb, onlyApi });
143
148
  }
149
+ // exposed requestHandler
144
150
  getRequestHandler() {
145
151
  return this.requestHandler.bind(this);
146
152
  }
@@ -162,6 +168,8 @@ class ModernServer {
162
168
  async createHTTPServer(handler) {
163
169
  return (0, import_http.createServer)(handler);
164
170
  }
171
+ /* —————————————————————— function will be overwrite —————————————————————— */
172
+ // get routes info
165
173
  getRoutes() {
166
174
  if (this.presetRoutes) {
167
175
  return this.presetRoutes;
@@ -173,9 +181,12 @@ class ModernServer {
173
181
  }
174
182
  return [];
175
183
  }
184
+ // add promisify request handler to server
185
+ // handler should do not do more things after invoke next
176
186
  addHandler(handler) {
177
187
  this.handlers.push(handler);
178
188
  }
189
+ // return 404 page
179
190
  render404(context) {
180
191
  context.error(import_constants.ERROR_DIGEST.ENOTF, "404 Not Found");
181
192
  this.renderErrorPage(context, 404);
@@ -193,19 +204,19 @@ class ModernServer {
193
204
  );
194
205
  this.beforeRouteHandler = handler;
195
206
  }
207
+ // gather frame extension and get framework handler
196
208
  async prepareFrameHandler(options) {
197
209
  const { workDir, runner } = this;
198
210
  const { onlyApi, onlyWeb } = options || {};
199
211
  const { getMiddlewares, ...collector } = (0, import_utils2.createMiddlewareCollecter)();
200
212
  await runner.gather(collector);
201
213
  const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
202
- const apiDir = import_path.default.join(workDir, API_DIR);
203
214
  const serverDir = import_path.default.join(workDir, SERVER_DIR);
204
215
  if (await import_utils.fs.pathExists(import_path.default.join(serverDir)) && !onlyApi) {
205
216
  const webExtension = (0, import_utils2.mergeExtension)(pluginWebExt);
206
217
  this.frameWebHandler = await this.prepareWebHandler(webExtension);
207
218
  }
208
- if (import_utils.fs.existsSync(apiDir) && !onlyWeb) {
219
+ if (!onlyWeb) {
209
220
  const apiExtension = (0, import_utils2.mergeExtension)(pluginAPIExt);
210
221
  this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
211
222
  }
@@ -263,6 +274,7 @@ class ModernServer {
263
274
  async proxy() {
264
275
  return null;
265
276
  }
277
+ // warmup ssr function
266
278
  warmupSSRBundle() {
267
279
  const { distDir } = this;
268
280
  const bundles = this.router.getBundles();
@@ -276,6 +288,8 @@ class ModernServer {
276
288
  createContext(req, res, options = {}) {
277
289
  return (0, import_context.createContext)(req, res, options);
278
290
  }
291
+ /* —————————————————————— private function —————————————————————— */
292
+ // handler route.json, include api / csr / ssr
279
293
  async routeHandler(context) {
280
294
  const { res } = context;
281
295
  const matched = this.router.match(context.path);
@@ -356,7 +370,7 @@ class ModernServer {
356
370
  (0, import_template.templateInjectableStream)({
357
371
  prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
358
372
  context.serverData
359
- )}<\/script>` : void 0
373
+ )}</script>` : void 0
360
374
  })
361
375
  ).pipe(res);
362
376
  return;
@@ -376,7 +390,7 @@ class ModernServer {
376
390
  afterRenderContext.template.prependHead(
377
391
  `<script>window._SERVER_DATA=${JSON.stringify(
378
392
  context.serverData
379
- )}<\/script>`
393
+ )}</script>`
380
394
  );
381
395
  response = afterRenderContext.template.get();
382
396
  }
@@ -392,6 +406,7 @@ class ModernServer {
392
406
  }
393
407
  return false;
394
408
  }
409
+ // compose handlers and create the final handler
395
410
  compose() {
396
411
  const { handlers } = this;
397
412
  if (!Array.isArray(handlers)) {