@teyik0/furin 0.2.0-alpha.4 → 0.3.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 (263) hide show
  1. package/README.md +2 -3
  2. package/dist/adapter/bun.d.ts +17 -1
  3. package/dist/adapter/package.d.ts +26 -0
  4. package/dist/adapter/static.d.ts +1 -1
  5. package/dist/build/bun-build.d.ts +3 -0
  6. package/dist/build/client.d.ts +1 -1
  7. package/dist/build/compile-entry.d.ts +3 -3
  8. package/dist/build/compile-entry.ts +88 -83
  9. package/dist/build/entry-template.d.ts +37 -9
  10. package/dist/build/entry-template.ts +109 -56
  11. package/dist/build/hydrate.d.ts +2 -2
  12. package/dist/build/index.js +6817 -4544
  13. package/dist/build/production-instrumentation.d.ts +1 -0
  14. package/dist/build/route-types.d.ts +1 -1
  15. package/dist/build/scan-server.d.ts +10 -4
  16. package/dist/build/server-routes-entry.d.ts +3 -3
  17. package/dist/build/server-routes-entry.ts +5 -11
  18. package/dist/build/ssg-cache.d.ts +1 -1
  19. package/dist/build/types.d.ts +37 -1
  20. package/dist/cli/config.d.ts +6 -1
  21. package/dist/cli/index.js +6854 -4549
  22. package/dist/client/boundaries.d.ts +4 -4
  23. package/dist/client/internal/runtime-types.d.ts +30 -0
  24. package/dist/client/link.d.ts +2 -1
  25. package/dist/client/link.js +712 -338
  26. package/dist/client/router/boundary-tree.d.ts +1 -1
  27. package/dist/client/router/index.d.ts +1 -1
  28. package/dist/client/router/link-utils.d.ts +5 -1
  29. package/dist/client/router/sync-catch-up.d.ts +4 -4
  30. package/dist/client/router/types.d.ts +2 -2
  31. package/dist/client-only.d.ts +1 -0
  32. package/dist/client-only.js +1 -0
  33. package/dist/client.d.ts +108 -111
  34. package/dist/client.js +16 -17
  35. package/dist/config.d.ts +11 -2
  36. package/dist/config.js +7 -3
  37. package/dist/devtools/devtools-element.d.ts +1 -0
  38. package/dist/devtools/protocol.d.ts +119 -0
  39. package/dist/furin.d.ts +40 -21
  40. package/dist/furin.js +7198 -3518
  41. package/dist/isomorphic.d.ts +18 -0
  42. package/dist/plugin/binding-scope.d.ts +2 -0
  43. package/dist/plugin/dead-code-elimination.d.ts +3 -0
  44. package/dist/plugin/index.js +933 -423
  45. package/dist/plugin/transform-client.d.ts +0 -2
  46. package/dist/plugin/transform-isomorphic.d.ts +9 -0
  47. package/dist/rsc/build/environment.d.ts +2 -0
  48. package/dist/rsc/build/index.d.ts +15 -0
  49. package/dist/rsc/client-codec.d.ts +1 -0
  50. package/dist/rsc/codec.d.ts +4 -0
  51. package/dist/rsc/flight-drain.d.ts +11 -0
  52. package/dist/rsc/render-error.d.ts +17 -0
  53. package/dist/rsc/server-codec.d.ts +2 -0
  54. package/dist/rsc/server-codec.js +24 -0
  55. package/dist/rsc/shared.d.ts +30 -0
  56. package/dist/rsc/symbols.d.ts +2 -0
  57. package/dist/rsc/transport-benchmark.d.ts +13 -0
  58. package/dist/rsc/version.d.ts +7 -0
  59. package/dist/rsc-client.d.ts +4 -0
  60. package/dist/rsc-client.js +166 -0
  61. package/dist/rsc-server.d.ts +11 -0
  62. package/dist/rsc.d.ts +11 -0
  63. package/dist/rsc.js +353 -0
  64. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  65. package/dist/server/cache/dev-loader.d.ts +3 -2
  66. package/dist/server/cache/index.d.ts +2 -2
  67. package/dist/server/cache/invalidation.d.ts +20 -2
  68. package/dist/server/cache/isr-ssg.d.ts +5 -1
  69. package/dist/server/cache/isr.d.ts +15 -2
  70. package/dist/server/cache/registry.d.ts +3 -3
  71. package/dist/server/cache/route-cache.d.ts +10 -8
  72. package/dist/server/cache/ssg.d.ts +6 -2
  73. package/dist/server/cache/store-view.d.ts +11 -0
  74. package/dist/server/dev-page-plugin.d.ts +4 -0
  75. package/dist/server/devtools/hub.d.ts +11 -0
  76. package/dist/server/devtools/instrumentation.d.ts +22 -0
  77. package/dist/server/devtools/instrumentation.production.d.ts +44 -0
  78. package/dist/server/devtools/plugin.d.ts +3 -0
  79. package/dist/server/devtools/request-context.d.ts +6 -0
  80. package/dist/server/instance.d.ts +86 -0
  81. package/dist/server/internal.d.ts +18 -1
  82. package/dist/server/internal.js +74 -7
  83. package/dist/server/lang-detect.d.ts +1 -0
  84. package/dist/server/render/assemble.d.ts +4 -0
  85. package/dist/server/render/element.d.ts +1 -1
  86. package/dist/server/render/index.d.ts +2 -2
  87. package/dist/server/render/index.ts +6 -2
  88. package/dist/server/render/loaders.d.ts +8 -0
  89. package/dist/server/render/not-found.d.ts +1 -1
  90. package/dist/server/render/ppr-route.d.ts +8 -0
  91. package/dist/server/render/route-frame-transport.d.ts +2 -0
  92. package/dist/server/render/shell.ts +0 -1
  93. package/dist/server/render/ssr.d.ts +2 -3
  94. package/dist/server/render/template.d.ts +3 -2
  95. package/dist/server/router/discovery.d.ts +1 -1
  96. package/dist/server/router/hmr.d.ts +1 -1
  97. package/dist/server/router/index.d.ts +2 -1
  98. package/dist/server/router/index.js +23 -23
  99. package/dist/server/router/index.ts +1 -1
  100. package/dist/server/router/patterns.d.ts +1 -1
  101. package/dist/server/router/plugin.d.ts +4 -7
  102. package/dist/server/router/schema-merge.d.ts +3 -0
  103. package/dist/server/router/schemas.d.ts +12 -7
  104. package/dist/server/router/types.d.ts +1 -1
  105. package/dist/server/runtime-env.js +2 -2
  106. package/dist/server/sync/adapter.d.ts +45 -26
  107. package/dist/server/sync/config.d.ts +9 -2
  108. package/dist/server/sync/fingerprint.d.ts +2 -1
  109. package/dist/server/sync/index.d.ts +15 -4
  110. package/dist/server/sync/index.js +2868 -0
  111. package/dist/server/sync/notifier.d.ts +18 -0
  112. package/dist/server/sync/plugin.d.ts +53 -54
  113. package/dist/server/sync/postgres/index.d.ts +19 -0
  114. package/dist/server/sync/postgres/index.js +252 -0
  115. package/dist/server/sync/postgres/migrate.d.ts +2 -0
  116. package/dist/server/sync/postgres/migrate.js +16 -0
  117. package/dist/server/sync/postgres/migration.sql +66 -0
  118. package/dist/server/sync/redis/index.d.ts +30 -0
  119. package/dist/server/sync/redis/index.js +336 -0
  120. package/dist/server/sync/redis/scripts.d.ts +4 -0
  121. package/dist/server/sync/response.d.ts +10 -1
  122. package/dist/server/sync/runtime.d.ts +6 -0
  123. package/dist/server/sync/sqlite/index.d.ts +20 -0
  124. package/dist/server/sync/sqlite/index.js +250 -0
  125. package/dist/server/sync/stream.d.ts +12 -4
  126. package/dist/server-only.d.ts +1 -0
  127. package/dist/server-only.js +1 -0
  128. package/dist/shared/parser.d.ts +2 -0
  129. package/dist/shared/prefix.d.ts +15 -0
  130. package/dist/shared/route-frame.d.ts +49 -0
  131. package/dist/shared/utils/index.d.ts +1 -1
  132. package/package.json +59 -12
  133. package/src/adapter/bun.ts +175 -55
  134. package/src/adapter/package.ts +208 -0
  135. package/src/adapter/static.ts +94 -48
  136. package/src/build/bun-build.ts +11 -0
  137. package/src/build/client.ts +31 -27
  138. package/src/build/compile-entry.ts +88 -83
  139. package/src/build/entry-template.ts +109 -56
  140. package/src/build/hydrate.ts +37 -10
  141. package/src/build/index.ts +111 -28
  142. package/src/build/production-instrumentation.ts +14 -0
  143. package/src/build/route-types.ts +7 -8
  144. package/src/build/scan-server.ts +26 -9
  145. package/src/build/server-routes-entry.ts +5 -11
  146. package/src/build/ssg-cache.ts +13 -4
  147. package/src/build/types.ts +38 -1
  148. package/src/cli/config.ts +9 -3
  149. package/src/cli/index.ts +73 -24
  150. package/src/client/boundaries.tsx +42 -18
  151. package/src/client/default-screens.tsx +4 -4
  152. package/src/client/internal/runtime-types.ts +31 -0
  153. package/src/client/link.tsx +35 -37
  154. package/src/client/router/boundary-tree.tsx +2 -2
  155. package/src/client/router/context.ts +13 -9
  156. package/src/client/router/index.ts +3 -0
  157. package/src/client/router/link-utils.ts +66 -3
  158. package/src/client/router/navigation.ts +9 -15
  159. package/src/client/router/provider.tsx +74 -52
  160. package/src/client/router/search/index.ts +5 -7
  161. package/src/client/router/search-store.ts +5 -1
  162. package/src/client/router/spa-response.ts +3 -4
  163. package/src/client/router/sync-catch-up.ts +9 -8
  164. package/src/client/router/types.ts +2 -2
  165. package/src/client/sync.ts +3 -2
  166. package/src/client-only.ts +1 -0
  167. package/src/client.ts +221 -174
  168. package/src/config.ts +14 -1
  169. package/src/devtools/devtools-element.js +884 -0
  170. package/src/devtools/protocol.ts +139 -0
  171. package/src/furin.ts +414 -171
  172. package/src/isomorphic.ts +66 -0
  173. package/src/plugin/binding-scope.ts +158 -0
  174. package/src/plugin/dead-code-elimination.ts +202 -0
  175. package/src/plugin/index.ts +14 -18
  176. package/src/plugin/transform-client.ts +237 -273
  177. package/src/plugin/transform-isomorphic.ts +464 -0
  178. package/src/rsc/build/environment.ts +30 -0
  179. package/src/rsc/build/index.ts +94 -0
  180. package/src/rsc/client-codec.ts +11 -0
  181. package/src/rsc/codec.ts +85 -0
  182. package/src/rsc/flight-drain.ts +68 -0
  183. package/src/rsc/react-server-dom-webpack.d.ts +23 -0
  184. package/src/rsc/render-error.ts +120 -0
  185. package/src/rsc/server-codec.ts +25 -0
  186. package/src/rsc/shared.tsx +113 -0
  187. package/src/rsc/symbols.ts +2 -0
  188. package/src/rsc/transport-benchmark.ts +73 -0
  189. package/src/rsc/version.ts +57 -0
  190. package/src/rsc-client.ts +12 -0
  191. package/src/rsc-server.tsx +91 -0
  192. package/src/rsc.tsx +75 -0
  193. package/src/server/auto-invalidate/registry.ts +23 -1
  194. package/src/server/auto-invalidate/runtime.ts +58 -7
  195. package/src/server/cache/dev-loader.ts +143 -44
  196. package/src/server/cache/index.ts +7 -3
  197. package/src/server/cache/invalidation.ts +100 -41
  198. package/src/server/cache/isr-ssg.ts +12 -3
  199. package/src/server/cache/isr.ts +95 -5
  200. package/src/server/cache/registry.ts +17 -20
  201. package/src/server/cache/route-cache.ts +49 -36
  202. package/src/server/cache/ssg.ts +19 -4
  203. package/src/server/cache/store-view.ts +30 -0
  204. package/src/server/context-logger.ts +7 -7
  205. package/src/server/dev-page-plugin.ts +8 -6
  206. package/src/server/devtools/hub.ts +64 -0
  207. package/src/server/devtools/instrumentation.production.ts +80 -0
  208. package/src/server/devtools/instrumentation.ts +72 -0
  209. package/src/server/devtools/plugin.ts +267 -0
  210. package/src/server/devtools/request-context.ts +76 -0
  211. package/src/server/instance.ts +252 -0
  212. package/src/server/internal.ts +102 -5
  213. package/src/server/lang-detect.ts +14 -10
  214. package/src/server/render/assemble.ts +73 -10
  215. package/src/server/render/element.tsx +11 -8
  216. package/src/server/render/index.ts +6 -2
  217. package/src/server/render/isr.ts +154 -110
  218. package/src/server/render/loaders.ts +259 -45
  219. package/src/server/render/not-found.ts +18 -17
  220. package/src/server/render/ppr-route.ts +146 -0
  221. package/src/server/render/route-frame-transport.ts +62 -0
  222. package/src/server/render/shell.ts +0 -1
  223. package/src/server/render/ssg.ts +6 -5
  224. package/src/server/render/ssr.ts +170 -70
  225. package/src/server/render/template.ts +59 -25
  226. package/src/server/router/discovery.ts +39 -22
  227. package/src/server/router/hmr.ts +15 -13
  228. package/src/server/router/index.ts +1 -1
  229. package/src/server/router/patterns.ts +16 -15
  230. package/src/server/router/plugin.ts +151 -158
  231. package/src/server/router/schema-merge.ts +52 -0
  232. package/src/server/router/schemas.ts +55 -73
  233. package/src/server/router/types.ts +1 -1
  234. package/src/server/sync/adapter.ts +44 -28
  235. package/src/server/sync/config.ts +22 -9
  236. package/src/server/sync/fingerprint.ts +8 -3
  237. package/src/server/sync/index.ts +37 -13
  238. package/src/server/sync/notifier.ts +109 -0
  239. package/src/server/sync/plugin.ts +184 -90
  240. package/src/server/sync/postgres/index.ts +301 -0
  241. package/src/server/sync/postgres/migrate.ts +15 -0
  242. package/src/server/sync/postgres/migration.sql +66 -0
  243. package/src/server/sync/redis/index.ts +342 -0
  244. package/src/server/sync/redis/scripts.ts +74 -0
  245. package/src/server/sync/response.ts +141 -16
  246. package/src/server/sync/runtime.ts +27 -0
  247. package/src/server/sync/sql.d.ts +4 -0
  248. package/src/server/sync/sqlite/index.ts +337 -0
  249. package/src/server/sync/sqlite/migration.sql +70 -0
  250. package/src/server/sync/stream.ts +136 -70
  251. package/src/server-only.ts +1 -0
  252. package/src/shared/await.tsx +4 -4
  253. package/src/shared/deferred-ndjson.ts +34 -6
  254. package/src/shared/digest.ts +2 -2
  255. package/src/shared/parser.ts +5 -0
  256. package/src/shared/prefix.ts +38 -0
  257. package/src/shared/route-frame.ts +429 -0
  258. package/src/shared/search-params.ts +7 -7
  259. package/src/shared/utils/index.ts +5 -3
  260. package/dist/server/dev-inspector.d.ts +0 -89
  261. package/dist/server/sync/memory-adapter.d.ts +0 -17
  262. package/src/server/dev-inspector.ts +0 -60
  263. package/src/server/sync/memory-adapter.ts +0 -196
