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