@teyik0/furin 0.2.0-alpha.5 → 0.4.0-alpha.1
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/README.md +2 -3
- package/dist/adapter/bun.d.ts +1 -1
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/client.d.ts +2 -2
- package/dist/build/entry-template.d.ts +6 -0
- package/dist/build/entry-template.ts +20 -3
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +9363 -5827
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +3 -23
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +2 -1
- package/dist/cli/index.js +9467 -5870
- package/dist/client/boundaries.d.ts +6 -4
- package/dist/client/define-route.d.ts +35 -0
- package/dist/client/document.d.ts +26 -0
- package/dist/client/hmr.d.ts +10 -0
- package/dist/client/internal/runtime-types.d.ts +32 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +1884 -1227
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +2 -2
- package/dist/client/router/link-utils.d.ts +14 -1
- package/dist/client/router/search/index.d.ts +3 -7
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +34 -26
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +11 -169
- package/dist/client.js +197 -36
- package/dist/config.d.ts +8 -8
- package/dist/config.js +10 -10
- package/dist/define-route.d.ts +1355 -0
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +24 -39
- package/dist/furin.js +14432 -7929
- package/dist/isomorphic.d.ts +18 -0
- package/dist/plugin/binding-scope.d.ts +2 -0
- package/dist/plugin/dead-code-elimination.d.ts +3 -0
- package/dist/plugin/index.js +1708 -489
- package/dist/plugin/route-config-autofix.d.ts +31 -0
- package/dist/plugin/routes.d.ts +27 -0
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +1 -0
- package/dist/rsc/build/environment.d.ts +2 -0
- package/dist/rsc/build/index.d.ts +15 -0
- package/dist/rsc/client-codec.d.ts +1 -0
- package/dist/rsc/codec.d.ts +4 -0
- package/dist/rsc/flight-drain.d.ts +11 -0
- package/dist/rsc/render-error.d.ts +17 -0
- package/dist/rsc/server-codec.d.ts +2 -0
- package/dist/rsc/server-codec.js +24 -0
- package/dist/rsc/shared.d.ts +30 -0
- package/dist/rsc/symbols.d.ts +2 -0
- package/dist/rsc/transport-benchmark.d.ts +13 -0
- package/dist/rsc/version.d.ts +7 -0
- package/dist/rsc-client.d.ts +4 -0
- package/dist/rsc-client.js +166 -0
- package/dist/rsc-server.d.ts +11 -0
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +353 -0
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +2 -3
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +9 -0
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/store-view.d.ts +7 -7
- package/dist/server/dev-page-plugin.d.ts +11 -5
- package/dist/server/devtools/hub.d.ts +11 -0
- package/dist/server/devtools/instrumentation.d.ts +22 -0
- package/dist/server/devtools/instrumentation.production.d.ts +44 -0
- package/dist/server/devtools/plugin.d.ts +3 -0
- package/dist/server/devtools/request-context.d.ts +6 -0
- package/dist/server/internal.d.ts +5 -0
- package/dist/server/internal.js +3 -3
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/logger.d.ts +4 -0
- package/dist/server/render/assemble.d.ts +4 -9
- package/dist/server/render/document.d.ts +4 -0
- package/dist/server/render/element.d.ts +3 -2
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -3
- package/dist/server/render/isr.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +9 -1
- package/dist/server/render/not-found.d.ts +2 -2
- package/dist/server/render/ppr-route.d.ts +8 -0
- package/dist/server/render/route-frame-transport.d.ts +2 -0
- package/dist/server/render/shell.ts +0 -1
- package/dist/server/render/ssg.d.ts +1 -1
- package/dist/server/render/ssr.d.ts +9 -9
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +24 -0
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +17 -9
- package/dist/server/router/patterns.d.ts +7 -1
- package/dist/server/router/plugin.d.ts +8 -10
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +14 -9
- package/dist/server/router/source-version.d.ts +2 -0
- package/dist/server/router/types.d.ts +2 -1
- package/dist/server/runtime-env.js +2 -2
- package/dist/server/sync/adapter.d.ts +45 -26
- package/dist/server/sync/config.d.ts +5 -9
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +3023 -0
- package/dist/server/sync/notifier.d.ts +18 -0
- package/dist/server/sync/plugin.d.ts +53 -54
- package/dist/server/sync/postgres/index.d.ts +19 -0
- package/dist/server/sync/postgres/index.js +252 -0
- package/dist/server/sync/postgres/migrate.d.ts +2 -0
- package/dist/server/sync/postgres/migrate.js +17 -0
- package/dist/server/sync/postgres/migration.sql +66 -0
- package/dist/server/sync/redis/index.d.ts +30 -0
- package/dist/server/sync/redis/index.js +336 -0
- package/dist/server/sync/redis/scripts.d.ts +4 -0
- package/dist/server/sync/response.d.ts +10 -1
- package/dist/server/sync/runtime.d.ts +6 -0
- package/dist/server/sync/sqlite/index.d.ts +20 -0
- package/dist/server/sync/sqlite/index.js +250 -0
- package/dist/server/sync/stream.d.ts +12 -11
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/await.d.ts +6 -7
- package/dist/shared/elysia-contract.d.ts +41 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/route-map.d.ts +5 -0
- package/dist/shared/utils/index.d.ts +1 -3
- package/package.json +86 -41
- package/src/adapter/bun.ts +113 -36
- package/src/adapter/package.ts +23 -15
- package/src/adapter/static.ts +97 -50
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +41 -26
- package/src/build/entry-template.ts +20 -3
- package/src/build/hydrate.ts +128 -33
- package/src/build/index.ts +14 -2
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +20 -116
- package/src/build/scan-server.ts +3 -2
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +4 -2
- package/src/build/types.ts +2 -1
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +58 -27
- package/src/client/boundaries.tsx +51 -19
- package/src/client/default-screens.tsx +4 -4
- package/src/client/define-route.ts +48 -0
- package/src/client/document.tsx +165 -0
- package/src/client/hmr.ts +40 -0
- package/src/client/internal/runtime-types.ts +33 -0
- package/src/client/link.tsx +51 -38
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +4 -0
- package/src/client/router/link-utils.ts +92 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +98 -53
- package/src/client/router/search/index.ts +9 -19
- package/src/client/router/search-store.ts +5 -1
- package/src/client/router/spa-response.ts +3 -4
- package/src/client/router/sync-catch-up.ts +9 -8
- package/src/client/router/types.ts +52 -26
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +28 -283
- package/src/config.ts +10 -10
- package/src/define-route.ts +959 -0
- package/src/devtools/devtools-element.js +885 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +475 -136
- package/src/isomorphic.ts +66 -0
- package/src/plugin/binding-scope.ts +158 -0
- package/src/plugin/dead-code-elimination.ts +202 -0
- package/src/plugin/index.ts +14 -18
- package/src/plugin/route-config-autofix.ts +1140 -0
- package/src/plugin/routes.ts +891 -0
- package/src/plugin/transform-client.ts +160 -330
- package/src/plugin/transform-isomorphic.ts +464 -0
- package/src/routes.ts +6 -0
- package/src/rsc/build/environment.ts +30 -0
- package/src/rsc/build/index.ts +94 -0
- package/src/rsc/client-codec.ts +11 -0
- package/src/rsc/codec.ts +85 -0
- package/src/rsc/flight-drain.ts +68 -0
- package/src/rsc/react-server-dom-webpack.d.ts +23 -0
- package/src/rsc/render-error.ts +120 -0
- package/src/rsc/server-codec.ts +25 -0
- package/src/rsc/shared.tsx +113 -0
- package/src/rsc/symbols.ts +2 -0
- package/src/rsc/transport-benchmark.ts +73 -0
- package/src/rsc/version.ts +57 -0
- package/src/rsc-client.ts +12 -0
- package/src/rsc-server.tsx +91 -0
- package/src/rsc.tsx +75 -0
- package/src/server/auto-invalidate/registry.ts +2 -2
- package/src/server/auto-invalidate/runtime.ts +32 -6
- package/src/server/cache/dev-loader.ts +70 -10
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +33 -15
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +84 -9
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/store-view.ts +7 -7
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +38 -17
- package/src/server/devtools/hub.ts +64 -0
- package/src/server/devtools/instrumentation.production.ts +80 -0
- package/src/server/devtools/instrumentation.ts +72 -0
- package/src/server/devtools/plugin.ts +269 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +4 -4
- package/src/server/internal.ts +5 -0
- package/src/server/lang-detect.ts +14 -10
- package/src/server/logger.ts +21 -0
- package/src/server/render/assemble.ts +58 -36
- package/src/server/render/document.tsx +25 -0
- package/src/server/render/element.tsx +24 -9
- package/src/server/render/index.ts +6 -3
- package/src/server/render/isr.ts +182 -117
- package/src/server/render/loaders.ts +289 -50
- package/src/server/render/not-found.ts +57 -38
- package/src/server/render/ppr-route.ts +146 -0
- package/src/server/render/route-frame-transport.ts +62 -0
- package/src/server/render/shell.ts +0 -1
- package/src/server/render/ssg.ts +7 -6
- package/src/server/render/ssr.ts +412 -166
- package/src/server/render/template.ts +93 -2
- package/src/server/router/defined-route.ts +110 -0
- package/src/server/router/discovery.ts +165 -48
- package/src/server/router/hmr.ts +126 -53
- package/src/server/router/patterns.ts +56 -27
- package/src/server/router/plugin.ts +210 -200
- package/src/server/router/schema-merge.ts +42 -0
- package/src/server/router/schemas.ts +61 -76
- package/src/server/router/source-version.ts +16 -0
- package/src/server/router/types.ts +3 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +9 -27
- package/src/server/sync/fingerprint.ts +8 -3
- package/src/server/sync/index.ts +37 -13
- package/src/server/sync/notifier.ts +109 -0
- package/src/server/sync/plugin.ts +184 -89
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +16 -0
- package/src/server/sync/postgres/migration.sql +66 -0
- package/src/server/sync/redis/index.ts +342 -0
- package/src/server/sync/redis/scripts.ts +74 -0
- package/src/server/sync/response.ts +145 -16
- package/src/server/sync/runtime.ts +27 -0
- package/src/server/sync/sql.d.ts +4 -0
- package/src/server/sync/sqlite/index.ts +337 -0
- package/src/server/sync/sqlite/migration.sql +70 -0
- package/src/server/sync/stream.ts +137 -81
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +10 -11
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/elysia-contract.ts +63 -0
- package/src/shared/parser.ts +5 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/route-map.ts +37 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +6 -22
- package/dist/build/server-routes-entry.d.ts +0 -13
- package/dist/build/server-routes-entry.ts +0 -30
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/router/index.d.ts +0 -6
- package/dist/server/router/index.js +0 -27
- package/dist/server/router/index.ts +0 -28
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/build/server-routes-entry.ts +0 -30
- package/src/server/dev-inspector.ts +0 -60
- package/src/server/router/index.ts +0 -28
- package/src/server/sync/memory-adapter.ts +0 -196
|
@@ -1,35 +1,140 @@
|
|
|
1
1
|
import { type AnyElysia, type Context, Elysia, t } from "elysia";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { toCrossJSONAsync } from "seroval";
|
|
3
|
+
import type { HeadOptions } from "../../client.ts";
|
|
4
4
|
import { computeErrorDigest } from "../../shared/digest.ts";
|
|
5
|
+
import type { FurinSchema } from "../../shared/elysia-contract.ts";
|
|
6
|
+
import { containsRscSource, serializeRouteFrames } from "../../shared/route-frame.ts";
|
|
5
7
|
import type { SearchParamsInput, SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
6
|
-
import { autoInvalidateRegistry } from "../auto-invalidate/registry.ts";
|
|
7
8
|
import { useLogger } from "../context-logger.ts";
|
|
9
|
+
import {
|
|
10
|
+
currentInstrumentationRequest,
|
|
11
|
+
emitPayloadSerialized,
|
|
12
|
+
} from "../devtools/instrumentation.ts";
|
|
8
13
|
import { injectSyncRuntimeScript, resolvePath } from "../render/assemble.ts";
|
|
14
|
+
import { handleISR } from "../render/isr.ts";
|
|
9
15
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
renderSSR,
|
|
16
|
+
hasRequestLoader,
|
|
17
|
+
type LoaderResult,
|
|
13
18
|
runLoaders,
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
runPublicLoaders,
|
|
20
|
+
withRequestLoaderData,
|
|
21
|
+
} from "../render/loaders.ts";
|
|
22
|
+
import { renderPprRoute } from "../render/ppr-route.ts";
|
|
23
|
+
import { createDeferredRouteFrameStream } from "../render/route-frame-transport.ts";
|
|
16
24
|
import { extractTitle } from "../render/shell.ts";
|
|
25
|
+
import { prerenderSSG } from "../render/ssg.ts";
|
|
26
|
+
import { renderSSR } from "../render/ssr.ts";
|
|
17
27
|
import { IS_DEV } from "../runtime-env.ts";
|
|
18
28
|
import { handleDevRequest } from "./hmr.ts";
|
|
19
29
|
import { buildRouteMatcher } from "./patterns.ts";
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
import { mergeRouteSchemas } from "./schema-merge.ts";
|
|
31
|
+
import { parseDataEndpointPath, parseRouteParams, parseRouteQuery } from "./schemas.ts";
|
|
32
|
+
import type { ResolvedRoute, ResolvedRoutesSource, RootLayout } from "./types.ts";
|
|
33
|
+
|
|
34
|
+
const MAX_NAVIGATION_HEAD_BYTES = 64 * 1024;
|
|
35
|
+
|
|
36
|
+
interface DataRouteParamsInput {
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
27
39
|
|
|
28
40
|
type SyntheticDataContext = Omit<Context, "params" | "query"> & {
|
|
29
|
-
params:
|
|
41
|
+
params: DataRouteParamsInput;
|
|
30
42
|
query: SearchParamsInput;
|
|
31
43
|
};
|
|
32
44
|
|
|
45
|
+
function emitSerializedPayload(body: string, kind: "route-data" | "rsc", requestUrl: string): void {
|
|
46
|
+
if (!IS_DEV) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const request = currentInstrumentationRequest();
|
|
50
|
+
if (request === undefined) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
emitPayloadSerialized({
|
|
54
|
+
bytes: new TextEncoder().encode(body).byteLength,
|
|
55
|
+
kind,
|
|
56
|
+
operationId: request.operationId,
|
|
57
|
+
path: new URL(requestUrl).pathname,
|
|
58
|
+
requestId: request.requestId,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function runDataEndpointLoaders(route: ResolvedRoute, ctx: Context): Promise<LoaderResult> {
|
|
63
|
+
if (route.mode !== "isr" && route.mode !== "ssg") {
|
|
64
|
+
return runLoaders(route, ctx);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = await runPublicLoaders(route, ctx);
|
|
68
|
+
if (result.type !== "data" || !hasRequestLoader(route)) {
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
return withRequestLoaderData(route, ctx, result);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function createLoaderDataResponse(
|
|
75
|
+
result: LoaderResult,
|
|
76
|
+
route: ResolvedRoute,
|
|
77
|
+
requestUrl: string
|
|
78
|
+
): Promise<Response> {
|
|
79
|
+
if (result.type === "redirect") {
|
|
80
|
+
const redirectUrl = new URL(result.response.headers.get("location") ?? "/", requestUrl);
|
|
81
|
+
const serialized = await toCrossJSONAsync({
|
|
82
|
+
__furinRedirect: redirectUrl.pathname + redirectUrl.search,
|
|
83
|
+
});
|
|
84
|
+
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
85
|
+
headers: { "content-type": "application/x-ndjson" },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (result.type === "not-found") {
|
|
89
|
+
const serialized = await toCrossJSONAsync({
|
|
90
|
+
__furinNotFound: { data: result.error.data, message: result.error.message },
|
|
91
|
+
__furinStatus: 404,
|
|
92
|
+
});
|
|
93
|
+
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
94
|
+
headers: { "content-type": "application/x-ndjson" },
|
|
95
|
+
status: 200,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (result.type === "error") {
|
|
99
|
+
const serialized = await toCrossJSONAsync({
|
|
100
|
+
__furinError: {
|
|
101
|
+
digest: computeErrorDigest(result.error),
|
|
102
|
+
message: result.message,
|
|
103
|
+
status: result.status,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
107
|
+
headers: { "content-type": "application/x-ndjson" },
|
|
108
|
+
status: result.status,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const syncDataWithTitle = withResolvedHead(route, result.syncData);
|
|
113
|
+
if (result.deferredPromises !== undefined) {
|
|
114
|
+
return new Response(
|
|
115
|
+
createDeferredRouteFrameStream(syncDataWithTitle, result.deferredPromises),
|
|
116
|
+
{
|
|
117
|
+
headers: {
|
|
118
|
+
...result.headers,
|
|
119
|
+
"content-type": "application/x-furin-route",
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const body = serializeRouteFrames(syncDataWithTitle, undefined);
|
|
125
|
+
emitSerializedPayload(
|
|
126
|
+
body,
|
|
127
|
+
containsRscSource(syncDataWithTitle) ? "rsc" : "route-data",
|
|
128
|
+
requestUrl
|
|
129
|
+
);
|
|
130
|
+
return new Response(body, {
|
|
131
|
+
headers: {
|
|
132
|
+
...result.headers,
|
|
133
|
+
"content-type": "application/x-furin-route",
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
33
138
|
/** @internal Handles a production SSG route — sets ETags, Cache-Control, and Cache-Tag. */
|
|
34
139
|
async function handleSSGRequest(
|
|
35
140
|
route: ResolvedRoute,
|
|
@@ -38,8 +143,8 @@ async function handleSSGRequest(
|
|
|
38
143
|
buildId: string,
|
|
39
144
|
searchRoutes: SearchRouteMetadata[] | undefined
|
|
40
145
|
): Promise<unknown> {
|
|
41
|
-
const origin = new URL(ctx.request.url)
|
|
42
|
-
const entry = await prerenderSSG(route, ctx.params, root, origin, undefined, searchRoutes);
|
|
146
|
+
const { origin } = new URL(ctx.request.url);
|
|
147
|
+
const entry = await prerenderSSG(route, ctx.params ?? {}, root, origin, undefined, searchRoutes);
|
|
43
148
|
|
|
44
149
|
// Loader issued a redirect — forward it directly to the client.
|
|
45
150
|
if (entry instanceof Response) {
|
|
@@ -84,62 +189,65 @@ export function createRoutePlugin(
|
|
|
84
189
|
|
|
85
190
|
if (allParams || allQuery) {
|
|
86
191
|
plugin.guard({
|
|
87
|
-
params: allParams as
|
|
88
|
-
query: allQuery as
|
|
192
|
+
params: allParams as FurinSchema,
|
|
193
|
+
query: allQuery as FurinSchema,
|
|
89
194
|
});
|
|
90
195
|
}
|
|
91
196
|
|
|
92
|
-
plugin.get(pattern, (ctx) =>
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// - renderDevISRWithLoaderCache (mode === "isr")
|
|
96
|
-
// - renderDevSSGWithLoaderCache (mode === "ssg")
|
|
97
|
-
// - renderSSR (otherwise)
|
|
98
|
-
//
|
|
99
|
-
// Only the LOADER OUTPUT is cached in dev — HTML is always re-assembled
|
|
100
|
-
// fresh so the response always embeds the latest Bun client chunk URL.
|
|
101
|
-
// This avoids the "infinite reload loop" footgun where a cached ISR/SSG
|
|
102
|
-
// HTML response held an OLD chunk URL after Bun rebundled. The dev
|
|
103
|
-
// cache is invalidated source-aware via `isDevLoaderCacheValid`
|
|
104
|
-
// (mtime-checked dependency walk on every read).
|
|
105
|
-
if (IS_DEV) {
|
|
106
|
-
return handleDevRequest(route, ctx, root, searchRoutes);
|
|
107
|
-
}
|
|
197
|
+
plugin.get(pattern, (ctx) =>
|
|
198
|
+
renderResolvedRoute(route, ctx, root, resolvedBuildId, searchRoutes)
|
|
199
|
+
);
|
|
108
200
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
201
|
+
return plugin;
|
|
202
|
+
}
|
|
112
203
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
204
|
+
export function renderResolvedRoute(
|
|
205
|
+
route: ResolvedRoute,
|
|
206
|
+
ctx: Context,
|
|
207
|
+
root: RootLayout,
|
|
208
|
+
buildId: string,
|
|
209
|
+
searchRoutes: SearchRouteMetadata[] | undefined
|
|
210
|
+
): unknown {
|
|
211
|
+
// Dev mode: re-imports page + layouts on every request via the
|
|
212
|
+
// ?furin-server cache-buster. Only loader output is cached in dev; HTML is
|
|
213
|
+
// always reassembled with the latest Bun client chunk URL.
|
|
214
|
+
if (IS_DEV) {
|
|
215
|
+
return handleDevRequest(route, ctx, root, searchRoutes);
|
|
216
|
+
}
|
|
117
217
|
|
|
118
|
-
|
|
119
|
-
|
|
218
|
+
if ((route.mode === "ssg" || route.mode === "isr") && hasRequestLoader(route)) {
|
|
219
|
+
return renderPprRoute(route, ctx, root, buildId, searchRoutes);
|
|
220
|
+
}
|
|
120
221
|
|
|
121
|
-
|
|
222
|
+
if (route.mode === "ssg") {
|
|
223
|
+
return handleSSGRequest(route, ctx, root, buildId, searchRoutes);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (route.mode === "isr") {
|
|
227
|
+
ctx.set.headers["cache-tag"] = resolvePath(route.pattern, ctx.params ?? {});
|
|
228
|
+
return handleISR(route, ctx, root, buildId, searchRoutes);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return renderSSR(route, ctx, root, undefined, searchRoutes);
|
|
122
232
|
}
|
|
123
233
|
|
|
124
234
|
/**
|
|
125
235
|
* Elysia plugin that handles `GET /_furin/data?path=<logicalHref>`.
|
|
126
236
|
*
|
|
127
|
-
* Returns
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* serialising, so the client receives all data resolved in one round-trip.
|
|
132
|
-
* SPA navigation calls this endpoint via `parseDeferredNdjson` in
|
|
133
|
-
* `router-provider.tsx`.
|
|
237
|
+
* Returns route-frame loader data for successful routes. Deferred routes stream
|
|
238
|
+
* the initial sync frame first, then append one frame batch per settled Promise.
|
|
239
|
+
* SPA navigation calls this endpoint via `parseDeferredNdjson`, which supports
|
|
240
|
+
* both route frames and legacy NDJSON sentinels.
|
|
134
241
|
*
|
|
135
242
|
* Special fields emitted alongside data:
|
|
136
243
|
* - `__furinStatus: 404` — when the loader called `notFound()`
|
|
137
244
|
* - `__furinNotFound` — not-found payload
|
|
138
245
|
* - `__furinRedirect` — logical path after a server-side redirect
|
|
139
246
|
*/
|
|
140
|
-
export function createDataEndpoint(
|
|
247
|
+
export function createDataEndpoint(routesSource: ResolvedRoutesSource): AnyElysia {
|
|
141
248
|
const plugin = new Elysia();
|
|
142
|
-
|
|
249
|
+
let matchedRoutes = typeof routesSource === "function" ? routesSource() : routesSource;
|
|
250
|
+
let matchRoute = buildRouteMatcher(matchedRoutes);
|
|
143
251
|
|
|
144
252
|
plugin.get(
|
|
145
253
|
"/_furin/data",
|
|
@@ -163,8 +271,11 @@ export function createDataEndpoint(routes: ResolvedRoute[]): AnyElysia {
|
|
|
163
271
|
const wideEventLog = useLogger();
|
|
164
272
|
wideEventLog.set({ path: rawPath });
|
|
165
273
|
|
|
166
|
-
|
|
167
|
-
|
|
274
|
+
const currentRoutes = typeof routesSource === "function" ? routesSource() : routesSource;
|
|
275
|
+
if (currentRoutes !== matchedRoutes) {
|
|
276
|
+
matchedRoutes = currentRoutes;
|
|
277
|
+
matchRoute = buildRouteMatcher(matchedRoutes);
|
|
278
|
+
}
|
|
168
279
|
const matched = matchRoute(pathname);
|
|
169
280
|
|
|
170
281
|
if (!matched) {
|
|
@@ -180,21 +291,25 @@ export function createDataEndpoint(routes: ResolvedRoute[]): AnyElysia {
|
|
|
180
291
|
// Build the synthetic URL from the parsed `pathname + search` only —
|
|
181
292
|
// never from `rawPath` directly — so an attacker cannot smuggle a
|
|
182
293
|
// foreign origin into `syntheticRequest.url`.
|
|
183
|
-
|
|
294
|
+
// Forward the real request headers (cookies, auth) so loaders reading
|
|
295
|
+
// `request.headers` behave the same during SPA navigation as in SSR.
|
|
296
|
+
const syntheticRequest = new Request(new URL(pathname + url.search, ctx.request.url), {
|
|
297
|
+
headers: ctx.request.headers,
|
|
298
|
+
});
|
|
184
299
|
const syntheticSet = { headers: {} as Record<string, string>, status: 200 as number };
|
|
185
300
|
const syntheticCtx: SyntheticDataContext = {
|
|
186
|
-
request: syntheticRequest,
|
|
187
|
-
params: matched.params,
|
|
188
|
-
query: Object.fromEntries(url.searchParams),
|
|
189
|
-
set: syntheticSet,
|
|
190
|
-
headers: ctx.headers,
|
|
191
301
|
cookie: ctx.cookie,
|
|
302
|
+
headers: ctx.headers,
|
|
303
|
+
params: matched.params,
|
|
192
304
|
path: pathname,
|
|
305
|
+
query: Object.fromEntries(url.searchParams),
|
|
193
306
|
// Loader-emitted redirects flow through `runLoaders` → `result.type
|
|
194
307
|
// === "redirect"` and are converted to NDJSON below. The Response we
|
|
195
308
|
// return here only has to be detectable by that pipeline.
|
|
196
309
|
redirect: (location: string, status?: number) =>
|
|
197
|
-
new Response(null, {
|
|
310
|
+
new Response(null, { headers: { location }, status: status ?? 302 }),
|
|
311
|
+
request: syntheticRequest,
|
|
312
|
+
set: syntheticSet,
|
|
198
313
|
// Synthetic `status` helper: numeric codes only. Callers that reach
|
|
199
314
|
// this endpoint never dispatch a string-keyed status; rejecting them
|
|
200
315
|
// is safer than coercing via `Number(code)` and silently producing
|
|
@@ -202,10 +317,17 @@ export function createDataEndpoint(routes: ResolvedRoute[]): AnyElysia {
|
|
|
202
317
|
status: (code: number) => new Response(null, { status: code }),
|
|
203
318
|
} as unknown as SyntheticDataContext;
|
|
204
319
|
|
|
205
|
-
// Normalize params and query through the
|
|
206
|
-
//
|
|
320
|
+
// Normalize params and query through the route chain schemas so SPA and
|
|
321
|
+
// document requests expose identical typed/defaulted inputs.
|
|
207
322
|
const mergedParams = mergeRouteSchemas(matched.route.routeChain, "params");
|
|
208
323
|
const mergedQuery = mergeRouteSchemas(matched.route.routeChain, "query");
|
|
324
|
+
const parsedParams = await parseRouteParams(matched.params, mergedParams);
|
|
325
|
+
if (!parsedParams.ok) {
|
|
326
|
+
return Response.json(
|
|
327
|
+
{ errors: parsedParams.errors, message: "Invalid params", type: "validation" },
|
|
328
|
+
{ status: 422 }
|
|
329
|
+
);
|
|
330
|
+
}
|
|
209
331
|
const parsedQuery = await parseRouteQuery(url, mergedQuery);
|
|
210
332
|
if (!parsedQuery.ok) {
|
|
211
333
|
return Response.json(
|
|
@@ -213,100 +335,15 @@ export function createDataEndpoint(routes: ResolvedRoute[]): AnyElysia {
|
|
|
213
335
|
{ status: 422 }
|
|
214
336
|
);
|
|
215
337
|
}
|
|
216
|
-
syntheticCtx.params =
|
|
217
|
-
mergedParams as Record<string, unknown> | undefined,
|
|
218
|
-
syntheticCtx.params as Record<string, unknown>
|
|
219
|
-
) as Record<string, string>;
|
|
338
|
+
syntheticCtx.params = parsedParams.params;
|
|
220
339
|
syntheticCtx.query = parsedQuery.query as SearchParamsInput;
|
|
221
340
|
|
|
222
|
-
const result = await
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// afterHandle) can still find this URL by tag. Without this, a SPA-only
|
|
227
|
-
// navigation path (which never goes through the full-HTML render that
|
|
228
|
-
// also registers tags) would silently fall off the registry — the first
|
|
229
|
-
// mutation invalidates and unregisters via the cache `onDelete` hook,
|
|
230
|
-
// the next SPA fetch re-loads but does not re-register, and from then
|
|
231
|
-
// on `revalidateTag` finds no path to invalidate.
|
|
232
|
-
if (result.type === "data") {
|
|
233
|
-
autoInvalidateRegistry.registerLoaderTags(pathname, matched.route.tags);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
if (result.type === "redirect") {
|
|
237
|
-
// Encode the redirect target as a special field for the client to follow
|
|
238
|
-
const redirectUrl = new URL(
|
|
239
|
-
result.response.headers.get("location") ?? "/",
|
|
240
|
-
ctx.request.url
|
|
241
|
-
);
|
|
242
|
-
const serialized = await toCrossJSONAsync({
|
|
243
|
-
__furinRedirect: redirectUrl.pathname + redirectUrl.search,
|
|
244
|
-
});
|
|
245
|
-
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
246
|
-
headers: { "content-type": "application/x-ndjson" },
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (result.type === "not-found") {
|
|
251
|
-
const serialized = await toCrossJSONAsync({
|
|
252
|
-
__furinStatus: 404,
|
|
253
|
-
__furinNotFound: { message: result.error.message, data: result.error.data },
|
|
254
|
-
});
|
|
255
|
-
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
256
|
-
status: 200,
|
|
257
|
-
headers: { "content-type": "application/x-ndjson" },
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
if (result.type === "error") {
|
|
262
|
-
// Compute the digest server-side ONCE so:
|
|
263
|
-
// 1. The HTTP response body carries it (client error UI displays the
|
|
264
|
-
// same id the server logs).
|
|
265
|
-
// 2. The server-log line below uses the SAME digest a user would see
|
|
266
|
-
// on screen, enabling support correlation.
|
|
267
|
-
// Recomputing client-side from a synthetic message + stack would yield
|
|
268
|
-
// a different hash — see boundaries.tsx for the digest precedence.
|
|
269
|
-
const digest = computeErrorDigest(result.error);
|
|
270
|
-
const serialized = await toCrossJSONAsync({
|
|
271
|
-
__furinError: {
|
|
272
|
-
digest,
|
|
273
|
-
message: result.message,
|
|
274
|
-
status: result.status,
|
|
275
|
-
},
|
|
276
|
-
});
|
|
277
|
-
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
278
|
-
status: result.status,
|
|
279
|
-
headers: { "content-type": "application/x-ndjson" },
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Resolve the page title server-side: head() never runs in the browser,
|
|
284
|
-
// so SPA navigation depends on the endpoint shipping it as __furinTitle.
|
|
285
|
-
const syncDataWithTitle = withResolvedTitle(matched.route, result.syncData);
|
|
286
|
-
|
|
287
|
-
if (result.deferredPromises) {
|
|
288
|
-
return new Response(
|
|
289
|
-
createDeferredNdjsonStream(syncDataWithTitle, result.deferredPromises),
|
|
290
|
-
{
|
|
291
|
-
// Loader headers first so custom headers survive; the NDJSON
|
|
292
|
-
// content-type is framework-owned and must win — a loader cannot be
|
|
293
|
-
// allowed to mislabel the data-endpoint payload.
|
|
294
|
-
headers: {
|
|
295
|
-
...result.headers,
|
|
296
|
-
"content-type": "application/x-ndjson",
|
|
297
|
-
},
|
|
298
|
-
}
|
|
299
|
-
);
|
|
300
|
-
}
|
|
341
|
+
const result = await runDataEndpointLoaders(
|
|
342
|
+
matched.route,
|
|
343
|
+
syntheticCtx as unknown as Context
|
|
344
|
+
);
|
|
301
345
|
|
|
302
|
-
|
|
303
|
-
const serialized = await toCrossJSONAsync(syncDataWithTitle);
|
|
304
|
-
return new Response(`${JSON.stringify(serialized)}\n`, {
|
|
305
|
-
headers: {
|
|
306
|
-
...result.headers,
|
|
307
|
-
"content-type": "application/x-ndjson",
|
|
308
|
-
},
|
|
309
|
-
});
|
|
346
|
+
return createLoaderDataResponse(result, matched.route, syntheticRequest.url);
|
|
310
347
|
},
|
|
311
348
|
{
|
|
312
349
|
query: t.Object({ path: t.Optional(t.String()) }),
|
|
@@ -327,58 +364,31 @@ export function createDataEndpoint(routes: ResolvedRoute[]): AnyElysia {
|
|
|
327
364
|
* synchronous loader data. A throwing `head()` is swallowed: a missing title
|
|
328
365
|
* must never break the data response.
|
|
329
366
|
*/
|
|
330
|
-
function
|
|
367
|
+
function withResolvedHead(
|
|
331
368
|
route: ResolvedRoute,
|
|
332
369
|
syncData: Record<string, unknown>
|
|
333
370
|
): Record<string, unknown> {
|
|
334
|
-
const head = route.page
|
|
371
|
+
const { head } = route.page;
|
|
335
372
|
if (!head) {
|
|
336
373
|
return syncData;
|
|
337
374
|
}
|
|
338
|
-
let
|
|
375
|
+
let headOptions: HeadOptions;
|
|
339
376
|
try {
|
|
340
|
-
|
|
377
|
+
headOptions = head(syncData);
|
|
341
378
|
} catch {
|
|
342
379
|
return syncData;
|
|
343
380
|
}
|
|
344
|
-
|
|
345
|
-
|
|
381
|
+
const title = extractTitle(headOptions.meta);
|
|
382
|
+
let includeHead = false;
|
|
383
|
+
try {
|
|
384
|
+
includeHead =
|
|
385
|
+
new TextEncoder().encode(JSON.stringify(headOptions)).byteLength <= MAX_NAVIGATION_HEAD_BYTES;
|
|
386
|
+
} catch {
|
|
387
|
+
// Invalid or cyclic metadata must not break an otherwise valid navigation.
|
|
346
388
|
}
|
|
347
|
-
return {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
deferredPromises: Record<string, Promise<unknown>>
|
|
353
|
-
): ReadableStream<Uint8Array> {
|
|
354
|
-
const enc = new TextEncoder();
|
|
355
|
-
const entries = Object.entries(deferredPromises);
|
|
356
|
-
|
|
357
|
-
return new ReadableStream<Uint8Array>({
|
|
358
|
-
async start(controller) {
|
|
359
|
-
const initial = toCrossJSON({
|
|
360
|
-
...syncData,
|
|
361
|
-
__furinDeferredKeys: entries.map(([key]) => key),
|
|
362
|
-
});
|
|
363
|
-
controller.enqueue(enc.encode(`${JSON.stringify(initial)}\n`));
|
|
364
|
-
|
|
365
|
-
await Promise.all(
|
|
366
|
-
entries.map(async ([key, promise]) => {
|
|
367
|
-
try {
|
|
368
|
-
const value = await promise;
|
|
369
|
-
controller.enqueue(
|
|
370
|
-
enc.encode(
|
|
371
|
-
`${JSON.stringify({ key, action: "resolve", value: toCrossJSON(value) })}\n`
|
|
372
|
-
)
|
|
373
|
-
);
|
|
374
|
-
} catch (err) {
|
|
375
|
-
const normalized = await serializeDeferredRejection(err);
|
|
376
|
-
const value = toCrossJSON(normalized);
|
|
377
|
-
controller.enqueue(enc.encode(`${JSON.stringify({ key, action: "reject", value })}\n`));
|
|
378
|
-
}
|
|
379
|
-
})
|
|
380
|
-
);
|
|
381
|
-
controller.close();
|
|
382
|
-
},
|
|
383
|
-
});
|
|
389
|
+
return {
|
|
390
|
+
...syncData,
|
|
391
|
+
...(includeHead ? { __furinHead: headOptions } : {}),
|
|
392
|
+
...(title === undefined ? {} : { __furinTitle: title }),
|
|
393
|
+
};
|
|
384
394
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { t } from "elysia";
|
|
2
|
+
import type { RuntimeRoute } from "../../client/internal/runtime-types.ts";
|
|
3
|
+
import { type FurinSchema, isTypeBoxObjectSchema } from "../../shared/elysia-contract.ts";
|
|
4
|
+
|
|
5
|
+
interface SchemaObject {
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const TOBJECT_STRUCTURAL_KEYS = new Set(["type", "properties", "required"]);
|
|
10
|
+
export function mergeRouteSchemas(
|
|
11
|
+
routeChain: RuntimeRoute[],
|
|
12
|
+
key: "params" | "query"
|
|
13
|
+
): FurinSchema | undefined {
|
|
14
|
+
const schemas = routeChain.flatMap((route) => (route[key] ? [route[key] as SchemaObject] : []));
|
|
15
|
+
|
|
16
|
+
if (schemas.length === 0) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (schemas.length === 1) {
|
|
20
|
+
return schemas[0] as FurinSchema;
|
|
21
|
+
}
|
|
22
|
+
if (schemas.some((schema) => !isTypeBoxObjectSchema(schema))) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`[furin] Merging ${key} schemas across the route chain requires TypeBox in V1. Use TypeBox for parent/child ${key}, or define ${key} only on leaf routes.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const properties = Object.assign(
|
|
29
|
+
{},
|
|
30
|
+
...schemas.map((schema) => schema.properties as SchemaObject)
|
|
31
|
+
);
|
|
32
|
+
const options = Object.assign(
|
|
33
|
+
{},
|
|
34
|
+
...schemas.map((schema) =>
|
|
35
|
+
Object.fromEntries(
|
|
36
|
+
Object.entries(schema).filter(([name]) => !TOBJECT_STRUCTURAL_KEYS.has(name))
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return t.Object(properties, options) as FurinSchema;
|
|
42
|
+
}
|