@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.
- package/dist/cjs/cli/artifacts.js +157 -0
- package/dist/cjs/cli/index.js +8 -79
- package/dist/cjs/cli/tanstackTypes/canonicalRoutes.js +113 -0
- package/dist/cjs/cli/tanstackTypes/shared.js +130 -0
- package/dist/cjs/cli/tanstackTypes/source.js +251 -0
- package/dist/cjs/cli/tanstackTypes.js +4 -350
- package/dist/cjs/runtime/blockingSubscribe.js +64 -0
- package/dist/cjs/runtime/{pluginCore.js → clientBasepath.js} +12 -19
- package/dist/cjs/runtime/clientHydration.js +129 -0
- package/dist/cjs/runtime/dataMutation.js +11 -176
- package/dist/cjs/runtime/formData.js +116 -0
- package/dist/cjs/runtime/loaderBridge.js +4 -1
- package/dist/cjs/runtime/plugin.js +21 -125
- package/dist/cjs/runtime/plugin.node.js +45 -125
- package/dist/cjs/runtime/pluginShared.js +90 -0
- package/dist/cjs/runtime/prefetchLink.js +15 -2
- package/dist/cjs/runtime/routeTree/components.js +102 -0
- package/dist/cjs/runtime/routeTree/construction.js +152 -0
- package/dist/cjs/runtime/routeTree/index.js +47 -0
- package/dist/cjs/runtime/routeTree/loaders.js +158 -0
- package/dist/cjs/runtime/routeTree/paths.js +57 -0
- package/dist/cjs/runtime/routeTree/staticData.js +63 -0
- package/dist/cjs/runtime/routeTree/tanstackRoutes.js +57 -0
- package/dist/cjs/runtime/routeTree/types.js +18 -0
- package/dist/cjs/runtime/routeTree.js +7 -320
- package/dist/cjs/runtime/rsc/CompositeComponent.js +18 -13
- package/dist/cjs/runtime/rsc/RscNodeRenderer.js +5 -16
- package/dist/cjs/runtime/rsc/client.js +8 -25
- package/dist/cjs/runtime/rsc/flightSerialization.js +132 -0
- package/dist/cjs/runtime/rsc/payloadErrors.js +99 -0
- package/dist/cjs/runtime/rsc/payloadFetch.js +117 -0
- package/dist/cjs/runtime/rsc/payloadRouter.js +21 -142
- package/dist/cjs/runtime/rsc/payloadRoutes.js +106 -0
- package/dist/cjs/runtime/rsc/payloadStorage.js +58 -0
- package/dist/cjs/runtime/rsc/server.js +14 -18
- package/dist/cjs/runtime/rsc/shared.js +71 -0
- package/dist/cjs/runtime/rsc/treeGetter.js +72 -0
- package/dist/cjs/runtime/ssrManagedTags.js +103 -0
- package/dist/cjs/runtime/ssrPreload.js +89 -0
- package/dist/cjs/runtime/ssrTypes.js +18 -0
- package/dist/cjs/runtime/submitAction.js +161 -0
- package/dist/cjs/runtime/utils.js +49 -86
- package/dist/esm/cli/artifacts.mjs +103 -0
- package/dist/esm/cli/index.mjs +4 -78
- package/dist/esm/cli/tanstackTypes/canonicalRoutes.mjs +75 -0
- package/dist/esm/cli/tanstackTypes/shared.mjs +68 -0
- package/dist/esm/cli/tanstackTypes/source.mjs +203 -0
- package/dist/esm/cli/tanstackTypes.mjs +2 -339
- package/dist/esm/runtime/blockingSubscribe.mjs +19 -0
- package/dist/esm/runtime/clientBasepath.mjs +10 -0
- package/dist/esm/runtime/clientHydration.mjs +70 -0
- package/dist/esm/runtime/dataMutation.mjs +2 -167
- package/dist/esm/runtime/formData.mjs +63 -0
- package/dist/esm/runtime/loaderBridge.mjs +4 -1
- package/dist/esm/runtime/plugin.mjs +14 -118
- package/dist/esm/runtime/plugin.node.mjs +41 -121
- package/dist/esm/runtime/pluginShared.mjs +40 -0
- package/dist/esm/runtime/prefetchLink.mjs +18 -5
- package/dist/esm/runtime/routeTree/components.mjs +55 -0
- package/dist/esm/runtime/routeTree/construction.mjs +111 -0
- package/dist/esm/runtime/routeTree/index.mjs +2 -0
- package/dist/esm/runtime/routeTree/loaders.mjs +117 -0
- package/dist/esm/runtime/routeTree/paths.mjs +16 -0
- package/dist/esm/runtime/routeTree/staticData.mjs +22 -0
- package/dist/esm/runtime/routeTree/tanstackRoutes.mjs +13 -0
- package/dist/esm/runtime/routeTree/types.mjs +0 -0
- package/dist/esm/runtime/routeTree.mjs +1 -318
- package/dist/esm/runtime/rsc/CompositeComponent.mjs +18 -13
- package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +4 -15
- package/dist/esm/runtime/rsc/client.mjs +6 -26
- package/dist/esm/runtime/rsc/flightSerialization.mjs +88 -0
- package/dist/esm/runtime/rsc/payloadErrors.mjs +49 -0
- package/dist/esm/runtime/rsc/payloadFetch.mjs +61 -0
- package/dist/esm/runtime/rsc/payloadRouter.mjs +12 -133
- package/dist/esm/runtime/rsc/payloadRoutes.mjs +50 -0
- package/dist/esm/runtime/rsc/payloadStorage.mjs +14 -0
- package/dist/esm/runtime/rsc/server.mjs +17 -21
- package/dist/esm/runtime/rsc/shared.mjs +21 -0
- package/dist/esm/runtime/rsc/treeGetter.mjs +34 -0
- package/dist/esm/runtime/ssrManagedTags.mjs +53 -0
- package/dist/esm/runtime/ssrPreload.mjs +51 -0
- package/dist/esm/runtime/ssrTypes.mjs +0 -0
- package/dist/esm/runtime/submitAction.mjs +120 -0
- package/dist/esm/runtime/utils.mjs +47 -75
- package/dist/esm-node/cli/artifacts.mjs +104 -0
- package/dist/esm-node/cli/index.mjs +4 -78
- package/dist/esm-node/cli/tanstackTypes/canonicalRoutes.mjs +76 -0
- package/dist/esm-node/cli/tanstackTypes/shared.mjs +69 -0
- package/dist/esm-node/cli/tanstackTypes/source.mjs +204 -0
- package/dist/esm-node/cli/tanstackTypes.mjs +2 -339
- package/dist/esm-node/runtime/blockingSubscribe.mjs +20 -0
- package/dist/esm-node/runtime/clientBasepath.mjs +11 -0
- package/dist/esm-node/runtime/clientHydration.mjs +71 -0
- package/dist/esm-node/runtime/dataMutation.mjs +2 -167
- package/dist/esm-node/runtime/formData.mjs +64 -0
- package/dist/esm-node/runtime/loaderBridge.mjs +4 -1
- package/dist/esm-node/runtime/plugin.mjs +14 -118
- package/dist/esm-node/runtime/plugin.node.mjs +41 -121
- package/dist/esm-node/runtime/pluginShared.mjs +41 -0
- package/dist/esm-node/runtime/prefetchLink.mjs +18 -5
- package/dist/esm-node/runtime/routeTree/components.mjs +56 -0
- package/dist/esm-node/runtime/routeTree/construction.mjs +112 -0
- package/dist/esm-node/runtime/routeTree/index.mjs +3 -0
- package/dist/esm-node/runtime/routeTree/loaders.mjs +118 -0
- package/dist/esm-node/runtime/routeTree/paths.mjs +17 -0
- package/dist/esm-node/runtime/routeTree/staticData.mjs +23 -0
- package/dist/esm-node/runtime/routeTree/tanstackRoutes.mjs +14 -0
- package/dist/esm-node/runtime/routeTree/types.mjs +1 -0
- package/dist/esm-node/runtime/routeTree.mjs +1 -318
- package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +18 -13
- package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +4 -15
- package/dist/esm-node/runtime/rsc/client.mjs +6 -26
- package/dist/esm-node/runtime/rsc/flightSerialization.mjs +89 -0
- package/dist/esm-node/runtime/rsc/payloadErrors.mjs +50 -0
- package/dist/esm-node/runtime/rsc/payloadFetch.mjs +62 -0
- package/dist/esm-node/runtime/rsc/payloadRouter.mjs +12 -133
- package/dist/esm-node/runtime/rsc/payloadRoutes.mjs +51 -0
- package/dist/esm-node/runtime/rsc/payloadStorage.mjs +15 -0
- package/dist/esm-node/runtime/rsc/server.mjs +17 -21
- package/dist/esm-node/runtime/rsc/shared.mjs +22 -0
- package/dist/esm-node/runtime/rsc/treeGetter.mjs +35 -0
- package/dist/esm-node/runtime/ssrManagedTags.mjs +54 -0
- package/dist/esm-node/runtime/ssrPreload.mjs +52 -0
- package/dist/esm-node/runtime/ssrTypes.mjs +1 -0
- package/dist/esm-node/runtime/submitAction.mjs +121 -0
- package/dist/esm-node/runtime/utils.mjs +47 -75
- package/dist/types/cli/artifacts.d.ts +33 -0
- package/dist/types/cli/index.d.ts +2 -22
- package/dist/types/cli/tanstackTypes/canonicalRoutes.d.ts +22 -0
- package/dist/types/cli/tanstackTypes/shared.d.ts +20 -0
- package/dist/types/cli/tanstackTypes/source.d.ts +10 -0
- package/dist/types/cli/tanstackTypes.d.ts +3 -31
- package/dist/types/runtime/blockingSubscribe.d.ts +3 -0
- package/dist/types/runtime/clientBasepath.d.ts +1 -0
- package/dist/types/runtime/clientHydration.d.ts +19 -0
- package/dist/types/runtime/dataMutation.d.ts +3 -12
- package/dist/types/runtime/formData.d.ts +23 -0
- package/dist/types/runtime/plugin.d.ts +1 -1
- package/dist/types/runtime/plugin.node.d.ts +1 -1
- package/dist/types/runtime/pluginShared.d.ts +39 -0
- package/dist/types/runtime/routeTree/components.d.ts +7 -0
- package/dist/types/runtime/routeTree/construction.d.ts +5 -0
- package/dist/types/runtime/routeTree/index.d.ts +2 -0
- package/dist/types/runtime/routeTree/loaders.d.ts +4 -0
- package/dist/types/runtime/routeTree/paths.d.ts +3 -0
- package/dist/types/runtime/routeTree/staticData.d.ts +19 -0
- package/dist/types/runtime/routeTree/tanstackRoutes.d.ts +4 -0
- package/dist/types/runtime/routeTree/types.d.ts +75 -0
- package/dist/types/runtime/routeTree.d.ts +1 -9
- package/dist/types/runtime/rsc/CompositeComponent.d.ts +1 -2
- package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +2 -3
- package/dist/types/runtime/rsc/client.d.ts +2 -0
- package/dist/types/runtime/rsc/flightSerialization.d.ts +3 -0
- package/dist/types/runtime/rsc/payloadErrors.d.ts +6 -0
- package/dist/types/runtime/rsc/payloadFetch.d.ts +10 -0
- package/dist/types/runtime/rsc/payloadRouter.d.ts +2 -10
- package/dist/types/runtime/rsc/payloadRoutes.d.ts +43 -0
- package/dist/types/runtime/rsc/payloadStorage.d.ts +5 -0
- package/dist/types/runtime/rsc/shared.d.ts +19 -0
- package/dist/types/runtime/rsc/treeGetter.d.ts +1 -0
- package/dist/types/runtime/ssrManagedTags.d.ts +7 -0
- package/dist/types/runtime/ssrPreload.d.ts +2 -0
- package/dist/types/runtime/ssrTypes.d.ts +43 -0
- package/dist/types/runtime/state.d.ts +1 -1
- package/dist/types/runtime/submitAction.d.ts +14 -0
- package/dist/types/runtime/types.d.ts +4 -9
- package/dist/types/runtime/utils.d.ts +4 -24
- package/package.json +30 -8
- package/rstest.config.mts +3 -0
- package/src/cli/artifacts.ts +255 -0
- package/src/cli/index.ts +7 -185
- package/src/cli/tanstackTypes/canonicalRoutes.ts +157 -0
- package/src/cli/tanstackTypes/shared.ts +119 -0
- package/src/cli/tanstackTypes/source.ts +405 -0
- package/src/cli/tanstackTypes.ts +4 -647
- package/src/runtime/blockingSubscribe.ts +48 -0
- package/src/runtime/clientBasepath.ts +12 -0
- package/src/runtime/clientHydration.ts +151 -0
- package/src/runtime/dataMutation.tsx +9 -306
- package/src/runtime/formData.ts +153 -0
- package/src/runtime/loaderBridge.ts +8 -3
- package/src/runtime/plugin.node.tsx +76 -291
- package/src/runtime/plugin.tsx +21 -257
- package/src/runtime/pluginShared.ts +112 -0
- package/src/runtime/prefetchLink.tsx +78 -15
- package/src/runtime/routeTree/components.ts +130 -0
- package/src/runtime/routeTree/construction.ts +238 -0
- package/src/runtime/routeTree/index.ts +7 -0
- package/src/runtime/routeTree/loaders.ts +232 -0
- package/src/runtime/routeTree/paths.ts +36 -0
- package/src/runtime/routeTree/staticData.ts +66 -0
- package/src/runtime/routeTree/tanstackRoutes.ts +36 -0
- package/src/runtime/routeTree/types.ts +87 -0
- package/src/runtime/routeTree.ts +5 -750
- package/src/runtime/rsc/CompositeComponent.tsx +29 -20
- package/src/runtime/rsc/RscNodeRenderer.tsx +9 -27
- package/src/runtime/rsc/client.tsx +12 -40
- package/src/runtime/rsc/flightSerialization.ts +158 -0
- package/src/runtime/rsc/payloadErrors.ts +73 -0
- package/src/runtime/rsc/payloadFetch.ts +97 -0
- package/src/runtime/rsc/payloadRouter.ts +29 -234
- package/src/runtime/rsc/payloadRoutes.ts +122 -0
- package/src/runtime/rsc/payloadStorage.ts +35 -0
- package/src/runtime/rsc/server.tsx +20 -29
- package/src/runtime/rsc/shared.ts +70 -0
- package/src/runtime/rsc/treeGetter.tsx +52 -0
- package/src/runtime/ssrManagedTags.ts +107 -0
- package/src/runtime/ssrPreload.ts +125 -0
- package/src/runtime/ssrTypes.ts +51 -0
- package/src/runtime/state.ts +4 -2
- package/src/runtime/submitAction.ts +194 -0
- package/src/runtime/types.ts +6 -13
- package/src/runtime/utils.tsx +76 -110
- package/src/types/react-server-dom-rspack.d.ts +56 -0
- package/tests/castRatchet.test.ts +85 -0
- package/tests/router/cli.test.ts +7 -0
- package/tests/router/dataMutation.test.tsx +59 -0
- package/tests/router/fixtures/router-gen.golden.txt +72 -0
- package/tests/router/generateRouteArtifacts.test.ts +106 -0
- package/tests/router/loaderBridge.test.ts +21 -0
- package/tests/router/prefetchLink.test.tsx +106 -59
- package/tests/router/prefetchLinkPreload.test.tsx +85 -0
- package/tests/router/routeTree.test.ts +46 -11
- package/tests/router/rsc.test.tsx +189 -0
- package/tests/router/ssrPreload.test.ts +30 -0
- package/tests/router/tanstackTypes.test.ts +254 -1
- package/dist/esm/runtime/pluginCore.mjs +0 -14
- package/dist/esm-node/runtime/pluginCore.mjs +0 -15
- package/dist/types/runtime/pluginCore.d.ts +0 -21
- package/src/runtime/pluginCore.ts +0 -48
|
@@ -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 {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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)
|
|
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
|
|
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 {
|
|
3
|
-
import { createSerializationAdapter } from "@tanstack/
|
|
4
|
-
import {
|
|
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
|
|
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
|
|
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, '&').replace(/"/g, '"');
|
|
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 };
|