@bleedingdev/modern-js-plugin-tanstack 3.5.0-ultramodern.3 → 3.5.0-ultramodern.31

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 (230) hide show
  1. package/dist/cjs/cli/artifacts.js +157 -0
  2. package/dist/cjs/cli/index.js +8 -79
  3. package/dist/cjs/cli/tanstackTypes/canonicalRoutes.js +113 -0
  4. package/dist/cjs/cli/tanstackTypes/shared.js +130 -0
  5. package/dist/cjs/cli/tanstackTypes/source.js +251 -0
  6. package/dist/cjs/cli/tanstackTypes.js +4 -350
  7. package/dist/cjs/runtime/blockingSubscribe.js +64 -0
  8. package/dist/cjs/runtime/{pluginCore.js → clientBasepath.js} +12 -19
  9. package/dist/cjs/runtime/clientHydration.js +129 -0
  10. package/dist/cjs/runtime/dataMutation.js +11 -176
  11. package/dist/cjs/runtime/formData.js +116 -0
  12. package/dist/cjs/runtime/loaderBridge.js +4 -1
  13. package/dist/cjs/runtime/plugin.js +21 -125
  14. package/dist/cjs/runtime/plugin.node.js +45 -125
  15. package/dist/cjs/runtime/pluginShared.js +90 -0
  16. package/dist/cjs/runtime/prefetchLink.js +15 -2
  17. package/dist/cjs/runtime/routeTree/components.js +102 -0
  18. package/dist/cjs/runtime/routeTree/construction.js +152 -0
  19. package/dist/cjs/runtime/routeTree/index.js +47 -0
  20. package/dist/cjs/runtime/routeTree/loaders.js +158 -0
  21. package/dist/cjs/runtime/routeTree/paths.js +57 -0
  22. package/dist/cjs/runtime/routeTree/staticData.js +63 -0
  23. package/dist/cjs/runtime/routeTree/tanstackRoutes.js +57 -0
  24. package/dist/cjs/runtime/routeTree/types.js +18 -0
  25. package/dist/cjs/runtime/routeTree.js +7 -320
  26. package/dist/cjs/runtime/rsc/CompositeComponent.js +18 -13
  27. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +5 -16
  28. package/dist/cjs/runtime/rsc/client.js +8 -25
  29. package/dist/cjs/runtime/rsc/flightSerialization.js +132 -0
  30. package/dist/cjs/runtime/rsc/payloadErrors.js +99 -0
  31. package/dist/cjs/runtime/rsc/payloadFetch.js +117 -0
  32. package/dist/cjs/runtime/rsc/payloadRouter.js +21 -142
  33. package/dist/cjs/runtime/rsc/payloadRoutes.js +106 -0
  34. package/dist/cjs/runtime/rsc/payloadStorage.js +58 -0
  35. package/dist/cjs/runtime/rsc/server.js +14 -18
  36. package/dist/cjs/runtime/rsc/shared.js +71 -0
  37. package/dist/cjs/runtime/rsc/treeGetter.js +72 -0
  38. package/dist/cjs/runtime/ssrManagedTags.js +103 -0
  39. package/dist/cjs/runtime/ssrPreload.js +89 -0
  40. package/dist/cjs/runtime/ssrTypes.js +18 -0
  41. package/dist/cjs/runtime/submitAction.js +161 -0
  42. package/dist/cjs/runtime/utils.js +49 -86
  43. package/dist/esm/cli/artifacts.mjs +103 -0
  44. package/dist/esm/cli/index.mjs +4 -78
  45. package/dist/esm/cli/tanstackTypes/canonicalRoutes.mjs +75 -0
  46. package/dist/esm/cli/tanstackTypes/shared.mjs +68 -0
  47. package/dist/esm/cli/tanstackTypes/source.mjs +203 -0
  48. package/dist/esm/cli/tanstackTypes.mjs +2 -339
  49. package/dist/esm/runtime/blockingSubscribe.mjs +19 -0
  50. package/dist/esm/runtime/clientBasepath.mjs +10 -0
  51. package/dist/esm/runtime/clientHydration.mjs +70 -0
  52. package/dist/esm/runtime/dataMutation.mjs +2 -167
  53. package/dist/esm/runtime/formData.mjs +63 -0
  54. package/dist/esm/runtime/loaderBridge.mjs +4 -1
  55. package/dist/esm/runtime/plugin.mjs +14 -118
  56. package/dist/esm/runtime/plugin.node.mjs +41 -121
  57. package/dist/esm/runtime/pluginShared.mjs +40 -0
  58. package/dist/esm/runtime/prefetchLink.mjs +18 -5
  59. package/dist/esm/runtime/routeTree/components.mjs +55 -0
  60. package/dist/esm/runtime/routeTree/construction.mjs +111 -0
  61. package/dist/esm/runtime/routeTree/index.mjs +2 -0
  62. package/dist/esm/runtime/routeTree/loaders.mjs +117 -0
  63. package/dist/esm/runtime/routeTree/paths.mjs +16 -0
  64. package/dist/esm/runtime/routeTree/staticData.mjs +22 -0
  65. package/dist/esm/runtime/routeTree/tanstackRoutes.mjs +13 -0
  66. package/dist/esm/runtime/routeTree/types.mjs +0 -0
  67. package/dist/esm/runtime/routeTree.mjs +1 -318
  68. package/dist/esm/runtime/rsc/CompositeComponent.mjs +18 -13
  69. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +4 -15
  70. package/dist/esm/runtime/rsc/client.mjs +6 -26
  71. package/dist/esm/runtime/rsc/flightSerialization.mjs +88 -0
  72. package/dist/esm/runtime/rsc/payloadErrors.mjs +49 -0
  73. package/dist/esm/runtime/rsc/payloadFetch.mjs +61 -0
  74. package/dist/esm/runtime/rsc/payloadRouter.mjs +12 -133
  75. package/dist/esm/runtime/rsc/payloadRoutes.mjs +50 -0
  76. package/dist/esm/runtime/rsc/payloadStorage.mjs +14 -0
  77. package/dist/esm/runtime/rsc/server.mjs +17 -21
  78. package/dist/esm/runtime/rsc/shared.mjs +21 -0
  79. package/dist/esm/runtime/rsc/treeGetter.mjs +34 -0
  80. package/dist/esm/runtime/ssrManagedTags.mjs +53 -0
  81. package/dist/esm/runtime/ssrPreload.mjs +51 -0
  82. package/dist/esm/runtime/ssrTypes.mjs +0 -0
  83. package/dist/esm/runtime/submitAction.mjs +120 -0
  84. package/dist/esm/runtime/utils.mjs +47 -75
  85. package/dist/esm-node/cli/artifacts.mjs +104 -0
  86. package/dist/esm-node/cli/index.mjs +4 -78
  87. package/dist/esm-node/cli/tanstackTypes/canonicalRoutes.mjs +76 -0
  88. package/dist/esm-node/cli/tanstackTypes/shared.mjs +69 -0
  89. package/dist/esm-node/cli/tanstackTypes/source.mjs +204 -0
  90. package/dist/esm-node/cli/tanstackTypes.mjs +2 -339
  91. package/dist/esm-node/runtime/blockingSubscribe.mjs +20 -0
  92. package/dist/esm-node/runtime/clientBasepath.mjs +11 -0
  93. package/dist/esm-node/runtime/clientHydration.mjs +71 -0
  94. package/dist/esm-node/runtime/dataMutation.mjs +2 -167
  95. package/dist/esm-node/runtime/formData.mjs +64 -0
  96. package/dist/esm-node/runtime/loaderBridge.mjs +4 -1
  97. package/dist/esm-node/runtime/plugin.mjs +14 -118
  98. package/dist/esm-node/runtime/plugin.node.mjs +41 -121
  99. package/dist/esm-node/runtime/pluginShared.mjs +41 -0
  100. package/dist/esm-node/runtime/prefetchLink.mjs +18 -5
  101. package/dist/esm-node/runtime/routeTree/components.mjs +56 -0
  102. package/dist/esm-node/runtime/routeTree/construction.mjs +112 -0
  103. package/dist/esm-node/runtime/routeTree/index.mjs +3 -0
  104. package/dist/esm-node/runtime/routeTree/loaders.mjs +118 -0
  105. package/dist/esm-node/runtime/routeTree/paths.mjs +17 -0
  106. package/dist/esm-node/runtime/routeTree/staticData.mjs +23 -0
  107. package/dist/esm-node/runtime/routeTree/tanstackRoutes.mjs +14 -0
  108. package/dist/esm-node/runtime/routeTree/types.mjs +1 -0
  109. package/dist/esm-node/runtime/routeTree.mjs +1 -318
  110. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +18 -13
  111. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +4 -15
  112. package/dist/esm-node/runtime/rsc/client.mjs +6 -26
  113. package/dist/esm-node/runtime/rsc/flightSerialization.mjs +89 -0
  114. package/dist/esm-node/runtime/rsc/payloadErrors.mjs +50 -0
  115. package/dist/esm-node/runtime/rsc/payloadFetch.mjs +62 -0
  116. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +12 -133
  117. package/dist/esm-node/runtime/rsc/payloadRoutes.mjs +51 -0
  118. package/dist/esm-node/runtime/rsc/payloadStorage.mjs +15 -0
  119. package/dist/esm-node/runtime/rsc/server.mjs +17 -21
  120. package/dist/esm-node/runtime/rsc/shared.mjs +22 -0
  121. package/dist/esm-node/runtime/rsc/treeGetter.mjs +35 -0
  122. package/dist/esm-node/runtime/ssrManagedTags.mjs +54 -0
  123. package/dist/esm-node/runtime/ssrPreload.mjs +52 -0
  124. package/dist/esm-node/runtime/ssrTypes.mjs +1 -0
  125. package/dist/esm-node/runtime/submitAction.mjs +121 -0
  126. package/dist/esm-node/runtime/utils.mjs +47 -75
  127. package/dist/types/cli/artifacts.d.ts +33 -0
  128. package/dist/types/cli/index.d.ts +2 -22
  129. package/dist/types/cli/tanstackTypes/canonicalRoutes.d.ts +22 -0
  130. package/dist/types/cli/tanstackTypes/shared.d.ts +20 -0
  131. package/dist/types/cli/tanstackTypes/source.d.ts +10 -0
  132. package/dist/types/cli/tanstackTypes.d.ts +3 -31
  133. package/dist/types/runtime/blockingSubscribe.d.ts +3 -0
  134. package/dist/types/runtime/clientBasepath.d.ts +1 -0
  135. package/dist/types/runtime/clientHydration.d.ts +19 -0
  136. package/dist/types/runtime/dataMutation.d.ts +3 -12
  137. package/dist/types/runtime/formData.d.ts +23 -0
  138. package/dist/types/runtime/plugin.d.ts +1 -1
  139. package/dist/types/runtime/plugin.node.d.ts +1 -1
  140. package/dist/types/runtime/pluginShared.d.ts +39 -0
  141. package/dist/types/runtime/routeTree/components.d.ts +7 -0
  142. package/dist/types/runtime/routeTree/construction.d.ts +5 -0
  143. package/dist/types/runtime/routeTree/index.d.ts +2 -0
  144. package/dist/types/runtime/routeTree/loaders.d.ts +4 -0
  145. package/dist/types/runtime/routeTree/paths.d.ts +3 -0
  146. package/dist/types/runtime/routeTree/staticData.d.ts +19 -0
  147. package/dist/types/runtime/routeTree/tanstackRoutes.d.ts +4 -0
  148. package/dist/types/runtime/routeTree/types.d.ts +75 -0
  149. package/dist/types/runtime/routeTree.d.ts +1 -9
  150. package/dist/types/runtime/rsc/CompositeComponent.d.ts +1 -2
  151. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +2 -3
  152. package/dist/types/runtime/rsc/client.d.ts +2 -0
  153. package/dist/types/runtime/rsc/flightSerialization.d.ts +3 -0
  154. package/dist/types/runtime/rsc/payloadErrors.d.ts +6 -0
  155. package/dist/types/runtime/rsc/payloadFetch.d.ts +10 -0
  156. package/dist/types/runtime/rsc/payloadRouter.d.ts +2 -10
  157. package/dist/types/runtime/rsc/payloadRoutes.d.ts +43 -0
  158. package/dist/types/runtime/rsc/payloadStorage.d.ts +5 -0
  159. package/dist/types/runtime/rsc/shared.d.ts +19 -0
  160. package/dist/types/runtime/rsc/treeGetter.d.ts +1 -0
  161. package/dist/types/runtime/ssrManagedTags.d.ts +7 -0
  162. package/dist/types/runtime/ssrPreload.d.ts +2 -0
  163. package/dist/types/runtime/ssrTypes.d.ts +43 -0
  164. package/dist/types/runtime/state.d.ts +1 -1
  165. package/dist/types/runtime/submitAction.d.ts +14 -0
  166. package/dist/types/runtime/types.d.ts +4 -9
  167. package/dist/types/runtime/utils.d.ts +4 -24
  168. package/package.json +30 -8
  169. package/rstest.config.mts +3 -0
  170. package/src/cli/artifacts.ts +255 -0
  171. package/src/cli/index.ts +7 -185
  172. package/src/cli/tanstackTypes/canonicalRoutes.ts +157 -0
  173. package/src/cli/tanstackTypes/shared.ts +119 -0
  174. package/src/cli/tanstackTypes/source.ts +405 -0
  175. package/src/cli/tanstackTypes.ts +4 -647
  176. package/src/runtime/blockingSubscribe.ts +48 -0
  177. package/src/runtime/clientBasepath.ts +12 -0
  178. package/src/runtime/clientHydration.ts +151 -0
  179. package/src/runtime/dataMutation.tsx +9 -306
  180. package/src/runtime/formData.ts +153 -0
  181. package/src/runtime/loaderBridge.ts +8 -3
  182. package/src/runtime/plugin.node.tsx +76 -291
  183. package/src/runtime/plugin.tsx +21 -257
  184. package/src/runtime/pluginShared.ts +112 -0
  185. package/src/runtime/prefetchLink.tsx +78 -15
  186. package/src/runtime/routeTree/components.ts +130 -0
  187. package/src/runtime/routeTree/construction.ts +238 -0
  188. package/src/runtime/routeTree/index.ts +7 -0
  189. package/src/runtime/routeTree/loaders.ts +232 -0
  190. package/src/runtime/routeTree/paths.ts +36 -0
  191. package/src/runtime/routeTree/staticData.ts +66 -0
  192. package/src/runtime/routeTree/tanstackRoutes.ts +36 -0
  193. package/src/runtime/routeTree/types.ts +87 -0
  194. package/src/runtime/routeTree.ts +5 -750
  195. package/src/runtime/rsc/CompositeComponent.tsx +29 -20
  196. package/src/runtime/rsc/RscNodeRenderer.tsx +9 -27
  197. package/src/runtime/rsc/client.tsx +12 -40
  198. package/src/runtime/rsc/flightSerialization.ts +158 -0
  199. package/src/runtime/rsc/payloadErrors.ts +73 -0
  200. package/src/runtime/rsc/payloadFetch.ts +97 -0
  201. package/src/runtime/rsc/payloadRouter.ts +29 -234
  202. package/src/runtime/rsc/payloadRoutes.ts +122 -0
  203. package/src/runtime/rsc/payloadStorage.ts +35 -0
  204. package/src/runtime/rsc/server.tsx +20 -29
  205. package/src/runtime/rsc/shared.ts +70 -0
  206. package/src/runtime/rsc/treeGetter.tsx +52 -0
  207. package/src/runtime/ssrManagedTags.ts +107 -0
  208. package/src/runtime/ssrPreload.ts +125 -0
  209. package/src/runtime/ssrTypes.ts +51 -0
  210. package/src/runtime/state.ts +4 -2
  211. package/src/runtime/submitAction.ts +194 -0
  212. package/src/runtime/types.ts +6 -13
  213. package/src/runtime/utils.tsx +76 -110
  214. package/src/types/react-server-dom-rspack.d.ts +56 -0
  215. package/tests/castRatchet.test.ts +85 -0
  216. package/tests/router/cli.test.ts +7 -0
  217. package/tests/router/dataMutation.test.tsx +59 -0
  218. package/tests/router/fixtures/router-gen.golden.txt +72 -0
  219. package/tests/router/generateRouteArtifacts.test.ts +106 -0
  220. package/tests/router/loaderBridge.test.ts +21 -0
  221. package/tests/router/prefetchLink.test.tsx +106 -59
  222. package/tests/router/prefetchLinkPreload.test.tsx +85 -0
  223. package/tests/router/routeTree.test.ts +46 -11
  224. package/tests/router/rsc.test.tsx +189 -0
  225. package/tests/router/ssrPreload.test.ts +30 -0
  226. package/tests/router/tanstackTypes.test.ts +254 -1
  227. package/dist/esm/runtime/pluginCore.mjs +0 -14
  228. package/dist/esm-node/runtime/pluginCore.mjs +0 -15
  229. package/dist/types/runtime/pluginCore.d.ts +0 -21
  230. package/src/runtime/pluginCore.ts +0 -48
