@modern-js/prod-server 2.35.0 → 2.35.1

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 (47) hide show
  1. package/dist/cjs/libs/context/context.js +29 -5
  2. package/dist/cjs/libs/preload/flushServerHeader.js +2 -2
  3. package/dist/cjs/libs/preload/parseLinks.js +8 -9
  4. package/dist/cjs/libs/preload/transformLinks2String.js +1 -2
  5. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +7 -12
  6. package/dist/cjs/libs/render/cache/index.js +1 -2
  7. package/dist/cjs/libs/render/cache/spr.js +1 -2
  8. package/dist/cjs/libs/render/index.js +3 -2
  9. package/dist/cjs/libs/render/reader.js +1 -2
  10. package/dist/cjs/libs/route/matcher.js +2 -2
  11. package/dist/cjs/server/index.js +5 -7
  12. package/dist/cjs/server/modernServer.js +25 -27
  13. package/dist/cjs/workerServer.js +5 -6
  14. package/dist/esm/libs/context/context.js +29 -5
  15. package/dist/esm/libs/preload/flushServerHeader.js +3 -3
  16. package/dist/esm/libs/preload/parseLinks.js +7 -8
  17. package/dist/esm/libs/preload/transformLinks2String.js +1 -2
  18. package/dist/esm/libs/render/cache/__tests__/cache.test.js +12 -12
  19. package/dist/esm/libs/render/cache/index.js +2 -2
  20. package/dist/esm/libs/render/cache/spr.js +2 -2
  21. package/dist/esm/libs/render/index.js +5 -2
  22. package/dist/esm/libs/render/reader.js +2 -2
  23. package/dist/esm/libs/route/matcher.js +2 -2
  24. package/dist/esm/server/index.js +6 -8
  25. package/dist/esm/server/modernServer.js +26 -37
  26. package/dist/esm/workerServer.js +5 -6
  27. package/dist/esm-node/libs/context/context.js +29 -5
  28. package/dist/esm-node/libs/preload/flushServerHeader.js +2 -2
  29. package/dist/esm-node/libs/preload/parseLinks.js +7 -8
  30. package/dist/esm-node/libs/preload/transformLinks2String.js +1 -2
  31. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +7 -12
  32. package/dist/esm-node/libs/render/cache/index.js +1 -2
  33. package/dist/esm-node/libs/render/cache/spr.js +1 -2
  34. package/dist/esm-node/libs/render/index.js +3 -2
  35. package/dist/esm-node/libs/render/reader.js +1 -2
  36. package/dist/esm-node/libs/route/matcher.js +2 -2
  37. package/dist/esm-node/server/index.js +6 -8
  38. package/dist/esm-node/server/modernServer.js +25 -27
  39. package/dist/esm-node/workerServer.js +5 -6
  40. package/dist/types/libs/context/context.d.ts +2 -1
  41. package/dist/types/type.d.ts +1 -0
  42. package/dist/types/utils.d.ts +1 -1
  43. package/package.json +9 -8
  44. package/dist/cjs/libs/logger.js +0 -123
  45. package/dist/esm/libs/logger.js +0 -125
  46. package/dist/esm-node/libs/logger.js +0 -106
  47. package/dist/types/libs/logger.d.ts +0 -61
@@ -4,8 +4,8 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import path from "path";
5
5
  import { NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, ROUTE_SPEC_FILE, fs } from "@modern-js/utils";
6
6
  import { parse as htmlParse } from "node-html-parser";
7
- import { matchRoutes } from "@modern-js/utils/runtime/remix-router";
8
- import { matchEntry } from "@modern-js/utils/runtime-node";
7
+ import { matchRoutes } from "@modern-js/runtime-utils/remix-router";
8
+ import { matchEntry } from "@modern-js/runtime-utils/node";
9
9
  export function parseLinks(_) {
10
10
  return _parseLinks.apply(this, arguments);
11
11
  }
@@ -69,7 +69,7 @@ function parseLinksFromRoutes(pathname, distDir) {
69
69
  }
