@teyik0/furin 0.1.0-alpha.10
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/bun.d.ts +3 -0
- package/dist/adapter/static.d.ts +3 -0
- package/dist/build/client.d.ts +25 -0
- package/dist/build/compile-entry.d.ts +35 -0
- package/dist/build/entry-template.d.ts +26 -0
- package/dist/build/hydrate.d.ts +24 -0
- package/dist/build/index.d.ts +7 -0
- package/dist/build/index.js +4716 -0
- package/dist/build/route-types.d.ts +21 -0
- package/dist/build/scan-server.d.ts +8 -0
- package/dist/build/server-routes-entry.d.ts +35 -0
- package/dist/build/shared.d.ts +11 -0
- package/dist/build/types.d.ts +76 -0
- package/dist/cli/config.d.ts +9 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +4751 -0
- package/dist/client.d.ts +195 -0
- package/dist/client.js +20 -0
- package/dist/config.d.ts +50 -0
- package/dist/config.js +28 -0
- package/dist/context-logger.d.ts +28 -0
- package/dist/dev-page-plugin.d.ts +109 -0
- package/dist/error.d.ts +23 -0
- package/dist/furin.d.ts +52 -0
- package/dist/furin.js +3948 -0
- package/dist/internal.d.ts +33 -0
- package/dist/link.d.ts +454 -0
- package/dist/link.js +1790 -0
- package/dist/not-found.d.ts +19 -0
- package/dist/plugin/index.d.ts +20 -0
- package/dist/plugin/index.js +1408 -0
- package/dist/plugin/transform-client.d.ts +9 -0
- package/dist/render/assemble.d.ts +15 -0
- package/dist/render/boundaries.d.ts +79 -0
- package/dist/render/cache.d.ts +96 -0
- package/dist/render/default-screens.d.ts +11 -0
- package/dist/render/digest.d.ts +16 -0
- package/dist/render/element.d.ts +8 -0
- package/dist/render/index.d.ts +53 -0
- package/dist/render/loaders.d.ts +21 -0
- package/dist/render/shell.d.ts +29 -0
- package/dist/render/template.d.ts +6 -0
- package/dist/router.d.ts +96 -0
- package/dist/router.js +2693 -0
- package/dist/runtime-env.d.ts +3 -0
- package/dist/utils.d.ts +6 -0
- package/package.json +77 -0
- package/src/adapter/README.md +13 -0
- package/src/adapter/bun.ts +239 -0
- package/src/adapter/static.ts +341 -0
- package/src/build/client.ts +153 -0
- package/src/build/compile-entry.ts +129 -0
- package/src/build/entry-template.ts +126 -0
- package/src/build/hydrate.ts +250 -0
- package/src/build/index.ts +151 -0
- package/src/build/route-types.ts +89 -0
- package/src/build/scan-server.ts +110 -0
- package/src/build/server-routes-entry.ts +55 -0
- package/src/build/shared.ts +78 -0
- package/src/build/types.ts +85 -0
- package/src/cli/config.ts +68 -0
- package/src/cli/index.ts +115 -0
- package/src/client.ts +278 -0
- package/src/config.ts +66 -0
- package/src/context-logger.ts +74 -0
- package/src/dev-page-plugin.ts +434 -0
- package/src/env.d.ts +49 -0
- package/src/error.ts +32 -0
- package/src/furin.ts +383 -0
- package/src/internal.ts +51 -0
- package/src/link.tsx +1578 -0
- package/src/not-found.ts +40 -0
- package/src/plugin/index.ts +80 -0
- package/src/plugin/transform-client.ts +372 -0
- package/src/render/assemble.ts +66 -0
- package/src/render/boundaries.tsx +176 -0
- package/src/render/cache.ts +251 -0
- package/src/render/default-screens.tsx +299 -0
- package/src/render/digest.ts +71 -0
- package/src/render/element.tsx +119 -0
- package/src/render/index.ts +867 -0
- package/src/render/loaders.ts +127 -0
- package/src/render/shell.ts +187 -0
- package/src/render/template.ts +55 -0
- package/src/router.ts +779 -0
- package/src/runtime-env.ts +6 -0
- package/src/utils.ts +68 -0
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { buildApp } from "../build/index.ts";
|
|
6
|
+
import { BUILD_TARGETS, type BuildTarget } from "../config.ts";
|
|
7
|
+
import { loadCliConfig } from "./config.ts";
|
|
8
|
+
|
|
9
|
+
const argv = process.argv.slice(2);
|
|
10
|
+
const command = argv[0];
|
|
11
|
+
|
|
12
|
+
function log(msg: string): void {
|
|
13
|
+
console.log(`\x1b[32m◆\x1b[0m ${msg}`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function bail(msg: string): never {
|
|
17
|
+
console.error(`\x1b[31m✗\x1b[0m ${msg}`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function resolveCompileMode(
|
|
22
|
+
flag: string | boolean | undefined,
|
|
23
|
+
configCompile: "server" | "embed" | undefined
|
|
24
|
+
): "server" | "embed" | undefined {
|
|
25
|
+
if (flag === "embed") {
|
|
26
|
+
return "embed";
|
|
27
|
+
}
|
|
28
|
+
if (flag === true || flag === "server") {
|
|
29
|
+
return "server";
|
|
30
|
+
}
|
|
31
|
+
if (flag !== undefined && flag !== false) {
|
|
32
|
+
bail(`Invalid compile mode "${flag}". Valid: --compile server or --compile embed`);
|
|
33
|
+
}
|
|
34
|
+
return configCompile;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (command === "build") {
|
|
38
|
+
const { values: rawValues } = parseArgs({
|
|
39
|
+
args: argv.slice(1),
|
|
40
|
+
options: {
|
|
41
|
+
target: { type: "string" },
|
|
42
|
+
pagesDir: { type: "string" },
|
|
43
|
+
config: { type: "string" },
|
|
44
|
+
},
|
|
45
|
+
strict: false,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const values = rawValues as {
|
|
49
|
+
target?: string;
|
|
50
|
+
pagesDir?: string;
|
|
51
|
+
config?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// --compile has an optional value: absent → undefined, present alone → true, present with "embed" → "embed"
|
|
55
|
+
const buildArgv = argv.slice(1);
|
|
56
|
+
const compileIdx = buildArgv.indexOf("--compile");
|
|
57
|
+
let compileFlag: string | boolean | undefined;
|
|
58
|
+
if (compileIdx < 0) {
|
|
59
|
+
compileFlag = undefined;
|
|
60
|
+
} else {
|
|
61
|
+
const next = buildArgv[compileIdx + 1];
|
|
62
|
+
compileFlag = next && !next.startsWith("-") ? next : true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const target = values.target ?? "bun";
|
|
66
|
+
|
|
67
|
+
if (target !== "all" && !(BUILD_TARGETS as readonly string[]).includes(target)) {
|
|
68
|
+
bail(`Unsupported build target "${target}". Valid: ${BUILD_TARGETS.join(", ")}, all`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const config = await loadCliConfig(process.cwd(), values.config);
|
|
72
|
+
|
|
73
|
+
const isStaticTarget = target === "static";
|
|
74
|
+
|
|
75
|
+
const resolvedServerEntry = isStaticTarget
|
|
76
|
+
? undefined
|
|
77
|
+
: (() => {
|
|
78
|
+
const entry = resolve(config.rootDir, config.serverEntry ?? "src/server.ts");
|
|
79
|
+
if (!existsSync(entry)) {
|
|
80
|
+
const expected = config.serverEntry ?? "src/server.ts";
|
|
81
|
+
throw new Error(`[furin] Entrypoint ${expected} not found`);
|
|
82
|
+
}
|
|
83
|
+
return entry;
|
|
84
|
+
})();
|
|
85
|
+
|
|
86
|
+
log(`Building Furin for ${target}…`);
|
|
87
|
+
|
|
88
|
+
const result = await buildApp({
|
|
89
|
+
target: target as BuildTarget | "all",
|
|
90
|
+
compile: resolveCompileMode(compileFlag, config.bun?.compile),
|
|
91
|
+
rootDir: config.rootDir,
|
|
92
|
+
pagesDir: values.pagesDir ?? config.pagesDir,
|
|
93
|
+
serverEntry: resolvedServerEntry,
|
|
94
|
+
plugins: config.plugins,
|
|
95
|
+
staticConfig: config.static,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const built = Object.keys(result.targets).join(", ") || "none";
|
|
99
|
+
log(`Done: ${built} → .furin/build`);
|
|
100
|
+
} else if (!command || command === "help") {
|
|
101
|
+
console.log(
|
|
102
|
+
`Furin CLI
|
|
103
|
+
|
|
104
|
+
USAGE furin build [options]
|
|
105
|
+
|
|
106
|
+
OPTIONS
|
|
107
|
+
--target ${BUILD_TARGETS.join(" | ")} | all (default: bun)
|
|
108
|
+
--pagesDir Pages directory
|
|
109
|
+
--config Config file path
|
|
110
|
+
--compile server | embed Compile to binary: "server" keeps client on disk, "embed" is self-contained
|
|
111
|
+
`
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
bail(`Unknown command "${command}". Run "furin help" for usage.`);
|
|
115
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/*
|
|
2
|
+
biome-ignore-all lint/complexity/noBannedTypes: The fundamental problem is that
|
|
3
|
+
`Record<string, unknown>` requires an index signature, and any type without one
|
|
4
|
+
(like `{}`, `object`, or a named interface) won't satisfy it. But `{}` is the only type that:
|
|
5
|
+
1. Satisfies `Record<string, unknown>` as a generic default (TS special-cases `{}`)
|
|
6
|
+
2. Doesn't have an index signature (so unknown prop access errors)
|
|
7
|
+
3. Is transparent in intersections(`{} & T = T`)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Cookie, StatusMap } from "elysia";
|
|
11
|
+
import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
|
|
12
|
+
import type { RequestLogger } from "evlog";
|
|
13
|
+
|
|
14
|
+
declare const UNSET: unique symbol;
|
|
15
|
+
type Unset = typeof UNSET;
|
|
16
|
+
|
|
17
|
+
// Expands interfaces into plain mapped types so they satisfy Record<string, unknown>.
|
|
18
|
+
// Interfaces lack an implicit index signature; mapped types have one.
|
|
19
|
+
type ToRecord<T> = { [K in keyof T]: T[K] };
|
|
20
|
+
|
|
21
|
+
type ResolvedSchema<T> = [T] extends [Unset]
|
|
22
|
+
? Unset
|
|
23
|
+
: T extends AnySchema
|
|
24
|
+
? UnwrapSchema<T>
|
|
25
|
+
: Unset;
|
|
26
|
+
|
|
27
|
+
type MergeSchema<TParent, TOwn> = [TParent] extends [Unset]
|
|
28
|
+
? TOwn
|
|
29
|
+
: [TOwn] extends [Unset]
|
|
30
|
+
? TParent
|
|
31
|
+
: TParent & TOwn;
|
|
32
|
+
|
|
33
|
+
type NormalizeUnset<T> = [T] extends [Unset] ? {} : T;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Transforms each key of a parent-data record into an individual `Promise<T>`.
|
|
37
|
+
* This is what child loaders see for inherited fields — direct values from
|
|
38
|
+
* `RouteContext` (request, params, set, …) remain unchanged.
|
|
39
|
+
*/
|
|
40
|
+
type PromisifyData<T extends Record<string, unknown>> = {
|
|
41
|
+
[K in keyof T]: Promise<T[K]>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export interface RouteContext<TParams = {}, TQuery = {}> {
|
|
45
|
+
cookie: Record<string, Cookie<unknown>>;
|
|
46
|
+
headers: Record<string, string | undefined>;
|
|
47
|
+
/** Request-scoped logger. Call `log.set({})` to attach fields to the wide event for this render. */
|
|
48
|
+
log: RequestLogger;
|
|
49
|
+
params: NormalizeUnset<TParams>;
|
|
50
|
+
path: string;
|
|
51
|
+
query: NormalizeUnset<TQuery>;
|
|
52
|
+
redirect: (url: string, status?: 301 | 302 | 303 | 307 | 308) => Response;
|
|
53
|
+
request: Request;
|
|
54
|
+
set: {
|
|
55
|
+
headers: HTTPHeaders;
|
|
56
|
+
status?: number | keyof StatusMap;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ComponentProps<TParams = {}, TQuery = {}> {
|
|
61
|
+
params: NormalizeUnset<TParams>;
|
|
62
|
+
path: string;
|
|
63
|
+
query: NormalizeUnset<TQuery>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type ResolveParent<T> =
|
|
67
|
+
T extends RouteRef<infer D, infer P, infer Q>
|
|
68
|
+
? { data: D; params: P; query: Q }
|
|
69
|
+
: { data: {}; params: Unset; query: Unset };
|
|
70
|
+
|
|
71
|
+
interface Resolved<TParentRef, TLoaderData, TParamsSchema = Unset, TQuerySchema = Unset> {
|
|
72
|
+
data: ToRecord<ResolveParent<TParentRef>["data"] & TLoaderData>;
|
|
73
|
+
params: MergeSchema<ResolveParent<TParentRef>["params"], ResolvedSchema<TParamsSchema>>;
|
|
74
|
+
query: MergeSchema<ResolveParent<TParentRef>["query"], ResolvedSchema<TQuerySchema>>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type MetaDescriptor =
|
|
78
|
+
| { charSet: "utf-8" }
|
|
79
|
+
| { title: string }
|
|
80
|
+
| { name: string; content: string }
|
|
81
|
+
| { property: string; content: string }
|
|
82
|
+
| { httpEquiv: string; content: string }
|
|
83
|
+
| { "script:ld+json": object }
|
|
84
|
+
| { tagName: "meta" | "link"; [name: string]: string | undefined };
|
|
85
|
+
|
|
86
|
+
export interface HeadOptions {
|
|
87
|
+
links?: Array<{ rel: string; href: string; [key: string]: string }>;
|
|
88
|
+
meta?: MetaDescriptor[];
|
|
89
|
+
/**
|
|
90
|
+
* Inline scripts injected into `<head>`.
|
|
91
|
+
*
|
|
92
|
+
* **Security warning:** `children` is injected as raw HTML — never pass
|
|
93
|
+
* user-controlled or loader-derived data here without sanitisation.
|
|
94
|
+
*/
|
|
95
|
+
scripts?: Array<{
|
|
96
|
+
src?: string;
|
|
97
|
+
type?: string;
|
|
98
|
+
children?: string;
|
|
99
|
+
[key: string]: string | undefined;
|
|
100
|
+
}>;
|
|
101
|
+
/**
|
|
102
|
+
* Inline styles injected into `<head>`.
|
|
103
|
+
*
|
|
104
|
+
* **Security warning:** `children` is injected as raw HTML — never pass
|
|
105
|
+
* user-controlled or loader-derived data here without sanitisation.
|
|
106
|
+
*/
|
|
107
|
+
styles?: Array<{ type?: string; children: string }>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Extracts the resolved return type from a loader function type.
|
|
111
|
+
// Using ReturnType + Awaited here means TLoader is inferred as the whole function
|
|
112
|
+
// first, and then we extract the data — so inference is order-independent.
|
|
113
|
+
type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown
|
|
114
|
+
? Awaited<ReturnType<TLoader>> extends object
|
|
115
|
+
? ToRecord<Awaited<ReturnType<TLoader>>>
|
|
116
|
+
: {}
|
|
117
|
+
: {};
|
|
118
|
+
|
|
119
|
+
export interface PageConfig<
|
|
120
|
+
TParentData extends Record<string, unknown>,
|
|
121
|
+
TParams,
|
|
122
|
+
TQuery,
|
|
123
|
+
TPageLoaderData extends object = {},
|
|
124
|
+
> {
|
|
125
|
+
component: React.FC<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
|
|
126
|
+
head?: (ctx: ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData) => HeadOptions;
|
|
127
|
+
loader?: (
|
|
128
|
+
ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
|
|
129
|
+
) => Promise<TPageLoaderData> | TPageLoaderData;
|
|
130
|
+
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface RuntimeRoute {
|
|
134
|
+
__type: "FURIN_ROUTE";
|
|
135
|
+
layout?: React.FC<Record<string, unknown> & { children: React.ReactNode }>;
|
|
136
|
+
loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
137
|
+
mode?: "ssr" | "ssg" | "isr";
|
|
138
|
+
params?: unknown;
|
|
139
|
+
parent?: RuntimeRoute;
|
|
140
|
+
query?: unknown;
|
|
141
|
+
revalidate?: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface RuntimePage {
|
|
145
|
+
__type: "FURIN_PAGE";
|
|
146
|
+
_route: RuntimeRoute;
|
|
147
|
+
component: React.FC<Record<string, unknown>>;
|
|
148
|
+
head?(ctx: Record<string, unknown>): HeadOptions;
|
|
149
|
+
loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
150
|
+
staticParams?(): Promise<Record<string, string>[]> | Record<string, string>[];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface RouteRef<
|
|
154
|
+
TData extends Record<string, unknown> = Record<string, unknown>,
|
|
155
|
+
TParams = unknown,
|
|
156
|
+
TQuery = unknown,
|
|
157
|
+
> {
|
|
158
|
+
readonly __brand: "FURIN_ROUTE_REF";
|
|
159
|
+
readonly __phantom: { data: TData; params: TParams; query: TQuery };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface PageResult<
|
|
163
|
+
TData extends Record<string, unknown>,
|
|
164
|
+
TParams,
|
|
165
|
+
TQuery,
|
|
166
|
+
TPageLoaderData extends object,
|
|
167
|
+
> {
|
|
168
|
+
__type: "FURIN_PAGE";
|
|
169
|
+
_route: Route<TData, TParams, TQuery>;
|
|
170
|
+
component: React.FC<TData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
|
|
171
|
+
head?: (ctx: ComponentProps<TParams, TQuery> & TData & TPageLoaderData) => HeadOptions;
|
|
172
|
+
loader?: (
|
|
173
|
+
ctx: RouteContext<TParams, TQuery> & PromisifyData<TData>
|
|
174
|
+
) => Promise<TPageLoaderData> | TPageLoaderData;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface Route<TParentData extends Record<string, unknown>, TParams, TQuery> {
|
|
178
|
+
__type: "FURIN_ROUTE";
|
|
179
|
+
layout?: React.FC<TParentData & { children: React.ReactNode } & ComponentProps<TParams, TQuery>>;
|
|
180
|
+
loader?(
|
|
181
|
+
ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
|
|
182
|
+
): Promise<TParentData> | TParentData;
|
|
183
|
+
mode?: "ssr" | "ssg" | "isr";
|
|
184
|
+
|
|
185
|
+
// Overload 1 — loader present (required).
|
|
186
|
+
// Two type params: TLoader is inferred solely from the `loader` position; TPageLoaderData
|
|
187
|
+
// has no inference sites (all NoInfer) so TypeScript applies its default AFTER TLoader is
|
|
188
|
+
// resolved — making declaration order of head/component irrelevant.
|
|
189
|
+
page<
|
|
190
|
+
TLoader extends (ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>) => unknown,
|
|
191
|
+
TPageLoaderData extends object = ExtractLoaderReturn<TLoader>,
|
|
192
|
+
>(config: {
|
|
193
|
+
loader: TLoader;
|
|
194
|
+
mode?: "ssr" | "ssg" | "isr";
|
|
195
|
+
revalidate?: number;
|
|
196
|
+
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
197
|
+
component: React.FC<NoInfer<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>>;
|
|
198
|
+
head?: (
|
|
199
|
+
ctx: NoInfer<ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData>
|
|
200
|
+
) => HeadOptions;
|
|
201
|
+
}): PageResult<TParentData, TParams, TQuery, TPageLoaderData>;
|
|
202
|
+
|
|
203
|
+
// Overload 2 — no loader.
|
|
204
|
+
page(config: {
|
|
205
|
+
mode?: "ssr" | "ssg" | "isr";
|
|
206
|
+
revalidate?: number;
|
|
207
|
+
staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
|
|
208
|
+
component: React.FC<TParentData & ComponentProps<TParams, TQuery>>;
|
|
209
|
+
head?: (ctx: ComponentProps<TParams, TQuery> & TParentData) => HeadOptions;
|
|
210
|
+
}): PageResult<TParentData, TParams, TQuery, {}>;
|
|
211
|
+
|
|
212
|
+
params?: unknown;
|
|
213
|
+
parent?: RuntimeRoute;
|
|
214
|
+
query?: unknown;
|
|
215
|
+
|
|
216
|
+
/** Branded ref for type inference when used as a parent. */
|
|
217
|
+
ref: RouteRef<TParentData, TParams, TQuery>;
|
|
218
|
+
revalidate?: number;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function createRoute<
|
|
222
|
+
TParentRef extends RouteRef | undefined = undefined,
|
|
223
|
+
TParamsSchema extends AnySchema | Unset = Unset,
|
|
224
|
+
TQuerySchema extends AnySchema | Unset = Unset,
|
|
225
|
+
TLoaderData extends object = {},
|
|
226
|
+
>(config?: {
|
|
227
|
+
parent?: { ref: TParentRef } & { __type: "FURIN_ROUTE" };
|
|
228
|
+
mode?: "ssr" | "ssg" | "isr";
|
|
229
|
+
revalidate?: number;
|
|
230
|
+
params?: TParamsSchema;
|
|
231
|
+
query?: TQuerySchema;
|
|
232
|
+
loader?: (
|
|
233
|
+
ctx: RouteContext<
|
|
234
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
|
|
235
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
|
|
236
|
+
> &
|
|
237
|
+
PromisifyData<ResolveParent<TParentRef>["data"]>
|
|
238
|
+
) => Promise<TLoaderData> | TLoaderData;
|
|
239
|
+
layout?: React.FC<
|
|
240
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"] & {
|
|
241
|
+
children: React.ReactNode;
|
|
242
|
+
} & ComponentProps<
|
|
243
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
|
|
244
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
|
|
245
|
+
>
|
|
246
|
+
>;
|
|
247
|
+
}): Route<
|
|
248
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"],
|
|
249
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
|
|
250
|
+
Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
|
|
251
|
+
> {
|
|
252
|
+
type R = Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>;
|
|
253
|
+
|
|
254
|
+
const route = {
|
|
255
|
+
...config,
|
|
256
|
+
__type: "FURIN_ROUTE" as const,
|
|
257
|
+
ref: {} as RouteRef<R["data"], R["params"], R["query"]>,
|
|
258
|
+
|
|
259
|
+
// biome-ignore lint/suspicious/noExplicitAny: implementation signature for both overloads
|
|
260
|
+
page(pageConfig: any) {
|
|
261
|
+
return {
|
|
262
|
+
...pageConfig,
|
|
263
|
+
__type: "FURIN_PAGE" as const,
|
|
264
|
+
_route: route,
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
return route as Route<R["data"], R["params"], R["query"]>;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export type InferProps<T> = T extends {
|
|
272
|
+
__type: "FURIN_PAGE";
|
|
273
|
+
component: React.FC<infer P>;
|
|
274
|
+
}
|
|
275
|
+
? P
|
|
276
|
+
: T extends Route<infer D, infer P, infer Q>
|
|
277
|
+
? D & { children: React.ReactNode } & ComponentProps<P, Q>
|
|
278
|
+
: never;
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BunPlugin } from "bun";
|
|
2
|
+
import { t } from "elysia";
|
|
3
|
+
|
|
4
|
+
export const BUILD_TARGETS = ["bun", "node", "vercel", "cloudflare", "static"] as const;
|
|
5
|
+
|
|
6
|
+
export type BuildTarget = (typeof BUILD_TARGETS)[number];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for the `static` build target.
|
|
10
|
+
* Produces a fully pre-rendered directory deployable to any static host
|
|
11
|
+
* (GitHub Pages, Netlify, Surge.sh, …).
|
|
12
|
+
*/
|
|
13
|
+
export interface StaticExportConfig {
|
|
14
|
+
/**
|
|
15
|
+
* Sub-path prefix for GitHub Pages sub-path deployments.
|
|
16
|
+
* e.g. "/furin" when the site is served at `https://user.github.io/furin/`.
|
|
17
|
+
* Must start with "/" and have no trailing slash.
|
|
18
|
+
* Default: "" (site served at root).
|
|
19
|
+
*/
|
|
20
|
+
basePath?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Behaviour when SSR or ISR routes are encountered during a static build.
|
|
23
|
+
* - "error" (default): throw at build time with the list of incompatible routes.
|
|
24
|
+
* - "skip": emit a warning and omit those routes from the output.
|
|
25
|
+
*/
|
|
26
|
+
onSSR?: "error" | "skip";
|
|
27
|
+
/**
|
|
28
|
+
* Output directory for the static export.
|
|
29
|
+
* Default: "dist".
|
|
30
|
+
*/
|
|
31
|
+
outDir?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const buildTargetSchema = t.Union(BUILD_TARGETS.map((v) => t.Literal(v)));
|
|
35
|
+
const compileTargetSchema = t.Union([t.Literal("server"), t.Literal("embed")]);
|
|
36
|
+
|
|
37
|
+
export const configSchema = t.Object({
|
|
38
|
+
rootDir: t.Optional(t.String()),
|
|
39
|
+
pagesDir: t.Optional(t.String()),
|
|
40
|
+
serverEntry: t.Optional(t.String()),
|
|
41
|
+
targets: t.Optional(t.Array(buildTargetSchema)),
|
|
42
|
+
bun: t.Optional(
|
|
43
|
+
t.Object({
|
|
44
|
+
compile: t.Optional(compileTargetSchema),
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
static: t.Optional(
|
|
48
|
+
t.Object({
|
|
49
|
+
basePath: t.Optional(t.String()),
|
|
50
|
+
outDir: t.Optional(t.String()),
|
|
51
|
+
onSSR: t.Optional(t.Union([t.Literal("error"), t.Literal("skip")])),
|
|
52
|
+
})
|
|
53
|
+
),
|
|
54
|
+
// plugins omitted : TypeBox can't validate Bun.BunPlugin[] (functions)
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export type FurinPlugin = BunPlugin & { buildOnly?: boolean };
|
|
58
|
+
|
|
59
|
+
export type FurinConfig = (typeof configSchema)["static"] & {
|
|
60
|
+
plugins?: FurinPlugin[];
|
|
61
|
+
static?: StaticExportConfig;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function defineConfig(config: FurinConfig): FurinConfig {
|
|
65
|
+
return config;
|
|
66
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { RequestLogger } from "evlog";
|
|
3
|
+
// biome-ignore lint/style/noExportedImports: used locally and re-exported for consumers
|
|
4
|
+
import { createLogger } from "evlog";
|
|
5
|
+
import { useLogger as _evlogUseLogger } from "evlog/elysia";
|
|
6
|
+
|
|
7
|
+
export { createLogger };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Fallback used when useLogger() is called completely outside any context
|
|
11
|
+
* (not in a live request, not in a synthetic render scope).
|
|
12
|
+
*/
|
|
13
|
+
const noopLogger: RequestLogger = {
|
|
14
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
15
|
+
set: () => {},
|
|
16
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
17
|
+
error: () => {},
|
|
18
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
19
|
+
info: () => {},
|
|
20
|
+
// biome-ignore lint/suspicious/noEmptyBlockStatements: intentional no-op
|
|
21
|
+
warn: () => {},
|
|
22
|
+
emit: () => null,
|
|
23
|
+
getContext: () => ({}),
|
|
24
|
+
fork: (_label, fn) => fn() as undefined,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const syntheticRenderStorage = new AsyncLocalStorage<RequestLogger>();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns the current request-scoped logger.
|
|
31
|
+
*
|
|
32
|
+
* Priority:
|
|
33
|
+
* 1. Live Elysia request handled by the evlog() plugin → full request-scoped wide event
|
|
34
|
+
* 2. Synthetic render scope (ISR revalidation, SSG pre-render) → detached createLogger()
|
|
35
|
+
* that still drains to the configured adapter (Datadog, Axiom, etc.)
|
|
36
|
+
* 3. Completely outside any context → no-op
|
|
37
|
+
*
|
|
38
|
+
* Import from `@teyik0/furin` instead of `evlog/elysia` so this fallback chain
|
|
39
|
+
* applies in all rendering contexts.
|
|
40
|
+
*/
|
|
41
|
+
export function useLogger(): RequestLogger {
|
|
42
|
+
try {
|
|
43
|
+
return _evlogUseLogger();
|
|
44
|
+
} catch {
|
|
45
|
+
return syntheticRenderStorage.getStore() ?? noopLogger;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Runs `fn` inside a synthetic render scope.
|
|
51
|
+
*
|
|
52
|
+
* Creates a detached `createLogger()` instance for the duration of `fn`.
|
|
53
|
+
* `useLogger()` calls inside `fn` (including user loaders) return this logger
|
|
54
|
+
* instead of throwing. On completion, the accumulated wide event is emitted to
|
|
55
|
+
* the global drain with the provided initial context (e.g. route pattern).
|
|
56
|
+
*
|
|
57
|
+
* Used by renderForPath() which drives both ISR background revalidation and
|
|
58
|
+
* SSG pre-renders — neither has a live Elysia request context.
|
|
59
|
+
*/
|
|
60
|
+
export async function runInSyntheticRenderScope<T>(
|
|
61
|
+
fn: () => Promise<T> | T,
|
|
62
|
+
initialContext: Record<string, unknown>
|
|
63
|
+
): Promise<T> {
|
|
64
|
+
const logger = createLogger(initialContext);
|
|
65
|
+
try {
|
|
66
|
+
const result = await syntheticRenderStorage.run(logger, () => Promise.resolve(fn()));
|
|
67
|
+
logger.emit();
|
|
68
|
+
return result;
|
|
69
|
+
} catch (err) {
|
|
70
|
+
logger.error(err instanceof Error ? err : new Error(String(err)));
|
|
71
|
+
logger.emit();
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
}
|