@@ -1,77 +1,9 @@
1
1
  import "node:module";
2
- import { isRouteErrorResponse } from "@modern-js/runtime-utils/router";
3
- import { notFound, redirect } from "@tanstack/react-router";
4
- const payloadFetchCache = new Map();
5
- let payloadDecoder;
6
- function getRouteId(match) {
7
- const routeId = match.routeId ?? match.route?.id ?? match.id;
8
- return 'string' == typeof routeId ? routeId : void 0;
9
- }
10
- function getRouteStaticData(match) {
11
- return match.route?.options?.staticData || {};
12
- }
13
- function getRouteParentId(match) {
14
- const parentId = match.route?.parentRoute?.id;
15
- return 'string' == typeof parentId ? parentId : void 0;
16
- }
17
- function toRoutePath(match) {
18
- const path = match.route?.options?.path;
19
- return 'string' == typeof path ? path : void 0;
20
- }
21
- function toPayloadRoute(match) {
22
- const routeId = getRouteId(match);
23
- if (!routeId) return;
24
- const staticData = getRouteStaticData(match);
25
- const params = match.params && 'object' == typeof match.params ? match.params : {};
26
- const pathname = 'string' == typeof match.pathname ? match.pathname : '';
27
- return {
28
- handle: staticData.modernRouteHandle,
29
- hasAction: Boolean(staticData.modernRouteHasAction || staticData.modernRouteAction),
30
- hasErrorBoundary: false,
31
- hasLoader: Boolean(staticData.modernRouteHasLoader),
32
- hasClientLoader: Boolean(staticData.modernRouteHasClientLoader),
33
- id: routeId,
34
- index: Boolean(match.route?.options?.index) || void 0,
35
- params,
36
- parentId: getRouteParentId(match),
37
- path: toRoutePath(match),
38
- pathname,
39
- pathnameBase: 'string' == typeof match.pathnameBase ? match.pathnameBase : pathname
40
- };
41
- }
42
- function shouldRedactServerError(status = 500) {
43
- return status >= 500 && 'development' !== process.env.NODE_ENV && 'test' !== process.env.NODE_ENV;
44
- }
45
- function serializePayloadError(error) {
46
- if (isRouteErrorResponse(error)) {
47
- if (shouldRedactServerError(error.status)) return {
48
- status: error.status,
49
- statusText: 'Internal Server Error',
50
- data: 'Unexpected Server Error',
51
- __type: 'RouteErrorResponse'
52
- };
53
- return {
54
- ...error,
55
- __type: 'RouteErrorResponse'
56
- };
57
- }
58
- if (error instanceof Error) {
59
- if (shouldRedactServerError()) return {
60
- message: 'Unexpected Server Error',
61
- stack: void 0,
62
- __type: 'Error'
63
- };
64
- return {
65
- message: error.message,
66
- stack: error.stack,
67
- __type: 'Error',
68
- ...'Error' !== error.name ? {
69
- __subType: error.name
70
- } : {}
71
- };
72
- }
73
- return error;
74
- }
2
+ import { notFound } from "@tanstack/react-router";
3
+ import { serializeTanstackRscFlightValues } from "./flightSerialization.mjs";
4
+ import { isSerializedNotFound, serializePayloadError, toRouteErrors, toRouteLoaderData } from "./payloadErrors.mjs";
5
+ import { __setTanstackRscPayloadDecoderForTests, createPayloadFetchKey, fetchTanstackRscPayload, payloadFetchCache } from "./payloadFetch.mjs";
6
+ import { toPayloadRoute, toPlainLocation } from "./payloadRoutes.mjs";
75
7
  function createTanstackRscServerPayload(router, options = {}) {
76
8
  const matches = Array.isArray(router.state?.matches) ? router.state.matches : [];
77
9
  const routes = [];
@@ -81,7 +13,7 @@ function createTanstackRscServerPayload(router, options = {}) {
81
13
  const payloadRoute = toPayloadRoute(match);
82
14
  if (payloadRoute) {
83
15
  routes.push(payloadRoute);
84
- if ('loaderData' in match && void 0 !== match.loaderData && !(options.omitClientLoaderData && payloadRoute.hasClientLoader)) loaderData[payloadRoute.id] = match.loaderData;
16
+ if ('loaderData' in match && void 0 !== match.loaderData && !(options.omitClientLoaderData && payloadRoute.hasClientLoader)) loaderData[payloadRoute.id] = serializeTanstackRscFlightValues(match.loaderData);
85
17
  if (void 0 !== match.error) errors[payloadRoute.id] = serializePayloadError(match.error);
86
18
  }
87
19
  }
@@ -90,14 +22,17 @@ function createTanstackRscServerPayload(router, options = {}) {
90
22
  actionData: null,
91
23
  errors: Object.keys(errors).length > 0 ? errors : null,
92
24
  loaderData,
93
- location: router.state?.location,
25
+ location: toPlainLocation(router.state?.location),
94
26
  routes
95
27
  };
96
28
  }
