@modern-js/prod-server 2.27.0 → 2.28.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 (42) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/index.js +4 -0
  3. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +12 -7
  4. package/dist/cjs/libs/render/cache/index.js +2 -1
  5. package/dist/cjs/libs/render/cache/spr.js +2 -1
  6. package/dist/cjs/libs/render/index.js +12 -3
  7. package/dist/cjs/libs/render/reader.js +2 -1
  8. package/dist/cjs/libs/render/ssr.js +17 -18
  9. package/dist/cjs/libs/route/matcher.js +2 -2
  10. package/dist/cjs/server/index.js +6 -4
  11. package/dist/cjs/server/modernServer.js +21 -14
  12. package/dist/cjs/workerServer.js +3 -3
  13. package/dist/esm/index.js +1 -0
  14. package/dist/esm/libs/render/cache/__tests__/cache.test.js +12 -12
  15. package/dist/esm/libs/render/cache/index.js +2 -2
  16. package/dist/esm/libs/render/cache/spr.js +2 -2
  17. package/dist/esm/libs/render/index.js +12 -3
  18. package/dist/esm/libs/render/reader.js +2 -2
  19. package/dist/esm/libs/render/ssr.js +2 -1
  20. package/dist/esm/libs/route/matcher.js +2 -2
  21. package/dist/esm/renderHtml.js +6 -6
  22. package/dist/esm/server/index.js +6 -4
  23. package/dist/esm/server/modernServer.js +27 -18
  24. package/dist/esm/server/modernServerSplit.js +6 -6
  25. package/dist/esm/workerServer.js +2 -2
  26. package/dist/esm-node/index.js +1 -0
  27. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +12 -7
  28. package/dist/esm-node/libs/render/cache/index.js +2 -1
  29. package/dist/esm-node/libs/render/cache/spr.js +2 -1
  30. package/dist/esm-node/libs/render/index.js +12 -3
  31. package/dist/esm-node/libs/render/reader.js +2 -1
  32. package/dist/esm-node/libs/render/ssr.js +17 -18
  33. package/dist/esm-node/libs/route/matcher.js +2 -2
  34. package/dist/esm-node/server/index.js +6 -4
  35. package/dist/esm-node/server/modernServer.js +21 -14
  36. package/dist/esm-node/workerServer.js +3 -3
  37. package/dist/types/index.d.ts +1 -0
  38. package/dist/types/libs/context/context.d.ts +1 -1
  39. package/dist/types/libs/render/index.d.ts +13 -18
  40. package/dist/types/server/modernServer.d.ts +5 -3
  41. package/dist/types/utils.d.ts +1 -1
  42. package/package.json +8 -8
@@ -11,9 +11,9 @@ import { Readable } from "stream";
11
11
  import { Socket } from "net";
12
12
  import { Server } from "./server";
13
13
  import { isRedirect } from "./utils";
