@teyik0/furin 0.1.0-alpha.12 → 0.1.0-alpha.14

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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/dist/await.d.ts +67 -0
  4. package/dist/build/compile-entry.ts +129 -0
  5. package/dist/build/entry-template.ts +133 -0
  6. package/dist/build/index.js +2224 -408
  7. package/dist/build/server-routes-entry.ts +55 -0
  8. package/dist/cli/index.js +2094 -283
  9. package/dist/client.d.ts +33 -2
  10. package/dist/client.js +65 -1
  11. package/dist/deferred-ndjson.d.ts +32 -0
  12. package/dist/env.d.ts +50 -0
  13. package/dist/error.d.ts +14 -0
  14. package/dist/furin.d.ts +3 -0
  15. package/dist/furin.js +2689 -319
  16. package/dist/internal.js +17 -0
  17. package/dist/lang-detect.d.ts +5 -0
  18. package/dist/link.d.ts +1 -0
  19. package/dist/link.js +1460 -100
  20. package/dist/plugin/index.js +103 -30
  21. package/dist/plugin/transform-client.d.ts +2 -1
  22. package/dist/render/assemble.d.ts +32 -0
  23. package/dist/render/cache.d.ts +7 -0
  24. package/dist/render/element.d.ts +17 -1
  25. package/dist/render/index.d.ts +10 -1
  26. package/dist/render/index.ts +1073 -0
  27. package/dist/render/loaders.d.ts +46 -3
  28. package/dist/render/shell.d.ts +16 -6
  29. package/dist/render/shell.ts +202 -0
  30. package/dist/router-provider.d.ts +88 -19
  31. package/dist/router.d.ts +63 -0
  32. package/dist/router.js +2777 -318
  33. package/dist/router.ts +1523 -0
  34. package/dist/runtime-env.js +10 -0
  35. package/dist/server-error.d.ts +12 -0
  36. package/dist/utils.d.ts +7 -0
  37. package/package.json +46 -16
  38. package/src/adapter/bun.ts +7 -4
  39. package/src/adapter/static.ts +73 -31
  40. package/src/await.tsx +175 -0
  41. package/src/build/client.ts +4 -8
  42. package/src/build/entry-template.ts +13 -6
  43. package/src/build/hydrate.ts +67 -1
  44. package/src/build/route-types.ts +5 -1
  45. package/src/build/scan-server.ts +16 -4
  46. package/src/client.ts +58 -1
  47. package/src/deferred-ndjson.ts +212 -0
  48. package/src/dev-page-plugin.ts +2 -0
  49. package/src/error.ts +18 -1
  50. package/src/furin.ts +12 -2
  51. package/src/lang-detect.ts +46 -0
  52. package/src/link.tsx +18 -4
  53. package/src/plugin/index.ts +7 -8
  54. package/src/plugin/transform-client.ts +114 -43
  55. package/src/render/assemble.ts +67 -0
  56. package/src/render/boundaries.tsx +23 -5
  57. package/src/render/cache.ts +7 -0
  58. package/src/render/default-screens.tsx +4 -11
  59. package/src/render/element.tsx +26 -3
  60. package/src/render/index.ts +251 -47
  61. package/src/render/loaders.ts +221 -18
  62. package/src/render/shell.ts +27 -12
  63. package/src/router-provider.tsx +422 -128
  64. package/src/router.ts +543 -54
  65. package/src/server-error.ts +47 -0
  66. package/src/utils.ts +31 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 teyik0
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ <div align="center">
2
+ <img src="https://raw.githubusercontent.com/teyik0/furin/main/apps/docs/public/furin-logo.webp" alt="Furin" width="120" />
3
+ <h1>Furin</h1>
4
+ <p>React meta-framework powered by Elysia and Bun — file-based routing, SSR, SSG, ISR, and full TypeScript inference.</p>
5
+
6
+ <a href="https://www.npmjs.com/package/@teyik0/furin"><img src="https://img.shields.io/npm/v/%40teyik0%2Ffurin?style=flat-square&logo=npm&color=orange" alt="npm version" /></a>
7
+ <a href="https://www.npmjs.com/package/@teyik0/furin"><img src="https://img.shields.io/npm/dm/%40teyik0%2Ffurin?style=flat-square&color=orange" alt="npm downloads" /></a>
8
+ <a href="https://github.com/teyik0/furin/blob/main/LICENSE"><img src="https://img.shields.io/github/license/teyik0/furin?style=flat-square" alt="License" /></a>
9
+ <a href="https://bun.sh"><img src="https://img.shields.io/badge/bun-%3E%3D1.3.0-f5d147?style=flat-square&logo=bun&logoColor=black" alt="Bun" /></a>
10
+ <a href="https://www.typescriptlang.org"><img src="https://img.shields.io/badge/TypeScript-strict-3178c6?style=flat-square&logo=typescript&logoColor=white" alt="TypeScript" /></a>
11
+ <a href="https://react.review/repo/Teyik0/furin"><img src="https://react.review/api/badge/Teyik0/furin" alt="React Review" /></a>
12
+ </div>
13
+
14
+ ---
15
+
16
+ ## Quick Start
17
+
18
+ ```bash
19
+ bun create furin@latest my-app
20
+ cd my-app
21
+ bun install
22
+ bun run dev
23
+ ```
24
+
25
+ For the shadcn/ui starter:
26
+
27
+ ```bash
28
+ bun create furin@latest my-app --template full
29
+ ```
30
+
31
+ ## Documentation
32
+
33
+ Full API reference, rendering modes, routing, and deployment guides at **[teyik0.github.io/furin](https://teyik0.github.io/furin/)**.
@@ -0,0 +1,67 @@
1
+ import { type ReactNode } from "react";
2
+ /**
3
+ * Context used to pass the caught error from `AsyncErrorBoundary` down to
4
+ * `errorElement` children so they can read it via `useAsyncError()`.
5
+ */
6
+ export declare const AsyncErrorContext: import("react").Context<unknown>;
7
+ /**
8
+ * Inside an `<Await errorElement={...}>`, returns the error that caused the
9
+ * deferred Promise to reject. Throws outside of an error-boundary subtree.
10
+ */
11
+ export declare function useAsyncError(): unknown;
12
+ /**
13
+ * Inside a resolved `<Await>`, returns the resolved value via React 19's
14
+ * `use()`. Can be used as an alternative to the render-prop children API.
15
+ *
16
+ * Must be used inside a component that is rendered as children of `<Await>`.
17
+ */
18
+ export declare function useAsyncValue<T>(): T;
19
+ export interface AwaitProps<T> {
20
+ /**
21
+ * Render-prop children: called with the resolved value once the Promise
22
+ * settles. The returned `ReactNode` is rendered inside a Suspense boundary.
23
+ *
24
+ * Marked optional here so `createElement(Await, props, renderFn)` is
25
+ * accepted by TypeScript — the 3rd argument to `createElement` becomes
26
+ * `children` at runtime and is always required for correct behaviour.
27
+ */
28
+ children?: (value: T) => ReactNode;
29
+ /**
30
+ * Rendered when the Promise rejects. Inside this subtree, `useAsyncError()`
31
+ * returns the rejection reason. When omitted the error propagates up to the
32
+ * next React error boundary.
33
+ */
34
+ errorElement?: ReactNode;
35
+ /**
36
+ * The deferred Promise to await. Passed as a prop so React's `use()` can
37
+ * suspend the boundary until it resolves. This is the same Promise object
38
+ * that the loader returned inside `defer()`.
39
+ */
40
+ resolve: Promise<T>;
41
+ }
42
+ /**
43
+ * Deferred-data boundary component. Uses React 19's `use()` to suspend the
44
+ * render until `resolve` settles, then calls the render-prop `children` with
45
+ * the resolved value.
46
+ *
47
+ * Wrap with `<Suspense fallback={…}>` to show a loading indicator while the
48
+ * Promise is pending.
49
+ *
50
+ * @example
51
+ * ```tsx
52
+ * export default route.page({
53
+ * loader: () => defer({ board: "x", stats: fetchStats() }),
54
+ * component: ({ board, stats }) => (
55
+ * <div>
56
+ * <h1>{board}</h1>
57
+ * <Suspense fallback={<Spinner />}>
58
+ * <Await resolve={stats}>
59
+ * {(s) => <StatsBar stats={s} />}
60
+ * </Await>
61
+ * </Suspense>
62
+ * </div>
63
+ * ),
64
+ * });
65
+ * ```
66
+ */
67
+ export declare function Await<T>({ resolve, errorElement, children }: AwaitProps<T>): ReactNode;
@@ -0,0 +1,129 @@
1
+ import { existsSync, writeFileSync } from "node:fs";
2
+ import { join, relative } from "node:path";
3
+ import { buildEntrySource } from "./entry-template";
4
+ import { collectFilesRecursive, ensureDir, toPosixPath } from "./shared";
5
+
6
+ export interface CompileEntryOptions {
7
+ buildId: string | undefined;
8
+ embed: { clientDir: string } | undefined;
9
+ outDir: string;
10
+ publicDir: string | undefined;
11
+ rootConventions: { errorPath: string | undefined; notFoundPath: string | undefined } | undefined;
12
+ rootPath: string;
13
+ routeMetadata:
14
+ | Record<
15
+ string,
16
+ {
17
+ segmentBoundaries: Array<{
18
+ depth: number;
19
+ path: string;
20
+ errorPath: string | undefined;
21
+ notFoundPath: string | undefined;
22
+ }>;
23
+ }
24
+ >
25
+ | undefined;
26
+ routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
27
+ serverEntry: string;
28
+ }
29
+
30
+ /**
31
+ * Generates a single `_compile-entry.ts` that:
32
+ * 1. Statically imports every page module so Bun bundles them into the binary
33
+ * 2. Optionally embeds client assets via `with { type: "file" }` (embed mode)
34
+ * 3. Sets production mode and registers everything in a single CompileContext
35
+ * 4. Dynamically imports server.ts to boot the app
36
+ */
37
+ export function generateCompileEntry(options: CompileEntryOptions): string {
38
+ const {
39
+ buildId,
40
+ outDir,
41
+ rootPath,
42
+ routes,
43
+ serverEntry,
44
+ embed,
45
+ publicDir,
46
+ rootConventions,
47
+ routeMetadata,
48
+ } = options;
49
+ ensureDir(outDir);
50
+
51
+ // Build embedded asset imports if embed mode
52
+ const assetImports: string[] = [];
53
+ let embeddedBlock: string[] = [];
54
+ if (embed) {
55
+ if (!existsSync(embed.clientDir)) {
56
+ throw new Error(
57
+ `[furin] Client directory not found: ${embed.clientDir}. Run the client build first.`
58
+ );
59
+ }
60
+ const clientFiles = collectFilesRecursive(embed.clientDir);
61
+ const assetEntries: string[] = [];
62
+ let templateVarName: string | null = null;
63
+
64
+ let assetIndex = 0;
65
+ for (const file of clientFiles) {
66
+ const varName = `_asset${assetIndex++}`;
67
+ const relPath = toPosixPath(relative(outDir, file));
68
+ const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
69
+
70
+ assetImports.push(
71
+ `import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
72
+ );
73
+
74
+ const clientRelativePath = toPosixPath(relative(embed.clientDir, file));
75
+ if (clientRelativePath === "index.html") {
76
+ templateVarName = varName;
77
+ } else {
78
+ assetEntries.push(` ${JSON.stringify(`/_client/${clientRelativePath}`)}: ${varName},`);
79
+ }
80
+ }
81
+
82
+ if (templateVarName === null) {
83
+ throw new Error(
84
+ `[furin] Embed mode requires a client index.html at ${join(embed.clientDir, "index.html")}. Run the client build first.`
85
+ );
86
+ }
87
+
88
+ if (publicDir && existsSync(publicDir)) {
89
+ const publicFiles = collectFilesRecursive(publicDir);
90
+ for (const file of publicFiles) {
91
+ const varName = `_asset${assetIndex++}`;
92
+ const relPath = toPosixPath(relative(outDir, file));
93
+ const importPath = relPath.startsWith(".") ? relPath : `./${relPath}`;
94
+
95
+ assetImports.push(
96
+ `import ${varName} from ${JSON.stringify(importPath)} with { type: "file" };`
97
+ );
98
+
99
+ const publicRelativePath = toPosixPath(relative(publicDir, file));
100
+ assetEntries.push(` ${JSON.stringify(`/public/${publicRelativePath}`)}: ${varName},`);
101
+ }
102
+ }
103
+
104
+ embeddedBlock = [
105
+ " embedded: {",
106
+ ` template: ${templateVarName},`,
107
+ " assets: {",
108
+ ...assetEntries,
109
+ " },",
110
+ " },",
111
+ ];
112
+ }
113
+
114
+ const source = buildEntrySource({
115
+ buildId,
116
+ headerComment: "// Auto-generated by furin compile — do not edit",
117
+ rootPath,
118
+ routes,
119
+ serverEntry,
120
+ extraImports: assetImports,
121
+ extraContext: embeddedBlock,
122
+ rootConventions,
123
+ routeMetadata,
124
+ });
125
+
126
+ const entryPath = join(outDir, "_compile-entry.ts");
127
+ writeFileSync(entryPath, source);
128
+ return entryPath;
129
+ }
@@ -0,0 +1,133 @@
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ // import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
6
+ // through package exports, and is the Web-standard API.
7
+ const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
8
+ const _pkgSrcDirRaw = existsSync(join(_pkgRoot, "src", "furin.ts"))
9
+ ? join(_pkgRoot, "src")
10
+ : join(_pkgRoot, "dist");
11
+ // Normalize to forward slashes so endsWith checks and template paths work on Windows.
12
+ const _pkgSrcDir = _pkgSrcDirRaw.replace(/\\/g, "/");
13
+ const _ext = _pkgSrcDir.endsWith("/src") ? ".ts" : ".js";
14
+ const INTERNAL_MODULE_PATH = `${_pkgSrcDir}/internal${_ext}`;
15
+ const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/runtime-env${_ext}`;
16
+
17
+ export interface EntryTemplateOptions {
18
+ buildId?: string;
19
+ extraContext?: string[];
20
+ extraImports?: string[];
21
+ headerComment: string;
22
+ rootConventions?: { errorPath?: string; notFoundPath?: string };
23
+ rootPath: string;
24
+ routeMetadata?: Record<
25
+ string,
26
+ {
27
+ segmentBoundaries: Array<{
28
+ depth: number;
29
+ path: string;
30
+ errorPath?: string;
31
+ notFoundPath?: string;
32
+ }>;
33
+ }
34
+ >;
35
+ routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
36
+ serverEntry: string;
37
+ }
38
+
39
+ function collectConventionPaths(
40
+ rootConventions: EntryTemplateOptions["rootConventions"],
41
+ routeMetadata: EntryTemplateOptions["routeMetadata"]
42
+ ): string[] {
43
+ const paths: string[] = [];
44
+ if (rootConventions?.errorPath) {
45
+ paths.push(rootConventions.errorPath);
46
+ }
47
+ if (rootConventions?.notFoundPath) {
48
+ paths.push(rootConventions.notFoundPath);
49
+ }
50
+ if (routeMetadata) {
51
+ for (const meta of Object.values(routeMetadata)) {
52
+ for (const seg of meta.segmentBoundaries) {
53
+ if (seg.errorPath) {
54
+ paths.push(seg.errorPath);
55
+ }
56
+ if (seg.notFoundPath) {
57
+ paths.push(seg.notFoundPath);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ return [...new Set(paths)];
63
+ }
64
+
65
+ export function buildEntrySource(options: EntryTemplateOptions): string {
66
+ const { buildId, headerComment, rootPath, routes, serverEntry, rootConventions, routeMetadata } =
67
+ options;
68
+ let { extraImports, extraContext } = options;
69
+ if (extraImports === undefined) {
70
+ extraImports = [];
71
+ }
72
+ if (extraContext === undefined) {
73
+ extraContext = [];
74
+ }
75
+
76
+ const allModulePaths = [
77
+ rootPath,
78
+ ...routes.map((r) => r.path),
79
+ ...collectConventionPaths(rootConventions, routeMetadata),
80
+ ];
81
+ const moduleImports: string[] = [];
82
+ const moduleEntries: string[] = [];
83
+
84
+ for (let i = 0; i < allModulePaths.length; i++) {
85
+ const absPath = (allModulePaths[i] as string).replace(/\\/g, "/");
86
+ const varName = `_mod${i}`;
87
+ moduleImports.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
88
+ moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
89
+ }
90
+
91
+ const routeEntries = routes.map(
92
+ (r) =>
93
+ ` { pattern: ${JSON.stringify(r.pattern)}, path: ${JSON.stringify(r.path.replace(/\\/g, "/"))}, mode: ${JSON.stringify(r.mode)} },`
94
+ );
95
+
96
+ const rootConventionsLine = rootConventions
97
+ ? ` rootConventions: ${JSON.stringify(rootConventions)},`
98
+ : "";
99
+ const routeMetadataLine = routeMetadata
100
+ ? ` routeMetadata: ${JSON.stringify(routeMetadata)},`
101
+ : "";
102
+
103
+ const lines = [
104
+ headerComment,
105
+ `import { __setCompileContext } from ${JSON.stringify(INTERNAL_MODULE_PATH)};`,
106
+ `import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`,
107
+ ...moduleImports,
108
+ ...(extraImports.length > 0 ? ["", ...extraImports] : []),
109
+ "",
110
+ "// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
111
+ "__setDevMode(false);",
112
+ 'process.env.NODE_ENV = "production";',
113
+ "",
114
+ "__setCompileContext({",
115
+ ` buildId: ${JSON.stringify(buildId ?? "")},`,
116
+ ` rootPath: ${JSON.stringify(rootPath.replace(/\\/g, "/"))},`,
117
+ rootConventionsLine,
118
+ " modules: {",
119
+ ...moduleEntries,
120
+ " },",
121
+ " routes: [",
122
+ ...routeEntries,
123
+ " ],",
124
+ routeMetadataLine,
125
+ ...extraContext,
126
+ "});",
127
+ "",
128
+ `await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
129
+ "",
130
+ ];
131
+
132
+ return lines.join("\n");
133
+ }