@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
@@ -15,13 +15,13 @@ var __copyProps = (to, from, except, desc) => {
15
15
  return to;
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var modern_server_split_exports = {};
19
- __export(modern_server_split_exports, {
18
+ var modernServerSplit_exports = {};
19
+ __export(modernServerSplit_exports, {
20
20
  createProdServer: () => createProdServer
21
21
  });
22
- module.exports = __toCommonJS(modern_server_split_exports);
23
- var import_modern_server = require("./modern-server");
24
- class ModernSSRServer extends import_modern_server.ModernServer {
22
+ module.exports = __toCommonJS(modernServerSplit_exports);
23
+ var import_modernServer = require("./modernServer");
24
+ class ModernSSRServer extends import_modernServer.ModernServer {
25
25
  prepareAPIHandler(_) {
26
26
  return null;
27
27
  }
@@ -29,7 +29,7 @@ class ModernSSRServer extends import_modern_server.ModernServer {
29
29
  return this.render404(context);
30
30
  }
31
31
  }
32
- class ModernAPIServer extends import_modern_server.ModernServer {
32
+ class ModernAPIServer extends import_modernServer.ModernServer {
33
33
  prepareWebHandler(_) {
34
34
  return null;
35
35
  }
@@ -37,7 +37,7 @@ class ModernAPIServer extends import_modern_server.ModernServer {
37
37
  return routes.filter((route) => route.isApi);
38
38
  }
39
39
  }
40
- class ModernWebServer extends import_modern_server.ModernServer {
40
+ class ModernWebServer extends import_modernServer.ModernServer {
41
41
  async warmupSSRBundle() {
42
42
  return null;
43
43
  }
@@ -57,7 +57,7 @@ const createProdServer = (options) => {
57
57
  } else if (options.webOnly) {
58
58
  return new ModernWebServer(options);
59
59
  } else {
60
- return new import_modern_server.ModernServer(options);
60
+ return new import_modernServer.ModernServer(options);
61
61
  }
62
62
  };
63
63
  // Annotate the CommonJS export names for ESM import in node:
@@ -15,12 +15,12 @@ var __copyProps = (to, from, except, desc) => {
15
15
  return to;
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var worker_server_exports = {};
19
- __export(worker_server_exports, {
18
+ var workerServer_exports = {};
19
+ __export(workerServer_exports, {
20
20
  createHandler: () => createHandler,
21
21
  handleUrl: () => handleUrl
22
22
  });
23
- module.exports = __toCommonJS(worker_server_exports);
23
+ module.exports = __toCommonJS(workerServer_exports);
24
24
  var import_logger = require("./libs/logger");
25
25
  var import_route = require("./libs/route");
26
26
  var import_metrics = require("./libs/metrics");
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Server } from "./server";
2
- import { ModernServer } from "./server/modern-server";
2
+ import { ModernServer } from "./server/modernServer";
3
3
  import { createProxyHandler } from "./libs/proxy";
4
4
  export * from "./type";
5
5
  export * from "./constants";
@@ -47,9 +47,15 @@ var ModernServerContext = /*#__PURE__*/ function() {
47
47
  "use strict";
48
48
  function ModernServerContext(req, res, options) {
49
49
  _classCallCheck(this, ModernServerContext);
50
- _defineProperty(this, "req", void 0);
51
- _defineProperty(this, "res", void 0);
52
- _defineProperty(this, "params", {});
50
+ /**
51
+ * http request
52
+ */ _defineProperty(this, "req", void 0);
53
+ /**
54
+ * http response
55
+ */ _defineProperty(this, "res", void 0);
56
+ /**
57
+ * url params
58
+ */ _defineProperty(this, "params", {});
53
59
  _defineProperty(this, "serverData", void 0);
54
60
  _defineProperty(this, "options", {});
55
61
  this.req = req;
@@ -88,6 +94,7 @@ var ModernServerContext = /*#__PURE__*/ function() {
88
94
  }
89
95
  },
90
96
  {
97
+ // compat express res.send, only support etag now
91
98
  key: "send",
92
99
  value: function send(body) {
93
100
  try {
@@ -155,7 +162,7 @@ var ModernServerContext = /*#__PURE__*/ function() {
155
162
  },
156
163
  {
157
164
  key: "headers",
158
- get: function get() {
165
+ get: /* request property */ function get() {
159
166
  return this.req.headers;
160
167
  }
161
168
  },
@@ -248,7 +255,7 @@ var ModernServerContext = /*#__PURE__*/ function() {
248
255
  },
249
256
  {
250
257
  key: "status",
251
- get: function get() {
258
+ get: /* response property */ function get() {
252
259
  return this.res.statusCode;
253
260
  },
254
261
  set: function set(statusCode) {
@@ -256,7 +263,9 @@ var ModernServerContext = /*#__PURE__*/ function() {
256
263
  }
257
264
  },
258
265
  {
259
- key: "resHasHandled",
266
+ /**
267
+ * 判断链接是否已经关闭
268
+ */ key: "resHasHandled",
260
269
  value: function resHasHandled() {
261
270
  return this.res.writableEnded;
262
271
  }
@@ -203,6 +203,7 @@ var CacheManager = /*#__PURE__*/ function() {
203
203
  this.cacheOptions = cacheOptions;
204
204
  this.cache = new LRUCache({
205
205
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
206
+ // 默认存 100M,最大 600M
206
207
  length: function length(n) {
207
208
  var len = n.caches.keys().reduce(function(total, cur) {
208
209
  var _n_caches_peek;
@@ -124,16 +124,15 @@ var __generator = this && this.__generator || function(thisArg, body) {
124
124
  };
125
125
  import path from "path";
126
126
  import { fs, LOADABLE_STATS_FILE, mime, ROUTE_MINIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
127
- import cookie from "cookie";
128
127
  import cache from "./cache";
129
128
  import { createLogger, createMetrics } from "./measure";
130
129
  var render = function() {
131
130
  var _ref = _asyncToGenerator(function(ctx, renderOptions, runner) {
132
- var _ctx_res, urlPath, bundle, distDir, template, entryName, staticGenerate, bundleJS, loadableUri, loadableStats, routesManifestUri, routeManifest, context, serverRender, content, _context_redirection, url, _context_redirection_status, status;
131
+ var _ctx_res, urlPath, bundle, distDir, template, entryName, staticGenerate, _renderOptions_enableUnsafeCtx, enableUnsafeCtx, bundleJS, loadableUri, loadableStats, routesManifestUri, routeManifest, context, serverRender, content, _context_redirection, url, _context_redirection_status, status;
133
132
  return __generator(this, function(_state) {
134
133
  switch(_state.label){
135
134
  case 0:
136
- urlPath = renderOptions.urlPath, bundle = renderOptions.bundle, distDir = renderOptions.distDir, template = renderOptions.template, entryName = renderOptions.entryName, staticGenerate = renderOptions.staticGenerate;
135
+ urlPath = renderOptions.urlPath, bundle = renderOptions.bundle, distDir = renderOptions.distDir, template = renderOptions.template, entryName = renderOptions.entryName, staticGenerate = renderOptions.staticGenerate, _renderOptions_enableUnsafeCtx = renderOptions.enableUnsafeCtx, enableUnsafeCtx = _renderOptions_enableUnsafeCtx === void 0 ? false : _renderOptions_enableUnsafeCtx;
137
136
  bundleJS = path.join(distDir, bundle);
138
137
  loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
139
138
  loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
@@ -147,7 +146,6 @@ var render = function() {
147
146
  host: ctx.host,
148
147
  query: ctx.query,
149
148
  url: ctx.href,
150
- cookieMap: cookie.parse(ctx.headers.cookie || ""),
151
149
  headers: ctx.headers
152
150
  },
153
151
  response: {
@@ -163,12 +161,14 @@ var render = function() {
163
161
  template: template,
164
162
  loadableStats: loadableStats,
165
163
  routeManifest: routeManifest,
164
+ // for streaming ssr
166
165
  entryName: entryName,
167
166
  staticGenerate: staticGenerate,
168
167
  logger: void 0,
169
168
  metrics: void 0,
170
169
  req: ctx.req,
171
- res: ctx.res
170
+ res: ctx.res,
171
+ enableUnsafeCtx: enableUnsafeCtx
172
172
  };
173
173
  context.logger = createLogger(context, ctx.logger);
174
174
  context.metrics = createMetrics(context, ctx.metrics);
@@ -41,6 +41,7 @@ var RouteMatchManager = /*#__PURE__*/ function() {
41
41
  }
42
42
  _createClass(RouteMatchManager, [
43
43
  {
44
+ // get all routes matches pathname
44
45
  key: "filter",
45
46
  value: function filter(pathname) {
46
47
  return this.matchers.reduce(function(matches, matcher) {
@@ -52,6 +53,7 @@ var RouteMatchManager = /*#__PURE__*/ function() {
52
53
  }
53
54
  },
54
55
  {
56
+ // get best match from a set of matches
55
57
  key: "best",
56
58
  value: function best(pathname, matches) {
57
59
  var best;
@@ -87,6 +89,7 @@ var RouteMatchManager = /*#__PURE__*/ function() {
87
89
  }
88
90
  },
89
91
  {
92
+ // reset routes matcher
90
93
  key: "reset",
91
94
  value: function reset(specs) {
92
95
  this.specs = specs;
@@ -98,6 +101,7 @@ var RouteMatchManager = /*#__PURE__*/ function() {
98
101
  }
99
102
  },
100
103
  {
104
+ // get best match from all matcher in manager
101
105
  key: "match",
102
106
  value: function match(pathname) {
103
107
  var matches = this.filter(pathname);
@@ -61,6 +61,7 @@ var RouteMatcher = /*#__PURE__*/ function() {
61
61
  }
62
62
  _createClass(RouteMatcher, [
63
63
  {
64
+ // generate modern route object
64
65
  key: "generate",
65
66
  value: function generate(url) {
66
67
  var route = new ModernRoute(this.spec);
@@ -84,6 +85,7 @@ var RouteMatcher = /*#__PURE__*/ function() {
84
85
  }
85
86
  },
86
87
  {
88
+ // get match url length
87
89
  key: "matchLength",
88
90
  value: function matchLength(pathname) {
89
91
  if (!this.urlReg) {
@@ -96,6 +98,7 @@ var RouteMatcher = /*#__PURE__*/ function() {
96
98
  }
97
99
  },
98
100
  {
101
+ // if match url path
99
102
  key: "matchUrlPath",
100
103
  value: function matchUrlPath(requestUrl) {
101
104
  var urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
@@ -121,6 +124,7 @@ var RouteMatcher = /*#__PURE__*/ function() {
121
124
  }
122
125
  },
123
126
  {
127
+ // compiler urlPath to regexp if necessary
124
128
  key: "setupUrlPath",
125
129
  value: function setupUrlPath() {
126
130
  var urlPath = this.spec.urlPath;
@@ -146,7 +146,8 @@ var faviconFallbackHandler = function(context, next) {
146
146
  var createStaticFileHandler = function(rules) {
147
147
  var output = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
148
148
  return function() {
149
- var _ref = _asyncToGenerator(function(context, next) {
149
+ var _ref = // eslint-disable-next-line consistent-return
150
+ _asyncToGenerator(function(context, next) {
150
151
  var requestUrl, req, res, _output_assetPrefix, assetPrefix, hitRule, resume;
151
152
  return __generator(this, function(_state) {
152
153
  requestUrl = context.url, req = context.req, res = context.res;
@@ -225,7 +225,7 @@ import { serverManager, AppContext, ConfigContext, loadPlugins } from "@modern-j
225
225
  import { metrics as defaultMetrics } from "../libs/metrics";
226
226
  import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
227
227
  import { debug } from "../utils";
228
- import { createProdServer } from "./modern-server-split";
228
+ import { createProdServer } from "./modernServerSplit";
229
229
  var Server = /*#__PURE__*/ function() {
230
230
  "use strict";
231
231
  function Server(options) {
@@ -246,7 +246,20 @@ var Server = /*#__PURE__*/ function() {
246
246
  _createClass(Server, [
247
247
  {
248
248
  key: "init",
249
- value: function init() {
249
+ value: /**
250
+ * 初始化顺序
251
+ * - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
252
+ * - 获取 server runtime config
253
+ * - 设置 context
254
+ * - 创建 hooksRunner
255
+ * - 合并插件,内置插件和 serverConfig 中配置的插件
256
+ * - 执行 config hook
257
+ * - 获取最终的配置
258
+ * - 设置配置到 context
259
+ * - 初始化 server
260
+ * - 执行 prepare hook
261
+ * - 执行 server init
262
+ */ function init() {
250
263
  var _this = this;
251
264
  return _asyncToGenerator(function() {
252
265
  var options;
@@ -309,7 +322,11 @@ var Server = /*#__PURE__*/ function() {
309
322
  }
310
323
  },
311
324
  {
312
- key: "runConfigHook",
325
+ /**
326
+ * Execute config hooks
327
+ * @param runner
328
+ * @param options
329
+ */ key: "runConfigHook",
313
330
  value: function runConfigHook(runner, serverConfig) {
314
331
  var newServerConfig = runner.config(serverConfig || {});
315
332
  return newServerConfig;
@@ -340,7 +357,10 @@ var Server = /*#__PURE__*/ function() {
340
357
  },
341
358
  {
342
359
  key: "initConfig",
343
- value: function initConfig(runner, options) {
360
+ value: /**
361
+ *
362
+ * merge cliConfig and serverConfig
363
+ */ function initConfig(runner, options) {
344
364
  var _this = this;
345
365
  return _asyncToGenerator(function() {
346
366
  var pwd, config, serverConfig, finalServerConfig, resolvedConfigPath;
@@ -382,6 +402,11 @@ var Server = /*#__PURE__*/ function() {
382
402
  listener === null || listener === void 0 ? void 0 : listener();
383
403
  };
384
404
  if (typeof options === "object") {
405
+ if (process.env.PORT) {
406
+ Object.assign(options, {
407
+ port: process.env.PORT
408
+ });
409
+ }
385
410
  this.app.listen(options, callback);
386
411
  } else {
387
412
  this.app.listen(process.env.PORT || options || 8080, callback);
@@ -201,7 +201,7 @@ import path from "path";
201
201
  import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
202
202
  import { RouteMatchManager } from "../libs/route";
203
203
  import { createRenderHandler } from "../libs/render";
204
- import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serve-file";
204
+ import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serveFile";
205
205
  import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExtension, noop, debug, isRedirect } from "../utils";
206
206
  import * as reader from "../libs/render/reader";
207
207
  import { createProxyHandler } from "../libs/proxy";
@@ -209,15 +209,17 @@ import { createContext } from "../libs/context";
209
209
  import { templateInjectableStream } from "../libs/hook-api/template";
210
210
  import { AGGRED_DIR, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE } from "../constants";
211
211
  import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "../libs/hook-api";
212
- var API_DIR = "./api";
213
212
  var SERVER_DIR = "./server";
214
213
  var ModernServer = /*#__PURE__*/ function() {
215
214
  "use strict";
216
215
  function ModernServer(param) {
217
216
  var pwd = param.pwd, config = param.config, routes = param.routes, staticGenerate = param.staticGenerate, logger = param.logger, metrics = param.metrics, runMode = param.runMode, proxyTarget = param.proxyTarget;
218
217
  _classCallCheck(this, ModernServer);
218
+ // appDirectory
219
219
  _defineProperty(this, "pwd", void 0);
220
+ // product dist dir
220
221
  _defineProperty(this, "distDir", void 0);
222
+ // work on src or dist
221
223
  _defineProperty(this, "workDir", void 0);
222
224
  _defineProperty(this, "router", void 0);
223
225
  _defineProperty(this, "conf", void 0);
@@ -254,7 +256,8 @@ var ModernServer = /*#__PURE__*/ function() {
254
256
  _createClass(ModernServer, [
255
257
  {
256
258
  key: "onInit",
257
- value: function onInit(runner, app) {
259
+ value: // server prepare
260
+ function onInit(runner, app) {
258
261
  var _this = this;
259
262
  return _asyncToGenerator(function() {
260
263
  var _conf_bff, _this_conf_server, distDir, staticGenerate, conf, usageRoutes, staticPathRegExp, ssrConfig, forceCSR;
@@ -323,6 +326,7 @@ var ModernServer = /*#__PURE__*/ function() {
323
326
  }
324
327
  },
325
328
  {
329
+ // server ready
326
330
  key: "onRepack",
327
331
  value: function onRepack(_) {}
328
332
  },
@@ -382,6 +386,7 @@ var ModernServer = /*#__PURE__*/ function() {
382
386
  }
383
387
  },
384
388
  {
389
+ // exposed requestHandler
385
390
  key: "getRequestHandler",
386
391
  value: function getRequestHandler() {
387
392
  return this.requestHandler.bind(this);
@@ -442,6 +447,7 @@ var ModernServer = /*#__PURE__*/ function() {
442
447
  }
443
448
  },
444
449
  {
450
+ /* —————————————————————— function will be overwrite —————————————————————— */ // get routes info
445
451
  key: "getRoutes",
446
452
  value: function getRoutes() {
447
453
  if (this.presetRoutes) {
@@ -456,12 +462,15 @@ var ModernServer = /*#__PURE__*/ function() {
456
462
  }
457
463
  },
458
464
  {
465
+ // add promisify request handler to server
466
+ // handler should do not do more things after invoke next
459
467
  key: "addHandler",
460
468
  value: function addHandler(handler) {
461
469
  this.handlers.push(handler);
462
470
  }
463
471
  },
464
472
  {
473
+ // return 404 page
465
474
  key: "render404",
466
475
  value: function render404(context) {
467
476
  context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
@@ -502,10 +511,11 @@ var ModernServer = /*#__PURE__*/ function() {
502
511
  },
503
512
  {
504
513
  key: "prepareFrameHandler",
505
- value: function prepareFrameHandler(options) {
514
+ value: // gather frame extension and get framework handler
515
+ function prepareFrameHandler(options) {
506
516
  var _this = this;
507
517
  return _asyncToGenerator(function() {
508
- var workDir, runner, _ref, onlyApi, onlyWeb, _createMiddlewareCollecter, getMiddlewares, collector, _getMiddlewares, pluginAPIExt, pluginWebExt, apiDir, serverDir, webExtension, apiExtension;
518
+ var workDir, runner, _ref, onlyApi, onlyWeb, _createMiddlewareCollecter, getMiddlewares, collector, _getMiddlewares, pluginAPIExt, pluginWebExt, serverDir, webExtension, apiExtension;
509
519
  return __generator(this, function(_state) {
510
520
  switch(_state.label){
511
521
  case 0:
@@ -521,7 +531,6 @@ var ModernServer = /*#__PURE__*/ function() {
521
531
  case 1:
522
532
  _state.sent();
523
533
  _getMiddlewares = getMiddlewares(), pluginAPIExt = _getMiddlewares.api, pluginWebExt = _getMiddlewares.web;
524
- apiDir = path.join(workDir, API_DIR);
525
534
  serverDir = path.join(workDir, SERVER_DIR);
526
535
  return [
527
536
  4,
@@ -541,7 +550,7 @@ var ModernServer = /*#__PURE__*/ function() {
541
550
  _this.frameWebHandler = _state.sent();
542
551
  _state.label = 4;
543
552
  case 4:
544
- if (!(fs.existsSync(apiDir) && !onlyWeb)) return [
553
+ if (!!onlyWeb) return [
545
554
  3,
546
555
  6
547
556
  ];
@@ -714,6 +723,7 @@ var ModernServer = /*#__PURE__*/ function() {
714
723
  }
715
724
  },
716
725
  {
726
+ // warmup ssr function
717
727
  key: "warmupSSRBundle",
718
728
  value: function warmupSSRBundle() {
719
729
  var distDir = this.distDir;
@@ -735,7 +745,8 @@ var ModernServer = /*#__PURE__*/ function() {
735
745
  },
736
746
  {
737
747
  key: "routeHandler",
738
- value: function routeHandler(context) {
748
+ value: /* —————————————————————— private function —————————————————————— */ // handler route.json, include api / csr / ssr
749
+ function routeHandler(context) {
739
750
  var _this = this;
740
751
  return _asyncToGenerator(function() {
741
752
  var res, matched, route, afterMatchContext, _afterMatchContext_router, current, url, status, matched2, middlewareContext, renderResult, contentStream, response, afterRenderContext;
@@ -920,6 +931,7 @@ var ModernServer = /*#__PURE__*/ function() {
920
931
  }
921
932
  },
922
933
  {
934
+ // compose handlers and create the final handler
923
935
  key: "compose",
924
936
  value: function compose() {
925
937
  var _this = this;
@@ -230,7 +230,7 @@ var __generator = this && this.__generator || function(thisArg, body) {
230
230
  };
231
231
  }
232
232
  };
233
- import { ModernServer } from "./modern-server";
233
+ import { ModernServer } from "./modernServer";
234
234
  var ModernSSRServer = /*#__PURE__*/ function(ModernServer) {
235
235
  "use strict";
236
236
  _inherits(ModernSSRServer, ModernServer);
@@ -1,5 +1,5 @@
1
1
  import { Server } from "./server";
2
- import { ModernServer } from "./server/modern-server";
2
+ import { ModernServer } from "./server/modernServer";
3
3
  import { createProxyHandler } from "./libs/proxy";
4
4
  export * from "./type";
5
5
  export * from "./constants";
@@ -6,6 +6,9 @@ import fresh from "fresh";
6
6
  import { headersWithoutCookie } from "../../utils";
7
7
  class ModernServerContext {
8
8
  constructor(req, res, options) {
9
+ /**
10
+ * url params
11
+ */
9
12
  this.params = {};
10
13
  this.options = {};
11
14
  this.req = req;
@@ -28,6 +31,7 @@ class ModernServerContext {
28
31
  this.send(body);
29
32
  };
30
33
  }
34
+ // compat express res.send, only support etag now
31
35
  send(body) {
32
36
  try {
33
37
  const generateETag = !this.res.getHeader("ETag") && this.options.etag;
@@ -77,6 +81,7 @@ class ModernServerContext {
77
81
  }
78
82
  return false;
79
83
  }
84
+ /* request property */
80
85
  get headers() {
81
86
  return this.req.headers;
82
87
  }
@@ -137,12 +142,16 @@ class ModernServerContext {
137
142
  const str = this.querystring;
138
143
  return qs.parse(str);
139
144
  }
145
+ /* response property */
140
146
  get status() {
141
147
  return this.res.statusCode;
142
148
  }
143
149
  set status(statusCode) {
144
150
  this.res.statusCode = statusCode;
145
151
  }
152
+ /**
153
+ * 判断链接是否已经关闭
154
+ */
146
155
  resHasHandled() {
147
156
  return this.res.writableEnded;
148
157
  }
@@ -23,6 +23,7 @@ const loadConfig = ({
23
23
  {
24
24
  ...resolvedConfig,
25
25
  plugins: []
26
+ // filter cli plugins
26
27
  },
27
28
  serverConfig,
28
29
  cliConfig
@@ -54,6 +54,7 @@ class CacheManager {
54
54
  this.cacheOptions = cacheOptions;
55
55
  this.cache = new LRUCache({
56
56
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
57
+ // 默认存 100M,最大 600M
57
58
  length(n) {
58
59
  const len = n.caches.keys().reduce((total, cur) => {
59
60
  var _a;
@@ -12,11 +12,13 @@ const createCacheItem = async (filepath, mtime) => {
12
12
  };
13
13
  };
14
14
  class LruReader {
15
+ // private timer?: NodeJS.Timeout;
15
16
  constructor() {
16
17
  this.cache = new LRU({
17
18
  max: 256 * MB,
18
19
  length: getContentLength,
19
20
  maxAge: 5 * 60 * 5e3
21
+ // 60s
20
22
  });
21
23
  }
22
24
  init() {
@@ -61,6 +63,9 @@ class LruReader {
61
63
  }
62
64
  }
63
65
  }
66
+ // private timeTask() {
67
+ // this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
68
+ // }
64
69
  }
65
70
  const reader = new LruReader();
66
71
  const readFile = async (filepath) => {
@@ -6,12 +6,19 @@ import {
6
6
  ROUTE_MINIFEST_FILE,
7
7
  SERVER_RENDER_FUNCTION_NAME
8
8
  } from "@modern-js/utils";
9
- import cookie from "cookie";
10
9
  import cache from "./cache";
11
10
  import { createLogger, createMetrics } from "./measure";
12
11
  const render = async (ctx, renderOptions, runner) => {
13
12
  var _a;
14
- const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
13
+ const {
14
+ urlPath,
15
+ bundle,
16
+ distDir,
17
+ template,
18
+ entryName,
19
+ staticGenerate,
20
+ enableUnsafeCtx = false
21
+ } = renderOptions;
15
22
  const bundleJS = path.join(distDir, bundle);
16
23
  const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
17
24
  const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
@@ -25,7 +32,6 @@ const render = async (ctx, renderOptions, runner) => {
25
32
  host: ctx.host,
26
33
  query: ctx.query,
27
34
  url: ctx.href,
28
- cookieMap: cookie.parse(ctx.headers.cookie || ""),
29
35
  headers: ctx.headers
30
36
  },
31
37
  response: {
@@ -41,12 +47,14 @@ const render = async (ctx, renderOptions, runner) => {
41
47
  template,
42
48
  loadableStats,
43
49
  routeManifest,
50
+ // for streaming ssr
44
51
  entryName,
45
52
  staticGenerate,
46
53
  logger: void 0,
47
54
  metrics: void 0,
48
55
  req: ctx.req,
49
- res: ctx.res
56
+ res: ctx.res,
57
+ enableUnsafeCtx
50
58
  };
51
59
  context.logger = createLogger(context, ctx.logger);
52
60
  context.metrics = createMetrics(context, ctx.metrics);
@@ -4,6 +4,7 @@ class RouteMatchManager {
4
4
  this.specs = [];
5
5
  this.matchers = [];
6
6
  }
7
+ // get all routes matches pathname
7
8
  filter(pathname) {
8
9
  return this.matchers.reduce((matches, matcher) => {
9
10
  if (matcher.matchUrlPath(pathname)) {
@@ -12,6 +13,7 @@ class RouteMatchManager {
12
13
  return matches;
13
14
  }, []);
14
15
  }
16
+ // get best match from a set of matches
15
17
  best(pathname, matches) {
16
18
  let best;
17
19
  let matchedLen = 0;
@@ -27,6 +29,7 @@ class RouteMatchManager {
27
29
  }
28
30
  return best;
29
31
  }
32
+ // reset routes matcher
30
33
  reset(specs) {
31
34
  this.specs = specs;
32
35
  const matchers = specs.reduce((ms, spec) => {
@@ -35,6 +38,7 @@ class RouteMatchManager {
35
38
  }, []);
36
39
  this.matchers = matchers;
37
40
  }
41
+ // get best match from all matcher in manager
38
42
  match(pathname) {
39
43
  const matches = this.filter(pathname);
40
44
  const best = this.best(pathname, matches);
@@ -22,6 +22,7 @@ class RouteMatcher {
22
22
  this.spec = spec;
23
23
  this.setupUrlPath();
24
24
  }
25
+ // generate modern route object
25
26
  generate(url) {
26
27
  const route = new ModernRoute(this.spec);
27
28
  if (this.urlPath) {
@@ -39,6 +40,7 @@ class RouteMatcher {
39
40
  return matchResult.params;
40
41
  }
41
42
  }
43
+ // get match url length
42
44
  matchLength(pathname) {
43
45
  var _a;
44
46
  if (!this.urlReg) {
@@ -48,6 +50,7 @@ class RouteMatcher {
48
50
  return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
49
51
  }
50
52
  }
53
+ // if match url path
51
54
  matchUrlPath(requestUrl) {
52
55
  let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
53
56
  urlWithoutSlash = removeHtmlSuffix(urlWithoutSlash);
@@ -67,6 +70,7 @@ class RouteMatcher {
67
70
  matchEntry(entryName) {
68
71
  return this.spec.entryName === entryName;
69
72
  }
73
+ // compiler urlPath to regexp if necessary
70
74
  setupUrlPath() {
71
75
  const { urlPath } = this.spec;
72
76
  this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);