14
- var IncomingMessageLike = /* @__PURE__ */ function(Readable2) {
14
+ var IncomingMessageLike = /* @__PURE__ */ function(Readable1) {
15
15
  "use strict";
16
- _inherits(IncomingMessageLike2, Readable2);
16
+ _inherits(IncomingMessageLike2, Readable1);
17
17
  var _super = _create_super(IncomingMessageLike2);
18
18
  function IncomingMessageLike2(param) {
19
19
  var method = param.method, url = param.url, headers = param.headers;
@@ -33,9 +33,9 @@ var IncomingMessageLike = /* @__PURE__ */ function(Readable2) {
33
33
  }
34
34
  return IncomingMessageLike2;
35
35
  }(Readable);
36
- var ServerResponseLike = /* @__PURE__ */ function(OutgoingMessage2) {
36
+ var ServerResponseLike = /* @__PURE__ */ function(OutgoingMessage1) {
37
37
  "use strict";
38
- _inherits(ServerResponseLike2, OutgoingMessage2);
38
+ _inherits(ServerResponseLike2, OutgoingMessage1);
39
39
  var _super = _create_super(ServerResponseLike2);
40
40
  function ServerResponseLike2() {
41
41
  _class_call_check(this, ServerResponseLike2);
@@ -60,9 +60,9 @@ var ServerResponseLike = /* @__PURE__ */ function(OutgoingMessage2) {
60
60
  ]);
61
61
  return ServerResponseLike2;
62
62
  }(OutgoingMessage);
63
- var CustomServer = /* @__PURE__ */ function(Server2) {
63
+ var CustomServer = /* @__PURE__ */ function(Server1) {
64
64
  "use strict";
65
- _inherits(CustomServer2, Server2);
65
+ _inherits(CustomServer2, Server1);
66
66
  var _super = _create_super(CustomServer2);
67
67
  function CustomServer2() {
68
68
  _class_call_check(this, CustomServer2);
@@ -210,7 +210,8 @@ export var Server = /* @__PURE__ */ function() {
210
210
  key: "listen",
211
211
  value: function listen(options, listener) {
212
212
  var callback = function() {
213
- listener === null || listener === void 0 ? void 0 : listener();
213
+ var _listener;
214
+ (_listener = listener) === null || _listener === void 0 ? void 0 : _listener();
214
215
  };
215
216
  if (typeof options === "object") {
216
217
  if (process.env.PORT) {
@@ -306,6 +307,7 @@ export var Server = /* @__PURE__ */ function() {
306
307
  {
307
308
  key: "initAppContext",
308
309
  value: function initAppContext() {
310
+ var _appContext, _appContext1, _appContext2;
309
311
  var options = this.options;
310
312
  var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config, appContext = options.appContext;
311
313
  var serverPlugins = plugins.map(function(p) {
@@ -315,9 +317,9 @@ export var Server = /* @__PURE__ */ function() {
315
317
  });
316
318
  return {
317
319
  appDirectory: appDirectory,
318
- apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
319
- lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
320
- sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
320
+ apiDirectory: (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory,
321
+ lambdaDirectory: (_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.lambdaDirectory,
322
+ sharedDirectory: ((_appContext2 = appContext) === null || _appContext2 === void 0 ? void 0 : _appContext2.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
321
323
  distDirectory: path.join(appDirectory, config.output.path || "dist"),
322
324
  plugins: serverPlugins
323
325
  };
@@ -23,6 +23,7 @@ export var ModernServer = /* @__PURE__ */ function() {
23
23
  function ModernServer2(param) {
24
24
  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, appContext = param.appContext;
25
25
  _class_call_check(this, ModernServer2);
26
+ var _appContext;
26
27
  _define_property(this, "pwd", void 0);
27
28
  _define_property(this, "distDir", void 0);
28
29
  _define_property(this, "workDir", void 0);
@@ -37,13 +38,13 @@ export var ModernServer = /* @__PURE__ */ function() {
37
38
  _define_property(this, "reader", reader);
38
39
  _define_property(this, "proxyTarget", void 0);
39
40
  _define_property(this, "routeRenderHandler", void 0);
41
+ _define_property(this, "staticGenerate", void 0);
42
+ _define_property(this, "metaName", void 0);
40
43
  _define_property(this, "loaderHandler", null);
41
44
  _define_property(this, "frameWebHandler", null);
42
45
  _define_property(this, "frameAPIHandler", null);
43
46
  _define_property(this, "proxyHandler", null);
44
47
  _define_property(this, "_handler", void 0);
45
- _define_property(this, "staticGenerate", void 0);
46
- _define_property(this, "metaName", void 0);
47
48
  require("ignore-styles");
48
49
  this.pwd = pwd;
49
50
  this.distDir = path.resolve(pwd, config.output.path || "dist");
@@ -57,7 +58,7 @@ export var ModernServer = /* @__PURE__ */ function() {
57
58
  this.proxyTarget = proxyTarget;
58
59
  this.staticGenerate = staticGenerate || false;
59
60
  this.runMode = runMode || RUN_MODE.FULL;
60
- this.metaName = appContext === null || appContext === void 0 ? void 0 : appContext.metaName;
61
+ this.metaName = (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.metaName;
61
62
  }
62
63
  _create_class(ModernServer2, [
63
64
  {
@@ -67,12 +68,12 @@ export var ModernServer = /* @__PURE__ */ function() {
67
68
  function onInit(runner, app) {
68
69
  var _this = this;
69
70
  return _async_to_generator(function() {
70
- var _conf_bff, _this_conf_server, _conf_security, _this_conf_output, distDir, staticGenerate, conf, metaName, usageRoutes, ssrConfig, forceCSR;
71
+ var _conf_bff, _app, _this_conf_output, distDir, conf, usageRoutes;
71
72
  return _ts_generator(this, function(_state) {
72
73
  switch (_state.label) {
73
74
  case 0:
74
75
  _this.runner = runner;
75
- distDir = _this.distDir, staticGenerate = _this.staticGenerate, conf = _this.conf, metaName = _this.metaName;
76
+ distDir = _this.distDir, conf = _this.conf;
76
77
  _this.initReader();
77
78
  debug("final server conf", _this.conf);
78
79
  _this.proxyHandler = createProxyHandler((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy);
@@ -81,7 +82,7 @@ export var ModernServer = /* @__PURE__ */ function() {
81
82
  _this.addHandler(handler);
82
83
  });
83
84
  }
84
- app === null || app === void 0 ? void 0 : app.on("close", function() {
85
+ (_app = app) === null || _app === void 0 ? void 0 : _app.on("close", function() {
85
86
  _this.reader.close();
86
87
  });
87
88
  usageRoutes = _this.filterRoutes(_this.getRoutes());
@@ -99,15 +100,7 @@ export var ModernServer = /* @__PURE__ */ function() {
99
100
  ];
100
101
  case 2:
101
102
  _state.sent();
102
- ssrConfig = (_this_conf_server = _this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
103
- forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
104
- _this.routeRenderHandler = createRenderHandler({
105
- distDir: distDir,
106
- staticGenerate: staticGenerate,
107
- forceCSR: forceCSR,
108
- nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
109
- metaName: metaName
110
- });
103
+ _this.routeRenderHandler = _this.getRenderHandler();
111
104
  return [
112
105
  4,
113
106
  _this.setupBeforeProdMiddleware()
@@ -127,6 +120,22 @@ export var ModernServer = /* @__PURE__ */ function() {
127
120
  }
128
121
  )
129
122
  },
123
+ {
124
+ key: "getRenderHandler",
125
+ value: function getRenderHandler() {
126
+ var _this_conf_server, _conf_security;
127
+ var _this = this, distDir = _this.distDir, staticGenerate = _this.staticGenerate, conf = _this.conf, metaName = _this.metaName;
128
+ var ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
129
+ var forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
130
+ return createRenderHandler({
131
+ distDir: distDir,
132
+ staticGenerate: staticGenerate,
133
+ forceCSR: forceCSR,
134
+ nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
135
+ metaName: metaName
136
+ });
137
+ }
138
+ },
130
139
  {
131
140
  key: "onRepack",
132
141
  value: (
@@ -418,13 +427,13 @@ export var ModernServer = /* @__PURE__ */ function() {
418
427
  value: function prepareAPIHandler(extension) {
419
428
  var _this = this;
420
429
  return _async_to_generator(function() {
421
- var workDir, runner, conf, bff, prefix, webOnly;
430
+ var _bff, _bff1, workDir, runner, conf, bff, prefix, webOnly;
422
431
  return _ts_generator(this, function(_state) {
423
432
  switch (_state.label) {
424
433
  case 0:
425
434
  workDir = _this.workDir, runner = _this.runner, conf = _this.conf;
426
435
  bff = conf.bff;
427
- prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
436
+ prefix = ((_bff = bff) === null || _bff === void 0 ? void 0 : _bff.prefix) || "/api";
428
437
  return [
429
438
  4,
430
439
  isWebOnly()
@@ -446,7 +455,7 @@ export var ModernServer = /* @__PURE__ */ function() {
446
455
  pwd: workDir,
447
456
  config: extension,
448
457
  prefix: Array.isArray(prefix) ? prefix[0] : prefix,
449
- httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
458
+ httpMethodDecider: (_bff1 = bff) === null || _bff1 === void 0 ? void 0 : _bff1.httpMethodDecider,
450
459
  render: _this.render.bind(_this)
451
460
  }, {
452
461
  onLast: function() {
@@ -7,9 +7,9 @@ import { _ as _inherits } from "@swc/helpers/_/_inherits";
7
7
  import { _ as _create_super } from "@swc/helpers/_/_create_super";
8
8
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
9
9
  import { ModernServer } from "./modernServer";
10
- var ModernSSRServer = /* @__PURE__ */ function(ModernServer2) {
10
+ var ModernSSRServer = /* @__PURE__ */ function(ModernServer1) {
11
11
  "use strict";
12
- _inherits(ModernSSRServer2, ModernServer2);
12
+ _inherits(ModernSSRServer2, ModernServer1);
13
13
  var _super = _create_super(ModernSSRServer2);
14
14
  function ModernSSRServer2() {
15
15
  _class_call_check(this, ModernSSRServer2);
@@ -39,9 +39,9 @@ var ModernSSRServer = /* @__PURE__ */ function(ModernServer2) {
39
39
  ]);
40
40
  return ModernSSRServer2;
41
41
  }(ModernServer);
42
- var ModernAPIServer = /* @__PURE__ */ function(ModernServer2) {
42
+ var ModernAPIServer = /* @__PURE__ */ function(ModernServer1) {
43
43
  "use strict";
44
- _inherits(ModernAPIServer2, ModernServer2);
44
+ _inherits(ModernAPIServer2, ModernServer1);
45
45
  var _super = _create_super(ModernAPIServer2);
46
46
  function ModernAPIServer2() {
47
47
  _class_call_check(this, ModernAPIServer2);
@@ -65,9 +65,9 @@ var ModernAPIServer = /* @__PURE__ */ function(ModernServer2) {
65
65
  ]);
66
66
  return ModernAPIServer2;
67
67
  }(ModernServer);
68
- var ModernWebServer = /* @__PURE__ */ function(ModernServer2) {
68
+ var ModernWebServer = /* @__PURE__ */ function(ModernServer1) {
69
69
  "use strict";
70
- _inherits(ModernWebServer2, ModernServer2);
70
+ _inherits(ModernWebServer2, ModernServer1);
71
71
  var _super = _create_super(ModernWebServer2);
72
72
  function ModernWebServer2() {
73
73
  _class_call_check(this, ModernWebServer2);
@@ -91,7 +91,7 @@ export var createHandler = function(manifest) {
91
91
  routeMgr.reset(routes);
92
92
  return function() {
93
93
  var _ref = _async_to_generator(function(options) {
94
- var _page_serverHooks, _page_serverHooks_afterMatch, request, loadableStats, routeManifest, url, pageMatch, entryName, page, logger, metrics, hookContext, afterMatchHookContext, _page_serverHooks1, _page_serverHooks2, _page_serverHooks_afterRender, middlewarsHookContext, responseLike, params, baseUrl, serverRenderContext, body, afterRenderHookContext, e;
94
+ var _page_serverHooks_afterMatch, _page_serverHooks, _page, request, loadableStats, routeManifest, url, pageMatch, entryName, page, logger, metrics, hookContext, afterMatchHookContext, _page_serverHooks1, _page_serverHooks_afterRender, _page_serverHooks2, middlewarsHookContext, responseLike, params, baseUrl, serverRenderContext, body, afterRenderHookContext, e;
95
95
  function createServerRequest(url2, baseUrl2, request2, params2) {
96
96
  var pathname = url2.pathname, host = url2.host, searchParams = url2.searchParams;
97
97
  var rawHeaders = request2.headers;
@@ -130,7 +130,7 @@ export var createHandler = function(manifest) {
130
130
  metrics = defaultMetrics;
131
131
  hookContext = createWorkerHookContext(request.url, logger, metrics);
132
132
  afterMatchHookContext = createAfterMatchContext(hookContext, entryName);
133
- page === null || page === void 0 ? void 0 : (_page_serverHooks = page.serverHooks) === null || _page_serverHooks === void 0 ? void 0 : (_page_serverHooks_afterMatch = _page_serverHooks.afterMatch) === null || _page_serverHooks_afterMatch === void 0 ? void 0 : _page_serverHooks_afterMatch.call(_page_serverHooks, afterMatchHookContext, function() {
133
+ (_page = page) === null || _page === void 0 ? void 0 : (_page_serverHooks = _page.serverHooks) === null || _page_serverHooks === void 0 ? void 0 : (_page_serverHooks_afterMatch = _page_serverHooks.afterMatch) === null || _page_serverHooks_afterMatch === void 0 ? void 0 : _page_serverHooks_afterMatch.call(_page_serverHooks, afterMatchHookContext, function() {
134
134
  return void 0;
135
135
  });
136
136
  if (checkIsSent(hookContext)) {
@@ -4,6 +4,7 @@ export { ModernServer } from "./server/modernServer";
4
4
  export { createProxyHandler } from "./libs/proxy";
5
5
  export * from "./type";
6
6
  export * from "./constants";
7
+ export { createRenderHandler } from "./libs/render";
7
8
  export default (options) => {
8
9
  if (options == null) {
9
10
  throw new Error("can not start mserver without options");
@@ -15,6 +15,7 @@ const createCacheConfig = (config = {}) => ({
15
15
  jest.setTimeout(6e4);
16
16
  describe("cache", () => {
17
17
  it("should cache correctly", async () => {
18
+ var _cacheResult;
18
19
  destroyCache();
19
20
  const cache = createCache();
20
21
  const context = {
@@ -28,7 +29,7 @@ describe("cache", () => {
28
29
  await cache.set(context, content, cacheConfig, true);
29
30
  const cacheResult = await cache.get(context);
30
31
  expect(cacheResult).not.toBe(null);
31
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe("hello");
32
+ expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe("hello");
32
33
  });
33
34
  it("should ignore cache set when cache config not exist", async () => {
34
35
  destroyCache();
@@ -123,6 +124,7 @@ describe("cache", () => {
123
124
  destroyCache();
124
125
  const cache = createCache();
125
126
  for (const cacheable of cacheabelAry) {
127
+ var _cacheResult;
126
128
  const context = {
127
129
  entry: "",
128
130
  pathname: cacheable.requestOpt.url,
@@ -132,13 +134,14 @@ describe("cache", () => {
132
134
  const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
133
135
  await cache.set(context, cacheable.content, cacheConfig, true);
134
136
  const cacheResult = await cache.get(context);
135
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
137
+ expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(cacheable.content);
136
138
  }
137
139
  });
138
140
  it("should match cache correctly", async () => {
139
141
  destroyCache();
140
142
  const cache = createCache();
141
143
  for (const cacheable of matchedCacheableAry) {
144
+ var _cacheResult;
142
145
  const [baseCacheable, matchOne, ...other] = cacheable;
143
146
  const { requestOpt = {}, cacheConfig, content } = baseCacheable;
144
147
  const context = {
@@ -155,7 +158,7 @@ describe("cache", () => {
155
158
  headers: matchOne.headers
156
159
  };
157
160
  const cacheResult = await cache.get(matchContext);
158
- expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
161
+ expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(content);
159
162
  for (const notMatch of other) {
160
163
  const notMatchContext = {
161
164
  entry: "",
@@ -169,6 +172,7 @@ describe("cache", () => {
169
172
  }
170
173
  });
171
174
  it("should stale cache correctly", async () => {
175
+ var _freshResult, _staleResult;
172
176
  destroyCache();
173
177
  const cache = createCache();
174
178
  const context = {
@@ -184,16 +188,17 @@ describe("cache", () => {
184
188
  const shouldCache = await cache.set(context, content, config, true);
185
189
  expect(shouldCache.value).toBe(true);
186
190
  const freshResult = await cache.get(context);
187
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
191
+ expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isStale).toBe(false);
188
192
  await new Promise((resolve) => {
189
193
  setTimeout(() => {
190
194
  resolve();
191
195
  }, 6e3);
192
196
  });
193
197
  const staleResult = await cache.get(context);
194
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
198
+ expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isStale).toBe(true);
195
199
  });
196
200
  it("should garbage cache correctly", async () => {
201
+ var _freshResult, _staleResult;
197
202
  destroyCache();
198
203
  const cache = createCache();
199
204
  const context = {
@@ -210,13 +215,13 @@ describe("cache", () => {
210
215
  const shouldCache = await cache.set(context, content, config, true);
211
216
  expect(shouldCache.value).toBe(true);
212
217
  const freshResult = await cache.get(context);
213
- expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
218
+ expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isGarbage).toBe(false);
214
219
  await new Promise((resolve) => {
215
220
  setTimeout(() => {
216
221
  resolve();
217
222
  }, 1e4);
218
223
  });
219
224
  const staleResult = await cache.get(context);
220
- expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
225
+ expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isGarbage).toBe(true);
221
226
  });
222
227
  });
@@ -5,6 +5,7 @@ import { namespaceHash, withCoalescedInvoke } from "./util";
5
5
  export default (renderFn, ctx) => {
6
6
  const sprCache = createCache();
7
7
  const doRender = async (context) => {
8
+ var _cacheFile;
8
9
  const cacheContext = {
9
10
  entry: context.entryName,
10
11
  ...context.request
@@ -40,7 +41,7 @@ export default (renderFn, ctx) => {
40
41
  const renderResult = await renderFn(context);
41
42
  return afterRender(renderResult, saveHtmlIntoCache);
42
43
  }
43
- const cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash;
44
+ const cacheHash = (_cacheFile = cacheFile) === null || _cacheFile === void 0 ? void 0 : _cacheFile.hash;
44
45
  if (cacheFile.isGarbage) {
45
46
  const renderResult = await renderFn(context);
46
47
  return afterRender(renderResult, saveHtmlIntoCache);
@@ -141,9 +141,10 @@ class CacheManager {
141
141
  return maybeSync(doCache)(sync);
142
142
  }
143
143
  async del(context, cacheHash) {
144
+ var _data;
144
145
  const cacheKey = this.generateRequestKey(context);
145
146
  const data = this.cache.get(cacheKey);
146
- data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
147
+ (_data = data) === null || _data === void 0 ? void 0 : _data.caches.del(cacheHash);
147
148
  }
148
149
  constructor(cacheOptions) {
149
150
  _define_property(this, "cache", void 0);
@@ -5,7 +5,7 @@ import { handleDirectory } from "./static";
5
5
  import { readFile } from "./reader";
6
6
  import * as ssr from "./ssr";
7
7
  import { injectServerData } from "./utils";
8
- export const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
8
+ export const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce, ssrRender, metaName = "modern-js" }) => async function render({ ctx, route, runner }) {
9
9
  if (ctx.resHasHandled()) {
10
10
  return null;
11
11
  }
@@ -23,7 +23,7 @@ export const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce,
23
23
  const useCSR = forceCSR && (ctx.query.csr || ctx.headers[`x-${cutNameByHyphen(metaName)}-ssr-fallback`]);
24
24
  if (route.isSSR && !useCSR) {
25
25
  try {
26
- const result = await ssr.render(ctx, {
26
+ const ssrRenderOptions = {
27
27
  distDir,
28
28
  entryName: route.entryName,
29
29
  urlPath: route.urlPath,
@@ -31,7 +31,16 @@ export const createRenderHandler = ({ distDir, staticGenerate, forceCSR, nonce,
31
31
  template: content.toString(),
32
32
  staticGenerate,
33
33
  nonce
34
- }, runner);
34
+ };
35
+ const result = await (ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : ssr.render(ctx, {
36
+ distDir,
37
+ entryName: route.entryName,
38
+ urlPath: route.urlPath,
39
+ bundle: route.bundle,
40
+ template: content.toString(),
41
+ staticGenerate,
42
+ nonce
43
+ }, runner));
35
44
  return result;
36
45
  } catch (err) {
37
46
  ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
@@ -72,8 +72,9 @@ export class LruReader {
72
72
  }
73
73
  const reader = new LruReader();
74
74
  export const readFile = async (filepath) => {
75
+ var _file;
75
76
  const file = await reader.read(filepath);
76
- return file === null || file === void 0 ? void 0 : file.content;
77
+ return (_file = file) === null || _file === void 0 ? void 0 : _file.content;
77
78
  };
78
79
  export const updateFile = () => {
79
80
  reader.update();
@@ -73,21 +73,20 @@ export const render = async (ctx, renderOptions, runner) => {
73
73
  };
74
74
  }
75
75
  };
76
- const getRequestBody = (req) => {
77
- return new Promise((resolve, reject) => {
78
- if ((req === null || req === void 0 ? void 0 : req.method) && req.method.toLowerCase() !== "get") {
79
- let body = "";
80
- req.on("data", (chunk) => {
81
- body += chunk.toString();
82
- });
83
- req.on("end", () => {
84
- resolve(body);
85
- });
86
- req.on("error", (err) => {
87
- reject(err);
88
- });
89
- } else {
90
- resolve(void 0);
91
- }
92
- });
93
- };
76
+ const getRequestBody = (req) => new Promise((resolve, reject) => {
77
+ var _req;
78
+ if (((_req = req) === null || _req === void 0 ? void 0 : _req.method) && req.method.toLowerCase() !== "get") {
79
+ let body = "";
80
+ req.on("data", (chunk) => {
81
+ body += chunk.toString();
82
+ });
83
+ req.on("end", () => {
84
+ resolve(body);
85
+ });
86
+ req.on("error", (err) => {
87
+ reject(err);
88
+ });
89
+ } else {
90
+ resolve(void 0);
91
+ }
92
+ });
@@ -39,9 +39,9 @@ export class RouteMatcher {
39
39
  if (!this.urlReg) {
40
40
  return this.urlPath.length;
41
41
  } else {
42
- var _result_;
42
+ var _result_, _result;
43
43
  const result = this.urlReg.exec(pathname);
44
- return (result === null || result === void 0 ? void 0 : (_result_ = result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
44
+ return ((_result = result) === null || _result === void 0 ? void 0 : (_result_ = _result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
45
45
  }
46
46
  }
47
47
  // if match url path
@@ -78,7 +78,8 @@ export class Server {
78
78
  }
79
79
  listen(options, listener) {
80
80
  const callback = () => {
81
- listener === null || listener === void 0 ? void 0 : listener();
81
+ var _listener;
82
+ (_listener = listener) === null || _listener === void 0 ? void 0 : _listener();
82
83
  };
83
84
  if (typeof options === "object") {
84
85
  if (process.env.PORT) {
@@ -128,6 +129,7 @@ export class Server {
128
129
  });
129
130
  }
130
131
  initAppContext() {
132
+ var _appContext, _appContext1, _appContext2;
131
133
  const { options } = this;
132
134
  const { pwd: appDirectory, plugins = [], config, appContext } = options;
133
135
  const serverPlugins = plugins.map((p) => ({
@@ -135,9 +137,9 @@ export class Server {
135
137
  }));
136
138
  return {
137
139
  appDirectory,
138
- apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
139
- lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
140
- sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
140
+ apiDirectory: (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory,
141
+ lambdaDirectory: (_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.lambdaDirectory,
142
+ sharedDirectory: ((_appContext2 = appContext) === null || _appContext2 === void 0 ? void 0 : _appContext2.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
141
143
  distDirectory: path.join(appDirectory, config.output.path || "dist"),
142
144
  plugins: serverPlugins
143
145
  };
@@ -15,9 +15,9 @@ const SERVER_DIR = "./server";
15
15
  export class ModernServer {
16
16
  // server prepare
17
17
  async onInit(runner, app) {
18
- var _conf_bff, _this_conf_server, _conf_security, _this_conf_output;
18
+ var _conf_bff, _app, _this_conf_output;
19
19
  this.runner = runner;
20
- const { distDir, staticGenerate, conf, metaName } = this;
20
+ const { distDir, conf } = this;
21
21
  this.initReader();
22
22
  debug("final server conf", this.conf);
23
23
  this.proxyHandler = createProxyHandler((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy);
@@ -26,7 +26,7 @@ export class ModernServer {
26
26
  this.addHandler(handler);
27
27
  });
28
28
  }
29
- app === null || app === void 0 ? void 0 : app.on("close", () => {
29
+ (_app = app) === null || _app === void 0 ? void 0 : _app.on("close", () => {
30
30
  this.reader.close();
31
31
  });
32
32
  const usageRoutes = this.filterRoutes(this.getRoutes());
@@ -34,20 +34,25 @@ export class ModernServer {
34
34
  this.warmupSSRBundle();
35
35
  await this.prepareFrameHandler();
36
36
  await this.prepareLoaderHandler(usageRoutes, distDir);
37
+ this.routeRenderHandler = this.getRenderHandler();
38
+ await this.setupBeforeProdMiddleware();
39
+ this.addHandler(this.setupStaticMiddleware((_this_conf_output = this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
40
+ this.addHandler(faviconFallbackHandler);
41
+ this.addHandler(this.routeHandler.bind(this));
42
+ this.compose();
43
+ }
44
+ getRenderHandler() {
45
+ var _this_conf_server, _conf_security;
46
+ const { distDir, staticGenerate, conf, metaName } = this;
37
47
  const ssrConfig = (_this_conf_server = this.conf.server) === null || _this_conf_server === void 0 ? void 0 : _this_conf_server.ssr;
38
48
  const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
39
- this.routeRenderHandler = createRenderHandler({
49
+ return createRenderHandler({
40
50
  distDir,
41
51
  staticGenerate,
42
52
  forceCSR,
43
53
  nonce: (_conf_security = conf.security) === null || _conf_security === void 0 ? void 0 : _conf_security.nonce,
44
54
  metaName
45
55
  });
46
- await this.setupBeforeProdMiddleware();
47
- this.addHandler(this.setupStaticMiddleware((_this_conf_output = this.conf.output) === null || _this_conf_output === void 0 ? void 0 : _this_conf_output.assetPrefix));
48
- this.addHandler(faviconFallbackHandler);
49
- this.addHandler(this.routeHandler.bind(this));
50
- this.compose();
51
56
  }
52
57
  // server ready
53
58
  onRepack(_) {
@@ -153,9 +158,10 @@ export class ModernServer {
153
158
  return handler;
154
159
  }
155
160
  async prepareAPIHandler(extension) {
161
+ var _bff, _bff1;
156
162
  const { workDir, runner, conf } = this;
157
163
  const { bff } = conf;
158
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
164
+ const prefix = ((_bff = bff) === null || _bff === void 0 ? void 0 : _bff.prefix) || "/api";
159
165
  const webOnly = await isWebOnly();
160
166
  if (webOnly && process.env.NODE_ENV === "development") {
161
167
  return (req, res) => {
@@ -167,7 +173,7 @@ export class ModernServer {
167
173
  pwd: workDir,
168
174
  config: extension,
169
175
  prefix: Array.isArray(prefix) ? prefix[0] : prefix,
170
- httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
176
+ httpMethodDecider: (_bff1 = bff) === null || _bff1 === void 0 ? void 0 : _bff1.httpMethodDecider,
171
177
  render: this.render.bind(this)
172
178
  }, {
173
179
  onLast: () => null
@@ -434,6 +440,7 @@ export class ModernServer {
434
440
  context.res.end(createErrorDocument(status, text));
435
441
  }
436
442
  constructor({ pwd, config, routes, staticGenerate, logger, metrics, runMode, proxyTarget, appContext }) {
443
+ var _appContext;
437
444
  _define_property(this, "pwd", void 0);
438
445
  _define_property(this, "distDir", void 0);
439
446
  _define_property(this, "workDir", void 0);
@@ -448,13 +455,13 @@ export class ModernServer {
448
455
  _define_property(this, "reader", reader);
449
456
  _define_property(this, "proxyTarget", void 0);
450
457
  _define_property(this, "routeRenderHandler", void 0);
458
+ _define_property(this, "staticGenerate", void 0);
459
+ _define_property(this, "metaName", void 0);
451
460
  _define_property(this, "loaderHandler", null);
452
461
  _define_property(this, "frameWebHandler", null);
453
462
  _define_property(this, "frameAPIHandler", null);
454
463
  _define_property(this, "proxyHandler", null);
455
464
  _define_property(this, "_handler", void 0);
456
- _define_property(this, "staticGenerate", void 0);
457
- _define_property(this, "metaName", void 0);
458
465
  require("ignore-styles");
459
466
  this.pwd = pwd;
460
467
  this.distDir = path.resolve(pwd, config.output.path || "dist");
@@ -468,6 +475,6 @@ export class ModernServer {
468
475
  this.proxyTarget = proxyTarget;
469
476
  this.staticGenerate = staticGenerate || false;
470
477
  this.runMode = runMode || RUN_MODE.FULL;
471
- this.metaName = appContext === null || appContext === void 0 ? void 0 : appContext.metaName;
478
+ this.metaName = (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.metaName;
472
479
  }
473
480
  }
@@ -69,7 +69,7 @@ export const createHandler = (manifest) => {
69
69
  const { pages, routes } = manifest;
70
70
  routeMgr.reset(routes);
71
71
  return async (options) => {
72
- var _page_serverHooks, _page_serverHooks_afterMatch;
72
+ var _page_serverHooks_afterMatch, _page_serverHooks, _page;
73
73
  const { request, loadableStats, routeManifest } = options;
74
74
  const url = new URL(request.url);
75
75
  const pageMatch = routeMgr.match(url.pathname);
@@ -84,13 +84,13 @@ export const createHandler = (manifest) => {
84
84
  const metrics = defaultMetrics;
85
85
  const hookContext = createWorkerHookContext(request.url, logger, metrics);
86
86
  const afterMatchHookContext = createAfterMatchContext(hookContext, entryName);
87
- page === null || page === void 0 ? void 0 : (_page_serverHooks = page.serverHooks) === null || _page_serverHooks === void 0 ? void 0 : (_page_serverHooks_afterMatch = _page_serverHooks.afterMatch) === null || _page_serverHooks_afterMatch === void 0 ? void 0 : _page_serverHooks_afterMatch.call(_page_serverHooks, afterMatchHookContext, () => void 0);
87
+ (_page = page) === null || _page === void 0 ? void 0 : (_page_serverHooks = _page.serverHooks) === null || _page_serverHooks === void 0 ? void 0 : (_page_serverHooks_afterMatch = _page_serverHooks.afterMatch) === null || _page_serverHooks_afterMatch === void 0 ? void 0 : _page_serverHooks_afterMatch.call(_page_serverHooks, afterMatchHookContext, () => void 0);
88
88
  if (checkIsSent(hookContext)) {
89
89
  return new ReturnResponse(hookContext.res.body || "Unkown body", hookContext.res.status, hookContext.res.headers);
90
90
  }
91
91
  if (page.serverRender) {
92
92
  try {
93
- var _page_serverHooks1, _page_serverHooks2, _page_serverHooks_afterRender;
93
+ var _page_serverHooks1, _page_serverHooks_afterRender, _page_serverHooks2;
94
94
  const middlewarsHookContext = createMiddlewareContext(hookContext);
95
95
  applyMiddlewares(middlewarsHookContext, (_page_serverHooks1 = page.serverHooks) === null || _page_serverHooks1 === void 0 ? void 0 : _page_serverHooks1.middleware);
96
96
  if (checkIsSent(hookContext)) {
@@ -6,5 +6,6 @@ export { ModernServer } from './server/modernServer';
6
6
  export { createProxyHandler } from './libs/proxy';
7
7
  export * from './type';
8
8
  export * from './constants';
9
+ export { createRenderHandler } from './libs/render';
9
10
  declare const _default: (options: ModernServerOptions) => Promise<Server>;
10
11
  export default _default;