@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.
Files changed (130) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/package.json +61 -25
  4. package/src/adapter/bun.ts +90 -41
  5. package/src/adapter/static.ts +100 -40
  6. package/src/build/client.ts +13 -14
  7. package/src/build/compile-entry.ts +20 -14
  8. package/src/build/entry-template.ts +90 -8
  9. package/src/build/hydrate.ts +219 -36
  10. package/src/build/index.ts +10 -9
  11. package/src/build/route-types.ts +65 -9
  12. package/src/build/scan-server.ts +52 -67
  13. package/src/build/server-routes-entry.ts +7 -11
  14. package/src/build/shared.ts +2 -1
  15. package/src/build/ssg-cache.ts +32 -0
  16. package/src/build/types.ts +5 -1
  17. package/src/cli/index.ts +1 -0
  18. package/src/client/boundaries.tsx +259 -0
  19. package/src/client/default-screens.tsx +304 -0
  20. package/src/client/link.tsx +347 -0
  21. package/src/client/router/boundary-tree.tsx +116 -0
  22. package/src/client/router/context.ts +40 -0
  23. package/src/client/router/history.ts +41 -0
  24. package/src/client/router/index.ts +56 -0
  25. package/src/client/router/link-utils.ts +177 -0
  26. package/src/client/router/navigation.ts +61 -0
  27. package/src/client/router/provider.tsx +759 -0
  28. package/src/client/router/search/index.ts +99 -0
  29. package/src/client/router/search-store.ts +86 -0
  30. package/src/client/router/spa-response.ts +143 -0
  31. package/src/client/router/types.ts +271 -0
  32. package/src/client/server-error.ts +47 -0
  33. package/src/client.ts +80 -3
  34. package/src/config.ts +11 -3
  35. package/src/env.d.ts +1 -0
  36. package/src/furin.ts +114 -29
  37. package/src/plugin/index.ts +7 -8
  38. package/src/plugin/transform-client.ts +146 -120
  39. package/src/server/auto-invalidate/index.ts +10 -0
  40. package/src/server/auto-invalidate/plugin.ts +25 -0
  41. package/src/server/auto-invalidate/registry.ts +60 -0
  42. package/src/server/auto-invalidate/runtime.ts +133 -0
  43. package/src/server/auto-invalidate/types.ts +16 -0
  44. package/src/server/cache/dev-loader.ts +211 -0
  45. package/src/server/cache/index.ts +34 -0
  46. package/src/server/cache/invalidation.ts +111 -0
  47. package/src/server/cache/isr-ssg.ts +41 -0
  48. package/src/server/cache/isr.ts +17 -0
  49. package/src/server/cache/registry.ts +35 -0
  50. package/src/server/cache/route-cache.ts +124 -0
  51. package/src/server/cache/ssg.ts +15 -0
  52. package/src/{context-logger.ts → server/context-logger.ts} +5 -0
  53. package/src/server/dev-inspector.ts +60 -0
  54. package/src/{dev-page-plugin.ts → server/dev-page-plugin.ts} +15 -0
  55. package/src/{internal.ts → server/internal.ts} +17 -0
  56. package/src/server/lang-detect.ts +46 -0
  57. package/src/server/render/assemble.ts +133 -0
  58. package/src/server/render/element.tsx +113 -0
  59. package/src/server/render/index.ts +22 -0
  60. package/src/server/render/isr.ts +317 -0
  61. package/src/server/render/loaders.ts +328 -0
  62. package/src/server/render/not-found.ts +98 -0
  63. package/src/{render → server/render}/shell.ts +28 -13
  64. package/src/server/render/ssg.ts +148 -0
  65. package/src/server/render/ssr.ts +583 -0
  66. package/src/server/router/discovery.ts +474 -0
  67. package/src/server/router/hmr.ts +376 -0
  68. package/src/server/router/index.ts +28 -0
  69. package/src/server/router/patterns.ts +219 -0
  70. package/src/server/router/plugin.ts +384 -0
  71. package/src/server/router/schemas.ts +278 -0
  72. package/src/server/router/types.ts +59 -0
  73. package/src/shared/await.tsx +192 -0
  74. package/src/shared/deferred-ndjson.ts +244 -0
  75. package/src/shared/digest.ts +71 -0
  76. package/src/shared/error.ts +49 -0
  77. package/src/shared/not-found.ts +40 -0
  78. package/src/shared/search-params.ts +182 -0
  79. package/src/shared/utils/ast-walk.ts +51 -0
  80. package/src/{utils.ts → shared/utils/index.ts} +32 -1
  81. package/dist/adapter/bun.d.ts +0 -3
  82. package/dist/adapter/static.d.ts +0 -3
  83. package/dist/build/client.d.ts +0 -25
  84. package/dist/build/compile-entry.d.ts +0 -23
  85. package/dist/build/entry-template.d.ts +0 -14
  86. package/dist/build/hydrate.d.ts +0 -24
  87. package/dist/build/index.d.ts +0 -7
  88. package/dist/build/index.js +0 -3727
  89. package/dist/build/route-types.d.ts +0 -21
  90. package/dist/build/scan-server.d.ts +0 -8
  91. package/dist/build/server-routes-entry.d.ts +0 -23
  92. package/dist/build/shared.d.ts +0 -11
  93. package/dist/build/types.d.ts +0 -76
  94. package/dist/cli/config.d.ts +0 -9
  95. package/dist/cli/index.d.ts +0 -2
  96. package/dist/cli/index.js +0 -3762
  97. package/dist/client.d.ts +0 -195
  98. package/dist/client.js +0 -20
  99. package/dist/config.d.ts +0 -47
  100. package/dist/config.js +0 -28
  101. package/dist/context-logger.d.ts +0 -26
  102. package/dist/dev-page-plugin.d.ts +0 -109
  103. package/dist/furin.d.ts +0 -34
  104. package/dist/furin.js +0 -2744
  105. package/dist/internal.d.ts +0 -19
  106. package/dist/link.d.ts +0 -245
  107. package/dist/link.js +0 -1232
  108. package/dist/plugin/index.d.ts +0 -20
  109. package/dist/plugin/index.js +0 -1408
  110. package/dist/plugin/transform-client.d.ts +0 -9
  111. package/dist/render/assemble.d.ts +0 -15
  112. package/dist/render/cache.d.ts +0 -95
  113. package/dist/render/element.d.ts +0 -4
  114. package/dist/render/index.d.ts +0 -27
  115. package/dist/render/loaders.d.ts +0 -12
  116. package/dist/render/shell.d.ts +0 -29
  117. package/dist/render/template.d.ts +0 -6
  118. package/dist/router.d.ts +0 -45
  119. package/dist/router.js +0 -1671
  120. package/dist/runtime-env.d.ts +0 -3
  121. package/dist/utils.d.ts +0 -6
  122. package/src/link.tsx +0 -1120
  123. package/src/render/assemble.ts +0 -66
  124. package/src/render/cache.ts +0 -241
  125. package/src/render/element.tsx +0 -28
  126. package/src/render/index.ts +0 -483
  127. package/src/render/loaders.ts +0 -117
  128. package/src/router.ts +0 -439
  129. /package/src/{render → server/render}/template.ts +0 -0
  130. /package/src/{runtime-env.ts → server/runtime-env.ts} +0 -0
