@teyik0/furin 0.2.0-alpha.4 → 0.3.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 +17 -1
- package/dist/adapter/package.d.ts +26 -0
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +3 -3
- package/dist/build/compile-entry.ts +88 -83
- package/dist/build/entry-template.d.ts +37 -9
- package/dist/build/entry-template.ts +109 -56
- package/dist/build/hydrate.d.ts +2 -2
- package/dist/build/index.js +6817 -4544
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/scan-server.d.ts +10 -4
- package/dist/build/server-routes-entry.d.ts +3 -3
- package/dist/build/server-routes-entry.ts +5 -11
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +37 -1
- package/dist/cli/config.d.ts +6 -1
- package/dist/cli/index.js +6854 -4549
- package/dist/client/boundaries.d.ts +4 -4
- package/dist/client/internal/runtime-types.d.ts +30 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +712 -338
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +5 -1
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +2 -2
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +108 -111
- package/dist/client.js +16 -17
- package/dist/config.d.ts +11 -2
- package/dist/config.js +7 -3
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +40 -21
- package/dist/furin.js +7198 -3518
- 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 +933 -423
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -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/auto-invalidate/registry.d.ts +8 -1
- package/dist/server/cache/dev-loader.d.ts +3 -2
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +20 -2
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +15 -2
- package/dist/server/cache/registry.d.ts +3 -3
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/ssg.d.ts +6 -2
- package/dist/server/cache/store-view.d.ts +11 -0
- package/dist/server/dev-page-plugin.d.ts +4 -0
- 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/instance.d.ts +86 -0
- package/dist/server/internal.d.ts +18 -1
- package/dist/server/internal.js +74 -7
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/render/assemble.d.ts +4 -0
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -2
- package/dist/server/render/loaders.d.ts +8 -0
- package/dist/server/render/not-found.d.ts +1 -1
- 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/ssr.d.ts +2 -3
- package/dist/server/render/template.d.ts +3 -2
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/index.d.ts +2 -1
- package/dist/server/router/index.js +23 -23
- package/dist/server/router/index.ts +1 -1
- package/dist/server/router/patterns.d.ts +1 -1
- package/dist/server/router/plugin.d.ts +4 -7
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +12 -7
- package/dist/server/router/types.d.ts +1 -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 +9 -2
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +2868 -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 +16 -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 -4
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/prefix.d.ts +15 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/utils/index.d.ts +1 -1
- package/package.json +59 -12
- package/src/adapter/bun.ts +175 -55
- package/src/adapter/package.ts +208 -0
- package/src/adapter/static.ts +94 -48
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +31 -27
- package/src/build/compile-entry.ts +88 -83
- package/src/build/entry-template.ts +109 -56
- package/src/build/hydrate.ts +37 -10
- package/src/build/index.ts +111 -28
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +7 -8
- package/src/build/scan-server.ts +26 -9
- package/src/build/server-routes-entry.ts +5 -11
- package/src/build/ssg-cache.ts +13 -4
- package/src/build/types.ts +38 -1
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +73 -24
- package/src/client/boundaries.tsx +42 -18
- package/src/client/default-screens.tsx +4 -4
- package/src/client/internal/runtime-types.ts +31 -0
- package/src/client/link.tsx +35 -37
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +66 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +74 -52
- package/src/client/router/search/index.ts +5 -7
- 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 +2 -2
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +221 -174
- package/src/config.ts +14 -1
- package/src/devtools/devtools-element.js +884 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +414 -171
- 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/transform-client.ts +237 -273
- package/src/plugin/transform-isomorphic.ts +464 -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 +23 -1
- package/src/server/auto-invalidate/runtime.ts +58 -7
- package/src/server/cache/dev-loader.ts +143 -44
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +100 -41
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +95 -5
- package/src/server/cache/registry.ts +17 -20
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/ssg.ts +19 -4
- package/src/server/cache/store-view.ts +30 -0
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +8 -6
- 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 +267 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +252 -0
- package/src/server/internal.ts +102 -5
- package/src/server/lang-detect.ts +14 -10
- package/src/server/render/assemble.ts +73 -10
- package/src/server/render/element.tsx +11 -8
- package/src/server/render/index.ts +6 -2
- package/src/server/render/isr.ts +154 -110
- package/src/server/render/loaders.ts +259 -45
- package/src/server/render/not-found.ts +18 -17
- 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 +6 -5
- package/src/server/render/ssr.ts +170 -70
- package/src/server/render/template.ts +59 -25
- package/src/server/router/discovery.ts +39 -22
- package/src/server/router/hmr.ts +15 -13
- package/src/server/router/index.ts +1 -1
- package/src/server/router/patterns.ts +16 -15
- package/src/server/router/plugin.ts +151 -158
- package/src/server/router/schema-merge.ts +52 -0
- package/src/server/router/schemas.ts +55 -73
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +22 -9
- 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 -90
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +15 -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 +141 -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 +136 -70
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +4 -4
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/parser.ts +5 -0
- package/src/shared/prefix.ts +38 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +5 -3
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/server/dev-inspector.ts +0 -60
- package/src/server/sync/memory-adapter.ts +0 -196
package/src/furin.ts
CHANGED
|
@@ -5,57 +5,83 @@ import { staticPlugin } from "@elysiajs/static";
|
|
|
5
5
|
import { type AnyElysia, Elysia, file } from "elysia";
|
|
6
6
|
import { type DrainContext, initLogger } from "evlog";
|
|
7
7
|
import { type EvlogElysiaOptions, evlog } from "evlog/elysia";
|
|
8
|
-
import {
|
|
9
|
-
_runWithRequestInvalidationScope,
|
|
10
|
-
consumePendingInvalidations,
|
|
11
|
-
getBuildId,
|
|
12
|
-
setBuildId,
|
|
13
|
-
} from "./server/cache/invalidation.ts";
|
|
8
|
+
import { consumePendingInvalidations } from "./server/cache/invalidation.ts";
|
|
14
9
|
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
10
|
+
import {
|
|
11
|
+
createInstrumentationPlugin,
|
|
12
|
+
instrumentationLoggerExclusions,
|
|
13
|
+
runWithRequestInstrumentation,
|
|
14
|
+
shouldInstrumentRequest,
|
|
15
|
+
} from "./server/devtools/instrumentation.ts";
|
|
16
|
+
import {
|
|
17
|
+
assertPrefixAvailable,
|
|
18
|
+
createInstance,
|
|
19
|
+
type FurinInstance,
|
|
20
|
+
hasRequestScope,
|
|
21
|
+
markTraffic,
|
|
22
|
+
normalizePrefix,
|
|
23
|
+
registerInstance,
|
|
24
|
+
resolveInstanceByPath,
|
|
25
|
+
runWithInstanceScope,
|
|
26
|
+
withInstance,
|
|
27
|
+
} from "./server/instance.ts";
|
|
15
28
|
import type { CompileContext, EmbeddedAppData } from "./server/internal.ts";
|
|
16
29
|
import { getCompileContext } from "./server/internal.ts";
|
|
17
|
-
import { renderRootNotFound
|
|
30
|
+
import { renderRootNotFound } from "./server/render/not-found.ts";
|
|
31
|
+
import { warmSSGCache } from "./server/render/ssg.ts";
|
|
18
32
|
import {
|
|
19
33
|
setProductionTemplateContent,
|
|
20
34
|
setProductionTemplatePath,
|
|
21
35
|
} from "./server/render/template.ts";
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
createSearchRouteMetadata,
|
|
26
|
-
loadProdRoutes,
|
|
27
|
-
} from "./server/router/index.ts";
|
|
36
|
+
import { loadProdRoutes } from "./server/router/discovery.ts";
|
|
37
|
+
import { createDataEndpoint, createRoutePlugin } from "./server/router/plugin.ts";
|
|
38
|
+
import { createSearchRouteMetadata } from "./server/router/schemas.ts";
|
|
28
39
|
import { IS_DEV } from "./server/runtime-env.ts";
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
type FurinSyncOption,
|
|
32
|
-
resolveSyncStreamPath,
|
|
33
|
-
runWithSyncStreamPath,
|
|
34
|
-
} from "./server/sync/index.ts";
|
|
40
|
+
import { type FurinSyncOption, resolveSyncStreamPath } from "./server/sync/config.ts";
|
|
41
|
+
import { createSyncStreamPlugin } from "./server/sync/stream.ts";
|
|
35
42
|
|
|
36
43
|
// biome-ignore lint/suspicious/noEmptyInterface: intentionally augmentable via furin-env.d.ts
|
|
37
44
|
export interface FurinCacheTags {}
|
|
38
45
|
|
|
39
46
|
export type CacheTag = keyof FurinCacheTags extends never ? string : keyof FurinCacheTags;
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
import { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
49
|
+
|
|
50
|
+
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
51
|
+
export {
|
|
52
|
+
type ClientIsomorphicFn,
|
|
53
|
+
createIsomorphicFn,
|
|
54
|
+
type IsomorphicFn,
|
|
55
|
+
type IsomorphicFnBuilder,
|
|
56
|
+
type ServerIsomorphicFn,
|
|
57
|
+
} from "./isomorphic.ts";
|
|
58
|
+
export { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
59
|
+
|
|
60
|
+
const MAX_BROWSER_INGEST_BYTES = 64 * 1024;
|
|
61
|
+
const MAX_BROWSER_INGEST_EVENTS = 100;
|
|
62
|
+
|
|
63
|
+
function resolveClientDirFromArgv(prefix: string): string {
|
|
64
|
+
const dirName = clientDirNameForPrefix(prefix);
|
|
42
65
|
return (
|
|
43
|
-
resolveClientDirFromEnv() ??
|
|
44
|
-
resolveClientDirFromModuleUrl() ??
|
|
45
|
-
resolveClientDirFromProcessArgs() ??
|
|
46
|
-
resolveFallbackClientDir()
|
|
66
|
+
resolveClientDirFromEnv(dirName) ??
|
|
67
|
+
resolveClientDirFromModuleUrl(dirName) ??
|
|
68
|
+
resolveClientDirFromProcessArgs(dirName) ??
|
|
69
|
+
resolveFallbackClientDir(dirName)
|
|
47
70
|
);
|
|
48
71
|
}
|
|
49
72
|
|
|
50
|
-
function resolveClientDirFromEnv(): string | null {
|
|
73
|
+
function resolveClientDirFromEnv(dirName: string): string | null {
|
|
51
74
|
const envClientDir = process.env.FURIN_CLIENT_DIR;
|
|
52
75
|
if (!envClientDir) {
|
|
53
76
|
return null;
|
|
54
77
|
}
|
|
55
|
-
|
|
78
|
+
const base = envClientDir.startsWith("/") ? envClientDir : resolve(process.cwd(), envClientDir);
|
|
79
|
+
// FURIN_CLIENT_DIR points at the ROOT instance's client dir; sibling
|
|
80
|
+
// instances live next to it under their own dir name.
|
|
81
|
+
return dirName === "client" ? base : join(dirname(base), dirName);
|
|
56
82
|
}
|
|
57
83
|
|
|
58
|
-
function resolveClientDirFromModuleUrl(): string | null {
|
|
84
|
+
function resolveClientDirFromModuleUrl(dirName: string): string | null {
|
|
59
85
|
try {
|
|
60
86
|
const moduleUrl = new URL(import.meta.url);
|
|
61
87
|
if (moduleUrl.protocol !== "file:") {
|
|
@@ -65,7 +91,7 @@ function resolveClientDirFromModuleUrl(): string | null {
|
|
|
65
91
|
if (modulePath.includes("/$bunfs/")) {
|
|
66
92
|
return null;
|
|
67
93
|
}
|
|
68
|
-
const moduleClientDir = join(dirname(modulePath),
|
|
94
|
+
const moduleClientDir = join(dirname(modulePath), dirName);
|
|
69
95
|
if (existsSync(join(moduleClientDir, "index.html"))) {
|
|
70
96
|
return moduleClientDir;
|
|
71
97
|
}
|
|
@@ -75,7 +101,7 @@ function resolveClientDirFromModuleUrl(): string | null {
|
|
|
75
101
|
return null;
|
|
76
102
|
}
|
|
77
103
|
|
|
78
|
-
function resolveClientDirFromProcessArgs(): string | null {
|
|
104
|
+
function resolveClientDirFromProcessArgs(dirName: string): string | null {
|
|
79
105
|
const candidates = [
|
|
80
106
|
process.argv[1],
|
|
81
107
|
process.argv[0],
|
|
@@ -84,7 +110,7 @@ function resolveClientDirFromProcessArgs(): string | null {
|
|
|
84
110
|
].filter((value): value is string => typeof value === "string" && value.length > 0);
|
|
85
111
|
|
|
86
112
|
for (const candidate of candidates) {
|
|
87
|
-
const resolved = resolveClientDirFromCandidate(candidate);
|
|
113
|
+
const resolved = resolveClientDirFromCandidate(candidate, dirName);
|
|
88
114
|
if (resolved) {
|
|
89
115
|
return resolved;
|
|
90
116
|
}
|
|
@@ -93,7 +119,7 @@ function resolveClientDirFromProcessArgs(): string | null {
|
|
|
93
119
|
return null;
|
|
94
120
|
}
|
|
95
121
|
|
|
96
|
-
function resolveClientDirFromCandidate(candidate: string): string | null {
|
|
122
|
+
function resolveClientDirFromCandidate(candidate: string, dirName: string): string | null {
|
|
97
123
|
const name = basename(candidate);
|
|
98
124
|
if (name === "bun" || name === "node") {
|
|
99
125
|
return null;
|
|
@@ -104,46 +130,47 @@ function resolveClientDirFromCandidate(candidate: string): string | null {
|
|
|
104
130
|
|
|
105
131
|
const absolute = candidate.startsWith("/") ? candidate : resolve(process.cwd(), candidate);
|
|
106
132
|
if (existsSync(absolute)) {
|
|
107
|
-
return join(dirname(absolute),
|
|
133
|
+
return join(dirname(absolute), dirName);
|
|
108
134
|
}
|
|
109
135
|
|
|
110
136
|
if (!candidate.includes("/")) {
|
|
111
|
-
return resolveClientDirFromPath(candidate);
|
|
137
|
+
return resolveClientDirFromPath(candidate, dirName);
|
|
112
138
|
}
|
|
113
139
|
|
|
114
140
|
return null;
|
|
115
141
|
}
|
|
116
142
|
|
|
117
|
-
function resolveClientDirFromPath(candidate: string): string | null {
|
|
143
|
+
function resolveClientDirFromPath(candidate: string, dirName: string): string | null {
|
|
118
144
|
const pathEntries = process.env.PATH?.split(":") ?? [];
|
|
119
145
|
for (const dir of pathEntries) {
|
|
120
146
|
const fullPath = join(dir, candidate);
|
|
121
147
|
if (existsSync(fullPath)) {
|
|
122
|
-
return join(dirname(fullPath),
|
|
148
|
+
return join(dirname(fullPath), dirName);
|
|
123
149
|
}
|
|
124
150
|
}
|
|
125
151
|
return null;
|
|
126
152
|
}
|
|
127
153
|
|
|
128
|
-
function resolveFallbackClientDir(): string {
|
|
129
|
-
const defaultClientDir = resolve(process.cwd(), ".furin/build/bun
|
|
154
|
+
function resolveFallbackClientDir(dirName: string): string {
|
|
155
|
+
const defaultClientDir = resolve(process.cwd(), ".furin/build/bun", dirName);
|
|
130
156
|
if (existsSync(join(defaultClientDir, "index.html"))) {
|
|
131
157
|
return defaultClientDir;
|
|
132
158
|
}
|
|
133
159
|
|
|
134
|
-
return join(process.cwd(),
|
|
160
|
+
return join(process.cwd(), dirName);
|
|
135
161
|
}
|
|
136
162
|
|
|
137
163
|
async function setupProdTemplate(
|
|
138
164
|
embedded: EmbeddedAppData | undefined,
|
|
139
|
-
clientDir: string
|
|
165
|
+
clientDir: string,
|
|
166
|
+
instance: FurinInstance
|
|
140
167
|
): Promise<void> {
|
|
141
168
|
if (embedded) {
|
|
142
169
|
if (!embedded.template) {
|
|
143
170
|
throw new Error("[furin] Embedded app is missing its HTML template (index.html).");
|
|
144
171
|
}
|
|
145
172
|
const html = await Bun.file(embedded.template).text();
|
|
146
|
-
setProductionTemplateContent(html);
|
|
173
|
+
setProductionTemplateContent(html, instance);
|
|
147
174
|
return;
|
|
148
175
|
}
|
|
149
176
|
|
|
@@ -151,27 +178,187 @@ async function setupProdTemplate(
|
|
|
151
178
|
if (!existsSync(templatePath)) {
|
|
152
179
|
throw new Error("[furin] No pre-built assets found. Run `bun run build` first.");
|
|
153
180
|
}
|
|
154
|
-
setProductionTemplatePath(templatePath);
|
|
181
|
+
setProductionTemplatePath(templatePath, instance);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Shape of one browser-submitted log event. The named keys are the dangerous
|
|
186
|
+
* ones stripped before `log.set` (prototype-pollution vectors, plus the
|
|
187
|
+
* browser `environment` which must not overwrite the server's); everything
|
|
188
|
+
* else is forwarded as-is.
|
|
189
|
+
*/
|
|
190
|
+
interface FurinBrowserEvent {
|
|
191
|
+
__proto__?: unknown;
|
|
192
|
+
constructor?: unknown;
|
|
193
|
+
environment?: unknown;
|
|
194
|
+
prototype?: unknown;
|
|
195
|
+
[key: string]: unknown;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
type BrowserIngestRead =
|
|
199
|
+
| { body: unknown; kind: "ok" }
|
|
200
|
+
| { kind: "invalid" }
|
|
201
|
+
| { kind: "oversized" };
|
|
202
|
+
|
|
203
|
+
async function readBrowserIngest(request: Request): Promise<BrowserIngestRead> {
|
|
204
|
+
const contentLength = request.headers.get("content-length");
|
|
205
|
+
if (contentLength) {
|
|
206
|
+
const declaredBytes = Number(contentLength);
|
|
207
|
+
if (Number.isFinite(declaredBytes) && declaredBytes > MAX_BROWSER_INGEST_BYTES) {
|
|
208
|
+
await request.body?.cancel();
|
|
209
|
+
return { kind: "oversized" };
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (request.body === null) {
|
|
213
|
+
return { kind: "invalid" };
|
|
214
|
+
}
|
|
215
|
+
const reader = request.body.getReader();
|
|
216
|
+
const chunks: Uint8Array[] = [];
|
|
217
|
+
let total = 0;
|
|
218
|
+
try {
|
|
219
|
+
for (;;) {
|
|
220
|
+
// biome-ignore lint/performance/noAwaitInLoops: request body chunks must be read sequentially.
|
|
221
|
+
const { done, value } = await reader.read();
|
|
222
|
+
if (done) {
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
total += value.byteLength;
|
|
226
|
+
if (total > MAX_BROWSER_INGEST_BYTES) {
|
|
227
|
+
await reader.cancel();
|
|
228
|
+
return { kind: "oversized" };
|
|
229
|
+
}
|
|
230
|
+
chunks.push(value);
|
|
231
|
+
}
|
|
232
|
+
} finally {
|
|
233
|
+
reader.releaseLock();
|
|
234
|
+
}
|
|
235
|
+
const bytes = new Uint8Array(total);
|
|
236
|
+
let offset = 0;
|
|
237
|
+
for (const chunk of chunks) {
|
|
238
|
+
bytes.set(chunk, offset);
|
|
239
|
+
offset += chunk.byteLength;
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
return { body: JSON.parse(new TextDecoder().decode(bytes)), kind: "ok" };
|
|
243
|
+
} catch {
|
|
244
|
+
return { kind: "invalid" };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Evlog wide-event plugin + browser log ingest endpoint for one instance. */
|
|
249
|
+
function createLoggerPlugin(
|
|
250
|
+
prefix: string,
|
|
251
|
+
syncStreamPath: string | undefined,
|
|
252
|
+
logger: EvlogElysiaOptions | undefined,
|
|
253
|
+
clientLogging: boolean
|
|
254
|
+
): Elysia {
|
|
255
|
+
const { exclude: userExclude, ...evlogOptions } = logger ?? {};
|
|
256
|
+
const app = new Elysia().use(
|
|
257
|
+
evlog({
|
|
258
|
+
...evlogOptions,
|
|
259
|
+
// Exclude patterns match the PHYSICAL request path — prefix them.
|
|
260
|
+
exclude: [
|
|
261
|
+
`${prefix}/_client/**`,
|
|
262
|
+
`${prefix}/public/**`,
|
|
263
|
+
`${prefix}/favicon.ico`,
|
|
264
|
+
`${prefix}/_bun_hmr_entry/**`,
|
|
265
|
+
...instrumentationLoggerExclusions(prefix),
|
|
266
|
+
...(syncStreamPath ? [`${prefix}${syncStreamPath}`] : []),
|
|
267
|
+
// Note: /_furin/data is logged with the *logical* path rewritten by
|
|
268
|
+
// createDataEndpoint via useLogger().set({ path }), so SPA navigations
|
|
269
|
+
// appear as "GET /board/123 200" — same shape as a normal SSR nav.
|
|
270
|
+
// /_furin/ingest remains loggable when browser logging is explicitly
|
|
271
|
+
// enabled so browser-side events show up.
|
|
272
|
+
// evlog's `matchesPattern` only supports `*`, `**`, `?` — extglob
|
|
273
|
+
// like `!(...)` matches nothing, so don't add patterns relying on it.
|
|
274
|
+
...(userExclude ?? []),
|
|
275
|
+
],
|
|
276
|
+
})
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
if (!clientLogging) {
|
|
280
|
+
return app as unknown as Elysia;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return app.post(
|
|
284
|
+
"/_furin/ingest",
|
|
285
|
+
async ({ log, request, status }) => {
|
|
286
|
+
const parsed = await readBrowserIngest(request);
|
|
287
|
+
if (parsed.kind === "oversized") {
|
|
288
|
+
return status(413);
|
|
289
|
+
}
|
|
290
|
+
if (parsed.kind === "invalid") {
|
|
291
|
+
return status("Bad Request");
|
|
292
|
+
}
|
|
293
|
+
const { body } = parsed;
|
|
294
|
+
if (!Array.isArray(body)) {
|
|
295
|
+
return status("Bad Request");
|
|
296
|
+
}
|
|
297
|
+
const batch = (body as DrainContext[]).slice(0, MAX_BROWSER_INGEST_EVENTS);
|
|
298
|
+
for (const entry of batch) {
|
|
299
|
+
if (!entry || typeof entry !== "object" || !("event" in entry)) {
|
|
300
|
+
log.set({ msg: "[furin] ingest: skipping malformed entry" });
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
// Pick only safe, known fields from the event to prevent prototype pollution
|
|
304
|
+
const event = entry.event as FurinBrowserEvent | undefined;
|
|
305
|
+
if (!event || typeof event !== "object") {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const {
|
|
309
|
+
__proto__,
|
|
310
|
+
constructor: _ctor,
|
|
311
|
+
prototype,
|
|
312
|
+
environment: _browserEnv,
|
|
313
|
+
...safeEvent
|
|
314
|
+
} = event;
|
|
315
|
+
log.set({ ...safeEvent, service: "furin:browser" });
|
|
316
|
+
}
|
|
317
|
+
return status("No Content");
|
|
318
|
+
},
|
|
319
|
+
{ parse: "none" }
|
|
320
|
+
) as unknown as Elysia;
|
|
155
321
|
}
|
|
156
322
|
|
|
157
323
|
/**
|
|
158
324
|
* Registers a HigherOrderFunction on the Elysia instance so that every request
|
|
159
|
-
* runs inside a fresh `AsyncLocalStorage` scope. This isolates
|
|
160
|
-
* `pendingInvalidations` per request
|
|
161
|
-
*
|
|
325
|
+
* runs inside a fresh instance-bound `AsyncLocalStorage` scope. This isolates
|
|
326
|
+
* `pendingInvalidations` per request AND binds the request to the furin
|
|
327
|
+
* instance that owns its path, so render/cache code resolves the right
|
|
328
|
+
* per-instance state (build ID, template, caches, sync path).
|
|
162
329
|
*
|
|
163
330
|
* Uses `app.wrap()` (Elysia HigherOrderFunction) instead of mutating
|
|
164
331
|
* `app.handle`, because handle mutations are lost when the Furin plugin is
|
|
165
332
|
* `.use()`-d by a parent Elysia instance. HigherOrderFunctions survive the
|
|
166
333
|
* plugin merge and wrap the entire composed `map` handler.
|
|
334
|
+
*
|
|
335
|
+
* IMPORTANT — multi-instance: HigherOrderFunctions apply to the WHOLE parent
|
|
336
|
+
* app, so with N mounted furin instances all N wraps run stacked on every
|
|
337
|
+
* request. The scope guard makes the wrap idempotent (first one wins) and the
|
|
338
|
+
* owning instance is resolved from the request PATH, never from this
|
|
339
|
+
* closure — which wrap executes first is therefore irrelevant.
|
|
167
340
|
*/
|
|
168
|
-
function wrapWithRequestScope(app: AnyElysia
|
|
169
|
-
return app.wrap(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
341
|
+
function wrapWithRequestScope(app: AnyElysia): Elysia {
|
|
342
|
+
return app.wrap((handler, request) => (ctx: unknown) => {
|
|
343
|
+
if (hasRequestScope()) {
|
|
344
|
+
return handler(ctx);
|
|
345
|
+
}
|
|
346
|
+
markTraffic();
|
|
347
|
+
const req = request ?? (ctx as { request?: Request } | undefined)?.request;
|
|
348
|
+
const pathname = req ? new URL(req.url).pathname : "/";
|
|
349
|
+
const instance = resolveInstanceByPath(pathname);
|
|
350
|
+
return runWithInstanceScope(instance, () => {
|
|
351
|
+
if (!(req && shouldInstrumentRequest(pathname, instance.prefix))) {
|
|
352
|
+
return handler(ctx);
|
|
353
|
+
}
|
|
354
|
+
return runWithRequestInstrumentation(req, () => handler(ctx));
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async function loadDevelopmentRoutes(resolvedPagesDir: string) {
|
|
360
|
+
const { scanPages } = await import("./server/router/index.ts");
|
|
361
|
+
return scanPages(resolvedPagesDir);
|
|
175
362
|
}
|
|
176
363
|
|
|
177
364
|
function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
@@ -183,6 +370,39 @@ function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
|
183
370
|
}
|
|
184
371
|
}
|
|
185
372
|
|
|
373
|
+
/** Options for the {@link furin} plugin. */
|
|
374
|
+
export interface FurinOptions {
|
|
375
|
+
/**
|
|
376
|
+
* Production only: explicit directory holding this app's built client
|
|
377
|
+
* assets (chunks + index.html template). Packaged furin apps pass their own
|
|
378
|
+
* `dist/furin/client` here; when omitted the directory is auto-resolved
|
|
379
|
+
* next to the server artifact.
|
|
380
|
+
*/
|
|
381
|
+
clientDir?: string;
|
|
382
|
+
/**
|
|
383
|
+
* Initialize the browser HTTP log drain in the hydration entry. Off by
|
|
384
|
+
* default — enabling it adds `evlog/http` drain setup and points browser
|
|
385
|
+
* events at `/_furin/ingest`. Server-side logging is configured via `logger`
|
|
386
|
+
* and unaffected.
|
|
387
|
+
*/
|
|
388
|
+
clientLogging?: boolean;
|
|
389
|
+
logger?: EvlogElysiaOptions;
|
|
390
|
+
pagesDir?: string;
|
|
391
|
+
/**
|
|
392
|
+
* Mount prefix for this app, e.g. `"/admin"`. All pages, framework
|
|
393
|
+
* endpoints (`/_furin/*`) and client assets (`/_client/*`) are served under
|
|
394
|
+
* it, and the client bundle is built with the matching basePath. Defaults
|
|
395
|
+
* to `""` (root). Mounting two furin instances on the same prefix throws.
|
|
396
|
+
*/
|
|
397
|
+
prefix?: string;
|
|
398
|
+
/**
|
|
399
|
+
* Configures Furin's sync event stream with the required adapter. The
|
|
400
|
+
* optional `streamPath` defaults to `/_furin/sync`. Omit this option or pass
|
|
401
|
+
* `false` to disable sync.
|
|
402
|
+
*/
|
|
403
|
+
sync?: FurinSyncOption;
|
|
404
|
+
}
|
|
405
|
+
|
|
186
406
|
/**
|
|
187
407
|
* Main Furin plugin.
|
|
188
408
|
*
|
|
@@ -193,108 +413,60 @@ function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
|
193
413
|
*
|
|
194
414
|
* ```ts
|
|
195
415
|
* new Elysia()
|
|
196
|
-
* .use(await furin({
|
|
416
|
+
* .use(await furin({ pagesDir: "./src/pages" }))
|
|
417
|
+
* .use(await furin({ pagesDir: "./src/admin", prefix: "/admin" }))
|
|
197
418
|
* .listen(3000)
|
|
198
419
|
* ```
|
|
199
420
|
*/
|
|
200
421
|
export async function furin({
|
|
201
422
|
pagesDir,
|
|
423
|
+
prefix: rawPrefix,
|
|
424
|
+
clientDir: explicitClientDir,
|
|
202
425
|
logger,
|
|
203
426
|
clientLogging,
|
|
204
427
|
sync,
|
|
205
|
-
}: {
|
|
206
|
-
|
|
207
|
-
logger?: EvlogElysiaOptions;
|
|
208
|
-
/**
|
|
209
|
-
* Initialize the browser HTTP log drain in the hydration entry. Off by
|
|
210
|
-
* default — enabling it adds `evlog/http` drain setup and points browser
|
|
211
|
-
* events at `/_furin/ingest`. Server-side logging is configured via `logger`
|
|
212
|
-
* and unaffected.
|
|
213
|
-
*/
|
|
214
|
-
clientLogging?: boolean;
|
|
215
|
-
/**
|
|
216
|
-
* Enables Furin's built-in sync event stream. The opinionated default mounts
|
|
217
|
-
* Server-Sent Events at `/_furin/sync`; pass an object only for constrained
|
|
218
|
-
* reverse-proxy deployments that need a custom internal path.
|
|
219
|
-
*/
|
|
220
|
-
sync?: FurinSyncOption;
|
|
221
|
-
}) {
|
|
428
|
+
}: FurinOptions = {}) {
|
|
429
|
+
const prefix = normalizePrefix(rawPrefix);
|
|
222
430
|
const syncStreamPath = resolveSyncStreamPath(sync);
|
|
223
|
-
const { exclude: userExclude, ...evlogOptions } = logger ?? {};
|
|
224
431
|
initLogger({ env: { service: "furin" } });
|
|
225
432
|
|
|
226
|
-
const loggerPlugin = new Elysia()
|
|
227
|
-
.use(
|
|
228
|
-
evlog({
|
|
229
|
-
...evlogOptions,
|
|
230
|
-
exclude: [
|
|
231
|
-
"/_client/**",
|
|
232
|
-
"/public/**",
|
|
233
|
-
"/favicon.ico",
|
|
234
|
-
"/_bun_hmr_entry/**",
|
|
235
|
-
...(syncStreamPath ? [syncStreamPath] : []),
|
|
236
|
-
// Note: /_furin/data is logged with the *logical* path rewritten by
|
|
237
|
-
// createDataEndpoint via useLogger().set({ path }), so SPA navigations
|
|
238
|
-
// appear as "GET /board/123 200" — same shape as a normal SSR nav.
|
|
239
|
-
// /_furin/ingest is kept loggable so browser-side events show up.
|
|
240
|
-
// evlog's `matchesPattern` only supports `*`, `**`, `?` — extglob
|
|
241
|
-
// like `!(...)` matches nothing, so don't add patterns relying on it.
|
|
242
|
-
...(userExclude ?? []),
|
|
243
|
-
],
|
|
244
|
-
})
|
|
245
|
-
)
|
|
246
|
-
.post(
|
|
247
|
-
"/_furin/ingest",
|
|
248
|
-
({ body, log, status }) => {
|
|
249
|
-
if (!Array.isArray(body)) {
|
|
250
|
-
return status("Bad Request");
|
|
251
|
-
}
|
|
252
|
-
// Cap batch size to prevent abuse
|
|
253
|
-
const batch = (body as DrainContext[]).slice(0, 100);
|
|
254
|
-
for (const entry of batch) {
|
|
255
|
-
if (!entry || typeof entry !== "object" || !("event" in entry)) {
|
|
256
|
-
log.set({ msg: "[furin] ingest: skipping malformed entry" });
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
// Pick only safe, known fields from the event to prevent prototype pollution
|
|
260
|
-
const event = entry.event as Record<string, unknown> | undefined;
|
|
261
|
-
if (!event || typeof event !== "object") {
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
const {
|
|
265
|
-
__proto__,
|
|
266
|
-
constructor: _ctor,
|
|
267
|
-
prototype,
|
|
268
|
-
environment: _browserEnv,
|
|
269
|
-
...safeEvent
|
|
270
|
-
} = event as Record<string, unknown>;
|
|
271
|
-
log.set({ ...safeEvent, service: "furin:browser" });
|
|
272
|
-
}
|
|
273
|
-
return status("No Content");
|
|
274
|
-
},
|
|
275
|
-
{ parse: "json" }
|
|
276
|
-
);
|
|
277
|
-
|
|
278
433
|
const cwd = process.cwd();
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
434
|
+
// The pagesDir param drives which compile context this instance loads. In a
|
|
435
|
+
// deployed binary the cwd-resolved path may miss the build-time key — the
|
|
436
|
+
// lookup then falls back to the (stable) prefix, then to the sole context.
|
|
437
|
+
const paramPagesDir = resolve(cwd, pagesDir ?? "src/pages");
|
|
438
|
+
const ctx = getCompileContext(paramPagesDir, prefix);
|
|
439
|
+
const loggerPlugin = createLoggerPlugin(
|
|
440
|
+
prefix,
|
|
441
|
+
syncStreamPath,
|
|
442
|
+
logger,
|
|
443
|
+
clientLogging === true || ctx?.clientLogging === true
|
|
444
|
+
);
|
|
445
|
+
const resolvedPagesDir = ctx?.rootPath ? dirname(ctx.rootPath) : paramPagesDir;
|
|
283
446
|
|
|
284
447
|
// Unique name per pagesDir to avoid Elysia's name-based plugin dedup.
|
|
285
|
-
const instanceName = `furin-${resolvedPagesDir.replaceAll("\\", "/")}`;
|
|
448
|
+
const instanceName = `furin-${prefix}-${resolvedPagesDir.replaceAll("\\", "/")}`;
|
|
449
|
+
|
|
450
|
+
// Same prefix + different pagesDir is a mount collision — fail fast, but
|
|
451
|
+
// only REGISTER right before returning so a failed mount leaves no stale
|
|
452
|
+
// registration behind. All per-app runtime state (build ID, caches,
|
|
453
|
+
// template, sync path) hangs off this object; requests are bound to it by
|
|
454
|
+
// path in wrapWithRequestScope.
|
|
455
|
+
const normalizedPagesDir = resolvedPagesDir.replaceAll("\\", "/");
|
|
456
|
+
assertPrefixAvailable(prefix, normalizedPagesDir);
|
|
457
|
+
const instance = createInstance(prefix, normalizedPagesDir);
|
|
458
|
+
instance.syncStreamPath = syncStreamPath;
|
|
286
459
|
|
|
287
460
|
// ── Dev: Bun native HMR ────────────────────────────────────────────────
|
|
288
461
|
if (IS_DEV) {
|
|
289
|
-
|
|
462
|
+
// Each instance gets its own generated-files dir so two mounted apps do
|
|
463
|
+
// not overwrite each other's hydrate entry (root keeps plain `.furin`).
|
|
464
|
+
const instanceSlug = prefix === "" ? "" : prefix.slice(1).replaceAll("/", "__");
|
|
465
|
+
const furinDir = resolve(cwd, ".furin", instanceSlug);
|
|
290
466
|
// Lazy import — build pipeline has native deps not available in compiled binaries
|
|
291
467
|
const { registerDevPagePlugin } = await import("./server/dev-page-plugin.ts");
|
|
292
468
|
registerDevPagePlugin();
|
|
293
|
-
|
|
294
|
-
const { createDevInspectorPlugin } = await import("./server/dev-inspector.ts");
|
|
295
|
-
|
|
296
|
-
const { scanPages } = await import("./server/router/index.ts");
|
|
297
|
-
const { root, routes } = await scanPages(resolvedPagesDir);
|
|
469
|
+
const { root, routes } = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
298
470
|
const searchRoutes = createSearchRouteMetadata(routes);
|
|
299
471
|
|
|
300
472
|
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
@@ -303,9 +475,12 @@ export async function furin({
|
|
|
303
475
|
{
|
|
304
476
|
outDir: furinDir,
|
|
305
477
|
rootLayout: root.path,
|
|
306
|
-
basePath:
|
|
307
|
-
publicPath:
|
|
478
|
+
basePath: prefix,
|
|
479
|
+
publicPath: `${prefix}/_client/`,
|
|
308
480
|
clientLogging: clientLogging ?? false,
|
|
481
|
+
// furin-env.d.ts is one file at the project root — only the root
|
|
482
|
+
// instance owns it, otherwise mounted apps clobber each other's types.
|
|
483
|
+
skipRouteTypes: prefix !== "",
|
|
309
484
|
},
|
|
310
485
|
cwd
|
|
311
486
|
);
|
|
@@ -313,14 +488,23 @@ export async function furin({
|
|
|
313
488
|
const publicDir = resolve(cwd, "public");
|
|
314
489
|
const publicExists = existsSync(publicDir);
|
|
315
490
|
|
|
316
|
-
|
|
491
|
+
// Routes registered below are LOGICAL — Elysia's `prefix` makes them
|
|
492
|
+
// physical when this plugin is merged into the parent app (child prefixes
|
|
493
|
+
// like staticPlugin's compose underneath).
|
|
494
|
+
const devApp = new Elysia({
|
|
495
|
+
name: instanceName,
|
|
496
|
+
seed: resolvedPagesDir,
|
|
497
|
+
prefix: prefix || undefined,
|
|
498
|
+
})
|
|
317
499
|
.use(loggerPlugin)
|
|
318
|
-
|
|
500
|
+
// Local scope (default) — a global hook would leak onto sibling furin
|
|
501
|
+
// instances mounted on the same parent app.
|
|
502
|
+
.onError(async ({ code, request, server }) => {
|
|
319
503
|
if (code === "NOT_FOUND") {
|
|
320
|
-
return await renderRootNotFound(root, request);
|
|
504
|
+
return await renderRootNotFound(root, request, server?.url.origin);
|
|
321
505
|
}
|
|
322
506
|
})
|
|
323
|
-
.onAfterHandle(
|
|
507
|
+
.onAfterHandle(({ set }) => {
|
|
324
508
|
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
325
509
|
const pending = consumePendingInvalidations();
|
|
326
510
|
if (pending.length > 0) {
|
|
@@ -337,16 +521,18 @@ export async function furin({
|
|
|
337
521
|
? file(join(publicDir, "favicon.ico"))
|
|
338
522
|
: () => new Response(null, { status: 404 })
|
|
339
523
|
)
|
|
340
|
-
.use(
|
|
341
|
-
.use(
|
|
524
|
+
.use(createInstrumentationPlugin(routes, syncStreamPath))
|
|
525
|
+
.use(sync ? createSyncStreamPlugin(sync) : new Elysia())
|
|
342
526
|
.use(createDataEndpoint(routes))
|
|
343
|
-
.use((app) =>
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
527
|
+
.use((app) =>
|
|
528
|
+
routes.reduce(
|
|
529
|
+
(chain, route) => chain.use(createRoutePlugin(route, root, undefined, searchRoutes)),
|
|
530
|
+
app
|
|
531
|
+
)
|
|
532
|
+
)
|
|
533
|
+
.use(createNotFoundHandling(prefix, routes, root));
|
|
534
|
+
registerInstance(instance);
|
|
535
|
+
return wrapWithRequestScope(devApp);
|
|
350
536
|
}
|
|
351
537
|
|
|
352
538
|
// ── Production ──────────────────────────────────────────────────────────
|
|
@@ -356,24 +542,35 @@ export async function furin({
|
|
|
356
542
|
const { root, routes } = loadProdRoutes(ctx);
|
|
357
543
|
const searchRoutes = createSearchRouteMetadata(routes);
|
|
358
544
|
const prodBuildId = ctx.buildId ?? "";
|
|
359
|
-
|
|
360
|
-
|
|
545
|
+
instance.buildId = prodBuildId;
|
|
546
|
+
// Init-time writes target THIS instance explicitly — with several mounted
|
|
547
|
+
// apps there is no ambient request scope to resolve it from.
|
|
548
|
+
withInstance(instance, () => {
|
|
549
|
+
hydrateSSGCacheFromCompileContext(ctx);
|
|
550
|
+
});
|
|
361
551
|
|
|
362
552
|
const embedded = ctx?.embedded;
|
|
363
|
-
const clientDir = embedded ? "" : resolveClientDirFromArgv();
|
|
364
|
-
await setupProdTemplate(embedded, clientDir);
|
|
365
|
-
|
|
366
|
-
const
|
|
553
|
+
const clientDir = embedded ? "" : (explicitClientDir ?? resolveClientDirFromArgv(prefix));
|
|
554
|
+
await setupProdTemplate(embedded, clientDir, instance);
|
|
555
|
+
|
|
556
|
+
const clientAssetPrefix = `${prefix}/_client/`;
|
|
557
|
+
const prodApp = new Elysia({
|
|
558
|
+
name: instanceName,
|
|
559
|
+
seed: resolvedPagesDir,
|
|
560
|
+
prefix: prefix || undefined,
|
|
561
|
+
})
|
|
367
562
|
.use(loggerPlugin)
|
|
368
|
-
|
|
563
|
+
// Local scope (default) — a global hook would leak onto sibling furin
|
|
564
|
+
// instances mounted on the same parent app.
|
|
565
|
+
.onError(async ({ code, request, server }) => {
|
|
369
566
|
if (code === "NOT_FOUND") {
|
|
370
|
-
return await renderRootNotFound(root, request);
|
|
567
|
+
return await renderRootNotFound(root, request, server?.url.origin);
|
|
371
568
|
}
|
|
372
569
|
})
|
|
373
|
-
.onAfterHandle(
|
|
570
|
+
.onAfterHandle(({ path, set }) => {
|
|
374
571
|
// Content-hashed client assets are permanently cacheable — browsers never need to
|
|
375
572
|
// revalidate them because any change produces a new filename.
|
|
376
|
-
if (path.startsWith(
|
|
573
|
+
if (path.startsWith(clientAssetPrefix)) {
|
|
377
574
|
set.headers["cache-control"] = "public, max-age=31536000, immutable";
|
|
378
575
|
}
|
|
379
576
|
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
@@ -382,9 +579,8 @@ export async function furin({
|
|
|
382
579
|
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
383
580
|
}
|
|
384
581
|
// Tell the client the current build ID so it can detect stale deploys
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
set.headers["x-furin-build-id"] = buildId;
|
|
582
|
+
if (instance.buildId) {
|
|
583
|
+
set.headers["x-furin-build-id"] = instance.buildId;
|
|
388
584
|
}
|
|
389
585
|
})
|
|
390
586
|
.onStart(async ({ server }) => {
|
|
@@ -392,7 +588,9 @@ export async function furin({
|
|
|
392
588
|
return;
|
|
393
589
|
}
|
|
394
590
|
const origin = server?.url?.origin ?? "http://localhost:3000";
|
|
395
|
-
|
|
591
|
+
// Synthetic (non-request) renders — bind them to this instance so the
|
|
592
|
+
// render pipeline resolves its template/caches, not a sibling's.
|
|
593
|
+
await withInstance(instance, () => warmSSGCache(routes, root, origin, searchRoutes));
|
|
396
594
|
})
|
|
397
595
|
.use(
|
|
398
596
|
await (async () => {
|
|
@@ -431,29 +629,74 @@ export async function furin({
|
|
|
431
629
|
return app;
|
|
432
630
|
})()
|
|
433
631
|
)
|
|
434
|
-
.use(
|
|
632
|
+
.use(sync ? createSyncStreamPlugin(sync) : new Elysia())
|
|
435
633
|
.use(createDataEndpoint(routes))
|
|
436
|
-
.use((app) =>
|
|
437
|
-
|
|
438
|
-
|
|
634
|
+
.use((app) =>
|
|
635
|
+
routes.reduce(
|
|
636
|
+
(chain, route) => chain.use(createRoutePlugin(route, root, prodBuildId, searchRoutes)),
|
|
637
|
+
app
|
|
638
|
+
)
|
|
639
|
+
)
|
|
640
|
+
.use(createNotFoundHandling(prefix, routes, root));
|
|
641
|
+
registerInstance(instance);
|
|
642
|
+
return wrapWithRequestScope(prodApp);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* 404 handling per mount position:
|
|
647
|
+
*
|
|
648
|
+
* - ROOT instance (`prefix === ""`): the historical `{as:"global"}`
|
|
649
|
+
* onError(NOT_FOUND) hook — it owns the root scope, and a parent `.onError`
|
|
650
|
+
* registered BEFORE `.use(furin)` still wins (documented escape hatch for
|
|
651
|
+
* JSON API 404s).
|
|
652
|
+
* - PREFIXED instance: a global hook would leak onto sibling apps, and a
|
|
653
|
+
* local one never sees unmatched paths (they belong to no route). Instead
|
|
654
|
+
* the instance registers a lowest-priority catch-all under its own prefix;
|
|
655
|
+
* the router prefers every more-specific route, so this only fires for
|
|
656
|
+
* paths no page matched. Skipped when the app defines its own `[...rest]`
|
|
657
|
+
* catch-all page.
|
|
658
|
+
*/
|
|
659
|
+
function createNotFoundHandling(
|
|
660
|
+
prefix: string,
|
|
661
|
+
routes: Array<{ pattern: string }>,
|
|
662
|
+
root: Parameters<typeof renderRootNotFound>[0]
|
|
663
|
+
): Elysia {
|
|
664
|
+
const app = new Elysia();
|
|
665
|
+
if (prefix === "") {
|
|
666
|
+
app.onError({ as: "global" }, async ({ code, request, server }) => {
|
|
667
|
+
if (code === "NOT_FOUND") {
|
|
668
|
+
return await renderRootNotFound(root, request, server?.url.origin);
|
|
439
669
|
}
|
|
440
|
-
return app;
|
|
441
670
|
});
|
|
442
|
-
|
|
671
|
+
return app;
|
|
672
|
+
}
|
|
673
|
+
if (routes.some((route) => route.pattern === "/*")) {
|
|
674
|
+
return app;
|
|
675
|
+
}
|
|
676
|
+
app.get("/*", ({ request, server }) => renderRootNotFound(root, request, server?.url.origin));
|
|
677
|
+
return app;
|
|
443
678
|
}
|
|
444
679
|
|
|
445
|
-
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
446
680
|
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
447
681
|
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
448
682
|
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
449
|
-
export type { DeferredData
|
|
683
|
+
export type { DeferredData } from "./client.ts";
|
|
450
684
|
export { defer, isDeferred } from "./client.ts";
|
|
451
685
|
export type { InvalidationInput, InvalidationRule } from "./server/auto-invalidate/index.ts";
|
|
452
686
|
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
453
687
|
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
454
688
|
export { buildElement, buildErrorElement, renderRootNotFound } from "./server/render/index.ts";
|
|
455
689
|
export type { ResolvedRoute, SegmentBoundary } from "./server/router/index.ts";
|
|
456
|
-
export {
|
|
690
|
+
export {
|
|
691
|
+
type FurinSyncOption,
|
|
692
|
+
type FurinSyncOptions,
|
|
693
|
+
furinSync,
|
|
694
|
+
type SyncAdapter,
|
|
695
|
+
type SyncInput,
|
|
696
|
+
type SyncNotifier,
|
|
697
|
+
type SyncRouteOption,
|
|
698
|
+
type SyncRuntimeOptions,
|
|
699
|
+
} from "./server/sync/index.ts";
|
|
457
700
|
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
458
701
|
export type { ErrorComponent, ErrorProps } from "./shared/error.ts";
|
|
459
702
|
export type {
|