@@ -17,6 +17,28 @@ const SERVER_RESET_NOOP = () => {
17
17
  /* reset is a client-only action; on the server the response is already committed */
18
18
  };
19
19
 
20
+ function getServerDigest(error: unknown): string | undefined {
21
+ if (isFurinServerError(error)) {
22
+ return error.digest;
23
+ }
24
+ if ((typeof error !== "object" && typeof error !== "function") || error === null) {
25
+ return;
26
+ }
27
+ const digest = (error as Error & { __furinDigest?: unknown }).__furinDigest;
28
+ return typeof digest === "string" ? digest : undefined;
29
+ }
30
+
31
+ function normalizeCaughtError(error: unknown): Error {
32
+ if (error instanceof Error) {
33
+ return error;
34
+ }
35
+ const normalized = new Error(String(error));
36
+ if ((typeof error === "object" && error !== null) || typeof error === "function") {
37
+ Object.defineProperties(normalized, Object.getOwnPropertyDescriptors(error));
38
+ }
39
+ return normalized;
40
+ }
41
+
20
42
  interface ErrorBoundaryProps {
21
43
  children: ReactNode;
22
44
  /**
@@ -46,9 +68,9 @@ interface ErrorBoundaryProps {
46
68
 
47
69
  interface ErrorBoundaryState {
48
70
  /**
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.
71
+ * Digest preserved from a server error or computed when the error was
72
+ * latched. Stored in state so each distinct caught error gets its own ID,
73
+ * instead of all of them inheriting the server-provided `digest` prop.
52
74
  */
53
75
  digest: string | null;
54
76
  /** Unmount/remount counter — bumped on reset to force React to discard
@@ -73,11 +95,14 @@ export class FurinErrorBoundary extends Component<ErrorBoundaryProps, ErrorBound
73
95
  error: null,
74
96
  };
75
97
 
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 };
98
+ static getDerivedStateFromError(error: unknown): Partial<ErrorBoundaryState> {
99
+ // Preserve server digests carried by SPA error sentinels or deferred
100
+ // rejections. Otherwise compute one right at catch-time so the value is
101
+ // anchored to this specific error instance across re-renders.
102
+ return {
103
+ digest: getServerDigest(error) ?? computeErrorDigest(error),
104
+ error: normalizeCaughtError(error),
105
+ };
81
106
  }
82
107
 
83
108
  override componentDidUpdate(prevProps: ErrorBoundaryProps) {
@@ -109,18 +134,17 @@ export class FurinErrorBoundary extends Component<ErrorBoundaryProps, ErrorBound
109
134
  }
110
135
  const Fallback = this.props.fallback ?? DefaultErrorFallback;
111
136
  // 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.
137
+ // 1. A server digest — carried by an SPA error sentinel or serialized
138
+ // deferred rejection so logs correlate without recomputing a
139
+ // different hash from a sanitized error and synthetic stack.
140
+ // 2. state.digest — preserved or computed at catch time for the
141
+ // actual caught error.
118
142
  // 3. props.digest — the server-rendered digest, only meaningful for
119
143
  // the very first error post-hydration; we still honour it as a
120
144
  // last-resort fallback in case state.digest is somehow missing
121
145
  // (e.g. tests that inject state by hand).
122
146
  // 4. recompute on the spot — defensive, should never be reached.
123
- const serverDigest = isFurinServerError(error) ? error.digest : undefined;
147
+ const serverDigest = getServerDigest(error);
124
148
  const finalDigest = serverDigest ?? digest ?? this.props.digest ?? computeErrorDigest(error);
125
149
  const message = this.props.fallback ? error.message : getPublicErrorMessage(error);
126
150
  // Status precedence: FurinServerError carries the original Response.status
@@ -129,7 +153,7 @@ export class FurinErrorBoundary extends Component<ErrorBoundaryProps, ErrorBound
129
153
  const status = isFurinServerError(error) ? error.status : DEFAULT_ERROR_STATUS;
130
154
  return (
131
155
  <Fallback
132
- error={{ message, digest: finalDigest, status }}
156
+ error={{ digest: finalDigest, message, status }}
133
157
  reset={typeof window === "undefined" ? SERVER_RESET_NOOP : this.reset}
134
158
  />
135
159
  );
@@ -168,7 +192,7 @@ export class FurinNotFoundBoundary extends Component<NotFoundBoundaryProps, NotF
168
192
 
169
193
  override componentDidUpdate(prevProps: NotFoundBoundaryProps) {
170
194
  if (prevProps.resetKey !== this.props.resetKey && this.state.error) {
171
- this.setState((s) => ({ error: null, epoch: s.epoch + 1 }));
195
+ this.setState((s) => ({ epoch: s.epoch + 1, error: null }));
172
196
  }
173
197
  }
174
198
 
@@ -179,7 +203,7 @@ export class FurinNotFoundBoundary extends Component<NotFoundBoundaryProps, NotF
179
203
  throw error;
180
204
  }
181
205
  const Fallback = this.props.fallback ?? DefaultNotFoundFallback;
182
- return <Fallback error={{ message: error.message, data: error.data }} />;
206
+ return <Fallback error={{ data: error.data, message: error.message }} />;
183
207
  }
184
208
  return <Fragment key={epoch}>{this.props.children}</Fragment>;
185
209
  }
@@ -147,22 +147,22 @@ const ACTIONS_STYLE: CSSProperties = {
147
147
  };
148
148
 
149
149
  const BUTTON_BASE: CSSProperties = {
150
+ alignItems: "center",
151
+ appearance: "none",
150
152
  border: 0,
153
+ boxSizing: "border-box",
151
154
  cursor: "pointer",
152
155
  display: "inline-flex",
153
156
  fontFamily: FONT_SANS,
154
157
  fontSize: "14px",
155
158
  fontWeight: 500,
159
+ justifyContent: "center",
156
160
  lineHeight: 1,
157
161
  minWidth: "120px",
158
162
  padding: "10px 16px",
159
163
  textAlign: "center",
160
164
  textDecoration: "none",
161
165
  transition: "background-color 120ms ease, color 120ms ease, opacity 120ms ease",
162
- appearance: "none",
163
- alignItems: "center",
164
- justifyContent: "center",
165
- boxSizing: "border-box",
166
166
  };
167
167
 
168
168
  // Outlined buttons: thin neutral border, no fill — discreet enough to blend
@@ -0,0 +1,31 @@
1
+ import type React from "react";
2
+ import type { HeadOptions, RenderingMode, RequestLoaderContext } from "../../client.ts";
3
+
4
+ export type RuntimeData = Record<string, unknown>;
5
+
6
+ type Awaitable<T> = Promise<T> | T;
7
+
8
+ export interface RuntimeRoute {
9
+ __type: "FURIN_ROUTE";
10
+ layout?: React.FC<RuntimeData & { children: React.ReactNode }>;
11
+ loader?: (ctx: RuntimeData) => Awaitable<RuntimeData>;
12
+ mode?: RenderingMode;
13
+ params?: unknown;
14
+ parent?: RuntimeRoute;
15
+ query?: unknown;
16
+ requestLoader?: (ctx: RequestLoaderContext) => Awaitable<object>;
17
+ revalidate?: number;
18
+ tags?: string[];
19
+ }
20
+
21
+ export interface RuntimePage {
22
+ __type: "FURIN_PAGE";
23
+ _route: RuntimeRoute;
24
+ component: React.FC<RuntimeData>;
25
+ head?: (ctx: RuntimeData) => HeadOptions;
26
+ loader?: (ctx: RuntimeData) => Awaitable<RuntimeData>;
27
+ mode?: RenderingMode;
28
+ revalidate?: number;
29
+ staticParams?: () => Awaitable<Record<string, string>[]>;
30
+ tags?: string[];
31
+ }
@@ -4,16 +4,9 @@ import {
4
4
  findSearchDefaultsForRouteTarget,
5
5
  type SearchParamsInput,
6
6
  } from "../shared/search-params.ts";
7
- import {
8
- buildHref,
9
- CLIENT_FALLBACK_ROUTER,
10
- type LinkProps,
11
- normalizeHref,
12
- RouterContext,
13
- type RouterContextValue,
14
- type RouteTo,
15
- useRouter,
16
- } from "./router/index.ts";
7
+ import { CLIENT_FALLBACK_ROUTER, RouterContext, useRouter } from "./router/context.ts";
8
+ import { buildHref, navigationHrefPolicy, normalizeHref } from "./router/link-utils.ts";
9
+ import type { LinkProps, RouterContextValue, RouteTo } from "./router/types.ts";
17
10
 
18
11
  // biome-ignore lint/performance/noBarrelFile: re-exporting router symbols preserves backward compatibility for @teyik0/furin/link consumers
19
12
  export * from "./router/index.ts";
@@ -60,14 +53,19 @@ function computeLinkView<To extends RouteTo>(
60
53
  logicalHref.startsWith("http://") ||
61
54
  logicalHref.startsWith("https://") ||
62
55
  logicalHref.startsWith("//");
63
- const href = isAbsolute ? logicalHref : router.basePath + logicalHref;
56
+ const candidateHref = isAbsolute ? logicalHref : router.basePath + logicalHref;
57
+ const href = navigationHrefPolicy(candidateHref, undefined) === "blocked" ? "#" : candidateHref;
64
58
  const isActive = !isAbsolute && router.currentHref === normalizeHref(logicalHrefWithoutHash);
65
59
  const resolvedChildren = typeof children === "function" ? children({ isActive }) : children;
66
60
  const extraProps: React.AnchorHTMLAttributes<HTMLAnchorElement> = {
67
61
  ...(inactiveProps && !isActive ? inactiveProps() : {}),
68
62
  ...(activeProps ? activeProps({ isActive }) : {}),
69
63
  };
70
- return { logicalHref, href, isActive, resolvedChildren, extraProps };
64
+ return { extraProps, href, isActive, logicalHref, resolvedChildren };
65
+ }
66
+
67
+ function isSameOriginUrl(url: string): boolean {
68
+ return navigationHrefPolicy(url, window.location.origin) === "internal";
71
69
  }
72
70
 
73
71
  /**
@@ -104,7 +102,7 @@ function LinkInteractive<To extends RouteTo>({
104
102
  // logicalHref: route-relative path (no basePath), used for navigation + active state.
105
103
  // physicalHref (href): what the browser sees — basePath + logicalHref.
106
104
  const { logicalHref, href, isActive, resolvedChildren, extraProps } = computeLinkView(
107
- { to, search, hash, children, activeProps, inactiveProps },
105
+ { activeProps, children, hash, inactiveProps, search, to },
108
106
  router
109
107
  );
110
108
  const effectivePreload = preload ?? router.defaultPreload;
@@ -115,6 +113,7 @@ function LinkInteractive<To extends RouteTo>({
115
113
  const triggerPrefetch = useCallback(() => {
116
114
  // prefetch() expects the logical href (no basePath prefix).
117
115
  prefetch(logicalHref, { staleTime: effectiveStaleTime });
116
+ // react-doctor-disable-next-line react-doctor/exhaustive-deps
118
117
  }, [prefetch, logicalHref, effectiveStaleTime]);
119
118
 
120
119
  // "render": preload immediately on mount
@@ -122,6 +121,7 @@ function LinkInteractive<To extends RouteTo>({
122
121
  if (effectivePreload === "render") {
123
122
  triggerPrefetch();
124
123
  }
124
+ // react-doctor-disable-next-line react-doctor/exhaustive-deps
125
125
  }, [effectivePreload, triggerPrefetch]);
126
126
 
127
127
  // "viewport": preload when link enters viewport
@@ -140,6 +140,7 @@ function LinkInteractive<To extends RouteTo>({
140
140
  );
141
141
  observer.observe(anchorRef.current);
142
142
  return () => observer.disconnect();
143
+ // react-doctor-disable-next-line react-doctor/exhaustive-deps
143
144
  }, [effectivePreload, triggerPrefetch]);
144
145
 
145
146
  // Clear any pending intent prefetch timer on unmount to avoid leaking
@@ -154,14 +155,6 @@ function LinkInteractive<To extends RouteTo>({
154
155
  []
155
156
  );
156
157
 
157
- const isInternal = (url: string): boolean => {
158
- try {
159
- return new URL(url, window.location.origin).origin === window.location.origin;
160
- } catch {
161
- return false;
162
- }
163
- };
164
-
165
158
  const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
166
159
  onClick?.(e);
167
160
  if (e.defaultPrevented) {
@@ -179,8 +172,12 @@ function LinkInteractive<To extends RouteTo>({
179
172
  if (anchorProps.target && anchorProps.target !== "_self") {
180
173
  return;
181
174
  }
175
+ if (navigationHrefPolicy(logicalHref, window.location.origin) === "blocked") {
176
+ e.preventDefault();
177
+ return;
178
+ }
182
179
  // Let browser handle external links
183
- if (!isInternal(href)) {
180
+ if (!isSameOriginUrl(href)) {
184
181
  return;
185
182
  }
186
183
  e.preventDefault();
@@ -196,7 +193,7 @@ function LinkInteractive<To extends RouteTo>({
196
193
 
197
194
  const handleMouseEnter = (e: React.MouseEvent<HTMLAnchorElement>) => {
198
195
  onMouseEnter?.(e);
199
- if (!disabled && effectivePreload === "intent" && isInternal(href)) {
196
+ if (!disabled && effectivePreload === "intent" && isSameOriginUrl(href)) {
200
197
  intentTimerRef.current = setTimeout(triggerPrefetch, effectiveDelay);
201
198
  }
202
199
  };
@@ -211,7 +208,7 @@ function LinkInteractive<To extends RouteTo>({
211
208
 
212
209
  const handleFocus = (e: React.FocusEvent<HTMLAnchorElement>) => {
213
210
  onFocus?.(e);
214
- if (!disabled && effectivePreload === "intent" && isInternal(href)) {
211
+ if (!disabled && effectivePreload === "intent" && isSameOriginUrl(href)) {
215
212
  triggerPrefetch();
216
213
  }
217
214
  };
@@ -219,13 +216,13 @@ function LinkInteractive<To extends RouteTo>({
219
216
  return createElement(
220
217
  "a",
221
218
  {
222
- ref: anchorRef,
223
- href,
224
219
  "data-furin-link": true,
220
+ href,
225
221
  onClick: handleClick,
222
+ onFocus: handleFocus,
226
223
  onMouseEnter: handleMouseEnter,
227
224
  onMouseLeave: handleMouseLeave,
228
- onFocus: handleFocus,
225
+ ref: anchorRef,
229
226
  ...(isActive ? { "data-status": "active" } : {}),
230
227
  ...anchorProps,
231
228
  ...extraProps,
@@ -245,20 +242,20 @@ function LinkInteractive<To extends RouteTo>({
245
242
  export const SSR_FALLBACK_ROUTER: RouterContextValue = {
246
243
  basePath: "",
247
244
  currentHref: "/",
248
- search: {},
249
- searchRoutes: [],
250
- navigate: (_href, _opts) => Promise.resolve(),
251
- prefetch: (_href, _opts) => {
245
+ defaultPreload: "intent",
246
+ defaultPreloadDelay: 50,
247
+ defaultPreloadStaleTime: 30_000,
248
+ invalidatePrefetch: (_path, _type) => {
252
249
  /* noop */
253
250
  },
