@teyik0/furin 0.2.0-alpha.2 → 0.2.0-alpha.4
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 +4 -0
- package/dist/adapter/bun.d.ts +3 -0
- package/dist/adapter/static.d.ts +3 -0
- package/dist/build/client.d.ts +25 -0
- package/dist/build/compile-entry.d.ts +9 -0
- package/dist/build/compile-entry.ts +109 -0
- package/dist/build/entry-template.d.ts +38 -0
- package/dist/build/entry-template.ts +156 -0
- package/dist/build/hydrate.d.ts +24 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.js +8106 -0
- package/dist/build/route-types.d.ts +23 -0
- package/dist/build/scan-server.d.ts +8 -0
- package/dist/build/server-routes-entry.d.ts +13 -0
- package/dist/build/server-routes-entry.ts +36 -0
- package/dist/build/shared.d.ts +12 -0
- package/dist/build/ssg-cache.d.ts +4 -0
- package/dist/build/types.d.ts +80 -0
- package/dist/cli/config.d.ts +9 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +8097 -0
- package/dist/client/boundaries.d.ts +117 -0
- package/dist/client/default-screens.d.ts +17 -0
- package/dist/client/link.d.ts +22 -0
- package/dist/client/link.js +4391 -0
- package/dist/client/router/abort.d.ts +1 -0
- package/dist/client/router/boundary-tree.d.ts +26 -0
- package/dist/client/router/context.d.ts +8 -0
- package/dist/client/router/history.d.ts +9 -0
- package/dist/client/router/index.d.ts +11 -0
- package/dist/client/router/link-utils.d.ts +53 -0
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +4 -0
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/spa-response.d.ts +58 -0
- package/dist/client/router/sync-catch-up.d.ts +29 -0
- package/dist/client/router/types.d.ts +292 -0
- package/dist/client/server-error.d.ts +12 -0
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +243 -0
- package/dist/client.js +140 -0
- package/dist/config.d.ts +57 -0
- package/dist/config.js +29 -0
- package/dist/env.d.ts +50 -0
- package/dist/furin.d.ts +77 -0
- package/dist/furin.js +8977 -0
- package/dist/plugin/index.d.ts +20 -0
- package/dist/plugin/index.js +1500 -0
- package/dist/plugin/transform-client.d.ts +10 -0
- package/dist/server/auto-invalidate/index.d.ts +4 -0
- package/dist/server/auto-invalidate/plugin.d.ts +38 -0
- package/dist/server/auto-invalidate/registry.d.ts +9 -0
- package/dist/server/auto-invalidate/runtime.d.ts +9 -0
- package/dist/server/auto-invalidate/types.d.ts +12 -0
- package/dist/server/cache/dev-loader.d.ts +28 -0
- package/dist/server/cache/index.d.ts +10 -0
- package/dist/server/cache/invalidation.d.ts +12 -0
- package/dist/server/cache/isr-ssg.d.ts +26 -0
- package/dist/server/cache/isr.d.ts +6 -0
- package/dist/server/cache/registry.d.ts +5 -0
- package/dist/server/cache/route-cache.d.ts +26 -0
- package/dist/server/cache/ssg.d.ts +5 -0
- package/dist/server/context-logger.d.ts +28 -0
- package/dist/server/dev-inspector.d.ts +89 -0
- package/dist/server/dev-page-plugin.d.ts +112 -0
- package/dist/server/internal.d.ts +35 -0
- package/dist/server/internal.js +17 -0
- package/dist/server/lang-detect.d.ts +5 -0
- package/dist/server/render/assemble.d.ts +49 -0
- package/dist/server/render/element.d.ts +24 -0
- package/dist/server/render/index.d.ts +7 -0
- package/dist/server/render/index.ts +22 -0
- package/dist/server/render/isr.d.ts +4 -0
- package/dist/server/render/loaders.d.ts +64 -0
- package/dist/server/render/not-found.d.ts +6 -0
- package/dist/server/render/shell.d.ts +39 -0
- package/dist/server/render/shell.ts +202 -0
- package/dist/server/render/ssg.d.ts +9 -0
- package/dist/server/render/ssr.d.ts +108 -0
- package/dist/server/render/template.d.ts +6 -0
- package/dist/server/router/discovery.d.ts +16 -0
- package/dist/server/router/hmr.d.ts +65 -0
- package/dist/server/router/index.d.ts +6 -0
- package/dist/server/router/index.js +27 -0
- package/dist/server/router/index.ts +28 -0
- package/dist/server/router/patterns.d.ts +40 -0
- package/dist/server/router/plugin.d.ts +21 -0
- package/dist/server/router/schemas.d.ts +59 -0
- package/dist/server/router/types.d.ts +60 -0
- package/dist/server/runtime-env.d.ts +3 -0
- package/dist/server/runtime-env.js +10 -0
- package/dist/server/sync/adapter.d.ts +62 -0
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/fingerprint.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/memory-adapter.d.ts +17 -0
- package/dist/server/sync/plugin.d.ts +186 -0
- package/dist/server/sync/response.d.ts +4 -0
- package/dist/server/sync/stream.d.ts +59 -0
- package/dist/shared/await.d.ts +67 -0
- package/dist/shared/deferred-ndjson.d.ts +32 -0
- package/dist/shared/digest.d.ts +16 -0
- package/dist/shared/error.d.ts +37 -0
- package/dist/shared/not-found.d.ts +19 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/dist/shared/utils/ast-walk.d.ts +18 -0
- package/dist/shared/utils/index.d.ts +13 -0
- package/package.json +5 -5
- package/src/build/hydrate.ts +4 -1
- package/src/build/shared.ts +4 -3
- package/src/client/router/abort.ts +5 -0
- package/src/client/router/link-utils.ts +8 -1
- package/src/client/router/provider.tsx +130 -6
- package/src/client/router/sync-catch-up.ts +113 -0
- package/src/client/router/types.ts +5 -0
- package/src/client/sync.ts +98 -0
- package/src/client.ts +15 -1
- package/src/furin.ts +25 -4
- package/src/server/auto-invalidate/runtime.ts +3 -2
- package/src/server/dev-page-plugin.ts +15 -10
- package/src/server/render/assemble.ts +30 -3
- package/src/server/render/isr.ts +8 -2
- package/src/server/render/ssr.ts +9 -1
- package/src/server/router/plugin.ts +2 -2
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/fingerprint.ts +44 -0
- package/src/server/sync/index.ts +15 -0
- package/src/server/sync/memory-adapter.ts +196 -0
- package/src/server/sync/plugin.ts +200 -0
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +152 -0
- package/src/shared/deferred-ndjson.ts +7 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type SearchParamValue = string | number | boolean | null | undefined | SearchParamValue[] | {
|
|
2
|
+
[key: string]: SearchParamValue;
|
|
3
|
+
};
|
|
4
|
+
export interface SearchParamsInput {
|
|
5
|
+
[key: string]: SearchParamValue;
|
|
6
|
+
}
|
|
7
|
+
export interface SearchRouteMetadata {
|
|
8
|
+
pattern: string;
|
|
9
|
+
regex: RegExp;
|
|
10
|
+
searchDefaults?: SearchParamsInput;
|
|
11
|
+
}
|
|
12
|
+
export declare function stripSearchDefaults(search: SearchParamsInput, defaults: SearchParamsInput | undefined): SearchParamsInput;
|
|
13
|
+
export declare function appendSearchParamValue(params: URLSearchParams, key: string, value: unknown): void;
|
|
14
|
+
export declare function buildSearchParams(search: SearchParamsInput, defaults?: SearchParamsInput): URLSearchParams;
|
|
15
|
+
export declare function collectSearchDefaults(schema: unknown): SearchParamsInput | undefined;
|
|
16
|
+
export declare function findSearchDefaults(pathname: string, routes: readonly SearchRouteMetadata[]): SearchParamsInput | undefined;
|
|
17
|
+
export declare function findSearchDefaultsForRouteTarget(to: string, routes: readonly SearchRouteMetadata[]): SearchParamsInput | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { BaseNode, Node as YukuNode } from "yuku-parser";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal ESTree node interface used by AST-walking utilities across the
|
|
4
|
+
* framework (build scanning, client transform, etc.).
|
|
5
|
+
*/
|
|
6
|
+
export interface AstNode extends BaseNode {
|
|
7
|
+
body?: AstNode[];
|
|
8
|
+
type: YukuNode["type"] | string;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Walks an ESTree-compatible AST node recursively, calling `visitor` for every
|
|
13
|
+
* node that has a `type` property.
|
|
14
|
+
*
|
|
15
|
+
* Arrays are traversed element-by-element. The keys `type`, `start` and `end`
|
|
16
|
+
* are skipped so that the visitor is only invoked on genuine child nodes.
|
|
17
|
+
*/
|
|
18
|
+
export declare function walkAST(node: unknown, visitor: (n: AstNode) => void): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RuntimePage, RuntimeRoute } from "../../client";
|
|
2
|
+
export declare function isFurinPage(value: unknown): value is RuntimePage;
|
|
3
|
+
export declare function isFurinRoute(value: unknown): value is RuntimeRoute;
|
|
4
|
+
export declare function collectRouteChainFromRoute(route: RuntimeRoute): RuntimeRoute[];
|
|
5
|
+
export declare function hasCycle(route: RuntimeRoute): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Maps over `items` with a bounded worker pool, preserving input order.
|
|
8
|
+
* Use whenever an `await Promise.all(items.map(...))` would open as many
|
|
9
|
+
* concurrent async operations as there are items — unbounded fan-out can
|
|
10
|
+
* exhaust file descriptors, sockets, or memory on large inputs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function mapWithConcurrency<T, R>(items: readonly T[], concurrency: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
|
|
13
|
+
export declare function validateRouteChain(chain: RuntimeRoute[], root: RuntimeRoute, pagePath?: string): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teyik0/furin",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.4",
|
|
4
4
|
"description": "React meta-framework powered by Elysia and Bun with file-based routing, SSR, SSG, and ISR.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "teyik0",
|
|
@@ -97,16 +97,16 @@
|
|
|
97
97
|
"@elysiajs/static": "^1.4.10",
|
|
98
98
|
"magic-string": "^0.30.21",
|
|
99
99
|
"seroval": "^1.5.4",
|
|
100
|
-
"yuku-parser": "^0.5.
|
|
100
|
+
"yuku-parser": "^0.5.43"
|
|
101
101
|
},
|
|
102
102
|
"devDependencies": {
|
|
103
103
|
"@types/react": "^19.2.17",
|
|
104
104
|
"@types/react-dom": "^19.2.3",
|
|
105
|
-
"expect-type": "^1.
|
|
105
|
+
"expect-type": "^1.4.0"
|
|
106
106
|
},
|
|
107
107
|
"peerDependencies": {
|
|
108
|
-
"elysia": "^1.4.
|
|
109
|
-
"evlog": "^2.19.
|
|
108
|
+
"elysia": "^1.4.29",
|
|
109
|
+
"evlog": "^2.19.2",
|
|
110
110
|
"react": "^19.2.7",
|
|
111
111
|
"react-dom": "^19.2.7"
|
|
112
112
|
}
|
package/src/build/hydrate.ts
CHANGED
|
@@ -116,7 +116,7 @@ export function generateHydrateEntry(
|
|
|
116
116
|
: "const log = { error() {}, info() {} };";
|
|
117
117
|
|
|
118
118
|
// RouterProvider receives basePath so navigate() / Link push physical paths.
|
|
119
|
-
const routerProviderDefaults = `\n autoRefresh: true,\n basePath: ${basePathLiteral},\n defaultPreload: "intent",\n defaultPreloadDelay: 50,\n defaultPreloadStaleTime: 30000,\n prefetchCacheSize: 50,`;
|
|
119
|
+
const routerProviderDefaults = `\n autoRefresh: true,\n basePath: ${basePathLiteral},\n defaultPreload: "intent",\n defaultPreloadDelay: 50,\n defaultPreloadStaleTime: 30000,\n prefetchCacheSize: 50,\n syncStream,`;
|
|
120
120
|
|
|
121
121
|
return `import { hydrateRoot, createRoot } from "react-dom/client";
|
|
122
122
|
import { createElement } from "react";
|
|
@@ -143,6 +143,9 @@ const _match = routes.find((r) => r.regex.test(pathname));
|
|
|
143
143
|
// former does not — so the two cases fork on _match below.
|
|
144
144
|
const dataEl = document.getElementById("__FURIN_DATA__");
|
|
145
145
|
const loaderData = dataEl ? JSON.parse(dataEl.textContent || "{}") : {};
|
|
146
|
+
const syncEl = document.getElementById("__FURIN_SYNC__");
|
|
147
|
+
const syncConfig = syncEl ? JSON.parse(syncEl.textContent || "{}") : {};
|
|
148
|
+
const syncStream = typeof syncConfig.stream === "string" ? syncConfig.stream : undefined;
|
|
146
149
|
|
|
147
150
|
// ── Deferred data hydration ─────────────────────────────────────────────────
|
|
148
151
|
// window.__FURIN_DEFERRED__ is injected by the server when a loader returns
|
package/src/build/shared.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cpSync, existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
|
|
2
2
|
import { join, relative } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
3
4
|
import type { BuildTarget } from "../config";
|
|
4
5
|
import type { ResolvedRoute } from "../server/router/index.ts";
|
|
5
6
|
import type { BuildRouteManifestEntry, TargetBuildManifest } from "./types";
|
|
@@ -8,9 +9,9 @@ import type { BuildRouteManifestEntry, TargetBuildManifest } from "./types";
|
|
|
8
9
|
// location — so relative paths break when bundled. import.meta.resolve() is a
|
|
9
10
|
// function call (not inlined), runs at runtime, and resolves through package
|
|
10
11
|
// exports. This is the Web-standard approach (Bun, Node 20.6+, browsers).
|
|
11
|
-
export const CLIENT_MODULE_PATH =
|
|
12
|
-
export const LINK_MODULE_PATH =
|
|
13
|
-
export const SEARCH_MODULE_PATH =
|
|
12
|
+
export const CLIENT_MODULE_PATH = fileURLToPath(import.meta.resolve("@teyik0/furin/client"));
|
|
13
|
+
export const LINK_MODULE_PATH = fileURLToPath(import.meta.resolve("@teyik0/furin/link"));
|
|
14
|
+
export const SEARCH_MODULE_PATH = fileURLToPath(import.meta.resolve("@teyik0/furin/search"));
|
|
14
15
|
|
|
15
16
|
export function ensureDir(path: string): void {
|
|
16
17
|
if (!existsSync(path)) {
|
|
@@ -43,7 +43,14 @@ export function applyRevalidateHeader(
|
|
|
43
43
|
if (!headerValue) {
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
applyRevalidateEntries(headerValue.split(","), invalidate);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function applyRevalidateEntries(
|
|
50
|
+
entries: Iterable<string>,
|
|
51
|
+
invalidate: (path: string, type: "page" | "layout" | undefined) => void
|
|
52
|
+
): void {
|
|
53
|
+
for (const entry of entries) {
|
|
47
54
|
const trimmed = entry.trim();
|
|
48
55
|
if (!trimmed) {
|
|
49
56
|
continue;
|
|
@@ -3,6 +3,7 @@ import type React from "react";
|
|
|
3
3
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { parseDeferredNdjson } from "../../shared/deferred-ndjson.ts";
|
|
5
5
|
import type { SearchParamsInput } from "../../shared/search-params.ts";
|
|
6
|
+
import { isAbortError } from "./abort.ts";
|
|
6
7
|
import { buildPageElement, buildRouterTree } from "./boundary-tree.tsx";
|
|
7
8
|
import {
|
|
8
9
|
generateHistoryKey,
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
saveScrollPosition,
|
|
13
14
|
} from "./history.ts";
|
|
14
15
|
import {
|
|
16
|
+
applyRevalidateEntries,
|
|
15
17
|
applyRevalidateHeader,
|
|
16
18
|
normalizeHref,
|
|
17
19
|
shouldAutoRefreshPath,
|
|
@@ -28,6 +30,11 @@ import {
|
|
|
28
30
|
detectStaticMode,
|
|
29
31
|
parsePageResponse,
|
|
30
32
|
} from "./spa-response.ts";
|
|
33
|
+
import {
|
|
34
|
+
createInvalidationRefresh,
|
|
35
|
+
createSyncCatchUp,
|
|
36
|
+
type SyncChangePagePayload,
|
|
37
|
+
} from "./sync-catch-up.ts";
|
|
31
38
|
import type {
|
|
32
39
|
CacheEntry,
|
|
33
40
|
ClientSegmentBoundary,
|
|
@@ -69,6 +76,7 @@ export function RouterProvider({
|
|
|
69
76
|
defaultPreloadDelay,
|
|
70
77
|
defaultPreloadStaleTime,
|
|
71
78
|
prefetchCacheSize,
|
|
79
|
+
syncStream,
|
|
72
80
|
}: RouterProviderProps): React.ReactElement {
|
|
73
81
|
// Initial state. When `initialMatch` is `null`, `initialNotFound` MUST be set —
|
|
74
82
|
// the provider boots into the inline not-found UI.
|
|
@@ -288,7 +296,13 @@ export function RouterProvider({
|
|
|
288
296
|
|
|
289
297
|
return { match: loadedMatch, data, title, finalHref };
|
|
290
298
|
} catch (err: unknown) {
|
|
291
|
-
|
|
299
|
+
if (!isAbortError(err)) {
|
|
300
|
+
log.error({
|
|
301
|
+
action: "navigate_failed",
|
|
302
|
+
href: basePath + logicalHref,
|
|
303
|
+
error: String(err),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
292
306
|
return null;
|
|
293
307
|
}
|
|
294
308
|
},
|
|
@@ -497,6 +511,16 @@ export function RouterProvider({
|
|
|
497
511
|
},
|
|
498
512
|
[navigate, invalidatePrefetch, basePath]
|
|
499
513
|
);
|
|
514
|
+
const invalidationRefresh = useMemo(
|
|
515
|
+
() =>
|
|
516
|
+
createInvalidationRefresh({
|
|
517
|
+
onError: (error) => {
|
|
518
|
+
log.warn({ action: "sync_refresh_failed", error: String(error) });
|
|
519
|
+
},
|
|
520
|
+
refresh: () => refresh(undefined),
|
|
521
|
+
}),
|
|
522
|
+
[refresh]
|
|
523
|
+
);
|
|
500
524
|
|
|
501
525
|
// Expose refresh() to the HMR handler in _hydrate.tsx so that after a hot
|
|
502
526
|
// reload of a loader-bearing route the client re-fetches fresh data instead
|
|
@@ -680,16 +704,17 @@ export function RouterProvider({
|
|
|
680
704
|
const wrapped = async (...args: Parameters<typeof fetch>): Promise<Response> => {
|
|
681
705
|
const response = await originalFetch.apply(window, args);
|
|
682
706
|
const invalidated: Array<{ path: string; type: "page" | "layout" }> = [];
|
|
683
|
-
applyRevalidateHeader(response.headers, (path, type
|
|
684
|
-
|
|
685
|
-
|
|
707
|
+
applyRevalidateHeader(response.headers, (path, type) => {
|
|
708
|
+
const resolvedType = type ?? "page";
|
|
709
|
+
invalidatePrefetch(path, resolvedType);
|
|
710
|
+
invalidated.push({ path, type: resolvedType });
|
|
686
711
|
});
|
|
687
712
|
if (autoRefresh && invalidated.length > 0) {
|
|
688
713
|
const currentLogicalPath =
|
|
689
714
|
normalizeHref(toLogical(window.location.pathname, basePath)) + window.location.search;
|
|
690
715
|
if (shouldAutoRefreshPath(currentLogicalPath, invalidated)) {
|
|
691
716
|
// fire-and-forget: don't block the original fetch caller
|
|
692
|
-
|
|
717
|
+
invalidationRefresh.run();
|
|
693
718
|
}
|
|
694
719
|
}
|
|
695
720
|
return response;
|
|
@@ -701,7 +726,106 @@ export function RouterProvider({
|
|
|
701
726
|
return () => {
|
|
702
727
|
window.fetch = originalFetch;
|
|
703
728
|
};
|
|
704
|
-
}, [invalidatePrefetch,
|
|
729
|
+
}, [invalidatePrefetch, invalidationRefresh, autoRefresh, basePath]);
|
|
730
|
+
|
|
731
|
+
// Listen for sync cursor notifications and recover changes over HTTP.
|
|
732
|
+
useEffect(() => {
|
|
733
|
+
if (!syncStream || typeof window === "undefined" || typeof EventSource === "undefined") {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const streamUrl = syncStream.startsWith("/")
|
|
738
|
+
? `${basePath}${syncStream}`
|
|
739
|
+
: `${basePath}/${syncStream}`;
|
|
740
|
+
let source: EventSource | undefined;
|
|
741
|
+
let disposed = false;
|
|
742
|
+
const applyInvalidations = (entries: readonly string[]) => {
|
|
743
|
+
if (disposed) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const invalidations: Array<{ path: string; type: "page" | "layout" }> = [];
|
|
747
|
+
applyRevalidateEntries(entries, (path, type) => {
|
|
748
|
+
const resolvedType = type ?? "page";
|
|
749
|
+
invalidatePrefetch(path, resolvedType);
|
|
750
|
+
invalidations.push({ path, type: resolvedType });
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
if (autoRefresh && invalidations.length > 0) {
|
|
754
|
+
const currentLogicalPath =
|
|
755
|
+
normalizeHref(toLogical(window.location.pathname, basePath)) + window.location.search;
|
|
756
|
+
if (shouldAutoRefreshPath(currentLogicalPath, invalidations)) {
|
|
757
|
+
invalidationRefresh.run();
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
const catchUp = createSyncCatchUp({
|
|
762
|
+
fetchPage: async (after) => {
|
|
763
|
+
const url = new URL(`${streamUrl}/changes`, window.location.origin);
|
|
764
|
+
if (after !== undefined) {
|
|
765
|
+
url.searchParams.set("after", after);
|
|
766
|
+
}
|
|
767
|
+
const response = await window.fetch(url);
|
|
768
|
+
if (!response.ok) {
|
|
769
|
+
throw new Error(`Sync catch-up failed with status ${response.status}`);
|
|
770
|
+
}
|
|
771
|
+
return (await response.json()) as SyncChangePagePayload;
|
|
772
|
+
},
|
|
773
|
+
onInvalidations: applyInvalidations,
|
|
774
|
+
});
|
|
775
|
+
const recover = () => {
|
|
776
|
+
if (disposed) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
catchUp.catchUp().catch((error: unknown) => {
|
|
780
|
+
if (!disposed) {
|
|
781
|
+
log.warn({ action: "sync_catch_up_failed", error: String(error) });
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
};
|
|
785
|
+
const onSync = (event: MessageEvent) => {
|
|
786
|
+
try {
|
|
787
|
+
const payload = JSON.parse(event.data) as { cursor?: unknown };
|
|
788
|
+
if (typeof payload.cursor !== "string") {
|
|
789
|
+
throw new Error("Missing sync cursor");
|
|
790
|
+
}
|
|
791
|
+
} catch {
|
|
792
|
+
log.warn({ action: "sync_invalid_event", event: "furin.sync" });
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
recover();
|
|
796
|
+
};
|
|
797
|
+
const connect = () => {
|
|
798
|
+
if (disposed || source) {
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
source = new EventSource(streamUrl);
|
|
802
|
+
source.addEventListener("furin.sync", onSync);
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
catchUp
|
|
806
|
+
.initialize()
|
|
807
|
+
.then(() => {
|
|
808
|
+
if (disposed) {
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
connect();
|
|
812
|
+
// Close the initialization/SSE race using the durable change log.
|
|
813
|
+
recover();
|
|
814
|
+
})
|
|
815
|
+
.catch((error: unknown) => {
|
|
816
|
+
if (disposed) {
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
log.warn({ action: "sync_initialize_failed", error: String(error) });
|
|
820
|
+
// Keep notifications alive; the next event recovers from cursor zero.
|
|
821
|
+
connect();
|
|
822
|
+
});
|
|
823
|
+
return () => {
|
|
824
|
+
disposed = true;
|
|
825
|
+
source?.removeEventListener("furin.sync", onSync);
|
|
826
|
+
source?.close();
|
|
827
|
+
};
|
|
828
|
+
}, [syncStream, basePath, invalidatePrefetch, invalidationRefresh, autoRefresh]);
|
|
705
829
|
|
|
706
830
|
let pageElement: React.ReactNode;
|
|
707
831
|
if (state.notFound || !state.match) {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
export interface SyncChangePayload {
|
|
2
|
+
cursor: string;
|
|
3
|
+
invalidations: readonly string[];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface SyncChangePagePayload {
|
|
7
|
+
changes: readonly SyncChangePayload[];
|
|
8
|
+
cursor: string;
|
|
9
|
+
hasMore: boolean;
|
|
10
|
+
reset: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface SyncCatchUpOptions {
|
|
14
|
+
fetchPage: (after: string | undefined) => Promise<SyncChangePagePayload>;
|
|
15
|
+
onInvalidations: (invalidations: readonly string[]) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SyncCatchUp {
|
|
19
|
+
catchUp(): Promise<void>;
|
|
20
|
+
cursor(): string | undefined;
|
|
21
|
+
initialize(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface InvalidationRefreshOptions {
|
|
25
|
+
onError: (error: unknown) => void;
|
|
26
|
+
refresh: () => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InvalidationRefresh {
|
|
30
|
+
run(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createInvalidationRefresh(
|
|
34
|
+
options: InvalidationRefreshOptions
|
|
35
|
+
): InvalidationRefresh {
|
|
36
|
+
let running: Promise<void> | undefined;
|
|
37
|
+
let requested = false;
|
|
38
|
+
|
|
39
|
+
const refreshUntilCurrent = async (): Promise<void> => {
|
|
40
|
+
do {
|
|
41
|
+
requested = false;
|
|
42
|
+
try {
|
|
43
|
+
await options.refresh();
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (!isAbortError(error)) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
} while (requested);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
run() {
|
|
54
|
+
requested = true;
|
|
55
|
+
if (!running) {
|
|
56
|
+
running = refreshUntilCurrent()
|
|
57
|
+
.catch((error: unknown) => {
|
|
58
|
+
if (!isAbortError(error)) {
|
|
59
|
+
options.onError(error);
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
.finally(() => {
|
|
63
|
+
running = undefined;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return running;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createSyncCatchUp(options: SyncCatchUpOptions): SyncCatchUp {
|
|
72
|
+
let currentCursor: string | undefined;
|
|
73
|
+
let running: Promise<void> | undefined;
|
|
74
|
+
let requested = false;
|
|
75
|
+
|
|
76
|
+
const readUntilCurrent = async (): Promise<void> => {
|
|
77
|
+
do {
|
|
78
|
+
requested = false;
|
|
79
|
+
let page: SyncChangePagePayload;
|
|
80
|
+
do {
|
|
81
|
+
page = await options.fetchPage(currentCursor ?? "0");
|
|
82
|
+
if (page.reset) {
|
|
83
|
+
options.onInvalidations(["/:layout"]);
|
|
84
|
+
currentCursor = page.cursor;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
for (const change of page.changes) {
|
|
88
|
+
options.onInvalidations(change.invalidations);
|
|
89
|
+
}
|
|
90
|
+
currentCursor = page.cursor;
|
|
91
|
+
} while (page.hasMore);
|
|
92
|
+
} while (requested);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
async initialize() {
|
|
97
|
+
const page = await options.fetchPage(undefined);
|
|
98
|
+
currentCursor = page.cursor;
|
|
99
|
+
},
|
|
100
|
+
catchUp() {
|
|
101
|
+
requested = true;
|
|
102
|
+
if (!running) {
|
|
103
|
+
running = readUntilCurrent().finally(() => {
|
|
104
|
+
running = undefined;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return running;
|
|
108
|
+
},
|
|
109
|
+
cursor: () => currentCursor,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
import { isAbortError } from "./abort.ts";
|
|
@@ -205,6 +205,11 @@ export interface RouterProviderProps {
|
|
|
205
205
|
prefetchCacheSize: number;
|
|
206
206
|
root: RuntimeRoute | null;
|
|
207
207
|
routes: ClientRoute[];
|
|
208
|
+
/**
|
|
209
|
+
* Optional internal SSE endpoint emitted by `furin({ sync: true })`.
|
|
210
|
+
* When present, RouterProvider listens for cross-client invalidations.
|
|
211
|
+
*/
|
|
212
|
+
syncStream?: string | undefined;
|
|
208
213
|
}
|
|
209
214
|
|
|
210
215
|
/** @internal Router state held inside RouterProvider. Exported so CacheEntry can reference it. */
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
export interface SyncMutationHeaders {
|
|
4
|
+
"Idempotency-Key": string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface SyncMutationOptions {
|
|
8
|
+
headers: SyncMutationHeaders;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SyncMutationContext<TInput> {
|
|
12
|
+
idempotencyKey: string;
|
|
13
|
+
input: TInput;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface SyncMutationSuccessContext<TInput, TResult> extends SyncMutationContext<TInput> {
|
|
17
|
+
result: TResult;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SyncMutationErrorContext<TInput, TResult> extends SyncMutationContext<TInput> {
|
|
21
|
+
error: unknown;
|
|
22
|
+
result?: TResult;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UseSyncOptions<TInput, TResult> {
|
|
26
|
+
onError?: (ctx: SyncMutationErrorContext<TInput, TResult>) => Promise<void> | void;
|
|
27
|
+
onSuccess?: (ctx: SyncMutationSuccessContext<TInput, TResult>) => Promise<void> | void;
|
|
28
|
+
optimistic?: (ctx: SyncMutationContext<TInput>) => (() => void) | undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type SyncMutation<TInput, TResult> = (
|
|
32
|
+
input: TInput,
|
|
33
|
+
options: SyncMutationOptions
|
|
34
|
+
) => Promise<TResult>;
|
|
35
|
+
|
|
36
|
+
export type SyncMutationRunner<TInput, TResult> = undefined extends TInput
|
|
37
|
+
? (input?: TInput) => Promise<TResult>
|
|
38
|
+
: (input: TInput) => Promise<TResult>;
|
|
39
|
+
|
|
40
|
+
interface EdenErrorResult {
|
|
41
|
+
error?: unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createIdempotencyKey(): string {
|
|
45
|
+
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
46
|
+
return globalThis.crypto.randomUUID();
|
|
47
|
+
}
|
|
48
|
+
return `${Date.now()}-${Math.random()}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getResolvedError(result: unknown): unknown | undefined {
|
|
52
|
+
if (!(typeof result === "object" && result !== null && "error" in result)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const error = (result as EdenErrorResult).error;
|
|
57
|
+
return error == null ? undefined : error;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useSync<TInput, TResult>(
|
|
61
|
+
mutation: SyncMutation<TInput, TResult>,
|
|
62
|
+
options?: UseSyncOptions<TInput, TResult>
|
|
63
|
+
): SyncMutationRunner<TInput, TResult> {
|
|
64
|
+
const optimistic = options?.optimistic;
|
|
65
|
+
const onError = options?.onError;
|
|
66
|
+
const onSuccess = options?.onSuccess;
|
|
67
|
+
|
|
68
|
+
const run = useCallback(
|
|
69
|
+
async (input: TInput) => {
|
|
70
|
+
const idempotencyKey = createIdempotencyKey();
|
|
71
|
+
const rollback = optimistic?.({ idempotencyKey, input });
|
|
72
|
+
|
|
73
|
+
let result: TResult;
|
|
74
|
+
try {
|
|
75
|
+
result = await mutation(input, {
|
|
76
|
+
headers: { "Idempotency-Key": idempotencyKey },
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
rollback?.();
|
|
80
|
+
await onError?.({ error, idempotencyKey, input });
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const resolvedError = getResolvedError(result);
|
|
85
|
+
if (resolvedError !== undefined) {
|
|
86
|
+
rollback?.();
|
|
87
|
+
await onError?.({ error: resolvedError, idempotencyKey, input, result });
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
await onSuccess?.({ idempotencyKey, input, result });
|
|
92
|
+
return result;
|
|
93
|
+
},
|
|
94
|
+
[mutation, optimistic, onError, onSuccess]
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return run as SyncMutationRunner<TInput, TResult>;
|
|
98
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -6,12 +6,26 @@
|
|
|
6
6
|
2. Doesn't have an index signature (so unknown prop access errors)
|
|
7
7
|
3. Is transparent in intersections(`{} & T = T`)
|
|
8
8
|
*/
|
|
9
|
+
/*
|
|
10
|
+
biome-ignore-all lint/performance/noBarrelFile: client.ts is the canonical DX
|
|
11
|
+
entry for furin/client consumers, not a generic internal barrel.
|
|
12
|
+
*/
|
|
9
13
|
|
|
10
14
|
import type { Cookie, StatusMap } from "elysia";
|
|
11
15
|
import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
|
|
12
16
|
import type { RequestLogger } from "evlog";
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
export {
|
|
19
|
+
type SyncMutation,
|
|
20
|
+
type SyncMutationContext,
|
|
21
|
+
type SyncMutationErrorContext,
|
|
22
|
+
type SyncMutationHeaders,
|
|
23
|
+
type SyncMutationOptions,
|
|
24
|
+
type SyncMutationRunner,
|
|
25
|
+
type SyncMutationSuccessContext,
|
|
26
|
+
type UseSyncOptions,
|
|
27
|
+
useSync,
|
|
28
|
+
} from "./client/sync.ts";
|
|
15
29
|
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|
|
16
30
|
|
|
17
31
|
declare const UNSET: unique symbol;
|