@teyik0/furin 0.1.0-alpha.9 → 0.2.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/LICENSE +21 -0
- package/README.md +33 -0
- package/package.json +61 -25
- package/src/adapter/bun.ts +90 -41
- package/src/adapter/static.ts +100 -40
- package/src/build/client.ts +13 -14
- package/src/build/compile-entry.ts +20 -14
- package/src/build/entry-template.ts +90 -8
- package/src/build/hydrate.ts +219 -36
- package/src/build/index.ts +10 -9
- package/src/build/route-types.ts +65 -9
- package/src/build/scan-server.ts +52 -67
- package/src/build/server-routes-entry.ts +7 -11
- package/src/build/shared.ts +2 -1
- package/src/build/ssg-cache.ts +32 -0
- package/src/build/types.ts +5 -1
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +259 -0
- package/src/client/default-screens.tsx +304 -0
- package/src/client/link.tsx +347 -0
- package/src/client/router/boundary-tree.tsx +116 -0
- package/src/client/router/context.ts +40 -0
- package/src/client/router/history.ts +41 -0
- package/src/client/router/index.ts +56 -0
- package/src/client/router/link-utils.ts +177 -0
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +759 -0
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/spa-response.ts +143 -0
- package/src/client/router/types.ts +271 -0
- package/src/client/server-error.ts +47 -0
- package/src/client.ts +80 -3
- package/src/config.ts +11 -3
- package/src/env.d.ts +1 -0
- package/src/furin.ts +114 -29
- package/src/plugin/index.ts +7 -8
- package/src/plugin/transform-client.ts +146 -120
- package/src/server/auto-invalidate/index.ts +10 -0
- package/src/server/auto-invalidate/plugin.ts +25 -0
- package/src/server/auto-invalidate/registry.ts +60 -0
- package/src/server/auto-invalidate/runtime.ts +133 -0
- package/src/server/auto-invalidate/types.ts +16 -0
- package/src/server/cache/dev-loader.ts +211 -0
- package/src/server/cache/index.ts +34 -0
- package/src/server/cache/invalidation.ts +111 -0
- package/src/server/cache/isr-ssg.ts +41 -0
- package/src/server/cache/isr.ts +17 -0
- package/src/server/cache/registry.ts +35 -0
- package/src/server/cache/route-cache.ts +124 -0
- package/src/server/cache/ssg.ts +15 -0
- package/src/{context-logger.ts → server/context-logger.ts} +5 -0
- package/src/server/dev-inspector.ts +60 -0
- package/src/{dev-page-plugin.ts → server/dev-page-plugin.ts} +15 -0
- package/src/{internal.ts → server/internal.ts} +17 -0
- package/src/server/lang-detect.ts +46 -0
- package/src/server/render/assemble.ts +133 -0
- package/src/server/render/element.tsx +113 -0
- package/src/server/render/index.ts +22 -0
- package/src/server/render/isr.ts +317 -0
- package/src/server/render/loaders.ts +328 -0
- package/src/server/render/not-found.ts +98 -0
- package/src/{render → server/render}/shell.ts +28 -13
- package/src/server/render/ssg.ts +148 -0
- package/src/server/render/ssr.ts +583 -0
- package/src/server/router/discovery.ts +474 -0
- package/src/server/router/hmr.ts +376 -0
- package/src/server/router/index.ts +28 -0
- package/src/server/router/patterns.ts +219 -0
- package/src/server/router/plugin.ts +384 -0
- package/src/server/router/schemas.ts +278 -0
- package/src/server/router/types.ts +59 -0
- package/src/shared/await.tsx +192 -0
- package/src/shared/deferred-ndjson.ts +244 -0
- package/src/shared/digest.ts +71 -0
- package/src/shared/error.ts +49 -0
- package/src/shared/not-found.ts +40 -0
- package/src/shared/search-params.ts +182 -0
- package/src/shared/utils/ast-walk.ts +51 -0
- package/src/{utils.ts → shared/utils/index.ts} +32 -1
- package/dist/adapter/bun.d.ts +0 -3
- package/dist/adapter/static.d.ts +0 -3
- package/dist/build/client.d.ts +0 -25
- package/dist/build/compile-entry.d.ts +0 -23
- package/dist/build/entry-template.d.ts +0 -14
- package/dist/build/hydrate.d.ts +0 -24
- package/dist/build/index.d.ts +0 -7
- package/dist/build/index.js +0 -3727
- package/dist/build/route-types.d.ts +0 -21
- package/dist/build/scan-server.d.ts +0 -8
- package/dist/build/server-routes-entry.d.ts +0 -23
- package/dist/build/shared.d.ts +0 -11
- package/dist/build/types.d.ts +0 -76
- package/dist/cli/config.d.ts +0 -9
- package/dist/cli/index.d.ts +0 -2
- package/dist/cli/index.js +0 -3762
- package/dist/client.d.ts +0 -195
- package/dist/client.js +0 -20
- package/dist/config.d.ts +0 -47
- package/dist/config.js +0 -28
- package/dist/context-logger.d.ts +0 -26
- package/dist/dev-page-plugin.d.ts +0 -109
- package/dist/furin.d.ts +0 -34
- package/dist/furin.js +0 -2744
- package/dist/internal.d.ts +0 -19
- package/dist/link.d.ts +0 -245
- package/dist/link.js +0 -1232
- package/dist/plugin/index.d.ts +0 -20
- package/dist/plugin/index.js +0 -1408
- package/dist/plugin/transform-client.d.ts +0 -9
- package/dist/render/assemble.d.ts +0 -15
- package/dist/render/cache.d.ts +0 -95
- package/dist/render/element.d.ts +0 -4
- package/dist/render/index.d.ts +0 -27
- package/dist/render/loaders.d.ts +0 -12
- package/dist/render/shell.d.ts +0 -29
- package/dist/render/template.d.ts +0 -6
- package/dist/router.d.ts +0 -45
- package/dist/router.js +0 -1671
- package/dist/runtime-env.d.ts +0 -3
- package/dist/utils.d.ts +0 -6
- package/src/link.tsx +0 -1120
- package/src/render/assemble.ts +0 -66
- package/src/render/cache.ts +0 -241
- package/src/render/element.tsx +0 -28
- package/src/render/index.ts +0 -483
- package/src/render/loaders.ts +0 -117
- package/src/router.ts +0 -439
- /package/src/{render → server/render}/template.ts +0 -0
- /package/src/{runtime-env.ts → server/runtime-env.ts} +0 -0
package/src/client.ts
CHANGED
|
@@ -11,6 +11,9 @@ import type { Cookie, StatusMap } from "elysia";
|
|
|
11
11
|
import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
|
|
12
12
|
import type { RequestLogger } from "evlog";
|
|
13
13
|
|
|
14
|
+
// biome-ignore lint/performance/noBarrelFile: client.ts is the canonical DX entry for furin/client consumers — not a barrel
|
|
15
|
+
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
16
|
+
|
|
14
17
|
declare const UNSET: unique symbol;
|
|
15
18
|
type Unset = typeof UNSET;
|
|
16
19
|
|
|
@@ -36,9 +39,15 @@ type NormalizeUnset<T> = [T] extends [Unset] ? {} : T;
|
|
|
36
39
|
* Transforms each key of a parent-data record into an individual `Promise<T>`.
|
|
37
40
|
* This is what child loaders see for inherited fields — direct values from
|
|
38
41
|
* `RouteContext` (request, params, set, …) remain unchanged.
|
|
42
|
+
*
|
|
43
|
+
* `Awaited<T[K]>` mirrors the JS Promise-chaining auto-flatten used by
|
|
44
|
+
* `createLoaderCtx`: when an ancestor wrapped a field with `defer()` (so the
|
|
45
|
+
* raw value is itself a `Promise<U>`), the type collapses to `Promise<U>`
|
|
46
|
+
* instead of `Promise<Promise<U>>`. Otherwise TypeScript would force a
|
|
47
|
+
* redundant `await await ctx.field` dance.
|
|
39
48
|
*/
|
|
40
49
|
type PromisifyData<T extends Record<string, unknown>> = {
|
|
41
|
-
[K in keyof T]: Promise<T[K]
|
|
50
|
+
[K in keyof T]: Promise<Awaited<T[K]>>;
|
|
42
51
|
};
|
|
43
52
|
|
|
44
53
|
export interface RouteContext<TParams = {}, TQuery = {}> {
|
|
@@ -69,7 +78,11 @@ type ResolveParent<T> =
|
|
|
69
78
|
: { data: {}; params: Unset; query: Unset };
|
|
70
79
|
|
|
71
80
|
interface Resolved<TParentRef, TLoaderData, TParamsSchema = Unset, TQuerySchema = Unset> {
|
|
72
|
-
|
|
81
|
+
// `Omit<TLoaderData, "__isDeferred">` strips the runtime brand that `defer()`
|
|
82
|
+
// attaches to its return value. A layout/route loader wrapped with `defer()`
|
|
83
|
+
// would otherwise leak `__isDeferred: true` into descendant loader contexts
|
|
84
|
+
// and component props.
|
|
85
|
+
data: ToRecord<ResolveParent<TParentRef>["data"] & Omit<TLoaderData, "__isDeferred">>;
|
|
73
86
|
params: MergeSchema<ResolveParent<TParentRef>["params"], ResolvedSchema<TParamsSchema>>;
|
|
74
87
|
query: MergeSchema<ResolveParent<TParentRef>["query"], ResolvedSchema<TQuerySchema>>;
|
|
75
88
|
}
|
|
@@ -110,9 +123,12 @@ export interface HeadOptions {
|
|
|
110
123
|
// Extracts the resolved return type from a loader function type.
|
|
111
124
|
// Using ReturnType + Awaited here means TLoader is inferred as the whole function
|
|
112
125
|
// first, and then we extract the data — so inference is order-independent.
|
|
126
|
+
// The `__isDeferred` key (DEFERRED_BRAND, see below) is stripped: a `defer()`
|
|
127
|
+
// return carries that runtime marker, but it must never surface as a
|
|
128
|
+
// component / head() prop — it is an internal implementation detail.
|
|
113
129
|
type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown
|
|
114
130
|
? Awaited<ReturnType<TLoader>> extends object
|
|
115
|
-
? ToRecord<Awaited<ReturnType<TLoader
|
|
131
|
+
? ToRecord<Omit<Awaited<ReturnType<TLoader>>, "__isDeferred">>
|
|
116
132
|
: {}
|
|
117
133
|
: {};
|
|
118
134
|
|
|
@@ -128,6 +144,7 @@ export interface PageConfig<
|
|
|
128
144
|
ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
|
|
129
145
|
) => Promise<TPageLoaderData> | TPageLoaderData;
|
|
130
146
|
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
147
|
+
tags?: string[];
|
|
131
148
|
}
|
|
132
149
|
|
|
133
150
|
export interface RuntimeRoute {
|
|
@@ -139,6 +156,7 @@ export interface RuntimeRoute {
|
|
|
139
156
|
parent?: RuntimeRoute;
|
|
140
157
|
query?: unknown;
|
|
141
158
|
revalidate?: number;
|
|
159
|
+
tags?: string[];
|
|
142
160
|
}
|
|
143
161
|
|
|
144
162
|
export interface RuntimePage {
|
|
@@ -148,6 +166,7 @@ export interface RuntimePage {
|
|
|
148
166
|
head?(ctx: Record<string, unknown>): HeadOptions;
|
|
149
167
|
loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
150
168
|
staticParams?(): Promise<Record<string, string>[]> | Record<string, string>[];
|
|
169
|
+
tags?: string[];
|
|
151
170
|
}
|
|
152
171
|
|
|
153
172
|
export interface RouteRef<
|
|
@@ -172,6 +191,7 @@ interface PageResult<
|
|
|
172
191
|
loader?: (
|
|
173
192
|
ctx: RouteContext<TParams, TQuery> & PromisifyData<TData>
|
|
174
193
|
) => Promise<TPageLoaderData> | TPageLoaderData;
|
|
194
|
+
tags?: string[];
|
|
175
195
|
}
|
|
176
196
|
|
|
177
197
|
export interface Route<TParentData extends Record<string, unknown>, TParams, TQuery> {
|
|
@@ -194,6 +214,7 @@ export interface Route<TParentData extends Record<string, unknown>, TParams, TQu
|
|
|
194
214
|
mode?: "ssr" | "ssg" | "isr";
|
|
195
215
|
revalidate?: number;
|
|
196
216
|
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
217
|
+
tags?: string[];
|
|
197
218
|
component: React.FC<NoInfer<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>>;
|
|
198
219
|
head?: (
|
|
199
220
|
ctx: NoInfer<ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData>
|
|
@@ -205,6 +226,7 @@ export interface Route<TParentData extends Record<string, unknown>, TParams, TQu
|
|
|
205
226
|
mode?: "ssr" | "ssg" | "isr";
|
|
206
227
|
revalidate?: number;
|
|
207
228
|
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
229
|
+
tags?: string[];
|
|
208
230
|
component: React.FC<TParentData & ComponentProps<TParams, TQuery>>;
|
|
209
231
|
head?: (ctx: ComponentProps<TParams, TQuery> & TParentData) => HeadOptions;
|
|
210
232
|
}): PageResult<TParentData, TParams, TQuery, {}>;
|
|
@@ -216,6 +238,7 @@ export interface Route<TParentData extends Record<string, unknown>, TParams, TQu
|
|
|
216
238
|
/** Branded ref for type inference when used as a parent. */
|
|
217
239
|
ref: RouteRef<TParentData, TParams, TQuery>;
|
|
218
240
|
revalidate?: number;
|
|
241
|
+
tags?: string[];
|
|
219
242
|
}
|
|
220
243
|
|
|
221
244
|
export function createRoute<
|
|
@@ -229,6 +252,7 @@ export function createRoute<
|
|
|
229
252
|
revalidate?: number;
|
|
230
253
|
params?: TParamsSchema;
|
|
231
254
|
query?: TQuerySchema;
|
|
255
|
+
tags?: string[];
|
|
232
256
|
loader?: (
|
|
233
257
|
ctx: RouteContext<
|
|
234
258
|
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
|
|
@@ -276,3 +300,56 @@ export type InferProps<T> = T extends {
|
|
|
276
300
|
: T extends Route<infer D, infer P, infer Q>
|
|
277
301
|
? D & { children: React.ReactNode } & ComponentProps<P, Q>
|
|
278
302
|
: never;
|
|
303
|
+
|
|
304
|
+
// ── Deferred data ──────────────────────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Brand key that marks an object as deferred. Using a `const` string avoids
|
|
308
|
+
* a `unique symbol` that would complicate cross-module inference.
|
|
309
|
+
*/
|
|
310
|
+
const DEFERRED_BRAND = "__isDeferred" as const;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* A loader return value that contains a mix of synchronous scalar fields and
|
|
314
|
+
* lazy `Promise<T>` fields. Scalar fields are serialised into the initial HTML
|
|
315
|
+
* shell; Promise fields are streamed as late `<script>` resolution chunks.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* loader: () => defer({
|
|
319
|
+
* title: "My Board", // synchronous — available immediately
|
|
320
|
+
* stats: fetchStats(), // Promise — streamed when it resolves
|
|
321
|
+
* })
|
|
322
|
+
*/
|
|
323
|
+
export type DeferredData<T extends Record<string, unknown>> = T & {
|
|
324
|
+
readonly [DEFERRED_BRAND]: true;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Wraps loader data so that Promise-valued fields are streamed lazily while
|
|
329
|
+
* scalar fields are embedded in the initial HTML shell immediately.
|
|
330
|
+
*
|
|
331
|
+
* Use in any loader — page (`route.page({ loader })`) or route/layout
|
|
332
|
+
* (`createRoute({ loader })`). Promise-valued fields are streamed lazily;
|
|
333
|
+
* scalar fields are embedded in the initial HTML shell.
|
|
334
|
+
*/
|
|
335
|
+
export function defer<T extends Record<string, unknown>>(data: T): DeferredData<T> {
|
|
336
|
+
if (Object.hasOwn(data, DEFERRED_BRAND)) {
|
|
337
|
+
throw new Error(
|
|
338
|
+
`[furin] defer() received an object with a reserved key "${DEFERRED_BRAND}". Rename this field to avoid conflicts with the deferred-data runtime.`
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return { ...data, [DEFERRED_BRAND]: true } as DeferredData<T>;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Type guard for DeferredData. Used by the render pipeline to distinguish a
|
|
346
|
+
* plain loader return from a deferred one.
|
|
347
|
+
*/
|
|
348
|
+
export function isDeferred(v: unknown): v is DeferredData<Record<string, unknown>> {
|
|
349
|
+
return (
|
|
350
|
+
typeof v === "object" &&
|
|
351
|
+
v !== null &&
|
|
352
|
+
Object.hasOwn(v, DEFERRED_BRAND) &&
|
|
353
|
+
(v as Record<typeof DEFERRED_BRAND, unknown>)[DEFERRED_BRAND] === true
|
|
354
|
+
);
|
|
355
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { BunPlugin } from "bun";
|
|
3
2
|
import { t } from "elysia";
|
|
4
3
|
|
|
5
4
|
export const BUILD_TARGETS = ["bun", "node", "vercel", "cloudflare", "static"] as const;
|
|
@@ -39,6 +38,13 @@ export const configSchema = t.Object({
|
|
|
39
38
|
rootDir: t.Optional(t.String()),
|
|
40
39
|
pagesDir: t.Optional(t.String()),
|
|
41
40
|
serverEntry: t.Optional(t.String()),
|
|
41
|
+
/**
|
|
42
|
+
* Initialize the browser HTTP log drain in the hydration entry. Off by
|
|
43
|
+
* default — enabling it adds `evlog/http` drain setup and points browser
|
|
44
|
+
* events at `/_furin/ingest`. Server-side logging is unaffected (configured
|
|
45
|
+
* via the `furin({ logger })` plugin option).
|
|
46
|
+
*/
|
|
47
|
+
clientLogging: t.Optional(t.Boolean()),
|
|
42
48
|
targets: t.Optional(t.Array(buildTargetSchema)),
|
|
43
49
|
bun: t.Optional(
|
|
44
50
|
t.Object({
|
|
@@ -55,8 +61,10 @@ export const configSchema = t.Object({
|
|
|
55
61
|
// plugins omitted : TypeBox can't validate Bun.BunPlugin[] (functions)
|
|
56
62
|
});
|
|
57
63
|
|
|
64
|
+
export type FurinPlugin = BunPlugin & { buildOnly?: boolean };
|
|
65
|
+
|
|
58
66
|
export type FurinConfig = (typeof configSchema)["static"] & {
|
|
59
|
-
plugins?:
|
|
67
|
+
plugins?: FurinPlugin[];
|
|
60
68
|
static?: StaticExportConfig;
|
|
61
69
|
};
|
|
62
70
|
|
package/src/env.d.ts
CHANGED
package/src/furin.ts
CHANGED
|
@@ -5,18 +5,32 @@ 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 type { EmbeddedAppData } from "./internal.ts";
|
|
9
|
-
import { getCompileContext } from "./internal.ts";
|
|
10
8
|
import {
|
|
11
9
|
_runWithRequestInvalidationScope,
|
|
12
10
|
consumePendingInvalidations,
|
|
13
11
|
getBuildId,
|
|
14
12
|
setBuildId,
|
|
15
|
-
} from "./
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
13
|
+
} from "./server/cache/invalidation.ts";
|
|
14
|
+
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
15
|
+
import type { CompileContext, EmbeddedAppData } from "./server/internal.ts";
|
|
16
|
+
import { getCompileContext } from "./server/internal.ts";
|
|
17
|
+
import { renderRootNotFound, warmSSGCache } from "./server/render/index.ts";
|
|
18
|
+
import {
|
|
19
|
+
setProductionTemplateContent,
|
|
20
|
+
setProductionTemplatePath,
|
|
21
|
+
} from "./server/render/template.ts";
|
|
22
|
+
import {
|
|
23
|
+
createDataEndpoint,
|
|
24
|
+
createRoutePlugin,
|
|
25
|
+
createSearchRouteMetadata,
|
|
26
|
+
loadProdRoutes,
|
|
27
|
+
} from "./server/router/index.ts";
|
|
28
|
+
import { IS_DEV } from "./server/runtime-env.ts";
|
|
29
|
+
|
|
30
|
+
// biome-ignore lint/suspicious/noEmptyInterface: intentionally augmentable via furin-env.d.ts
|
|
31
|
+
export interface FurinCacheTags {}
|
|
32
|
+
|
|
33
|
+
export type CacheTag = keyof FurinCacheTags extends never ? string : keyof FurinCacheTags;
|
|
20
34
|
|
|
21
35
|
function resolveClientDirFromArgv(): string {
|
|
22
36
|
return (
|
|
@@ -134,6 +148,32 @@ async function setupProdTemplate(
|
|
|
134
148
|
setProductionTemplatePath(templatePath);
|
|
135
149
|
}
|
|
136
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Registers a HigherOrderFunction on the Elysia instance so that every request
|
|
153
|
+
* runs inside a fresh `AsyncLocalStorage` scope. This isolates
|
|
154
|
+
* `pendingInvalidations` per request, preventing concurrent requests from
|
|
155
|
+
* stealing each other's `revalidatePath()` calls.
|
|
156
|
+
*
|
|
157
|
+
* Uses `app.wrap()` (Elysia HigherOrderFunction) instead of mutating
|
|
158
|
+
* `app.handle`, because handle mutations are lost when the Furin plugin is
|
|
159
|
+
* `.use()`-d by a parent Elysia instance. HigherOrderFunctions survive the
|
|
160
|
+
* plugin merge and wrap the entire composed `map` handler.
|
|
161
|
+
*/
|
|
162
|
+
function wrapWithRequestScope(app: AnyElysia): Elysia {
|
|
163
|
+
return app.wrap(
|
|
164
|
+
(handler, _request) => (ctx: unknown) => _runWithRequestInvalidationScope(() => handler(ctx))
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
169
|
+
if (!ctx.ssgCache) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
for (const [path, entry] of Object.entries(ctx.ssgCache)) {
|
|
173
|
+
setSSGCache(path, entry);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
137
177
|
/**
|
|
138
178
|
* Main Furin plugin.
|
|
139
179
|
*
|
|
@@ -148,25 +188,20 @@ async function setupProdTemplate(
|
|
|
148
188
|
* .listen(3000)
|
|
149
189
|
* ```
|
|
150
190
|
*/
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Wraps an Elysia app's `handle` method so that every request runs inside a
|
|
154
|
-
* fresh `AsyncLocalStorage` scope. This isolates `pendingInvalidations` per
|
|
155
|
-
* request, preventing concurrent requests from stealing each other's
|
|
156
|
-
* `revalidatePath()` calls.
|
|
157
|
-
*/
|
|
158
|
-
function wrapWithRequestScope(app: AnyElysia): Elysia {
|
|
159
|
-
const original = app.handle.bind(app);
|
|
160
|
-
app.handle = (request: Request) => _runWithRequestInvalidationScope(() => original(request));
|
|
161
|
-
return app;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
191
|
export async function furin({
|
|
165
192
|
pagesDir,
|
|
166
193
|
logger,
|
|
194
|
+
clientLogging,
|
|
167
195
|
}: {
|
|
168
196
|
pagesDir?: string;
|
|
169
197
|
logger?: EvlogElysiaOptions;
|
|
198
|
+
/**
|
|
199
|
+
* Initialize the browser HTTP log drain in the hydration entry. Off by
|
|
200
|
+
* default — enabling it adds `evlog/http` drain setup and points browser
|
|
201
|
+
* events at `/_furin/ingest`. Server-side logging is configured via `logger`
|
|
202
|
+
* and unaffected.
|
|
203
|
+
*/
|
|
204
|
+
clientLogging?: boolean;
|
|
170
205
|
}) {
|
|
171
206
|
const { exclude: userExclude, ...evlogOptions } = logger ?? {};
|
|
172
207
|
initLogger({ env: { service: "furin" } });
|
|
@@ -179,8 +214,13 @@ export async function furin({
|
|
|
179
214
|
"/_client/**",
|
|
180
215
|
"/public/**",
|
|
181
216
|
"/favicon.ico",
|
|
182
|
-
"/_furin/!(ingest)",
|
|
183
217
|
"/_bun_hmr_entry/**",
|
|
218
|
+
// Note: /_furin/data is logged with the *logical* path rewritten by
|
|
219
|
+
// createDataEndpoint via useLogger().set({ path }), so SPA navigations
|
|
220
|
+
// appear as "GET /board/123 200" — same shape as a normal SSR nav.
|
|
221
|
+
// /_furin/ingest is kept loggable so browser-side events show up.
|
|
222
|
+
// evlog's `matchesPattern` only supports `*`, `**`, `?` — extglob
|
|
223
|
+
// like `!(...)` matches nothing, so don't add patterns relying on it.
|
|
184
224
|
...(userExclude ?? []),
|
|
185
225
|
],
|
|
186
226
|
})
|
|
@@ -207,6 +247,7 @@ export async function furin({
|
|
|
207
247
|
__proto__,
|
|
208
248
|
constructor: _ctor,
|
|
209
249
|
prototype,
|
|
250
|
+
environment: _browserEnv,
|
|
210
251
|
...safeEvent
|
|
211
252
|
} = event as Record<string, unknown>;
|
|
212
253
|
log.set({ ...safeEvent, service: "furin:browser" });
|
|
@@ -229,16 +270,25 @@ export async function furin({
|
|
|
229
270
|
if (IS_DEV) {
|
|
230
271
|
const furinDir = resolve(cwd, ".furin");
|
|
231
272
|
// Lazy import — build pipeline has native deps not available in compiled binaries
|
|
232
|
-
const { registerDevPagePlugin } = await import("./dev-page-plugin.ts");
|
|
273
|
+
const { registerDevPagePlugin } = await import("./server/dev-page-plugin.ts");
|
|
233
274
|
registerDevPagePlugin();
|
|
234
275
|
|
|
235
|
-
const {
|
|
276
|
+
const { createDevInspectorPlugin } = await import("./server/dev-inspector.ts");
|
|
277
|
+
|
|
278
|
+
const { scanPages } = await import("./server/router/index.ts");
|
|
236
279
|
const { root, routes } = await scanPages(resolvedPagesDir);
|
|
280
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
237
281
|
|
|
238
282
|
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
239
283
|
writeDevFiles(
|
|
240
284
|
routes,
|
|
241
|
-
{
|
|
285
|
+
{
|
|
286
|
+
outDir: furinDir,
|
|
287
|
+
rootLayout: root.path,
|
|
288
|
+
basePath: "",
|
|
289
|
+
publicPath: "/_client/",
|
|
290
|
+
clientLogging: clientLogging ?? false,
|
|
291
|
+
},
|
|
242
292
|
cwd
|
|
243
293
|
);
|
|
244
294
|
|
|
@@ -247,6 +297,11 @@ export async function furin({
|
|
|
247
297
|
|
|
248
298
|
const devApp = new Elysia({ name: instanceName, seed: resolvedPagesDir })
|
|
249
299
|
.use(loggerPlugin)
|
|
300
|
+
.onError({ as: "global" }, async ({ code, request }) => {
|
|
301
|
+
if (code === "NOT_FOUND") {
|
|
302
|
+
return await renderRootNotFound(root, request);
|
|
303
|
+
}
|
|
304
|
+
})
|
|
250
305
|
.onAfterHandle({ as: "global" }, ({ set }) => {
|
|
251
306
|
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
252
307
|
const pending = consumePendingInvalidations();
|
|
@@ -264,9 +319,11 @@ export async function furin({
|
|
|
264
319
|
? file(join(publicDir, "favicon.ico"))
|
|
265
320
|
: () => new Response(null, { status: 404 })
|
|
266
321
|
)
|
|
322
|
+
.use(createDevInspectorPlugin())
|
|
323
|
+
.use(createDataEndpoint(routes))
|
|
267
324
|
.use((app) => {
|
|
268
325
|
for (const route of routes) {
|
|
269
|
-
app.use(createRoutePlugin(route, root));
|
|
326
|
+
app.use(createRoutePlugin(route, root, undefined, searchRoutes));
|
|
270
327
|
}
|
|
271
328
|
return app;
|
|
272
329
|
});
|
|
@@ -278,8 +335,10 @@ export async function furin({
|
|
|
278
335
|
throw new Error("[furin] No pre-built assets found. Run `bunx furin build` first.");
|
|
279
336
|
}
|
|
280
337
|
const { root, routes } = loadProdRoutes(ctx);
|
|
338
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
281
339
|
const prodBuildId = ctx.buildId ?? "";
|
|
282
340
|
setBuildId(prodBuildId);
|
|
341
|
+
hydrateSSGCacheFromCompileContext(ctx);
|
|
283
342
|
|
|
284
343
|
const embedded = ctx?.embedded;
|
|
285
344
|
const clientDir = embedded ? "" : resolveClientDirFromArgv();
|
|
@@ -287,6 +346,11 @@ export async function furin({
|
|
|
287
346
|
|
|
288
347
|
const prodApp = new Elysia({ name: instanceName, seed: resolvedPagesDir })
|
|
289
348
|
.use(loggerPlugin)
|
|
349
|
+
.onError({ as: "global" }, async ({ code, request }) => {
|
|
350
|
+
if (code === "NOT_FOUND") {
|
|
351
|
+
return await renderRootNotFound(root, request);
|
|
352
|
+
}
|
|
353
|
+
})
|
|
290
354
|
.onAfterHandle({ as: "global" }, ({ path, set }) => {
|
|
291
355
|
// Content-hashed client assets are permanently cacheable — browsers never need to
|
|
292
356
|
// revalidate them because any change produces a new filename.
|
|
@@ -305,8 +369,11 @@ export async function furin({
|
|
|
305
369
|
}
|
|
306
370
|
})
|
|
307
371
|
.onStart(async ({ server }) => {
|
|
372
|
+
if (ctx.ssgCache) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
308
375
|
const origin = server?.url?.origin ?? "http://localhost:3000";
|
|
309
|
-
await warmSSGCache(routes, root, origin);
|
|
376
|
+
await warmSSGCache(routes, root, origin, searchRoutes);
|
|
310
377
|
})
|
|
311
378
|
.use(
|
|
312
379
|
await (async () => {
|
|
@@ -345,15 +412,33 @@ export async function furin({
|
|
|
345
412
|
return app;
|
|
346
413
|
})()
|
|
347
414
|
)
|
|
415
|
+
.use(createDataEndpoint(routes))
|
|
348
416
|
.use((app) => {
|
|
349
417
|
for (const route of routes) {
|
|
350
|
-
app.use(createRoutePlugin(route, root, prodBuildId));
|
|
418
|
+
app.use(createRoutePlugin(route, root, prodBuildId, searchRoutes));
|
|
351
419
|
}
|
|
352
420
|
return app;
|
|
353
421
|
});
|
|
354
422
|
return wrapWithRequestScope(prodApp);
|
|
355
423
|
}
|
|
356
424
|
|
|
425
|
+
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
426
|
+
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
357
427
|
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
358
|
-
// biome-ignore lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
359
|
-
export {
|
|
428
|
+
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
429
|
+
export type { DeferredData, RuntimePage, RuntimeRoute } from "./client.ts";
|
|
430
|
+
export { defer, isDeferred } from "./client.ts";
|
|
431
|
+
export type { InvalidationInput, InvalidationRule } from "./server/auto-invalidate/index.ts";
|
|
432
|
+
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
433
|
+
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
434
|
+
export { buildElement, buildErrorElement, renderRootNotFound } from "./server/render/index.ts";
|
|
435
|
+
export type { ResolvedRoute, SegmentBoundary } from "./server/router/index.ts";
|
|
436
|
+
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
437
|
+
export type { ErrorComponent, ErrorProps } from "./shared/error.ts";
|
|
438
|
+
export type {
|
|
439
|
+
NotFoundComponent,
|
|
440
|
+
NotFoundOptions,
|
|
441
|
+
NotFoundProps,
|
|
442
|
+
} from "./shared/not-found.ts";
|
|
443
|
+
export { isNotFoundError, notFound } from "./shared/not-found.ts";
|
|
444
|
+
// biome-ignore-end lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
package/src/plugin/index.ts
CHANGED
|
@@ -4,7 +4,6 @@ const ELYSIA_FILTER = /^elysia$/;
|
|
|
4
4
|
const BUN_BUILTIN_FILTER = /^bun:/;
|
|
5
5
|
const ANY_FILTER = /.*/;
|
|
6
6
|
const TS_FILE_FILTER = /\.(tsx|ts)$/;
|
|
7
|
-
const REACT_IMPORT_FILTER = /import\s+React\b/;
|
|
8
7
|
|
|
9
8
|
// Minimal browser stub for elysia — `t` is only used for schema definitions
|
|
10
9
|
// in params/query, which the client never validates at runtime.
|
|
@@ -62,13 +61,13 @@ const plugin: Bun.BunPlugin = {
|
|
|
62
61
|
|
|
63
62
|
try {
|
|
64
63
|
const result = transformForClient(source, args.path);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
64
|
+
// Output is TS/TSX (yuku parses directly, no pre-transpile). Bun's
|
|
65
|
+
// bundler picks the loader from the file extension and applies the
|
|
66
|
+
// project tsconfig — including the JSX automatic runtime.
|
|
67
|
+
return {
|
|
68
|
+
contents: result.code,
|
|
69
|
+
loader: args.path.endsWith(".tsx") ? "tsx" : "ts",
|
|
70
|
+
};
|
|
72
71
|
} catch (err) {
|
|
73
72
|
console.error(`[furin] strip-plugin transform error for ${args.path}:`, err);
|
|
74
73
|
return;
|