254
- invalidatePrefetch: (_path, _type) => {
251
+ isNavigating: false,
252
+ navigate: (_href, _opts) => Promise.resolve(),
253
+ prefetch: (_href, _opts) => {
255
254
  /* noop */
256
255
  },
257
256
  refresh: (_opts) => Promise.resolve(),
258
- isNavigating: false,
259
- defaultPreload: "intent",
260
- defaultPreloadDelay: 50,
261
- defaultPreloadStaleTime: 30_000,
257
+ search: {},
258
+ searchRoutes: [],
262
259
  };
263
260
 
264
261
  /**
@@ -298,8 +295,8 @@ function renderLinkElement<To extends RouteTo>(
298
295
  return createElement(
299
296
  "a",
300
297
  {
301
- href,
302
298
  "data-furin-link": true,
299
+ href,
303
300
  ...(isActive ? { "data-status": "active" } : {}),
304
301
  ...anchorProps,
305
302
  ...extraProps,
@@ -345,3 +342,4 @@ export function Link<To extends RouteTo>(props: LinkProps<To>): React.ReactEleme
345
342
  // `@teyik0/furin/link` (already a client-only bundle entry) without requiring
346
343
  // apps to add "seroval" as a direct dependency.
347
344
  export { fromCrossJSON } from "seroval";
345
+ export { parseDeferredNdjson } from "../shared/deferred-ndjson.ts";
@@ -1,7 +1,7 @@
1
1
  import type React from "react";
2
2
  import { createElement } from "react";
3
- import type { RuntimeRoute } from "../../client.ts";
4
3
  import { type BoundaryOptions, FurinErrorBoundary, wrapSegmentBoundaries } from "../boundaries.tsx";
4
+ import type { RuntimeRoute } from "../internal/runtime-types.ts";
5
5
  import { FurinServerError } from "../server-error.ts";
6
6
  import { RouterContext } from "./context.ts";
7
7
  import type {
@@ -94,7 +94,7 @@ export function buildPageElement(
94
94
  // Inside-out: at each non-root depth wrap the subtree with its same-depth
95
95
  // boundary (so the boundary sits INSIDE the layout), then wrap with the
96
96
  // layout itself when this route declares one.
97
- for (let i = chain.length - 1; i >= rootOffset; i--) {
97
+ for (let i = chain.length - 1; i >= rootOffset; i -= 1) {
98
98
  element = wrapSegmentBoundaries(element, byDepth.get(i), options);
99
99
  const Layout = chain[i]?.layout;
100
100
  if (Layout) {
@@ -1,4 +1,5 @@
1
1
  import { createContext, useContext } from "react";
2
+ import { navigationHrefPolicy } from "./link-utils.ts";
2
3
  import type { RouterContextValue } from "./types.ts";
3
4
 
4
5
  export const RouterContext = createContext<RouterContextValue | null>(null);
@@ -8,26 +9,29 @@ export const CLIENT_FALLBACK_ROUTER: RouterContextValue = {
8
9
  // Use the same "/" as SSR_FALLBACK_ROUTER so SSR and client render the
9
10
  // same active-state when no RouterProvider is present, avoiding hydration mismatches.
10
11
  currentHref: "/",
11
- search: {},
12
- searchRoutes: [],
12
+ defaultPreload: "intent",
13
+ defaultPreloadDelay: 50,
14
+ defaultPreloadStaleTime: 30_000,
15
+ invalidatePrefetch: (_path, _type) => {
16
+ /* noop fallback */
17
+ },
18
+ isNavigating: false,
13
19
  navigate: (href, _opts) => {
20
+ if (navigationHrefPolicy(href, window.location.origin) === "blocked") {
21
+ return Promise.reject(new Error("[furin] Unsafe navigation URL."));
22
+ }
14
23
  window.location.href = href;
15
24
  return Promise.resolve();
16
25
  },
