@teyik0/furin 0.5.0-alpha.2 → 0.7.0-alpha.1

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 (230) hide show
  1. package/dist/adapter/runtime-build.d.ts +3 -1
  2. package/dist/build/client.d.ts +4 -1
  3. package/dist/build/entry-template.d.ts +5 -0
  4. package/dist/build/entry-template.ts +9 -0
  5. package/dist/build/hydrate.d.ts +1 -1
  6. package/dist/build/index.js +9589 -6685
  7. package/dist/build/preload-manifest.d.ts +17 -0
  8. package/dist/build/request-keys.d.ts +4 -0
  9. package/dist/build/types.d.ts +6 -0
  10. package/dist/cli/index.js +9539 -6644
  11. package/dist/client/browser-events-runtime.d.ts +2 -1
  12. package/dist/client/client-module.d.ts +26 -0
  13. package/dist/client/define-route.d.ts +9 -1
  14. package/dist/client/dev-error-overlay.ts +1 -0
  15. package/dist/client/document.d.ts +3 -0
  16. package/dist/client/hmr.d.ts +1 -1
  17. package/dist/client/internal/runtime-types.d.ts +2 -0
  18. package/dist/client/link.js +1011 -157
  19. package/dist/client/mutation.d.ts +38 -0
  20. package/dist/client/query-store.d.ts +85 -0
  21. package/dist/client/query.d.ts +14 -0
  22. package/dist/client/route-api.d.ts +11 -0
  23. package/dist/client/router/context.d.ts +3 -2
  24. package/dist/client/router/index.d.ts +1 -1
  25. package/dist/client/router/link-utils.d.ts +8 -1
  26. package/dist/client/router/navigation.d.ts +2 -9
  27. package/dist/client/router/optimistic.d.ts +41 -0
  28. package/dist/client/router/search/index.d.ts +3 -3
  29. package/dist/client/router/search-store.d.ts +1 -0
  30. package/dist/client/router/sync-catch-up.d.ts +3 -0
  31. package/dist/client/router/types.d.ts +49 -7
  32. package/dist/client/sync.d.ts +39 -22
  33. package/dist/client.d.ts +11 -33
  34. package/dist/client.js +1222 -57
  35. package/dist/config.d.ts +7 -0
  36. package/dist/config.js +1 -0
  37. package/dist/define-route.d.ts +231 -123
  38. package/dist/env.d.ts +2 -2
  39. package/dist/furin.d.ts +4 -2
  40. package/dist/furin.js +9717 -6796
  41. package/dist/plugin/hmr-dependencies.d.ts +2 -0
  42. package/dist/plugin/index.js +811 -145
  43. package/dist/plugin/transform-client-module.d.ts +16 -0
  44. package/dist/plugin/transform-client.d.ts +2 -0
  45. package/dist/plugin/transform-dev-page.d.ts +7 -0
  46. package/dist/plugin/transform-isomorphic.d.ts +1 -1
  47. package/dist/routes.d.ts +6 -0
  48. package/dist/rsc-client.js +87 -16
  49. package/dist/rsc.js +87 -16
  50. package/dist/server/auto-invalidate/registry.d.ts +5 -2
  51. package/dist/server/auto-invalidate/types.d.ts +2 -0
  52. package/dist/server/browser-events/plugin.d.ts +3 -0
  53. package/dist/server/cache/isr.d.ts +2 -4
  54. package/dist/server/cache/route-cache.d.ts +5 -0
  55. package/dist/server/dev/graph.d.ts +1 -0
  56. package/dist/server/dev/runtime.production.d.ts +1 -0
  57. package/dist/server/dev-page-plugin.d.ts +6 -19
  58. package/dist/server/internal.d.ts +5 -0
  59. package/dist/server/internal.js +105 -57
  60. package/dist/server/production-server.js +104 -56
  61. package/dist/server/render/assemble.d.ts +6 -6
  62. package/dist/server/render/document.d.ts +1 -1
  63. package/dist/server/render/element.d.ts +1 -1
  64. package/dist/server/render/loaders.d.ts +15 -5
  65. package/dist/server/render/mixed-cache.d.ts +6 -0
  66. package/dist/server/render/react-stream.d.ts +3 -0
  67. package/dist/server/render/ssr.d.ts +5 -4
  68. package/dist/server/render/template.d.ts +4 -0
  69. package/dist/server/router/defined-route.d.ts +2 -0
  70. package/dist/server/router/discovery.d.ts +1 -0
  71. package/dist/server/router/hmr.d.ts +1 -1
  72. package/dist/server/router/patterns.d.ts +3 -0
  73. package/dist/server/router/plugin.d.ts +3 -0
  74. package/dist/server/router/types.d.ts +4 -0
  75. package/dist/server/security/csp.d.ts +12 -0
  76. package/dist/server/sync/adapter.d.ts +25 -4
  77. package/dist/server/sync/atomic.d.ts +25 -0
  78. package/dist/server/sync/drizzle/index.d.ts +45 -0
  79. package/dist/server/sync/drizzle/index.js +540 -0
  80. package/dist/server/sync/execution-error.d.ts +2 -0
  81. package/dist/server/sync/index.d.ts +2 -1
  82. package/dist/server/sync/index.js +580 -124
  83. package/dist/server/sync/mutation-handler.d.ts +4 -0
  84. package/dist/server/sync/plugin.d.ts +34 -11
  85. package/dist/server/sync/postgres/execute.d.ts +3 -0
  86. package/dist/server/sync/postgres/index.d.ts +12 -1
  87. package/dist/server/sync/postgres/index.js +42 -13
  88. package/dist/server/sync/postgres/sql.d.ts +8 -0
  89. package/dist/server/sync/prisma/index.d.ts +24 -0
  90. package/dist/server/sync/prisma/index.js +340 -0
  91. package/dist/server/sync/queries.d.ts +21 -0
  92. package/dist/server/sync/query-context.d.ts +5 -0
  93. package/dist/server/sync/redis/index.js +3 -3
  94. package/dist/server/sync/redis/scripts.d.ts +1 -1
  95. package/dist/server/sync/response.d.ts +1 -0
  96. package/dist/server/sync/sqlite/index.d.ts +3 -0
  97. package/dist/server/sync/sqlite/index.js +10 -4
  98. package/dist/server/sync/stream.d.ts +2 -5
  99. package/dist/server/sync/validation.d.ts +4 -0
  100. package/dist/shared/defer.d.ts +31 -0
  101. package/dist/shared/error.d.ts +2 -2
  102. package/dist/shared/page-key.d.ts +7 -0
  103. package/dist/shared/query-bindings.d.ts +12 -0
  104. package/dist/shared/route-frame.d.ts +1 -0
  105. package/dist/shared/sync-query.d.ts +40 -0
  106. package/package.json +40 -16
  107. package/src/adapter/README.md +3 -0
  108. package/src/adapter/bun.ts +3 -0
  109. package/src/adapter/package.ts +16 -2
  110. package/src/adapter/runtime-build.ts +99 -14
  111. package/src/adapter/static.ts +17 -6
  112. package/src/adapter/vercel.ts +33 -23
  113. package/src/build/client.ts +23 -5
  114. package/src/build/entry-template.ts +9 -0
  115. package/src/build/hydrate.ts +18 -3
  116. package/src/build/preload-manifest.ts +100 -0
  117. package/src/build/production-instrumentation.ts +6 -3
  118. package/src/build/request-keys.ts +93 -0
  119. package/src/build/ssg-cache.ts +9 -4
  120. package/src/build/types.ts +4 -0
  121. package/src/cli/index.ts +1 -0
  122. package/src/client/boundaries.tsx +1 -1
  123. package/src/client/browser-events-runtime.ts +73 -16
  124. package/src/client/client-module.ts +57 -0
  125. package/src/client/define-route.ts +11 -2
  126. package/src/client/dev-error-overlay.ts +1 -0
  127. package/src/client/document.tsx +47 -20
  128. package/src/client/hmr.ts +4 -2
  129. package/src/client/internal/runtime-types.ts +2 -0
  130. package/src/client/link.tsx +8 -8
  131. package/src/client/mutation.ts +136 -0
  132. package/src/client/query-store.ts +418 -0
  133. package/src/client/query.tsx +67 -0
  134. package/src/client/route-api.tsx +75 -0
  135. package/src/client/router/boundary-tree.tsx +10 -6
  136. package/src/client/router/context.ts +57 -4
  137. package/src/client/router/index.ts +3 -0
  138. package/src/client/router/link-utils.ts +29 -1
  139. package/src/client/router/navigation.ts +3 -50
  140. package/src/client/router/optimistic.ts +233 -0
  141. package/src/client/router/provider.tsx +404 -96
  142. package/src/client/router/search/index.ts +37 -17
  143. package/src/client/router/search-store.ts +4 -0
  144. package/src/client/router/sync-catch-up.ts +7 -0
  145. package/src/client/router/types.ts +71 -9
  146. package/src/client/sync.ts +438 -65
  147. package/src/client.ts +20 -60
  148. package/src/config.ts +5 -0
  149. package/src/define-route.ts +442 -155
  150. package/src/env.d.ts +2 -2
  151. package/src/furin.ts +213 -38
  152. package/src/plugin/hmr-dependencies.ts +58 -0
  153. package/src/plugin/index.ts +38 -2
  154. package/src/plugin/route-config-autofix.ts +2 -2
  155. package/src/plugin/routes.ts +19 -8
  156. package/src/plugin/transform-client-module.ts +123 -0
  157. package/src/plugin/transform-client.ts +293 -13
  158. package/src/plugin/transform-dev-page.ts +231 -0
  159. package/src/plugin/transform-isomorphic.ts +11 -5
  160. package/src/routes.ts +8 -0
  161. package/src/rsc/shared.tsx +104 -18
  162. package/src/rsc/version.ts +5 -0
  163. package/src/server/auto-invalidate/registry.ts +56 -24
  164. package/src/server/auto-invalidate/runtime.ts +17 -0
  165. package/src/server/auto-invalidate/types.ts +6 -0
  166. package/src/server/browser-events/plugin.ts +112 -1
  167. package/src/server/cache/dev-loader.ts +1 -1
  168. package/src/server/cache/invalidation.ts +2 -0
  169. package/src/server/cache/isr-ssg.ts +1 -1
  170. package/src/server/cache/isr.ts +6 -34
  171. package/src/server/cache/route-cache.ts +40 -4
  172. package/src/server/cache/ssg.ts +1 -1
  173. package/src/server/dev/diagnostics.ts +4 -2
  174. package/src/server/dev/error.ts +2 -1
  175. package/src/server/dev/graph.ts +70 -7
  176. package/src/server/dev/runtime.production.ts +4 -0
  177. package/src/server/dev/symbolicate.ts +2 -0
  178. package/src/server/dev-page-plugin.ts +160 -63
  179. package/src/server/evlog.ts +1 -1
  180. package/src/server/internal.ts +5 -0
  181. package/src/server/render/assemble.ts +15 -11
  182. package/src/server/render/document.tsx +13 -2
  183. package/src/server/render/element.tsx +11 -6
  184. package/src/server/render/isr.ts +71 -33
  185. package/src/server/render/loaders.ts +328 -70
  186. package/src/server/render/mixed-cache.ts +327 -0
  187. package/src/server/render/not-found.ts +14 -11
  188. package/src/server/render/ppr-document.ts +47 -11
  189. package/src/server/render/ppr-route.ts +54 -20
  190. package/src/server/render/react-stream.ts +17 -0
  191. package/src/server/render/route-frame-transport.ts +3 -0
  192. package/src/server/render/ssg.ts +53 -19
  193. package/src/server/render/ssr.ts +105 -36
  194. package/src/server/render/static-params.ts +1 -1
  195. package/src/server/render/template.ts +23 -0
  196. package/src/server/router/defined-route.ts +3 -0
  197. package/src/server/router/discovery.ts +14 -0
  198. package/src/server/router/hmr.ts +109 -18
  199. package/src/server/router/patterns.ts +27 -0
  200. package/src/server/router/plugin.ts +94 -19
  201. package/src/server/router/source-version.ts +20 -2
  202. package/src/server/router/types.ts +4 -0
  203. package/src/server/security/csp.ts +44 -0
  204. package/src/server/sync/adapter.ts +41 -4
  205. package/src/server/sync/atomic.ts +303 -0
  206. package/src/server/sync/drizzle/index.ts +145 -0
  207. package/src/server/sync/execution-error.ts +1 -0
  208. package/src/server/sync/fingerprint.ts +2 -6
  209. package/src/server/sync/index.ts +6 -0
  210. package/src/server/sync/mutation-handler.ts +69 -0
  211. package/src/server/sync/plugin.ts +177 -60
  212. package/src/server/sync/postgres/execute.ts +20 -0
  213. package/src/server/sync/postgres/index.ts +60 -16
  214. package/src/server/sync/postgres/sql.ts +20 -0
  215. package/src/server/sync/prisma/index.ts +70 -0
  216. package/src/server/sync/queries.ts +75 -0
  217. package/src/server/sync/query-context.ts +85 -0
  218. package/src/server/sync/redis/index.ts +7 -3
  219. package/src/server/sync/redis/scripts.ts +1 -1
  220. package/src/server/sync/response.ts +21 -0
  221. package/src/server/sync/sqlite/index.ts +19 -8
  222. package/src/server/sync/stream.ts +14 -22
  223. package/src/server/sync/validation.ts +34 -0
  224. package/src/shared/defer.ts +48 -0
  225. package/src/shared/error.ts +2 -2
  226. package/src/shared/page-key.ts +36 -0
  227. package/src/shared/query-bindings.ts +138 -0
  228. package/src/shared/route-frame.ts +13 -1
  229. package/src/shared/route-map.ts +12 -2
  230. package/src/shared/sync-query.ts +138 -0
