@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/cli/index.js +268 -0
  3. package/dist/cjs/cli/tanstackTypes.js +388 -0
  4. package/dist/cjs/cli.js +65 -0
  5. package/dist/cjs/runtime/DefaultNotFound.js +47 -0
  6. package/dist/cjs/runtime/basepathRewrite.js +62 -0
  7. package/dist/cjs/runtime/dataMutation.js +345 -0
  8. package/dist/cjs/runtime/hooks.js +57 -0
  9. package/dist/cjs/runtime/index.js +114 -0
  10. package/dist/cjs/runtime/lifecycle.js +125 -0
  11. package/dist/cjs/runtime/plugin.js +250 -0
  12. package/dist/cjs/runtime/plugin.node.js +304 -0
  13. package/dist/cjs/runtime/prefetchLink.js +55 -0
  14. package/dist/cjs/runtime/routeTree.js +492 -0
  15. package/dist/cjs/runtime/rsc/ClientSlot.js +53 -0
  16. package/dist/cjs/runtime/rsc/CompositeComponent.js +75 -0
  17. package/dist/cjs/runtime/rsc/ReplayableStream.js +141 -0
  18. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +65 -0
  19. package/dist/cjs/runtime/rsc/SlotContext.js +54 -0
  20. package/dist/cjs/runtime/rsc/client.js +93 -0
  21. package/dist/cjs/runtime/rsc/createRscProxy.js +141 -0
  22. package/dist/cjs/runtime/rsc/index.js +42 -0
  23. package/dist/cjs/runtime/rsc/payloadRouter.js +211 -0
  24. package/dist/cjs/runtime/rsc/server.js +246 -0
  25. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +65 -0
  26. package/dist/cjs/runtime/rsc/symbols.js +72 -0
  27. package/dist/cjs/runtime/types.js +18 -0
  28. package/dist/cjs/runtime/utils.js +142 -0
  29. package/dist/cjs/runtime.js +58 -0
  30. package/dist/esm/cli/index.mjs +201 -0
  31. package/dist/esm/cli/tanstackTypes.mjs +341 -0
  32. package/dist/esm/cli.mjs +2 -0
  33. package/dist/esm/rslib-runtime.mjs +18 -0
  34. package/dist/esm/runtime/DefaultNotFound.mjs +13 -0
  35. package/dist/esm/runtime/basepathRewrite.mjs +28 -0
  36. package/dist/esm/runtime/dataMutation.mjs +305 -0
  37. package/dist/esm/runtime/hooks.mjs +8 -0
  38. package/dist/esm/runtime/index.mjs +6 -0
  39. package/dist/esm/runtime/lifecycle.mjs +82 -0
  40. package/dist/esm/runtime/plugin.mjs +214 -0
  41. package/dist/esm/runtime/plugin.node.mjs +268 -0
  42. package/dist/esm/runtime/prefetchLink.mjs +18 -0
  43. package/dist/esm/runtime/routeTree.mjs +452 -0
  44. package/dist/esm/runtime/rsc/ClientSlot.mjs +19 -0
  45. package/dist/esm/runtime/rsc/CompositeComponent.mjs +41 -0
  46. package/dist/esm/runtime/rsc/ReplayableStream.mjs +104 -0
  47. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +31 -0
  48. package/dist/esm/runtime/rsc/SlotContext.mjs +17 -0
  49. package/dist/esm/runtime/rsc/client.mjs +53 -0
  50. package/dist/esm/runtime/rsc/createRscProxy.mjs +107 -0
  51. package/dist/esm/runtime/rsc/index.mjs +1 -0
  52. package/dist/esm/runtime/rsc/payloadRouter.mjs +162 -0
  53. package/dist/esm/runtime/rsc/server.mjs +200 -0
  54. package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +31 -0
  55. package/dist/esm/runtime/rsc/symbols.mjs +17 -0
  56. package/dist/esm/runtime/types.mjs +0 -0
  57. package/dist/esm/runtime/utils.mjs +89 -0
  58. package/dist/esm/runtime.mjs +1 -0
  59. package/dist/esm-node/cli/index.mjs +205 -0
  60. package/dist/esm-node/cli/tanstackTypes.mjs +342 -0
  61. package/dist/esm-node/cli.mjs +3 -0
  62. package/dist/esm-node/rslib-runtime.mjs +19 -0
  63. package/dist/esm-node/runtime/DefaultNotFound.mjs +14 -0
  64. package/dist/esm-node/runtime/basepathRewrite.mjs +29 -0
  65. package/dist/esm-node/runtime/dataMutation.mjs +306 -0
  66. package/dist/esm-node/runtime/hooks.mjs +9 -0
  67. package/dist/esm-node/runtime/index.mjs +7 -0
  68. package/dist/esm-node/runtime/lifecycle.mjs +83 -0
  69. package/dist/esm-node/runtime/plugin.mjs +215 -0
  70. package/dist/esm-node/runtime/plugin.node.mjs +269 -0
  71. package/dist/esm-node/runtime/prefetchLink.mjs +19 -0
  72. package/dist/esm-node/runtime/routeTree.mjs +453 -0
  73. package/dist/esm-node/runtime/rsc/ClientSlot.mjs +20 -0
  74. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +42 -0
  75. package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +105 -0
  76. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +32 -0
  77. package/dist/esm-node/runtime/rsc/SlotContext.mjs +18 -0
  78. package/dist/esm-node/runtime/rsc/client.mjs +54 -0
  79. package/dist/esm-node/runtime/rsc/createRscProxy.mjs +108 -0
  80. package/dist/esm-node/runtime/rsc/index.mjs +2 -0
  81. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +163 -0
  82. package/dist/esm-node/runtime/rsc/server.mjs +201 -0
  83. package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +32 -0
  84. package/dist/esm-node/runtime/rsc/symbols.mjs +18 -0
  85. package/dist/esm-node/runtime/types.mjs +1 -0
  86. package/dist/esm-node/runtime/utils.mjs +90 -0
  87. package/dist/esm-node/runtime.mjs +2 -0
  88. package/dist/types/cli/index.d.ts +20 -0
  89. package/dist/types/cli/tanstackTypes.d.ts +11 -0
  90. package/dist/types/cli.d.ts +2 -0
  91. package/dist/types/runtime/DefaultNotFound.d.ts +2 -0
  92. package/dist/types/runtime/basepathRewrite.d.ts +8 -0
  93. package/dist/types/runtime/dataMutation.d.ts +29 -0
  94. package/dist/types/runtime/hooks.d.ts +18 -0
  95. package/dist/types/runtime/index.d.ts +9 -0
  96. package/dist/types/runtime/lifecycle.d.ts +22 -0
  97. package/dist/types/runtime/plugin.d.ts +17 -0
  98. package/dist/types/runtime/plugin.node.d.ts +17 -0
  99. package/dist/types/runtime/prefetchLink.d.ts +11 -0
  100. package/dist/types/runtime/routeTree.d.ts +11 -0
  101. package/dist/types/runtime/rsc/ClientSlot.d.ts +5 -0
  102. package/dist/types/runtime/rsc/CompositeComponent.d.ts +3 -0
  103. package/dist/types/runtime/rsc/ReplayableStream.d.ts +24 -0
  104. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  105. package/dist/types/runtime/rsc/SlotContext.d.ts +11 -0
  106. package/dist/types/runtime/rsc/client.d.ts +11 -0
  107. package/dist/types/runtime/rsc/createRscProxy.d.ts +7 -0
  108. package/dist/types/runtime/rsc/index.d.ts +2 -0
  109. package/dist/types/runtime/rsc/payloadRouter.d.ts +24 -0
  110. package/dist/types/runtime/rsc/server.d.ts +14 -0
  111. package/dist/types/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  112. package/dist/types/runtime/rsc/symbols.d.ts +46 -0
  113. package/dist/types/runtime/types.d.ts +68 -0
  114. package/dist/types/runtime/utils.d.ts +36 -0
  115. package/dist/types/runtime.d.ts +1 -0
  116. package/dist/types-direct/cli/index.d.ts +20 -0
  117. package/dist/types-direct/cli/tanstackTypes.d.ts +11 -0
  118. package/dist/types-direct/cli.d.ts +2 -0
  119. package/dist/types-direct/runtime/DefaultNotFound.d.ts +2 -0
  120. package/dist/types-direct/runtime/basepathRewrite.d.ts +8 -0
  121. package/dist/types-direct/runtime/dataMutation.d.ts +29 -0
  122. package/dist/types-direct/runtime/hooks.d.ts +18 -0
  123. package/dist/types-direct/runtime/index.d.ts +9 -0
  124. package/dist/types-direct/runtime/lifecycle.d.ts +22 -0
  125. package/dist/types-direct/runtime/plugin.d.ts +17 -0
  126. package/dist/types-direct/runtime/plugin.node.d.ts +17 -0
  127. package/dist/types-direct/runtime/prefetchLink.d.ts +11 -0
  128. package/dist/types-direct/runtime/routeTree.d.ts +11 -0
  129. package/dist/types-direct/runtime/rsc/ClientSlot.d.ts +5 -0
  130. package/dist/types-direct/runtime/rsc/CompositeComponent.d.ts +3 -0
  131. package/dist/types-direct/runtime/rsc/ReplayableStream.d.ts +24 -0
  132. package/dist/types-direct/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  133. package/dist/types-direct/runtime/rsc/SlotContext.d.ts +11 -0
  134. package/dist/types-direct/runtime/rsc/client.d.ts +11 -0
  135. package/dist/types-direct/runtime/rsc/createRscProxy.d.ts +7 -0
  136. package/dist/types-direct/runtime/rsc/index.d.ts +2 -0
  137. package/dist/types-direct/runtime/rsc/payloadRouter.d.ts +24 -0
  138. package/dist/types-direct/runtime/rsc/server.d.ts +14 -0
  139. package/dist/types-direct/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  140. package/dist/types-direct/runtime/rsc/symbols.d.ts +46 -0
  141. package/dist/types-direct/runtime/types.d.ts +68 -0
  142. package/dist/types-direct/runtime/utils.d.ts +36 -0
  143. package/dist/types-direct/runtime.d.ts +1 -0
  144. package/package.json +126 -0
  145. package/rslib.config.mts +4 -0
  146. package/rstest.config.mts +43 -0
  147. package/src/cli/index.ts +388 -0
  148. package/src/cli/tanstackTypes.ts +503 -0
  149. package/src/cli.ts +2 -0
  150. package/src/runtime/DefaultNotFound.tsx +15 -0
  151. package/src/runtime/basepathRewrite.ts +59 -0
  152. package/src/runtime/dataMutation.tsx +517 -0
  153. package/src/runtime/hooks.ts +34 -0
  154. package/src/runtime/index.tsx +30 -0
  155. package/src/runtime/lifecycle.ts +150 -0
  156. package/src/runtime/plugin.node.tsx +534 -0
  157. package/src/runtime/plugin.tsx +395 -0
  158. package/src/runtime/prefetchLink.tsx +87 -0
  159. package/src/runtime/routeTree.ts +942 -0
  160. package/src/runtime/rsc/ClientSlot.tsx +25 -0
  161. package/src/runtime/rsc/CompositeComponent.tsx +65 -0
  162. package/src/runtime/rsc/ReplayableStream.ts +155 -0
  163. package/src/runtime/rsc/RscNodeRenderer.tsx +45 -0
  164. package/src/runtime/rsc/SlotContext.tsx +31 -0
  165. package/src/runtime/rsc/client.tsx +90 -0
  166. package/src/runtime/rsc/createRscProxy.tsx +189 -0
  167. package/src/runtime/rsc/index.ts +10 -0
  168. package/src/runtime/rsc/payloadRouter.ts +318 -0
  169. package/src/runtime/rsc/server.tsx +303 -0
  170. package/src/runtime/rsc/slotUsageSanitizer.ts +76 -0
  171. package/src/runtime/rsc/symbols.ts +106 -0
  172. package/src/runtime/ssr-shim.d.ts +12 -0
  173. package/src/runtime/types.ts +83 -0
  174. package/src/runtime/utils.tsx +161 -0
  175. package/src/runtime.ts +1 -0
  176. package/tests/router/cli.test.ts +386 -0
  177. package/tests/router/dataMutation.test.tsx +396 -0
  178. package/tests/router/prefetchLink.test.tsx +43 -0
  179. package/tests/router/routeTree.test.ts +502 -0
  180. package/tests/router/rsc.test.tsx +256 -0
  181. package/tests/router/tanstackTypes.test.ts +62 -0
  182. package/tsconfig.json +12 -0
  183. package/tsconfig.tsgo.json +6 -0