17
26
  prefetch: (_href, _opts) => {
18
27
  /* noop fallback */
19
28
  },
20
- invalidatePrefetch: (_path, _type) => {
21
- /* noop fallback */
22
- },
23
29
  refresh: (_opts) => {
24
30
  window.location.reload();
25
31
  return Promise.resolve();
26
32
  },
27
- isNavigating: false,
28
- defaultPreload: "intent",
29
- defaultPreloadDelay: 50,
30
- defaultPreloadStaleTime: 30_000,
33
+ search: {},
34
+ searchRoutes: [],
31
35
  };
32
36
 
33
37
  /**
@@ -11,6 +11,9 @@ export {
11
11
  export {
12
12
  applyRevalidateHeader,
13
13
  buildHref,
14
+ decodeHashFragment,
15
+ isSameOriginFetchResult,
16
+ navigationHrefPolicy,
14
17
  normalizeHref,
15
18
  normalizePath,
16
19
  shouldAutoRefreshPath,
@@ -5,6 +5,69 @@ const HASH_FRAGMENT_RE = /#.*$/;
5
5
  /** Strips one or more trailing slashes — used by `buildDataEndpoint` in static mode. */
6
6
  export const TRAILING_SLASHES_RE = /\/+$/;
7
7
 
8
+ export type NavigationHrefPolicy = "blocked" | "external" | "internal";
9
+
10
+ export function navigationHrefPolicy(
11
+ href: string,
12
+ currentOrigin: string | undefined
13
+ ): NavigationHrefPolicy {
14
+ for (let index = 0; index < href.length; index += 1) {
15
+ const code = href.charCodeAt(index);
16
+ if (code <= 31 || code === 127) {
17
+ return "blocked";
18
+ }
19
+ }
20
+ let url: URL;
21
+ try {
22
+ url = new URL(href, currentOrigin ?? "http://localhost");
23
+ } catch {
24
+ return "blocked";
25
+ }
26
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
27
+ return "blocked";
28
+ }
29
+ if (currentOrigin === undefined) {
30
+ return href.startsWith("http://") || href.startsWith("https://") || href.startsWith("//")
31
+ ? "external"
32
+ : "internal";
33
+ }
34
+ return url.origin === currentOrigin ? "internal" : "external";
35
+ }
36
+
37
+ export function decodeHashFragment(fragment: string): string {
38
+ try {
39
+ return decodeURIComponent(fragment);
40
+ } catch {
41
+ return fragment;
42
+ }
43
+ }
44
+
45
+ export function isSameOriginFetchResult(
46
+ input: RequestInfo | URL,
47
+ responseUrl: string,
48
+ currentOrigin: string
49
+ ): boolean {
50
+ if (responseUrl.length === 0) {
51
+ return false;
52
+ }
53
+ try {
54
+ let inputUrl: string;
55
+ if (typeof input === "string") {
56
+ inputUrl = input;
57
+ } else if (input instanceof URL) {
58
+ inputUrl = input.href;
59
+ } else {
60
+ inputUrl = input.url;
61
+ }
62
+ return (
63
+ new URL(inputUrl, currentOrigin).origin === currentOrigin &&
64
+ new URL(responseUrl, currentOrigin).origin === currentOrigin
65
+ );
66
+ } catch {
67
+ return false;
68
+ }
69
+ }
70
+
8
71
  /**
9
72
  * Builds a full href string from a pathname, optional search params, and optional hash.
10
73
  * Null/undefined search values are omitted.
@@ -90,7 +153,7 @@ export function stripHashFromHref(href: string): string {
90
153
  * Query strings and hashes are preserved and re-attached after normalization. */