97
29
  function handleTanstackRscRedirect(headers, basename, status) {
98
30
  const newHeaders = new Headers(headers);
99
31
  let redirectUrl = headers.get('Location') || '/';
100
- if ('/' !== basename) redirectUrl = redirectUrl.replace(basename, '') || '/';
32
+ if ('/' !== basename) {
33
+ if (redirectUrl === basename) redirectUrl = '/';
34
+ else if (redirectUrl.startsWith(`${basename}/`)) redirectUrl = redirectUrl.slice(basename.length);
35
+ }
101
36
  newHeaders.set('X-Modernjs-Redirect', redirectUrl);
102
37
  newHeaders.set('X-Modernjs-BaseUrl', basename);
103
38
  newHeaders.delete('Location');
@@ -109,69 +44,17 @@ function handleTanstackRscRedirect(headers, basename, status) {
109
44
  function isTanstackRscPayloadNavigationEnabled() {
110
45
  return "u" > typeof window;
111
46
  }
112
- async function decodePayload(stream) {
113
- if (payloadDecoder) return payloadDecoder(stream);
114
- const runtime = await import("@modern-js/runtime/rsc/client");
115
- return runtime.createFromReadableStream(stream);
116
- }
117
- function isServerPayload(value) {
118
- return Boolean(value) && 'object' == typeof value && 'render' === value.type && Array.isArray(value.routes);
119
- }
120
- function createPayloadFetchKey(request) {
121
- return request.url;
122
- }
123
- function isAbsoluteUrl(value) {
124
- try {
125
- new URL(value);
126
- return true;
127
- } catch {
128
- return false;
129
- }
130
- }
131
- async function fetchTanstackRscPayload(request) {
132
- const headers = new Headers(request.headers);
133
- headers.set('x-rsc-tree', 'true');
134
- const response = await fetch(request.url, {
135
- credentials: 'same-origin',
136
- headers,
137
- method: 'GET',
138
- signal: request.signal
139
- });
140
- const redirectLocation = response.headers.get('X-Modernjs-Redirect');
141
- if (redirectLocation) {
142
- if (isAbsoluteUrl(redirectLocation)) throw redirect({
143
- href: redirectLocation
144
- });
145
- throw redirect({
146
- to: redirectLocation || '/'
147
- });
148
- }
149
- if (404 === response.status && !response.body) throw notFound();
150
- if (!response.body) throw new Error('TanStack RSC payload response body is null.');
151
- const payload = await decodePayload(response.body);
152
- if (!isServerPayload(payload)) throw new Error('Unexpected TanStack RSC payload type.');
153
- return payload;
154
- }
155
47
  function loadTanstackRscPayload(request) {
156
48
  const key = createPayloadFetchKey(request);
157
49
  let payloadPromise = payloadFetchCache.get(key);
158
50
  if (!payloadPromise) {
159
51
  payloadPromise = fetchTanstackRscPayload(request).finally(()=>{
160
- payloadFetchCache.delete(key);
52
+ payloadFetchCache["delete"](key);
161
53
  });
162
54
  payloadFetchCache.set(key, payloadPromise);
163
55
  }
164
56
  return payloadPromise;
165
57
  }
166
- function isSerializedNotFound(value) {
167
- return Boolean(value) && 'object' == typeof value && true === value.isNotFound;
168
- }
169
- function toRouteErrors(payload) {
170
- return payload.errors && 'object' == typeof payload.errors ? payload.errors : {};
171
- }
172
- function toRouteLoaderData(payload) {
173
- return payload.loaderData && 'object' == typeof payload.loaderData ? payload.loaderData : {};
174
- }
175
58
  async function loadTanstackRscRouteData({ hasClientLoader, loadClientData, request, routeId }) {
176
59
  if (hasClientLoader) return loadClientData();
177
60
  if (!routeId) return loadClientData();
@@ -190,8 +73,4 @@ async function loadTanstackRscRouteData({ hasClientLoader, loadClientData, reque
190
73
  const payloadRoute = payload.routes.find((route)=>route.id === routeId);
191
74
  if (payloadRoute && payloadRoute.hasClientLoader) return loadClientData();
192
75
  }
193
- function __setTanstackRscPayloadDecoderForTests(decoder) {
194
- payloadDecoder = decoder;
195
- payloadFetchCache.clear();
196
- }
197
76
  export { __setTanstackRscPayloadDecoderForTests, createTanstackRscServerPayload, handleTanstackRscRedirect, isTanstackRscPayloadNavigationEnabled, loadTanstackRscPayload, loadTanstackRscRouteData };
@@ -0,0 +1,51 @@
1
+ import "node:module";
2
+ function toPlainRecord(value) {
3
+ if (!value || 'object' != typeof value) return {};
4
+ return Object.fromEntries(Object.entries(value));
5
+ }
6
+ function toPlainLocation(location) {
7
+ if (!location || 'object' != typeof location) return location;
8
+ const plainLocation = toPlainRecord(location);
9
+ for (const key of [
10
+ 'search',
11
+ 'state'
12
+ ])if (plainLocation[key] && 'object' == typeof plainLocation[key] && !Array.isArray(plainLocation[key])) plainLocation[key] = toPlainRecord(plainLocation[key]);
13
+ return plainLocation;
14
+ }
15
+ function getRouteId(match) {
16
+ const routeId = match.routeId ?? match.route?.id ?? match.id;
17
+ return 'string' == typeof routeId ? routeId : void 0;
18
+ }
19
+ function getRouteStaticData(match) {
20
+ return match.route?.options?.staticData || {};
21
+ }
22
+ function getRouteParentId(match) {
23
+ const parentId = match.route?.parentRoute?.id;
24
+ return 'string' == typeof parentId ? parentId : void 0;
25
+ }
26
+ function toRoutePath(match) {
27
+ const path = match.route?.options?.path;
28
+ return 'string' == typeof path ? path : void 0;
29
+ }
30
+ function toPayloadRoute(match) {
31
+ const routeId = getRouteId(match);
32
+ if (!routeId) return;
33
+ const staticData = getRouteStaticData(match);
34
+ const params = match.params && 'object' == typeof match.params ? toPlainRecord(match.params) : {};
35
+ const pathname = 'string' == typeof match.pathname ? match.pathname : '';
36
+ return {
37
+ handle: staticData.modernRouteHandle,
38
+ hasAction: Boolean(staticData.modernRouteHasAction || staticData.modernRouteAction),
39
+ hasErrorBoundary: false,
40
+ hasLoader: Boolean(staticData.modernRouteHasLoader),
41
+ hasClientLoader: Boolean(staticData.modernRouteHasClientLoader),
42
+ id: routeId,
43
+ index: Boolean(match.route?.options?.index) || void 0,
44
+ params,
45
+ parentId: getRouteParentId(match),
46
+ path: toRoutePath(match),
47
+ pathname,
48
+ pathnameBase: 'string' == typeof match.pathnameBase ? match.pathnameBase : pathname
49
+ };
50
+ }
51
+ export { getRouteId, getRouteParentId, getRouteStaticData, toPayloadRoute, toPlainLocation, toPlainRecord, toRoutePath };
@@ -0,0 +1,15 @@
1
+ import "node:module";
2
+ import { storage } from "@modern-js/runtime-utils/node";
3
+ const setTanstackRscServerPayload = (payload)=>{
4
+ const storageContext = storage.useContext?.();
5
+ if (storageContext) storageContext.serverPayload = payload;
6
+ };
7
+ const setTanstackRscRouter = (router)=>{
8
+ const storageContext = storage.useContext?.();
9
+ if (storageContext) storageContext.tanstackRscRouter = router;
10
+ };
11
+ const getTanstackRscRouter = ()=>{
12
+ const storageContext = storage.useContext?.();
13
+ return storageContext?.tanstackRscRouter;
14
+ };
15
+ export { getTanstackRscRouter, setTanstackRscRouter, setTanstackRscServerPayload };
@@ -1,27 +1,12 @@
1
1
  import "node:module";
2
2
  import { Fragment, jsx } from "react/jsx-runtime";
3
- import { createFromReadableStream, renderRsc } from "@modern-js/runtime/rsc/server";
4
- import { createSerializationAdapter } from "@tanstack/react-router";
5
- import { RawStream } from "@tanstack/router-core";
6
- import { createElement, use } from "react";
3
+ import { renderRsc } from "@modern-js/runtime/rsc/server";
4
+ import { RawStream, createSerializationAdapter } from "@tanstack/router-core";
5
+ import { createElement } from "react";
7
6
  import { ClientSlot } from "./ClientSlot.mjs";
8
- import { createRscProxy } from "./createRscProxy.mjs";
9
7
  import { ReplayableStream } from "./ReplayableStream.mjs";
10
8
  import { sanitizeSlotArgs } from "./slotUsageSanitizer.mjs";
11
- import { RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM, isRenderableServerComponent, isServerComponent } from "./symbols.mjs";
12
- function createTreeGetter(stream) {
13
- let ready = false;
14
- let tree;
15
- const treePromise = Promise.resolve(createFromReadableStream(stream.createReplayStream())).then((value)=>{
16
- tree = value;
17
- ready = true;
18
- return value;
19
- });
20
- return ()=>{
21
- if (ready) return tree;
22
- return use(treePromise);
23
- };
24
- }
9
+ import { RENDERABLE_RSC, RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM, isRenderableServerComponent, isServerComponent } from "./symbols.mjs";
25
10
  function createSlotProxy(options) {
26
11
  const cache = new Map();
27
12
  return new Proxy({}, {
@@ -45,6 +30,17 @@ function createSlotProxy(options) {
45
30
  }
46
31
  });
47
32
  }
33
+ function createServerRscValue(options) {
34
+ return {
35
+ [SERVER_COMPONENT_STREAM]: options.stream,
36
+ ...options.renderable ? {
37
+ [RENDERABLE_RSC]: true
38
+ } : {},
39
+ ...options.slotUsagesStream ? {
40
+ [RSC_SLOT_USAGES_STREAM]: options.slotUsagesStream
41
+ } : {}
42
+ };
43
+ }
48
44
  function toReplayableFlightStream(node, handlers) {
49
45
  const flightStream = renderRsc({
50
46
  element: node
@@ -58,7 +54,7 @@ async function renderServerComponent(node) {
58
54
  const streamWrapper = {
59
55
  createReplayStream: ()=>stream.createReplayStream()
60
56
  };
61
- return createRscProxy(createTreeGetter(streamWrapper), {
57
+ return createServerRscValue({
62
58
  renderable: true,
63
59
  stream: streamWrapper
64
60
  });
@@ -87,7 +83,7 @@ async function createCompositeComponent(component) {
87
83
  const streamWrapper = {
88
84
  createReplayStream: ()=>flightStream.createReplayStream()
89
85
  };
90
- return createRscProxy(createTreeGetter(streamWrapper), {
86
+ return createServerRscValue({
91
87
  renderable: false,
92
88
  slotUsagesStream: slotUsagesEmitter?.stream,
93
89
  stream: streamWrapper
@@ -0,0 +1,22 @@
1
+ import "node:module";
2
+ import { RSC_PROXY_GET_TREE, RSC_PROXY_PATH } from "./symbols.mjs";
3
+ const TANSTACK_RSC_FLIGHT_VALUE = '__modernTanstackRsc';
4
+ function isSerializedTanstackRscFlightValue(value) {
5
+ return Boolean(value) && 'object' == typeof value && true === value[TANSTACK_RSC_FLIGHT_VALUE] && ('renderable' === value.kind || 'composite' === value.kind) && value.stream instanceof ReadableStream;
6
+ }
7
+ function EmptyFallback() {
8
+ return null;
9
+ }
10
+ function selectTreePath(tree, path) {
11
+ let current = tree;
12
+ for (const key of path){
13
+ if (null === current || 'object' != typeof current) return null;
14
+ current = current[key];
15
+ }
16
+ return current;
17
+ }
18
+ function renderSelectedTree(src, { getTree = src[RSC_PROXY_GET_TREE], missingTreeError }) {
19
+ if (!getTree) throw new Error(missingTreeError);
20
+ return selectTreePath(getTree(), src[RSC_PROXY_PATH] || []);
21
+ }
22
+ export { EmptyFallback, TANSTACK_RSC_FLIGHT_VALUE, isSerializedTanstackRscFlightValue, renderSelectedTree, selectTreePath };
@@ -0,0 +1,35 @@
1
+ import "node:module";
2
+ "use client";
3
+ import { use } from "react";
4
+ let modernRscClientRuntimePromise;
5
+ function loadModernRscClientRuntime() {
6
+ modernRscClientRuntimePromise ??= import("@modern-js/runtime/rsc/client").then((runtime)=>{
7
+ runtime.setServerCallback(runtime.callServer);
8
+ return runtime;
9
+ });
10
+ return modernRscClientRuntimePromise;
11
+ }
12
+ async function createFromFlightStream(stream) {
13
+ if ("u" < typeof window) {
14
+ const runtime = await import("react-server-dom-rspack/client.edge");
15
+ return runtime.createFromReadableStream(stream);
16
+ }
17
+ const runtime = await loadModernRscClientRuntime();
18
+ return runtime.createFromReadableStream(stream, {
19
+ callServer: runtime.callServer
20
+ });
21
+ }
22
+ function createTreeGetterFromFlightStream(stream) {
23
+ let ready = false;
24
+ let tree;
25
+ const treePromise = createFromFlightStream(stream).then((value)=>{
26
+ tree = value;
27
+ ready = true;
28
+ return value;
29
+ });
30
+ return ()=>{
31
+ if (ready) return tree;
32
+ return use(treePromise);
33
+ };
34
+ }
35
+ export { createTreeGetterFromFlightStream };
@@ -0,0 +1,54 @@
1
+ import "node:module";
2
+ async function waitForRouterSerialization(tanstackRouter) {
3
+ const serverSsr = tanstackRouter.serverSsr;
4
+ if (!serverSsr || 'function' != typeof serverSsr.onSerializationFinished || serverSsr.isSerializationFinished?.()) return;
5
+ await new Promise((resolve)=>{
6
+ serverSsr.onSerializationFinished?.(resolve);
7
+ });
8
+ }
9
+ function htmlEscapeAttr(value) {
10
+ return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;');
11
+ }
12
+ function routerManagedTagToHtml(tag) {
13
+ if (!tag || 'object' != typeof tag) return '';
14
+ const managedTag = tag;
15
+ if (!managedTag || "script" !== managedTag.tag) return '';
16
+ const attrs = managedTag.attrs || {};
17
+ const attrsStr = Object.entries(attrs).filter(([, v])=>null != v && false !== v).map(([k, v])=>{
18
+ const name = 'className' === k ? 'class' : k;
19
+ if (true === v) return name;
20
+ return `${name}="${htmlEscapeAttr(String(v))}"`;
21
+ }).join(' ');
22
+ const open = attrsStr.length ? `<script ${attrsStr}>` : "<script>";
23
+ const children = 'string' == typeof managedTag.children ? managedTag.children : '';
24
+ return `${open}${children}</script>`;
25
+ }
26
+ function routerManagedTagsToHtml(tags) {
27
+ const normalizedTags = Array.isArray(tags) ? tags : [
28
+ tags
29
+ ];
30
+ return normalizedTags.map(routerManagedTagToHtml).filter(Boolean);
31
+ }
32
+ function createGetSsrHref(request) {
33
+ const url = new URL(request.url);
34
+ return `${url.pathname}${url.search}${url.hash}`;
35
+ }
36
+ function collectRouterErrors(tanstackRouter) {
37
+ const state = tanstackRouter.state;
38
+ const matches = Array.isArray(state.matches) ? state.matches : [];
39
+ const errors = matches.reduce((acc, match)=>{
40
+ if (!match.error) return acc;
41
+ const routeId = 'string' == typeof match.routeId ? match.routeId : 'string' == typeof match.route?.id ? match.route.id : `match-${Object.keys(acc).length}`;
42
+ acc[routeId] = match.error;
43
+ return acc;
44
+ }, {});
45
+ return Object.keys(errors).length > 0 ? errors : void 0;
46
+ }
47
+ async function attachServerSsrUtils(router) {
48
+ const { attachRouterServerSsrUtils } = await import("@tanstack/router-core/ssr/server");
49
+ attachRouterServerSsrUtils({
50
+ router,
51
+ manifest: void 0
52
+ });
53
+ }
54
+ export { attachServerSsrUtils, collectRouterErrors, createGetSsrHref, routerManagedTagsToHtml, waitForRouterSerialization };
@@ -0,0 +1,52 @@
1
+ import "node:module";
2
+ function isPromiseLike(value) {
3
+ return Boolean(value && 'function' == typeof value.then);
4
+ }
5
+ function isPreloadableRouteComponent(component) {
6
+ if (!component || 'function' != typeof component) return false;
7
+ const preloadable = component;
8
+ return 'function' == typeof preloadable.load || 'function' == typeof preloadable.preload;
9
+ }
10
+ function isReactLazyRouteComponent(component) {
11
+ return null != component && 'object' == typeof component && 'function' == typeof component._init && '_payload' in component;
12
+ }
13
+ async function preloadReactLazyRouteComponent(component) {
14
+ try {
15
+ component._init?.(component._payload);
16
+ } catch (thrown) {
17
+ if (!isPromiseLike(thrown)) throw thrown;
18
+ await thrown;
19
+ component._init?.(component._payload);
20
+ }
21
+ }
22
+ async function preloadRouteComponent(component, preloadedComponents) {
23
+ if (isReactLazyRouteComponent(component)) {
24
+ if (preloadedComponents.has(component)) return;
25
+ preloadedComponents.add(component);
26
+ await preloadReactLazyRouteComponent(component);
27
+ return;
28
+ }
29
+ if (!isPreloadableRouteComponent(component)) return;
30
+ if (preloadedComponents.has(component)) return;
31
+ preloadedComponents.add(component);
32
+ if ('function' == typeof component.load) return void await component.load({});
33
+ await component.preload?.({});
34
+ }
35
+ async function preloadMatchedRouteComponents(tanstackRouter) {
36
+ const matches = Array.isArray(tanstackRouter.state.matches) ? tanstackRouter.state.matches : [];
37
+ const routesById = tanstackRouter.routesById || {};
38
+ const preloadedComponents = new Set();
39
+ await Promise.all(matches.map(async (match)=>{
40
+ const routeId = 'string' == typeof match.routeId ? match.routeId : 'string' == typeof match.route?.id ? match.route.id : void 0;
41
+ const route = routeId ? routesById[routeId] : match.route;
42
+ const options = route?.options;
43
+ if (!options) return;
44
+ await Promise.all([
45
+ preloadRouteComponent(options.component, preloadedComponents),
46
+ preloadRouteComponent(options.pendingComponent, preloadedComponents),
47
+ preloadRouteComponent(options.errorComponent, preloadedComponents),
48
+ preloadRouteComponent(options.notFoundComponent, preloadedComponents)
49
+ ]);
50
+ }));
51
+ }
52
+ export { preloadMatchedRouteComponents };
@@ -0,0 +1 @@
1
+ import "node:module";
@@ -0,0 +1,121 @@
1
+ import "node:module";
2
+ import { formDataToTextPlain, formDataToUrlSearchParams, toFormData } from "./formData.mjs";
3
+ class RouteActionResponseError extends Error {
4
+ constructor(response, data){
5
+ super(`Route action failed with status ${response.status}`);
6
+ this.name = 'RouteActionResponseError';
7
+ this.response = response;
8
+ this.data = data;
9
+ }
10
+ }
11
+ function resolveRouteHandlers(router, actionTo) {
12
+ const builtLocation = router.buildLocation({
13
+ to: actionTo
14
+ });
15
+ const href = router.getParsedLocationHref(builtLocation);
16
+ const matchedRoutes = router.getMatchedRoutes(builtLocation.pathname);
17
+ const routeStaticData = matchedRoutes.foundRoute?.options?.staticData;
18
+ const action = routeStaticData?.modernRouteAction;
19
+ const loader = routeStaticData?.modernRouteLoader;
20
+ return {
21
+ action,
22
+ loader,
23
+ href,
24
+ params: matchedRoutes.routeParams || {}
25
+ };
26
+ }
27
+ function isRedirectResponse(value) {
28
+ if (!(value instanceof Response)) return false;
29
+ return [
30
+ 301,
31
+ 302,
32
+ 303,
33
+ 307,
34
+ 308
35
+ ].includes(value.status);
36
+ }
37
+ async function parseResponseData(response) {
38
+ if (204 === response.status) return null;
39
+ const contentType = response.headers.get('Content-Type') || '';
40
+ if (contentType.includes('application/json')) return response.json();
41
+ return response.text();
42
+ }
43
+ async function parseResponseResultOrThrow(response) {
44
+ const parsed = await parseResponseData(response);
45
+ if (!response.ok) throw new RouteActionResponseError(response, parsed);
46
+ return parsed;
47
+ }
48
+ async function submitRouteAction({ router, target, options = {}, isFetcher = false, onInvalidateStart }) {
49
+ const method = (options.method || 'post').toLowerCase();
50
+ const encType = options.encType || 'application/x-www-form-urlencoded';
51
+ const actionTo = options.action || '.';
52
+ const formData = toFormData(target);
53
+ const resolved = resolveRouteHandlers(router, actionTo);
54
+ if ('get' === method) {
55
+ const search = formDataToUrlSearchParams(formData).toString();
56
+ const requestUrl = new URL(resolved.href, window.location.origin);
57
+ requestUrl.search = search;
58
+ if (isFetcher && resolved.loader) {
59
+ const result = await resolved.loader({
60
+ request: new Request(requestUrl, {
61
+ method: 'GET'
62
+ }),
63
+ params: resolved.params
64
+ });
65
+ if (result instanceof Response) {
66
+ const redirectTo = result.headers.get('X-Modernjs-Redirect') || result.headers.get('Location');
67
+ if (redirectTo || isRedirectResponse(result)) {
68
+ await router.navigate({
69
+ to: redirectTo || '/'
70
+ });
71
+ return parseResponseData(result);
72
+ }
73
+ return parseResponseResultOrThrow(result);
74
+ }
75
+ return result;
76
+ }
77
+ await router.navigate({
78
+ href: `${requestUrl.pathname}${requestUrl.search}${requestUrl.hash}`
79
+ });
80
+ return;
81
+ }
82
+ if (!resolved.action) throw new Error(`No route action found for "${actionTo}"`);
83
+ const headers = new Headers();
84
+ let body = null;
85
+ if (encType.includes('application/json')) {
86
+ headers.set('Content-Type', 'application/json');
87
+ body = JSON.stringify(Object.fromEntries(formDataToUrlSearchParams(formData).entries()));
88
+ } else if (encType.includes('text/plain')) {
89
+ headers.set('Content-Type', 'text/plain;charset=UTF-8');
90
+ body = formDataToTextPlain(formData);
91
+ } else if (encType.includes('application/x-www-form-urlencoded')) {
92
+ headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
93
+ body = formDataToUrlSearchParams(formData);
94
+ } else body = formData;
95
+ const request = new Request(new URL(resolved.href, window.location.origin), {
96
+ method: method.toUpperCase(),
97
+ headers,
98
+ body
99
+ });
100
+ const result = await resolved.action({
101
+ request,
102
+ params: resolved.params
103
+ });
104
+ if (result instanceof Response) {
105
+ const redirectTo = result.headers.get('X-Modernjs-Redirect') || result.headers.get('Location');
106
+ if (redirectTo || isRedirectResponse(result)) {
107
+ await router.navigate({
108
+ to: redirectTo || '/'
109
+ });
110
+ return parseResponseData(result);
111
+ }
112
+ const parsed = isFetcher ? await parseResponseResultOrThrow(result) : await parseResponseData(result);
113
+ onInvalidateStart?.();
114
+ await router.invalidate();
115
+ return parsed;
116
+ }
117
+ onInvalidateStart?.();
118
+ await router.invalidate();
119
+ return result;
120
+ }
121
+ export { RouteActionResponseError, submitRouteAction };