@@ -0,0 +1,53 @@
1
+ "use client";
2
+ import { createSerializationAdapter } from "@tanstack/react-router";
3
+ import { use } from "react";
4
+ import { createRscProxy } from "./createRscProxy.mjs";
5
+ let modernRscClientRuntimePromise;
6
+ function loadModernRscClientRuntime() {
7
+ modernRscClientRuntimePromise ??= import("@modern-js/runtime/rsc/client").then((runtime)=>{
8
+ runtime.setServerCallback(runtime.callServer);
9
+ return runtime;
10
+ });
11
+ return modernRscClientRuntimePromise;
12
+ }
13
+ function createTreeGetter(stream) {
14
+ let ready = false;
15
+ let tree;
16
+ const treePromise = loadModernRscClientRuntime().then((runtime)=>runtime.createFromReadableStream(stream, {
17
+ callServer: runtime.callServer
18
+ })).then((value)=>{
19
+ tree = value;
20
+ ready = true;
21
+ return value;
22
+ });
23
+ return ()=>{
24
+ if (ready) return tree;
25
+ return use(treePromise);
26
+ };
27
+ }
28
+ function createFromFlightStream(serialized) {
29
+ const streamWrapper = {
30
+ createReplayStream: ()=>serialized.stream
31
+ };
32
+ return createRscProxy(createTreeGetter(serialized.stream), {
33
+ renderable: 'renderable' === serialized.kind,
34
+ slotUsagesStream: serialized.slotUsagesStream,
35
+ stream: streamWrapper
36
+ });
37
+ }
38
+ const adapter = createSerializationAdapter({
39
+ key: '$MODERN_TANSTACK_RSC',
40
+ test: (_value)=>false,
41
+ toSerializable: ()=>{
42
+ throw new Error('TanStack RSC data cannot be serialized on client.');
43
+ },
44
+ fromSerializable: (value)=>createFromFlightStream(value)
45
+ });
46
+ function getTanstackRscSerializationAdapters() {
47
+ return [
48
+ adapter
49
+ ];
50
+ }
51
+ const rscSerializationAdapter = getTanstackRscSerializationAdapters;
52
+ export { CompositeComponent } from "./CompositeComponent.mjs";
53
+ export { getTanstackRscSerializationAdapters, rscSerializationAdapter };
@@ -0,0 +1,107 @@
1
+ "use client";
2
+ import { createElement } from "react";
3
+ import { RscNodeRenderer } from "./RscNodeRenderer.mjs";
4
+ import { RENDERABLE_RSC, RSC_PROXY_GET_TREE, RSC_PROXY_PATH, RSC_SLOT_USAGES, RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM } from "./symbols.mjs";
5
+ const UNHANDLED = Symbol('modern.tanstack.rsc.unhandled');
6
+ function handleProxyTrap(kind, prop, options) {
7
+ switch(prop){
8
+ case '__SEROVAL_STREAM__':
9
+ case '__SEROVAL_SEQUENCE__':
10
+ case Symbol.iterator:
11
+ case Symbol.asyncIterator:
12
+ return 'get' === kind ? void 0 : false;
13
+ case SERVER_COMPONENT_STREAM:
14
+ return 'get' === kind ? options.stream : Boolean(options.stream);
15
+ case RENDERABLE_RSC:
16
+ return 'get' === kind ? options.renderable : true;
17
+ case RSC_PROXY_GET_TREE:
18
+ return 'get' === kind ? options.getTree : true;
19
+ case RSC_PROXY_PATH:
20
+ return 'get' === kind ? options.path : true;
21
+ case RSC_SLOT_USAGES_STREAM:
22
+ return 'get' === kind ? options.slotUsagesStream : Boolean(options.slotUsagesStream);
23
+ case RSC_SLOT_USAGES:
24
+ return 'get' === kind ? options.slotUsages : Boolean(options.slotUsages);
25
+ case 'then':
26
+ return 'get' === kind ? void 0 : UNHANDLED;
27
+ case 'toString':
28
+ return 'get' === kind ? Object.prototype.toString : UNHANDLED;
29
+ case 'valueOf':
30
+ return 'get' === kind ? Object.prototype.valueOf : UNHANDLED;
31
+ case 'constructor':
32
+ return 'get' === kind ? Object : UNHANDLED;
33
+ default:
34
+ return UNHANDLED;
35
+ }
36
+ }
37
+ function createRscProxyInternal(options) {
38
+ const childCache = new Map();
39
+ const dataProxy = options.renderable ? createRscProxyInternal({
40
+ ...options,
41
+ renderable: false
42
+ }) : void 0;
43
+ const target = options.renderable ? /*#__PURE__*/ createElement(RscNodeRenderer, {
44
+ data: dataProxy
45
+ }) : {};
46
+ const getChild = (key)=>{
47
+ if (!childCache.has(key)) childCache.set(key, createRscProxyInternal({
48
+ ...options,
49
+ path: [
50
+ ...options.path,
51
+ key
52
+ ]
53
+ }));
54
+ return childCache.get(key);
55
+ };
56
+ return new Proxy(target, {
57
+ get (proxyTarget, prop) {
58
+ const handled = handleProxyTrap('get', prop, options);
59
+ if (handled !== UNHANDLED) return handled;
60
+ if (options.renderable) {
61
+ if ('props' === prop) return proxyTarget.props;
62
+ if ('data' === prop) return dataProxy;
63
+ if (prop in proxyTarget) return proxyTarget[prop];
64
+ }
65
+ if ('symbol' == typeof prop) return;
66
+ return getChild(prop);
67
+ },
68
+ getOwnPropertyDescriptor (proxyTarget, prop) {
69
+ return options.renderable ? Object.getOwnPropertyDescriptor(proxyTarget, prop) : void 0;
70
+ },
71
+ getPrototypeOf (proxyTarget) {
72
+ return options.renderable ? Object.getPrototypeOf(proxyTarget) : Object.prototype;
73
+ },
74
+ has (proxyTarget, prop) {
75
+ const handled = handleProxyTrap('has', prop, options);
76
+ if (handled !== UNHANDLED) return Boolean(handled);
77
+ if (options.renderable) return prop in proxyTarget || 'string' == typeof prop;
78
+ return 'string' == typeof prop;
79
+ },
80
+ ownKeys (proxyTarget) {
81
+ return options.renderable ? Reflect.ownKeys(proxyTarget) : [];
82
+ }
83
+ });
84
+ }
85
+ function createRscProxy(getTree, options = {}) {
86
+ const slotUsages = 'development' === process.env.NODE_ENV && options.slotUsagesStream ? options.slotUsages ?? [] : options.slotUsages;
87
+ if ('development' === process.env.NODE_ENV && options.slotUsagesStream && slotUsages) consumeSlotUsages(options.slotUsagesStream, slotUsages);
88
+ return createRscProxyInternal({
89
+ getTree,
90
+ path: [],
91
+ renderable: true === options.renderable,
92
+ slotUsages,
93
+ slotUsagesStream: options.slotUsagesStream,
94
+ stream: options.stream
95
+ });
96
+ }
97
+ async function consumeSlotUsages(stream, slotUsages) {
98
+ try {
99
+ const reader = stream.getReader();
100
+ for(;;){
101
+ const { value, done } = await reader.read();
102
+ if (done) break;
103
+ if (value?.slot) slotUsages.push(value);
104
+ }
105
+ } catch {}
106
+ }
107
+ export { createRscProxy };
@@ -0,0 +1 @@
1
+ export { CompositeComponent, getTanstackRscSerializationAdapters, rscSerializationAdapter } from "./client.mjs";
@@ -0,0 +1,162 @@
1
+ import { notFound, redirect } from "@tanstack/react-router";
2
+ const payloadFetchCache = new Map();
3
+ let payloadDecoder;
4
+ function getRouteId(match) {
5
+ const routeId = match.routeId ?? match.route?.id ?? match.id;
6
+ return 'string' == typeof routeId ? routeId : void 0;
7
+ }
8
+ function getRouteStaticData(match) {
9
+ return match.route?.options?.staticData || {};
10
+ }
11
+ function getRouteParentId(match) {
12
+ const parentId = match.route?.parentRoute?.id;
13
+ return 'string' == typeof parentId ? parentId : void 0;
14
+ }
15
+ function toRoutePath(match) {
16
+ const path = match.route?.options?.path;
17
+ return 'string' == typeof path ? path : void 0;
18
+ }
19
+ function toPayloadRoute(match) {
20
+ const routeId = getRouteId(match);
21
+ if (!routeId) return;
22
+ const staticData = getRouteStaticData(match);
23
+ const params = match.params && 'object' == typeof match.params ? match.params : {};
24
+ const pathname = 'string' == typeof match.pathname ? match.pathname : '';
25
+ return {
26
+ handle: staticData.modernRouteHandle,
27
+ hasAction: Boolean(staticData.modernRouteHasAction || staticData.modernRouteAction),
28
+ hasErrorBoundary: false,
29
+ hasLoader: Boolean(staticData.modernRouteHasLoader),
30
+ hasClientLoader: Boolean(staticData.modernRouteHasClientLoader),
31
+ id: routeId,
32
+ index: Boolean(match.route?.options?.index) || void 0,
33
+ params,
34
+ parentId: getRouteParentId(match),
35
+ path: toRoutePath(match),
36
+ pathname,
37
+ pathnameBase: 'string' == typeof match.pathnameBase ? match.pathnameBase : pathname
38
+ };
39
+ }
40
+ function createTanstackRscServerPayload(router, options = {}) {
41
+ const matches = Array.isArray(router.state?.matches) ? router.state.matches : [];
42
+ const routes = [];
43
+ const loaderData = {};
44
+ const errors = {};
45
+ for (const match of matches){
46
+ const payloadRoute = toPayloadRoute(match);
47
+ if (payloadRoute) {
48
+ routes.push(payloadRoute);
49
+ if ('loaderData' in match && void 0 !== match.loaderData && !(options.omitClientLoaderData && payloadRoute.hasClientLoader)) loaderData[payloadRoute.id] = match.loaderData;
50
+ if (void 0 !== match.error) errors[payloadRoute.id] = match.error;
51
+ }
52
+ }
53
+ return {
54
+ type: 'render',
55
+ actionData: null,
56
+ errors: Object.keys(errors).length > 0 ? errors : null,
57
+ loaderData,
58
+ location: router.state?.location,
59
+ routes
60
+ };
61
+ }
62
+ function handleTanstackRscRedirect(headers, basename, status) {
63
+ const newHeaders = new Headers(headers);
64
+ let redirectUrl = headers.get('Location') || '/';
65
+ if ('/' !== basename) redirectUrl = redirectUrl.replace(basename, '') || '/';
66
+ newHeaders.set('X-Modernjs-Redirect', redirectUrl);
67
+ newHeaders.set('X-Modernjs-BaseUrl', basename);
68
+ newHeaders.delete('Location');
69
+ return new Response(null, {
70
+ headers: newHeaders,
71
+ status
72
+ });
73
+ }
74
+ function isTanstackRscPayloadNavigationEnabled() {
75
+ return "u" > typeof window;
76
+ }
77
+ async function decodePayload(stream) {
78
+ if (payloadDecoder) return payloadDecoder(stream);
79
+ const runtime = await import("@modern-js/runtime/rsc/client");
80
+ return runtime.createFromReadableStream(stream);
81
+ }
82
+ function isServerPayload(value) {
83
+ return Boolean(value) && 'object' == typeof value && 'render' === value.type && Array.isArray(value.routes);
84
+ }
85
+ function createPayloadFetchKey(request) {
86
+ return request.url;
87
+ }
88
+ function isAbsoluteUrl(value) {
89
+ try {
90
+ new URL(value);
91
+ return true;
92
+ } catch {
93
+ return false;
94
+ }
95
+ }
96
+ async function fetchTanstackRscPayload(request) {
97
+ const headers = new Headers(request.headers);
98
+ headers.set('x-rsc-tree', 'true');
99
+ const response = await fetch(request.url, {
100
+ credentials: 'same-origin',
101
+ headers,
102
+ method: 'GET',
103
+ signal: request.signal
104
+ });
105
+ const redirectLocation = response.headers.get('X-Modernjs-Redirect');
106
+ if (redirectLocation) {
107
+ if (isAbsoluteUrl(redirectLocation)) throw redirect({
108
+ href: redirectLocation
109
+ });
110
+ throw redirect({
111
+ to: redirectLocation || '/'
112
+ });
113
+ }
114
+ if (404 === response.status && !response.body) throw notFound();
115
+ if (!response.body) throw new Error('TanStack RSC payload response body is null.');
116
+ const payload = await decodePayload(response.body);
117
+ if (!isServerPayload(payload)) throw new Error('Unexpected TanStack RSC payload type.');
118
+ return payload;
119
+ }
120
+ function loadTanstackRscPayload(request) {
121
+ const key = createPayloadFetchKey(request);
122
+ let payloadPromise = payloadFetchCache.get(key);
123
+ if (!payloadPromise) {
124
+ payloadPromise = fetchTanstackRscPayload(request).finally(()=>{
125
+ payloadFetchCache.delete(key);
126
+ });
127
+ payloadFetchCache.set(key, payloadPromise);
128
+ }
129
+ return payloadPromise;
130
+ }
131
+ function isSerializedNotFound(value) {
132
+ return Boolean(value) && 'object' == typeof value && true === value.isNotFound;
133
+ }
134
+ function toRouteErrors(payload) {
135
+ return payload.errors && 'object' == typeof payload.errors ? payload.errors : {};
136
+ }
137
+ function toRouteLoaderData(payload) {
138
+ return payload.loaderData && 'object' == typeof payload.loaderData ? payload.loaderData : {};
139
+ }
140
+ async function loadTanstackRscRouteData({ hasClientLoader, loadClientData, request, routeId }) {
141
+ if (hasClientLoader) return loadClientData();
142
+ if (!routeId) return loadClientData();
143
+ const payload = await loadTanstackRscPayload(request);
144
+ const errors = toRouteErrors(payload);
145
+ const routeError = errors[routeId];
146
+ if (void 0 !== routeError) {
147
+ if (isSerializedNotFound(routeError)) throw notFound({
148
+ ...routeError,
149
+ routeId
150
+ });
151
+ throw routeError;
152
+ }
153
+ const loaderData = toRouteLoaderData(payload);
154
+ if (routeId in loaderData) return loaderData[routeId];
155
+ const payloadRoute = payload.routes.find((route)=>route.id === routeId);
156
+ if (payloadRoute && payloadRoute.hasClientLoader) return loadClientData();
157
+ }
158
+ function __setTanstackRscPayloadDecoderForTests(decoder) {
159
+ payloadDecoder = decoder;
160
+ payloadFetchCache.clear();
161
+ }
162
+ export { __setTanstackRscPayloadDecoderForTests, createTanstackRscServerPayload, handleTanstackRscRedirect, isTanstackRscPayloadNavigationEnabled, loadTanstackRscPayload, loadTanstackRscRouteData };
@@ -0,0 +1,200 @@
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";
6
+ import { ClientSlot } from "./ClientSlot.mjs";
7
+ import { createRscProxy } from "./createRscProxy.mjs";
8
+ import { ReplayableStream } from "./ReplayableStream.mjs";
9
+ 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
+ }
24
+ function createSlotProxy(options) {
25
+ const cache = new Map();
26
+ return new Proxy({}, {
27
+ get (_target, prop) {
28
+ if ('then' === prop || 'string' != typeof prop) return;
29
+ if ('children' === prop) {
30
+ options?.onSlotCall?.('children', []);
31
+ return /*#__PURE__*/ createElement(ClientSlot, {
32
+ args: [],
33
+ slot: 'children'
34
+ });
35
+ }
36
+ if (!cache.has(prop)) cache.set(prop, (...args)=>{
37
+ options?.onSlotCall?.(prop, args);
38
+ return /*#__PURE__*/ createElement(ClientSlot, {
39
+ args,
40
+ slot: prop
41
+ });
42
+ });
43
+ return cache.get(prop);
44
+ }
45
+ });
46
+ }
47
+ function toReplayableFlightStream(node, handlers) {
48
+ const flightStream = renderRsc({
49
+ element: node
50
+ });
51
+ return new ReplayableStream(handlers ? wrapReadableStream(flightStream, handlers) : flightStream);
52
+ }
53
+ async function renderServerComponent(node) {
54
+ const stream = toReplayableFlightStream(/*#__PURE__*/ jsx(Fragment, {
55
+ children: node
56
+ }));
57
+ const streamWrapper = {
58
+ createReplayStream: ()=>stream.createReplayStream()
59
+ };
60
+ return createRscProxy(createTreeGetter(streamWrapper), {
61
+ renderable: true,
62
+ stream: streamWrapper
63
+ });
64
+ }
65
+ async function createCompositeComponent(component) {
66
+ const slotUsagesEmitter = 'development' === process.env.NODE_ENV ? createReadableStreamEmitter() : void 0;
67
+ const slotProxy = createSlotProxy({
68
+ onSlotCall: slotUsagesEmitter ? (slot, args)=>{
69
+ const sanitizedArgs = sanitizeSlotArgs(args);
70
+ slotUsagesEmitter.emit({
71
+ slot,
72
+ ...sanitizedArgs.length ? {
73
+ args: sanitizedArgs
74
+ } : {}
75
+ });
76
+ } : void 0
77
+ });
78
+ async function ServerComponentWrapper() {
79
+ return component(slotProxy);
80
+ }
81
+ const flightStream = toReplayableFlightStream(/*#__PURE__*/ createElement(ServerComponentWrapper), slotUsagesEmitter ? {
82
+ onCancel: slotUsagesEmitter.close,
83
+ onDone: slotUsagesEmitter.close,
84
+ onError: slotUsagesEmitter.close
85
+ } : void 0);
86
+ const streamWrapper = {
87
+ createReplayStream: ()=>flightStream.createReplayStream()
88
+ };
89
+ return createRscProxy(createTreeGetter(streamWrapper), {
90
+ renderable: false,
91
+ slotUsagesStream: slotUsagesEmitter?.stream,
92
+ stream: streamWrapper
93
+ });
94
+ }
95
+ const adapter = createSerializationAdapter({
96
+ key: '$MODERN_TANSTACK_RSC',
97
+ test: isServerComponent,
98
+ toSerializable: (component)=>{
99
+ const streamWrapper = component[SERVER_COMPONENT_STREAM];
100
+ if (!streamWrapper) throw new Error('Cannot serialize TanStack RSC without a Flight stream.');
101
+ const kind = isRenderableServerComponent(component) ? 'renderable' : 'composite';
102
+ const slotUsagesStream = 'development' === process.env.NODE_ENV && 'composite' === kind ? component[RSC_SLOT_USAGES_STREAM] : void 0;
103
+ return {
104
+ kind,
105
+ stream: new RawStream(streamWrapper.createReplayStream(), {
106
+ hint: 'text'
107
+ }),
108
+ ...slotUsagesStream ? {
109
+ slotUsagesStream
110
+ } : {}
111
+ };
112
+ },
113
+ fromSerializable: ()=>{
114
+ throw new Error('TanStack RSC data should not be deserialized on server.');
115
+ }
116
+ });
117
+ function getTanstackRscSerializationAdapters() {
118
+ return [
119
+ adapter
120
+ ];
121
+ }
122
+ const rscSerializationAdapter = getTanstackRscSerializationAdapters;
123
+ function createReadableStreamEmitter() {
124
+ let closed = false;
125
+ const queue = [];
126
+ let controller = null;
127
+ const stream = new ReadableStream({
128
+ start (ctrl) {
129
+ controller = ctrl;
130
+ for (const value of queue)ctrl.enqueue(value);
131
+ queue.length = 0;
132
+ if (closed) ctrl.close();
133
+ },
134
+ cancel () {
135
+ closed = true;
136
+ controller = null;
137
+ queue.length = 0;
138
+ }
139
+ });
140
+ const emit = (value)=>{
141
+ if (closed) return;
142
+ if (!controller) return void queue.push(value);
143
+ try {
144
+ controller.enqueue(value);
145
+ } catch {}
146
+ };
147
+ const close = ()=>{
148
+ if (closed) return;
149
+ closed = true;
150
+ try {
151
+ controller?.close();
152
+ } catch {}
153
+ controller = null;
154
+ };
155
+ return {
156
+ close,
157
+ emit,
158
+ stream
159
+ };
160
+ }
161
+ function wrapReadableStream(source, handlers) {
162
+ const reader = source.getReader();
163
+ let finished = false;
164
+ const finish = (kind)=>{
165
+ if (finished) return;
166
+ finished = true;
167
+ if ('cancel' === kind) handlers.onCancel?.();
168
+ else if ('error' === kind) handlers.onError?.();
169
+ else handlers.onDone?.();
170
+ try {
171
+ reader.releaseLock();
172
+ } catch {}
173
+ };
174
+ return new ReadableStream({
175
+ async pull (controller) {
176
+ try {
177
+ const { done, value } = await reader.read();
178
+ if (done) {
179
+ controller.close();
180
+ finish('done');
181
+ return;
182
+ }
183
+ controller.enqueue(value);
184
+ } catch (err) {
185
+ try {
186
+ controller.error(err);
187
+ } catch {}
188
+ finish('error');
189
+ }
190
+ },
191
+ async cancel (reason) {
192
+ try {
193
+ await reader.cancel(reason);
194
+ } catch {}
195
+ finish('cancel');
196
+ }
197
+ });
198
+ }
199
+ export { CompositeComponent } from "./CompositeComponent.mjs";
200
+ export { createCompositeComponent, getTanstackRscSerializationAdapters, renderServerComponent, rscSerializationAdapter };
@@ -0,0 +1,31 @@
1
+ import { isValidElement } from "react";
2
+ const REACT_ELEMENT_TYPE = Symbol.for('react.element');
3
+ const REACT_PORTAL_TYPE = Symbol.for('react.portal');
4
+ const REACT_TRANSITIONAL_ELEMENT_TYPE = Symbol.for('react.transitional.element');
5
+ function isReactElementLike(value) {
6
+ if (!value || 'object' != typeof value && 'function' != typeof value) return false;
7
+ if (isValidElement(value)) return true;
8
+ const type = value.$$typeof;
9
+ return type === REACT_ELEMENT_TYPE || type === REACT_TRANSITIONAL_ELEMENT_TYPE || type === REACT_PORTAL_TYPE;
10
+ }
11
+ function sanitizeSlotArg(value, seen) {
12
+ if (isReactElementLike(value)) return 'React element';
13
+ if (null == value) return value;
14
+ if ('string' == typeof value || 'number' == typeof value || 'boolean' == typeof value || 'bigint' == typeof value) return value;
15
+ if ('object' != typeof value && 'function' != typeof value) return String(value);
16
+ if (seen.has(value)) return '[Circular]';
17
+ seen.add(value);
18
+ if (Array.isArray(value)) return value.map((item)=>sanitizeSlotArg(item, seen));
19
+ const proto = Object.getPrototypeOf(value);
20
+ if (proto === Object.prototype || null === proto) {
21
+ const out = {};
22
+ for (const [key, item] of Object.entries(value))out[key] = sanitizeSlotArg(item, seen);
23
+ return out;
24
+ }
25
+ return String(value);
26
+ }
27
+ function sanitizeSlotArgs(args) {
28
+ const seen = new WeakSet();
29
+ return args.map((arg)=>sanitizeSlotArg(arg, seen));
30
+ }
31
+ export { sanitizeSlotArgs };
@@ -0,0 +1,17 @@
1
+ const SERVER_COMPONENT_STREAM = Symbol.for('modern.tanstack.rsc.stream');
2
+ const RENDERABLE_RSC = Symbol.for('modern.tanstack.rsc.renderable');
3
+ const RSC_PROXY_GET_TREE = Symbol.for('modern.tanstack.rsc.proxy.getTree');
4
+ const RSC_PROXY_PATH = Symbol.for('modern.tanstack.rsc.proxy.path');
5
+ const RSC_SLOT_USAGES_STREAM = Symbol.for('modern.tanstack.rsc.slotUsages.stream');
6
+ const RSC_SLOT_USAGES = Symbol.for('modern.tanstack.rsc.slotUsages');
7
+ function isServerComponent(value) {
8
+ if (null == value) return false;
9
+ if ('object' != typeof value && 'function' != typeof value) return false;
10
+ return SERVER_COMPONENT_STREAM in value && void 0 !== value[SERVER_COMPONENT_STREAM];
11
+ }
12
+ function isRenderableServerComponent(value) {
13
+ if (null == value) return false;
14
+ if ('object' != typeof value && 'function' != typeof value) return false;
15
+ return RENDERABLE_RSC in value && true === value[RENDERABLE_RSC];
16
+ }
17
+ export { RENDERABLE_RSC, RSC_PROXY_GET_TREE, RSC_PROXY_PATH, RSC_SLOT_USAGES, RSC_SLOT_USAGES_STREAM, SERVER_COMPONENT_STREAM, isRenderableServerComponent, isServerComponent };
File without changes
@@ -0,0 +1,89 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import react from "react";
3
+ import { DefaultNotFound } from "./DefaultNotFound.mjs";
4
+ function getRouteObjects(routes, { globalApp, ssrMode, props }) {
5
+ const createLayoutElement = (Component)=>{
6
+ const GlobalLayout = globalApp;
7
+ if (!GlobalLayout) return Component;
8
+ const LayoutWrapper = (props)=>{
9
+ const LayoutComponent = GlobalLayout;
10
+ return /*#__PURE__*/ jsx(LayoutComponent, {
11
+ Component: Component,
12
+ ...props
13
+ });
14
+ };
15
+ return LayoutWrapper;
16
+ };
17
+ const routeObjects = [];
18
+ for (const route of routes)if ('nested' === route.type) {
19
+ const nestedRouteObject = {
20
+ path: route.path,
21
+ id: route.id,
22
+ loader: route.loader,
23
+ action: route.action,
24
+ hasErrorBoundary: route.hasErrorBoundary,
25
+ shouldRevalidate: route.shouldRevalidate,
26
+ handle: {
27
+ ...route.handle,
28
+ ...'object' == typeof route.config ? route.config?.handle : {}
29
+ },
30
+ index: route.index,
31
+ hasLoader: Boolean(route.loader),
32
+ hasClientLoader: Boolean(route.clientData),
33
+ hasAction: Boolean(route.action),
34
+ ...route.isClientComponent ? {
35
+ isClientComponent: true
36
+ } : {},
37
+ ...route.inValidSSRRoute ? {
38
+ inValidSSRRoute: true
39
+ } : {},
40
+ lazyImport: route.lazyImport,
41
+ Component: route.component ? route.component : void 0,
42
+ errorElement: route.error ? /*#__PURE__*/ jsx(route.error, {}) : void 0,
43
+ children: route.children ? route.children.map((child)=>getRouteObjects([
44
+ child
45
+ ], {
46
+ globalApp,
47
+ ssrMode,
48
+ props
49
+ })[0]) : void 0
50
+ };
51
+ routeObjects.push(nestedRouteObject);
52
+ } else if ('function' == typeof route.component || 'object' == typeof route.component) {
53
+ const LayoutComponent = createLayoutElement(route.component);
54
+ const routeObject = {
55
+ path: route.path,
56
+ element: /*#__PURE__*/ react.createElement(LayoutComponent)
57
+ };
58
+ routeObjects.push(routeObject);
59
+ }
60
+ routeObjects.push({
61
+ path: '*',
62
+ element: /*#__PURE__*/ jsx(DefaultNotFound, {})
63
+ });
64
+ return routeObjects;
65
+ }
66
+ function createRouteObjectsFromConfig({ routesConfig, props, ssrMode }) {
67
+ if (!routesConfig) return null;
68
+ const { routes, globalApp } = routesConfig;
69
+ if (!routes) return null;
70
+ return getRouteObjects(routes, {
71
+ globalApp,
72
+ ssrMode,
73
+ props
74
+ });
75
+ }
76
+ const urlJoin = (...parts)=>{
77
+ const separator = '/';
78
+ const replace = new RegExp(`${separator}{1,}`, 'g');
79
+ return standardSlash(parts.join(separator).replace(replace, separator));
80
+ };
81
+ function standardSlash(str) {
82
+ let addr = str;
83
+ if (!addr || 'string' != typeof addr) return addr;
84
+ if (addr.startsWith('.')) addr = addr.slice(1);
85
+ if (!addr.startsWith('/')) addr = `/${addr}`;
86
+ if (addr.endsWith('/') && '/' !== addr) addr = addr.slice(0, addr.length - 1);
87
+ return addr;
88
+ }
89
+ export { createRouteObjectsFromConfig, getRouteObjects, standardSlash, urlJoin };
@@ -0,0 +1 @@
1
+ export * from "./runtime/index.mjs";