91
154
  export function normalizeHref(href: string): string {
92
155
  const url = new URL(href, "http://localhost");
93
- let pathname = url.pathname;
156
+ let { pathname } = url;
94
157
  if (pathname !== "/") {
95
158
  pathname = pathname.replace(/\/+$/g, "");
96
159
  }
@@ -118,7 +181,7 @@ export function shouldRefetch(entry: CacheEntry): boolean {
118
181
  * @internal Exported for unit testing only.
119
182
  */
120
183
  export function shouldInterceptClick(
121
- anchor: { href: string; target: string; hasAttribute(name: string): boolean },
184
+ anchor: { href: string; target: string; hasAttribute: (name: string) => boolean },
122
185
  event: { metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean },
123
186
  basePath: string,
124
187
  currentOrigin: string,
@@ -136,7 +199,7 @@ export function shouldInterceptClick(
136
199
  return null;
137
200
  }
138
201
 
139
- const href = anchor.href;
202
+ const { href } = anchor;
140
203
  if (!href) {
141
204
  return null;
142
205
  }
@@ -4,7 +4,7 @@ import {
4
4
  type SearchParamsInput,
5
5
  } from "../../shared/search-params.ts";
6
6
  import { useRouter } from "./context.ts";
7
- import { buildHref } from "./link-utils.ts";
7
+ import { buildHref, navigationHrefPolicy } from "./link-utils.ts";
8
8
  import type { RouteSearch, RouteTo } from "./types.ts";
9
9
 
10
10
  export interface NavigateInput<To extends RouteTo> {
@@ -18,19 +18,6 @@ export interface NavigateInput<To extends RouteTo> {
18
18
  export type Navigate = <To extends RouteTo>(next: NavigateInput<To>) => Promise<void>;
19
19
  type NavigateOptions = Parameters<ReturnType<typeof useRouter>["navigate"]>[1];
20
20
 
21
- function isExternalAbsoluteHref(href: string): boolean {
22
- if (href.startsWith("//")) {
23
- return true;
24
- }
25
- if (!(href.startsWith("http://") || href.startsWith("https://"))) {
26
- return false;
27
- }
28
- if (typeof window === "undefined") {
29
- return true;
30
- }
31
- return new URL(href).origin !== window.location.origin;
32
- }
33
-
34
21
  export function useNavigate(): Navigate {
35
22
  const router = useRouter();
36
23
 
@@ -46,7 +33,14 @@ export function useNavigate(): Navigate {
46
33
  next.hash,
47
34
  searchDefaults
48
35
  );
49
- if (isExternalAbsoluteHref(href) && typeof window !== "undefined") {
36
+ const policy = navigationHrefPolicy(
37
+ href,
38
+ typeof window === "undefined" ? undefined : window.location.origin
39
+ );
40
+ if (policy === "blocked") {
41
+ return Promise.reject(new Error("[furin] Unsafe navigation URL."));
42
+ }
43
+ if (policy === "external" && typeof window !== "undefined") {
50
44
  window.location.assign(href);
51
45
  return Promise.resolve();
52
46
  }