@@ -0,0 +1,182 @@
1
+ export type SearchParamValue =
2
+ | string
3
+ | number
4
+ | boolean
5
+ | null
6
+ | undefined
7
+ | SearchParamValue[]
8
+ | { [key: string]: SearchParamValue };
9
+
10
+ export interface SearchParamsInput {
11
+ [key: string]: SearchParamValue;
12
+ }
13
+
14
+ export interface SearchRouteMetadata {
15
+ pattern: string;
16
+ regex: RegExp;
17
+ searchDefaults?: SearchParamsInput;
18
+ }
19
+
20
+ function routeSegmentSpecificity(segment: string): number {
21
+ if (segment === "*") {
22
+ return 1;
23
+ }
24
+ if (segment.startsWith(":")) {
25
+ return 2;
26
+ }
27
+ return 3;
28
+ }
29
+
30
+ function compareSearchRouteSpecificity(a: string, b: string): number {
31
+ const aSegments = a.split("/").filter((segment) => segment.length > 0);
32
+ const bSegments = b.split("/").filter((segment) => segment.length > 0);
33
+ const length = Math.max(aSegments.length, bSegments.length);
34
+ for (let i = 0; i < length; i++) {
35
+ const aSegment = aSegments[i];
36
+ const bSegment = bSegments[i];
37
+ if (aSegment === undefined) {
38
+ return -1;
39
+ }
40
+ if (bSegment === undefined) {
41
+ return 1;
42
+ }
43
+ const diff = routeSegmentSpecificity(aSegment) - routeSegmentSpecificity(bSegment);
44
+ if (diff !== 0) {
45
+ return diff;
46
+ }
47
+ }
48
+ return 0;
49
+ }
50
+
51
+ function isObject(value: unknown): value is { [key: string]: unknown } {
52
+ return !!value && typeof value === "object" && !Array.isArray(value);
53
+ }
54
+
55
+ function arraysDeepEqual(a: unknown[], b: unknown[]): boolean {
56
+ if (a.length !== b.length) {
57
+ return false;
58
+ }
59
+ for (let i = 0; i < a.length; i++) {
60
+ if (!deepEqual(a[i], b[i])) {
61
+ return false;
62
+ }
63
+ }
64
+ return true;
65
+ }
66
+
67
+ function objectsDeepEqual(a: { [key: string]: unknown }, b: { [key: string]: unknown }): boolean {
68
+ const aKeys = Object.keys(a);
69
+ if (aKeys.length !== Object.keys(b).length) {
70
+ return false;
71
+ }
72
+ for (const key of aKeys) {
73
+ if (!(Object.hasOwn(b, key) && deepEqual(a[key], b[key]))) {
74
+ return false;
75
+ }
76
+ }
77
+ return true;
78
+ }
79
+
80
+ function deepEqual(a: unknown, b: unknown): boolean {
81
+ if (Object.is(a, b)) {
82
+ return true;
83
+ }
84
+ if (Array.isArray(a) && Array.isArray(b)) {
85
+ return arraysDeepEqual(a, b);
86
+ }
87
+ if (isObject(a) && isObject(b)) {
88
+ return objectsDeepEqual(a, b);
89
+ }
90
+ return false;
91
+ }
92
+
93
+ export function stripSearchDefaults(
94
+ search: SearchParamsInput,
95
+ defaults: SearchParamsInput | undefined
96
+ ): SearchParamsInput {
97
+ if (!defaults) {
98
+ return search;
99
+ }
100
+ const stripped: SearchParamsInput = {};
101
+ for (const [key, value] of Object.entries(search)) {
102
+ if (Object.hasOwn(defaults, key) && deepEqual(value, defaults[key])) {
103
+ continue;
104
+ }
105
+ stripped[key] = value;
106
+ }
107
+ return stripped;
108
+ }
109
+
110
+ export function appendSearchParamValue(params: URLSearchParams, key: string, value: unknown): void {
111
+ if (value == null) {
112
+ return;
113
+ }
114
+
115
+ if (Array.isArray(value)) {
116
+ for (const item of value) {
117
+ if (item != null) {
118
+ params.append(key, typeof item === "object" ? JSON.stringify(item) : String(item));
119
+ }
120
+ }
121
+ return;
122
+ }
123
+
124
+ params.set(key, typeof value === "object" ? JSON.stringify(value) : String(value));
125
+ }
126
+
127
+ export function buildSearchParams(
128
+ search: SearchParamsInput,
129
+ defaults?: SearchParamsInput
130
+ ): URLSearchParams {
131
+ const params = new URLSearchParams();
132
+ for (const [key, value] of Object.entries(stripSearchDefaults(search, defaults))) {
133
+ appendSearchParamValue(params, key, value);
134
+ }
135
+ return params;
136
+ }
137
+
138
+ export function collectSearchDefaults(schema: unknown): SearchParamsInput | undefined {
139
+ if (!(isObject(schema) && isObject(schema.properties))) {
140
+ return;
141
+ }
142
+
143
+ const defaults: SearchParamsInput = {};
144
+ for (const [key, value] of Object.entries(schema.properties)) {
145
+ if (isObject(value) && Object.hasOwn(value, "default") && value.default != null) {
146
+ defaults[key] = value.default as SearchParamValue;
147
+ }
148
+ }
149
+
150
+ return Object.keys(defaults).length > 0 ? defaults : undefined;
151
+ }
152
+
153
+ export function findSearchDefaults(
154
+ pathname: string,
155
+ routes: readonly SearchRouteMetadata[]
156
+ ): SearchParamsInput | undefined {
157
+ let match: SearchRouteMetadata | undefined;
158
+ for (const route of routes) {
159
+ if (!route.regex.test(pathname)) {
160
+ continue;
161
+ }
162
+ if (!match || compareSearchRouteSpecificity(route.pattern, match.pattern) > 0) {
163
+ match = route;
164
+ }
165
+ }
166
+ return match?.searchDefaults;
167
+ }
168
+
169
+ export function findSearchDefaultsForRouteTarget(
170
+ to: string,
171
+ routes: readonly SearchRouteMetadata[]
172
+ ): SearchParamsInput | undefined {
173
+ if (to.startsWith("http://") || to.startsWith("https://") || to.startsWith("//")) {
174
+ return;
175
+ }
176
+ try {
177
+ const pathname = new URL(to, "http://furin.local").pathname;
178
+ return findSearchDefaults(pathname, routes);
179
+ } catch {
180
+ return;
181
+ }
182
+ }
@@ -0,0 +1,51 @@
1
+ import type { BaseNode, Node as YukuNode } from "yuku-parser";
2
+
3
+ /**
4
+ * Minimal ESTree node interface used by AST-walking utilities across the
5
+ * framework (build scanning, client transform, etc.).
6
+ */
7
+ export interface AstNode extends BaseNode {
8
+ body?: AstNode[];
9
+ type: YukuNode["type"] | string;
10
+ [key: string]: unknown;
11
+ }
12
+
13
+ const SKIP_KEYS = new Set(["type", "start", "end", "comments"]);
14
+
15
+ function isAstNode(value: unknown): value is AstNode {
16
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
17
+ return false;
18
+ }
19
+ const candidate = value as Partial<AstNode>;
20
+ return typeof candidate.type === "string";
21
+ }
22
+
23
+ /**
24
+ * Walks an ESTree-compatible AST node recursively, calling `visitor` for every
25
+ * node that has a `type` property.
26
+ *
27
+ * Arrays are traversed element-by-element. The keys `type`, `start` and `end`
28
+ * are skipped so that the visitor is only invoked on genuine child nodes.
29
+ */
30
+ export function walkAST(node: unknown, visitor: (n: AstNode) => void): void {
31
+ if (!node || typeof node !== "object") {
32
+ return;
33
+ }
34
+ if (Array.isArray(node)) {
35
+ for (const child of node) {
36
+ walkAST(child, visitor);
37
+ }
38
+ return;
39
+ }
40
+ if (!isAstNode(node)) {
41
+ return;
42
+ }
43
+ const n = node;
44
+ visitor(n);
45
+ for (const key of Object.keys(n)) {
46
+ if (SKIP_KEYS.has(key)) {
47
+ continue;
48
+ }
49
+ walkAST(n[key], visitor);
50
+ }
51
+ }
@@ -1,4 +1,4 @@
1
- import type { RuntimePage, RuntimeRoute } from "./client";
1
+ import type { RuntimePage, RuntimeRoute } from "../../client";
2
2
 
