@syncular/server 0.58.0 → 0.59.0
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 +31 -22
- package/dist/admin.js +8 -4
- package/dist/blob-handlers.d.ts +25 -31
- package/dist/blob-handlers.js +63 -41
- package/dist/blob-store.d.ts +42 -24
- package/dist/blob-store.js +0 -0
- package/dist/bucket-blob-store.d.ts +12 -8
- package/dist/bucket-blob-store.js +54 -45
- package/dist/bucket-segment-store.js +18 -16
- package/dist/content-encoding.d.ts +5 -2
- package/dist/content-encoding.js +42 -13
- package/dist/context.d.ts +16 -0
- package/dist/context.js +21 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +223 -1111
- package/dist/errors.js +8 -0
- package/dist/events.d.ts +1 -1
- package/dist/handler.js +135 -103
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +17 -3
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- package/dist/pg-executor-pglite.d.ts +5 -6
- package/dist/pg-executor-pglite.js +22 -14
- package/dist/pg-executor.d.ts +4 -1
- package/dist/postgres-storage.d.ts +1 -1
- package/dist/postgres-storage.js +182 -105
- package/dist/pull.d.ts +28 -20
- package/dist/pull.js +85 -87
- package/dist/push.js +272 -147
- package/dist/realtime.d.ts +68 -28
- package/dist/realtime.js +297 -136
- package/dist/relational-rows.d.ts +27 -15
- package/dist/relational-rows.js +38 -22
- package/dist/s3-blob-store.d.ts +11 -12
- package/dist/s3-blob-store.js +20 -13
- package/dist/s3-object-bucket.d.ts +8 -1
- package/dist/s3-object-bucket.js +16 -3
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +23 -0
- package/dist/scopes.d.ts +6 -2
- package/dist/scopes.js +22 -4
- package/dist/seed.js +1 -2
- package/dist/segment-download.js +18 -4
- package/dist/signed-url.d.ts +2 -2
- package/dist/signed-url.js +9 -2
- package/dist/sigv4.d.ts +8 -2
- package/dist/sigv4.js +8 -4
- package/dist/sqlite-blob-store.d.ts +1 -1
- package/dist/sqlite-blob-store.js +23 -22
- package/dist/sqlite-dialect.d.ts +150 -47
- package/dist/sqlite-dialect.js +987 -88
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +293 -1117
- package/dist/storage.d.ts +32 -12
- package/package.json +2 -2
- package/src/admin.ts +16 -2
- package/src/blob-handlers.ts +91 -72
- package/src/blob-store.ts +0 -0
- package/src/bucket-blob-store.ts +75 -48
- package/src/bucket-segment-store.ts +20 -15
- package/src/content-encoding.ts +48 -16
- package/src/context.ts +32 -1
- package/src/d1-storage.ts +326 -1692
- package/src/errors.ts +8 -0
- package/src/events.ts +1 -1
- package/src/handler.ts +178 -127
- package/src/local-authority.ts +17 -3
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +231 -138
- package/src/pull.ts +133 -147
- package/src/push.ts +356 -205
- package/src/realtime.ts +380 -179
- package/src/relational-rows.ts +54 -27
- package/src/s3-blob-store.ts +23 -13
- package/src/s3-object-bucket.ts +20 -2
- package/src/schema.ts +31 -0
- package/src/scopes.ts +23 -3
- package/src/seed.ts +1 -2
- package/src/segment-download.ts +25 -4
- package/src/signed-url.ts +10 -4
- package/src/sigv4.ts +17 -4
- package/src/sqlite-blob-store.ts +27 -26
- package/src/sqlite-dialect.ts +1607 -114
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +360 -1811
- package/src/storage.ts +33 -12
package/dist/sigv4.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Minimal AWS Signature Version 4 — exactly the subset the S3 segment
|
|
3
|
-
*
|
|
3
|
+
* and blob stores need (header-signed requests and presigned URLs), hand-rolled
|
|
4
4
|
* per the dependency-light doctrine: SigV4 is small and
|
|
5
5
|
* well-specified, so no SDK.
|
|
6
6
|
*
|
|
@@ -127,7 +127,7 @@ export async function signRequest(args) {
|
|
|
127
127
|
authorization: `AWS4-HMAC-SHA256 Credential=${args.credentials.accessKeyId}/${scope},SignedHeaders=${signedHeaders},Signature=${signature}`,
|
|
128
128
|
};
|
|
129
129
|
}
|
|
130
|
-
/** Query-authenticated (presigned) URL
|
|
130
|
+
/** Query-authenticated (presigned) URL over `host` plus `headers`. */
|
|
131
131
|
export async function presignUrl(args) {
|
|
132
132
|
const service = args.service ?? 's3';
|
|
133
133
|
const method = args.method ?? 'GET';
|
|
@@ -138,7 +138,11 @@ export async function presignUrl(args) {
|
|
|
138
138
|
url.searchParams.set('X-Amz-Credential', `${args.credentials.accessKeyId}/${scope}`);
|
|
139
139
|
url.searchParams.set('X-Amz-Date', amzDate);
|
|
140
140
|
url.searchParams.set('X-Amz-Expires', String(args.expiresSeconds));
|
|
141
|
-
|
|
141
|
+
const headers = { ...args.headers, host: url.host };
|
|
142
|
+
url.searchParams.set('X-Amz-SignedHeaders', Object.keys(headers)
|
|
143
|
+
.map((name) => name.toLowerCase())
|
|
144
|
+
.sort()
|
|
145
|
+
.join(';'));
|
|
142
146
|
if (args.credentials.sessionToken !== undefined) {
|
|
143
147
|
url.searchParams.set('X-Amz-Security-Token', args.credentials.sessionToken);
|
|
144
148
|
}
|
|
@@ -146,7 +150,7 @@ export async function presignUrl(args) {
|
|
|
146
150
|
method,
|
|
147
151
|
canonicalPath: url.pathname,
|
|
148
152
|
query: url.searchParams,
|
|
149
|
-
headers
|
|
153
|
+
headers,
|
|
150
154
|
payloadHash: UNSIGNED_PAYLOAD,
|
|
151
155
|
});
|
|
152
156
|
const signature = await sigV4Signature({
|
|
@@ -13,6 +13,6 @@ export declare class SqliteBlobStore implements BlobStore {
|
|
|
13
13
|
record: BlobRecord;
|
|
14
14
|
bytes: Uint8Array;
|
|
15
15
|
} | undefined>;
|
|
16
|
-
sweepOrphans(partition: string, olderThanMs: number,
|
|
16
|
+
sweepOrphans(partition: string, olderThanMs: number, isReferenced: (blobId: string) => Promise<boolean>): Promise<string[]>;
|
|
17
17
|
stats(partition: string): Promise<BlobStoreStats>;
|
|
18
18
|
}
|
|
@@ -16,29 +16,27 @@ export class SqliteBlobStore {
|
|
|
16
16
|
`);
|
|
17
17
|
}
|
|
18
18
|
async put(partition, blobId, bytes, nowMs, mediaType) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
// A re-upload refreshes the sweep grace clock and names the media type an
|
|
20
|
+
// earlier upload left out; it never replaces a recorded one.
|
|
21
|
+
const row = this.db
|
|
22
|
+
.query(`INSERT INTO sync_blobs(
|
|
23
|
+
partition, blob_id, byte_length, media_type, created_at_ms, bytes
|
|
24
|
+
) VALUES (?,?,?,?,?,?)
|
|
25
|
+
ON CONFLICT(partition, blob_id) DO UPDATE SET
|
|
26
|
+
media_type = coalesce(media_type, excluded.media_type),
|
|
27
|
+
created_at_ms = max(created_at_ms, excluded.created_at_ms)
|
|
28
|
+
RETURNING media_type, created_at_ms`)
|
|
29
|
+
.get(partition, blobId, bytes.length, mediaType ?? null, nowMs, bytes);
|
|
30
|
+
if (row === null) {
|
|
31
|
+
throw new Error('SqliteBlobStore: blob upsert returned no row');
|
|
28
32
|
}
|
|
29
|
-
|
|
33
|
+
return {
|
|
30
34
|
blobId,
|
|
31
35
|
partition,
|
|
32
36
|
byteLength: bytes.length,
|
|
33
|
-
...(
|
|
34
|
-
createdAtMs:
|
|
37
|
+
...(row.media_type !== null ? { mediaType: row.media_type } : {}),
|
|
38
|
+
createdAtMs: row.created_at_ms,
|
|
35
39
|
};
|
|
36
|
-
this.db
|
|
37
|
-
.query(`INSERT OR IGNORE INTO sync_blobs(
|
|
38
|
-
partition, blob_id, byte_length, media_type, created_at_ms, bytes
|
|
39
|
-
) VALUES (?,?,?,?,?,?)`)
|
|
40
|
-
.run(partition, blobId, bytes.length, mediaType ?? null, nowMs, bytes);
|
|
41
|
-
return record;
|
|
42
40
|
}
|
|
43
41
|
async delete(partition, blobId) {
|
|
44
42
|
this.db
|
|
@@ -69,16 +67,19 @@ export class SqliteBlobStore {
|
|
|
69
67
|
bytes: new Uint8Array(row.bytes),
|
|
70
68
|
};
|
|
71
69
|
}
|
|
72
|
-
async sweepOrphans(partition, olderThanMs,
|
|
70
|
+
async sweepOrphans(partition, olderThanMs, isReferenced) {
|
|
73
71
|
const candidates = this.db
|
|
74
72
|
.query(`SELECT blob_id FROM sync_blobs
|
|
75
73
|
WHERE partition=? AND created_at_ms < ?`)
|
|
76
74
|
.all(partition, olderThanMs);
|
|
77
75
|
const swept = [];
|
|
78
|
-
|
|
76
|
+
// The age condition repeats in the DELETE: a re-upload during the
|
|
77
|
+
// reference lookup refreshed the grace clock and keeps the blob.
|
|
78
|
+
const del = this.db.query('DELETE FROM sync_blobs WHERE partition=? AND blob_id=? AND created_at_ms < ?');
|
|
79
79
|
for (const { blob_id } of candidates) {
|
|
80
|
-
if (
|
|
81
|
-
|
|
80
|
+
if (await isReferenced(blob_id))
|
|
81
|
+
continue;
|
|
82
|
+
if (del.run(partition, blob_id, olderThanMs).changes > 0) {
|
|
82
83
|
swept.push(blob_id);
|
|
83
84
|
}
|
|
84
85
|
}
|
package/dist/sqlite-dialect.d.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared SQLite dialect for synchronous `SqliteServerStorage` on Bun or Node
|
|
3
3
|
* and asynchronous `D1ServerStorage` on Cloudflare Workers. D1 is SQLite:
|
|
4
|
-
* same DDL, same statement grammar,
|
|
5
|
-
*
|
|
6
|
-
* OR IGNORE` upsert idioms — so the schema and the value (de)serialization are
|
|
7
|
-
* genuinely common ground and live here.
|
|
4
|
+
* same DDL, same statement grammar, same `?` positional placeholders, same
|
|
5
|
+
* `INSERT ... ON CONFLICT` / `INSERT OR IGNORE` upsert idioms.
|
|
8
6
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
7
|
+
* `SqliteFamilyStorage` and `SqliteFamilyTransaction` hold every statement
|
|
8
|
+
* and row mapping the two backends share. A backend supplies statement
|
|
9
|
+
* execution through a small seam (`all`, `guardedAll`, `atomic`; `run` and
|
|
10
|
+
* `all` inside a transaction) and keeps only what differs: connection
|
|
11
|
+
* handling, the request lock in SQLite, transaction execution (interactive
|
|
12
|
+
* `BEGIN IMMEDIATE` in SQLite, one buffered `db.batch` in D1), D1's
|
|
13
|
+
* bound-parameter cap and its published-schema guard. The seam returns
|
|
14
|
+
* promises; the synchronous SQLite driver resolves them with rows it read at
|
|
15
|
+
* call time. Both classes run the identical `test/storage-contract.ts`.
|
|
17
16
|
*/
|
|
18
17
|
import type { ScopeMap } from '@syncular/core';
|
|
19
|
-
import
|
|
18
|
+
import { type BoundAuthoritativeQuery } from './authoritative-query.js';
|
|
19
|
+
import type { CompiledTable } from './schema.js';
|
|
20
|
+
import type { SqliteValue } from './sqlite-driver.js';
|
|
21
|
+
import type { AuthoritativeQueryRequest, ClientCursorInfo, ClientPushResult, ClientRecord, ClientRecordWriteOptions, CommitDetail, CommitMetadata, CommitMetadataQuery, CommitPruneQuery, CommitPruneResult, CommitWindowQuery, IndexRowScanQuery, NewCommit, NewReaction, PartitionRegistryEntry, PrunedReactionCounts, ReactionClaimQuery, ReactionFailureUpdate, ReactionListQuery, ReactionPruneQuery, RowChangeRecord, RowChangesQuery, RowListQuery, RowScanQuery, ScopeActivityQuery, ScopeCommitActivity, ScopeValueHistoryQuery, ScopeValueQuery, StoredChange, StoredCommit, StoredPushResult, StoredReaction, StoredRow, Tombstone } from './storage.js';
|
|
20
22
|
/**
|
|
21
23
|
* Schema DDL, one statement per `;`-delimited chunk. Native SQLite applies
|
|
22
24
|
* the whole string via `db.exec(SQLITE_DDL)`; D1 applies each statement
|
|
@@ -30,47 +32,26 @@ import type { ScopeValueHistoryQuery, StoredChange, StoredCommit, StoredPushResu
|
|
|
30
32
|
* the Postgres storage documents (§3.1, performance-by-
|
|
31
33
|
* construction).
|
|
32
34
|
*
|
|
33
|
-
* `sync_row_scopes_by_row` is
|
|
34
|
-
* scope
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* The old `sync_row_scopes_by_row` index is dropped: with it present the
|
|
36
|
+
* planner answered a scope scan of two or more values by walking the index
|
|
37
|
+
* in `row_id` order and filtering, instead of seeking the PRIMARY KEY once
|
|
38
|
+
* per value. The per-row scope replacement (`deleteSqliteRowScopesSql`)
|
|
39
|
+
* binds `(partition, tbl, var, value, row_id)` and needs no secondary index.
|
|
40
|
+
* `DROP INDEX IF EXISTS` stays in the DDL so existing databases lose the
|
|
41
|
+
* index the next time the DDL runs: on every `SqliteServerStorage`
|
|
42
|
+
* construction, and on D1 in `migrate()` and the core phase of
|
|
43
|
+
* `migrateSchema`.
|
|
38
44
|
*/
|
|
39
|
-
export declare const SQLITE_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq INTEGER NOT NULL DEFAULT 0,\n horizon_seq INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required INTEGER NOT NULL DEFAULT 0,\n last_authenticated_at_ms INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\
|
|
40
|
-
/** Columns added to
|
|
41
|
-
export declare const
|
|
42
|
-
readonly last_round_at_ms: 'INTEGER';
|
|
43
|
-
readonly last_error_code: 'TEXT';
|
|
44
|
-
readonly consecutive_failures: 'INTEGER NOT NULL DEFAULT 0';
|
|
45
|
-
};
|
|
45
|
+
export declare const SQLITE_DDL = "\nCREATE TABLE IF NOT EXISTS sync_partitions(\n partition TEXT PRIMARY KEY,\n max_commit_seq INTEGER NOT NULL DEFAULT 0,\n horizon_seq INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS sync_partition_registry(\n partition TEXT PRIMARY KEY,\n log_epoch TEXT NOT NULL,\n epoch_required INTEGER NOT NULL DEFAULT 0,\n last_authenticated_at_ms INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS sync_row_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, row_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, row_id)\n);\nDROP INDEX IF EXISTS sync_row_scopes_by_row;\nCREATE TABLE IF NOT EXISTS sync_commits(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n client_id TEXT NOT NULL, client_commit_id TEXT NOT NULL,\n actor_id TEXT NOT NULL, created_at_ms INTEGER NOT NULL,\n writer_version INTEGER,\n PRIMARY KEY(partition, commit_seq)\n);\nCREATE INDEX IF NOT EXISTS sync_commits_by_time\n ON sync_commits(partition, created_at_ms);\nCREATE TABLE IF NOT EXISTS sync_changes(\n partition TEXT NOT NULL, commit_seq INTEGER NOT NULL, idx INTEGER NOT NULL,\n tbl TEXT NOT NULL, row_id TEXT NOT NULL, op INTEGER NOT NULL,\n row_version INTEGER, scopes TEXT NOT NULL, payload BLOB,\n PRIMARY KEY(partition, commit_seq, idx)\n);\nCREATE INDEX IF NOT EXISTS sync_changes_by_table\n ON sync_changes(partition, commit_seq, tbl, idx);\nCREATE TABLE IF NOT EXISTS sync_change_scopes(\n partition TEXT NOT NULL, tbl TEXT NOT NULL,\n var TEXT NOT NULL, value TEXT NOT NULL, commit_seq INTEGER NOT NULL,\n PRIMARY KEY(partition, tbl, var, value, commit_seq)\n);\nCREATE TABLE IF NOT EXISTS sync_push_results(\n partition TEXT NOT NULL, client_id TEXT NOT NULL,\n client_commit_id TEXT NOT NULL, result TEXT NOT NULL,\n PRIMARY KEY(partition, client_id, client_commit_id)\n);\nCREATE TABLE IF NOT EXISTS sync_reactions(\n partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,\n type TEXT NOT NULL, version INTEGER NOT NULL, payload TEXT NOT NULL,\n source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,\n source_commit_seq INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,\n available_at_ms INTEGER NOT NULL, status TEXT NOT NULL,\n attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,\n lease_owner TEXT, lease_expires_at_ms INTEGER, completed_at_ms INTEGER,\n last_failure TEXT,\n PRIMARY KEY(partition, idempotency_key),\n CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))\n);\nCREATE INDEX IF NOT EXISTS sync_reactions_due\n ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_lease\n ON sync_reactions(partition, status, lease_expires_at_ms);\nCREATE INDEX IF NOT EXISTS sync_reactions_completed\n ON sync_reactions(partition, status, completed_at_ms, idempotency_key);\nCREATE INDEX IF NOT EXISTS sync_reactions_dead_letter\n ON sync_reactions(partition, status, available_at_ms, idempotency_key);\nCREATE TABLE IF NOT EXISTS sync_clients(\n partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,\n wire_version INTEGER NOT NULL DEFAULT 1,\n cursor INTEGER NOT NULL, subscriptions TEXT NOT NULL,\n updated_at_ms INTEGER NOT NULL,\n last_round_at_ms INTEGER, last_error_code TEXT,\n consecutive_failures INTEGER NOT NULL DEFAULT 0,\n PRIMARY KEY(partition, client_id)\n);\nCREATE TABLE IF NOT EXISTS sync_blob_refs(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n blob_id TEXT NOT NULL,\n PRIMARY KEY(partition, tbl, row_id, blob_id)\n);\nCREATE INDEX IF NOT EXISTS sync_blob_refs_by_blob\n ON sync_blob_refs(partition, blob_id);\nCREATE TABLE IF NOT EXISTS sync_tombstones(\n partition TEXT NOT NULL, tbl TEXT NOT NULL, row_id TEXT NOT NULL,\n commit_seq INTEGER NOT NULL, server_version INTEGER, scopes TEXT,\n PRIMARY KEY(partition, tbl, row_id)\n);\nCREATE TABLE IF NOT EXISTS sync_backfill_checkpoints(\n partition TEXT NOT NULL, name TEXT NOT NULL,\n schema_version INTEGER NOT NULL,\n state TEXT NOT NULL CHECK(state IN ('declared','backfilling','activated')),\n watermark INTEGER NOT NULL DEFAULT 0,\n owner_epoch INTEGER NOT NULL DEFAULT 0,\n observed_rows INTEGER NOT NULL DEFAULT 0,\n updated_at_ms INTEGER NOT NULL,\n PRIMARY KEY(partition, name)\n);\nCREATE TABLE IF NOT EXISTS sync_writer_fence(\n partition TEXT PRIMARY KEY,\n required_writer_version INTEGER NOT NULL\n);\n";
|
|
46
|
+
/** Columns added to core tables after their first release: table, column, type. */
|
|
47
|
+
export declare const SQLITE_ADDED_COLUMNS: readonly [readonly ['sync_clients', 'wire_version', 'INTEGER NOT NULL DEFAULT 1'], readonly ['sync_clients', 'last_round_at_ms', 'INTEGER'], readonly ['sync_clients', 'last_error_code', 'TEXT'], readonly ['sync_clients', 'consecutive_failures', 'INTEGER NOT NULL DEFAULT 0'], readonly ['sync_tombstones', 'server_version', 'INTEGER'], readonly ['sync_tombstones', 'scopes', 'TEXT']];
|
|
46
48
|
/** Split the DDL into individual statements (D1 applies them one by one). */
|
|
47
49
|
export declare function sqliteDdlStatements(): string[];
|
|
48
|
-
/** §4.6.1: rowIds of one table bound to one scope value, budgeted. */
|
|
49
|
-
export declare const LIST_SCOPE_VALUE_ROWS_SQLITE_SQL = "SELECT row_id FROM sync_row_scopes WHERE partition=? AND tbl=? AND var=? AND value=? ORDER BY row_id LIMIT ?";
|
|
50
|
-
/**
|
|
51
|
-
* §4.6.1 history scrub, bound by `eraseScopeValueHistoryParams`: upsert change
|
|
52
|
-
* records of the binding first, then the change scope entries whose commit
|
|
53
|
-
* no longer holds a change of the binding. Each returns one row per removed
|
|
54
|
-
* record (D1 batch results carry no reliable change count).
|
|
55
|
-
*/
|
|
56
|
-
export declare const ERASE_SCOPE_VALUE_HISTORY_SQLITE_SQL: readonly [`DELETE FROM sync_changes WHERE rowid IN (
|
|
57
|
-
SELECT c.rowid FROM sync_change_scopes s JOIN sync_changes c
|
|
58
|
-
ON c.partition=s.partition AND c.commit_seq=s.commit_seq AND c.tbl=s.tbl
|
|
59
|
-
WHERE s.partition=?1 AND s.tbl=?2 AND s.var=?3 AND s.value=?4
|
|
60
|
-
AND s.commit_seq<=?5 AND c.op=1 AND json_extract(c.scopes, ?6)=s.value
|
|
61
|
-
LIMIT ?7) RETURNING 1 AS removed`, `DELETE FROM sync_change_scopes WHERE rowid IN (
|
|
62
|
-
SELECT s.rowid FROM sync_change_scopes s
|
|
63
|
-
WHERE s.partition=?1 AND s.tbl=?2 AND s.var=?3 AND s.value=?4
|
|
64
|
-
AND s.commit_seq<=?5 AND NOT EXISTS (
|
|
65
|
-
SELECT 1 FROM sync_changes c
|
|
66
|
-
WHERE c.partition=s.partition AND c.commit_seq=s.commit_seq
|
|
67
|
-
AND c.tbl=s.tbl AND json_extract(c.scopes, ?6)=s.value)
|
|
68
|
-
LIMIT ?7) RETURNING 1 AS removed`];
|
|
69
|
-
export declare function eraseScopeValueHistoryParams(partition: string, query: ScopeValueHistoryQuery): [string, string, string, string, number, string, number];
|
|
70
50
|
/** `?,?,…` for an `IN (…)` clause of `count` positional parameters. */
|
|
71
51
|
export declare function placeholders(count: number): string;
|
|
72
52
|
/** Serialize a push result to the JSON `TEXT` stored in `sync_push_results`. */
|
|
73
53
|
export declare function serializePushResult(result: StoredPushResult): string;
|
|
54
|
+
/** Parse a stored push result; an unreadable record is a §6.3 cache miss. */
|
|
74
55
|
export declare function deserializePushResult(text: string): StoredPushResult;
|
|
75
56
|
/** Row-record shape the SQLite `SELECT`s in both storages return. */
|
|
76
57
|
export interface SqliteRowRecord {
|
|
@@ -91,6 +72,12 @@ export interface SqliteChangeRecord {
|
|
|
91
72
|
/** Native SQLite returns `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
92
73
|
export declare function asUint8Array(value: unknown): Uint8Array;
|
|
93
74
|
export declare function toStoredRow(record: SqliteRowRecord): StoredRow;
|
|
75
|
+
export interface SqliteTombstoneRecord {
|
|
76
|
+
commit_seq: number;
|
|
77
|
+
server_version: number | null;
|
|
78
|
+
scopes: string | null;
|
|
79
|
+
}
|
|
80
|
+
export declare function toTombstone(record: SqliteTombstoneRecord): Tombstone;
|
|
94
81
|
export declare function toStoredChange(record: SqliteChangeRecord): StoredChange;
|
|
95
82
|
/**
|
|
96
83
|
* One result row of `commitWindowPageSql` (candidate LEFT JOIN commit meta
|
|
@@ -133,3 +120,119 @@ export declare function collectCommitWindowPage(records: readonly SqliteCommitWi
|
|
|
133
120
|
lastSeq: number;
|
|
134
121
|
candidateCount: number;
|
|
135
122
|
};
|
|
123
|
+
/** One statement of an atomic unit: its SQL text and bound parameters. */
|
|
124
|
+
export interface SqliteStatementSpec {
|
|
125
|
+
readonly sql: string;
|
|
126
|
+
readonly params: readonly SqliteValue[];
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The `ServerStorage` methods SQLite and D1 share. A subclass supplies the
|
|
130
|
+
* execution seam and implements the rest of the interface: schema migration,
|
|
131
|
+
* `begin`, registered queries, the serve gate and checkpoints.
|
|
132
|
+
*/
|
|
133
|
+
export declare abstract class SqliteFamilyStorage {
|
|
134
|
+
/** Set by `ensureSchema`: app-table lookup for the relational row store. */
|
|
135
|
+
protected tables: ReadonlyMap<string, CompiledTable> | undefined;
|
|
136
|
+
protected schemaVersion: number | undefined;
|
|
137
|
+
/** Bound parameters one statement accepts. */
|
|
138
|
+
protected abstract readonly maxBindParams: number;
|
|
139
|
+
/** Run one statement and return its rows. */
|
|
140
|
+
protected abstract all<T>(sql: string, params: readonly SqliteValue[]): Promise<T[]>;
|
|
141
|
+
/**
|
|
142
|
+
* Run one read of schema-dependent state (row tables, the commit window,
|
|
143
|
+
* push results). D1 runs it behind its published-schema guard and joins the
|
|
144
|
+
* reads of one microtask turn into one batch; SQLite runs it as `all`.
|
|
145
|
+
*/
|
|
146
|
+
protected abstract guardedAll<T>(sql: string, params: readonly SqliteValue[]): Promise<T[]>;
|
|
147
|
+
/** Run statements as one all-or-nothing unit; returns each one's rows. */
|
|
148
|
+
protected abstract atomic(statements: readonly SqliteStatementSpec[]): Promise<unknown[][]>;
|
|
149
|
+
/** Resolve a table's compiled schema; row operations require `ensureSchema`. */
|
|
150
|
+
table(name: string): CompiledTable;
|
|
151
|
+
protected prepareAuthoritativeQuery(partition: string, query: AuthoritativeQueryRequest): BoundAuthoritativeQuery;
|
|
152
|
+
touchPartition(partition: string, authenticatedAtMs: number, initialLogEpoch: string): Promise<PartitionRegistryEntry>;
|
|
153
|
+
rotatePartitionLogEpoch(partition: string, logEpoch: string, authenticatedAtMs: number): Promise<PartitionRegistryEntry>;
|
|
154
|
+
listPartitionRegistry(): Promise<PartitionRegistryEntry[]>;
|
|
155
|
+
getMaxCommitSeq(partition: string): Promise<number>;
|
|
156
|
+
getPartitionLogEpoch(partition: string): Promise<string | undefined>;
|
|
157
|
+
getHorizonSeq(partition: string): Promise<number>;
|
|
158
|
+
setHorizonSeq(partition: string, seq: number): Promise<void>;
|
|
159
|
+
pruneCommitsThrough(partition: string, query: CommitPruneQuery): Promise<CommitPruneResult>;
|
|
160
|
+
getCommitSeqBefore(partition: string, createdBeforeMs: number): Promise<number>;
|
|
161
|
+
sourceCoverageSeq(partition: string, tables: readonly string[]): Promise<number>;
|
|
162
|
+
hasSourceChangesAbove(partition: string, tables: readonly string[], seq: number): Promise<'clean' | 'changed' | 'unverifiable'>;
|
|
163
|
+
getRow(partition: string, table: string, rowId: string): Promise<StoredRow | undefined>;
|
|
164
|
+
getPushResult(partition: string, clientId: string, clientCommitId: string): Promise<StoredPushResult | undefined>;
|
|
165
|
+
claimReactions(partition: string, query: ReactionClaimQuery): Promise<StoredReaction[]>;
|
|
166
|
+
completeReaction(partition: string, idempotencyKey: string, leaseOwner: string, completedAtMs: number): Promise<boolean>;
|
|
167
|
+
extendReactionLease(partition: string, idempotencyKey: string, leaseOwner: string, leaseExpiresAtMs: number): Promise<boolean>;
|
|
168
|
+
failReaction(partition: string, idempotencyKey: string, update: ReactionFailureUpdate): Promise<boolean>;
|
|
169
|
+
retryReaction(partition: string, idempotencyKey: string, nowMs: number): Promise<boolean>;
|
|
170
|
+
getReaction(partition: string, idempotencyKey: string): Promise<StoredReaction | undefined>;
|
|
171
|
+
listReactions(partition: string, query: ReactionListQuery): Promise<StoredReaction[]>;
|
|
172
|
+
pruneReactions(partition: string, query: ReactionPruneQuery): Promise<PrunedReactionCounts>;
|
|
173
|
+
latestScopedChangeSeq(partition: string, query: {
|
|
174
|
+
readonly table: string;
|
|
175
|
+
readonly scopeFilter: ScopeMap;
|
|
176
|
+
}): Promise<number>;
|
|
177
|
+
readCommitWindow(partition: string, query: CommitWindowQuery): Promise<StoredCommit[]>;
|
|
178
|
+
scanRows(partition: string, query: RowScanQuery): Promise<StoredRow[]>;
|
|
179
|
+
scanRowsByIndex(partition: string, query: IndexRowScanQuery): Promise<StoredRow[]>;
|
|
180
|
+
getClientRecord(partition: string, clientId: string): Promise<ClientRecord | undefined>;
|
|
181
|
+
putClientRecord(partition: string, record: ClientRecord, options?: ClientRecordWriteOptions): Promise<void>;
|
|
182
|
+
recordClientRoundFailure(partition: string, clientId: string, actorId: string, errorCode: string, atMs: number): Promise<void>;
|
|
183
|
+
advanceClientCursor(partition: string, clientId: string, actorId: string, logEpoch: string, cursor: number, updatedAtMs: number): Promise<void>;
|
|
184
|
+
updateClientCursor(partition: string, clientId: string, cursor: number, updatedAtMs: number): Promise<void>;
|
|
185
|
+
getActiveClientCursorFloor(partition: string, cutoffMs: number): Promise<number | null>;
|
|
186
|
+
listClientCursors(partition: string): Promise<ClientCursorInfo[]>;
|
|
187
|
+
listRowsReferencingBlob(partition: string, blobId: string): Promise<{
|
|
188
|
+
readonly table: string;
|
|
189
|
+
readonly rowId: string;
|
|
190
|
+
readonly scopes: Record<string, string>;
|
|
191
|
+
}[]>;
|
|
192
|
+
listReferencedBlobIds(partition: string): Promise<string[]>;
|
|
193
|
+
listScopeValueRows(partition: string, query: ScopeValueQuery): Promise<string[]>;
|
|
194
|
+
eraseScopeValueHistory(partition: string, query: ScopeValueHistoryQuery): Promise<number>;
|
|
195
|
+
listClientRecords(partition: string): Promise<ClientRecord[]>;
|
|
196
|
+
listCommitMetadata(partition: string, query: CommitMetadataQuery): Promise<CommitMetadata[]>;
|
|
197
|
+
getCommit(partition: string, commitSeq: number): Promise<CommitDetail | undefined>;
|
|
198
|
+
listRowChanges(partition: string, query: RowChangesQuery): Promise<RowChangeRecord[]>;
|
|
199
|
+
listRows(partition: string, query: RowListQuery): Promise<StoredRow[]>;
|
|
200
|
+
countRows(partition: string, table: string): Promise<number>;
|
|
201
|
+
listPushResults(partition: string, clientCommitId: string): Promise<ClientPushResult[]>;
|
|
202
|
+
scopeActivity(partition: string, query: ScopeActivityQuery): Promise<ScopeCommitActivity[]>;
|
|
203
|
+
listPartitions(): Promise<string[]>;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The `StorageTransaction` writes SQLite and D1 share. `run` executes a write
|
|
207
|
+
* at call time on SQLite, inside its `BEGIN IMMEDIATE`; D1 buffers it for the
|
|
208
|
+
* one `db.batch` at `commit()`.
|
|
209
|
+
*/
|
|
210
|
+
export declare abstract class SqliteFamilyTransaction {
|
|
211
|
+
protected readonly partition: string;
|
|
212
|
+
protected open: boolean;
|
|
213
|
+
constructor(partition: string);
|
|
214
|
+
/** Execute (SQLite) or buffer (D1) one write of this transaction. */
|
|
215
|
+
protected abstract run(sql: string, params: readonly SqliteValue[]): void;
|
|
216
|
+
/** Read persisted state from inside this transaction. */
|
|
217
|
+
protected abstract all<T>(sql: string, params: readonly SqliteValue[]): Promise<T[]>;
|
|
218
|
+
protected abstract table(name: string): CompiledTable;
|
|
219
|
+
/** Allocate the next dense commitSeq (§2.1) and write its `sync_commits` row. */
|
|
220
|
+
protected abstract appendCommitRow(commit: NewCommit): Promise<number>;
|
|
221
|
+
abstract getRow(table: string, rowId: string): Promise<StoredRow | undefined>;
|
|
222
|
+
protected assertOpen(): void;
|
|
223
|
+
getRows(table: string, rowIds: readonly string[]): Promise<ReadonlyMap<string, StoredRow>>;
|
|
224
|
+
getTombstone(table: string, rowId: string): Promise<Tombstone | undefined>;
|
|
225
|
+
clearTombstone(table: string, rowId: string): Promise<void>;
|
|
226
|
+
/**
|
|
227
|
+
* Write a row and refresh its scope-index entries. The DELETE drops only
|
|
228
|
+
* the keys the new scope map no longer carries and runs before the upsert,
|
|
229
|
+
* so its subquery reads the pre-upsert scope map; keys present in both
|
|
230
|
+
* maps are left alone and the `INSERT OR IGNORE` writes nothing for them.
|
|
231
|
+
*/
|
|
232
|
+
protected writeRow(table: string, row: StoredRow): void;
|
|
233
|
+
deleteRow(table: string, rowId: string): Promise<void>;
|
|
234
|
+
setBlobRefs(table: string, rowId: string, blobIds: readonly string[]): Promise<void>;
|
|
235
|
+
appendCommit(commit: NewCommit): Promise<number>;
|
|
236
|
+
putPushResult(clientId: string, clientCommitId: string, result: StoredPushResult): Promise<void>;
|
|
237
|
+
enqueueReactions(reactions: readonly NewReaction[]): Promise<void>;
|
|
238
|
+
}
|