@@ -0,0 +1,231 @@
1
+ import MagicString from "magic-string";
2
+ import {
3
+ detectLangFromPath,
4
+ detectLoaderFromPath,
5
+ unwrapTSExpression,
6
+ } from "../server/lang-detect.ts";
7
+ import { parseSource } from "../shared/parser.ts";
8
+ import { type AstNode, walkAST } from "../shared/utils/ast-walk.ts";
9
+ import { deadCodeElimination } from "./dead-code-elimination.ts";
10
+
11
+ interface SplitDevPage {
12
+ contract: string;
13
+ render: string;
14
+ }
15
+
16
+ function node(value: unknown): AstNode | undefined {
17
+ if (value && typeof value === "object" && "type" in value) {
18
+ return unwrapTSExpression(value as AstNode) as AstNode;
19
+ }
20
+ }
21
+
22
+ function namesIn(value: unknown): Set<string> {
23
+ const names = new Set<string>();
24
+ walkAST(value, (entry) => {
25
+ if (
26
+ (entry.type === "Identifier" || entry.type === "JSXIdentifier") &&
27
+ typeof entry.name === "string"
28
+ ) {
29
+ names.add(entry.name);
30
+ }
31
+ });
32
+ return names;
33
+ }
34
+
35
+ const CALLBACK_BINDING_TYPES = new Set([
36
+ "ArrowFunctionExpression",
37
+ "CatchClause",
38
+ "ClassDeclaration",
39
+ "ClassExpression",
40
+ "FunctionDeclaration",
41
+ "FunctionExpression",
42
+ "VariableDeclarator",
43
+ ]);
44
+
45
+ function callbackBindings(callback: AstNode): Set<string> {
46
+ const bindings = new Set<string>();
47
+ walkAST(callback, (entry) => {
48
+ if (!CALLBACK_BINDING_TYPES.has(entry.type)) {
49
+ return;
50
+ }
51
+ const patterns = [entry.id, entry.param, ...(Array.isArray(entry.params) ? entry.params : [])];
52
+ for (const pattern of patterns) {
53
+ for (const name of namesIn(pattern)) {
54
+ bindings.add(name);
55
+ }
56
+ }
57
+ });
58
+ return bindings;
59
+ }
60
+
61
+ function defineRouteBindings(imports: AstNode[]): Set<string> {
62
+ const bindings = new Set<string>();
63
+ for (const declaration of imports) {
64
+ const specifier = node(declaration.source)?.value;
65
+ if (specifier !== "furin" && specifier !== "@teyik0/furin") {
66
+ continue;
67
+ }
68
+ for (const entry of declaration.specifiers as AstNode[]) {
69
+ const local = node(entry.local)?.name;
70
+ if (node(entry.imported)?.name === "defineRoute" && typeof local === "string") {
71
+ bindings.add(local);
72
+ }
73
+ }
74
+ }
75
+ return bindings;
76
+ }
77
+
78
+ function inspectModule(statements: AstNode[]) {
79
+ const imports = statements.filter((statement) => statement.type === "ImportDeclaration");
80
+ let localBindings = new Set<string>();
81
+ let terminal: AstNode | undefined;
82
+ for (const statement of statements) {
83
+ const declaration = statement.type.startsWith("Export")
84
+ ? node(statement.declaration)
85
+ : statement;
86
+ if (declaration?.type === "VariableDeclaration") {
87
+ for (const binding of declaration.declarations as AstNode[]) {
88
+ localBindings = localBindings.union(namesIn(binding.id));
89
+ if (statement.type === "ExportNamedDeclaration" && node(binding.id)?.name === "route") {
90
+ terminal = node(binding.init);
91
+ }
92
+ }
93
+ } else if (declaration) {
94
+ localBindings = localBindings.union(namesIn(declaration.id));
95
+ }
96
+ }
97
+ return { imports, localBindings, terminal };
98
+ }
99
+
100
+ function inlinePage(terminal: AstNode | undefined, bindings: Set<string>): AstNode | undefined {
101
+ const callee = node(terminal?.callee);
102
+ const args = terminal?.arguments as AstNode[] | undefined;
103
+ const callback = node(args?.[0]);
104
+ if (
105
+ terminal?.type !== "CallExpression" ||
106
+ callee?.type !== "MemberExpression" ||
107
+ callee.computed ||
108
+ node(callee.property)?.name !== "page" ||
109
+ args?.length !== 1 ||
110
+ !callback ||
111
+ (callback.type !== "ArrowFunctionExpression" && callback.type !== "FunctionExpression")
112
+ ) {
113
+ return;
114
+ }
115
+ let root = node(callee.object);
116
+ while (root?.type === "CallExpression" || root?.type === "MemberExpression") {
117
+ root = node(root.type === "CallExpression" ? root.callee : root.object);
118
+ }
119
+ if (typeof root?.name === "string" && bindings.has(root.name)) {
120
+ return callback;
121
+ }
122
+ }
123
+
124
+ function hasLexicalCapture(callback: AstNode, program: unknown): boolean {
125
+ let opaque = false;
126
+ walkAST(program, (entry) => {
127
+ if (
128
+ (entry.type === "VariableDeclaration" && entry.kind === "var") ||
129
+ (entry.type === "Identifier" && entry.name === "eval")
130
+ ) {
131
+ opaque = true;
132
+ }
133
+ if (entry.start < callback.start || entry.end > callback.end) {
134
+ return;
135
+ }
136
+ if (
137
+ entry.type === "ThisExpression" ||
138
+ entry.type === "MetaProperty" ||
139
+ (entry.type === "Identifier" && (entry.name === "eval" || entry.name === "arguments"))
140
+ ) {
141
+ opaque = true;
142
+ }
143
+ });
144
+ return opaque;
145
+ }
146
+
147
+ function routeHasConsumers(statements: AstNode[]): boolean {
148
+ let references = 0;
149
+ for (const statement of statements) {
150
+ if (statement.type === "ImportDeclaration") {
151
+ continue;
152
+ }
153
+ walkAST(statement, (entry) => {
154
+ if (
155
+ (entry.type === "Identifier" || entry.type === "JSXIdentifier") &&
156
+ entry.name === "route"
157
+ ) {
158
+ references += 1;
159
+ }
160
+ });
161
+ }
162
+ return references !== 1; // The declaration itself accounts for one reference.
163
+ }
164
+
165
+ /** Split only a top-level route with an inline page that captures imports, not module state. */
166
+ export function splitDevPage(
167
+ source: string,
168
+ filePath: string,
169
+ renderSpecifier: string
170
+ ): SplitDevPage | undefined {
171
+ if (!source.includes(".page") || source.includes("@jsx")) {
172
+ return;
173
+ }
174
+ const lang = detectLangFromPath(filePath);
175
+ const { program, diagnostics } = parseSource(source, lang);
176
+ if (diagnostics.some((diagnostic) => diagnostic.severity === "error")) {
177
+ return;
178
+ }
179
+ const statements = program.body as unknown as AstNode[];
180
+ if (routeHasConsumers(statements)) {
181
+ return;
182
+ }
183
+ const { imports, localBindings, terminal } = inspectModule(statements);
184
+ const callback = inlinePage(terminal, defineRouteBindings(imports));
185
+ if (!callback || hasLexicalCapture(callback, program)) {
186
+ return;
187
+ }
188
+ const shadowed = callbackBindings(callback);
189
+ if (
190
+ imports.some((entry) =>
191
+ (entry.specifiers as AstNode[]).some((specifier) =>
192
+ shadowed.has(String(node(specifier.local)?.name))
193
+ )
194
+ )
195
+ ) {
196
+ return;
197
+ }
198
+ const references = namesIn(callback);
199
+ const bindings = localBindings.union(
200
+ new Set(
201
+ imports.flatMap((entry) =>
202
+ (entry.specifiers as AstNode[]).map((specifier) => String(node(specifier.local)?.name))
203
+ )
204
+ )
205
+ );
206
+ if (
207
+ [...localBindings].some((name) => references.has(name)) ||
208
+ ["__furinDeferredRender", "Object", "Symbol", "Error"].some((name) => bindings.has(name))
209
+ ) {
210
+ return;
211
+ }
212
+ const renderImports = imports.filter((entry) =>
213
+ (entry.specifiers as AstNode[]).some((specifier) =>
214
+ references.has(String(node(specifier.local)?.name))
215
+ )
216
+ );
217
+ const rewritten = new MagicString(source);
218
+ rewritten.overwrite(callback.start, callback.end, "__furinDeferredRender");
219
+ const contract = deadCodeElimination(rewritten, source, lang).toString();
220
+ // Splitting without removing an import cannot avoid loading any dependency.
221
+ const scanner = new Bun.Transpiler({ loader: detectLoaderFromPath(filePath) });
222
+ const retained = new Set(scanner.scanImports(contract).map((entry) => entry.path));
223
+ if (!renderImports.some((entry) => !retained.has(String(node(entry.source)?.value)))) {
224
+ return;
225
+ }
226
+ const fragment = `${renderImports.map((entry) => source.slice(entry.start, entry.end)).join("\n")}\nexport default ${source.slice(callback.start, callback.end)};`;
227
+ return {
228
+ contract: `const __furinDeferredRender = Object.assign(() => { throw new Error("Unresolved Furin page"); }, { [Symbol.for("furin.dev.render")]: () => import(${JSON.stringify(renderSpecifier)}) });\n${contract}`,
229
+ render: deadCodeElimination(new MagicString(fragment), source, lang).toString(),
230
+ };
231
+ }
@@ -10,9 +10,12 @@ import { parseSource } from "../shared/parser.ts";
10
10
  import type { AstNode } from "../shared/utils/ast-walk.ts";
