@teyik0/furin 0.2.0-alpha.5 → 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/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/entry-template.d.ts +2 -0
- package/dist/build/entry-template.ts +3 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +6944 -5067
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/types.d.ts +1 -0
- package/dist/cli/index.js +7874 -5971
- 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 +1 -1
- package/dist/config.js +2 -2
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +7 -6
- package/dist/furin.js +6547 -3187
- 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/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +2 -3
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +9 -0
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/store-view.d.ts +7 -7
- 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/internal.d.ts +2 -0
- package/dist/server/internal.js +3 -3
- 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/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 +5 -9
- 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 -11
- 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/route-frame.d.ts +49 -0
- package/dist/shared/utils/index.d.ts +1 -1
- package/package.json +54 -12
- package/src/adapter/bun.ts +73 -16
- package/src/adapter/package.ts +3 -1
- package/src/adapter/static.ts +94 -48
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +22 -24
- package/src/build/entry-template.ts +3 -0
- package/src/build/hydrate.ts +33 -8
- package/src/build/index.ts +12 -0
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +7 -8
- package/src/build/scan-server.ts +3 -2
- package/src/build/ssg-cache.ts +7 -2
- package/src/build/types.ts +1 -0
- package/src/cli/index.ts +53 -22
- 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/devtools/devtools-element.js +884 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +185 -96
- 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/runtime.ts +32 -6
- package/src/server/cache/dev-loader.ts +70 -10
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +33 -15
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +84 -9
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/store-view.ts +7 -7
- 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 +1 -1
- package/src/server/internal.ts +2 -0
- 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 +10 -15
- 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 +155 -67
- package/src/server/render/template.ts +2 -1
- 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 +9 -27
- 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 -89
- 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 +137 -81
- 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/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
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IS_DEV } from "../runtime-env.ts";
|
|
2
|
+
import type { SyncAdapter, SyncNotifier, SyncRuntimeOptions } from "./adapter.ts";
|
|
3
|
+
import { PollingSyncNotifier } from "./notifier.ts";
|
|
4
|
+
|
|
5
|
+
const POLL_INTERVAL_MS = 250;
|
|
6
|
+
const pollingNotifiers = new WeakMap<object, PollingSyncNotifier>();
|
|
7
|
+
|
|
8
|
+
export interface ResolvedSyncRuntime {
|
|
9
|
+
adapter: SyncAdapter;
|
|
10
|
+
notifier: SyncNotifier;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function resolveSyncRuntime(options: SyncRuntimeOptions): ResolvedSyncRuntime {
|
|
14
|
+
if (!IS_DEV && options.adapter.scope === "process-local") {
|
|
15
|
+
throw new Error("[furin] Production sync cannot use a process-local SyncAdapter.");
|
|
16
|
+
}
|
|
17
|
+
if (options.notifier) {
|
|
18
|
+
return { adapter: options.adapter, notifier: options.notifier };
|
|
19
|
+
}
|
|
20
|
+
const existing = pollingNotifiers.get(options.adapter);
|
|
21
|
+
if (existing) {
|
|
22
|
+
return { adapter: options.adapter, notifier: existing };
|
|
23
|
+
}
|
|
24
|
+
const notifier = new PollingSyncNotifier(options.adapter, POLL_INTERVAL_MS);
|
|
25
|
+
pollingNotifiers.set(options.adapter, notifier);
|
|
26
|
+
return { adapter: options.adapter, notifier };
|
|
27
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// biome-ignore-all lint/suspicious/useAwait: bun:sqlite is synchronous, while SyncAdapter methods intentionally normalize failures into rejected promises.
|
|
2
|
+
import type { Database } from "bun:sqlite";
|
|
3
|
+
import type {
|
|
4
|
+
BeginMutationInput,
|
|
5
|
+
BeginMutationResult,
|
|
6
|
+
ChangePage,
|
|
7
|
+
CompleteMutationInput,
|
|
8
|
+
CompleteMutationResult,
|
|
9
|
+
MutationLease,
|
|
10
|
+
ReadChangesInput,
|
|
11
|
+
StoredResponse,
|
|
12
|
+
SyncAdapter,
|
|
13
|
+
SyncChange,
|
|
14
|
+
SyncInvalidation,
|
|
15
|
+
} from "../adapter.ts";
|
|
16
|
+
import migrationSql from "./migration.sql" with { type: "text" };
|
|
17
|
+
|
|
18
|
+
const CHANGE_RETENTION = 1000;
|
|
19
|
+
const LEASE_MS = 30_000;
|
|
20
|
+
const MUTATION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
const UNSIGNED_INTEGER_PATTERN = /^\d+$/;
|
|
22
|
+
|
|
23
|
+
export interface SqliteSyncAdapterOptions {
|
|
24
|
+
database: Database;
|
|
25
|
+
namespace: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface MutationRow {
|
|
29
|
+
expires_at: number;
|
|
30
|
+
fingerprint: string;
|
|
31
|
+
lease_expires_at: number;
|
|
32
|
+
mutation_id: string;
|
|
33
|
+
response_body: Uint8Array | null;
|
|
34
|
+
response_headers: string | null;
|
|
35
|
+
response_status: number | null;
|
|
36
|
+
state: "in-progress" | "succeeded";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface CursorRow {
|
|
40
|
+
current_cursor: string;
|
|
41
|
+
oldest_cursor: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ChangeRow {
|
|
45
|
+
cursor: string;
|
|
46
|
+
invalidations: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface MinimumCursorRow {
|
|
50
|
+
cursor: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function mutationKey(input: Pick<MutationLease, "key" | "principal">): string {
|
|
54
|
+
return `${input.principal.length}:${input.principal}${input.key}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function storedResponse(row: MutationRow): StoredResponse {
|
|
58
|
+
if (row.response_body === null || row.response_headers === null || row.response_status === null) {
|
|
59
|
+
throw new Error("[furin-sync-sqlite] Succeeded mutation has no replay response.");
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
body: new Uint8Array(row.response_body),
|
|
63
|
+
headers: JSON.parse(row.response_headers) as [string, string][],
|
|
64
|
+
status: row.response_status,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function migrateSqliteSync(database: Database): void {
|
|
69
|
+
database.run(migrationSql);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class SqliteSyncAdapter implements SyncAdapter {
|
|
73
|
+
readonly scope: "host-local" | "process-local";
|
|
74
|
+
private readonly database: Database;
|
|
75
|
+
private readonly namespace: string;
|
|
76
|
+
|
|
77
|
+
constructor(options: SqliteSyncAdapterOptions) {
|
|
78
|
+
if (options.namespace.length === 0) {
|
|
79
|
+
throw new Error("[furin-sync-sqlite] namespace must not be empty.");
|
|
80
|
+
}
|
|
81
|
+
this.database = options.database;
|
|
82
|
+
this.namespace = options.namespace;
|
|
83
|
+
this.scope =
|
|
84
|
+
options.database.filename === "" || options.database.filename === ":memory:"
|
|
85
|
+
? "process-local"
|
|
86
|
+
: "host-local";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async beginMutation(input: BeginMutationInput): Promise<BeginMutationResult> {
|
|
90
|
+
const transaction = this.database.transaction(
|
|
91
|
+
(mutation: BeginMutationInput): BeginMutationResult => {
|
|
92
|
+
const now = Date.now();
|
|
93
|
+
const key = mutationKey(mutation);
|
|
94
|
+
this.database
|
|
95
|
+
.query<never, [number, number, number]>(
|
|
96
|
+
`DELETE FROM furin_sync_mutations WHERE rowid IN (
|
|
97
|
+
SELECT rowid FROM furin_sync_mutations
|
|
98
|
+
WHERE
|
|
99
|
+
(state = 'succeeded' AND expires_at <= ?)
|
|
100
|
+
OR (
|
|
101
|
+
state = 'in-progress'
|
|
102
|
+
AND lease_expires_at <= ?
|
|
103
|
+
AND expires_at <= ?
|
|
104
|
+
)
|
|
105
|
+
LIMIT 100
|
|
106
|
+
)`
|
|
107
|
+
)
|
|
108
|
+
.run(now, now, now);
|
|
109
|
+
const existing = this.database
|
|
110
|
+
.query<MutationRow, [string, string]>(
|
|
111
|
+
`SELECT mutation_id, fingerprint, state, response_status, response_headers,
|
|
112
|
+
response_body, lease_expires_at, expires_at
|
|
113
|
+
FROM furin_sync_mutations
|
|
114
|
+
WHERE namespace = ? AND mutation_key = ?`
|
|
115
|
+
)
|
|
116
|
+
.get(this.namespace, key);
|
|
117
|
+
if (existing?.fingerprint !== undefined && existing.fingerprint !== mutation.fingerprint) {
|
|
118
|
+
return { kind: "conflict", reason: "payload-mismatch" };
|
|
119
|
+
}
|
|
120
|
+
const expired =
|
|
121
|
+
existing?.state === "in-progress"
|
|
122
|
+
? existing.lease_expires_at <= now
|
|
123
|
+
: existing?.expires_at !== undefined && existing.expires_at <= now;
|
|
124
|
+
if (existing && !expired) {
|
|
125
|
+
return existing.state === "in-progress"
|
|
126
|
+
? { kind: "conflict", reason: "in-progress" }
|
|
127
|
+
: { kind: "replay", response: storedResponse(existing) };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const id = crypto.randomUUID();
|
|
131
|
+
this.database
|
|
132
|
+
.query<never, [string, string, string, string, number, number, number]>(
|
|
133
|
+
`INSERT INTO furin_sync_mutations (
|
|
134
|
+
namespace, mutation_key, mutation_id, fingerprint, state,
|
|
135
|
+
lease_expires_at, expires_at, created_at
|
|
136
|
+
) VALUES (?, ?, ?, ?, 'in-progress', ?, ?, ?)
|
|
137
|
+
ON CONFLICT (namespace, mutation_key) DO UPDATE SET
|
|
138
|
+
mutation_id = excluded.mutation_id,
|
|
139
|
+
fingerprint = excluded.fingerprint,
|
|
140
|
+
state = 'in-progress',
|
|
141
|
+
response_status = NULL,
|
|
142
|
+
response_headers = NULL,
|
|
143
|
+
response_body = NULL,
|
|
144
|
+
lease_expires_at = excluded.lease_expires_at,
|
|
145
|
+
expires_at = excluded.expires_at,
|
|
146
|
+
created_at = excluded.created_at,
|
|
147
|
+
completed_at = NULL`
|
|
148
|
+
)
|
|
149
|
+
.run(
|
|
150
|
+
this.namespace,
|
|
151
|
+
key,
|
|
152
|
+
id,
|
|
153
|
+
mutation.fingerprint,
|
|
154
|
+
now + LEASE_MS,
|
|
155
|
+
now + MUTATION_TTL_MS,
|
|
156
|
+
now
|
|
157
|
+
);
|
|
158
|
+
return {
|
|
159
|
+
kind: "execute",
|
|
160
|
+
lease: { id, key: mutation.key, leaseMs: LEASE_MS, principal: mutation.principal },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
return transaction.immediate(input);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async completeMutation(input: CompleteMutationInput): Promise<CompleteMutationResult> {
|
|
168
|
+
const transaction = this.database.transaction(
|
|
169
|
+
(completion: CompleteMutationInput): CompleteMutationResult => {
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
const key = mutationKey(completion.lease);
|
|
172
|
+
const active = this.database
|
|
173
|
+
.query<{ mutation_id: string }, [string, string, string, number]>(
|
|
174
|
+
`SELECT mutation_id FROM furin_sync_mutations
|
|
175
|
+
WHERE namespace = ? AND mutation_key = ? AND mutation_id = ?
|
|
176
|
+
AND state = 'in-progress' AND lease_expires_at > ?`
|
|
177
|
+
)
|
|
178
|
+
.get(this.namespace, key, completion.lease.id, now);
|
|
179
|
+
if (!active) {
|
|
180
|
+
return { kind: "lost" };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let cursor: string | undefined;
|
|
184
|
+
if (completion.invalidations.length > 0) {
|
|
185
|
+
this.database
|
|
186
|
+
.query<never, [string]>(
|
|
187
|
+
`INSERT INTO furin_sync_streams (namespace, current_cursor, oldest_cursor)
|
|
188
|
+
VALUES (?, 0, 0) ON CONFLICT (namespace) DO NOTHING`
|
|
189
|
+
)
|
|
190
|
+
.run(this.namespace);
|
|
191
|
+
this.database
|
|
192
|
+
.query<never, [string]>(
|
|
193
|
+
`UPDATE furin_sync_streams SET current_cursor = current_cursor + 1
|
|
194
|
+
WHERE namespace = ?`
|
|
195
|
+
)
|
|
196
|
+
.run(this.namespace);
|
|
197
|
+
const cursorRow = this.database
|
|
198
|
+
.query<Pick<CursorRow, "current_cursor">, [string]>(
|
|
199
|
+
`SELECT CAST(current_cursor AS TEXT) AS current_cursor
|
|
200
|
+
FROM furin_sync_streams WHERE namespace = ?`
|
|
201
|
+
)
|
|
202
|
+
.get(this.namespace);
|
|
203
|
+
if (!cursorRow) {
|
|
204
|
+
throw new Error("[furin-sync-sqlite] Could not allocate a change cursor.");
|
|
205
|
+
}
|
|
206
|
+
cursor = cursorRow.current_cursor;
|
|
207
|
+
this.database
|
|
208
|
+
.query<never, [string, string, string, number]>(
|
|
209
|
+
`INSERT INTO furin_sync_changes (namespace, cursor, invalidations, created_at)
|
|
210
|
+
VALUES (?, ?, ?, ?)`
|
|
211
|
+
)
|
|
212
|
+
.run(
|
|
213
|
+
this.namespace,
|
|
214
|
+
cursorRow.current_cursor,
|
|
215
|
+
JSON.stringify(completion.invalidations),
|
|
216
|
+
now
|
|
217
|
+
);
|
|
218
|
+
const removed = this.database
|
|
219
|
+
.query<never, [string, string, number]>(
|
|
220
|
+
`DELETE FROM furin_sync_changes
|
|
221
|
+
WHERE namespace = ? AND cursor <= MAX(0, CAST(? AS INTEGER) - ?)`
|
|
222
|
+
)
|
|
223
|
+
.run(this.namespace, cursorRow.current_cursor, CHANGE_RETENTION);
|
|
224
|
+
if (removed.changes > 0) {
|
|
225
|
+
const oldest = this.database
|
|
226
|
+
.query<MinimumCursorRow, [string]>(
|
|
227
|
+
`SELECT CAST(MIN(cursor) AS TEXT) AS cursor
|
|
228
|
+
FROM furin_sync_changes WHERE namespace = ?`
|
|
229
|
+
)
|
|
230
|
+
.get(this.namespace);
|
|
231
|
+
this.database
|
|
232
|
+
.query<never, [string, string]>(
|
|
233
|
+
"UPDATE furin_sync_streams SET oldest_cursor = ? WHERE namespace = ?"
|
|
234
|
+
)
|
|
235
|
+
.run(oldest?.cursor ?? cursorRow.current_cursor, this.namespace);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
this.database
|
|
240
|
+
.query<never, [number, string, Uint8Array, number, number, string, string, string]>(
|
|
241
|
+
`UPDATE furin_sync_mutations
|
|
242
|
+
SET state = 'succeeded', response_status = ?, response_headers = ?,
|
|
243
|
+
response_body = ?, completed_at = ?, expires_at = ?
|
|
244
|
+
WHERE namespace = ? AND mutation_key = ? AND mutation_id = ?`
|
|
245
|
+
)
|
|
246
|
+
.run(
|
|
247
|
+
completion.response.status,
|
|
248
|
+
JSON.stringify(completion.response.headers),
|
|
249
|
+
completion.response.body,
|
|
250
|
+
now,
|
|
251
|
+
now + MUTATION_TTL_MS,
|
|
252
|
+
this.namespace,
|
|
253
|
+
key,
|
|
254
|
+
completion.lease.id
|
|
255
|
+
);
|
|
256
|
+
return { cursor, kind: "committed" };
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
return transaction.immediate(input);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async abortMutation(lease: MutationLease): Promise<void> {
|
|
263
|
+
this.database
|
|
264
|
+
.query<never, [string, string, string]>(
|
|
265
|
+
`DELETE FROM furin_sync_mutations
|
|
266
|
+
WHERE namespace = ? AND mutation_key = ? AND mutation_id = ? AND state = 'in-progress'`
|
|
267
|
+
)
|
|
268
|
+
.run(this.namespace, mutationKey(lease), lease.id);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async currentCursor(): Promise<string> {
|
|
272
|
+
const row = this.database
|
|
273
|
+
.query<Pick<CursorRow, "current_cursor">, [string]>(
|
|
274
|
+
`SELECT CAST(current_cursor AS TEXT) AS current_cursor
|
|
275
|
+
FROM furin_sync_streams WHERE namespace = ?`
|
|
276
|
+
)
|
|
277
|
+
.get(this.namespace);
|
|
278
|
+
return row?.current_cursor ?? "0";
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async readChanges(input: ReadChangesInput): Promise<ChangePage> {
|
|
282
|
+
const transaction = this.database.transaction((page: ReadChangesInput): ChangePage => {
|
|
283
|
+
const cursorRow = this.database
|
|
284
|
+
.query<CursorRow, [string]>(
|
|
285
|
+
`SELECT CAST(current_cursor AS TEXT) AS current_cursor,
|
|
286
|
+
CAST(oldest_cursor AS TEXT) AS oldest_cursor
|
|
287
|
+
FROM furin_sync_streams WHERE namespace = ?`
|
|
288
|
+
)
|
|
289
|
+
.get(this.namespace);
|
|
290
|
+
const currentCursor = cursorRow?.current_cursor ?? "0";
|
|
291
|
+
if (page.after === undefined) {
|
|
292
|
+
return { changes: [], cursor: currentCursor, hasMore: false, reset: false };
|
|
293
|
+
}
|
|
294
|
+
if (
|
|
295
|
+
!UNSIGNED_INTEGER_PATTERN.test(page.after) ||
|
|
296
|
+
BigInt(page.after) > BigInt(currentCursor) ||
|
|
297
|
+
BigInt(page.after) < BigInt(cursorRow?.oldest_cursor ?? "0") - 1n
|
|
298
|
+
) {
|
|
299
|
+
return { changes: [], cursor: currentCursor, hasMore: false, reset: true };
|
|
300
|
+
}
|
|
301
|
+
const rows = this.database
|
|
302
|
+
.query<ChangeRow, [string, string, number]>(
|
|
303
|
+
`SELECT CAST(cursor AS TEXT) AS cursor, invalidations FROM furin_sync_changes
|
|
304
|
+
WHERE namespace = ? AND cursor > CAST(? AS INTEGER) ORDER BY cursor ASC LIMIT ?`
|
|
305
|
+
)
|
|
306
|
+
.all(this.namespace, page.after, page.limit + 1);
|
|
307
|
+
const hasMore = rows.length > page.limit;
|
|
308
|
+
const changes: SyncChange[] = rows.slice(0, page.limit).map((row) => ({
|
|
309
|
+
cursor: row.cursor,
|
|
310
|
+
invalidations: JSON.parse(row.invalidations) as SyncInvalidation[],
|
|
311
|
+
}));
|
|
312
|
+
return {
|
|
313
|
+
changes,
|
|
314
|
+
cursor: changes.at(-1)?.cursor ?? page.after,
|
|
315
|
+
hasMore,
|
|
316
|
+
reset: false,
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
return transaction(input);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async renewMutation(lease: MutationLease): Promise<"lost" | "renewed"> {
|
|
323
|
+
const now = Date.now();
|
|
324
|
+
const result = this.database
|
|
325
|
+
.query<never, [number, string, string, string, number]>(
|
|
326
|
+
`UPDATE furin_sync_mutations SET lease_expires_at = ?
|
|
327
|
+
WHERE namespace = ? AND mutation_key = ? AND mutation_id = ?
|
|
328
|
+
AND state = 'in-progress' AND lease_expires_at > ?`
|
|
329
|
+
)
|
|
330
|
+
.run(now + lease.leaseMs, this.namespace, mutationKey(lease), lease.id, now);
|
|
331
|
+
return result.changes === 1 ? "renewed" : "lost";
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function sqliteSyncAdapter(options: SqliteSyncAdapterOptions): SqliteSyncAdapter {
|
|
336
|
+
return new SqliteSyncAdapter(options);
|
|
337
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
CREATE TABLE IF NOT EXISTS furin_sync_mutations (
|
|
2
|
+
namespace TEXT NOT NULL,
|
|
3
|
+
mutation_key TEXT NOT NULL,
|
|
4
|
+
mutation_id TEXT NOT NULL,
|
|
5
|
+
fingerprint TEXT NOT NULL,
|
|
6
|
+
state TEXT NOT NULL CHECK (state IN ('in-progress', 'succeeded')),
|
|
7
|
+
response_status INTEGER,
|
|
8
|
+
response_headers TEXT,
|
|
9
|
+
response_body BLOB,
|
|
10
|
+
lease_expires_at INTEGER NOT NULL,
|
|
11
|
+
expires_at INTEGER NOT NULL,
|
|
12
|
+
created_at INTEGER NOT NULL,
|
|
13
|
+
completed_at INTEGER,
|
|
14
|
+
CONSTRAINT furin_sync_mutations_succeeded_response_check CHECK (
|
|
15
|
+
state <> 'succeeded'
|
|
16
|
+
OR (
|
|
17
|
+
response_status IS NOT NULL
|
|
18
|
+
AND response_headers IS NOT NULL
|
|
19
|
+
AND response_body IS NOT NULL
|
|
20
|
+
AND completed_at IS NOT NULL
|
|
21
|
+
)
|
|
22
|
+
),
|
|
23
|
+
PRIMARY KEY (namespace, mutation_key),
|
|
24
|
+
UNIQUE (mutation_id)
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
CREATE TRIGGER IF NOT EXISTS furin_sync_mutations_succeeded_response_insert
|
|
28
|
+
BEFORE INSERT ON furin_sync_mutations
|
|
29
|
+
WHEN NEW.state = 'succeeded'
|
|
30
|
+
AND (
|
|
31
|
+
NEW.response_status IS NULL
|
|
32
|
+
OR NEW.response_headers IS NULL
|
|
33
|
+
OR NEW.response_body IS NULL
|
|
34
|
+
OR NEW.completed_at IS NULL
|
|
35
|
+
)
|
|
36
|
+
BEGIN
|
|
37
|
+
SELECT RAISE(ABORT, 'CHECK constraint failed: furin_sync_mutations_succeeded_response_check');
|
|
38
|
+
END;
|
|
39
|
+
|
|
40
|
+
CREATE TRIGGER IF NOT EXISTS furin_sync_mutations_succeeded_response_update
|
|
41
|
+
BEFORE UPDATE ON furin_sync_mutations
|
|
42
|
+
WHEN NEW.state = 'succeeded'
|
|
43
|
+
AND (
|
|
44
|
+
NEW.response_status IS NULL
|
|
45
|
+
OR NEW.response_headers IS NULL
|
|
46
|
+
OR NEW.response_body IS NULL
|
|
47
|
+
OR NEW.completed_at IS NULL
|
|
48
|
+
)
|
|
49
|
+
BEGIN
|
|
50
|
+
SELECT RAISE(ABORT, 'CHECK constraint failed: furin_sync_mutations_succeeded_response_check');
|
|
51
|
+
END;
|
|
52
|
+
|
|
53
|
+
CREATE TABLE IF NOT EXISTS furin_sync_streams (
|
|
54
|
+
namespace TEXT PRIMARY KEY,
|
|
55
|
+
current_cursor INTEGER NOT NULL DEFAULT 0,
|
|
56
|
+
oldest_cursor INTEGER NOT NULL DEFAULT 0
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
CREATE TABLE IF NOT EXISTS furin_sync_changes (
|
|
60
|
+
namespace TEXT NOT NULL,
|
|
61
|
+
cursor INTEGER NOT NULL,
|
|
62
|
+
invalidations TEXT NOT NULL,
|
|
63
|
+
created_at INTEGER NOT NULL,
|
|
64
|
+
PRIMARY KEY (namespace, cursor)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
CREATE INDEX IF NOT EXISTS furin_sync_mutations_expiry_idx
|
|
68
|
+
ON furin_sync_mutations (expires_at);
|
|
69
|
+
|
|
70
|
+
DROP INDEX IF EXISTS furin_sync_changes_created_idx;
|