@teyik0/furin 0.6.0-alpha.1 → 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.
- package/dist/adapter/runtime-build.d.ts +1 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/entry-template.d.ts +3 -0
- package/dist/build/entry-template.ts +7 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +1671 -276
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +1672 -286
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -0
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +932 -118
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +49 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1219 -59
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +78 -32
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +1 -2
- package/dist/furin.js +2448 -710
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +349 -18
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +1 -0
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +5 -19
- package/dist/server/internal.d.ts +3 -0
- package/dist/server/internal.js +55 -35
- package/dist/server/production-server.js +54 -34
- package/dist/server/render/loaders.d.ts +4 -2
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +2 -1
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +533 -105
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +34 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +29 -6
- package/src/adapter/package.ts +12 -2
- package/src/adapter/runtime-build.ts +63 -12
- package/src/adapter/static.ts +7 -2
- package/src/adapter/vercel.ts +15 -13
- package/src/build/client.ts +23 -5
- package/src/build/entry-template.ts +7 -0
- package/src/build/hydrate.ts +17 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/browser-events-runtime.ts +4 -0
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -1
- package/src/client/document.tsx +28 -18
- package/src/client/hmr.ts +2 -1
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +7 -2
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +304 -44
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +71 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +113 -55
- package/src/env.d.ts +2 -2
- package/src/furin.ts +75 -13
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/routes.ts +3 -3
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/server/auto-invalidate/registry.ts +4 -0
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/dev/graph.ts +16 -2
- package/src/server/dev-page-plugin.ts +93 -59
- package/src/server/internal.ts +3 -0
- package/src/server/render/document.tsx +10 -1
- package/src/server/render/element.tsx +7 -2
- package/src/server/render/isr.ts +61 -29
- package/src/server/render/loaders.ts +118 -47
- package/src/server/render/mixed-cache.ts +33 -4
- package/src/server/render/not-found.ts +4 -7
- package/src/server/render/ppr-document.ts +2 -1
- package/src/server/render/ppr-route.ts +30 -10
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +51 -18
- package/src/server/render/ssr.ts +53 -8
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/hmr.ts +3 -2
- package/src/server/router/plugin.ts +27 -12
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +52 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
package/src/client/sync.ts
CHANGED
|
@@ -1,46 +1,132 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type PluginTypeFn,
|
|
3
|
+
type PluginVerbContext,
|
|
4
|
+
type Treaty,
|
|
5
|
+
type TreatyPlugin,
|
|
6
|
+
treaty,
|
|
7
|
+
} from "@elysia/eden";
|
|
8
|
+
import type { RouteMap, RoutePatternMap } from "@teyik0/furin/routes";
|
|
9
|
+
import type { AnyElysia } from "elysia";
|
|
10
|
+
import type { RouteLoaderData } from "../define-route.ts";
|
|
11
|
+
import type { QueryIdentity } from "../shared/sync-query.ts";
|
|
12
|
+
import {
|
|
13
|
+
currentQueryEnvironment,
|
|
14
|
+
QUERY_REFERENCE,
|
|
15
|
+
type QueryData,
|
|
16
|
+
type QueryMethod,
|
|
17
|
+
type QueryProjection,
|
|
18
|
+
QueryStore,
|
|
19
|
+
type ReadReference,
|
|
20
|
+
type ReadResult,
|
|
21
|
+
readReference,
|
|
22
|
+
readUrl,
|
|
23
|
+
} from "./query-store.ts";
|
|
24
|
+
import {
|
|
25
|
+
findOptimisticRuntime,
|
|
26
|
+
type OptimisticRuntime,
|
|
27
|
+
SYNC_REQUEST,
|
|
28
|
+
} from "./router/optimistic.ts";
|
|
29
|
+
import type { RouteSearch } from "./router/types.ts";
|
|
2
30
|
|
|
3
|
-
export interface
|
|
4
|
-
|
|
5
|
-
|
|
31
|
+
export interface OptimisticCache {
|
|
32
|
+
update: {
|
|
33
|
+
<Method extends QueryMethod>(
|
|
34
|
+
method: Method,
|
|
35
|
+
transform: (data: QueryData<Method>) => QueryData<Method>,
|
|
36
|
+
...args: object extends NonNullable<Parameters<Method>[0]>
|
|
37
|
+
? [options?: NonNullable<Parameters<Method>[0]>]
|
|
38
|
+
: [options: NonNullable<Parameters<Method>[0]>]
|
|
39
|
+
): void;
|
|
40
|
+
<Pattern extends keyof RoutePatternMap & string>(
|
|
41
|
+
pattern: Pattern,
|
|
42
|
+
transform: (
|
|
43
|
+
loader: RouteLoaderData<RoutePatternMap[Pattern]>
|
|
44
|
+
) => RouteLoaderData<RoutePatternMap[Pattern]>
|
|
45
|
+
): void;
|
|
46
|
+
<Path extends keyof RouteMap & string>(
|
|
47
|
+
target: Path | { path: Path; search?: RouteSearch<Path> },
|
|
48
|
+
transform: (loader: RouteLoaderData<RouteMap[Path]>) => RouteLoaderData<RouteMap[Path]>
|
|
49
|
+
): void;
|
|
50
|
+
};
|
|
6
51
|
}
|
|
7
52
|
|
|
8
|
-
export interface
|
|
9
|
-
|
|
53
|
+
export interface SyncCallOptions {
|
|
54
|
+
optimistic?: (cache: OptimisticCache) => void;
|
|
55
|
+
/** Additional attempts for Furin's explicit in-progress response. Never retries ambiguous writes. */
|
|
56
|
+
retry?: number;
|
|
10
57
|
}
|
|
11
58
|
|
|
12
|
-
export interface
|
|
13
|
-
|
|
14
|
-
input: TInput;
|
|
59
|
+
export interface SyncClientOptions {
|
|
60
|
+
retry?: number;
|
|
15
61
|
}
|
|
16
62
|
|
|
17
|
-
|
|
18
|
-
|
|
63
|
+
interface SyncPluginType extends PluginTypeFn {
|
|
64
|
+
callOptions: SyncCallOptions;
|
|
65
|
+
output: this["node"] extends { get: object }
|
|
66
|
+
? { get: { readonly [QUERY_REFERENCE]: true } }
|
|
67
|
+
: object;
|
|
19
68
|
}
|
|
20
69
|
|
|
21
|
-
|
|
22
|
-
error: unknown;
|
|
23
|
-
|
|
70
|
+
interface MutationResult {
|
|
71
|
+
error: { value?: unknown } | null;
|
|
72
|
+
response?: Response;
|
|
24
73
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
74
|
+
interface Operation {
|
|
75
|
+
method?: string;
|
|
76
|
+
projection?: Projection;
|
|
77
|
+
result?: MutationResult;
|
|
78
|
+
runtime?: OptimisticRuntime;
|
|
30
79
|
}
|
|
80
|
+
interface Projection {
|
|
81
|
+
outcome: "success" | "error" | "ambiguous";
|
|
82
|
+
pending: number;
|
|
83
|
+
queries: QueryStore;
|
|
84
|
+
queryToken: QueryProjection;
|
|
85
|
+
remove: () => void;
|
|
86
|
+
response?: Response;
|
|
87
|
+
runtime?: OptimisticRuntime;
|
|
88
|
+
token?: ReturnType<OptimisticRuntime["begin"]>;
|
|
89
|
+
}
|
|
90
|
+
type Projections = Map<object, Map<string, Projection>>;
|
|
31
91
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
92
|
+
function queryProjectionCache(
|
|
93
|
+
queries: QueryStore,
|
|
94
|
+
token: QueryProjection,
|
|
95
|
+
routeCache: OptimisticCache
|
|
96
|
+
): OptimisticCache {
|
|
97
|
+
return {
|
|
98
|
+
update(
|
|
99
|
+
destination: QueryMethod | string | { path: string },
|
|
100
|
+
transform: (data: unknown) => unknown,
|
|
101
|
+
queryOptions?: object
|
|
102
|
+
) {
|
|
103
|
+
if (typeof destination === "function") {
|
|
104
|
+
const reference = readReference(destination);
|
|
105
|
+
const url = readUrl(reference, queryOptions);
|
|
106
|
+
queries.bind(url, () => reference.load(queryOptions), reference.client);
|
|
107
|
+
queries.update(token, url, transform);
|
|
108
|
+
} else {
|
|
109
|
+
Reflect.apply(routeCache.update, routeCache, [destination, transform]);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
} as unknown as OptimisticCache;
|
|
43
113
|
}
|
|
114
|
+
const OPERATION = Symbol("furin.sync.operation");
|
|
115
|
+
interface CallOptions extends SyncCallOptions {
|
|
116
|
+
fetch?: RequestInit;
|
|
117
|
+
headers?: HeaderSource;
|
|
118
|
+
query?: object;
|
|
119
|
+
[OPERATION]: Operation;
|
|
120
|
+
}
|
|
121
|
+
type HeaderSource =
|
|
122
|
+
| HeadersInit
|
|
123
|
+
| ((
|
|
124
|
+
path: string,
|
|
125
|
+
init: RequestInit
|
|
126
|
+
) => HeaderSource | undefined | Promise<HeaderSource | undefined>)
|
|
127
|
+
| HeaderSource[];
|
|
128
|
+
type Callable = (...args: unknown[]) => unknown;
|
|
129
|
+
const MUTATIONS = new Set(["post", "put", "patch", "delete"]);
|
|
44
130
|
|
|
45
131
|
function createIdempotencyKey(): string {
|
|
46
132
|
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
@@ -49,52 +135,339 @@ function createIdempotencyKey(): string {
|
|
|
49
135
|
return `${Date.now()}-${Math.random()}`;
|
|
50
136
|
}
|
|
51
137
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
138
|
+
const plugin: TreatyPlugin<SyncPluginType> = {
|
|
139
|
+
name: "furin-sync",
|
|
140
|
+
verbs: { __furinQuery: (context) => context },
|
|
141
|
+
before(context) {
|
|
142
|
+
const options = context.options as CallOptions | undefined;
|
|
143
|
+
const operation = options?.[OPERATION];
|
|
144
|
+
if (!(operation && options)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (!operation.runtime) {
|
|
148
|
+
operation.runtime = findOptimisticRuntime(context.domain);
|
|
149
|
+
}
|
|
150
|
+
operation.method = context.method;
|
|
151
|
+
if (operation.runtime) {
|
|
152
|
+
options.fetch = { ...options.fetch, [SYNC_REQUEST]: true } as RequestInit;
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
after(result, context) {
|
|
156
|
+
const operation = (context.options as CallOptions | undefined)?.[OPERATION];
|
|
157
|
+
if (operation) {
|
|
158
|
+
operation.result = result;
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
async function resolveHeaders(
|
|
164
|
+
source: HeaderSource | undefined,
|
|
165
|
+
// biome-ignore lint/suspicious/noParametersOnlyUsedInRecursion: passed to Eden header callbacks.
|
|
166
|
+
path: string,
|
|
167
|
+
// biome-ignore lint/suspicious/noParametersOnlyUsedInRecursion: passed to Eden header callbacks.
|
|
168
|
+
init: RequestInit
|
|
169
|
+
): Promise<Headers> {
|
|
170
|
+
if (typeof source === "function") {
|
|
171
|
+
return resolveHeaders(await source(path, init), path, init);
|
|
55
172
|
}
|
|
173
|
+
if (
|
|
174
|
+
Array.isArray(source) &&
|
|
175
|
+
!(source.length > 0 && Array.isArray(source[0]) && typeof source[0][0] === "string")
|
|
176
|
+
) {
|
|
177
|
+
const headers = new Headers();
|
|
178
|
+
for (const entry of source) {
|
|
179
|
+
// biome-ignore lint/performance/noAwaitInLoops: header callbacks must retain Eden's ordering.
|
|
180
|
+
const resolved = await resolveHeaders(entry as HeaderSource, path, init);
|
|
181
|
+
resolved.forEach((value, name) => {
|
|
182
|
+
headers.set(name, value);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return headers;
|
|
186
|
+
}
|
|
187
|
+
return new Headers(source as HeadersInit | undefined);
|
|
188
|
+
}
|
|
56
189
|
|
|
57
|
-
|
|
58
|
-
|
|
190
|
+
function inProgress(result: MutationResult): boolean {
|
|
191
|
+
const value = result.error?.value;
|
|
192
|
+
return (
|
|
193
|
+
result.response?.status === 409 &&
|
|
194
|
+
typeof value === "object" &&
|
|
195
|
+
value !== null &&
|
|
196
|
+
"code" in value &&
|
|
197
|
+
value.code === "FURIN_MUTATION_IN_PROGRESS"
|
|
198
|
+
);
|
|
59
199
|
}
|
|
60
200
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
201
|
+
function retryDelay(response: Response | undefined, attempt: number): number {
|
|
202
|
+
const header = response?.headers.get("Retry-After");
|
|
203
|
+
if (header !== null && header !== undefined) {
|
|
204
|
+
const seconds = Number(header);
|
|
205
|
+
if (Number.isFinite(seconds) && seconds >= 0) {
|
|
206
|
+
return seconds * 1000;
|
|
207
|
+
}
|
|
208
|
+
const date = Date.parse(header);
|
|
209
|
+
if (Number.isFinite(date)) {
|
|
210
|
+
return Math.max(0, date - Date.now());
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return Math.min(250 * 2 ** attempt, 4000);
|
|
214
|
+
}
|
|
68
215
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
216
|
+
function delay(ms: number, signal: AbortSignal | null | undefined): Promise<boolean> {
|
|
217
|
+
if (signal?.aborted) {
|
|
218
|
+
return Promise.resolve(false);
|
|
219
|
+
}
|
|
220
|
+
return new Promise((resolve) => {
|
|
221
|
+
const abort = () => {
|
|
222
|
+
clearTimeout(timer);
|
|
223
|
+
resolve(false);
|
|
224
|
+
};
|
|
225
|
+
const timer = setTimeout(() => {
|
|
226
|
+
signal?.removeEventListener("abort", abort);
|
|
227
|
+
resolve(true);
|
|
228
|
+
}, ms);
|
|
229
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
230
|
+
});
|
|
231
|
+
}
|
|
73
232
|
|
|
74
|
-
|
|
233
|
+
async function runMutation(
|
|
234
|
+
target: Callable,
|
|
235
|
+
receiver: unknown,
|
|
236
|
+
args: unknown[],
|
|
237
|
+
clientOptions: SyncClientOptions | undefined,
|
|
238
|
+
projections: Projections,
|
|
239
|
+
clientQueries: QueryStore
|
|
240
|
+
): Promise<unknown> {
|
|
241
|
+
const supplied = args[1] as Omit<CallOptions, typeof OPERATION> | undefined;
|
|
242
|
+
const retry = supplied?.retry ?? clientOptions?.retry ?? 0;
|
|
243
|
+
if (!Number.isSafeInteger(retry) || retry < 0) {
|
|
244
|
+
throw new RangeError("[furin] retry must be a nonnegative integer.");
|
|
245
|
+
}
|
|
246
|
+
const operation: Operation = {};
|
|
247
|
+
let key: string | undefined;
|
|
248
|
+
const headers = async (path: string, init: RequestInit) => {
|
|
249
|
+
const resolved = await resolveHeaders(supplied?.headers, path, init);
|
|
250
|
+
// Eden reads this property after serializing the body.
|
|
251
|
+
Object.assign(headers, { "content-type": resolved.get("content-type") ?? undefined });
|
|
252
|
+
const fetchHeaders = new Headers(supplied?.fetch?.headers);
|
|
253
|
+
key ??=
|
|
254
|
+
fetchHeaders.get("Idempotency-Key") ??
|
|
255
|
+
resolved.get("Idempotency-Key") ??
|
|
256
|
+
new Headers(init.headers).get("Idempotency-Key") ??
|
|
257
|
+
createIdempotencyKey();
|
|
258
|
+
resolved.set("Idempotency-Key", key);
|
|
259
|
+
if (supplied?.fetch?.headers) {
|
|
260
|
+
fetchHeaders.set("Idempotency-Key", key);
|
|
261
|
+
options.fetch = { ...options.fetch, headers: fetchHeaders };
|
|
262
|
+
}
|
|
263
|
+
if (!operation.projection) {
|
|
264
|
+
const { runtime } = operation;
|
|
265
|
+
const queries = currentQueryEnvironment()?.store ?? runtime?.queries ?? clientQueries;
|
|
266
|
+
const owner = runtime ?? queries;
|
|
267
|
+
const entries = projections.get(owner) ?? new Map<string, Projection>();
|
|
268
|
+
const identity = JSON.stringify([
|
|
269
|
+
operation.method,
|
|
270
|
+
path,
|
|
271
|
+
key,
|
|
272
|
+
supplied?.query && Object.entries(supplied.query).sort(([a], [b]) => a.localeCompare(b)),
|
|
273
|
+
]);
|
|
274
|
+
let projection = entries.get(identity);
|
|
275
|
+
if (!projection) {
|
|
276
|
+
const queryToken = queries.begin();
|
|
277
|
+
const onRemove = () => {
|
|
278
|
+
entries.delete(identity);
|
|
279
|
+
if (entries.size === 0) {
|
|
280
|
+
projections.delete(owner);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
queryToken.onRemove = onRemove;
|
|
284
|
+
const optimistic = (routeCache: OptimisticCache) =>
|
|
285
|
+
supplied?.optimistic?.(queryProjectionCache(queries, queryToken, routeCache));
|
|
286
|
+
projection = {
|
|
287
|
+
remove: onRemove,
|
|
288
|
+
pending: 0,
|
|
289
|
+
outcome: "error",
|
|
290
|
+
runtime,
|
|
291
|
+
queries,
|
|
292
|
+
queryToken,
|
|
293
|
+
token: runtime?.begin(optimistic, onRemove, () => queryToken.transforms.size > 0),
|
|
294
|
+
};
|
|
295
|
+
if (!runtime) {
|
|
296
|
+
optimistic({ update: () => undefined } as OptimisticCache);
|
|
297
|
+
}
|
|
298
|
+
entries.set(identity, projection);
|
|
299
|
+
projections.set(owner, entries);
|
|
300
|
+
}
|
|
301
|
+
projection.pending += 1;
|
|
302
|
+
operation.projection = projection;
|
|
303
|
+
}
|
|
304
|
+
return resolved;
|
|
305
|
+
};
|
|
306
|
+
const options: CallOptions = {
|
|
307
|
+
...supplied,
|
|
308
|
+
[OPERATION]: operation,
|
|
309
|
+
headers,
|
|
310
|
+
};
|
|
311
|
+
let result: MutationResult;
|
|
312
|
+
let thrown: { error: unknown } | undefined;
|
|
313
|
+
try {
|
|
314
|
+
let attempt = 0;
|
|
315
|
+
for (;;) {
|
|
316
|
+
operation.result = undefined;
|
|
317
|
+
thrown = undefined;
|
|
75
318
|
try {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
});
|
|
319
|
+
// biome-ignore lint/performance/noAwaitInLoops: retries are sequential attempts of the same operation.
|
|
320
|
+
result = (await Reflect.apply(target, receiver, [args[0], options])) as MutationResult;
|
|
79
321
|
} catch (error) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
322
|
+
if (!operation.result) {
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
({ result } = operation);
|
|
326
|
+
thrown = { error };
|
|
83
327
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return result;
|
|
328
|
+
if (!inProgress(result) || attempt >= retry || options.fetch?.signal?.aborted) {
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
if (!(await delay(retryDelay(result.response, attempt), options.fetch?.signal))) {
|
|
332
|
+
break;
|
|
90
333
|
}
|
|
334
|
+
attempt += 1;
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
finishOperation(operation, undefined);
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
finishOperation(operation, result);
|
|
341
|
+
if (thrown) {
|
|
342
|
+
throw thrown.error;
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function finishOperation(operation: Operation, result: MutationResult | undefined): void {
|
|
348
|
+
const { projection } = operation;
|
|
349
|
+
if (!projection) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
let outcome: "success" | "error" | "ambiguous" = "ambiguous";
|
|
353
|
+
if (result?.error === null) {
|
|
354
|
+
outcome = "success";
|
|
355
|
+
} else if (
|
|
356
|
+
result?.response &&
|
|
357
|
+
result.response.status >= 400 &&
|
|
358
|
+
result.response.status < 500 &&
|
|
359
|
+
!inProgress(result)
|
|
360
|
+
) {
|
|
361
|
+
outcome = "error";
|
|
362
|
+
}
|
|
363
|
+
if (outcome === "success" || (outcome === "ambiguous" && projection.outcome === "error")) {
|
|
364
|
+
projection.outcome = outcome;
|
|
365
|
+
projection.response = result?.response;
|
|
366
|
+
}
|
|
367
|
+
projection.pending -= 1;
|
|
368
|
+
if (projection.pending === 0) {
|
|
369
|
+
if (!projection.runtime && projection.queryToken.transforms.size === 0) {
|
|
370
|
+
projection.remove();
|
|
371
|
+
}
|
|
372
|
+
const invalidations = (projection.response ?? result?.response)?.headers.get("x-furin-queries");
|
|
373
|
+
if (invalidations) {
|
|
374
|
+
projection.queries.invalidate(JSON.parse(invalidations) as QueryIdentity[]);
|
|
375
|
+
}
|
|
376
|
+
projection.queries.finish(projection.queryToken, projection.outcome);
|
|
377
|
+
if (projection.runtime && projection.token) {
|
|
378
|
+
projection.runtime.finish(
|
|
379
|
+
projection.token,
|
|
380
|
+
projection.outcome,
|
|
381
|
+
projection.response ?? result?.response
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
91
386
|
|
|
92
|
-
|
|
93
|
-
|
|
387
|
+
function wrapClient(
|
|
388
|
+
client: Callable,
|
|
389
|
+
method: string | undefined,
|
|
390
|
+
options: SyncClientOptions | undefined,
|
|
391
|
+
projections: Projections,
|
|
392
|
+
queries: QueryStore,
|
|
393
|
+
reference?: ReadReference
|
|
394
|
+
): Callable {
|
|
395
|
+
return new Proxy(client, {
|
|
396
|
+
get(target, property) {
|
|
397
|
+
if (property === QUERY_REFERENCE && reference) {
|
|
398
|
+
return reference;
|
|
399
|
+
}
|
|
400
|
+
const value = Reflect.get(target, property);
|
|
401
|
+
if (property === "get" && typeof value === "function") {
|
|
402
|
+
const context = Reflect.apply(
|
|
403
|
+
Reflect.get(target, "__furinQuery"),
|
|
404
|
+
target,
|
|
405
|
+
[]
|
|
406
|
+
) as PluginVerbContext;
|
|
407
|
+
const url = `${context.domain}/${context.paths.map(encodeURIComponent).join("/")}`;
|
|
408
|
+
const read: ReadReference = {
|
|
409
|
+
client: queries,
|
|
410
|
+
url,
|
|
411
|
+
load: (readOptions) => Reflect.apply(value, target, [readOptions]) as Promise<ReadResult>,
|
|
412
|
+
};
|
|
413
|
+
return wrapClient(value as Callable, "get", options, projections, queries, read);
|
|
414
|
+
}
|
|
415
|
+
return typeof value === "function"
|
|
416
|
+
? wrapClient(value as Callable, String(property), options, projections, queries)
|
|
417
|
+
: value;
|
|
94
418
|
},
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
419
|
+
apply(target, receiver, args) {
|
|
420
|
+
if (method && MUTATIONS.has(method)) {
|
|
421
|
+
return runMutation(target, receiver, args, options, projections, queries);
|
|
422
|
+
}
|
|
423
|
+
if (reference && method === "get") {
|
|
424
|
+
const environment = currentQueryEnvironment();
|
|
425
|
+
if (typeof window === "undefined" && !environment) {
|
|
426
|
+
return reference.load(args[0]);
|
|
427
|
+
}
|
|
428
|
+
const store =
|
|
429
|
+
environment?.store ??
|
|
430
|
+
findOptimisticRuntime(new URL(reference.url).origin)?.queries ??
|
|
431
|
+
queries;
|
|
432
|
+
const url = readUrl(reference, args[0]);
|
|
433
|
+
const epoch = store.generation();
|
|
434
|
+
store.bind(url, () => reference.load(args[0]), reference.client);
|
|
435
|
+
const version = store.version(url);
|
|
436
|
+
return reference.load(args[0]).then((result) => {
|
|
437
|
+
store.observe(url, result, epoch, version);
|
|
438
|
+
environment?.onRead();
|
|
439
|
+
return result;
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
const value = Reflect.apply(target, receiver, args);
|
|
443
|
+
return typeof value === "function"
|
|
444
|
+
? wrapClient(value as Callable, undefined, options, projections, queries)
|
|
445
|
+
: value;
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function withSync<
|
|
451
|
+
App extends AnyElysia,
|
|
452
|
+
Head extends { [header: string]: unknown },
|
|
453
|
+
Fns extends PluginTypeFn[],
|
|
454
|
+
>(
|
|
455
|
+
client: Treaty.Instance<App, Head, Fns>,
|
|
456
|
+
options?: SyncClientOptions
|
|
457
|
+
): Treaty.Instance<App, Head, [...Fns, SyncPluginType]> {
|
|
458
|
+
return wrapClient(
|
|
459
|
+
client.use(plugin) as unknown as Callable,
|
|
460
|
+
undefined,
|
|
461
|
+
options,
|
|
462
|
+
new Map(),
|
|
463
|
+
new QueryStore(typeof window === "undefined" ? undefined : window.location.origin)
|
|
464
|
+
) as unknown as Treaty.Instance<App, Head, [...Fns, SyncPluginType]>;
|
|
465
|
+
}
|
|
98
466
|
|
|
99
|
-
|
|
467
|
+
export function createClient<
|
|
468
|
+
const App extends AnyElysia,
|
|
469
|
+
Head extends { [header: string]: unknown } = { [header: string]: never },
|
|
470
|
+
>(domain: string | App, options?: Treaty.Config<Head> & SyncClientOptions) {
|
|
471
|
+
const { retry, ...eden } = options ?? {};
|
|
472
|
+
return withSync(treaty<App, Head>(domain, eden), { retry });
|
|
100
473
|
}
|
package/src/client.ts
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
entry for furin/client consumers, not a generic internal barrel.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
export {
|
|
7
|
+
type ClientModule,
|
|
8
|
+
clientModule,
|
|
9
|
+
preloadClientModule,
|
|
10
|
+
} from "./client/client-module.ts";
|
|
6
11
|
export { defineRootRoute, defineRoute } from "./client/define-route.ts";
|
|
7
12
|
export {
|
|
8
13
|
type DocumentAssets,
|
|
@@ -16,22 +21,21 @@ export {
|
|
|
16
21
|
reconcileHotComponentRegistry,
|
|
17
22
|
updateHotComponent,
|
|
18
23
|
} from "./client/hmr.ts";
|
|
24
|
+
export { useMutation } from "./client/mutation.ts";
|
|
25
|
+
export { useQuery } from "./client/query.tsx";
|
|
26
|
+
export { getRouteApi } from "./client/route-api.tsx";
|
|
19
27
|
export {
|
|
20
|
-
|
|
21
|
-
type
|
|
22
|
-
type
|
|
23
|
-
type
|
|
24
|
-
|
|
25
|
-
type SyncMutationRunner,
|
|
26
|
-
type SyncMutationSuccessContext,
|
|
27
|
-
type UseSyncOptions,
|
|
28
|
-
useSync,
|
|
28
|
+
createClient,
|
|
29
|
+
type OptimisticCache,
|
|
30
|
+
type SyncCallOptions,
|
|
31
|
+
type SyncClientOptions,
|
|
32
|
+
withSync,
|
|
29
33
|
} from "./client/sync.ts";
|
|
30
34
|
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
31
35
|
|
|
32
|
-
export type
|
|
36
|
+
export { type DeferredData, defer, isDeferred } from "./shared/defer.ts";
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
export type RenderingMode = "ssr" | "ssg" | "isr";
|
|
35
39
|
|
|
36
40
|
export type MetaDescriptor =
|
|
37
41
|
| { charSet: "utf-8" }
|
|
@@ -48,8 +52,11 @@ export interface HeadOptions {
|
|
|
48
52
|
/**
|
|
49
53
|
* Inline scripts injected into `<head>`.
|
|
50
54
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
55
|
+
* Server rendering inserts `children` as raw script content. Client rendering
|
|
56
|
+
* passes `children` to React as text, not as HTML.
|
|
57
|
+
*
|
|
58
|
+
* **Security warning:** Never pass user-controlled or loader-derived data
|
|
59
|
+
* here without sanitisation.
|
|
53
60
|
*/
|
|
54
61
|
scripts?: Array<{
|
|
55
62
|
src?: string;
|
|
@@ -65,50 +72,3 @@ export interface HeadOptions {
|
|
|
65
72
|
*/
|
|
66
73
|
styles?: Array<{ type?: string; children: string }>;
|
|
67
74
|
}
|
|
68
|
-
|
|
69
|
-
// ── Deferred data ──────────────────────────────────────────────────────────────
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* A loader return value that contains a mix of synchronous scalar fields and
|
|
73
|
-
* lazy `Promise<T>` fields. Scalar fields are serialised into the initial HTML
|
|
74
|
-
* shell; Promise fields are streamed as late `<script>` resolution chunks.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* loader: () => defer({
|
|
78
|
-
* title: "My Board", // synchronous — available immediately
|
|
79
|
-
* stats: fetchStats(), // Promise — streamed when it resolves
|
|
80
|
-
* })
|
|
81
|
-
*/
|
|
82
|
-
export type DeferredData<T extends object> = T & {
|
|
83
|
-
readonly [DEFERRED_BRAND]: true;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Wraps loader data so that Promise-valued fields are streamed lazily while
|
|
88
|
-
* scalar fields are embedded in the initial HTML shell immediately.
|
|
89
|
-
*
|
|
90
|
-
* Use in any `defineRoute().loader()`. Promise-valued fields are streamed lazily;
|
|
91
|
-
* scalar fields are embedded in the initial HTML shell.
|
|
92
|
-
*/
|
|
93
|
-
export function defer<T extends object>(
|
|
94
|
-
data: T & { readonly [DEFERRED_BRAND]?: never }
|
|
95
|
-
): DeferredData<T> {
|
|
96
|
-
if (Object.hasOwn(data, DEFERRED_BRAND)) {
|
|
97
|
-
throw new Error("[furin] defer() received data that is already deferred.");
|
|
98
|
-
}
|
|
99
|
-
return { ...data, [DEFERRED_BRAND]: true };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Type guard for DeferredData. Used by the render pipeline to distinguish a
|
|
104
|
-
* plain loader return from a deferred one.
|
|
105
|
-
*/
|
|
106
|
-
export function isDeferred(value: unknown): value is DeferredData<object> {
|
|
107
|
-
return (
|
|
108
|
-
typeof value === "object" &&
|
|
109
|
-
value !== null &&
|
|
110
|
-
DEFERRED_BRAND in value &&
|
|
111
|
-
Object.hasOwn(value, DEFERRED_BRAND) &&
|
|
112
|
-
value[DEFERRED_BRAND] === true
|
|
113
|
-
);
|
|
114
|
-
}
|
package/src/config.ts
CHANGED
|
@@ -98,6 +98,11 @@ export const configSchema = t.Object({
|
|
|
98
98
|
*/
|
|
99
99
|
optimizeImports: t.Optional(t.Array(t.String())),
|
|
100
100
|
pagesDir: t.Optional(t.String()),
|
|
101
|
+
/**
|
|
102
|
+
* Module preloading. `routeChunks` (default true) makes `<HeadContent />`
|
|
103
|
+
* emit `<link rel="modulepreload">` for the current route's chunks.
|
|
104
|
+
*/
|
|
105
|
+
preload: t.Optional(t.Object({ routeChunks: t.Optional(t.Boolean()) })),
|
|
101
106
|
/**
|
|
102
107
|
* Enable Bun's native Rust React Compiler for production client bundles.
|
|
103
108
|
* Enabled by default.
|