@teyik0/furin 0.6.0-alpha.1 → 0.7.0-alpha.2
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/adapter/runtime-build.d.ts +1 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/elysia-aot-worker.d.ts +22 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/entry-template.d.ts +3 -0
- package/dist/build/entry-template.ts +7 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +1880 -401
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +1878 -408
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -0
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +942 -122
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +51 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1219 -59
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +78 -32
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +1 -2
- package/dist/furin.js +2484 -708
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +349 -18
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +1 -0
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +5 -19
- package/dist/server/internal.d.ts +3 -0
- package/dist/server/internal.js +55 -35
- package/dist/server/production-server.js +54 -34
- package/dist/server/render/loaders.d.ts +4 -2
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +2 -1
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/patterns.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +533 -105
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +34 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +29 -6
- package/src/adapter/package.ts +12 -2
- package/src/adapter/runtime-build.ts +63 -12
- package/src/adapter/static.ts +7 -2
- package/src/adapter/vercel.ts +15 -13
- package/src/build/client.ts +23 -5
- package/src/build/elysia-aot-worker.ts +51 -0
- package/src/build/elysia-aot.ts +83 -19
- package/src/build/entry-template.ts +7 -0
- package/src/build/hydrate.ts +21 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/browser-events-runtime.ts +4 -0
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -1
- package/src/client/document.tsx +28 -18
- package/src/client/hmr.ts +2 -1
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +19 -12
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +306 -46
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +73 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +113 -55
- package/src/env.d.ts +2 -2
- package/src/furin.ts +75 -13
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/routes.ts +28 -16
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/server/auto-invalidate/registry.ts +4 -0
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/dev/graph.ts +16 -2
- package/src/server/dev-page-plugin.ts +93 -59
- package/src/server/internal.ts +3 -0
- package/src/server/render/document.tsx +10 -1
- package/src/server/render/element.tsx +21 -11
- package/src/server/render/isr.ts +61 -29
- package/src/server/render/loaders.ts +118 -47
- package/src/server/render/mixed-cache.ts +33 -4
- package/src/server/render/not-found.ts +4 -7
- package/src/server/render/ppr-document.ts +2 -1
- package/src/server/render/ppr-route.ts +30 -10
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +51 -18
- package/src/server/render/ssr.ts +53 -8
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/discovery.ts +25 -9
- package/src/server/router/hmr.ts +3 -2
- package/src/server/router/patterns.ts +16 -0
- package/src/server/router/plugin.ts +27 -12
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +52 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
package/src/furin.ts
CHANGED
|
@@ -37,6 +37,7 @@ import { initializeFurinLogger } from "./server/logger.ts";
|
|
|
37
37
|
import { renderRootNotFound } from "./server/render/not-found.ts";
|
|
38
38
|
import { warmSSGCache } from "./server/render/ssg.ts";
|
|
39
39
|
import {
|
|
40
|
+
setProductionPreloadManifest,
|
|
40
41
|
setProductionTemplateContent,
|
|
41
42
|
setProductionTemplatePath,
|
|
42
43
|
} from "./server/render/template.ts";
|
|
@@ -58,6 +59,7 @@ import {
|
|
|
58
59
|
import type { ResolvedRoute, RootLayout } from "./server/router/types.ts";
|
|
59
60
|
import { IS_DEV } from "./server/runtime-env.ts";
|
|
60
61
|
import { type FurinSyncOption, resolveSyncPath } from "./server/sync/config.ts";
|
|
62
|
+
import { bindSyncValidation } from "./server/sync/validation.ts";
|
|
61
63
|
import { physicalPath } from "./shared/prefix.ts";
|
|
62
64
|
|
|
63
65
|
// biome-ignore lint/suspicious/noEmptyInterface: intentionally augmentable via furin-env.d.ts
|
|
@@ -260,6 +262,9 @@ async function setupCompiledTemplate(
|
|
|
260
262
|
clientDir: string,
|
|
261
263
|
instance: FurinInstance
|
|
262
264
|
): Promise<void> {
|
|
265
|
+
if (ctx.preloadManifest) {
|
|
266
|
+
setProductionPreloadManifest(ctx.preloadManifest, instance);
|
|
267
|
+
}
|
|
263
268
|
if (ctx.templateHtml === undefined) {
|
|
264
269
|
await setupProdTemplate(embedded, clientDir, instance);
|
|
265
270
|
return;
|
|
@@ -532,9 +537,12 @@ function wrapWithRequestScope(app: AnyElysia): Elysia {
|
|
|
532
537
|
});
|
|
533
538
|
}
|
|
534
539
|
|
|
535
|
-
function createFurinPlugin(app: AnyElysia, hmrPrefix: string | undefined) {
|
|
540
|
+
function createFurinPlugin(app: AnyElysia, hmrPrefix: string | undefined, hasSync: boolean) {
|
|
536
541
|
const scopedApp = wrapWithRequestScope(app);
|
|
537
542
|
return <ParentApp extends AnyElysia>(parentApp: ParentApp) => {
|
|
543
|
+
if (hasSync) {
|
|
544
|
+
bindSyncValidation(parentApp);
|
|
545
|
+
}
|
|
538
546
|
const mounted = parentApp.use(scopedApp);
|
|
539
547
|
if (hmrPrefix !== undefined) {
|
|
540
548
|
const parentConfig = Reflect.get(parentApp, "~config") as { prefix?: string } | undefined;
|
|
@@ -576,7 +584,7 @@ function createNativeRouteRenderer(
|
|
|
576
584
|
const hasPrefix = prefix !== "" && (pathname === prefix || pathname.startsWith(`${prefix}/`));
|
|
577
585
|
const logicalPath = hasPrefix ? pathname.slice(prefix.length) : pathname;
|
|
578
586
|
const matched = matchNativeRoute(logicalPath || "/");
|
|
579
|
-
if (!matched) {
|
|
587
|
+
if (!matched || (IS_DEV && !existsSync(matched.route.path))) {
|
|
580
588
|
// Dev topology swap: the mounted Elysia route can outlive its source
|
|
581
589
|
// file (hot-remove). Render the root not-found page instead of failing.
|
|
582
590
|
const listenerOrigin = (context.server as { url?: { origin: string } } | undefined)?.url
|
|
@@ -841,7 +849,51 @@ export async function furin({
|
|
|
841
849
|
navigationDataMatchers.set(instance, (path) => matchNavigationData(path) !== null);
|
|
842
850
|
|
|
843
851
|
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
844
|
-
const
|
|
852
|
+
const { getHmrDataSignature } = await import("./plugin/transform-client.ts");
|
|
853
|
+
let serverSourceVersion = 0;
|
|
854
|
+
let serverDataSignatures = new Map<string, string>();
|
|
855
|
+
let serverSourcePaths: string | undefined;
|
|
856
|
+
const writeCurrentDevFiles = (
|
|
857
|
+
snapshot: DevelopmentRouteSnapshot,
|
|
858
|
+
changedSources: readonly string[]
|
|
859
|
+
): void => {
|
|
860
|
+
const paths = [
|
|
861
|
+
...new Set([
|
|
862
|
+
snapshot.root.path,
|
|
863
|
+
...snapshot.routes.flatMap((route) => [
|
|
864
|
+
route.path,
|
|
865
|
+
...route.routeChain.flatMap((entry) => (entry.sourcePath ? [entry.sourcePath] : [])),
|
|
866
|
+
]),
|
|
867
|
+
]),
|
|
868
|
+
].toSorted();
|
|
869
|
+
const nextPaths = JSON.stringify(paths);
|
|
870
|
+
const nextSignatures = new Map(
|
|
871
|
+
paths.map((path) => {
|
|
872
|
+
let signature: string;
|
|
873
|
+
try {
|
|
874
|
+
signature = getHmrDataSignature(readFileSync(path, "utf8"), path);
|
|
875
|
+
if (signature.startsWith("external:")) {
|
|
876
|
+
signature = graph.sourceVersion(path);
|
|
877
|
+
}
|
|
878
|
+
} catch {
|
|
879
|
+
// A broken route still needs a client entry for its diagnostic.
|
|
880
|
+
signature = graph.sourceVersion(path);
|
|
881
|
+
}
|
|
882
|
+
return [path, signature] as const;
|
|
883
|
+
})
|
|
884
|
+
);
|
|
885
|
+
// Route additions/removals already update the native client manifest.
|
|
886
|
+
// Rebase their signature without issuing a second data invalidation.
|
|
887
|
+
const changedPaths = new Set(changedSources);
|
|
888
|
+
const importsChanged = paths.some(
|
|
889
|
+
(path) =>
|
|
890
|
+
!changedPaths.has(path) && serverDataSignatures.get(path) !== nextSignatures.get(path)
|
|
891
|
+
);
|
|
892
|
+
if (importsChanged && serverSourcePaths === nextPaths) {
|
|
893
|
+
serverSourceVersion += 1;
|
|
894
|
+
}
|
|
895
|
+
serverSourcePaths = nextPaths;
|
|
896
|
+
serverDataSignatures = nextSignatures;
|
|
845
897
|
writeDevFiles(
|
|
846
898
|
snapshot.routes,
|
|
847
899
|
{
|
|
@@ -854,10 +906,11 @@ export async function furin({
|
|
|
854
906
|
// instance owns it, otherwise mounted apps clobber each other's types.
|
|
855
907
|
skipRouteTypes: prefix !== "",
|
|
856
908
|
},
|
|
857
|
-
cwd
|
|
909
|
+
cwd,
|
|
910
|
+
String(serverSourceVersion)
|
|
858
911
|
);
|
|
859
912
|
};
|
|
860
|
-
writeCurrentDevFiles(initialSnapshot);
|
|
913
|
+
writeCurrentDevFiles(initialSnapshot, []);
|
|
861
914
|
graph.commit(initialSnapshot);
|
|
862
915
|
const hmrEntry = (await import(join(furinDir, "index.html"))).default;
|
|
863
916
|
const refreshDevelopmentRoutes = (changedSources: readonly string[]): Promise<void> =>
|
|
@@ -867,8 +920,18 @@ export async function furin({
|
|
|
867
920
|
const previousSnapshot = currentSnapshot();
|
|
868
921
|
const repaired = repairedDevelopmentRoutes(previousSnapshot, changedSources, graph);
|
|
869
922
|
const next = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
870
|
-
|
|
871
|
-
|
|
923
|
+
// Retain tags only for routes whose current module could not be loaded.
|
|
924
|
+
const nextRoutes = next.routes.map((route) =>
|
|
925
|
+
route.routeChain.length > 0
|
|
926
|
+
? route
|
|
927
|
+
: {
|
|
928
|
+
...route,
|
|
929
|
+
tags: previousSnapshot.routes.find((previous) => previous.path === route.path)
|
|
930
|
+
?.tags,
|
|
931
|
+
}
|
|
932
|
+
);
|
|
933
|
+
const nextSnapshot = createDevelopmentRouteSnapshot(prefix, next.root, nextRoutes);
|
|
934
|
+
writeCurrentDevFiles(nextSnapshot, changedSources);
|
|
872
935
|
graph.commit(nextSnapshot);
|
|
873
936
|
matchNavigationData = buildRouteMatcher(nextSnapshot.routes);
|
|
874
937
|
const diagnostics = devDiagnosticStore(instance);
|
|
@@ -998,7 +1061,7 @@ export async function furin({
|
|
|
998
1061
|
})
|
|
999
1062
|
);
|
|
1000
1063
|
registerInstance(instance);
|
|
1001
|
-
return createFurinPlugin(devApp, prefix);
|
|
1064
|
+
return createFurinPlugin(devApp, prefix, Boolean(sync));
|
|
1002
1065
|
}
|
|
1003
1066
|
|
|
1004
1067
|
// ── Production ──────────────────────────────────────────────────────────
|
|
@@ -1073,7 +1136,7 @@ export async function furin({
|
|
|
1073
1136
|
.use(ctx.nativeRoutes)
|
|
1074
1137
|
.use(createNotFoundHandling(prefix, routes, root));
|
|
1075
1138
|
registerInstance(instance);
|
|
1076
|
-
return createFurinPlugin(prodApp, undefined);
|
|
1139
|
+
return createFurinPlugin(prodApp, undefined, Boolean(sync));
|
|
1077
1140
|
}
|
|
1078
1141
|
|
|
1079
1142
|
/**
|
|
@@ -1136,10 +1199,6 @@ function createNotFoundHandling(
|
|
|
1136
1199
|
|
|
1137
1200
|
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
1138
1201
|
export { HeadContent, Scripts } from "./client/document.tsx";
|
|
1139
|
-
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
1140
|
-
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
1141
|
-
export type { DeferredData } from "./client.ts";
|
|
1142
|
-
export { defer, isDeferred } from "./client.ts";
|
|
1143
1202
|
export type { InvalidationInput, InvalidationRule } from "./server/auto-invalidate/index.ts";
|
|
1144
1203
|
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
1145
1204
|
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
@@ -1155,6 +1214,9 @@ export {
|
|
|
1155
1214
|
type SyncRuntimeOptions,
|
|
1156
1215
|
} from "./server/sync/index.ts";
|
|
1157
1216
|
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
1217
|
+
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
1218
|
+
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
1219
|
+
export { type DeferredData, defer, isDeferred } from "./shared/defer.ts";
|
|
1158
1220
|
export type { ErrorComponent, ErrorProps } from "./shared/error.ts";
|
|
1159
1221
|
export type {
|
|
1160
1222
|
NotFoundComponent,
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { detectLoaderFromPath } from "../server/lang-detect.ts";
|
|
4
|
+
|
|
5
|
+
const SCRIPT_FILE = /\.[cm]?[jt]sx?$/;
|
|
6
|
+
|
|
7
|
+
/** Fingerprint server imports without importing their code into the client bundle. */
|
|
8
|
+
export function hmrDependencySignature(imports: Set<string>, filename: string): string | undefined {
|
|
9
|
+
const routePath = resolve(filename);
|
|
10
|
+
const visited = new Set<string>();
|
|
11
|
+
const hash = new Bun.CryptoHasher("sha256");
|
|
12
|
+
const visit = (specifier: string, importer: string): boolean => {
|
|
13
|
+
// Framework/package internals are not application source dependencies.
|
|
14
|
+
if (
|
|
15
|
+
specifier === "furin" ||
|
|
16
|
+
specifier.startsWith("furin/") ||
|
|
17
|
+
specifier === "@teyik0/furin" ||
|
|
18
|
+
specifier.startsWith("@teyik0/furin/")
|
|
19
|
+
) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
const path = Bun.resolveSync(specifier, dirname(importer));
|
|
23
|
+
// A helper can read route exports that are absent from the loader's AST.
|
|
24
|
+
if (path === routePath) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
hash.update(JSON.stringify([specifier, path]));
|
|
28
|
+
if (
|
|
29
|
+
!isAbsolute(path) ||
|
|
30
|
+
path.replaceAll("\\", "/").includes("/node_modules/") ||
|
|
31
|
+
visited.has(path)
|
|
32
|
+
) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
visited.add(path);
|
|
36
|
+
const source = readFileSync(path, "utf8");
|
|
37
|
+
hash.update(source);
|
|
38
|
+
if (SCRIPT_FILE.test(path)) {
|
|
39
|
+
const transpiler = new Bun.Transpiler({ loader: detectLoaderFromPath(path) });
|
|
40
|
+
for (const dependency of transpiler.scanImports(source)) {
|
|
41
|
+
if (!visit(dependency.path, path)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
for (const specifier of imports) {
|
|
50
|
+
if (!visit(specifier, routePath)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return hash.digest("hex");
|
|
55
|
+
} catch {
|
|
56
|
+
// An unresolved/invalid dependency must keep the conservative refresh path.
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/plugin/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
2
|
+
import { environmentGuardPlugin } from "../rsc/build/environment.ts";
|
|
1
3
|
import { detectLoaderFromPath } from "../server/lang-detect.ts";
|
|
2
4
|
import { transformForClient } from "./transform-client.ts";
|
|
3
5
|
|
|
@@ -36,6 +38,12 @@ export default {};
|
|
|
36
38
|
const plugin: Bun.BunPlugin = {
|
|
37
39
|
name: "furin-strip-server",
|
|
38
40
|
setup(build) {
|
|
41
|
+
environmentGuardPlugin("client").setup(build);
|
|
42
|
+
const topologyPaths = new Set<string>();
|
|
43
|
+
const loadedSources = new Map<
|
|
44
|
+
string,
|
|
45
|
+
{ contents: string; loader: Bun.Loader; isRouteModule: boolean }
|
|
46
|
+
>();
|
|
39
47
|
// ── browser stubs ───────────────────────────────────────────────────────
|
|
40
48
|
build.onResolve({ filter: ELYSIA_FILTER }, () => ({
|
|
41
49
|
namespace: "furin-stubs",
|
|
@@ -58,16 +66,44 @@ const plugin: Bun.BunPlugin = {
|
|
|
58
66
|
return;
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
let source: string;
|
|
70
|
+
try {
|
|
71
|
+
source = await Bun.file(args.path).text();
|
|
72
|
+
} catch (error) {
|
|
73
|
+
const loaded = loadedSources.get(args.path);
|
|
74
|
+
// Bun can revisit its previous client graph before topology changes remove
|
|
75
|
+
// a deleted route from the hydration entry. Keep that graph loadable.
|
|
76
|
+
if (
|
|
77
|
+
loaded?.isRouteModule &&
|
|
78
|
+
error instanceof Error &&
|
|
79
|
+
"code" in error &&
|
|
80
|
+
error.code === "ENOENT"
|
|
81
|
+
) {
|
|
82
|
+
return loaded;
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
62
86
|
|
|
87
|
+
const normalizedPath = args.path.replaceAll("\\", "/");
|
|
88
|
+
if (normalizedPath.includes("/.furin/") && normalizedPath.endsWith("/_hydrate.tsx")) {
|
|
89
|
+
const transpiler = new Bun.Transpiler({ loader: "tsx" });
|
|
90
|
+
for (const imported of transpiler.scanImports(source)) {
|
|
91
|
+
if (isAbsolute(imported.path) || imported.path.startsWith(".")) {
|
|
92
|
+
topologyPaths.add(resolve(dirname(args.path), imported.path));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
63
96
|
const result = transformForClient(source, args.path);
|
|
64
97
|
// Output is TS/TSX (yuku parses directly, no pre-transpile). Bun's
|
|
65
98
|
// bundler picks the loader from the file extension and applies the
|
|
66
99
|
// project tsconfig — including the JSX automatic runtime.
|
|
67
|
-
|
|
100
|
+
const loaded = {
|
|
68
101
|
contents: result.code,
|
|
102
|
+
isRouteModule: topologyPaths.has(args.path),
|
|
69
103
|
loader: detectLoaderFromPath(args.path),
|
|
70
104
|
};
|
|
105
|
+
loadedSources.set(args.path, loaded);
|
|
106
|
+
return loaded;
|
|
71
107
|
});
|
|
72
108
|
},
|
|
73
109
|
};
|
package/src/plugin/routes.ts
CHANGED
|
@@ -12,7 +12,13 @@ import { pathToFileURL } from "node:url";
|
|
|
12
12
|
import { type AnyElysia, Elysia } from "elysia";
|
|
13
13
|
import { type FurinNativeRouteContext, getFurinRenderer } from "../define-route.ts";
|
|
14
14
|
import { detectLoaderFromPath } from "../server/lang-detect.ts";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
filePathToPattern,
|
|
17
|
+
isRouteGroup,
|
|
18
|
+
parseDynamicRouteSegment,
|
|
19
|
+
routePatternKey,
|
|
20
|
+
routeSegmentToPattern,
|
|
21
|
+
} from "../server/router/patterns.ts";
|
|
16
22
|
import { routeModuleSourceVersion } from "../server/router/source-version.ts";
|
|
17
23
|
|
|
18
24
|
const ROUTES_NAMESPACE_PREFIX = "furin-routes";
|
|
@@ -118,7 +124,10 @@ function scanRouteFiles(
|
|
|
118
124
|
directorySegments: string[],
|
|
119
125
|
files: ScannedRouteFile[]
|
|
120
126
|
): void {
|
|
121
|
-
|
|
127
|
+
const entries = readdirSync(directory, { withFileTypes: true }).sort((left, right) =>
|
|
128
|
+
left.name.localeCompare(right.name)
|
|
129
|
+
);
|
|
130
|
+
for (const entry of entries) {
|
|
122
131
|
if (entry.isDirectory()) {
|
|
123
132
|
// Underscore-prefixed directories are co-located private folders
|
|
124
133
|
// (components, libs) — never route segments.
|
|
@@ -157,10 +166,6 @@ function segmentPath(segment: string): string {
|
|
|
157
166
|
return routeSegmentToPattern(segment);
|
|
158
167
|
}
|
|
159
168
|
|
|
160
|
-
function routePath(segments: string[]): string {
|
|
161
|
-
return segments.length === 0 ? "/" : `/${segments.map(segmentPath).join("/")}`;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
169
|
function routeId(instanceId: string, path: string): string {
|
|
165
170
|
const encodedPath = Array.from(new TextEncoder().encode(path), (byte) =>
|
|
166
171
|
byte.toString(16).padStart(2, "0")
|
|
@@ -193,18 +198,25 @@ function buildRouteTree(pagesDir: string, instanceId: string): RouteTreeNode {
|
|
|
193
198
|
return node;
|
|
194
199
|
};
|
|
195
200
|
const root = ensureNode([]);
|
|
201
|
+
const seenPatterns = new Map<string, string>();
|
|
196
202
|
|
|
197
203
|
for (const file of scannedFiles) {
|
|
198
204
|
const node = ensureNode(file.directorySegments);
|
|
199
205
|
if (file.base === "_route") {
|
|
200
|
-
const path =
|
|
206
|
+
const path = `/${file.directorySegments.map(segmentPath).join("/")}#layout`;
|
|
201
207
|
node.layout = { id: routeId(instanceId, path), path: "", sourcePath: file.sourcePath };
|
|
202
208
|
continue;
|
|
203
209
|
}
|
|
204
|
-
const path =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
210
|
+
const path = filePathToPattern(relative(pagesDir, file.sourcePath));
|
|
211
|
+
const patternKey = routePatternKey(path);
|
|
212
|
+
const relativePath = relative(pagesDir, file.sourcePath).replaceAll("\\", "/");
|
|
213
|
+
const previousPath = seenPatterns.get(patternKey);
|
|
214
|
+
if (previousPath !== undefined) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`[furin] Duplicate route pattern "${patternKey}" from "${previousPath}" and "${relativePath}".`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
seenPatterns.set(patternKey, relativePath);
|
|
208
220
|
const route = { id: routeId(instanceId, path), path, sourcePath: file.sourcePath };
|
|
209
221
|
if (file.base === "index") {
|
|
210
222
|
node.indexRoute = route;
|
|
@@ -547,7 +559,7 @@ function emitRouteNode(
|
|
|
547
559
|
includeRootCatchAll: boolean
|
|
548
560
|
): string {
|
|
549
561
|
const childIndentation = `${indentation} `;
|
|
550
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
562
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
551
563
|
const head = `new Elysia({ prefix: ${JSON.stringify(prefix)} })`;
|
|
552
564
|
const content: string[] = [];
|
|
553
565
|
if (node.indexRoute) {
|
|
@@ -770,6 +782,8 @@ function resolveRouteModuleImports(dependencyPath: string, packageRoot: string):
|
|
|
770
782
|
if (
|
|
771
783
|
isAbsolute(resolvedImport) &&
|
|
772
784
|
isWithinDirectory(resolvedImport, packageRoot) &&
|
|
785
|
+
// Each import has its own path segments; constructing a Set adds no reusable lookup.
|
|
786
|
+
// react-doctor-disable-next-line react-doctor/js-set-map-lookups
|
|
773
787
|
!relative(packageRoot, resolvedImport).split(sep).includes("node_modules")
|
|
774
788
|
) {
|
|
775
789
|
resolvedImports.push(resolvedImport);
|
|
@@ -865,7 +879,6 @@ async function loadRouteModule(sourcePath: string): Promise<{
|
|
|
865
879
|
loader?: unknown;
|
|
866
880
|
mode?: string;
|
|
867
881
|
schemas?: { params?: { properties?: object } };
|
|
868
|
-
useLoaderData?: () => unknown;
|
|
869
882
|
};
|
|
870
883
|
}> {
|
|
871
884
|
return (await importRouteModule(sourcePath)) as {
|
|
@@ -874,7 +887,6 @@ async function loadRouteModule(sourcePath: string): Promise<{
|
|
|
874
887
|
loader?: unknown;
|
|
875
888
|
mode?: string;
|
|
876
889
|
schemas?: { params?: { properties?: object } };
|
|
877
|
-
useLoaderData?: () => unknown;
|
|
878
890
|
};
|
|
879
891
|
};
|
|
880
892
|
}
|
|
@@ -905,7 +917,7 @@ async function composableRouteApps(route: RouteFile): Promise<DevRoutesApps | un
|
|
|
905
917
|
}
|
|
906
918
|
|
|
907
919
|
async function composeRuntimeNode(node: RouteTreeNode): Promise<DevRoutesApps> {
|
|
908
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
920
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
909
921
|
const { fileRoutes } = node;
|
|
910
922
|
const [layoutApps, indexRouteApps, fileRouteApps, childApps] = await Promise.all([
|
|
911
923
|
node.layout ? composableRouteApps(node.layout) : undefined,
|
|
@@ -1001,7 +1013,7 @@ async function clientRoutesSource(instance: RouteInstanceSpec): Promise<string>
|
|
|
1001
1013
|
const module = await loadRouteModule(routeFile.sourcePath);
|
|
1002
1014
|
validateRouteParams(routeFile.path, module.route?.schemas);
|
|
1003
1015
|
return {
|
|
1004
|
-
hasLoader: module.route?.loader !== undefined
|
|
1016
|
+
hasLoader: module.route?.loader !== undefined,
|
|
1005
1017
|
mode: module.route?.mode ?? "ssr",
|
|
1006
1018
|
pattern: routeFile.path,
|
|
1007
1019
|
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { walk } from "yuku-ast";
|
|
5
|
+
import type { CallExpression, ImportDeclaration, Program } from "yuku-parser";
|
|
6
|
+
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
7
|
+
import { parseSource } from "../shared/parser.ts";
|
|
8
|
+
import type { AstNode } from "../shared/utils/ast-walk.ts";
|
|
9
|
+
import { hasShadowingDeclaration } from "./binding-scope.ts";
|
|
10
|
+
import type { IsomorphicEnvironment } from "./transform-isomorphic.ts";
|
|
11
|
+
|
|
12
|
+
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
13
|
+
const SERVER_LOAD =
|
|
14
|
+
'() => Promise.reject(new Error("[furin] clientModule() can only be loaded in the browser."))';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Stable key shared by the `clientModule()` transform and the client build's
|
|
18
|
+
* preload manifest. It is an opaque ASCII token so it survives minification
|
|
19
|
+
* unchanged and can be swapped for the module's chunk URLs after the build.
|
|
20
|
+
*/
|
|
21
|
+
export function clientModuleKey(modulePath: string): string {
|
|
22
|
+
const realPath = realpathSync(modulePath).replaceAll("\\", "/");
|
|
23
|
+
return `__FURIN_CLIENT_MODULE_${Bun.hash(realPath).toString(36)}__`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function collectClientModuleBindings(program: Program): Set<string> {
|
|
27
|
+
const bindings = new Set<string>();
|
|
28
|
+
for (const statement of program.body) {
|
|
29
|
+
if (statement.type !== "ImportDeclaration") {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const declaration = statement as unknown as ImportDeclaration;
|
|
33
|
+
if (
|
|
34
|
+
declaration.importKind === "type" ||
|
|
35
|
+
!FURIN_CLIENT_MODULES.has(String(declaration.source.value))
|
|
36
|
+
) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
40
|
+
const imported = specifier.imported as AstNode | undefined;
|
|
41
|
+
const local = specifier.local as AstNode | undefined;
|
|
42
|
+
if (
|
|
43
|
+
specifier.type === "ImportSpecifier" &&
|
|
44
|
+
specifier.importKind !== "type" &&
|
|
45
|
+
imported?.name === "clientModule" &&
|
|
46
|
+
typeof local?.name === "string"
|
|
47
|
+
) {
|
|
48
|
+
bindings.add(local.name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return bindings;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Returns the `import("…")` specifier of a `() => import("…")` loader. */
|
|
56
|
+
function dynamicImportSpecifier(loader: AstNode): string | null {
|
|
57
|
+
if (loader.type !== "ArrowFunctionExpression" || (loader.params as unknown[]).length > 0) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const body = loader.body as unknown as AstNode;
|
|
61
|
+
const source = body.type === "ImportExpression" ? (body.source as AstNode) : null;
|
|
62
|
+
return source?.type === "Literal" && typeof source.value === "string" ? source.value : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Rewrites `clientModule(() => import("./x"))` calls imported from
|
|
67
|
+
* `@teyik0/furin/client`:
|
|
68
|
+
* - client: keeps the loader and appends the module key, which the production
|
|
69
|
+
* client build replaces with the module's chunk URLs;
|
|
70
|
+
* - server: drops the `import()` so the module is never bundled server-side and
|
|
71
|
+
* passes the key, resolved against the preload manifest at render time.
|
|
72
|
+
*/
|
|
73
|
+
export function transformClientModules(
|
|
74
|
+
source: string,
|
|
75
|
+
filename: string,
|
|
76
|
+
environment: IsomorphicEnvironment
|
|
77
|
+
): string {
|
|
78
|
+
if (!source.includes("clientModule")) {
|
|
79
|
+
return source;
|
|
80
|
+
}
|
|
81
|
+
const { program } = parseSource(source, detectLangFromPath(filename));
|
|
82
|
+
const bindings = collectClientModuleBindings(program);
|
|
83
|
+
if (bindings.size === 0) {
|
|
84
|
+
return source;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const transformed = new MagicString(source);
|
|
88
|
+
walk(program, {
|
|
89
|
+
CallExpression(node, context) {
|
|
90
|
+
const call = node as unknown as CallExpression;
|
|
91
|
+
const [loader, ...rest] = call.arguments as unknown as AstNode[];
|
|
92
|
+
if (
|
|
93
|
+
call.callee.type !== "Identifier" ||
|
|
94
|
+
!bindings.has(call.callee.name) ||
|
|
95
|
+
hasShadowingDeclaration(call.callee.name, context.ancestors() as AstNode[]) ||
|
|
96
|
+
!loader ||
|
|
97
|
+
rest.length > 0
|
|
98
|
+
) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const unwrapped = unwrapTSExpression(loader) as AstNode;
|
|
102
|
+
const specifier = dynamicImportSpecifier(unwrapped);
|
|
103
|
+
if (specifier === null) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
let key: string;
|
|
107
|
+
try {
|
|
108
|
+
key = JSON.stringify(clientModuleKey(Bun.resolveSync(specifier, dirname(filename))));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`[furin] Cannot resolve clientModule import ${JSON.stringify(specifier)} in ${filename}`,
|
|
112
|
+
{ cause: error }
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (environment === "client") {
|
|
116
|
+
transformed.appendLeft(loader.end, `, ${key}`);
|
|
117
|
+
} else {
|
|
118
|
+
transformed.overwrite(loader.start, loader.end, `${SERVER_LOAD}, ${key}`);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
return transformed.toString();
|
|
123
|
+
}
|