@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.
- package/README.md +2 -3
- package/dist/adapter/bun.d.ts +17 -1
- package/dist/adapter/package.d.ts +26 -0
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +3 -3
- package/dist/build/compile-entry.ts +88 -83
- package/dist/build/entry-template.d.ts +37 -9
- package/dist/build/entry-template.ts +109 -56
- package/dist/build/hydrate.d.ts +2 -2
- package/dist/build/index.js +6817 -4544
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/scan-server.d.ts +10 -4
- package/dist/build/server-routes-entry.d.ts +3 -3
- package/dist/build/server-routes-entry.ts +5 -11
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +37 -1
- package/dist/cli/config.d.ts +6 -1
- package/dist/cli/index.js +6854 -4549
- package/dist/client/boundaries.d.ts +4 -4
- package/dist/client/internal/runtime-types.d.ts +30 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +712 -338
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +5 -1
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +2 -2
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +108 -111
- package/dist/client.js +16 -17
- package/dist/config.d.ts +11 -2
- package/dist/config.js +7 -3
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +40 -21
- package/dist/furin.js +7198 -3518
- package/dist/isomorphic.d.ts +18 -0
- package/dist/plugin/binding-scope.d.ts +2 -0
- package/dist/plugin/dead-code-elimination.d.ts +3 -0
- package/dist/plugin/index.js +933 -423
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -0
- package/dist/rsc/build/environment.d.ts +2 -0
- package/dist/rsc/build/index.d.ts +15 -0
- package/dist/rsc/client-codec.d.ts +1 -0
- package/dist/rsc/codec.d.ts +4 -0
- package/dist/rsc/flight-drain.d.ts +11 -0
- package/dist/rsc/render-error.d.ts +17 -0
- package/dist/rsc/server-codec.d.ts +2 -0
- package/dist/rsc/server-codec.js +24 -0
- package/dist/rsc/shared.d.ts +30 -0
- package/dist/rsc/symbols.d.ts +2 -0
- package/dist/rsc/transport-benchmark.d.ts +13 -0
- package/dist/rsc/version.d.ts +7 -0
- package/dist/rsc-client.d.ts +4 -0
- package/dist/rsc-client.js +166 -0
- package/dist/rsc-server.d.ts +11 -0
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +353 -0
- package/dist/server/auto-invalidate/registry.d.ts +8 -1
- package/dist/server/cache/dev-loader.d.ts +3 -2
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +20 -2
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +15 -2
- package/dist/server/cache/registry.d.ts +3 -3
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/ssg.d.ts +6 -2
- package/dist/server/cache/store-view.d.ts +11 -0
- package/dist/server/dev-page-plugin.d.ts +4 -0
- package/dist/server/devtools/hub.d.ts +11 -0
- package/dist/server/devtools/instrumentation.d.ts +22 -0
- package/dist/server/devtools/instrumentation.production.d.ts +44 -0
- package/dist/server/devtools/plugin.d.ts +3 -0
- package/dist/server/devtools/request-context.d.ts +6 -0
- package/dist/server/instance.d.ts +86 -0
- package/dist/server/internal.d.ts +18 -1
- package/dist/server/internal.js +74 -7
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/render/assemble.d.ts +4 -0
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -2
- package/dist/server/render/loaders.d.ts +8 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-route.d.ts +8 -0
- package/dist/server/render/route-frame-transport.d.ts +2 -0
- package/dist/server/render/shell.ts +0 -1
- package/dist/server/render/ssr.d.ts +2 -3
- package/dist/server/render/template.d.ts +3 -2
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/index.d.ts +2 -1
- package/dist/server/router/index.js +23 -23
- package/dist/server/router/index.ts +1 -1
- package/dist/server/router/patterns.d.ts +1 -1
- package/dist/server/router/plugin.d.ts +4 -7
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +12 -7
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/runtime-env.js +2 -2
- package/dist/server/sync/adapter.d.ts +45 -26
- package/dist/server/sync/config.d.ts +9 -2
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +2868 -0
- package/dist/server/sync/notifier.d.ts +18 -0
- package/dist/server/sync/plugin.d.ts +53 -54
- package/dist/server/sync/postgres/index.d.ts +19 -0
- package/dist/server/sync/postgres/index.js +252 -0
- package/dist/server/sync/postgres/migrate.d.ts +2 -0
- package/dist/server/sync/postgres/migrate.js +16 -0
- package/dist/server/sync/postgres/migration.sql +66 -0
- package/dist/server/sync/redis/index.d.ts +30 -0
- package/dist/server/sync/redis/index.js +336 -0
- package/dist/server/sync/redis/scripts.d.ts +4 -0
- package/dist/server/sync/response.d.ts +10 -1
- package/dist/server/sync/runtime.d.ts +6 -0
- package/dist/server/sync/sqlite/index.d.ts +20 -0
- package/dist/server/sync/sqlite/index.js +250 -0
- package/dist/server/sync/stream.d.ts +12 -4
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/prefix.d.ts +15 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/utils/index.d.ts +1 -1
- package/package.json +59 -12
- package/src/adapter/bun.ts +175 -55
- package/src/adapter/package.ts +208 -0
- package/src/adapter/static.ts +94 -48
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +31 -27
- package/src/build/compile-entry.ts +88 -83
- package/src/build/entry-template.ts +109 -56
- package/src/build/hydrate.ts +37 -10
- package/src/build/index.ts +111 -28
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +7 -8
- package/src/build/scan-server.ts +26 -9
- package/src/build/server-routes-entry.ts +5 -11
- package/src/build/ssg-cache.ts +13 -4
- package/src/build/types.ts +38 -1
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +73 -24
- package/src/client/boundaries.tsx +42 -18
- package/src/client/default-screens.tsx +4 -4
- package/src/client/internal/runtime-types.ts +31 -0
- package/src/client/link.tsx +35 -37
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +66 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +74 -52
- package/src/client/router/search/index.ts +5 -7
- package/src/client/router/search-store.ts +5 -1
- package/src/client/router/spa-response.ts +3 -4
- package/src/client/router/sync-catch-up.ts +9 -8
- package/src/client/router/types.ts +2 -2
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +221 -174
- package/src/config.ts +14 -1
- package/src/devtools/devtools-element.js +884 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +414 -171
- package/src/isomorphic.ts +66 -0
- package/src/plugin/binding-scope.ts +158 -0
- package/src/plugin/dead-code-elimination.ts +202 -0
- package/src/plugin/index.ts +14 -18
- package/src/plugin/transform-client.ts +237 -273
- package/src/plugin/transform-isomorphic.ts +464 -0
- package/src/rsc/build/environment.ts +30 -0
- package/src/rsc/build/index.ts +94 -0
- package/src/rsc/client-codec.ts +11 -0
- package/src/rsc/codec.ts +85 -0
- package/src/rsc/flight-drain.ts +68 -0
- package/src/rsc/react-server-dom-webpack.d.ts +23 -0
- package/src/rsc/render-error.ts +120 -0
- package/src/rsc/server-codec.ts +25 -0
- package/src/rsc/shared.tsx +113 -0
- package/src/rsc/symbols.ts +2 -0
- package/src/rsc/transport-benchmark.ts +73 -0
- package/src/rsc/version.ts +57 -0
- package/src/rsc-client.ts +12 -0
- package/src/rsc-server.tsx +91 -0
- package/src/rsc.tsx +75 -0
- package/src/server/auto-invalidate/registry.ts +23 -1
- package/src/server/auto-invalidate/runtime.ts +58 -7
- package/src/server/cache/dev-loader.ts +143 -44
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +100 -41
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +95 -5
- package/src/server/cache/registry.ts +17 -20
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/ssg.ts +19 -4
- package/src/server/cache/store-view.ts +30 -0
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +8 -6
- package/src/server/devtools/hub.ts +64 -0
- package/src/server/devtools/instrumentation.production.ts +80 -0
- package/src/server/devtools/instrumentation.ts +72 -0
- package/src/server/devtools/plugin.ts +267 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +252 -0
- package/src/server/internal.ts +102 -5
- package/src/server/lang-detect.ts +14 -10
- package/src/server/render/assemble.ts +73 -10
- package/src/server/render/element.tsx +11 -8
- package/src/server/render/index.ts +6 -2
- package/src/server/render/isr.ts +154 -110
- package/src/server/render/loaders.ts +259 -45
- package/src/server/render/not-found.ts +18 -17
- package/src/server/render/ppr-route.ts +146 -0
- package/src/server/render/route-frame-transport.ts +62 -0
- package/src/server/render/shell.ts +0 -1
- package/src/server/render/ssg.ts +6 -5
- package/src/server/render/ssr.ts +170 -70
- package/src/server/render/template.ts +59 -25
- package/src/server/router/discovery.ts +39 -22
- package/src/server/router/hmr.ts +15 -13
- package/src/server/router/index.ts +1 -1
- package/src/server/router/patterns.ts +16 -15
- package/src/server/router/plugin.ts +151 -158
- package/src/server/router/schema-merge.ts +52 -0
- package/src/server/router/schemas.ts +55 -73
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +22 -9
- package/src/server/sync/fingerprint.ts +8 -3
- package/src/server/sync/index.ts +37 -13
- package/src/server/sync/notifier.ts +109 -0
- package/src/server/sync/plugin.ts +184 -90
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +15 -0
- package/src/server/sync/postgres/migration.sql +66 -0
- package/src/server/sync/redis/index.ts +342 -0
- package/src/server/sync/redis/scripts.ts +74 -0
- package/src/server/sync/response.ts +141 -16
- package/src/server/sync/runtime.ts +27 -0
- package/src/server/sync/sql.d.ts +4 -0
- package/src/server/sync/sqlite/index.ts +337 -0
- package/src/server/sync/sqlite/migration.sql +70 -0
- package/src/server/sync/stream.ts +136 -70
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +4 -4
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/parser.ts +5 -0
- package/src/shared/prefix.ts +38 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +5 -3
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/server/dev-inspector.ts +0 -60
- 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
|
|
50
|
-
* each distinct caught error gets its own ID,
|
|
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:
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
return {
|
|
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.
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
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 =
|
|
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={{
|
|
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) => ({
|
|
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={{
|
|
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
|
+
}
|
package/src/client/link.tsx
CHANGED
|
@@ -4,16 +4,9 @@ import {
|
|
|
4
4
|
findSearchDefaultsForRouteTarget,
|
|
5
5
|
type SearchParamsInput,
|
|
6
6
|
} from "../shared/search-params.ts";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
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 {
|
|
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
|
-
{
|
|
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 (!
|
|
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" &&
|
|
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" &&
|
|
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
|
-
|
|
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
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
245
|
+
defaultPreload: "intent",
|
|
246
|
+
defaultPreloadDelay: 50,
|
|
247
|
+
defaultPreloadStaleTime: 30_000,
|
|
248
|
+
invalidatePrefetch: (_path, _type) => {
|
|
252
249
|
/* noop */
|
|
253
250
|
},
|
|
254
|
-
|
|
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
|
-
|
|
259
|
-
|
|
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
|
-
|
|
12
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
defaultPreloadDelay: 50,
|
|
30
|
-
defaultPreloadStaleTime: 30_000,
|
|
33
|
+
search: {},
|
|
34
|
+
searchRoutes: [],
|
|
31
35
|
};
|
|
32
36
|
|
|
33
37
|
/**
|
|
@@ -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
|
|
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)
|
|
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
|
|
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
|
-
|
|
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
|
}
|