70
70
  function _parseLinksFromRoutes() {
71
71
  _parseLinksFromRoutes = _async_to_generator(function(pathname, distDir) {
72
- var noopLinks, nestedRoutesSpec, routesJsonPath, routeManifestPath, routesJson, serverRoutes, entry, _routeAssets_entryName, _matches, _assets_filter, _assets, _assets_filter1, _assets1, routes, entryName, entryRoutes, routesManifest, routeAssets, matches, entryAssets, assets, cssLinks, scriptLinks;
72
+ var noopLinks, nestedRoutesSpec, routesJsonPath, routeManifestPath, routesJson, serverRoutes, entry, _routeAssets_entryName, _assets_filter, _assets_filter1, routes, entryName, entryRoutes, routesManifest, routeAssets, matches, entryAssets, assets, cssLinks, scriptLinks;
73
73
  return _ts_generator(this, function(_state) {
74
74
  switch (_state.label) {
75
75
  case 0:
@@ -125,12 +125,11 @@ function _parseLinksFromRoutes() {
125
125
  routeAssets = routesManifest.routeAssets;
126
126
  matches = matchRoutes(entryRoutes, pathname, entry.urlPath);
127
127
  entryAssets = (_routeAssets_entryName = routeAssets[entryName]) === null || _routeAssets_entryName === void 0 ? void 0 : _routeAssets_entryName.assets;
128
- assets = (_matches = matches) === null || _matches === void 0 ? void 0 : _matches.reduce(function(acc, match) {
128
+ assets = matches === null || matches === void 0 ? void 0 : matches.reduce(function(acc, match) {
129
129
  var routeId = match.route.id;
130
130
  if (routeId) {
131
- var _matchedManifest;
132
131
  var matchedManifest = routeAssets[routeId];
133
- var assets2 = (_matchedManifest = matchedManifest) === null || _matchedManifest === void 0 ? void 0 : _matchedManifest.assets;
132
+ var assets2 = matchedManifest === null || matchedManifest === void 0 ? void 0 : matchedManifest.assets;
134
133
  if (Array.isArray(assets2)) {
135
134
  var _acc;
136
135
  (_acc = acc).push.apply(_acc, _to_consumable_array(assets2));
@@ -138,7 +137,7 @@ function _parseLinksFromRoutes() {
138
137
  }
139
138
  return acc;
140
139
  }, []).concat(entryAssets || []);
141
- cssLinks = (_assets = assets) === null || _assets === void 0 ? void 0 : (_assets_filter = _assets.filter(function(asset) {
140
+ cssLinks = assets === null || assets === void 0 ? void 0 : (_assets_filter = assets.filter(function(asset) {
142
141
  return asset.endsWith(".css");
143
142
  })) === null || _assets_filter === void 0 ? void 0 : _assets_filter.map(function(uri) {
144
143
  return {
@@ -146,7 +145,7 @@ function _parseLinksFromRoutes() {
146
145
  as: "style"
147
146
  };
148
147
  });
149
- scriptLinks = (_assets1 = assets) === null || _assets1 === void 0 ? void 0 : (_assets_filter1 = _assets1.filter(function(asset) {
148
+ scriptLinks = assets === null || assets === void 0 ? void 0 : (_assets_filter1 = assets.filter(function(asset) {
150
149
  return asset.endsWith(".js");
151
150
  })) === null || _assets_filter1 === void 0 ? void 0 : _assets_filter1.map(function(uri) {
152
151
  return {
@@ -11,7 +11,6 @@ export function transformLinks2String(links, preload) {
11
11
  return resolveLinks;
12
12
  }
13
13
  function addInclude(links, include) {
14
- var _include;
15
14
  var images = [
16
15
  "gif",
17
16
  "jpg",
@@ -37,7 +36,7 @@ function addInclude(links, include) {
37
36
  "ttf",
38
37
  "otf"
39
38
  ];
40
- var includes = ((_include = include) === null || _include === void 0 ? void 0 : _include.map(function(item) {
39
+ var includes = (include === null || include === void 0 ? void 0 : include.map(function(item) {
41
40
  if (typeof item === "string") {
42
41
  var type = function() {
43
42
  if (item.endsWith(".js")) {
@@ -21,7 +21,7 @@ var createCacheConfig = function() {
21
21
  jest.setTimeout(6e4);
22
22
  describe("cache", function() {
23
23
  it("should cache correctly", /* @__PURE__ */ _async_to_generator(function() {
24
- var _cacheResult, cache, context, content, cacheConfig, cacheResult;
24
+ var cache, context, content, cacheConfig, cacheResult;
25
25
  return _ts_generator(this, function(_state) {
26
26
  switch (_state.label) {
27
27
  case 0:
@@ -48,7 +48,7 @@ describe("cache", function() {
48
48
  case 2:
49
49
  cacheResult = _state.sent();
50
50
  expect(cacheResult).not.toBe(null);
51
- expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe("hello");
51
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe("hello");
52
52
  return [
53
53
  2
54
54
  ];
@@ -261,7 +261,7 @@ describe("cache", function() {
261
261
  });
262
262
  }));
263
263
  it("should get cache correctly", /* @__PURE__ */ _async_to_generator(function() {
264
- var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheResult, context, cacheConfig, cacheResult, err;
264
+ var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, context, cacheConfig, cacheResult, err;
265
265
  return _ts_generator(this, function(_state) {
266
266
  switch (_state.label) {
267
267
  case 0:
@@ -304,7 +304,7 @@ describe("cache", function() {
304
304
  ];
305
305
  case 4:
306
306
  cacheResult = _state.sent();
307
- expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(cacheable.content);
307
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(cacheable.content);
308
308
  _state.label = 5;
309
309
  case 5:
310
310
  _iteratorNormalCompletion = true;
@@ -346,7 +346,7 @@ describe("cache", function() {
346
346
  });
347
347
  }));
348
348
  it("should match cache correctly", /* @__PURE__ */ _async_to_generator(function() {
349
- var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheResult, _cacheable, baseCacheable, matchOne, other, _baseCacheable_requestOpt, requestOpt, cacheConfig, content, context, matchContext, cacheResult, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, notMatch, notMatchContext, nothing, err, err;
349
+ var cache, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, cacheable, _cacheable, baseCacheable, matchOne, other, _baseCacheable_requestOpt, requestOpt, cacheConfig, content, context, matchContext, cacheResult, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, notMatch, notMatchContext, nothing, err, err;
350
350
  return _ts_generator(this, function(_state) {
351
351
  switch (_state.label) {
352
352
  case 0:
@@ -396,7 +396,7 @@ describe("cache", function() {
396
396
  ];
397
397
  case 4:
398
398
  cacheResult = _state.sent();
399
- expect((_cacheResult = cacheResult) === null || _cacheResult === void 0 ? void 0 : _cacheResult.content).toBe(content);
399
+ expect(cacheResult === null || cacheResult === void 0 ? void 0 : cacheResult.content).toBe(content);
400
400
  _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
401
401
  _state.label = 5;
402
402
  case 5:
@@ -501,7 +501,7 @@ describe("cache", function() {
501
501
  });
502
502
  }));
503
503
  it("should stale cache correctly", /* @__PURE__ */ _async_to_generator(function() {
504
- var _freshResult, _staleResult, cache, context, config, content, shouldCache, freshResult, staleResult;
504
+ var cache, context, config, content, shouldCache, freshResult, staleResult;
505
505
  return _ts_generator(this, function(_state) {
506
506
  switch (_state.label) {
507
507
  case 0:
@@ -530,7 +530,7 @@ describe("cache", function() {
530
530
  ];
531
531
  case 2:
532
532
  freshResult = _state.sent();
533
- expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isStale).toBe(false);
533
+ expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isStale).toBe(false);
534
534
  return [
535
535
  4,
536
536
  new Promise(function(resolve) {
@@ -547,7 +547,7 @@ describe("cache", function() {
547
547
  ];
548
548
  case 4:
549
549
  staleResult = _state.sent();
550
- expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isStale).toBe(true);
550
+ expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isStale).toBe(true);
551
551
  return [
552
552
  2
553
553
  ];
@@ -555,7 +555,7 @@ describe("cache", function() {
555
555
  });
556
556
  }));
557
557
  it("should garbage cache correctly", /* @__PURE__ */ _async_to_generator(function() {
558
- var _freshResult, _staleResult, cache, context, config, content, shouldCache, freshResult, staleResult;
558
+ var cache, context, config, content, shouldCache, freshResult, staleResult;
559
559
  return _ts_generator(this, function(_state) {
560
560
  switch (_state.label) {
561
561
  case 0:
@@ -585,7 +585,7 @@ describe("cache", function() {
585
585
  ];
586
586
  case 2:
587
587
  freshResult = _state.sent();
588
- expect((_freshResult = freshResult) === null || _freshResult === void 0 ? void 0 : _freshResult.isGarbage).toBe(false);
588
+ expect(freshResult === null || freshResult === void 0 ? void 0 : freshResult.isGarbage).toBe(false);
589
589
  return [
590
590
  4,
591
591
  new Promise(function(resolve) {
@@ -602,7 +602,7 @@ describe("cache", function() {
602
602
  ];
603
603
  case 4:
604
604
  staleResult = _state.sent();
605
- expect((_staleResult = staleResult) === null || _staleResult === void 0 ? void 0 : _staleResult.isGarbage).toBe(true);
605
+ expect(staleResult === null || staleResult === void 0 ? void 0 : staleResult.isGarbage).toBe(true);
606
606
  return [
607
607
  2
608
608
  ];
@@ -9,7 +9,7 @@ export default function(renderFn, ctx) {
9
9
  var sprCache = createCache();
10
10
  var doRender = function() {
11
11
  var _ref = _async_to_generator(function(context) {
12
- var _cacheFile, cacheContext, cacheFile, renderResult, cacheHash, renderResult1, render;
12
+ var cacheContext, cacheFile, renderResult, cacheHash, renderResult1, render;
13
13
  function afterRender(source, onAfterRender) {
14
14
  return _afterRender.apply(this, arguments);
15
15
  }
@@ -124,7 +124,7 @@ export default function(renderFn, ctx) {
124
124
  afterRender(renderResult, saveHtmlIntoCache)
125
125
  ];
126
126
  case 3:
127
- cacheHash = (_cacheFile = cacheFile) === null || _cacheFile === void 0 ? void 0 : _cacheFile.hash;
127
+ cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash;
128
128
  if (!cacheFile.isGarbage)
129
129
  return [
130
130
  3,
@@ -311,11 +311,11 @@ var CacheManager = /* @__PURE__ */ function() {
311
311
  value: function del(context, cacheHash) {
312
312
  var _this = this;
313
313
  return _async_to_generator(function() {
314
- var _data, cacheKey, data;
314
+ var cacheKey, data;
315
315
  return _ts_generator(this, function(_state) {
316
316
  cacheKey = _this.generateRequestKey(context);
317
317
  data = _this.cache.get(cacheKey);
318
- (_data = data) === null || _data === void 0 ? void 0 : _data.caches.del(cacheHash);
318
+ data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
319
319
  return [
320
320
  2
321
321
  ];
@@ -8,6 +8,9 @@ import { handleDirectory } from "./static";
8
8
  import { readFile } from "./reader";
9
9
  import * as ssr from "./ssr";
10
10
  import { injectServerData } from "./utils";
11
+ var calcFallback = function(metaName) {
12
+ return "x-".concat(cutNameByHyphen(metaName), "-ssr-fallback");
13
+ };
11
14
  export var createRenderHandler = function(param) {
12
15
  var distDir = param.distDir, staticGenerate = param.staticGenerate, conf = param.conf, forceCSR = param.forceCSR, nonce = param.nonce, ssrRender = param.ssrRender, _param_metaName = param.metaName, metaName = _param_metaName === void 0 ? "modern-js" : _param_metaName;
13
16
  return function() {
@@ -54,7 +57,7 @@ export var createRenderHandler = function(param) {
54
57
  null
55
58
  ];
56
59
  }
57
- useCSR = forceCSR && (ctx.query.csr || ctx.headers["x-".concat(cutNameByHyphen(metaName), "-ssr-fallback")]);
60
+ useCSR = forceCSR && (ctx.query.csr || ctx.headers[calcFallback(metaName)]);
58
61
  if (!(route.isSSR && !useCSR))
59
62
  return [
60
63
  3,
@@ -122,7 +125,7 @@ export var createRenderHandler = function(param) {
122
125
  case 8:
123
126
  err = _state.sent();
124
127
  ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
125
- ctx.res.set("x-modern-ssr-fallback", "1");
128
+ ctx.res.set(calcFallback(metaName), "1");
126
129
  return [
127
130
  3,
128
131
  9
@@ -157,7 +157,7 @@ export var LruReader = /* @__PURE__ */ function() {
157
157
  var reader = new LruReader();
158
158
  export var readFile = function() {
159
159
  var _ref = _async_to_generator(function(filepath) {
160
- var _file, file;
160
+ var file;
161
161
  return _ts_generator(this, function(_state) {
162
162
  switch (_state.label) {
163
163
  case 0:
@@ -169,7 +169,7 @@ export var readFile = function() {
169
169
  file = _state.sent();
170
170
  return [
171
171
  2,
172
- (_file = file) === null || _file === void 0 ? void 0 : _file.content
172
+ file === null || file === void 0 ? void 0 : file.content
173
173
  ];
174
174
  }
175
175
  });
@@ -65,9 +65,9 @@ export var RouteMatcher = /* @__PURE__ */ function() {
65
65
  if (!this.urlReg) {
66
66
  return this.urlPath.length;
67
67
  } else {
68
- var _result_, _result;
68
+ var _result_;
69
69
  var result = this.urlReg.exec(pathname);
70
- return ((_result = result) === null || _result === void 0 ? void 0 : (_result_ = _result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
70
+ return (result === null || result === void 0 ? void 0 : (_result_ = result[0]) === null || _result_ === void 0 ? void 0 : _result_.length) || null;
71
71
  }
72
72
  }
73
73
  )
@@ -7,7 +7,7 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
7
7
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
8
8
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
9
9
  import path from "path";
10
- import { fs, Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS } from "@modern-js/utils";
10
+ import { fs, Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS, ensureAbsolutePath } from "@modern-js/utils";
11
11
  import { serverManager, AppContext, ConfigContext, loadPlugins } from "@modern-js/server-core";
12
12
  import { metrics as defaultMetrics } from "../libs/metrics";
13
13
  import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
@@ -198,7 +198,7 @@ export var Server = /* @__PURE__ */ function() {
198
198
  pwd = options.pwd, config = options.config;
199
199
  serverConfig = _this.serverConfig;
200
200
  finalServerConfig = _this.runConfigHook(runner, serverConfig);
201
- resolvedConfigPath = path.join(pwd, config.output.path || "dist", OUTPUT_CONFIG_FILE);
201
+ resolvedConfigPath = ensureAbsolutePath(pwd, path.join(config.output.path || "dist", OUTPUT_CONFIG_FILE));
202
202
  options.config = loadConfig({
203
203
  cliConfig: config,
204
204
  serverConfig: finalServerConfig,
@@ -230,8 +230,7 @@ export var Server = /* @__PURE__ */ function() {
230
230
  key: "listen",
231
231
  value: function listen(options, listener) {
232
232
  var callback = function() {
233
- var _listener;
234
- (_listener = listener) === null || _listener === void 0 ? void 0 : _listener();
233
+ listener === null || listener === void 0 ? void 0 : listener();
235
234
  };
236
235
  if (typeof options === "object") {
237
236
  if (process.env.PORT) {
@@ -327,7 +326,6 @@ export var Server = /* @__PURE__ */ function() {
327
326
  {
328
327
  key: "initAppContext",
329
328
  value: function initAppContext() {
330
- var _appContext, _appContext1, _appContext2;
331
329
  var options = this.options;
332
330
  var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config, appContext = options.appContext;
333
331
  var serverPlugins = plugins.map(function(p) {
@@ -337,9 +335,9 @@ export var Server = /* @__PURE__ */ function() {
337
335
  });
338
336
  return {
339
337
  appDirectory: appDirectory,
340
- apiDirectory: (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.apiDirectory,
341
- lambdaDirectory: (_appContext1 = appContext) === null || _appContext1 === void 0 ? void 0 : _appContext1.lambdaDirectory,
342
- sharedDirectory: ((_appContext2 = appContext) === null || _appContext2 === void 0 ? void 0 : _appContext2.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
338
+ apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
339
+ lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
340
+ sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
343
341
  distDirectory: path.join(appDirectory, config.output.path || "dist"),
344
342
  plugins: serverPlugins
345
343
  };
@@ -8,7 +8,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
8
  import { createServer } from "http";
9
9
  import path from "path";
10
10
  import { fs, isPromise, isWebOnly, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
11
- import { time } from "@modern-js/utils/universal/time";
11
+ import { time } from "@modern-js/runtime-utils/time";
12
12
  import { RouteMatchManager } from "../libs/route";
13
13
  import { createRenderHandler } from "../libs/render";
14
14
  import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serveFile";
@@ -24,7 +24,6 @@ export var ModernServer = /* @__PURE__ */ function() {
24
24
  function ModernServer2(param) {
25
25
  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;
26
26
  _class_call_check(this, ModernServer2);
27
- var _appContext;
28
27
  _define_property(this, "pwd", void 0);
29
28
  _define_property(this, "distDir", void 0);
30
29
  _define_property(this, "workDir", void 0);
@@ -58,7 +57,7 @@ export var ModernServer = /* @__PURE__ */ function() {
58
57
  this.proxyTarget = proxyTarget;
59
58
  this.staticGenerate = staticGenerate || false;
60
59
  this.runMode = runMode || RUN_MODE.FULL;
61
- this.metaName = (_appContext = appContext) === null || _appContext === void 0 ? void 0 : _appContext.metaName;
60
+ this.metaName = appContext === null || appContext === void 0 ? void 0 : appContext.metaName;
62
61
  }
63
62
  _create_class(ModernServer2, [
64
63
  {
@@ -68,7 +67,7 @@ export var ModernServer = /* @__PURE__ */ function() {
68
67
  function onInit(runner, app) {
69
68
  var _this = this;
70
69
  return _async_to_generator(function() {
71
- var _conf_bff, _app, _this_conf_output, distDir, conf, _createProxyHandler, handlers, handleUpgrade, usageRoutes;
70
+ var _conf_bff, _this_conf_output, distDir, conf, _createProxyHandler, handlers, handleUpgrade, usageRoutes;
72
71
  return _ts_generator(this, function(_state) {
73
72
  switch (_state.label) {
74
73
  case 0:
@@ -83,7 +82,7 @@ export var ModernServer = /* @__PURE__ */ function() {
83
82
  _this.addHandler(handler);
84
83
  });
85
84
  }
86
- (_app = app) === null || _app === void 0 ? void 0 : _app.on("close", function() {
85
+ app === null || app === void 0 ? void 0 : app.on("close", function() {
87
86
  _this.reader.close();
88
87
  });
89
88
  usageRoutes = _this.filterRoutes(_this.getRoutes());
@@ -432,13 +431,13 @@ export var ModernServer = /* @__PURE__ */ function() {
432
431
  value: function prepareAPIHandler(extension) {
433
432
  var _this = this;
434
433
  return _async_to_generator(function() {
435
- var _bff, _bff1, workDir, runner, conf, bff, prefix, webOnly;
434
+ var workDir, runner, conf, bff, prefix, webOnly;
436
435
  return _ts_generator(this, function(_state) {
437
436
  switch (_state.label) {
438
437
  case 0:
439
438
  workDir = _this.workDir, runner = _this.runner, conf = _this.conf;
440
439
  bff = conf.bff;
441
- prefix = ((_bff = bff) === null || _bff === void 0 ? void 0 : _bff.prefix) || "/api";
440
+ prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
442
441
  return [
443
442
  4,
444
443
  isWebOnly()
@@ -460,7 +459,7 @@ export var ModernServer = /* @__PURE__ */ function() {
460
459
  pwd: workDir,
461
460
  config: extension,
462
461
  prefix: Array.isArray(prefix) ? prefix[0] : prefix,
463
- httpMethodDecider: (_bff1 = bff) === null || _bff1 === void 0 ? void 0 : _bff1.httpMethodDecider,
462
+ httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
464
463
  render: _this.render.bind(_this)
465
464
  }, {
466
465
  onLast: function() {
@@ -712,17 +711,12 @@ export var ModernServer = /* @__PURE__ */ function() {
712
711
  2
713
712
  ];
714
713
  case 3:
715
- if (!route.entryName)
716
- return [
717
- 3,
718
- 6
719
- ];
720
- afterMatchContext = createAfterMatchContext(context, route.entryName);
721
- if (!(_this.runMode === RUN_MODE.FULL))
714
+ if (!(route.entryName && _this.runMode === RUN_MODE.FULL))
722
715
  return [
723
716
  3,
724
717
  5
725
718
  ];
719
+ afterMatchContext = createAfterMatchContext(context, route.entryName);
726
720
  end1 = time();
727
721
  return [
728
722
  4,
@@ -734,8 +728,6 @@ export var ModernServer = /* @__PURE__ */ function() {
734
728
  _state.sent();
735
729
  cost = end1();
736
730
  cost && reporter.reportTiming(ServerReportTimings.SERVER_HOOK_AFTER_MATCH, cost);
737
- _state.label = 5;
738
- case 5:
739
731
  if (_this.isSend(res)) {
740
732
  return [
741
733
  2
@@ -758,12 +750,12 @@ export var ModernServer = /* @__PURE__ */ function() {
758
750
  }
759
751
  route = matched1.generate(context.url);
760
752
  }
761
- _state.label = 6;
762
- case 6:
753
+ _state.label = 5;
754
+ case 5:
763
755
  if (!_this.frameWebHandler)
764
756
  return [
765
757
  3,
766
- 8
758
+ 7
767
759
  ];
768
760
  res.locals = res.locals || {};
769
761
  middlewareContext = createMiddlewareContext(context);
@@ -772,7 +764,7 @@ export var ModernServer = /* @__PURE__ */ function() {
772
764
  4,
773
765
  _this.frameWebHandler(middlewareContext)
774
766
  ];
775
- case 7:
767
+ case 6:
776
768
  _state.sent();
777
769
  cost1 = end2();
778
770
  cost1 && reporter.reportTiming(ServerReportTimings.SERVER_MIDDLEWARE, cost1);
@@ -782,13 +774,13 @@ export var ModernServer = /* @__PURE__ */ function() {
782
774
  2
783
775
  ];
784
776
  }
785
- _state.label = 8;
786
- case 8:
777
+ _state.label = 7;
778
+ case 7:
787
779
  return [
788
780
  4,
789
781
  _this.handleWeb(context, route)
790
782
  ];
791
- case 9:
783
+ case 8:
792
784
  renderResult = _state.sent();
793
785
  if (!renderResult) {
794
786
  return [
@@ -803,17 +795,12 @@ export var ModernServer = /* @__PURE__ */ function() {
803
795
  2
804
796
  ];
805
797
  }
806
- if (!route.entryName)
798
+ if (!(route.entryName && _this.runMode === RUN_MODE.FULL))
807
799
  return [
808
800
  3,
809
- 12
801
+ 10
810
802
  ];
811
803
  afterRenderContext = createAfterRenderContext(context, response.toString());
812
- if (!(_this.runMode === RUN_MODE.FULL))
813
- return [
814
- 3,
815
- 11
816
- ];
817
804
  end3 = time();
818
805
  return [
819
806
  4,
@@ -821,20 +808,18 @@ export var ModernServer = /* @__PURE__ */ function() {
821
808
  onLast: noop
822
809
  })
823
810
  ];
824
- case 10:
811
+ case 9:
825
812
  _state.sent();
826
813
  cost2 = end3();
827
814
  cost2 && reporter.reportTiming(ServerReportTimings.SERVER_HOOK_AFTER_RENDER, cost2);
828
- _state.label = 11;
829
- case 11:
830
815
  if (_this.isSend(res)) {
831
816
  return [
832
817
  2
833
818
  ];
834
819
  }
835
820
  response = afterRenderContext.template.get();
836
- _state.label = 12;
837
- case 12:
821
+ _state.label = 10;
822
+ case 10:
838
823
  res.end(response);
839
824
  return [
840
825
  2
@@ -848,7 +833,11 @@ export var ModernServer = /* @__PURE__ */ function() {
848
833
  {
849
834
  key: "isSend",
850
835
  value: function isSend(res) {
851
- if (res.writableEnded) {
836
+ if (res.modernFlushedHeaders) {
837
+ if (res.writableFinished) {
838
+ return true;
839
+ }
840
+ } else if (res.headersSent) {
852
841
  return true;
853
842
  }
854
843
  if (res.getHeader("Location") && isRedirect(res.statusCode)) {
@@ -5,8 +5,8 @@ import { _ as _define_property } from "@swc/helpers/_/_define_property";
5
5
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
7
  import { createAsyncPipeline } from "@modern-js/plugin";
8
+ import { Logger } from "@modern-js/utils/logger";
8
9
  import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "./libs/hook-api/index.worker";
9
- import { Logger } from "./libs/logger";
10
10
  import { RouteMatchManager } from "./libs/route";
11
11
  import { metrics as defaultMetrics } from "./libs/metrics";
12
12
  import { defaultReporter } from "./libs/reporter";
@@ -92,7 +92,7 @@ export var createHandler = function(manifest) {
92
92
  routeMgr.reset(routes);
93
93
  return function() {
94
94
  var _ref = _async_to_generator(function(options) {
95
- var _page_serverHooks_afterMatch, _page_serverHooks, _page, request, loadableStats, routeManifest, url, pageMatch, entryName, page, logger, metrics, reporter, hookContext, afterMatchHookContext, _page_serverHooks1, _page_serverHooks_afterRender, _page_serverHooks2, middlewarsHookContext, responseLike, params, baseUrl, serverRenderContext, body, afterRenderHookContext, e;
95
+ var _page_serverHooks_afterMatch, _page_serverHooks, request, loadableStats, routeManifest, url, pageMatch, entryName, page, logger, metrics, reporter, hookContext, afterMatchHookContext, _page_serverHooks1, _page_serverHooks_afterRender, _page_serverHooks2, middlewarsHookContext, responseLike, params, baseUrl, serverRenderContext, body, afterRenderHookContext, e;
96
96
  function createServerRequest(url2, baseUrl2, request2, params2) {
97
97
  var pathname = url2.pathname, host = url2.host, searchParams = url2.searchParams;
98
98
  var rawHeaders = request2.headers;
@@ -132,7 +132,7 @@ export var createHandler = function(manifest) {
132
132
  reporter = defaultReporter;
133
133
  hookContext = createWorkerHookContext(request.url, logger, metrics, reporter);
134
134
  afterMatchHookContext = createAfterMatchContext(hookContext, entryName);
135
- (_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() {
135
+ 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() {
136
136
  return void 0;
137
137
  });
138
138
  if (checkIsSent(hookContext)) {
@@ -186,14 +186,13 @@ export var createHandler = function(manifest) {
186
186
  logger: logger,
187
187
  reporter: defaultReporter,
188
188
  metrics: metrics,
189
- // FIXME: pass correctly req & res
190
189
  req: request,
190
+ res: responseLike,
191
191
  serverTiming: {
192
192
  addServeTiming: function addServeTiming() {
193
193
  return this;
194
194
  }
195
- },
196
- res: responseLike
195
+ }
197
196
  };
198
197
  return [
199
198
  4,
@@ -1,3 +1,6 @@
1
+ import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
2
+ import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
3
+ import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
1
4
  import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
5
  import { URL } from "url";
3
6
  import qs from "querystring";
@@ -9,6 +12,7 @@ import { ServerTiming } from "../serverTiming";
9
12
  import { defaultReporter } from "../reporter";
10
13
  import { headersWithoutCookie } from "../../utils";
11
14
  const MOCK_URL_BASE = "https://modernjs.dev/";
15
+ var _urls = /* @__PURE__ */ new WeakMap(), _queries = /* @__PURE__ */ new WeakMap();
12
16
  export class ModernServerContext {
13
17
  get logger() {
14
18
  return this.req.logger;
@@ -18,7 +22,12 @@ export class ModernServerContext {
18
22
  }
19
23
  get parsedURL() {
20
24
  try {
21
- return new URL(this.req.url, MOCK_URL_BASE);
25
+ let url = _class_private_field_get(this, _urls).get(this.req.url);
26
+ if (!url) {
27
+ url = new URL(this.req.url, MOCK_URL_BASE);
28
+ _class_private_field_get(this, _urls).set(this.req.url, url);
29
+ }
30
+ return url;
22
31
  } catch (e) {
23
32
  this.logger.error("Parse URL error", e.stack || e.message);
24
33
  return new URL("/_modern_mock_path", MOCK_URL_BASE);
@@ -107,7 +116,8 @@ export class ModernServerContext {
107
116
  if (!host) {
108
117
  host = this.getReqHeader("Host");
109
118
  }
110
- return host.split(/\s*,\s*/, 1)[0] || "undefined";
119
+ host = host.split(/\s*,\s*/, 1)[0] || "undefined";
120
+ return host;
111
121
  }
112
122
  get protocol() {
113
123
  if (this.req.socket.encrypted) {
@@ -142,7 +152,12 @@ export class ModernServerContext {
142
152
  }
143
153
  get query() {
144
154
  const str = this.querystring;
145
- return qs.parse(str);
155
+ let query = _class_private_field_get(this, _queries).get(str);
156
+ if (!query) {
157
+ query = qs.parse(str);
158
+ _class_private_field_get(this, _queries).set(str, query);
159
+ }
160
+ return query;
146
161
  }
147
162
  /* response property */
148
163
  get status() {
@@ -161,7 +176,6 @@ export class ModernServerContext {
161
176
  this.logger.error(`Web Server Error - ${dig}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, this.path, headersWithoutCookie(this.headers));
162
177
  }
163
178
  constructor(req, res, options) {
164
- var _options;
165
179
  _define_property(this, "req", void 0);
166
180
  _define_property(this, "res", void 0);
167
181
  _define_property(this, "params", {});
@@ -169,10 +183,20 @@ export class ModernServerContext {
169
183
  _define_property(this, "serverTiming", void 0);
170
184
  _define_property(this, "serverData", {});
171
185
  _define_property(this, "options", {});
186
+ _class_private_field_init(this, _urls, {
187
+ writable: true,
188
+ value: void 0
189
+ });
190
+ _class_private_field_init(this, _queries, {
191
+ writable: true,
192
+ value: void 0
193
+ });
172
194
  this.req = req;
173
195
  this.res = res;
174
196
  this.options = options || {};
175
197
  this.bind();
176
- this.serverTiming = new ServerTiming(this.res, cutNameByHyphen(((_options = options) === null || _options === void 0 ? void 0 : _options.metaName) || "modern-js"));
198
+ _class_private_field_set(this, _urls, /* @__PURE__ */ new Map());
199
+ _class_private_field_set(this, _queries, /* @__PURE__ */ new Map());
200
+ this.serverTiming = new ServerTiming(this.res, cutNameByHyphen((options === null || options === void 0 ? void 0 : options.metaName) || "modern-js"));
177
201
  }
178
202
  }
@@ -14,9 +14,9 @@ export async function flushServerHeader({ serverConf, ctx, distDir, template, he
14
14
  const link = transformLinks2String(links, ssrConf.preload);
15
15
  res.set("link", link);
16
16
  for (const key in headers || {}) {
17
- var _headers;
18
- const value = (_headers = headers) === null || _headers === void 0 ? void 0 : _headers[key];
17
+ const value = headers === null || headers === void 0 ? void 0 : headers[key];
19
18
  value && res.set(key, value);
20
19
  }
21
20
  res.flushHeaders();
21
+ res.modernFlushedHeaders = true;
22
22
  }