@modern-js/server-core 2.58.3-alpha.1 → 2.59.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 (61) hide show
  1. package/dist/cjs/adapters/node/hono.js +13 -6
  2. package/dist/cjs/adapters/node/node.js +2 -2
  3. package/dist/cjs/adapters/node/plugins/resource.js +1 -1
  4. package/dist/cjs/adapters/node/plugins/static.js +1 -1
  5. package/dist/cjs/adapters/node/polyfills/install.js +1 -1
  6. package/dist/cjs/plugins/customServer/base.js +3 -1
  7. package/dist/cjs/plugins/customServer/context.js +1 -1
  8. package/dist/cjs/plugins/customServer/index.js +6 -22
  9. package/dist/cjs/plugins/default.js +2 -2
  10. package/dist/cjs/plugins/render/index.js +5 -3
  11. package/dist/cjs/plugins/render/inject.js +13 -3
  12. package/dist/cjs/plugins/render/render.js +21 -19
  13. package/dist/cjs/plugins/render/ssrRender.js +1 -1
  14. package/dist/cjs/serverBase.js +2 -1
  15. package/dist/esm/adapters/node/helper/loadConfig.js +1 -1
  16. package/dist/esm/adapters/node/hono.js +29 -15
  17. package/dist/esm/adapters/node/node.js +2 -2
  18. package/dist/esm/adapters/node/plugins/resource.js +31 -3
  19. package/dist/esm/adapters/node/plugins/static.js +1 -1
  20. package/dist/esm/adapters/node/polyfills/install.js +3 -3
  21. package/dist/esm/plugins/customServer/base.js +7 -1
  22. package/dist/esm/plugins/customServer/context.js +1 -1
  23. package/dist/esm/plugins/customServer/index.js +8 -17
  24. package/dist/esm/plugins/default.js +3 -3
  25. package/dist/esm/plugins/render/index.js +6 -4
  26. package/dist/esm/plugins/render/inject.js +33 -5
  27. package/dist/esm/plugins/render/render.js +50 -42
  28. package/dist/esm/plugins/render/ssrRender.js +2 -2
  29. package/dist/esm/serverBase.js +2 -1
  30. package/dist/esm-node/adapters/node/helper/loadConfig.js +1 -1
  31. package/dist/esm-node/adapters/node/hono.js +13 -6
  32. package/dist/esm-node/adapters/node/node.js +2 -2
  33. package/dist/esm-node/adapters/node/plugins/resource.js +2 -2
  34. package/dist/esm-node/adapters/node/plugins/static.js +1 -1
  35. package/dist/esm-node/adapters/node/polyfills/install.js +3 -3
  36. package/dist/esm-node/plugins/customServer/base.js +3 -1
  37. package/dist/esm-node/plugins/customServer/context.js +1 -1
  38. package/dist/esm-node/plugins/customServer/index.js +6 -12
  39. package/dist/esm-node/plugins/default.js +3 -3
  40. package/dist/esm-node/plugins/render/index.js +5 -3
  41. package/dist/esm-node/plugins/render/inject.js +13 -3
  42. package/dist/esm-node/plugins/render/render.js +21 -19
  43. package/dist/esm-node/plugins/render/ssrRender.js +2 -2
  44. package/dist/esm-node/serverBase.js +2 -1
  45. package/dist/types/adapters/node/hono.d.ts +4 -2
  46. package/dist/types/adapters/node/plugins/nodeServer.d.ts +1 -1
  47. package/dist/types/adapters/node/plugins/static.d.ts +1 -1
  48. package/dist/types/pluginManager.d.ts +1 -1
  49. package/dist/types/plugins/customServer/context.d.ts +2 -2
  50. package/dist/types/plugins/customServer/index.d.ts +2 -2
  51. package/dist/types/plugins/render/inject.d.ts +1 -1
  52. package/dist/types/plugins/render/render.d.ts +2 -7
  53. package/dist/types/plugins/render/ssrRender.d.ts +2 -2
  54. package/dist/types/serverBase.d.ts +1 -0
  55. package/dist/types/types/config/bff.d.ts +1 -1
  56. package/dist/types/types/config/server.d.ts +0 -23
  57. package/dist/types/types/plugin.d.ts +8 -2
  58. package/dist/types/types/render.d.ts +2 -0
  59. package/dist/types/types/requestHandler.d.ts +1 -1
  60. package/dist/types/types/server.d.ts +2 -1
  61. package/package.json +7 -7
@@ -2,11 +2,11 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { time } from "@modern-js/runtime-utils/time";
5
- import { transformResponse } from "../../utils";
6
- import { getLoaderCtx } from "../../helper";
7
5
  import { ServerTimings } from "../../constants";