3
3
  export function isFurinPage(value: unknown): value is RuntimePage {
4
4
  return (
@@ -45,6 +45,37 @@ export function hasCycle(route: RuntimeRoute): boolean {
45
45
  return false;
46
46
  }
47
47
 
48
+ /**
49
+ * Maps over `items` with a bounded worker pool, preserving input order.
50
+ * Use whenever an `await Promise.all(items.map(...))` would open as many
51
+ * concurrent async operations as there are items — unbounded fan-out can
52
+ * exhaust file descriptors, sockets, or memory on large inputs.
53
+ */
54
+ export async function mapWithConcurrency<T, R>(
55
+ items: readonly T[],
56
+ concurrency: number,
57
+ fn: (item: T, index: number) => Promise<R>
58
+ ): Promise<R[]> {
59
+ if (items.length === 0) {
60
+ return [];
61
+ }
62
+ const results = new Array<R>(items.length);
63
+ let cursor = 0;
64
+ const workerCount = Math.min(Math.max(concurrency, 1), items.length);
65
+ await Promise.all(
66
+ Array.from({ length: workerCount }, async () => {
67
+ while (true) {
68
+ const index = cursor++;
69
+ if (index >= items.length) {
70
+ return;
71
+ }
72
+ results[index] = await fn(items[index] as T, index);
73
+ }
74
+ })
75
+ );
76
+ return results;
77
+ }
78
+
48
79
  export function validateRouteChain(
49
80
  chain: RuntimeRoute[],
50
81
  root: RuntimeRoute,
@@ -1,3 +0,0 @@
1
- import type { BuildAppOptions, TargetBuildManifest } from "../build/types.ts";
2
- import type { ResolvedRoute } from "../router.ts";
3
- export declare function buildBunTarget(routes: ResolvedRoute[], rootDir: string, buildRoot: string, rootPath: string, serverEntry: string | null, options: BuildAppOptions): Promise<TargetBuildManifest>;
@@ -1,3 +0,0 @@
1
- import type { BuildAppOptions, StaticTargetBuildManifest } from "../build/types.ts";
2
- import type { ResolvedRoute, RootLayout } from "../router.ts";
3
- export declare function buildStaticTarget(routes: ResolvedRoute[], rootDir: string, buildRoot: string, root: RootLayout, options: BuildAppOptions): Promise<StaticTargetBuildManifest>;
@@ -1,25 +0,0 @@
1
- import type { ResolvedRoute } from "../router";
2
- import type { BuildClientOptions } from "./types";
3
- export interface BuildClientResult {
4
- /** Public paths of all CSS chunks, e.g. `["/_client/chunk-abc.css"]` */
5
- cssChunks: string[];
6
- /** Public path of the JS entry chunk, e.g. `/_client/chunk-abc.js` */
7
- entryChunk: string;
8
- }
9
- /**
10
- * Builds the production client bundle via Bun.build() using _hydrate.tsx as
11
- * the JS entrypoint (NOT an HTML entrypoint). Bun produces:
12
- * <outDir>/client/chunk-*.js — code-split bundles
13
- * <outDir>/client/chunk-*.css — extracted CSS (if imported)
14
- *
15
- * Returns the chunk paths so the caller can compute a `buildId` and write
16
- * `index.html` with the correct meta tag. Using an HTML entrypoint with
17
- * code-splitting causes a Bun bug where the output index.html references a
18
- * leaf chunk instead of the actual entry chunk, preventing React from mounting
19
- * in production.
20
- *
21
- * The output index.html is NOT served to browsers directly. The server reads
22
- * it as an SSR template, injects the pre-rendered React HTML into
23
- * <!--ssr-outlet-->, and sends the complete page.
24
- */
25
- export declare function buildClient(routes: ResolvedRoute[], { outDir, rootLayout, plugins, publicPath, basePath }: BuildClientOptions): Promise<BuildClientResult>;
@@ -1,23 +0,0 @@
1
- export interface CompileEntryOptions {
2
- buildId?: string;
3
- embed?: {
4
- clientDir: string;
5
- };
6
- outDir: string;
7
- publicDir?: string;
8
- rootPath: string;
9
- routes: Array<{
10
- mode: "ssr" | "ssg" | "isr";
11
- path: string;
12
- pattern: string;
13
- }>;
14
- serverEntry: string;
15
- }
16
- /**
17
- * Generates a single `_compile-entry.ts` that:
18
- * 1. Statically imports every page module so Bun bundles them into the binary
19
- * 2. Optionally embeds client assets via `with { type: "file" }` (embed mode)
20
- * 3. Sets production mode and registers everything in a single CompileContext
21
- * 4. Dynamically imports server.ts to boot the app
22
- */
23
- export declare function generateCompileEntry(options: CompileEntryOptions): string;
@@ -1,14 +0,0 @@
1
- export interface EntryTemplateOptions {
2
- buildId?: string;
3
- extraContext?: string[];
4
- extraImports?: string[];
5
- headerComment: string;
6
- rootPath: string;
7
- routes: Array<{
8
- mode: "ssr" | "ssg" | "isr";
9
- path: string;
10
- pattern: string;
11
- }>;
12
- serverEntry: string;
13
- }
14
- export declare function buildEntrySource(options: EntryTemplateOptions): string;
@@ -1,24 +0,0 @@
1
- import type { ResolvedRoute } from "../router";
2
- import type { BuildClientOptions } from "./types";
3
- /**
4
- * Generates the client hydration entry.
5
- *
6
- * Renders into <div id="root"> (the SSR outlet element) and retains the React
7
- * root across hot reloads via import.meta.hot.data.root so React Fast Refresh
8
- * applies in-place instead of remounting.
9
- *
10
- * @param routes - Resolved routes to include in the hydration manifest.
11
- * @param rootLayout - Absolute path to the root layout module.
12
- * @param basePath - Optional sub-path prefix for static deployments (e.g. "/furin").
13
- * When set, the generated code strips the prefix from `window.location.pathname`
14
- * before matching routes, and passes `basePath` to `RouterProvider` so SPA
15
- * navigation uses the correct physical URLs.
16
- */
17
- export declare function generateHydrateEntry(routes: ResolvedRoute[], rootLayout: string, basePath: string): string;
18
- /**
19
- * Writes _hydrate.tsx + index.html to outDir for dev (Bun HMR) mode.
20
- *
21
- * Only rewrites a file when its content has actually changed so Bun's --hot
22
- * watcher does not trigger a spurious reload on every server restart.
23
- */
24
- export declare function writeDevFiles(routes: ResolvedRoute[], { outDir, rootLayout, basePath }: BuildClientOptions, projectRoot: string): void;
@@ -1,7 +0,0 @@
1
- import type { BuildAppOptions, BuildAppResult } from "./types";
2
- export { buildClient } from "./client";
3
- export { writeDevFiles } from "./hydrate";
4
- export { patternToTypeString, schemaToTypeString, writeRouteTypes } from "./route-types";
5
- export type { BuildAppOptions, BuildAppResult, BuildClientOptions, BuildManifest, BuildRouteManifestEntry, TargetBuildManifest, } from "./types";
6
- export declare const BUILD_OUTPUT_DIR = ".furin/build";
7
- export declare function buildApp(options: BuildAppOptions): Promise<BuildAppResult>;