@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/build/route-types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import type
|
|
3
|
+
import { mergeRouteSchemas, type ResolvedRoute } from "../server/router/index.ts";
|
|
4
4
|
|
|
5
5
|
/** @internal Exported for unit testing only. */
|
|
6
6
|
export function patternToTypeString(pattern: string): string {
|
|
@@ -17,12 +17,20 @@ export function patternToTypeString(pattern: string): string {
|
|
|
17
17
|
* @internal Exported for unit testing only.
|
|
18
18
|
*/
|
|
19
19
|
export function schemaToTypeString(schema: unknown): string {
|
|
20
|
+
return schemaToTypeStringWithDefaults(schema, true);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function schemaToTypeStringWithDefaults(schema: unknown, defaultsAreRequired: boolean): string {
|
|
20
24
|
if (!schema || typeof schema !== "object") {
|
|
21
25
|
return "unknown";
|
|
22
26
|
}
|
|
23
27
|
const s = schema as Record<string, unknown>;
|
|
24
28
|
if (s.anyOf && Array.isArray(s.anyOf)) {
|
|
25
|
-
const parts =
|
|
29
|
+
const parts: string[] = [];
|
|
30
|
+
for (const item of s.anyOf as unknown[]) {
|
|
31
|
+
const t = schemaToTypeStringWithDefaults(item, defaultsAreRequired);
|
|
32
|
+
if (t !== "null") parts.push(t);
|
|
33
|
+
}
|
|
26
34
|
return parts.join(" | ") || "unknown";
|
|
27
35
|
}
|
|
28
36
|
switch (s.type) {
|
|
@@ -35,13 +43,21 @@ export function schemaToTypeString(schema: unknown): string {
|
|
|
35
43
|
return "boolean";
|
|
36
44
|
case "null":
|
|
37
45
|
return "null";
|
|
46
|
+
case "array": {
|
|
47
|
+
const inner = schemaToTypeStringWithDefaults(s.items, defaultsAreRequired);
|
|
48
|
+
return inner.includes("|") || inner.includes("&") ? `(${inner})[]` : `${inner}[]`;
|
|
49
|
+
}
|
|
38
50
|
case "object": {
|
|
39
51
|
if (!s.properties || typeof s.properties !== "object") {
|
|
40
52
|
return "Record<string, unknown>";
|
|
41
53
|
}
|
|
42
54
|
const required = new Set<string>(Array.isArray(s.required) ? (s.required as string[]) : []);
|
|
43
55
|
const props = Object.entries(s.properties as Record<string, unknown>)
|
|
44
|
-
.map(([k, v]) =>
|
|
56
|
+
.map(([k, v]) => {
|
|
57
|
+
const isPresent =
|
|
58
|
+
required.has(k) || (defaultsAreRequired && hasNonNullSchemaDefault(v));
|
|
59
|
+
return `${k}${isPresent ? "" : "?"}: ${schemaToTypeStringWithDefaults(v, defaultsAreRequired)}`;
|
|
60
|
+
})
|
|
45
61
|
.join("; ");
|
|
46
62
|
return `{ ${props} }`;
|
|
47
63
|
}
|
|
@@ -50,6 +66,35 @@ export function schemaToTypeString(schema: unknown): string {
|
|
|
50
66
|
}
|
|
51
67
|
}
|
|
52
68
|
|
|
69
|
+
/** @internal Exported for unit testing only. */
|
|
70
|
+
export function schemaToInputTypeString(schema: unknown): string {
|
|
71
|
+
return schemaToTypeStringWithDefaults(schema, false);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function hasNonNullSchemaDefault(schema: unknown): boolean {
|
|
75
|
+
if (!schema || typeof schema !== "object") {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const s = schema as Record<string, unknown>;
|
|
79
|
+
return (
|
|
80
|
+
"default" in s &&
|
|
81
|
+
s.default != null
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function tagKeyToPropertyName(tag: string): string {
|
|
86
|
+
return /^[$A-Z_a-z][$\w]*$/.test(tag) ? tag : JSON.stringify(tag);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function tagToStringLiteral(tag: string): string {
|
|
90
|
+
// Use JSON.stringify for robust escaping of control chars, unicode
|
|
91
|
+
// separators, backslashes and quotes, then convert double quotes to single.
|
|
92
|
+
const json = JSON.stringify(tag);
|
|
93
|
+
// json is double-quoted; convert to single-quoted literal by escaping
|
|
94
|
+
// any contained single quotes and swapping the outer quotes.
|
|
95
|
+
return `'${json.slice(1, -1).replaceAll("'", "\\'")}'`;
|
|
96
|
+
}
|
|
97
|
+
|
|
53
98
|
/**
|
|
54
99
|
* Generates furin-env.d.ts at the project root — augments RouteManifest
|
|
55
100
|
* in furin/link so that <Link to="..."> has type-safe autocompletion and
|
|
@@ -60,15 +105,26 @@ export function schemaToTypeString(schema: unknown): string {
|
|
|
60
105
|
*/
|
|
61
106
|
/** @internal Exported for unit testing only. */
|
|
62
107
|
export function writeRouteTypes(routes: ResolvedRoute[], projectRoot: string): void {
|
|
63
|
-
const
|
|
108
|
+
const sortedRoutes = [...routes].sort((a, b) => a.pattern.localeCompare(b.pattern));
|
|
109
|
+
const entries = sortedRoutes.map((r) => {
|
|
64
110
|
const typeKey = patternToTypeString(r.pattern);
|
|
65
111
|
const isDynamic = typeKey.startsWith("`");
|
|
66
|
-
const querySchema = r.routeChain
|
|
112
|
+
const querySchema = mergeRouteSchemas(r.routeChain ?? [], "query");
|
|
67
113
|
const searchType = querySchema ? schemaToTypeString(querySchema) : "never";
|
|
114
|
+
const searchInputType = querySchema ? schemaToInputTypeString(querySchema) : "never";
|
|
68
115
|
return isDynamic
|
|
69
|
-
? ` [key: ${typeKey}]: { search?: ${searchType} }`
|
|
70
|
-
: ` ${typeKey}: { search?: ${searchType} }`;
|
|
116
|
+
? ` [key: ${typeKey}]: { search?: ${searchType}; searchInput?: ${searchInputType} }`
|
|
117
|
+
: ` ${typeKey}: { search?: ${searchType}; searchInput?: ${searchInputType} }`;
|
|
71
118
|
});
|
|
119
|
+
const routeManifestEntries = entries.join(";\n");
|
|
120
|
+
|
|
121
|
+
const sortedTags = [...new Set(routes.flatMap((route) => route.tags ?? []))].sort();
|
|
122
|
+
const tagBlock =
|
|
123
|
+
sortedTags.length > 0
|
|
124
|
+
? `\n\ndeclare module "@teyik0/furin" {\n interface FurinCacheTags {\n${sortedTags
|
|
125
|
+
.map((tag) => ` ${tagKeyToPropertyName(tag)}: ${tagToStringLiteral(tag)};`)
|
|
126
|
+
.join("\n")}\n }\n}`
|
|
127
|
+
: "";
|
|
72
128
|
|
|
73
129
|
const content = `// Auto-generated by Furin. Do not edit manually.
|
|
74
130
|
/// <reference types="@teyik0/furin/env" />
|
|
@@ -76,9 +132,9 @@ import "@teyik0/furin/link";
|
|
|
76
132
|
|
|
77
133
|
declare module "@teyik0/furin/link" {
|
|
78
134
|
interface RouteManifest {
|
|
79
|
-
${
|
|
135
|
+
${routeManifestEntries};
|
|
80
136
|
}
|
|
81
|
-
}
|
|
137
|
+
}${tagBlock}
|
|
82
138
|
`;
|
|
83
139
|
|
|
84
140
|
const typesPath = join(projectRoot, "furin-env.d.ts");
|
package/src/build/scan-server.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface AstNode {
|
|
6
|
-
type: string;
|
|
7
|
-
[key: string]: unknown;
|
|
8
|
-
}
|
|
2
|
+
import { parse, type CallExpression, type ObjectExpression, type ObjectProperty } from "yuku-parser";
|
|
3
|
+
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
4
|
+
import { walkAST } from "../shared/utils/ast-walk.ts";
|
|
9
5
|
|
|
10
6
|
/**
|
|
11
7
|
* Statically scans a server entry file and returns all `pagesDir` string
|
|
@@ -16,31 +12,45 @@ interface AstNode {
|
|
|
16
12
|
*/
|
|
17
13
|
export function scanFurinInstances(serverEntryPath: string): string[] {
|
|
18
14
|
const code = readFileSync(serverEntryPath, "utf8");
|
|
19
|
-
const
|
|
20
|
-
|
|
15
|
+
const lang = detectLangFromPath(serverEntryPath);
|
|
16
|
+
|
|
17
|
+
// Declaration files contain no runtime code — skip parsing.
|
|
18
|
+
if (lang === "dts") {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { program, diagnostics } = parse(code, { sourceType: "module", lang });
|
|
23
|
+
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
24
|
+
if (firstError) {
|
|
25
|
+
console.error("[furin] scan-server: parse error:", firstError.message, "in", serverEntryPath);
|
|
21
26
|
return [];
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
const results: string[] = [];
|
|
25
|
-
|
|
30
|
+
walkAST(program, (node) => {
|
|
31
|
+
if (node.type === "CallExpression") {
|
|
32
|
+
checkFurinCall(node as unknown as CallExpression, results);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
26
35
|
return results;
|
|
27
36
|
}
|
|
28
37
|
|
|
29
|
-
const SKIP_KEYS = new Set(["type", "start", "end"]);
|
|
30
|
-
|
|
31
38
|
/** Checks whether `node` is a `furin({ pagesDir: "..." })` call and, if so, pushes the value. */
|
|
32
|
-
function checkFurinCall(node:
|
|
33
|
-
const
|
|
34
|
-
const args = node.arguments as AstNode[] | undefined;
|
|
35
|
-
const isFurinCall =
|
|
36
|
-
callee?.type === "Identifier" && (callee as { name?: string }).name === "furin";
|
|
39
|
+
function checkFurinCall(node: CallExpression, out: string[]): void {
|
|
40
|
+
const isFurinCall = node.callee.type === "Identifier" && node.callee.name === "furin";
|
|
37
41
|
|
|
38
|
-
if (!
|
|
42
|
+
if (!isFurinCall || node.arguments.length === 0) {
|
|
39
43
|
return;
|
|
40
44
|
}
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
// Unwrap TS expression wrappers like `furin({...} as Config)` or
|
|
47
|
+
// `furin({...} satisfies Options)` so we still see the ObjectExpression.
|
|
48
|
+
const firstArgNode = node.arguments[0];
|
|
49
|
+
if (!firstArgNode) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const firstArg = unwrapTSExpression(firstArgNode);
|
|
53
|
+
if (!isObjectExpressionNode(firstArg)) {
|
|
44
54
|
return;
|
|
45
55
|
}
|
|
46
56
|
|
|
@@ -50,61 +60,36 @@ function checkFurinCall(node: AstNode, out: string[]): void {
|
|
|
50
60
|
}
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
function isObjectExpressionNode(node: { type: string }): node is ObjectExpression {
|
|
64
|
+
return node.type === "ObjectExpression";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function extractStringProperty(obj: ObjectExpression, propName: string): string | null {
|
|
68
|
+
for (const prop of obj.properties) {
|
|
69
|
+
if (prop.type !== "Property") {
|
|
57
70
|
continue;
|
|
58
71
|
}
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
walkNode(item as AstNode, out);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
} else if (child && typeof child === "object") {
|
|
67
|
-
walkNode(child as AstNode, out);
|
|
72
|
+
const value = getStringPropertyValue(prop, propName);
|
|
73
|
+
|
|
74
|
+
if (value !== undefined) {
|
|
75
|
+
return value;
|
|
68
76
|
}
|
|
69
77
|
}
|
|
78
|
+
return null;
|
|
70
79
|
}
|
|
71
80
|
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (node.type === "CallExpression") {
|
|
77
|
-
checkFurinCall(node, out);
|
|
78
|
-
}
|
|
79
|
-
walkChildren(node, out);
|
|
80
|
-
}
|
|
81
|
+
function getStringPropertyValue(prop: ObjectProperty, propName: string): string | null | undefined {
|
|
82
|
+
const keyMatches =
|
|
83
|
+
(prop.key.type === "Identifier" && prop.key.name === propName) ||
|
|
84
|
+
(prop.key.type === "Literal" && prop.key.value === propName);
|
|
81
85
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!Array.isArray(properties)) {
|
|
85
|
-
return null;
|
|
86
|
+
if (!keyMatches) {
|
|
87
|
+
return undefined;
|
|
86
88
|
}
|
|
87
89
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
const key = prop.key as AstNode & { name?: string; value?: unknown };
|
|
93
|
-
const value = prop.value as AstNode & { value?: unknown };
|
|
94
|
-
|
|
95
|
-
const keyMatches =
|
|
96
|
-
(key.type === "Identifier" && key.name === propName) ||
|
|
97
|
-
(key.type === "Literal" && key.value === propName);
|
|
98
|
-
|
|
99
|
-
if (!keyMatches) {
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Only accept string literals — ignore template literals, identifiers, etc.
|
|
104
|
-
if (value?.type === "Literal" && typeof value.value === "string") {
|
|
105
|
-
return value.value;
|
|
106
|
-
}
|
|
107
|
-
return null; // dynamic path — silently skip
|
|
90
|
+
// Only accept string literals — ignore template literals, identifiers, etc.
|
|
91
|
+
if (prop.value.type === "Literal" && typeof prop.value.value === "string") {
|
|
92
|
+
return prop.value.value;
|
|
108
93
|
}
|
|
109
|
-
return null;
|
|
94
|
+
return null; // dynamic path — silently skip
|
|
110
95
|
}
|
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import { writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { buildEntrySource } from "./entry-template";
|
|
3
|
+
import { buildEntrySource, type BuildEntryOptions } from "./entry-template";
|
|
4
4
|
import { ensureDir } from "./shared";
|
|
5
5
|
|
|
6
|
-
export interface ServerRoutesEntryOptions {
|
|
7
|
-
buildId?: string;
|
|
8
|
-
outDir: string;
|
|
9
|
-
rootPath: string;
|
|
10
|
-
routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
|
|
11
|
-
serverEntry: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
6
|
/**
|
|
15
7
|
* Generates `server.ts` — an intermediate entry used to produce `server.js`.
|
|
16
8
|
*
|
|
@@ -22,8 +14,9 @@ export interface ServerRoutesEntryOptions {
|
|
|
22
14
|
* This file is intermediate: `adapter/bun.ts` runs `Bun.build()` on it to produce
|
|
23
15
|
* the self-contained `server.js` bundle, then deletes this file.
|
|
24
16
|
*/
|
|
25
|
-
export function generateServerRoutesEntry(options:
|
|
26
|
-
const { buildId, outDir, rootPath, routes, serverEntry } =
|
|
17
|
+
export function generateServerRoutesEntry(options: BuildEntryOptions): string {
|
|
18
|
+
const { buildId, outDir, rootPath, routes, serverEntry, rootConventions, routeMetadata, ssgCache } =
|
|
19
|
+
options;
|
|
27
20
|
ensureDir(outDir);
|
|
28
21
|
const source = buildEntrySource({
|
|
29
22
|
buildId,
|
|
@@ -31,6 +24,9 @@ export function generateServerRoutesEntry(options: ServerRoutesEntryOptions): st
|
|
|
31
24
|
rootPath,
|
|
32
25
|
routes,
|
|
33
26
|
serverEntry,
|
|
27
|
+
rootConventions,
|
|
28
|
+
routeMetadata,
|
|
29
|
+
ssgCache,
|
|
34
30
|
});
|
|
35
31
|
|
|
36
32
|
// Named "server.ts" so Bun.build() outputs "server.js"
|
package/src/build/shared.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { cpSync, existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
|
|
2
2
|
import { join, relative } from "node:path";
|
|
3
3
|
import type { BuildTarget } from "../config";
|
|
4
|
-
import type { ResolvedRoute } from "../router";
|
|
4
|
+
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
5
5
|
import type { BuildRouteManifestEntry, TargetBuildManifest } from "./types";
|
|
6
6
|
|
|
7
7
|
// import.meta.dir/path/url are inlined at bundle time to the OUTPUT file's
|
|
@@ -10,6 +10,7 @@ import type { BuildRouteManifestEntry, TargetBuildManifest } from "./types";
|
|
|
10
10
|
// exports. This is the Web-standard approach (Bun, Node 20.6+, browsers).
|
|
11
11
|
export const CLIENT_MODULE_PATH = new URL(import.meta.resolve("@teyik0/furin/client")).pathname;
|
|
12
12
|
export const LINK_MODULE_PATH = new URL(import.meta.resolve("@teyik0/furin/link")).pathname;
|
|
13
|
+
export const SEARCH_MODULE_PATH = new URL(import.meta.resolve("@teyik0/furin/search")).pathname;
|
|
13
14
|
|
|
14
15
|
export function ensureDir(path: string): void {
|
|
15
16
|
if (!existsSync(path)) {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SsgCacheEntry } from "../server/cache/index.ts";
|
|
2
|
+
import { prerenderSSG } from "../server/render/index.ts";
|
|
3
|
+
import { createSearchRouteMetadata, type ResolvedRoute, type RootLayout } from "../server/router/index.ts";
|
|
4
|
+
import { resolvePath } from "../server/render/assemble.ts";
|
|
5
|
+
|
|
6
|
+
export type SSGCacheSnapshot = Record<string, SsgCacheEntry>;
|
|
7
|
+
|
|
8
|
+
export async function buildSSGCacheSnapshot(
|
|
9
|
+
routes: ResolvedRoute[],
|
|
10
|
+
root: RootLayout,
|
|
11
|
+
origin: string
|
|
12
|
+
): Promise<SSGCacheSnapshot> {
|
|
13
|
+
const snapshot: SSGCacheSnapshot = {};
|
|
14
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
15
|
+
|
|
16
|
+
for (const route of routes) {
|
|
17
|
+
if (route.mode !== "ssg" || !route.page.staticParams) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const paramSets = await route.page.staticParams();
|
|
22
|
+
for (const params of paramSets) {
|
|
23
|
+
const entry = await prerenderSSG(route, params, root, origin, undefined, searchRoutes);
|
|
24
|
+
if (entry instanceof Response) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
snapshot[resolvePath(route.pattern, params)] = entry;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return snapshot;
|
|
32
|
+
}
|
package/src/build/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BuildTarget, StaticExportConfig } from "../config";
|
|
2
|
-
import type { ResolvedRoute } from "../router";
|
|
2
|
+
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
3
3
|
|
|
4
4
|
export interface BuildClientOptions {
|
|
5
5
|
/**
|
|
@@ -8,6 +8,8 @@ export interface BuildClientOptions {
|
|
|
8
8
|
* correct physical URLs. Pass "" for root deployments.
|
|
9
9
|
*/
|
|
10
10
|
basePath: string;
|
|
11
|
+
/** Inject the evlog client logger into the hydrate entry. Off by default. */
|
|
12
|
+
clientLogging: boolean;
|
|
11
13
|
outDir: string;
|
|
12
14
|
pagesDir?: string;
|
|
13
15
|
plugins?: Bun.BunPlugin[];
|
|
@@ -53,6 +55,8 @@ export interface BuildManifest {
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
export interface BuildAppOptions {
|
|
58
|
+
/** Inject the evlog client logger into the hydrate entry. Defaults to false. */
|
|
59
|
+
clientLogging?: boolean;
|
|
56
60
|
compile?: "server" | "embed";
|
|
57
61
|
pagesDir?: string;
|
|
58
62
|
plugins?: Bun.BunPlugin[];
|
package/src/cli/index.ts
CHANGED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { Component, Fragment, type ReactNode } from "react";
|
|
2
|
+
import { computeErrorDigest } from "../shared/digest.ts";
|
|
3
|
+
import { type ErrorComponent, getPublicErrorMessage } from "../shared/error.ts";
|
|
4
|
+
import { isNotFoundError, type NotFoundComponent } from "../shared/not-found.ts";
|
|
5
|
+
import { DefaultErrorFallback, DefaultNotFoundFallback } from "./default-screens.tsx";
|
|
6
|
+
import { isFurinServerError } from "./server-error.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default HTTP status surfaced to error UIs when the caught error has no
|
|
10
|
+
* intrinsic status (e.g. a thrown `Error`, a non-Error throw). The loader
|
|
11
|
+
* pipeline produces a `FurinServerError` with the right status when a thrown
|
|
12
|
+
* `Response` is the cause; everything else collapses to 500.
|
|
13
|
+
*/
|
|
14
|
+
const DEFAULT_ERROR_STATUS = 500;
|
|
15
|
+
|
|
16
|
+
const SERVER_RESET_NOOP = () => {
|
|
17
|
+
/* reset is a client-only action; on the server the response is already committed */
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
interface ErrorBoundaryProps {
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
/**
|
|
23
|
+
* Server-provided digest for an error rendered server-side. Honoured only
|
|
24
|
+
* for the FIRST error this boundary surfaces post-hydration — i.e. when the
|
|
25
|
+
* latched error is plausibly the same one the server already logged. After
|
|
26
|
+
* any reset, or when subsequent client-side errors are caught, the boundary
|
|
27
|
+
* computes a fresh digest from the actual caught error so distinct failures
|
|
28
|
+
* keep distinct IDs.
|
|
29
|
+
*/
|
|
30
|
+
digest?: string;
|
|
31
|
+
/** Component rendered when an error is caught. Omit to use the built-in default. */
|
|
32
|
+
fallback?: ErrorComponent;
|
|
33
|
+
/**
|
|
34
|
+
* Invoked AFTER the boundary has cleared its local error state. Slice 7
|
|
35
|
+
* wires this to `router.navigate(currentHref, { force: true })` so the
|
|
36
|
+
* loader re-runs.
|
|
37
|
+
*/
|
|
38
|
+
onReset?: () => void;
|
|
39
|
+
/**
|
|
40
|
+
* When this value changes the boundary clears its error state, effectively
|
|
41
|
+
* retrying the render of `children`. Slice 7 drives this from router
|
|
42
|
+
* navigation success.
|
|
43
|
+
*/
|
|
44
|
+
resetKey?: string | number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface ErrorBoundaryState {
|
|
48
|
+
/**
|
|
49
|
+
* Digest computed at the moment the error was latched. Stored in state so
|
|
50
|
+
* each distinct caught error gets its own ID, instead of all of them
|
|
51
|
+
* inheriting the server-provided `digest` prop.
|
|
52
|
+
*/
|
|
53
|
+
digest: string | null;
|
|
54
|
+
/** Unmount/remount counter — bumped on reset to force React to discard
|
|
55
|
+
* the previous subtree (including any broken state). */
|
|
56
|
+
epoch: number;
|
|
57
|
+
error: Error | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Catches generic errors thrown during render of `children`. Lets
|
|
62
|
+
* `FurinNotFoundError` bubble past so a sibling `<FurinNotFoundBoundary>`
|
|
63
|
+
* can handle it.
|
|
64
|
+
*
|
|
65
|
+
* Must be a class: React error catching (`getDerivedStateFromError`,
|
|
66
|
+
* `componentDidCatch`) has no function-component equivalent.
|
|
67
|
+
*/
|
|
68
|
+
// biome-ignore lint/style/useReactFunctionComponents: React error boundaries require a class component.
|
|
69
|
+
export class FurinErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
70
|
+
override state: ErrorBoundaryState = {
|
|
71
|
+
digest: null,
|
|
72
|
+
epoch: 0,
|
|
73
|
+
error: null,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> {
|
|
77
|
+
// Compute the digest right at catch-time so the value is anchored to THIS
|
|
78
|
+
// specific error instance. Doing it here (vs. lazily in render) also means
|
|
79
|
+
// a re-render with the same latched error keeps the same ID.
|
|
80
|
+
return { digest: computeErrorDigest(error), error };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
override componentDidUpdate(prevProps: ErrorBoundaryProps) {
|
|
84
|
+
if (prevProps.resetKey !== this.props.resetKey && this.state.error) {
|
|
85
|
+
this.reset();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
reset = () => {
|
|
90
|
+
// setState is async; running onReset BEFORE the state has actually flipped
|
|
91
|
+
// would expose stale `state.error` to anything the callback transitively
|
|
92
|
+
// reads (e.g. router.refresh re-rendering this very subtree). The second
|
|
93
|
+
// argument to setState fires after the commit, which is what we want.
|
|
94
|
+
this.setState(
|
|
95
|
+
(s) => ({ digest: null, epoch: s.epoch + 1, error: null }),
|
|
96
|
+
() => {
|
|
97
|
+
this.props.onReset?.();
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
override render(): ReactNode {
|
|
103
|
+
const { error, epoch, digest } = this.state;
|
|
104
|
+
if (error) {
|
|
105
|
+
// notFound() is a control-flow signal — bubble it up to the nearest
|
|
106
|
+
// FurinNotFoundBoundary rather than displaying an error UI for it.
|
|
107
|
+
if (isNotFoundError(error)) {
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
const Fallback = this.props.fallback ?? DefaultErrorFallback;
|
|
111
|
+
// Digest precedence:
|
|
112
|
+
// 1. FurinServerError.digest — when the server emitted an error
|
|
113
|
+
// sentinel through the SPA-nav payload, the synthesized error
|
|
114
|
+
// carries the server's own digest so logs correlate without
|
|
115
|
+
// recomputing a different hash from a synthetic stack.
|
|
116
|
+
// 2. state.digest — computed at catch time, always correct for the
|
|
117
|
+
// ACTUAL caught error.
|
|
118
|
+
// 3. props.digest — the server-rendered digest, only meaningful for
|
|
119
|
+
// the very first error post-hydration; we still honour it as a
|
|
120
|
+
// last-resort fallback in case state.digest is somehow missing
|
|
121
|
+
// (e.g. tests that inject state by hand).
|
|
122
|
+
// 4. recompute on the spot — defensive, should never be reached.
|
|
123
|
+
const serverDigest = isFurinServerError(error) ? error.digest : undefined;
|
|
124
|
+
const finalDigest = serverDigest ?? digest ?? this.props.digest ?? computeErrorDigest(error);
|
|
125
|
+
const message = this.props.fallback ? error.message : getPublicErrorMessage(error);
|
|
126
|
+
// Status precedence: FurinServerError carries the original Response.status
|
|
127
|
+
// (or the loader's computed 500 for non-Response throws); everything else
|
|
128
|
+
// collapses to the default 500.
|
|
129
|
+
const status = isFurinServerError(error) ? error.status : DEFAULT_ERROR_STATUS;
|
|
130
|
+
return (
|
|
131
|
+
<Fallback
|
|
132
|
+
error={{ message, digest: finalDigest, status }}
|
|
133
|
+
reset={typeof window === "undefined" ? SERVER_RESET_NOOP : this.reset}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
// The `key` trick ensures that after a reset, children remount fresh
|
|
138
|
+
// rather than retaining stale state from the failed render.
|
|
139
|
+
return <Fragment key={epoch}>{this.props.children}</Fragment>;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface NotFoundBoundaryProps {
|
|
144
|
+
children: ReactNode;
|
|
145
|
+
fallback?: NotFoundComponent;
|
|
146
|
+
resetKey?: string | number;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface NotFoundBoundaryState {
|
|
150
|
+
epoch: number;
|
|
151
|
+
error: Error | null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Catches `FurinNotFoundError` (thrown by `notFound()`) and renders the
|
|
156
|
+
* nearest not-found UI. Generic errors are re-thrown from render() so a
|
|
157
|
+
* parent `<FurinErrorBoundary>` can handle them.
|
|
158
|
+
*
|
|
159
|
+
* Must be a class: React error catching has no function-component equivalent.
|
|
160
|
+
*/
|
|
161
|
+
// biome-ignore lint/style/useReactFunctionComponents: React error boundaries require a class component.
|
|
162
|
+
export class FurinNotFoundBoundary extends Component<NotFoundBoundaryProps, NotFoundBoundaryState> {
|
|
163
|
+
override state: NotFoundBoundaryState = { epoch: 0, error: null };
|
|
164
|
+
|
|
165
|
+
static getDerivedStateFromError(error: Error): Partial<NotFoundBoundaryState> {
|
|
166
|
+
return { error };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
override componentDidUpdate(prevProps: NotFoundBoundaryProps) {
|
|
170
|
+
if (prevProps.resetKey !== this.props.resetKey && this.state.error) {
|
|
171
|
+
this.setState((s) => ({ error: null, epoch: s.epoch + 1 }));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
override render(): ReactNode {
|
|
176
|
+
const { error, epoch } = this.state;
|
|
177
|
+
if (error) {
|
|
178
|
+
if (!isNotFoundError(error)) {
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
const Fallback = this.props.fallback ?? DefaultNotFoundFallback;
|
|
182
|
+
return <Fallback error={{ message: error.message, data: error.data }} />;
|
|
183
|
+
}
|
|
184
|
+
return <Fragment key={epoch}>{this.props.children}</Fragment>;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Options passed to `wrapSegmentBoundaries` when running on the client.
|
|
190
|
+
* The server path omits them entirely.
|
|
191
|
+
*/
|
|
192
|
+
export interface BoundaryOptions {
|
|
193
|
+
onReset?: () => void;
|
|
194
|
+
resetKey?: string | number;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Minimal shape of a segment boundary descriptor, accepted by both server-side
|
|
199
|
+
* `SegmentBoundary` and client-side `ClientSegmentBoundary`.
|
|
200
|
+
*/
|
|
201
|
+
export interface SegmentBoundaryLike {
|
|
202
|
+
error?: ErrorComponent;
|
|
203
|
+
notFound?: NotFoundComponent;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Wraps `inner` with the boundary pair declared at a single segment depth.
|
|
208
|
+
*
|
|
209
|
+
* The ordering is deliberate and MUST be kept identical on server **and**
|
|
210
|
+
* client so that hydration sees the exact same React tree:
|
|
211
|
+
*
|
|
212
|
+
* <FurinErrorBoundary>
|
|
213
|
+
* <FurinNotFoundBoundary>{inner}</FurinNotFoundBoundary>
|
|
214
|
+
* </FurinErrorBoundary>
|
|
215
|
+
*
|
|
216
|
+
* `FurinErrorBoundary` is OUTSIDE because it re-throws `FurinNotFoundError`
|
|
217
|
+
* during render, allowing an ancestor not-found boundary to catch it. Placing
|
|
218
|
+
* the not-found boundary inside would mean the same-depth error boundary
|
|
219
|
+
* catches a `notFound()` throw first, latches onto it, and the re-throw would
|
|
220
|
+
* not find a `NotFoundBoundary` deeper than itself — it needs one higher up.
|
|
221
|
+
*
|
|
222
|
+
* @param inner - React subtree to wrap.
|
|
223
|
+
* @param segment - Boundary descriptor for this depth (may be undefined).
|
|
224
|
+
* @param options - Optional client-side reset options (ignored on server).
|
|
225
|
+
* @returns The wrapped React node.
|
|
226
|
+
*/
|
|
227
|
+
export function wrapSegmentBoundaries(
|
|
228
|
+
inner: ReactNode,
|
|
229
|
+
segment: SegmentBoundaryLike | undefined,
|
|
230
|
+
options: BoundaryOptions | undefined
|
|
231
|
+
): ReactNode {
|
|
232
|
+
if (!segment) {
|
|
233
|
+
return inner;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let wrapped: ReactNode = inner;
|
|
237
|
+
|
|
238
|
+
if (segment.notFound) {
|
|
239
|
+
wrapped = (
|
|
240
|
+
<FurinNotFoundBoundary fallback={segment.notFound} resetKey={options?.resetKey}>
|
|
241
|
+
{wrapped}
|
|
242
|
+
</FurinNotFoundBoundary>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (segment.error) {
|
|
247
|
+
wrapped = (
|
|
248
|
+
<FurinErrorBoundary
|
|
249
|
+
fallback={segment.error}
|
|
250
|
+
onReset={options?.onReset}
|
|
251
|
+
resetKey={options?.resetKey}
|
|
252
|
+
>
|
|
253
|
+
{wrapped}
|
|
254
|
+
</FurinErrorBoundary>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return wrapped;
|
|
259
|
+
}
|