8
- import { getAfterMatchCtx, getAfterRenderCtx, createCustomMiddlewaresCtx, createAfterStreamingRenderContext } from "./context";
6
+ import { getLoaderCtx } from "../../helper";
7
+ import { transformResponse } from "../../utils";
9
8
  import { createBaseHookContext } from "./base";
9
+ import { createAfterStreamingRenderContext, createCustomMiddlewaresCtx, getAfterMatchCtx, getAfterRenderCtx } from "./context";
10
10
  var noop = function() {
11
11
  };
12
12
  var isHtmlResponse = function(response) {
@@ -36,7 +36,7 @@ var CustomServer = /* @__PURE__ */ function() {
36
36
  var _this = this;
37
37
  return function() {
38
38
  var _ref = _async_to_generator(function(c, next) {
39
- var routeInfo, monitors, baseHookCtx, afterMatchCtx, getCost, cost, _afterMatchCtx_router, url, status, current, rewriteRoute, _c_req_raw, headers, method, signal, url1, newUrl, afterStreamingRenderContext, afterRenderCtx, getCost1, cost1, newBody;
39
+ var routeInfo, monitors, baseHookCtx, afterMatchCtx, getCost, cost, _afterMatchCtx_router, url, status, current, rewriteRoute, afterStreamingRenderContext, afterRenderCtx, getCost1, cost1, newBody;
40
40
  return _ts_generator(this, function(_state) {
41
41
  switch (_state.label) {
42
42
  case 0:
@@ -70,17 +70,7 @@ var CustomServer = /* @__PURE__ */ function() {
70
70
  return route.entryName === current;
71
71
  });
72
72
  if (rewriteRoute) {
73
- _c_req_raw = c.req.raw, headers = _c_req_raw.headers, method = _c_req_raw.method, signal = _c_req_raw.signal;
74
- url1 = new URL(c.req.url);
75
- newUrl = new URL(rewriteRoute.urlPath, url1.origin);
76
- return [
77
- 2,
78
- _this.serverBase.request(newUrl, {
79
- headers,
80
- method,
81
- signal
82
- }, c.env)
83
- ];
73
+ c.set("matchPathname", rewriteRoute.urlPath);
84
74
  }
85
75
  }
86
76
  if (c.finalized) {
@@ -288,7 +278,7 @@ function createMiddlewareContextFromHono(c) {
288
278
  }
289
279
  function _createMiddlewareContextFromHono() {
290
280
  _createMiddlewareContextFromHono = _async_to_generator(function(c) {
291
- var loaderContext, rawRequest, method, createReadableStreamFromReadable, init;
281
+ var loaderContext, rawRequest, method, streamModulePath, createReadableStreamFromReadable, init;
292
282
  return _ts_generator(this, function(_state) {
293
283
  switch (_state.label) {
294
284
  case 0:
@@ -303,9 +293,10 @@ function _createMiddlewareContextFromHono() {
303
293
  3,
304
294
  2
305
295
  ];
296
+ streamModulePath = "../../adapters/node/polyfills/stream.js";
306
297
  return [
307
298
  4,
308
- import("../../adapters/node/polyfills/stream.js")
299
+ import(streamModulePath)
309
300
  ];
310
301
  case 1:
311
302
  createReadableStreamFromReadable = _state.sent().createReadableStreamFromReadable;
@@ -1,7 +1,7 @@
1
- import { injectRenderHandlerPlugin } from "./render";
2
- import { initMonitorsPlugin, injectloggerPluigin, injectServerTiming } from "./monitors";
3
- import { processedByPlugin } from "./processedBy";
4
1
  import { logPlugin } from "./log";
2
+ import { initMonitorsPlugin, injectServerTiming, injectloggerPluigin } from "./monitors";
3
+ import { processedByPlugin } from "./processedBy";
4
+ import { injectRenderHandlerPlugin } from "./render";
5
5
  function createDefaultPlugins() {
6
6
  var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
7
7
  var plugins = [
@@ -1,10 +1,10 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
4
- import { initReporter } from "../monitors";
4
+ import { getLoaderCtx } from "../../helper";
5
5
  import { sortRoutes } from "../../utils";
6
6
  import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
7
- import { getLoaderCtx } from "../../helper";
7
+ import { initReporter } from "../monitors";
8
8
  export * from "./inject";
9
9
  var renderPlugin = function() {
10
10
  return {
@@ -136,7 +136,7 @@ function getPageRoutes(routes) {
136
136
  function createRenderHandler(render) {
137
137
  return function() {
138
138
  var _ref = _async_to_generator(function(c, _) {
139
- var _c_env_node, logger, reporter, monitors, templates, serverManifest, locals, metrics, loaderContext, request, nodeReq, res, body, status, headers, headersData;
139
+ var _c_env_node, logger, reporter, monitors, templates, serverManifest, locals, metrics, matchPathname, loaderContext, request, nodeReq, res, body, status, headers, headersData;
140
140
  return _ts_generator(this, function(_state) {
141
141
  switch (_state.label) {
142
142
  case 0:
@@ -147,6 +147,7 @@ function createRenderHandler(render) {
147
147
  serverManifest = c.get("serverManifest") || {};
148
148
  locals = c.get("locals");
149
149
  metrics = c.get("metrics");
150
+ matchPathname = c.get("matchPathname");
150
151
  loaderContext = getLoaderCtx(c);
151
152
  request = c.req.raw;
152
153
  nodeReq = (_c_env_node = c.env.node) === null || _c_env_node === void 0 ? void 0 : _c_env_node.req;
@@ -161,7 +162,8 @@ function createRenderHandler(render) {
161
162
  metrics,
162
163
  serverManifest,
163
164
  loaderContext,
164
- locals
165
+ locals,
166
+ matchPathname
165
167
  })
166
168
  ];
167
169
  case 1:
@@ -11,24 +11,51 @@ var injectRenderHandlerPlugin = function(param) {
11
11
  return {
12
12
  prepare: function prepare() {
13
13
  return _async_to_generator(function() {
14
- var _config_render, _api_useAppContext, pwd, routes, metaName, config, getRenderHandlerOptions, render;
14
+ var _config_render, _api_useAppContext, pwd, routes, metaName, config, hookRunner, onFallback, getRenderHandlerOptions, render;
15
15
  return _ts_generator(this, function(_state) {
16
16
  switch (_state.label) {
17
17
  case 0:
18
18
  _api_useAppContext = api.useAppContext(), pwd = _api_useAppContext.distDirectory, routes = _api_useAppContext.routes, metaName = _api_useAppContext.metaName;
19
19
  config = api.useConfigContext();
20
+ hookRunner = api.useHookRunners();
20
21
  if (!routes) {
21
22
  return [
22
23
  2
23
24
  ];
24
25
  }
26
+ onFallback = function() {
27
+ var _ref = _async_to_generator(function(reason, utils, error) {
28
+ return _ts_generator(this, function(_state2) {
29
+ switch (_state2.label) {
30
+ case 0:
31
+ return [
32
+ 4,
33
+ hookRunner.fallback(_object_spread_props(_object_spread({
34
+ reason
35
+ }, utils), {
36
+ error
37
+ }))
38
+ ];
39
+ case 1:
40
+ _state2.sent();
41
+ return [
42
+ 2
43
+ ];
44
+ }
45
+ });
46
+ });
47
+ return function onFallback2(reason, utils, error) {
48
+ return _ref.apply(this, arguments);
49
+ };
50
+ }();
25
51
  getRenderHandlerOptions = {
26
52
  pwd,
27
53
  routes,
28
54
  config,
29
55
  metaName,
30
56
  cacheConfig: ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.cache) || cacheConfig,
31
- staticGenerate
57
+ staticGenerate,
58
+ onFallback
32
59
  };
33
60
  return [
34
61
  4,
@@ -56,9 +83,9 @@ function getRenderHandler(_) {
56
83
  }
57
84
  function _getRenderHandler() {
58
85
  _getRenderHandler = _async_to_generator(function(param) {
59
- var pwd, routes, config, cacheConfig, metaName, staticGenerate, _config_server, _config_security, ssrConfig, forceCSR, render;
86
+ var pwd, routes, config, cacheConfig, metaName, staticGenerate, onFallback, _config_server, _config_security, ssrConfig, forceCSR, render;
60
87
  return _ts_generator(this, function(_state) {
61
- pwd = param.pwd, routes = param.routes, config = param.config, cacheConfig = param.cacheConfig, metaName = param.metaName, staticGenerate = param.staticGenerate;
88
+ pwd = param.pwd, routes = param.routes, config = param.config, cacheConfig = param.cacheConfig, metaName = param.metaName, staticGenerate = param.staticGenerate, onFallback = param.onFallback;
62
89
  ssrConfig = (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr;
63
90
  forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
64
91
  render = createRender({
@@ -69,7 +96,8 @@ function _getRenderHandler() {
69
96
  cacheConfig,
70
97
  forceCSR,
71
98
  nonce: (_config_security = config.security) === null || _config_security === void 0 ? void 0 : _config_security.nonce,
72
- metaName: metaName || "modern-js"
99
+ metaName: metaName || "modern-js",
100
+ onFallback
73
101
  });
74
102
  return [
75
103
  2,
@@ -8,11 +8,10 @@ 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 { cutNameByHyphen } from "@modern-js/utils/universal";
10
10
  import { TrieRouter } from "hono/router/trie-router";
11
- import { parseQuery, getPathname, createErrorHtml, sortRoutes, transformResponse, onError as onErrorFn, ErrorDigest, parseHeaders } from "../../utils";
12
11
  import { REPLACE_REG, X_MODERNJS_RENDER } from "../../constants";
12
+ import { ErrorDigest, createErrorHtml, getPathname, getRuntimeEnv, onError as onErrorFn, parseHeaders, parseQuery, sortRoutes, transformResponse } from "../../utils";
13
13
  import { dataHandler } from "./dataHandler";
14
14
  import { ssrRender } from "./ssrRender";
15
- import { compatibleRequire } from "@modern-js/utils";
16
15
  var DYNAMIC_ROUTE_REG = /\/:./;
17
16
  function getRouter(routes) {
18
17
  var dynamicRoutes = [];
@@ -50,8 +49,7 @@ function getRouter(routes) {
50
49
  }
51
50
  return router;
52
51
  }
53
- function matchRoute(router, request) {
54
- var pathname = getPathname(request);
52
+ function matchRoute(router, pathname) {
55
53
  var matched = router.match("*", pathname);
56
54
  var result = matched[0][0];
57
55
  return result || [];
@@ -76,15 +74,20 @@ function _createRender() {
76
74
  2,
77
75
  function() {
78
76
  var _ref = _async_to_generator(function(req, param2) {
79
- var logger, reporter, metrics, monitors, nodeReq, templates, serverManifest, locals, loaderContext, _matchRoute, routeInfo, params, onFallback, html, renderMode, pathname, headerData, onError, onTiming, onBoundError, renderOptions, response, _tmp;
77
+ var logger, reporter, metrics, monitors, nodeReq, templates, serverManifest, locals, matchPathname, loaderContext, forMatchpathname, _matchRoute, routeInfo, params, framework, fallbackHeader, fallbackReason, onFallback, html, renderMode, headerData, onError, onTiming, onBoundError, renderOptions, response, _tmp;
80
78
  return _ts_generator(this, function(_state2) {
81
79
  switch (_state2.label) {
82
80
  case 0:
83
- logger = param2.logger, reporter = param2.reporter, metrics = param2.metrics, monitors = param2.monitors, nodeReq = param2.nodeReq, templates = param2.templates, serverManifest = param2.serverManifest, locals = param2.locals, loaderContext = param2.loaderContext;
84
- _matchRoute = _sliced_to_array(matchRoute(router, req), 2), routeInfo = _matchRoute[0], params = _matchRoute[1];
81
+ logger = param2.logger, reporter = param2.reporter, metrics = param2.metrics, monitors = param2.monitors, nodeReq = param2.nodeReq, templates = param2.templates, serverManifest = param2.serverManifest, locals = param2.locals, matchPathname = param2.matchPathname, loaderContext = param2.loaderContext;
82
+ forMatchpathname = matchPathname !== null && matchPathname !== void 0 ? matchPathname : getPathname(req);
83
+ _matchRoute = _sliced_to_array(matchRoute(router, forMatchpathname), 2), routeInfo = _matchRoute[0], params = _matchRoute[1];
84
+ framework = metaName || "modern-js";
85
+ fallbackHeader = "x-".concat(cutNameByHyphen(framework), "-ssr-fallback");
86
+ fallbackReason = null;
85
87
  onFallback = function() {
86
88
  var _ref2 = _async_to_generator(function(reason, error) {
87
89
  return _ts_generator(this, function(_state3) {
90
+ fallbackReason = reason;
88
91
  return [
89
92
  2,
90
93
  onFallbackFn === null || onFallbackFn === void 0 ? void 0 : onFallbackFn(reason, {
@@ -124,14 +127,13 @@ function _createRender() {
124
127
  }
125
128
  return [
126
129
  4,
127
- getRenderMode(req, metaName || "modern-js", routeInfo.isSSR, forceCSR, nodeReq, onFallback)
130
+ getRenderMode(req, fallbackHeader, routeInfo.isSSR, forceCSR, nodeReq, onFallback)
128
131
  ];
129
132
  case 1:
130
133
  renderMode = _state2.sent();
131
- pathname = getPathname(req);
132
134
  headerData = parseHeaders(req);
133
135
  onError = function(e) {
134
- monitors === null || monitors === void 0 ? void 0 : monitors.error("SSR Error - ".concat(_instanceof(e, Error) ? e.name : e, ", error = %s, req.url = %s, req.headers = %o"), _instanceof(e, Error) ? e.stack || e.message : e, pathname, getHeadersWithoutCookie(headerData));
136
+ monitors === null || monitors === void 0 ? void 0 : monitors.error("SSR Error - ".concat(_instanceof(e, Error) ? e.name : e, ", error = %s, req.url = %s, req.headers = %o"), _instanceof(e, Error) ? e.stack || e.message : e, forMatchpathname, getHeadersWithoutCookie(headerData));
135
137
  };
136
138
  onTiming = function(name, dur) {
137
139
  monitors === null || monitors === void 0 ? void 0 : monitors.timing(name, dur, "SSR");
@@ -237,6 +239,9 @@ function _createRender() {
237
239
  case 8:
238
240
  throw new Error("Unknown render mode: ".concat(renderMode));
239
241
  case 9:
242
+ if (fallbackReason) {
243
+ response.headers.set(fallbackHeader, "1;reason=".concat(fallbackReason));
244
+ }
240
245
  return [
241
246
  2,
242
247
  response
@@ -258,7 +263,7 @@ function renderHandler(request, options, mode, onError) {
258
263
  }
259
264
  function _renderHandler() {
260
265
  _renderHandler = _async_to_generator(function(request, options, mode, onError) {
261
- var _options_config_server, serverData, response, serverManifest, ssrByRouteIds, nestedRoutesJson, routes, matchRoutes, url, matchedRoutes, _lastMatch_route, lastMatch, e, newRes, routeInfo;
266
+ var _options_config_server, serverData, response, serverManifest, ssrByRouteIds, runtimeEnv, nestedRoutesJson, routes, urlPath, pathToFileURL, matchRoutes, url, matchedRoutes, _lastMatch_route, lastMatch, e, newRes, routeInfo;
262
267
  function applyExtendHeaders(r, route) {
263
268
  Object.entries(route.responseHeaders || {}).forEach(function(param) {
264
269
  var _param = _sliced_to_array(param, 2), k = _param[0], v = _param[1];
@@ -277,23 +282,31 @@ function _renderHandler() {
277
282
  response = null;
278
283
  serverManifest = options.serverManifest;
279
284
  ssrByRouteIds = (_options_config_server = options.config.server) === null || _options_config_server === void 0 ? void 0 : _options_config_server.ssrByRouteIds;
280
- if (!(serverManifest.nestedRoutesJson && ssrByRouteIds && (ssrByRouteIds === null || ssrByRouteIds === void 0 ? void 0 : ssrByRouteIds.length) > 0))
285
+ runtimeEnv = getRuntimeEnv();
286
+ if (!(serverManifest.nestedRoutesJson && ssrByRouteIds && (ssrByRouteIds === null || ssrByRouteIds === void 0 ? void 0 : ssrByRouteIds.length) > 0 && runtimeEnv === "node"))
281
287
  return [
282
288
  3,
283
- 2
289
+ 3
284
290
  ];
285
291
  nestedRoutesJson = serverManifest.nestedRoutesJson;
286
292
  routes = nestedRoutesJson === null || nestedRoutesJson === void 0 ? void 0 : nestedRoutesJson[options.routeInfo.entryName];
287
293
  if (!routes)
288
294
  return [
289
295
  3,
290
- 2
296
+ 3
291
297
  ];
298
+ urlPath = "node:url";
292
299
  return [
293
300
  4,
294
- compatibleRequire(require.resolve("@modern-js/runtime-utils/remix-router"), false)
301
+ import(urlPath)
295
302
  ];
296
303
  case 1:
304
+ pathToFileURL = _state.sent().pathToFileURL;
305
+ return [
306
+ 4,
307
+ import(pathToFileURL(require.resolve("@modern-js/runtime-utils/remix-router")).href)
308
+ ];
309
+ case 2:
297
310
  matchRoutes = _state.sent().matchRoutes;
298
311
  url = new URL(request.url);
299
312
  matchedRoutes = matchRoutes(routes, url.pathname, options.routeInfo.urlPath);
@@ -306,58 +319,54 @@ function _renderHandler() {
306
319
  response = csrRender(options.html);
307
320
  }
308
321
  }
309
- _state.label = 2;
310
- case 2:
322
+ _state.label = 3;
323
+ case 3:
311
324
  if (!(mode === "ssr" && !response))
312
325
  return [
313
326
  3,
314
- 8
327
+ 9
315
328
  ];
316
- _state.label = 3;
317
- case 3:
329
+ _state.label = 4;
330
+ case 4:
318
331
  _state.trys.push([
319
- 3,
320
- 5,
332
+ 4,
333
+ 6,
321
334
  ,
322
- 7
335
+ 8
323
336
  ]);
324
337
  return [
325
338
  4,
326
339
  ssrRender(request, options)
327
340
  ];
328
- case 4:
341
+ case 5:
329
342
  response = _state.sent();
330
343
  return [
331
344
  3,
332
- 7
345
+ 8
333
346
  ];
334
- case 5:
347
+ case 6:
335
348
  e = _state.sent();
336
349
  return [
337
350
  4,
338
351
  onError(e)
339
352
  ];
340
- case 6:
353
+ case 7:
341
354
  _state.sent();
342
355
  response = csrRender(options.html);
343
356
  return [
344
357
  3,
345
- 7
358
+ 8
346
359
  ];
347
- case 7:
360
+ case 8:
348
361
  return [
349
362
  3,
350
- 9
363
+ 10
351
364
  ];
352
- case 8:
353
- response = csrRender(options.html);
354
- _state.label = 9;
355
365
  case 9:
356
- if (!options.staticGenerate) {
357
- newRes = transformResponse(response, injectServerData(serverData));
358
- } else {
359
- newRes = response;
360
- }
366
+ response = csrRender(options.html);
367
+ _state.label = 10;
368
+ case 10:
369
+ newRes = transformResponse(response, injectServerData(serverData));
361
370
  routeInfo = options.routeInfo;
362
371
  applyExtendHeaders(newRes, routeInfo);
363
372
  return [
@@ -369,17 +378,16 @@ function _renderHandler() {
369
378
  });
370
379
  return _renderHandler.apply(this, arguments);
371
380
  }
372
- function getRenderMode(req, framework, isSSR, forceCSR, nodeReq, onFallback) {
381
+ function getRenderMode(req, fallbackHeader, isSSR, forceCSR, nodeReq, onFallback) {
373
382
  return _getRenderMode.apply(this, arguments);
374
383
  }
375
384
  function _getRenderMode() {
376
- _getRenderMode = _async_to_generator(function(req, framework, isSSR, forceCSR, nodeReq, onFallback) {
377
- var query, fallbackHeader;
385
+ _getRenderMode = _async_to_generator(function(req, fallbackHeader, isSSR, forceCSR, nodeReq, onFallback) {
386
+ var query;
378
387
  return _ts_generator(this, function(_state) {
379
388
  switch (_state.label) {
380
389
  case 0:
381
390
  query = parseQuery(req);
382
- fallbackHeader = "x-".concat(cutNameByHyphen(framework), "-ssr-fallback");
383
391
  if (!isSSR)
384
392
  return [
385
393
  3,
@@ -2,9 +2,9 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
5
- import { parseHeaders, getPathname } from "../../utils";
6
5
  import { X_MODERNJS_RENDER } from "../../constants";
7
- import { matchCacheControl, getCacheResult } from "./ssrCache";
6
+ import { getPathname, parseHeaders } from "../../utils";
7
+ import { getCacheResult, matchCacheControl } from "./ssrCache";
8
8
  var SERVER_RUNTIME_ENTRY = "requestHandler";
9
9
  function ssrRender(request, _) {
10
10
  return _ssrRender.apply(this, arguments);
@@ -5,8 +5,8 @@ import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_m
5
5
  import { _ as _create_class } from "@swc/helpers/_/_create_class";
6
6
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
7
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
- import { Hono } from "hono";
9
8
  import { createContext } from "@modern-js/plugin";
9
+ import { Hono } from "hono";
10
10
  import { PluginManager } from "./pluginManager";
11
11
  var _getAppContext = /* @__PURE__ */ new WeakSet(), _applyMiddlewares = /* @__PURE__ */ new WeakSet();
12
12
  var ServerBase = /* @__PURE__ */ function() {
@@ -136,6 +136,7 @@ function getAppContext() {
136
136
  middlewares: [],
137
137
  appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
138
138
  apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
139
+ internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
139
140
  lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
140
141
  sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
141
142
  distDirectory: pwd,
@@ -1,5 +1,5 @@
1
1
  import path from "path";
2
- import { fs, DEFAULT_SERVER_CONFIG, requireExistModule, ensureAbsolutePath, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire } from "@modern-js/utils";
2
+ import { fs, DEFAULT_SERVER_CONFIG, OUTPUT_CONFIG_FILE, lodash as _, compatibleRequire, ensureAbsolutePath, requireExistModule } from "@modern-js/utils";
3
3
  import { parse } from "flatted";
4
4
  const requireConfig = async (serverConfigPath) => {
5
5
  if (fs.pathExistsSync(serverConfigPath)) {
@@ -1,15 +1,22 @@
1
1
  const httpCallBack2HonoMid = (handler) => {
2
2
  return async (context, next) => {
3
3
  const { req, res } = context.env.node;
4
+ const onPipe = () => {
5
+ res._modernBodyPiped = true;
6
+ };
7
+ res.once("pipe", onPipe);
4
8
  req.__honoRequest = context.req;
5
9
  req.__templates = context.get("templates") || {};
6
10
  req.__serverManifest = context.get("serverManifest") || {};
7
- await handler(req, res);
8
- await new Promise((resolve) => setTimeout(resolve, 0));
9
- delete req.__honoRequest;
10
- delete req.__templates;
11
- delete req.__serverManifest;
12
- if (res.headersSent) {
11
+ try {
12
+ await handler(req, res);
13
+ } finally {
14
+ delete req.__honoRequest;
15
+ delete req.__templates;
16
+ delete req.__serverManifest;
17
+ res.removeListener("pipe", onPipe);
18
+ }
19
+ if (res.headersSent || res._modernBodyPiped) {
13
20
  context.finalized = true;
14
21
  } else {
15
22
  await next();
@@ -1,6 +1,6 @@
1
1
  import { ServerResponse } from "node:http";
2
- import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
3
2
  import { installGlobals } from "./polyfills/install";
3
+ import { createReadableStreamFromReadable, writeReadableStreamToWritable } from "./polyfills/stream";
4
4
  import { writeReadableStreamToWritable as writeReadableStreamToWritable2 } from "./polyfills";
5
5
  installGlobals();
6
6
  const createWebRequest = (req, res, body) => {
@@ -80,7 +80,7 @@ const getRequestListener = (handler) => {
80
80
  res
81
81
  }
82
82
  });
83
- if (!res.headersSent && !response.res) {
83
+ if (!res.headersSent && !response.res && !res._modernBodyPiped) {
84
84
  await sendResponse(response, res);
85
85
  }
86
86
  } catch (error) {
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
2
  import { fileReader } from "@modern-js/runtime-utils/fileReader";
3
- import { LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, fs, compatibleRequire } from "@modern-js/utils";
3
+ import { fs, LOADABLE_STATS_FILE, MAIN_ENTRY_NAME, NESTED_ROUTE_SPEC_FILE, ROUTE_MANIFEST_FILE, SERVER_BUNDLE_DIRECTORY, compatibleRequire } from "@modern-js/utils";
4
4
  async function getHtmlTemplates(pwd, routes) {
5
5
  const htmls = await Promise.all(routes.map(async (route) => {
6
6
  let html;
@@ -49,7 +49,7 @@ async function getServerManifest(pwd, routes, logger) {
49
49
  const renderBundle = await loadBundle(renderBundlePath, logger);
50
50
  const loaderBundle = await loadBundle(loaderBundlePath, logger);
51
51
  renderBundle && (renderBundles[entryName] = renderBundle);
52
- loaderBundle && (loaderBundles[entryName] = loaderBundle);
52
+ loaderBundle && (loaderBundles[entryName] = (loaderBundle === null || loaderBundle === void 0 ? void 0 : loaderBundle.loadModules) ? await (loaderBundle === null || loaderBundle === void 0 ? void 0 : loaderBundle.loadModules()) : loaderBundle);
53
53
  }));
54
54
  const loadableUri = path.join(pwd, LOADABLE_STATS_FILE);
55
55
  const loadableStats = await compatibleRequire(loadableUri).catch((_) => ({}));
@@ -1,7 +1,7 @@
1
1
  import path from "path";
2
+ import { fileReader } from "@modern-js/runtime-utils/fileReader";
2
3
  import { fs } from "@modern-js/utils";
3
4
  import { getMimeType } from "hono/utils/mime";
4
- import { fileReader } from "@modern-js/runtime-utils/fileReader";
5
5
  import { sortRoutes } from "../../../utils";
6
6
  const serverStaticPlugin = () => ({
7
7
  name: "@modern-js/plugin-server-static",
@@ -1,6 +1,6 @@
1
- import { Request as NodeRequest, Response as NodeResponse, Headers as NodeHeaders, FormData as NodeFormData } from "@web-std/fetch";
2
- import { TransformStream as NodeTransformStream, ReadableStream as NodeReadableStream, WritableStream as NodeWritableStream } from "@web-std/stream";
3
- import { File as NodeFile, Blob as NodeBlob } from "@web-std/file";
1
+ import { FormData as NodeFormData, Headers as NodeHeaders, Request as NodeRequest, Response as NodeResponse } from "@web-std/fetch";
2
+ import { Blob as NodeBlob, File as NodeFile } from "@web-std/file";
3
+ import { ReadableStream as NodeReadableStream, TransformStream as NodeTransformStream, WritableStream as NodeWritableStream } from "@web-std/stream";
4
4
  if (!Object.getOwnPropertyDescriptor(NodeHeaders.prototype, "getSetCookie")) {
5
5
  Object.defineProperty(NodeHeaders.prototype, "getSetCookie", {
6
6
  value: function getSetCookie() {
@@ -16,7 +16,9 @@ function createBaseHookContext(c, resParams) {
16
16
  var _req = /* @__PURE__ */ new WeakMap(), _c = /* @__PURE__ */ new WeakMap(), _headersData = /* @__PURE__ */ new WeakMap(), _headers = /* @__PURE__ */ new WeakMap();
17
17
  class BaseHookRequest {
18
18
  get url() {
19
- return _class_private_field_get(this, _req).path;
19
+ const query = _class_private_field_get(this, _c).req.query();
20
+ const q = Object.entries(query).map(([key, value]) => `${key}=${value}`).join("&");
21
+ return q ? `${_class_private_field_get(this, _c).req.path}?${q}` : _class_private_field_get(this, _c).req.path;
20
22
  }
21
23
  // TODO: remove next major version
22
24
  set url(_u) {
@@ -1,6 +1,6 @@
1
+ import { createBaseHookContext } from "./base";
1
2
  import { RouterAPI } from "./routerApi";
2
3
  import { TemplateApi } from "./template";
3
- import { createBaseHookContext } from "./base";
4
4
  function getAfterMatchCtx(entryName, baseHookCtx) {
5
5
  const afterMatchCtx = baseHookCtx;
6
6
  afterMatchCtx.router = new RouterAPI(entryName);
@@ -1,9 +1,9 @@
1
1
  import { time } from "@modern-js/runtime-utils/time";
2
- import { transformResponse } from "../../utils";
3
- import { getLoaderCtx } from "../../helper";
4
2
  import { ServerTimings } from "../../constants";
5
- import { getAfterMatchCtx, getAfterRenderCtx, createCustomMiddlewaresCtx, createAfterStreamingRenderContext } from "./context";
3
+ import { getLoaderCtx } from "../../helper";
4
+ import { transformResponse } from "../../utils";
6
5
  import { createBaseHookContext } from "./base";
6
+ import { createAfterStreamingRenderContext, createCustomMiddlewaresCtx, getAfterMatchCtx, getAfterRenderCtx } from "./context";
7
7
  const noop = () => {
8
8
  };
9
9
  const isHtmlResponse = (response) => {
@@ -31,14 +31,7 @@ class CustomServer {
31
31
  if (current !== entryName) {
32
32
  const rewriteRoute = routes.find((route) => route.entryName === current);
33
33
  if (rewriteRoute) {
34
- const { headers, method, signal } = c.req.raw;
35
- const url2 = new URL(c.req.url);
36
- const newUrl = new URL(rewriteRoute.urlPath, url2.origin);
37
- return this.serverBase.request(newUrl, {
38
- headers,
39
- method,
40
- signal
41
- }, c.env);
34
+ c.set("matchPathname", rewriteRoute.urlPath);
42
35
  }
43
36
  }
44
37
  if (c.finalized) {
@@ -148,7 +141,8 @@ async function createMiddlewareContextFromHono(c) {
148
141
  "GET",
149
142
  "HEAD"
150
143
  ].includes(method) && !rawRequest.body && c.env.node.req) {
151
- const { createReadableStreamFromReadable } = await import("../../adapters/node/polyfills/stream.js");
144
+ const streamModulePath = "../../adapters/node/polyfills/stream.js";
145
+ const { createReadableStreamFromReadable } = await import(streamModulePath);
152
146
  const init = {
153
147
  body: createReadableStreamFromReadable(c.env.node.req),
154
148
  headers: rawRequest.headers,
@@ -1,7 +1,7 @@
1
- import { injectRenderHandlerPlugin } from "./render";
2
- import { initMonitorsPlugin, injectloggerPluigin, injectServerTiming } from "./monitors";
3
- import { processedByPlugin } from "./processedBy";
4
1
  import { logPlugin } from "./log";
2
+ import { initMonitorsPlugin, injectServerTiming, injectloggerPluigin } from "./monitors";
3
+ import { processedByPlugin } from "./processedBy";
4
+ import { injectRenderHandlerPlugin } from "./render";
5
5
  function createDefaultPlugins(options = {}) {
6
6
  const plugins = [
7
7
  initMonitorsPlugin(),
@@ -1,8 +1,8 @@
1
1
  import { MAIN_ENTRY_NAME } from "@modern-js/utils/universal/constants";
2
- import { initReporter } from "../monitors";
2
+ import { getLoaderCtx } from "../../helper";
3
3
  import { sortRoutes } from "../../utils";
4
4
  import { CustomServer, getServerMidFromUnstableMid } from "../customServer";
5
- import { getLoaderCtx } from "../../helper";
5
+ import { initReporter } from "../monitors";
6
6
  export * from "./inject";
7
7
  const renderPlugin = () => ({
8
8
  name: "@modern-js/plugin-render",
@@ -61,6 +61,7 @@ function createRenderHandler(render) {
61
61
  const serverManifest = c.get("serverManifest") || {};
62
62
  const locals = c.get("locals");
63
63
  const metrics = c.get("metrics");
64
+ const matchPathname = c.get("matchPathname");
64
65
  const loaderContext = getLoaderCtx(c);
65
66
  const request = c.req.raw;
66
67
  const nodeReq = (_c_env_node = c.env.node) === null || _c_env_node === void 0 ? void 0 : _c_env_node.req;
@@ -73,7 +74,8 @@ function createRenderHandler(render) {
73
74
  metrics,
74
75
  serverManifest,
75
76
  loaderContext,
76
- locals
77
+ locals,
78
+ matchPathname
77
79
  });
78
80
  const { body, status, headers } = res;
79
81
  const headersData = {};