11
11
  import { hasShadowingDeclaration } from "./binding-scope.ts";
12
12
  import { deadCodeElimination } from "./dead-code-elimination.ts";
13
+ import { transformClientModules } from "./transform-client-module.ts";
13
14
 
14
15
  const FURIN_MODULES = new Set(["@teyik0/furin", "furin"]);
15
- const SCRIPT_FILE_FILTER = /^(?!.*(?:node_modules|[\\/]\.furin[\\/]build[\\/])).*\.(tsx?|jsx?)$/;
16
+ // Accepts a query suffix: dev-mode route discovery imports `page.tsx?furin-server&t=…`.
17
+ const SCRIPT_FILE_FILTER =
18
+ /^(?!.*(?:node_modules|[\\/]\.furin[\\/]build[\\/])).*\.(tsx?|jsx?)(?:\?.*)?$/;
16
19
 
17
20
  export type IsomorphicEnvironment = "client" | "server";
18
21
 
@@ -402,14 +405,16 @@ function assertStaticEnvironmentMethods(
402
405
  }
403
406
 
404
407
  export function transformIsomorphicFunctions(
405
- source: string,
408
+ input: string,
406
409
  filename: string,
407
410
  environment: IsomorphicEnvironment
408
411
  ): IsomorphicTransformResult {
409
412
  const lang = detectLangFromPath(filename);
410
413
  if (lang === "dts") {
411
- return { code: source, map: null, transformed: false };
414
+ return { code: input, map: null, transformed: false };
412
415
  }
416
+ const source = transformClientModules(input, filename, environment);
417
+ const clientModulesTransformed = source !== input;
413
418
 
414
419
  const { program, diagnostics } = parseSource(source, lang);
415
420
  const firstError = diagnostics.find((diagnostic) => diagnostic.severity === "error");
@@ -423,7 +428,7 @@ export function transformIsomorphicFunctions(
423
428
  assertNoSplitChains(source, filename, program, builders);
424
429
  const candidates = collectCandidates(source, filename, program, bindings);
425
430
  if (candidates.length === 0) {
426
- return { code: source, map: null, transformed: false };
431
+ return { code: source, map: null, transformed: clientModulesTransformed };
427
432
  }
428
433
 
429
434
  const transformed = new MagicString(source);
@@ -450,7 +455,8 @@ export function isomorphicTransformPlugin(environment: IsomorphicEnvironment): B
450
455
  return {
451
456
  name: `furin-isomorphic-${environment}`,
452
457
  setup(build) {
453
- build.onLoad({ filter: SCRIPT_FILE_FILTER }, async ({ path }) => {
458
+ build.onLoad({ filter: SCRIPT_FILE_FILTER }, async (args) => {
459
+ const path = args.path.split("?")[0] as string;
454
460
  const source = await Bun.file(path).text();
455
461
  const loader = detectLoaderFromPath(path);
456
462
  const result = transformIsomorphicFunctions(source, path, environment);
package/src/routes.ts CHANGED
@@ -4,3 +4,11 @@
4
4
  */
5
5
  // biome-ignore lint/suspicious/noEmptyInterface: intentionally augmented by generated route types
6
6
  export interface RouteMap {}
7
+
8
+ /** Literal route patterns for editor completion in APIs that accept path params. */
9
+ // biome-ignore lint/suspicious/noEmptyInterface: intentionally augmented by generated route types
10
+ export interface RoutePatternMap {}
11
+
12
+ /** Application contract for sync IDs and their resource scopes. */
13
+ // biome-ignore lint/suspicious/noEmptyInterface: intentionally augmented by applications
14
+ export interface SyncQueryMap {}
@@ -82,27 +82,113 @@ export function restoreRscSource(kind: RscSourceKind, bytes: Uint8Array): unknow
82
82
  return kind === "renderable" ? createRenderableSource(state) : { [RSC_SOURCE]: state };
83
83
  }
84
84
 
85
- function resolveSlots(node: ReactNode, slots: object): ReactNode {
86
- return Children.map(node, (child) => {
87
- if (!isValidElement(child)) {
88
- return child;
85
+ function isLazyNode(value: object): boolean {
86
+ return "$$typeof" in value && value.$$typeof === Symbol.for("react.lazy");
87
+ }
88
+
89
+ function containsSlot(value: unknown, visited: WeakSet<object>): value is object {
90
+ if (value === null || typeof value !== "object" || visited.has(value)) {
91
+ return false;
92
+ }
93
+ visited.add(value);
94
+ if (isLazyNode(value)) {
95
+ return true;
96
+ }
97
+ if (isValidElement(value)) {
98
+ return value.type === SLOT_MARKER || containsSlot(value.props, visited);
99
+ }
100
+ if (Array.isArray(value)) {
101
+ return value.some((item) => containsSlot(item, visited));
102
+ }
103
+ return (
104
+ Object.getPrototypeOf(value) === Object.prototype &&
105
+ Object.values(value).some((item) => containsSlot(item, visited))
106
+ );
107
+ }
108
+
109
+ function resolveSlotArgument(
110
+ value: unknown,
111
+ slots: object,
112
+ visited: WeakMap<object, unknown>
113
+ ): unknown {
114
+ if (!containsSlot(value, new WeakSet())) {
115
+ return value;
116
+ }
117
+ if (visited.has(value)) {
118
+ return visited.get(value);
119
+ }
120
+ if (isLazyNode(value)) {
121
+ const result = createElement(LazySlots, { node: value as ReactNode, slots });
122
+ visited.set(value, result);
123
+ return result;
124
+ }
125
+ if (isValidElement(value)) {
126
+ const resolved = resolveSlotElement(value, slots);
127
+ const result = isValidElement(resolved) ? Children.map(value, () => resolved)?.[0] : resolved;
128
+ visited.set(value, result);
129
+ return result;
130
+ }
131
+ if (Array.isArray(value)) {
132
+ const result: unknown[] = [];
133
+ visited.set(value, result);
134
+ for (const item of value) {
135
+ result.push(resolveSlotArgument(item, slots, visited));
89
136
  }
90
- if (child.type === SLOT_MARKER) {
91
- const marker = child.props as { args?: unknown[]; name?: string };
92
- if (typeof marker.name !== "string") {
93
- return null;
137
+ const keys = new Set(result.filter(isValidElement).map((element) => element.key));
138
+ for (const [index, resolved] of result.entries()) {
139
+ if (isValidElement(resolved) && resolved.key === null) {
140
+ let key = String(index);
141
+ while (keys.has(key)) {
142
+ key = `.${key}`;
143
+ }
144
+ keys.add(key);
145
+ result[index] = cloneElement(resolved, { key });
94
146
  }
95
- const implementation = Reflect.get(slots, marker.name) as unknown;
96
- if (typeof implementation === "function") {
97
- return (implementation as (...args: unknown[]) => ReactNode)(...(marker.args ?? []));
98
- }
99
- return implementation as ReactNode;
100
147
  }
101
- const props = child.props as { children?: ReactNode };
102
- return props.children === undefined
103
- ? child
104
- : cloneElement(child, undefined, resolveSlots(props.children, slots));
105
- });
148
+ return result;
149
+ }
150
+ const result = {};
151
+ visited.set(value, result);
152
+ for (const [key, item] of Object.entries(value)) {
153
+ Object.defineProperty(result, key, {
154
+ configurable: true,
155
+ enumerable: true,
156
+ value: resolveSlotArgument(item, slots, visited),
157
+ writable: true,
158
+ });
159
+ }
160
+ return result;
161
+ }
162
+
163
+ function resolveSlotElement(child: ReactElement, slots: object): ReactNode {
164
+ if (child.type === SLOT_MARKER) {
165
+ const marker = child.props as { args?: unknown[]; name?: string };
166
+ if (typeof marker.name !== "string") {
167
+ return null;
168
+ }
169
+ const implementation = Reflect.get(slots, marker.name) as unknown;
170
+ if (typeof implementation === "function") {
171
+ const visited = new WeakMap<object, unknown>();
172
+ const args = (marker.args ?? []).map((argument) =>
173
+ resolveSlotArgument(argument, slots, visited)
174
+ );
175
+ return (implementation as (...args: unknown[]) => ReactNode)(...args);
176
+ }
177
+ return implementation as ReactNode;
178
+ }
179
+ const props = resolveSlotArgument(child.props, slots, new WeakMap());
180
+ return props === child.props ? child : cloneElement(child, props as object);
181
+ }
182
+
183
+ function resolveSlots(node: ReactNode, slots: object): ReactNode {
184
+ return Children.map(node, (child) =>
185
+ isValidElement(child) ? resolveSlotElement(child, slots) : child
186
+ );
187
+ }
188
+
189
+ function LazySlots({ node, slots }: { node: ReactNode; slots: object }): ReactNode {
190
+ // React unwraps Flight's lazy nodes here, inside their nearest Suspense boundary.
191
+ return resolveSlots(node, slots);
106
192
  }
107
193
 
108
194
  export function CompositeComponent<TProps extends object>(
@@ -6,6 +6,7 @@ const MINIMUM_REACT_19_PATCH = new Map([
6
6
  [0, 6],
7
7
  [1, 7],
8
8
  [2, 6],
9
+ [3, 0],
9
10
  ]);
10
11
 
11
12
  export interface RscVersions {
@@ -23,10 +24,14 @@ export function assertCompatibleRscVersions(versions: RscVersions): void {
23
24
  );
24
25
  }
25
26
  const match = STABLE_REACT_VERSION.exec(versions.react);
27
+ // biome-ignore lint/suspicious/noUnnecessaryConditions: exec returns null for unsupported versions.
26
28
  const major = Number(match?.[1]);
29
+ // biome-ignore lint/suspicious/noUnnecessaryConditions: exec returns null for unsupported versions.
27
30
  const minor = Number(match?.[2]);
31
+ // biome-ignore lint/suspicious/noUnnecessaryConditions: exec returns null for unsupported versions.
28
32
  const patch = Number(match?.[3]);
29
33
  const minimumPatch = MINIMUM_REACT_19_PATCH.get(minor);
34
+ // biome-ignore lint/suspicious/noUnnecessaryConditions: exec returns null for unsupported versions.
30
35
  if (!match || major !== 19 || minimumPatch === undefined || patch < minimumPatch) {
31
36
  throw new Error("[furin/rsc] RSC requires a supported patched React 19 version.");
32
37
  }
@@ -2,17 +2,31 @@ import { type FurinInstance, instanceSlot } from "../instance.ts";
2
2
 
3
3
  export class AutoInvalidateRegistry {
4
4
  private readonly pathToTags = new Map<string, Set<string>>();
5
+ private readonly pathOwners = new Map<string, Map<string, Set<string>>>();
5
6
  private readonly tagToPaths = new Map<string, Set<string>>();
6
7
 
7
- registerLoaderTags(urlPath: string, tags: readonly string[] | undefined): void {
8
- if (!tags || tags.length === 0) {
9
- this.unregisterPath(urlPath);
10
- return;
8
+ private reindexPath(urlPath: string): void {
9
+ const previous = this.pathToTags.get(urlPath);
10
+ if (previous) {
11
+ for (const tag of previous) {
12
+ const paths = this.tagToPaths.get(tag);
13
+ paths?.delete(urlPath);
14
+ if (paths?.size === 0) {
15
+ this.tagToPaths.delete(tag);
16
+ }
17
+ }
11
18
  }
12
19
 
13
- this.unregisterPath(urlPath);
14
-
15
- const uniqueTags = new Set(tags);
20
+ const uniqueTags = new Set<string>();
21
+ for (const tags of this.pathOwners.get(urlPath)?.values() ?? []) {
22
+ for (const tag of tags) {
23
+ uniqueTags.add(tag);
24
+ }
25
+ }
26
+ if (uniqueTags.size === 0) {
27
+ this.pathToTags.delete(urlPath);
28
+ return;
29
+ }
16
30
  this.pathToTags.set(urlPath, uniqueTags);
17
31
  for (const tag of uniqueTags) {
18
32
  let paths = this.tagToPaths.get(tag);
@@ -24,6 +38,24 @@ export class AutoInvalidateRegistry {
24
38
  }
25
39
  }
26
40
 
41
+ registerLoaderTags(urlPath: string, tags: readonly string[] | undefined, owner?: string): void {
42
+ const ownerKey = owner ?? "default";
43
+ let owners = this.pathOwners.get(urlPath);
44
+ if (tags === undefined || tags.length === 0) {
45
+ owners?.delete(ownerKey);
46
+ if (owners?.size === 0) {
47
+ this.pathOwners.delete(urlPath);
48
+ }
49
+ } else {
50
+ if (!owners) {
51
+ owners = new Map<string, Set<string>>();
52
+ this.pathOwners.set(urlPath, owners);
53
+ }
54
+ owners.set(ownerKey, new Set(tags));
55
+ }
56
+ this.reindexPath(urlPath);
57
+ }
58
+
27
59
  pathsForTags(tags: readonly string[]): string[] {
28
60
  const paths = new Set<string>();
29
61
  for (const tag of tags) {
@@ -34,27 +66,26 @@ export class AutoInvalidateRegistry {
34
66
  return [...paths];
35
67
  }
36
68
 
37
- unregisterPath(urlPath: string): void {
38
- const tags = this.pathToTags.get(urlPath);
39
- if (!tags) {
40
- return;
41
- }
69
+ tagsForPath(path: string): string[] {
70
+ return [...(this.pathToTags.get(path) ?? [])];
71
+ }
42
72
 
43
- for (const tag of tags) {
44
- const paths = this.tagToPaths.get(tag);
45
- if (!paths) {
46
- continue;
47
- }
48
- paths.delete(urlPath);
49
- if (paths.size === 0) {
50
- this.tagToPaths.delete(tag);
73
+ unregisterPath(urlPath: string, owner?: string): void {
74
+ if (owner === undefined) {
75
+ this.pathOwners.delete(urlPath);
76
+ } else {
77
+ const owners = this.pathOwners.get(urlPath);
78
+ owners?.delete(owner);
79
+ if (owners?.size === 0) {
80
+ this.pathOwners.delete(urlPath);
51
81
  }
52
82
  }
53
- this.pathToTags.delete(urlPath);
83
+ this.reindexPath(urlPath);
54
84
  }
55
85
 
56
86
  reset(): void {
57
87
  this.pathToTags.clear();
88
+ this.pathOwners.clear();
58
89
  this.tagToPaths.clear();
59
90
  }
60
91
  }
@@ -75,8 +106,9 @@ export const autoInvalidateRegistry: Pick<
75
106
  "registerLoaderTags" | "pathsForTags" | "unregisterPath" | "reset"
76
107
  > = {
77
108
  pathsForTags: (tags) => instanceAutoInvalidateRegistry().pathsForTags(tags),
78
- registerLoaderTags: (urlPath, tags) =>
79
- instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags),
109
+ registerLoaderTags: (urlPath, tags, owner) =>
110
+ instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags, owner),
80
111
  reset: () => instanceAutoInvalidateRegistry().reset(),
81
- unregisterPath: (urlPath) => instanceAutoInvalidateRegistry().unregisterPath(urlPath),
112
+ unregisterPath: (urlPath, owner) =>
113
+ instanceAutoInvalidateRegistry().unregisterPath(urlPath, owner),
82
114
  };
@@ -132,6 +132,7 @@ async function invalidateTagsForInstance(
132
132
  let deleted = false;
133
133
  const purgedPaths = new Set<string>();
134
134
  const logicalPurgedPaths = new Set<string>();
135
+ const sharedInvalidations: Promise<{ invalidated: boolean; paths: readonly string[] }>[] = [];
135
136
  const pageCache = getPageCacheAdapter(instance);
136
137
  const sharedResult =
137
138
  pageCache === undefined
@@ -148,12 +149,26 @@ async function invalidateTagsForInstance(
148
149
  ...getAutoInvalidateRegistry(instance).pathsForTags(tagList),
149
150
  ...(sharedResult?.paths ?? []),
150
151
  ]);
152
+ const tags = new Set(tagList);
151
153
  // Registry paths are LOGICAL (unprefixed); the CDN caches the PHYSICAL
152
154
  // request URL, so prefix each with the instance's mount prefix before
153
155
  // queueing it for purge — otherwise a mounted app's `/admin/x` stays stale.
154
156
  for (const path of taggedPaths) {
155
157
  const logicalPath = pathWithoutSearch(path);
158
+ const registered = getAutoInvalidateRegistry(instance)
159
+ .tagsForPath(path)
160
+ .some((tag) => tags.has(tag));
156
161
  const result = revalidatePathForInstance(instance, logicalPath, "page", false);
162
+ if (pageCache !== undefined && registered) {
163
+ sharedInvalidations.push(
164
+ pageCache.invalidate({
165
+ kind: "path",
166
+ scope: instance.prefix,
167
+ path: logicalPath,
168
+ type: "page",
169
+ })
170
+ );
171
+ }
157
172
  deleted = result.deleted || deleted;
158
173
  purgedPaths.add(physicalPath(instance.prefix, logicalPath));
159
174
  logicalPurgedPaths.add(logicalPath);
@@ -162,6 +177,8 @@ async function invalidateTagsForInstance(
162
177
  logicalPurgedPaths.add(purged);
163
178
  }
164
179
  }
180
+ const pathResults = await Promise.all(sharedInvalidations);
181
+ deleted = pathResults.some((result) => result.invalidated) || deleted;
165
182
  if (IS_DEV) {
166
183
  withInstance(instance, () => {
167
184
  const request = currentInstrumentationRequest();
@@ -1,4 +1,5 @@
1
1
  import type { CacheTag } from "../../furin.ts";
2
+ import type { SyncQueryIdentity } from "../../shared/sync-query.ts";
2
3
  import type { RevalidateType } from "../cache/route-cache.ts";
3
4
 
4
5
  export type InvalidationRule =
@@ -14,3 +15,8 @@ export type InvalidationRule =
14
15
  };
15
16
 
16
17
  export type InvalidationInput = InvalidationRule | readonly InvalidationRule[];
18
+
19
+ export type SyncInvalidationInput =
20
+ | InvalidationRule
21
+ | SyncQueryIdentity
22
+ | readonly (InvalidationRule | SyncQueryIdentity)[];