@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
|
@@ -10,39 +10,40 @@ export interface BeginMutationInput {
|
|
|
10
10
|
principal: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
export interface MutationLease {
|
|
14
|
+
id: string;
|
|
15
|
+
key: string;
|
|
16
|
+
leaseMs: number;
|
|
17
|
+
principal: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
13
20
|
export type BeginMutationResult =
|
|
14
|
-
| { kind: "execute";
|
|
21
|
+
| { kind: "execute"; lease: MutationLease }
|
|
15
22
|
| { kind: "replay"; response: StoredResponse }
|
|
16
|
-
| { kind: "conflict"; reason: "in-progress" | "payload-mismatch" }
|
|
17
|
-
|
|
23
|
+
| { kind: "conflict"; reason: "in-progress" | "payload-mismatch" };
|
|
24
|
+
|
|
25
|
+
export type SyncInvalidation =
|
|
26
|
+
| { kind: "path"; path: string; type: "layout" | "page" }
|
|
27
|
+
| { kind: "tags"; tags: readonly string[] };
|
|
18
28
|
|
|
19
|
-
export interface
|
|
20
|
-
|
|
29
|
+
export interface CompleteMutationInput {
|
|
30
|
+
invalidations: readonly SyncInvalidation[];
|
|
31
|
+
lease: MutationLease;
|
|
21
32
|
response: StoredResponse;
|
|
22
33
|
}
|
|
23
34
|
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
}
|
|
35
|
+
export type CompleteMutationResult =
|
|
36
|
+
| { cursor: string | undefined; kind: "committed" }
|
|
37
|
+
| { kind: "lost" };
|
|
27
38
|
|
|
28
39
|
export interface SyncChange {
|
|
29
40
|
cursor: string;
|
|
30
|
-
invalidations: readonly
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface AppendChangesInput {
|
|
34
|
-
invalidations: readonly string[];
|
|
35
|
-
path: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface AppendChangesResult {
|
|
39
|
-
change: SyncChange;
|
|
41
|
+
invalidations: readonly SyncInvalidation[];
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
export interface ReadChangesInput {
|
|
43
45
|
after: string | undefined;
|
|
44
46
|
limit: number;
|
|
45
|
-
path: string;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
export interface ChangePage {
|
|
@@ -52,14 +53,29 @@ export interface ChangePage {
|
|
|
52
53
|
reset: boolean;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
export type SyncChangeListener = (change: SyncChange) => void;
|
|
56
|
-
type MaybePromise<T> = Promise<T> | T;
|
|
57
|
-
|
|
58
56
|
export interface SyncAdapter {
|
|
59
|
-
abortMutation(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
readChanges(input: ReadChangesInput)
|
|
64
|
-
|
|
57
|
+
abortMutation: (lease: MutationLease) => Promise<void>;
|
|
58
|
+
beginMutation: (input: BeginMutationInput) => Promise<BeginMutationResult>;
|
|
59
|
+
completeMutation: (input: CompleteMutationInput) => Promise<CompleteMutationResult>;
|
|
60
|
+
currentCursor: () => Promise<string>;
|
|
61
|
+
readChanges: (input: ReadChangesInput) => Promise<ChangePage>;
|
|
62
|
+
renewMutation: (lease: MutationLease) => Promise<"lost" | "renewed">;
|
|
63
|
+
readonly scope: "distributed" | "host-local" | "process-local";
|
|
65
64
|
}
|
|
65
|
+
|
|
66
|
+
export interface SyncSubscription {
|
|
67
|
+
unsubscribe: () => Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface SyncNotifier {
|
|
71
|
+
publish: (cursor: string) => Promise<void>;
|
|
72
|
+
subscribe: (listener: (cursor: string) => void) => Promise<SyncSubscription>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SyncRuntimeOptions {
|
|
76
|
+
adapter: SyncAdapter;
|
|
77
|
+
notifier?: SyncNotifier;
|
|
78
|
+
principal: (context: Context) => Promise<string> | string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
import type { Context } from "elysia";
|
|
@@ -1,29 +1,42 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { currentInstance } from "../instance.ts";
|
|
3
|
+
import type { SyncRuntimeOptions } from "./adapter.ts";
|
|
2
4
|
|
|
3
5
|
const SYNC_STREAM_DEFAULT_PATH = "/_furin/sync";
|
|
4
6
|
|
|
7
|
+
// Explicit override scope (tests, synthetic renders). When unset, the sync
|
|
8
|
+
// stream path comes from the current furin instance.
|
|
5
9
|
const requestSyncStreamPath = new AsyncLocalStorage<string | undefined>();
|
|
6
10
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export interface FurinSyncOptions extends SyncRuntimeOptions {
|
|
12
|
+
streamPath?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type FurinSyncOption = FurinSyncOptions | false;
|
|
12
16
|
|
|
13
17
|
export function resolveSyncStreamPath(sync: FurinSyncOption | undefined): string | undefined {
|
|
14
18
|
if (!sync) {
|
|
15
19
|
return;
|
|
16
20
|
}
|
|
17
|
-
if (sync === true) {
|
|
18
|
-
return SYNC_STREAM_DEFAULT_PATH;
|
|
19
|
-
}
|
|
20
21
|
return sync.streamPath ?? SYNC_STREAM_DEFAULT_PATH;
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Logical (unprefixed) sync stream path for the current request — injected
|
|
26
|
+
* into rendered HTML; the client prepends its own basePath before connecting.
|
|
27
|
+
*/
|
|
23
28
|
export function getSyncStreamPath(): string | undefined {
|
|
24
|
-
|
|
29
|
+
const override = requestSyncStreamPath.getStore();
|
|
30
|
+
if (override !== undefined) {
|
|
31
|
+
return override;
|
|
32
|
+
}
|
|
33
|
+
return currentInstance().syncStreamPath;
|
|
25
34
|
}
|
|
26
35
|
|
|
27
36
|
export function runWithSyncStreamPath<T>(path: string | undefined, fn: () => T): T {
|
|
28
37
|
return requestSyncStreamPath.run(path, fn);
|
|
29
38
|
}
|
|
39
|
+
|
|
40
|
+
export function syncRuntimeOptions(sync: FurinSyncOptions): SyncRuntimeOptions {
|
|
41
|
+
return { adapter: sync.adapter, notifier: sync.notifier, principal: sync.principal };
|
|
42
|
+
}
|
|
@@ -24,7 +24,13 @@ function canonicalize(value: unknown): unknown {
|
|
|
24
24
|
return value;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export
|
|
27
|
+
export function sha256Hex(source: string): string {
|
|
28
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
29
|
+
hasher.update(source);
|
|
30
|
+
return hasher.digest("hex");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createMutationFingerprint(input: FingerprintInput): string {
|
|
28
34
|
const url = new URL(input.request.url);
|
|
29
35
|
const query = [...url.searchParams.entries()].sort(
|
|
30
36
|
([leftKey, leftValue], [rightKey, rightValue]) =>
|
|
@@ -39,6 +45,5 @@ export async function createMutationFingerprint(input: FingerprintInput): Promis
|
|
|
39
45
|
pathname: url.pathname,
|
|
40
46
|
query,
|
|
41
47
|
});
|
|
42
|
-
|
|
43
|
-
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
48
|
+
return sha256Hex(source);
|
|
44
49
|
}
|
package/src/server/sync/index.ts
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
1
|
// biome-ignore-all lint/performance/noBarrelFile: sync has a small public/internal surface
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
getSyncStreamPath,
|
|
5
|
-
resolveSyncStreamPath,
|
|
6
|
-
runWithSyncStreamPath,
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getSyncStreamPath as getSyncStreamPathImplementation,
|
|
5
|
+
resolveSyncStreamPath as resolveSyncStreamPathImplementation,
|
|
6
|
+
runWithSyncStreamPath as runWithSyncStreamPathImplementation,
|
|
7
7
|
} from "./config.ts";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import { PollingSyncNotifier as PollingSyncNotifierImplementation } from "./notifier.ts";
|
|
9
|
+
import { furinSync as furinSyncImplementation } from "./plugin.ts";
|
|
10
|
+
import { createSyncStreamPlugin as createSyncStreamPluginImplementation } from "./stream.ts";
|
|
11
|
+
|
|
12
|
+
export const createSyncStreamPlugin = createSyncStreamPluginImplementation;
|
|
13
|
+
export const furinSync = furinSyncImplementation;
|
|
14
|
+
export const getSyncStreamPath = getSyncStreamPathImplementation;
|
|
15
|
+
export const PollingSyncNotifier = PollingSyncNotifierImplementation;
|
|
16
|
+
export const resolveSyncStreamPath = resolveSyncStreamPathImplementation;
|
|
17
|
+
export const runWithSyncStreamPath = runWithSyncStreamPathImplementation;
|
|
18
|
+
|
|
19
|
+
export type PollingSyncNotifier = PollingSyncNotifierImplementation;
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
BeginMutationInput,
|
|
23
|
+
BeginMutationResult,
|
|
24
|
+
ChangePage,
|
|
25
|
+
CompleteMutationInput,
|
|
26
|
+
CompleteMutationResult,
|
|
27
|
+
MutationLease,
|
|
28
|
+
ReadChangesInput,
|
|
29
|
+
StoredResponse,
|
|
30
|
+
SyncAdapter,
|
|
31
|
+
SyncChange,
|
|
32
|
+
SyncInvalidation,
|
|
33
|
+
SyncNotifier,
|
|
34
|
+
SyncRuntimeOptions,
|
|
35
|
+
SyncSubscription,
|
|
36
|
+
} from "./adapter.ts";
|
|
37
|
+
export type { FurinSyncOption, FurinSyncOptions } from "./config.ts";
|
|
38
|
+
export type { SyncInput, SyncRouteOption } from "./plugin.ts";
|
|
39
|
+
export type { SyncChangePage } from "./stream.ts";
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { SyncAdapter, SyncNotifier, SyncSubscription } from "./adapter.ts";
|
|
2
|
+
|
|
3
|
+
interface ListenerSubscription {
|
|
4
|
+
listener: (cursor: string) => void;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class PollingSyncNotifier implements SyncNotifier {
|
|
8
|
+
private readonly adapter: SyncAdapter;
|
|
9
|
+
private readonly intervalMs: number;
|
|
10
|
+
private readonly listeners = new Set<ListenerSubscription>();
|
|
11
|
+
private cursor: string | undefined;
|
|
12
|
+
private initialization: Promise<void> | undefined;
|
|
13
|
+
private polling: Promise<void> | undefined;
|
|
14
|
+
private revision = 0;
|
|
15
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
16
|
+
|
|
17
|
+
constructor(adapter: SyncAdapter, intervalMs: number) {
|
|
18
|
+
this.adapter = adapter;
|
|
19
|
+
this.intervalMs = intervalMs;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
publish(cursor: string): Promise<void> {
|
|
23
|
+
this.revision += 1;
|
|
24
|
+
this.emit(cursor);
|
|
25
|
+
return Promise.resolve();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async subscribe(listener: (cursor: string) => void): Promise<SyncSubscription> {
|
|
29
|
+
const subscription = { listener };
|
|
30
|
+
this.listeners.add(subscription);
|
|
31
|
+
try {
|
|
32
|
+
await this.start();
|
|
33
|
+
} catch (error) {
|
|
34
|
+
this.listeners.delete(subscription);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
unsubscribe: () => {
|
|
39
|
+
this.listeners.delete(subscription);
|
|
40
|
+
if (this.listeners.size === 0 && this.timer) {
|
|
41
|
+
clearTimeout(this.timer);
|
|
42
|
+
this.timer = undefined;
|
|
43
|
+
}
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private emit(cursor: string): void {
|
|
50
|
+
if (cursor === this.cursor) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.cursor = cursor;
|
|
54
|
+
for (const subscription of [...this.listeners]) {
|
|
55
|
+
try {
|
|
56
|
+
subscription.listener(cursor);
|
|
57
|
+
} catch {
|
|
58
|
+
// Notifications are best-effort wake-ups; durable recovery reads the change log.
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async poll(): Promise<void> {
|
|
64
|
+
const { revision } = this;
|
|
65
|
+
try {
|
|
66
|
+
const cursor = await this.adapter.currentCursor();
|
|
67
|
+
if (revision === this.revision && this.listeners.size > 0) {
|
|
68
|
+
this.emit(cursor);
|
|
69
|
+
}
|
|
70
|
+
} catch {
|
|
71
|
+
// A later poll retries transient adapter failures.
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private async start(): Promise<void> {
|
|
76
|
+
if (this.timer || this.polling) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!this.initialization) {
|
|
80
|
+
const { revision } = this;
|
|
81
|
+
this.initialization = this.adapter
|
|
82
|
+
.currentCursor()
|
|
83
|
+
.then((cursor) => {
|
|
84
|
+
if (revision === this.revision) {
|
|
85
|
+
this.cursor = cursor;
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.finally(() => {
|
|
89
|
+
this.initialization = undefined;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
await this.initialization;
|
|
93
|
+
this.schedulePoll();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private schedulePoll(): void {
|
|
97
|
+
if (this.listeners.size === 0 || this.timer || this.polling) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.timer = setTimeout(() => {
|
|
101
|
+
this.timer = undefined;
|
|
102
|
+
this.polling = this.poll().finally(() => {
|
|
103
|
+
this.polling = undefined;
|
|
104
|
+
this.schedulePoll();
|
|
105
|
+
});
|
|
106
|
+
}, this.intervalMs);
|
|
107
|
+
this.timer.unref?.();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Elysia } from "elysia";
|
|
1
|
+
import { type Context, Elysia } from "elysia";
|
|
2
2
|
import {
|
|
3
3
|
appendPendingInvalidationHeader,
|
|
4
4
|
isSuccessfulMutationResponse,
|
|
5
5
|
runInvalidationRules,
|
|
6
6
|
} from "../auto-invalidate/runtime.ts";
|
|
7
7
|
import type { InvalidationInput } from "../auto-invalidate/types.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { peekPendingInvalidations } from "../cache/invalidation.ts";
|
|
9
|
+
import type { MutationLease, SyncInvalidation, SyncRuntimeOptions } from "./adapter.ts";
|
|
9
10
|
import { createMutationFingerprint } from "./fingerprint.ts";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
11
|
+
import { mergeStoredResponseHeaders, replayResponse, storeResponse } from "./response.ts";
|
|
12
|
+
import { resolveSyncRuntime } from "./runtime.ts";
|
|
12
13
|
|
|
13
14
|
export type SyncRouteOption =
|
|
14
15
|
| false
|
|
@@ -26,7 +27,8 @@ interface RouteSyncMetadata {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
interface ActiveMutation {
|
|
29
|
-
|
|
30
|
+
lease: MutationLease;
|
|
31
|
+
renewal: ReturnType<typeof setTimeout> | undefined;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
interface MutationContext {
|
|
@@ -37,9 +39,25 @@ interface MutationContext {
|
|
|
37
39
|
request: Request;
|
|
38
40
|
}
|
|
39
41
|
|
|
42
|
+
type TransportHook<TContext> = (context: TContext) => Promise<void>;
|
|
43
|
+
|
|
44
|
+
type CompletionContext = MutationContext &
|
|
45
|
+
Pick<Context, "set"> & {
|
|
46
|
+
response?: unknown;
|
|
47
|
+
responseValue?: unknown;
|
|
48
|
+
};
|
|
49
|
+
type PathInvalidation = Extract<SyncInvalidation, { kind: "path" }>;
|
|
50
|
+
|
|
40
51
|
const routeMetadata = new WeakMap<Request, RouteSyncMetadata>();
|
|
41
52
|
const activeMutations = new WeakMap<Request, ActiveMutation>();
|
|
42
53
|
|
|
54
|
+
function hideTransportResponse<TContext>(
|
|
55
|
+
hook: (context: TContext) => Promise<Response | undefined>
|
|
56
|
+
): TransportHook<TContext> {
|
|
57
|
+
// Sync responses short-circuit Elysia at runtime; they are not route success payloads for Eden.
|
|
58
|
+
return hook as TransportHook<TContext>;
|
|
59
|
+
}
|
|
60
|
+
|
|
43
61
|
function invalidationInputFromSync(input: Exclude<SyncRouteOption, false>): InvalidationInput {
|
|
44
62
|
if (input && typeof input === "object" && "invalidate" in input) {
|
|
45
63
|
return input.invalidate;
|
|
@@ -60,15 +78,6 @@ function getIdempotencyKey(ctx: MutationContext): string | undefined {
|
|
|
60
78
|
return fromRequest || undefined;
|
|
61
79
|
}
|
|
62
80
|
|
|
63
|
-
async function getPrincipalScope(request: Request): Promise<string> {
|
|
64
|
-
const authorization = request.headers.get("authorization");
|
|
65
|
-
const credentials = authorization
|
|
66
|
-
? JSON.stringify(["authorization", authorization])
|
|
67
|
-
: JSON.stringify(["cookie", request.headers.get("cookie") ?? ""]);
|
|
68
|
-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(credentials));
|
|
69
|
-
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
81
|
function supportsReplayBody(request: Request): boolean {
|
|
73
82
|
const contentType = request.headers.get("content-type")?.toLowerCase();
|
|
74
83
|
const mediaType = contentType?.split(";", 1)[0]?.trim();
|
|
@@ -91,66 +100,179 @@ function conflictResponse(reason: "in-progress" | "payload-mismatch"): Response
|
|
|
91
100
|
: "The Idempotency-Key was already used with a different request.",
|
|
92
101
|
},
|
|
93
102
|
{
|
|
94
|
-
status: 409,
|
|
95
103
|
headers: inProgress ? { "retry-after": "1" } : undefined,
|
|
104
|
+
status: 409,
|
|
96
105
|
}
|
|
97
106
|
);
|
|
98
107
|
}
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
function leaseLostResponse(): Response {
|
|
110
|
+
return Response.json(
|
|
111
|
+
{
|
|
112
|
+
code: "FURIN_SYNC_LEASE_LOST",
|
|
113
|
+
message: "The mutation lease was lost before its response could be committed.",
|
|
114
|
+
},
|
|
115
|
+
{ status: 503 }
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function normalizedInvalidations(input: InvalidationInput | undefined): SyncInvalidation[] {
|
|
120
|
+
if (!input) {
|
|
121
|
+
return [];
|
|
110
122
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
123
|
+
const rules = Array.isArray(input) ? input : [input];
|
|
124
|
+
const invalidations: SyncInvalidation[] = [];
|
|
125
|
+
for (const rule of rules) {
|
|
126
|
+
if ("path" in rule && rule.path) {
|
|
127
|
+
invalidations.push({ kind: "path", path: rule.path, type: rule.type });
|
|
128
|
+
}
|
|
129
|
+
if (rule.tags && rule.tags.length > 0) {
|
|
130
|
+
invalidations.push({ kind: "tags", tags: [...rule.tags] });
|
|
131
|
+
}
|
|
119
132
|
}
|
|
133
|
+
return invalidations;
|
|
134
|
+
}
|
|
120
135
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
136
|
+
function pendingPathInvalidations(entries: readonly string[]): PathInvalidation[] {
|
|
137
|
+
return entries.map((entry) =>
|
|
138
|
+
entry.endsWith(":layout")
|
|
139
|
+
? { kind: "path" as const, path: entry.slice(0, -":layout".length), type: "layout" }
|
|
140
|
+
: { kind: "path" as const, path: entry, type: "page" }
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function furinSync(options: SyncRuntimeOptions) {
|
|
145
|
+
const runtime = resolveSyncRuntime(options);
|
|
146
|
+
|
|
147
|
+
async function beginMutation(ctx: MutationContext): Promise<Response | undefined> {
|
|
148
|
+
if (!isMutationMethod(ctx.request.method) || routeMetadata.get(ctx.request)?.disabled) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const idempotencyKey = getIdempotencyKey(ctx);
|
|
152
|
+
if (!idempotencyKey) {
|
|
153
|
+
return new Response("Missing Idempotency-Key header", {
|
|
154
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
155
|
+
status: 428,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
if (!supportsReplayBody(ctx.request)) {
|
|
159
|
+
return Response.json(
|
|
160
|
+
{
|
|
161
|
+
code: "FURIN_UNSUPPORTED_SYNC_BODY",
|
|
162
|
+
message: "This request body cannot be replayed. Set sync: false on the route.",
|
|
163
|
+
},
|
|
164
|
+
{ status: 415 }
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const url = new URL(ctx.request.url);
|
|
169
|
+
const principal = await options.principal(ctx as Context);
|
|
170
|
+
if (principal.length === 0) {
|
|
171
|
+
throw new Error("[furin] Sync principal must not be empty.");
|
|
172
|
+
}
|
|
173
|
+
const key = `${ctx.request.method}:${url.pathname}:${idempotencyKey}`;
|
|
174
|
+
const fingerprint = createMutationFingerprint({ body: ctx.body, request: ctx.request });
|
|
175
|
+
const result = await runtime.adapter.beginMutation({ fingerprint, key, principal });
|
|
176
|
+
if (result.kind === "replay") {
|
|
177
|
+
return replayResponse(result.response);
|
|
178
|
+
}
|
|
179
|
+
if (result.kind === "conflict") {
|
|
180
|
+
return conflictResponse(result.reason);
|
|
181
|
+
}
|
|
182
|
+
const active: ActiveMutation = { lease: result.lease, renewal: undefined };
|
|
183
|
+
const renewAfter = Math.max(1000, Math.floor(result.lease.leaseMs / 3));
|
|
184
|
+
const scheduleRenewal = () => {
|
|
185
|
+
active.renewal = setTimeout(async () => {
|
|
186
|
+
if (activeMutations.get(ctx.request) === active) {
|
|
187
|
+
scheduleRenewal();
|
|
188
|
+
}
|
|
189
|
+
await runtime.adapter.renewMutation(result.lease).catch(() => undefined);
|
|
190
|
+
}, renewAfter);
|
|
191
|
+
active.renewal.unref?.();
|
|
192
|
+
};
|
|
193
|
+
activeMutations.set(ctx.request, active);
|
|
194
|
+
scheduleRenewal();
|
|
128
195
|
}
|
|
129
|
-
|
|
130
|
-
|
|
196
|
+
|
|
197
|
+
async function abortMutation(request: Request): Promise<void> {
|
|
198
|
+
const active = activeMutations.get(request);
|
|
199
|
+
if (!active) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
activeMutations.delete(request);
|
|
203
|
+
if (active.renewal) {
|
|
204
|
+
clearTimeout(active.renewal);
|
|
205
|
+
}
|
|
206
|
+
await runtime.adapter.abortMutation(active.lease);
|
|
131
207
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
);
|
|
208
|
+
|
|
209
|
+
async function persistMutation(
|
|
210
|
+
ctx: CompletionContext,
|
|
211
|
+
active: ActiveMutation
|
|
212
|
+
): Promise<Response | undefined> {
|
|
213
|
+
const result = await storeResponse(ctx.responseValue, ctx.set);
|
|
214
|
+
const manualPending = peekPendingInvalidations();
|
|
215
|
+
const invalidate = routeMetadata.get(ctx.request)?.invalidate;
|
|
216
|
+
if (invalidate) {
|
|
217
|
+
runInvalidationRules(invalidate);
|
|
218
|
+
}
|
|
219
|
+
const pending = appendPendingInvalidationHeader(ctx.set);
|
|
220
|
+
if (pending.length > 0) {
|
|
221
|
+
ctx.set.headers["x-furin-sync"] = "1";
|
|
222
|
+
}
|
|
223
|
+
const response = mergeStoredResponseHeaders(result.response, ctx.set.headers);
|
|
224
|
+
const semanticInvalidations = normalizedInvalidations(invalidate);
|
|
225
|
+
const invalidations = [...semanticInvalidations];
|
|
226
|
+
for (const manual of pendingPathInvalidations(manualPending)) {
|
|
227
|
+
const duplicated = semanticInvalidations.some(
|
|
228
|
+
(semantic) =>
|
|
229
|
+
semantic.kind === "path" && semantic.path === manual.path && semantic.type === manual.type
|
|
230
|
+
);
|
|
231
|
+
if (!duplicated) {
|
|
232
|
+
invalidations.push(manual);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const completion = await runtime.adapter.completeMutation({
|
|
236
|
+
invalidations,
|
|
237
|
+
lease: active.lease,
|
|
238
|
+
response,
|
|
239
|
+
});
|
|
240
|
+
if (completion.kind === "lost") {
|
|
241
|
+
return leaseLostResponse();
|
|
242
|
+
}
|
|
243
|
+
if (completion.cursor !== undefined) {
|
|
244
|
+
runtime.notifier.publish(completion.cursor).catch(() => undefined);
|
|
245
|
+
}
|
|
246
|
+
if (result.kind === "unreplayable") {
|
|
247
|
+
return replayResponse(response);
|
|
248
|
+
}
|
|
140
249
|
}
|
|
141
|
-
activeMutations.set(ctx.request, { mutationId: result.mutationId });
|
|
142
|
-
}
|
|
143
250
|
|
|
144
|
-
async function
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
251
|
+
async function finishMutation(ctx: CompletionContext): Promise<Response | undefined> {
|
|
252
|
+
const active = activeMutations.get(ctx.request);
|
|
253
|
+
if (!active) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
if (!isSuccessfulMutationResponse(ctx)) {
|
|
258
|
+
await runtime.adapter.abortMutation(active.lease);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
return await persistMutation(ctx, active);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
await runtime.adapter.abortMutation(active.lease).catch(() => undefined);
|
|
264
|
+
throw error;
|
|
265
|
+
} finally {
|
|
266
|
+
activeMutations.delete(ctx.request);
|
|
267
|
+
if (active.renewal) {
|
|
268
|
+
clearTimeout(active.renewal);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
148
271
|
}
|
|
149
|
-
activeMutations.delete(request);
|
|
150
|
-
await memorySyncAdapter.abortMutation(active);
|
|
151
|
-
}
|
|
152
272
|
|
|
153
|
-
|
|
273
|
+
const beginMutationHook = hideTransportResponse(beginMutation);
|
|
274
|
+
const finishMutationHook = hideTransportResponse(finishMutation);
|
|
275
|
+
|
|
154
276
|
return new Elysia({ name: "furin-sync" })
|
|
155
277
|
.macro({
|
|
156
278
|
sync(input: SyncRouteOption) {
|
|
@@ -166,35 +288,7 @@ export function furinSync() {
|
|
|
166
288
|
};
|
|
167
289
|
},
|
|
168
290
|
})
|
|
169
|
-
.onBeforeHandle({ as: "global" },
|
|
170
|
-
.onAfterHandle({ as: "global" },
|
|
171
|
-
const active = activeMutations.get(ctx.request);
|
|
172
|
-
if (!active) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
if (!isSuccessfulMutationResponse(ctx)) {
|
|
176
|
-
await abortMutation(ctx.request);
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const invalidate = routeMetadata.get(ctx.request)?.invalidate;
|
|
181
|
-
if (invalidate) {
|
|
182
|
-
runInvalidationRules(invalidate);
|
|
183
|
-
}
|
|
184
|
-
const pending = appendPendingInvalidationHeader(ctx.set);
|
|
185
|
-
if (pending.length > 0) {
|
|
186
|
-
ctx.set.headers["x-furin-sync"] = "1";
|
|
187
|
-
}
|
|
188
|
-
const response = await storeResponse(ctx.responseValue, ctx.set);
|
|
189
|
-
await memorySyncAdapter.commitMutation({ ...active, response });
|
|
190
|
-
activeMutations.delete(ctx.request);
|
|
191
|
-
|
|
192
|
-
if (pending.length > 0) {
|
|
193
|
-
await memorySyncAdapter.appendChanges({
|
|
194
|
-
invalidations: pending,
|
|
195
|
-
path: getSyncStreamPath() ?? "/_furin/sync",
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
})
|
|
291
|
+
.onBeforeHandle({ as: "global" }, beginMutationHook)
|
|
292
|
+
.onAfterHandle({ as: "global" }, finishMutationHook)
|
|
199
293
|
.onError({ as: "global" }, ({ request }) => abortMutation(request));
|
|
200
294
|
}
|