@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
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import MagicString from "magic-string";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
compilerOptions: {
|
|
14
|
-
jsx: "react",
|
|
15
|
-
jsxFactory: "React.createElement",
|
|
16
|
-
jsxFragmentFactory: "React.Fragment",
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
});
|
|
2
|
+
import {
|
|
3
|
+
type CallExpression,
|
|
4
|
+
type ImportDeclaration,
|
|
5
|
+
type ObjectExpression,
|
|
6
|
+
type ObjectProperty,
|
|
7
|
+
type Program,
|
|
8
|
+
parse,
|
|
9
|
+
type SourceLang,
|
|
10
|
+
} from "yuku-parser";
|
|
11
|
+
import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
|
|
12
|
+
import { type AstNode, walkAST } from "../shared/utils/ast-walk.ts";
|
|
20
13
|
|
|
21
14
|
// loader: data fetching (runs on server only)
|
|
22
15
|
// query / params: Elysia TypeBox schemas — validated server-side, not used in browser
|
|
@@ -28,71 +21,6 @@ interface TransformResult {
|
|
|
28
21
|
removedServerCode: boolean;
|
|
29
22
|
}
|
|
30
23
|
|
|
31
|
-
// ---------------------------------------------------------------------------
|
|
32
|
-
// ESTree node types (minimal subset needed for our walk)
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
interface AstNode {
|
|
35
|
-
body?: AstNode[];
|
|
36
|
-
end: number;
|
|
37
|
-
start: number;
|
|
38
|
-
type: string;
|
|
39
|
-
[key: string]: unknown;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface Property extends AstNode {
|
|
43
|
-
// Identifier key: { loader: fn } → key.name === "loader"
|
|
44
|
-
// Literal key: { "loader": fn } → key.value === "loader"
|
|
45
|
-
key: AstNode & { name?: string; value?: unknown };
|
|
46
|
-
type: "Property";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
interface SpreadElement extends AstNode {
|
|
50
|
-
argument: AstNode;
|
|
51
|
-
type: "SpreadElement";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface ObjectExpression extends AstNode {
|
|
55
|
-
// properties may include SpreadElement nodes, e.g. { ...spread, loader: fn }
|
|
56
|
-
properties: Array<Property | SpreadElement>;
|
|
57
|
-
type: "ObjectExpression";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
interface CallExpression extends AstNode {
|
|
61
|
-
arguments: AstNode[];
|
|
62
|
-
callee: AstNode & { name?: string; property?: AstNode & { name?: string } };
|
|
63
|
-
type: "CallExpression";
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
interface ImportDeclaration extends AstNode {
|
|
67
|
-
specifiers: Array<AstNode & { local: AstNode & { name: string } }>;
|
|
68
|
-
type: "ImportDeclaration";
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ---------------------------------------------------------------------------
|
|
72
|
-
// AST walking
|
|
73
|
-
// ---------------------------------------------------------------------------
|
|
74
|
-
function walk(node: unknown, visitor: (n: AstNode) => void): void {
|
|
75
|
-
if (!node || typeof node !== "object") {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (Array.isArray(node)) {
|
|
79
|
-
for (const child of node) {
|
|
80
|
-
walk(child, visitor);
|
|
81
|
-
}
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const n = node as AstNode;
|
|
85
|
-
if (typeof n.type === "string") {
|
|
86
|
-
visitor(n);
|
|
87
|
-
}
|
|
88
|
-
for (const key of Object.keys(n)) {
|
|
89
|
-
if (key === "type" || key === "start" || key === "end") {
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
walk(n[key], visitor);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
24
|
// ---------------------------------------------------------------------------
|
|
97
25
|
// Check if a CallExpression is createRoute() or page() / route.page()
|
|
98
26
|
// ---------------------------------------------------------------------------
|
|
@@ -105,7 +33,11 @@ function isRoutePageCall(node: CallExpression): boolean {
|
|
|
105
33
|
if (callee.type === "Identifier" && callee.name === "page") {
|
|
106
34
|
return true;
|
|
107
35
|
}
|
|
108
|
-
if (
|
|
36
|
+
if (
|
|
37
|
+
callee.type === "MemberExpression" &&
|
|
38
|
+
callee.property.type === "Identifier" &&
|
|
39
|
+
callee.property.name === "page"
|
|
40
|
+
) {
|
|
109
41
|
return true;
|
|
110
42
|
}
|
|
111
43
|
return false;
|
|
@@ -115,12 +47,16 @@ function isTargetCall(node: CallExpression): boolean {
|
|
|
115
47
|
return isCreateRouteCall(node) || isRoutePageCall(node);
|
|
116
48
|
}
|
|
117
49
|
|
|
50
|
+
function isObjectExpressionNode(node: { type: string }): node is ObjectExpression {
|
|
51
|
+
return node.type === "ObjectExpression";
|
|
52
|
+
}
|
|
53
|
+
|
|
118
54
|
// ---------------------------------------------------------------------------
|
|
119
55
|
// Remove server-only properties from an ObjectExpression using MagicString.
|
|
120
56
|
// Returns true if any property was removed.
|
|
121
57
|
// ---------------------------------------------------------------------------
|
|
122
58
|
function removeServerProperties(s: MagicString, source: string, obj: ObjectExpression): boolean {
|
|
123
|
-
const toRemove = obj.properties.filter((p): p is
|
|
59
|
+
const toRemove = obj.properties.filter((p): p is ObjectProperty => {
|
|
124
60
|
// Skip spread elements — they have no key.
|
|
125
61
|
if (p.type !== "Property") {
|
|
126
62
|
return false;
|
|
@@ -184,47 +120,125 @@ function removeServerProperties(s: MagicString, source: string, obj: ObjectExpre
|
|
|
184
120
|
return true;
|
|
185
121
|
}
|
|
186
122
|
|
|
123
|
+
// TypeScript-specific node types that introduce a type-only scope. Any
|
|
124
|
+
// Identifier / JSXIdentifier beneath one of these nodes is a TypeScript type
|
|
125
|
+
// reference, not a runtime value reference, and must not count toward DCE refs.
|
|
126
|
+
const TYPE_SCOPE_NODES = new Set([
|
|
127
|
+
"TSTypeAnnotation", // `: Type` on variables, params, return types
|
|
128
|
+
"TSTypeParameterDeclaration", // `<T>` in generic type-param declarations
|
|
129
|
+
"TSTypeParameterInstantiation", // `<T>` in generic instantiation positions
|
|
130
|
+
"TSInterfaceDeclaration", // `interface Foo { … }` — entirely type-level
|
|
131
|
+
"TSTypeAliasDeclaration", // `type Foo = …` — entirely type-level
|
|
132
|
+
"TSTypePredicate", // `x is Type` in return-type position
|
|
133
|
+
"TSClassImplements", // `class C implements Iface` — the implements clause is type-only
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
// Walk `node` and add every Identifier / JSXIdentifier found inside it to
|
|
137
|
+
// `excluded`. Called for nodes that are entirely in type position (e.g. a
|
|
138
|
+
// TSTypeAnnotation) so their descendant identifiers are ignored by DCE.
|
|
139
|
+
function markTypeDescendants(node: unknown, excluded: Set<unknown>): void {
|
|
140
|
+
if (!node || typeof node !== "object") {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (Array.isArray(node)) {
|
|
144
|
+
for (const item of node) {
|
|
145
|
+
markTypeDescendants(item, excluded);
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const n = node as AstNode;
|
|
150
|
+
if (n.type === "Identifier" || n.type === "JSXIdentifier") {
|
|
151
|
+
excluded.add(n);
|
|
152
|
+
}
|
|
153
|
+
for (const key of Object.keys(n)) {
|
|
154
|
+
if (key === "type" || key === "start" || key === "end") {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
markTypeDescendants(n[key], excluded);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Excludes identifiers that appear in TypeScript type-only positions from the
|
|
162
|
+
// `excluded` set. Extracted from the Pass-1 walk callback to keep cognitive
|
|
163
|
+
// complexity within the allowed budget.
|
|
164
|
+
function excludeTypePositionIdentifiers(node: AstNode, excluded: Set<unknown>): void {
|
|
165
|
+
if (TYPE_SCOPE_NODES.has(node.type)) {
|
|
166
|
+
markTypeDescendants(node, excluded);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// TSAsExpression (`x as T`), TSSatisfiesExpression (`x satisfies T`), and
|
|
170
|
+
// TSTypeAssertion (`<T>x`) are mixed: the expression child is runtime; only
|
|
171
|
+
// the typeAnnotation child is type-only.
|
|
172
|
+
if (
|
|
173
|
+
node.type === "TSAsExpression" ||
|
|
174
|
+
node.type === "TSSatisfiesExpression" ||
|
|
175
|
+
node.type === "TSTypeAssertion"
|
|
176
|
+
) {
|
|
177
|
+
markTypeDescendants(node.typeAnnotation as unknown, excluded);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
187
181
|
// ---------------------------------------------------------------------------
|
|
188
|
-
// Collect all Identifier names referenced in the AST
|
|
189
|
-
// Skips identifiers
|
|
190
|
-
//
|
|
191
|
-
//
|
|
192
|
-
//
|
|
182
|
+
// Collect all Identifier / JSXIdentifier names referenced in the AST
|
|
183
|
+
// (excluding imports). Skips identifiers in non-reference positions:
|
|
184
|
+
//
|
|
185
|
+
// • Static (non-computed) object property keys — `{ loader: fn }`
|
|
186
|
+
// • Static (non-computed) member access properties — `obj.prop`
|
|
187
|
+
// • JSX attribute names — `<div className=...>`
|
|
188
|
+
// • Right-hand side of a JSXMemberExpression chain — `<UI.Button>` (Button)
|
|
189
|
+
// • TypeScript type positions — `: MyType`, `<T>`, etc.
|
|
190
|
+
//
|
|
193
191
|
// Computed keys like `{ [someVar]: v }` are left in — they ARE references.
|
|
192
|
+
// JSX tag positions ARE references — `<Link>` requires the `Link` binding
|
|
193
|
+
// to be in scope, so omitting JSXIdentifier (the default after the parser
|
|
194
|
+
// switch from Bun.Transpiler to yuku-parser) silently drops imports that
|
|
195
|
+
// are only used as JSX tags.
|
|
194
196
|
// ---------------------------------------------------------------------------
|
|
195
|
-
function collectReferencedNames(program:
|
|
197
|
+
function collectReferencedNames(program: Program): Set<string> {
|
|
196
198
|
const refs = new Set<string>();
|
|
197
|
-
// Nodes that occupy a non-reference Identifier position.
|
|
199
|
+
// Nodes that occupy a non-reference Identifier / JSXIdentifier position.
|
|
198
200
|
const excluded = new Set<unknown>();
|
|
199
201
|
|
|
200
|
-
for (const stmt of program.body
|
|
202
|
+
for (const stmt of program.body) {
|
|
201
203
|
if (stmt.type === "ImportDeclaration") {
|
|
202
204
|
continue;
|
|
203
205
|
}
|
|
204
206
|
// Pass 1 — mark non-reference identifier positions.
|
|
205
207
|
// Only exclude *static* keys (computed=false); computed keys like
|
|
206
208
|
// `{ [someVar]: v }` are genuine identifier references.
|
|
207
|
-
|
|
209
|
+
walkAST(stmt, (node) => {
|
|
208
210
|
if (node.type === "Property" && !node.computed) {
|
|
209
211
|
excluded.add(node.key);
|
|
210
212
|
}
|
|
211
213
|
if (node.type === "MemberExpression" && !node.computed) {
|
|
212
214
|
excluded.add(node.property);
|
|
213
215
|
}
|
|
216
|
+
// `<div className="...">` — the attribute *name* is a key, not a ref.
|
|
217
|
+
if (node.type === "JSXAttribute") {
|
|
218
|
+
excluded.add(node.name);
|
|
219
|
+
}
|
|
220
|
+
// `<UI.Button>` — `UI` is the live binding, `.Button` is a property
|
|
221
|
+
// lookup on it. Exclude the property side of every JSXMemberExpression
|
|
222
|
+
// so a same-named import (e.g. `import { Button } from ...`) doesn't
|
|
223
|
+
// get falsely kept alive by every `<Foo.Button>` in the file.
|
|
224
|
+
if (node.type === "JSXMemberExpression") {
|
|
225
|
+
excluded.add(node.property);
|
|
226
|
+
}
|
|
227
|
+
excludeTypePositionIdentifiers(node, excluded);
|
|
214
228
|
});
|
|
215
229
|
}
|
|
216
230
|
|
|
217
|
-
for (const stmt of program.body
|
|
231
|
+
for (const stmt of program.body) {
|
|
218
232
|
if (stmt.type === "ImportDeclaration") {
|
|
219
233
|
continue;
|
|
220
234
|
}
|
|
221
235
|
// Pass 2 — collect genuine identifier references.
|
|
222
|
-
|
|
236
|
+
walkAST(stmt, (node) => {
|
|
223
237
|
if (excluded.has(node)) {
|
|
224
238
|
return;
|
|
225
239
|
}
|
|
226
240
|
if (
|
|
227
|
-
node.type === "Identifier" &&
|
|
241
|
+
(node.type === "Identifier" || node.type === "JSXIdentifier") &&
|
|
228
242
|
typeof (node as AstNode & { name: string }).name === "string"
|
|
229
243
|
) {
|
|
230
244
|
refs.add((node as AstNode & { name: string }).name);
|
|
@@ -259,6 +273,7 @@ function removeUnusedSpecifiers(
|
|
|
259
273
|
while (removeEnd < code.length && (code[removeEnd] === "," || code[removeEnd] === " ")) {
|
|
260
274
|
removeEnd++;
|
|
261
275
|
}
|
|
276
|
+
// react-doctor-disable-next-line react-doctor/js-set-map-lookups
|
|
262
277
|
if (!code.slice(spec.end, removeEnd).includes(",")) {
|
|
263
278
|
while (removeStart > 0 && (code[removeStart - 1] === " " || code[removeStart - 1] === ",")) {
|
|
264
279
|
removeStart--;
|
|
@@ -278,19 +293,19 @@ function removeUnusedSpecifiers(
|
|
|
278
293
|
// original-source positions, which diverge from output positions whenever
|
|
279
294
|
// earlier passes have removed content.
|
|
280
295
|
// ---------------------------------------------------------------------------
|
|
281
|
-
export function deadCodeElimination(s: MagicString): MagicString {
|
|
296
|
+
export function deadCodeElimination(s: MagicString, lang: SourceLang): MagicString {
|
|
282
297
|
const code = s.toString();
|
|
283
|
-
const { program,
|
|
284
|
-
|
|
285
|
-
|
|
298
|
+
const { program, diagnostics } = parse(code, { sourceType: "module", lang });
|
|
299
|
+
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
300
|
+
if (firstError) {
|
|
301
|
+
console.error("[furin] DCE: failed to parse transformed output:", firstError.message);
|
|
286
302
|
return s;
|
|
287
303
|
}
|
|
288
304
|
|
|
289
305
|
const fresh = new MagicString(code);
|
|
290
|
-
const
|
|
291
|
-
const refs = collectReferencedNames(programNode);
|
|
306
|
+
const refs = collectReferencedNames(program);
|
|
292
307
|
|
|
293
|
-
const body =
|
|
308
|
+
const body = program.body;
|
|
294
309
|
for (let i = body.length - 1; i >= 0; i--) {
|
|
295
310
|
const stmt = body[i];
|
|
296
311
|
if (!stmt) {
|
|
@@ -320,10 +335,10 @@ export function deadCodeElimination(s: MagicString): MagicString {
|
|
|
320
335
|
// Remove server-only properties from createRoute() / page() / route.page()
|
|
321
336
|
// calls found anywhere in the AST.
|
|
322
337
|
// ---------------------------------------------------------------------------
|
|
323
|
-
function removeServerExports(s: MagicString, source: string, program:
|
|
338
|
+
function removeServerExports(s: MagicString, source: string, program: Program): boolean {
|
|
324
339
|
let removedServerCode = false;
|
|
325
340
|
|
|
326
|
-
|
|
341
|
+
walkAST(program, (node) => {
|
|
327
342
|
if (node.type !== "CallExpression") {
|
|
328
343
|
return;
|
|
329
344
|
}
|
|
@@ -331,11 +346,16 @@ function removeServerExports(s: MagicString, source: string, program: AstNode):
|
|
|
331
346
|
if (!isTargetCall(call)) {
|
|
332
347
|
return;
|
|
333
348
|
}
|
|
334
|
-
|
|
335
|
-
|
|
349
|
+
// Unwrap `createRoute({...} as Config)` / `page({...} satisfies Opts)` etc.
|
|
350
|
+
const firstArg = call.arguments[0];
|
|
351
|
+
if (!firstArg) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const arg = unwrapTSExpression(firstArg);
|
|
355
|
+
if (!isObjectExpressionNode(arg)) {
|
|
336
356
|
return;
|
|
337
357
|
}
|
|
338
|
-
if (removeServerProperties(s, source, arg
|
|
358
|
+
if (removeServerProperties(s, source, arg)) {
|
|
339
359
|
removedServerCode = true;
|
|
340
360
|
}
|
|
341
361
|
});
|
|
@@ -344,24 +364,30 @@ function removeServerExports(s: MagicString, source: string, program: AstNode):
|
|
|
344
364
|
}
|
|
345
365
|
|
|
346
366
|
export function transformForClient(code: string, filename: string): TransformResult {
|
|
347
|
-
//
|
|
348
|
-
|
|
367
|
+
// .d.ts files have no runtime code — yuku rejects parsing them as modules.
|
|
368
|
+
// Treat as a passthrough so callers don't need to pre-filter.
|
|
369
|
+
const lang = detectLangFromPath(filename);
|
|
370
|
+
if (lang === "dts") {
|
|
371
|
+
return { code, map: null, removedServerCode: false };
|
|
372
|
+
}
|
|
349
373
|
|
|
350
|
-
// Pass
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
374
|
+
// Pass 1 — yuku-parser: parse TS/TSX/JS/JSX directly to ESTree AST with
|
|
375
|
+
// span offsets calibrated against `code` itself (no transpile step).
|
|
376
|
+
const { program, diagnostics } = parse(code, { sourceType: "module", lang });
|
|
377
|
+
const firstError = diagnostics.find((d) => d.severity === "error");
|
|
378
|
+
if (firstError) {
|
|
379
|
+
throw new Error(`Failed to parse ${filename}: ${firstError.message}`);
|
|
354
380
|
}
|
|
355
381
|
|
|
356
|
-
// Pass
|
|
357
|
-
let s = new MagicString(
|
|
358
|
-
const removedServerCode = removeServerExports(s,
|
|
382
|
+
// Pass 2 — MagicString: surgically remove server-only properties.
|
|
383
|
+
let s = new MagicString(code);
|
|
384
|
+
const removedServerCode = removeServerExports(s, code, program);
|
|
359
385
|
|
|
360
|
-
// Pass
|
|
386
|
+
// Pass 3 — DCE: prune imports that are no longer referenced.
|
|
361
387
|
// deadCodeElimination returns a fresh MagicString keyed on the current
|
|
362
388
|
// output so its internal AST offsets remain consistent.
|
|
363
389
|
if (removedServerCode) {
|
|
364
|
-
s = deadCodeElimination(s);
|
|
390
|
+
s = deadCodeElimination(s, lang);
|
|
365
391
|
}
|
|
366
392
|
|
|
367
393
|
return {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: auto-invalidate has a small public/internal surface gathered for tests and furin.ts exports
|
|
2
|
+
export { furinInvalidate } from "./plugin.ts";
|
|
3
|
+
export { AutoInvalidateRegistry, autoInvalidateRegistry } from "./registry.ts";
|
|
4
|
+
export {
|
|
5
|
+
appendPendingInvalidationHeader,
|
|
6
|
+
isSuccessfulMutationResponse,
|
|
7
|
+
revalidateTag,
|
|
8
|
+
runInvalidationRules,
|
|
9
|
+
} from "./runtime.ts";
|
|
10
|
+
export type { InvalidationInput, InvalidationRule } from "./types.ts";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type AfterHandler, Elysia } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
appendPendingInvalidationHeader,
|
|
4
|
+
isSuccessfulMutationResponse,
|
|
5
|
+
runInvalidationRules,
|
|
6
|
+
} from "./runtime.ts";
|
|
7
|
+
import type { InvalidationInput } from "./types.ts";
|
|
8
|
+
|
|
9
|
+
type AnyAfterHandleContext = Parameters<AfterHandler>[0];
|
|
10
|
+
|
|
11
|
+
export function furinInvalidate() {
|
|
12
|
+
return new Elysia({ name: "furin-invalidate" }).macro({
|
|
13
|
+
invalidate(rules: InvalidationInput) {
|
|
14
|
+
return {
|
|
15
|
+
afterHandle(ctx: AnyAfterHandleContext) {
|
|
16
|
+
if (!isSuccessfulMutationResponse(ctx)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
runInvalidationRules(rules);
|
|
20
|
+
appendPendingInvalidationHeader(ctx.set);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export class AutoInvalidateRegistry {
|
|
2
|
+
private readonly pathToTags = new Map<string, Set<string>>();
|
|
3
|
+
private readonly tagToPaths = new Map<string, Set<string>>();
|
|
4
|
+
|
|
5
|
+
registerLoaderTags(urlPath: string, tags: readonly string[] | undefined): void {
|
|
6
|
+
if (!tags || tags.length === 0) {
|
|
7
|
+
this.unregisterPath(urlPath);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
this.unregisterPath(urlPath);
|
|
12
|
+
|
|
13
|
+
const uniqueTags = new Set(tags);
|
|
14
|
+
this.pathToTags.set(urlPath, uniqueTags);
|
|
15
|
+
for (const tag of uniqueTags) {
|
|
16
|
+
let paths = this.tagToPaths.get(tag);
|
|
17
|
+
if (!paths) {
|
|
18
|
+
paths = new Set<string>();
|
|
19
|
+
this.tagToPaths.set(tag, paths);
|
|
20
|
+
}
|
|
21
|
+
paths.add(urlPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pathsForTags(tags: readonly string[]): string[] {
|
|
26
|
+
const paths = new Set<string>();
|
|
27
|
+
for (const tag of tags) {
|
|
28
|
+
for (const path of this.tagToPaths.get(tag) ?? []) {
|
|
29
|
+
paths.add(path);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return [...paths];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
unregisterPath(urlPath: string): void {
|
|
36
|
+
const tags = this.pathToTags.get(urlPath);
|
|
37
|
+
if (!tags) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const tag of tags) {
|
|
42
|
+
const paths = this.tagToPaths.get(tag);
|
|
43
|
+
if (!paths) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
paths.delete(urlPath);
|
|
47
|
+
if (paths.size === 0) {
|
|
48
|
+
this.tagToPaths.delete(tag);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
this.pathToTags.delete(urlPath);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
reset(): void {
|
|
55
|
+
this.pathToTags.clear();
|
|
56
|
+
this.tagToPaths.clear();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const autoInvalidateRegistry = new AutoInvalidateRegistry();
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { Context } from "elysia";
|
|
2
|
+
import { consumePendingInvalidations, revalidatePath } from "../cache/invalidation.ts";
|
|
3
|
+
import { autoInvalidateRegistry } from "./registry.ts";
|
|
4
|
+
import type { InvalidationInput, InvalidationRule } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
function toRules(input: InvalidationInput): readonly InvalidationRule[] {
|
|
7
|
+
return Array.isArray(input) ? input : [input as InvalidationRule];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function statusFromResponseValue(responseValue: unknown): number | undefined {
|
|
11
|
+
if (responseValue instanceof Response) {
|
|
12
|
+
return responseValue.status;
|
|
13
|
+
}
|
|
14
|
+
if (responseValue && typeof responseValue === "object" && "status" in responseValue) {
|
|
15
|
+
const status = (responseValue as { status?: unknown }).status;
|
|
16
|
+
if (typeof status === "number") {
|
|
17
|
+
return status;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (responseValue && typeof responseValue === "object" && "code" in responseValue) {
|
|
21
|
+
const code = (responseValue as { code?: unknown }).code;
|
|
22
|
+
if (typeof code === "number") {
|
|
23
|
+
return code;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isSuccessfulMutationResponse(
|
|
30
|
+
ctx: Pick<Context, "set"> & {
|
|
31
|
+
responseValue?: unknown;
|
|
32
|
+
response?: unknown;
|
|
33
|
+
}
|
|
34
|
+
): boolean {
|
|
35
|
+
const responseStatus =
|
|
36
|
+
statusFromResponseValue(ctx.responseValue) ?? statusFromResponseValue(ctx.response);
|
|
37
|
+
if (responseStatus !== undefined) {
|
|
38
|
+
return responseStatus >= 200 && responseStatus < 400;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const setStatus = ctx.set.status;
|
|
42
|
+
if (typeof setStatus === "number") {
|
|
43
|
+
return setStatus >= 200 && setStatus < 400;
|
|
44
|
+
}
|
|
45
|
+
if (typeof setStatus === "string") {
|
|
46
|
+
// Elysia allows string status codes (e.g. "Not Found"). We cannot map
|
|
47
|
+
// every possible string to its numeric code without the StatusMap, so we
|
|
48
|
+
// treat known client-error (4xx) and server-error (5xx) texts as failures
|
|
49
|
+
// and everything else as success (1xx, 2xx, 3xx, and any unknown string).
|
|
50
|
+
const errorStatusTexts = new Set([
|
|
51
|
+
"Bad Request",
|
|
52
|
+
"Unauthorized",
|
|
53
|
+
"Payment Required",
|
|
54
|
+
"Forbidden",
|
|
55
|
+
"Not Found",
|
|
56
|
+
"Method Not Allowed",
|
|
57
|
+
"Not Acceptable",
|
|
58
|
+
"Proxy Authentication Required",
|
|
59
|
+
"Request Timeout",
|
|
60
|
+
"Conflict",
|
|
61
|
+
"Gone",
|
|
62
|
+
"Length Required",
|
|
63
|
+
"Precondition Failed",
|
|
64
|
+
"Payload Too Large",
|
|
65
|
+
"URI Too Long",
|
|
66
|
+
"Unsupported Media Type",
|
|
67
|
+
"Range Not Satisfiable",
|
|
68
|
+
"Expectation Failed",
|
|
69
|
+
"I'm a teapot",
|
|
70
|
+
"Misdirected Request",
|
|
71
|
+
"Unprocessable Content",
|
|
72
|
+
"Locked",
|
|
73
|
+
"Failed Dependency",
|
|
74
|
+
"Too Early",
|
|
75
|
+
"Upgrade Required",
|
|
76
|
+
"Precondition Required",
|
|
77
|
+
"Too Many Requests",
|
|
78
|
+
"Enhance Your Calm",
|
|
79
|
+
"Request Header Fields Too Large",
|
|
80
|
+
"Unavailable For Legal Reasons",
|
|
81
|
+
"Internal Server Error",
|
|
82
|
+
"Not Implemented",
|
|
83
|
+
"Bad Gateway",
|
|
84
|
+
"Service Unavailable",
|
|
85
|
+
"Gateway Timeout",
|
|
86
|
+
"HTTP Version Not Supported",
|
|
87
|
+
"Variant Also Negotiates",
|
|
88
|
+
"Insufficient Storage",
|
|
89
|
+
"Loop Detected",
|
|
90
|
+
"Not Extended",
|
|
91
|
+
"Network Authentication Required",
|
|
92
|
+
]);
|
|
93
|
+
return !errorStatusTexts.has(setStatus);
|
|
94
|
+
}
|
|
95
|
+
// No explicit status was set — default to success (200).
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function appendPendingInvalidationHeader(set: Context["set"]): void {
|
|
100
|
+
const pending = consumePendingInvalidations();
|
|
101
|
+
if (pending.length === 0) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const headerName = "x-furin-revalidate";
|
|
106
|
+
const existing = set.headers[headerName];
|
|
107
|
+
set.headers[headerName] =
|
|
108
|
+
typeof existing === "string" && existing.length > 0
|
|
109
|
+
? `${existing},${pending.join(",")}`
|
|
110
|
+
: pending.join(",");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function revalidateTag(tags: string | readonly string[]): boolean {
|
|
114
|
+
const tagList = typeof tags === "string" ? [tags] : [...tags];
|
|
115
|
+
let deleted = false;
|
|
116
|
+
for (const path of autoInvalidateRegistry.pathsForTags(tagList)) {
|
|
117
|
+
deleted = revalidatePath(path, "page") || deleted;
|
|
118
|
+
}
|
|
119
|
+
return deleted;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function runInvalidationRules(input: InvalidationInput): boolean {
|
|
123
|
+
let deleted = false;
|
|
124
|
+
for (const rule of toRules(input)) {
|
|
125
|
+
if ("path" in rule && rule.path) {
|
|
126
|
+
deleted = revalidatePath(rule.path, rule.type) || deleted;
|
|
127
|
+
}
|
|
128
|
+
if (rule.tags && rule.tags.length > 0) {
|
|
129
|
+
deleted = revalidateTag(rule.tags) || deleted;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return deleted;
|
|
133
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CacheTag } from "../../furin.ts";
|
|
2
|
+
import type { RevalidateType } from "../cache/route-cache.ts";
|
|
3
|
+
|
|
4
|
+
export type InvalidationRule =
|
|
5
|
+
| {
|
|
6
|
+
path: string;
|
|
7
|
+
tags?: readonly CacheTag[];
|
|
8
|
+
type: RevalidateType;
|
|
9
|
+
}
|
|
10
|
+
| {
|
|
11
|
+
path?: never;
|
|
12
|
+
tags: readonly CacheTag[];
|
|
13
|
+
type?: never;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type InvalidationInput = InvalidationRule | readonly InvalidationRule[];
|