@syncular/server 0.15.45 → 0.15.47
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 +144 -7
- package/dist/admin.d.ts +10 -4
- package/dist/admin.js +10 -0
- package/dist/authoritative-query.d.ts +20 -0
- package/dist/authoritative-query.js +184 -0
- package/dist/context.d.ts +11 -1
- package/dist/context.js +2 -0
- package/dist/d1-storage.d.ts +10 -1
- package/dist/d1-storage.js +216 -0
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +43 -1
- package/dist/events.d.ts +52 -3
- package/dist/handler.js +4 -1
- package/dist/index-bun.d.ts +2 -0
- package/dist/index-bun.js +2 -0
- package/dist/index-node.d.ts +2 -0
- package/dist/index-node.js +2 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +7 -6
- package/dist/operations-realtime.d.ts +16 -0
- package/dist/operations-realtime.js +196 -0
- package/dist/operations.d.ts +97 -0
- package/dist/operations.js +392 -0
- package/dist/postgres-storage.d.ts +11 -2
- package/dist/postgres-storage.js +220 -0
- package/dist/pull.js +1 -1
- package/dist/push.d.ts +8 -2
- package/dist/push.js +75 -21
- package/dist/reactions.d.ts +167 -0
- package/dist/reactions.js +442 -0
- package/dist/realtime.js +4 -1
- package/dist/sqlite-blob-store.d.ts +4 -9
- package/dist/sqlite-blob-store.js +5 -10
- package/dist/sqlite-bun-driver.d.ts +11 -0
- package/dist/sqlite-bun-driver.js +27 -0
- package/dist/sqlite-bun.d.ts +24 -0
- package/dist/sqlite-bun.js +40 -0
- package/dist/sqlite-dialect.d.ts +8 -8
- package/dist/sqlite-dialect.js +22 -2
- package/dist/sqlite-driver.d.ts +26 -0
- package/dist/sqlite-driver.js +8 -0
- package/dist/sqlite-image.d.ts +7 -9
- package/dist/sqlite-image.js +26 -28
- package/dist/sqlite-lease-store.d.ts +4 -9
- package/dist/sqlite-lease-store.js +5 -10
- package/dist/sqlite-node-driver.d.ts +10 -0
- package/dist/sqlite-node-driver.js +30 -0
- package/dist/sqlite-node.d.ts +24 -0
- package/dist/sqlite-node.js +50 -0
- package/dist/sqlite-segment-store.d.ts +4 -10
- package/dist/sqlite-segment-store.js +6 -9
- package/dist/sqlite-storage.d.ts +13 -12
- package/dist/sqlite-storage.js +223 -5
- package/dist/storage-errors.js +4 -1
- package/dist/storage.d.ts +109 -0
- package/dist/validate.js +1 -0
- package/package.json +18 -3
- package/src/admin.ts +27 -3
- package/src/authoritative-query.ts +218 -0
- package/src/context.ts +12 -1
- package/src/d1-storage.ts +352 -0
- package/src/errors.ts +43 -1
- package/src/events.ts +64 -2
- package/src/handler.ts +13 -1
- package/src/index-bun.ts +9 -0
- package/src/index-node.ts +9 -0
- package/src/index.ts +40 -6
- package/src/operations-realtime.ts +272 -0
- package/src/operations.ts +720 -0
- package/src/postgres-storage.ts +351 -0
- package/src/pull.ts +1 -1
- package/src/push.ts +97 -29
- package/src/reactions.ts +741 -0
- package/src/realtime.ts +7 -1
- package/src/sqlite-blob-store.ts +11 -10
- package/src/sqlite-bun-driver.ts +42 -0
- package/src/sqlite-bun.ts +53 -0
- package/src/sqlite-dialect.ts +27 -7
- package/src/sqlite-driver.ts +44 -0
- package/src/sqlite-image.ts +44 -49
- package/src/sqlite-lease-store.ts +11 -10
- package/src/sqlite-node-driver.ts +46 -0
- package/src/sqlite-node.ts +62 -0
- package/src/sqlite-segment-store.ts +11 -11
- package/src/sqlite-storage.ts +378 -7
- package/src/storage-errors.ts +4 -1
- package/src/storage.ts +165 -0
- package/src/validate.ts +1 -0
package/src/realtime.ts
CHANGED
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
type WakeReason,
|
|
32
32
|
} from '@syncular/core';
|
|
33
33
|
import type { SyncRequestContext, SyncServerConfig } from './context';
|
|
34
|
-
import { RESOLVER_OUTAGE } from './context';
|
|
34
|
+
import { REMOTE_COMMAND_CLIENT_ID_PREFIX, RESOLVER_OUTAGE } from './context';
|
|
35
35
|
import { SyncError, syncError } from './errors';
|
|
36
36
|
import { emitEvent, type SyncularServerEvents } from './events';
|
|
37
37
|
import { createSyncResponseStream } from './handler';
|
|
@@ -963,6 +963,12 @@ export class RealtimeHub {
|
|
|
963
963
|
async connect(options: RealtimeConnectOptions): Promise<RealtimeSession> {
|
|
964
964
|
const { storage } = this.#config;
|
|
965
965
|
const clock = this.#config.clock ?? Date.now;
|
|
966
|
+
if (options.clientId.startsWith(REMOTE_COMMAND_CLIENT_ID_PREFIX)) {
|
|
967
|
+
throw syncError(
|
|
968
|
+
'sync.invalid_client_id',
|
|
969
|
+
'clientId uses a reserved server-command namespace (§1.5)',
|
|
970
|
+
);
|
|
971
|
+
}
|
|
966
972
|
const record = await storage.getClientRecord(
|
|
967
973
|
options.partition,
|
|
968
974
|
options.clientId,
|
package/src/sqlite-blob-store.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SQLite-backed blob store
|
|
3
|
-
* dependency-free). Bun-specific by design (top-level `bun:sqlite` import),
|
|
4
|
-
* so it lives in its own module — the runtime-neutral `BlobStore` interface,
|
|
5
|
-
* `MemoryBlobStore`, `blobIdFor`, and `isBlobId` stay in `blob-store.ts` for
|
|
6
|
-
* the Workers/edge core (runtime neutrality is enforced by
|
|
7
|
-
* `test/runtime-neutrality.test.ts`).
|
|
2
|
+
* SQLite-backed blob store over the shared synchronous driver.
|
|
8
3
|
*/
|
|
9
|
-
import { Database } from 'bun:sqlite';
|
|
10
4
|
import type { BlobRecord, BlobStore, BlobStoreStats } from './blob-store';
|
|
5
|
+
import {
|
|
6
|
+
SqliteAdapterRequiredError,
|
|
7
|
+
type SqliteDatabase,
|
|
8
|
+
} from './sqlite-driver';
|
|
11
9
|
|
|
12
10
|
export class SqliteBlobStore implements BlobStore {
|
|
13
|
-
readonly db:
|
|
11
|
+
readonly db: SqliteDatabase;
|
|
14
12
|
|
|
15
|
-
constructor(db:
|
|
16
|
-
|
|
13
|
+
constructor(db: SqliteDatabase | string = ':memory:') {
|
|
14
|
+
if (typeof db === 'string') {
|
|
15
|
+
throw new SqliteAdapterRequiredError();
|
|
16
|
+
}
|
|
17
|
+
this.db = db;
|
|
17
18
|
this.db.exec(`
|
|
18
19
|
CREATE TABLE IF NOT EXISTS sync_blobs(
|
|
19
20
|
partition TEXT NOT NULL, blob_id TEXT NOT NULL,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Database, type SQLQueryBindings } from 'bun:sqlite';
|
|
2
|
+
import type {
|
|
3
|
+
SqliteDatabase,
|
|
4
|
+
SqliteRunResult,
|
|
5
|
+
SqliteStatement,
|
|
6
|
+
SqliteValue,
|
|
7
|
+
} from './sqlite-driver';
|
|
8
|
+
|
|
9
|
+
export class BunSqliteDatabase implements SqliteDatabase {
|
|
10
|
+
readonly native: Database;
|
|
11
|
+
|
|
12
|
+
constructor(path = ':memory:') {
|
|
13
|
+
this.native = new Database(path);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exec(sql: string): void {
|
|
17
|
+
this.native.exec(sql);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
run(sql: string, bindings: readonly SqliteValue[] = []): SqliteRunResult {
|
|
21
|
+
return this.native.run(sql, [...bindings]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
query<Row, Params extends readonly SqliteValue[]>(
|
|
25
|
+
sql: string,
|
|
26
|
+
): SqliteStatement<Row, Params> {
|
|
27
|
+
const statement = this.native.query<Row, SQLQueryBindings[]>(sql);
|
|
28
|
+
return {
|
|
29
|
+
run: (...params) => statement.run(...params),
|
|
30
|
+
get: (...params) => statement.get(...params),
|
|
31
|
+
all: (...params) => statement.all(...params),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
serialize(): Uint8Array {
|
|
36
|
+
return new Uint8Array(this.native.serialize());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
close(): void {
|
|
40
|
+
this.native.close();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SqliteBlobStore as SharedSqliteBlobStore } from './sqlite-blob-store';
|
|
2
|
+
import { BunSqliteDatabase } from './sqlite-bun-driver';
|
|
3
|
+
import type { SqliteDatabase } from './sqlite-driver';
|
|
4
|
+
import { type SqliteImageBuilder, writeSqliteImage } from './sqlite-image';
|
|
5
|
+
import { SqliteLeaseStore as SharedSqliteLeaseStore } from './sqlite-lease-store';
|
|
6
|
+
import { SqliteSegmentStore as SharedSqliteSegmentStore } from './sqlite-segment-store';
|
|
7
|
+
import { SqliteServerStorage as SharedSqliteServerStorage } from './sqlite-storage';
|
|
8
|
+
|
|
9
|
+
function database(value: SqliteDatabase | string): SqliteDatabase {
|
|
10
|
+
return typeof value === 'string' ? new BunSqliteDatabase(value) : value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class SqliteServerStorage extends SharedSqliteServerStorage {
|
|
14
|
+
constructor(value: SqliteDatabase | string = ':memory:') {
|
|
15
|
+
super(database(value));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SqliteSegmentStore extends SharedSqliteSegmentStore {
|
|
20
|
+
constructor(
|
|
21
|
+
value: SqliteDatabase | string = ':memory:',
|
|
22
|
+
options?: { ttlMs?: number },
|
|
23
|
+
) {
|
|
24
|
+
super(database(value), options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class SqliteBlobStore extends SharedSqliteBlobStore {
|
|
29
|
+
constructor(value: SqliteDatabase | string = ':memory:') {
|
|
30
|
+
super(database(value));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class SqliteLeaseStore extends SharedSqliteLeaseStore {
|
|
35
|
+
constructor(
|
|
36
|
+
value: SqliteDatabase | string = ':memory:',
|
|
37
|
+
options?: { readonly leaseId?: () => string },
|
|
38
|
+
) {
|
|
39
|
+
super(database(value), options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const buildSqliteImage: SqliteImageBuilder = (input) => {
|
|
44
|
+
const db = new BunSqliteDatabase();
|
|
45
|
+
try {
|
|
46
|
+
writeSqliteImage(db, input);
|
|
47
|
+
return db.serialize();
|
|
48
|
+
} finally {
|
|
49
|
+
db.close();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { BunSqliteDatabase } from './sqlite-bun-driver';
|
package/src/sqlite-dialect.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared SQLite dialect for
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Shared SQLite dialect for synchronous `SqliteServerStorage` on Bun or Node
|
|
3
|
+
* and asynchronous `D1ServerStorage` on Cloudflare Workers. D1 is SQLite:
|
|
4
|
+
* same DDL, same statement grammar,
|
|
5
5
|
* same `?` positional placeholders, same `INSERT ... ON CONFLICT` / `INSERT
|
|
6
6
|
* OR IGNORE` upsert idioms — so the schema and the value (de)serialization are
|
|
7
7
|
* genuinely common ground and live here.
|
|
8
8
|
*
|
|
9
|
-
* What is
|
|
9
|
+
* What is not shared: statement execution. The server SQLite driver is sync
|
|
10
10
|
* (`db.query(sql).get(...)`) and D1 is async (`await
|
|
11
11
|
* db.prepare(sql).bind(...).all()`); a shared execution layer would have to
|
|
12
12
|
* pick one calling convention and adapt the other, which is uglier than two
|
|
@@ -25,7 +25,7 @@ import type {
|
|
|
25
25
|
} from './storage';
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Schema DDL
|
|
28
|
+
* Schema DDL, one statement per `;`-delimited chunk. Native SQLite applies
|
|
29
29
|
* the whole string via `db.exec(SQLITE_DDL)`; D1 applies each statement
|
|
30
30
|
* separately (its `prepare`/`batch` API is one statement per call). Types
|
|
31
31
|
* are SQLite's: `INTEGER`/`TEXT`/`BLOB`. Scopes are stored as JSON `TEXT`
|
|
@@ -74,6 +74,26 @@ CREATE TABLE IF NOT EXISTS sync_push_results(
|
|
|
74
74
|
client_commit_id TEXT NOT NULL, result TEXT NOT NULL,
|
|
75
75
|
PRIMARY KEY(partition, client_id, client_commit_id)
|
|
76
76
|
);
|
|
77
|
+
CREATE TABLE IF NOT EXISTS sync_reactions(
|
|
78
|
+
partition TEXT NOT NULL, idempotency_key TEXT NOT NULL,
|
|
79
|
+
type TEXT NOT NULL, version INTEGER NOT NULL, payload TEXT NOT NULL,
|
|
80
|
+
source_client_id TEXT NOT NULL, source_client_commit_id TEXT NOT NULL,
|
|
81
|
+
source_commit_seq INTEGER NOT NULL, created_at_ms INTEGER NOT NULL,
|
|
82
|
+
available_at_ms INTEGER NOT NULL, status TEXT NOT NULL,
|
|
83
|
+
attempts INTEGER NOT NULL, max_attempts INTEGER NOT NULL,
|
|
84
|
+
lease_owner TEXT, lease_expires_at_ms INTEGER, completed_at_ms INTEGER,
|
|
85
|
+
last_failure TEXT,
|
|
86
|
+
PRIMARY KEY(partition, idempotency_key),
|
|
87
|
+
CHECK(status IN ('pending', 'leased', 'completed', 'dead-letter'))
|
|
88
|
+
);
|
|
89
|
+
CREATE INDEX IF NOT EXISTS sync_reactions_due
|
|
90
|
+
ON sync_reactions(partition, status, available_at_ms, created_at_ms, idempotency_key);
|
|
91
|
+
CREATE INDEX IF NOT EXISTS sync_reactions_lease
|
|
92
|
+
ON sync_reactions(partition, status, lease_expires_at_ms);
|
|
93
|
+
CREATE INDEX IF NOT EXISTS sync_reactions_completed
|
|
94
|
+
ON sync_reactions(partition, status, completed_at_ms, idempotency_key);
|
|
95
|
+
CREATE INDEX IF NOT EXISTS sync_reactions_dead_letter
|
|
96
|
+
ON sync_reactions(partition, status, available_at_ms, idempotency_key);
|
|
77
97
|
CREATE TABLE IF NOT EXISTS sync_clients(
|
|
78
98
|
partition TEXT NOT NULL, client_id TEXT NOT NULL, actor_id TEXT NOT NULL,
|
|
79
99
|
cursor INTEGER NOT NULL, subscriptions TEXT NOT NULL,
|
|
@@ -226,7 +246,7 @@ export interface SqliteChangeRecord {
|
|
|
226
246
|
payload: Uint8Array | null;
|
|
227
247
|
}
|
|
228
248
|
|
|
229
|
-
/**
|
|
249
|
+
/** Native SQLite returns `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
230
250
|
export function asUint8Array(value: unknown): Uint8Array {
|
|
231
251
|
if (value instanceof Uint8Array) return value;
|
|
232
252
|
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
@@ -260,7 +280,7 @@ export function toStoredChange(record: SqliteChangeRecord): StoredChange {
|
|
|
260
280
|
* One result row of `commitWindowPageSql` (candidate LEFT JOIN commit meta
|
|
261
281
|
* LEFT JOIN changes): meta/change columns are NULL when the joined row
|
|
262
282
|
* vanished (see the builder's LEFT JOIN contract). `payload` is a BLOB —
|
|
263
|
-
*
|
|
283
|
+
* Native SQLite hands back `Uint8Array`, D1 `ArrayBuffer`; `toStoredChange`
|
|
264
284
|
* normalizes via `asUint8Array`.
|
|
265
285
|
*/
|
|
266
286
|
export interface SqliteCommitWindowRecord {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Values accepted by the synchronous SQLite adapters. */
|
|
2
|
+
export type SqliteValue =
|
|
3
|
+
| string
|
|
4
|
+
| number
|
|
5
|
+
| bigint
|
|
6
|
+
| boolean
|
|
7
|
+
| Uint8Array
|
|
8
|
+
| null;
|
|
9
|
+
|
|
10
|
+
/** Result of a SQLite statement that does not return rows. */
|
|
11
|
+
export interface SqliteRunResult {
|
|
12
|
+
readonly changes: number | bigint;
|
|
13
|
+
readonly lastInsertRowid: number | bigint;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Prepared synchronous SQLite statement used by the server stores. */
|
|
17
|
+
export interface SqliteStatement<Row, Params extends readonly SqliteValue[]> {
|
|
18
|
+
run(...params: Params): SqliteRunResult;
|
|
19
|
+
get(...params: Params): Row | null;
|
|
20
|
+
all(...params: Params): Row[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Runtime-neutral database surface shared by the Bun and Node adapters. */
|
|
24
|
+
export interface SqliteDatabase {
|
|
25
|
+
exec(sql: string): void;
|
|
26
|
+
run(sql: string, bindings?: readonly SqliteValue[]): SqliteRunResult;
|
|
27
|
+
query<
|
|
28
|
+
Row = Record<string, SqliteValue>,
|
|
29
|
+
Params extends readonly SqliteValue[] = SqliteValue[],
|
|
30
|
+
>(
|
|
31
|
+
sql: string,
|
|
32
|
+
): SqliteStatement<Row, Params>;
|
|
33
|
+
close(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Raised when a neutral-runtime import is used without a SQLite adapter. */
|
|
37
|
+
export class SqliteAdapterRequiredError extends Error {
|
|
38
|
+
override readonly name = 'SqliteAdapterRequiredError';
|
|
39
|
+
readonly code = 'sync.sqlite_adapter_required';
|
|
40
|
+
|
|
41
|
+
constructor() {
|
|
42
|
+
super('SQLite paths require the @syncular/server/sqlite runtime adapter');
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/sqlite-image.ts
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
* SQLite-image segment generation (SPEC.md §5.3): a complete SQLite
|
|
3
3
|
* database file carrying one table's whole effective-scope snapshot at
|
|
4
4
|
* the bootstrap pin, plus the single-row `_syncular_segment` metadata
|
|
5
|
-
* table.
|
|
5
|
+
* table. Runtime entries provide the concrete SQLite database.
|
|
6
6
|
*
|
|
7
7
|
* Images are NOT byte-deterministic (§5.3): the content address pins the
|
|
8
8
|
* served bytes, and cross-client dedup comes from the segment store's
|
|
9
9
|
* metadata lookup (`SegmentStore.find`), not from hash convergence.
|
|
10
10
|
*/
|
|
11
|
-
import { Database } from 'bun:sqlite';
|
|
12
11
|
import { decodeRow, type RowColumn, type RowValue } from '@syncular/core';
|
|
13
12
|
import type { CompiledTable } from './schema';
|
|
13
|
+
import type { SqliteDatabase } from './sqlite-driver';
|
|
14
14
|
import type { StoredRow } from './storage';
|
|
15
15
|
|
|
16
16
|
/** The §5.6 version column as it appears inside a sqlite image (§5.3). */
|
|
@@ -63,62 +63,57 @@ export interface SqliteImageInput {
|
|
|
63
63
|
/**
|
|
64
64
|
* The §5.3 image-builder capability, injected through
|
|
65
65
|
* `SyncServerConfig.sqliteImageBuilder`. Building an image needs
|
|
66
|
-
* a real SQLite engine
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* pull serves the rows lane (§5.3 clients advertise sqlite as an *accept*,
|
|
71
|
-
* never a requirement — the host chooses the served format from what it can
|
|
72
|
-
* produce; this is a support floor, not a fallback).
|
|
66
|
+
* a real SQLite engine, which is not available on every runtime. The core
|
|
67
|
+
* takes the builder as an optional capability rather than importing a driver
|
|
68
|
+
* on the pull path. A Bun or Node host passes `buildSqliteImage`; a Workers
|
|
69
|
+
* host omits it and serves the rows lane.
|
|
73
70
|
*/
|
|
74
71
|
export type SqliteImageBuilder = (input: SqliteImageInput) => Uint8Array;
|
|
75
72
|
|
|
76
|
-
/**
|
|
77
|
-
export
|
|
73
|
+
/** Populate a §5.3 image database for a whole-table snapshot. */
|
|
74
|
+
export function writeSqliteImage(
|
|
75
|
+
db: SqliteDatabase,
|
|
76
|
+
input: SqliteImageInput,
|
|
77
|
+
): void {
|
|
78
78
|
const { table, rows } = input;
|
|
79
79
|
const primaryKey = table.columns[table.primaryKeyIndex]?.name;
|
|
80
|
-
const
|
|
80
|
+
const columnDefs = table.columns.map((column) => {
|
|
81
|
+
const notNull = column.nullable ? '' : ' NOT NULL';
|
|
82
|
+
const pk = column.name === primaryKey ? ' PRIMARY KEY' : '';
|
|
83
|
+
return `${quoteIdent(column.name)} ${sqlType(column)}${notNull}${pk}`;
|
|
84
|
+
});
|
|
85
|
+
columnDefs.push(`${quoteIdent(IMAGE_VERSION_COLUMN)} INTEGER NOT NULL`);
|
|
86
|
+
db.exec(`CREATE TABLE ${quoteIdent(table.name)} (${columnDefs.join(', ')})`);
|
|
87
|
+
db.exec(
|
|
88
|
+
`CREATE TABLE ${IMAGE_METADATA_TABLE} (
|
|
89
|
+
format INTEGER NOT NULL, "table" TEXT NOT NULL,
|
|
90
|
+
"schemaVersion" INTEGER NOT NULL, "asOfCommitSeq" INTEGER NOT NULL,
|
|
91
|
+
"scopeDigest" TEXT NOT NULL, "rowCount" INTEGER NOT NULL)`,
|
|
92
|
+
);
|
|
93
|
+
db.query(`INSERT INTO ${IMAGE_METADATA_TABLE} VALUES (1, ?, ?, ?, ?, ?)`).run(
|
|
94
|
+
table.name,
|
|
95
|
+
input.schemaVersion,
|
|
96
|
+
input.asOfCommitSeq,
|
|
97
|
+
input.scopeDigest,
|
|
98
|
+
rows.length,
|
|
99
|
+
);
|
|
100
|
+
const names = [
|
|
101
|
+
...table.columns.map((column) => quoteIdent(column.name)),
|
|
102
|
+
quoteIdent(IMAGE_VERSION_COLUMN),
|
|
103
|
+
];
|
|
104
|
+
const insert = db.query(
|
|
105
|
+
`INSERT INTO ${quoteIdent(table.name)} (${names.join(', ')})
|
|
106
|
+
VALUES (${names.map(() => '?').join(', ')})`,
|
|
107
|
+
);
|
|
108
|
+
db.exec('BEGIN');
|
|
81
109
|
try {
|
|
82
|
-
const columnDefs = table.columns.map((column) => {
|
|
83
|
-
const notNull = column.nullable ? '' : ' NOT NULL';
|
|
84
|
-
const pk = column.name === primaryKey ? ' PRIMARY KEY' : '';
|
|
85
|
-
return `${quoteIdent(column.name)} ${sqlType(column)}${notNull}${pk}`;
|
|
86
|
-
});
|
|
87
|
-
columnDefs.push(`${quoteIdent(IMAGE_VERSION_COLUMN)} INTEGER NOT NULL`);
|
|
88
|
-
db.exec(
|
|
89
|
-
`CREATE TABLE ${quoteIdent(table.name)} (${columnDefs.join(', ')})`,
|
|
90
|
-
);
|
|
91
|
-
db.exec(
|
|
92
|
-
`CREATE TABLE ${IMAGE_METADATA_TABLE} (
|
|
93
|
-
format INTEGER NOT NULL, "table" TEXT NOT NULL,
|
|
94
|
-
"schemaVersion" INTEGER NOT NULL, "asOfCommitSeq" INTEGER NOT NULL,
|
|
95
|
-
"scopeDigest" TEXT NOT NULL, "rowCount" INTEGER NOT NULL)`,
|
|
96
|
-
);
|
|
97
|
-
db.query(
|
|
98
|
-
`INSERT INTO ${IMAGE_METADATA_TABLE} VALUES (1, ?, ?, ?, ?, ?)`,
|
|
99
|
-
).run(
|
|
100
|
-
table.name,
|
|
101
|
-
input.schemaVersion,
|
|
102
|
-
input.asOfCommitSeq,
|
|
103
|
-
input.scopeDigest,
|
|
104
|
-
rows.length,
|
|
105
|
-
);
|
|
106
|
-
const names = [
|
|
107
|
-
...table.columns.map((column) => quoteIdent(column.name)),
|
|
108
|
-
quoteIdent(IMAGE_VERSION_COLUMN),
|
|
109
|
-
];
|
|
110
|
-
const insert = db.query(
|
|
111
|
-
`INSERT INTO ${quoteIdent(table.name)} (${names.join(', ')})
|
|
112
|
-
VALUES (${names.map(() => '?').join(', ')})`,
|
|
113
|
-
);
|
|
114
|
-
db.exec('BEGIN');
|
|
115
110
|
for (const row of rows) {
|
|
116
111
|
const values = decodeRow(table.columns, row.payload);
|
|
117
112
|
insert.run(...values.map(toSql), row.serverVersion);
|
|
118
113
|
}
|
|
119
114
|
db.exec('COMMIT');
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
115
|
+
} catch (error) {
|
|
116
|
+
db.exec('ROLLBACK');
|
|
117
|
+
throw error;
|
|
123
118
|
}
|
|
124
|
-
}
|
|
119
|
+
}
|
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SQLite-backed auth-lease store
|
|
3
|
-
* dependency-free). Bun-specific by design (top-level `bun:sqlite` import),
|
|
4
|
-
* so it lives in its own module — the runtime-neutral `LeaseStore` interface,
|
|
5
|
-
* `LeaseRecord`, and `MemoryLeaseStore` stay in `lease-store.ts` for the
|
|
6
|
-
* Workers/edge core (runtime neutrality is enforced by
|
|
7
|
-
* `test/runtime-neutrality.test.ts`).
|
|
2
|
+
* SQLite-backed auth-lease store over the shared synchronous driver.
|
|
8
3
|
*/
|
|
9
|
-
import { Database } from 'bun:sqlite';
|
|
10
4
|
import type { ScopeMap } from '@syncular/core';
|
|
11
5
|
import type { LeaseIdFactory, LeaseRecord, LeaseStore } from './lease-store';
|
|
6
|
+
import {
|
|
7
|
+
SqliteAdapterRequiredError,
|
|
8
|
+
type SqliteDatabase,
|
|
9
|
+
} from './sqlite-driver';
|
|
12
10
|
|
|
13
11
|
function defaultLeaseId(): string {
|
|
14
12
|
return `lease_${crypto.randomUUID()}`;
|
|
15
13
|
}
|
|
16
14
|
|
|
17
15
|
export class SqliteLeaseStore implements LeaseStore {
|
|
18
|
-
readonly db:
|
|
16
|
+
readonly db: SqliteDatabase;
|
|
19
17
|
readonly #newId: LeaseIdFactory;
|
|
20
18
|
|
|
21
19
|
constructor(
|
|
22
|
-
db:
|
|
20
|
+
db: SqliteDatabase | string = ':memory:',
|
|
23
21
|
options?: { readonly leaseId?: LeaseIdFactory },
|
|
24
22
|
) {
|
|
25
|
-
|
|
23
|
+
if (typeof db === 'string') {
|
|
24
|
+
throw new SqliteAdapterRequiredError();
|
|
25
|
+
}
|
|
26
|
+
this.db = db;
|
|
26
27
|
this.#newId = options?.leaseId ?? defaultLeaseId;
|
|
27
28
|
this.db.exec(`
|
|
28
29
|
CREATE TABLE IF NOT EXISTS sync_leases(
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DatabaseSync, type SQLInputValue } from 'node:sqlite';
|
|
2
|
+
import type {
|
|
3
|
+
SqliteDatabase,
|
|
4
|
+
SqliteRunResult,
|
|
5
|
+
SqliteStatement,
|
|
6
|
+
SqliteValue,
|
|
7
|
+
} from './sqlite-driver';
|
|
8
|
+
|
|
9
|
+
function bind(value: SqliteValue): SQLInputValue {
|
|
10
|
+
return typeof value === 'boolean' ? (value ? 1 : 0) : value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function row<Row>(value: Record<string, unknown> | undefined): Row | null {
|
|
14
|
+
return (value ?? null) as Row | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class NodeSqliteDatabase implements SqliteDatabase {
|
|
18
|
+
readonly native: DatabaseSync;
|
|
19
|
+
|
|
20
|
+
constructor(path = ':memory:') {
|
|
21
|
+
this.native = new DatabaseSync(path);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exec(sql: string): void {
|
|
25
|
+
this.native.exec(sql);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
run(sql: string, bindings: readonly SqliteValue[] = []): SqliteRunResult {
|
|
29
|
+
return this.native.prepare(sql).run(...bindings.map(bind));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
query<Row, Params extends readonly SqliteValue[]>(
|
|
33
|
+
sql: string,
|
|
34
|
+
): SqliteStatement<Row, Params> {
|
|
35
|
+
const statement = this.native.prepare(sql);
|
|
36
|
+
return {
|
|
37
|
+
run: (...params) => statement.run(...params.map(bind)),
|
|
38
|
+
get: (...params) => row<Row>(statement.get(...params.map(bind))),
|
|
39
|
+
all: (...params) => statement.all(...params.map(bind)) as Row[],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
close(): void {
|
|
44
|
+
this.native.close();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { SqliteBlobStore as SharedSqliteBlobStore } from './sqlite-blob-store';
|
|
5
|
+
import type { SqliteDatabase } from './sqlite-driver';
|
|
6
|
+
import { type SqliteImageBuilder, writeSqliteImage } from './sqlite-image';
|
|
7
|
+
import { SqliteLeaseStore as SharedSqliteLeaseStore } from './sqlite-lease-store';
|
|
8
|
+
import { NodeSqliteDatabase } from './sqlite-node-driver';
|
|
9
|
+
import { SqliteSegmentStore as SharedSqliteSegmentStore } from './sqlite-segment-store';
|
|
10
|
+
import { SqliteServerStorage as SharedSqliteServerStorage } from './sqlite-storage';
|
|
11
|
+
|
|
12
|
+
function database(value: SqliteDatabase | string): SqliteDatabase {
|
|
13
|
+
return typeof value === 'string' ? new NodeSqliteDatabase(value) : value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class SqliteServerStorage extends SharedSqliteServerStorage {
|
|
17
|
+
constructor(value: SqliteDatabase | string = ':memory:') {
|
|
18
|
+
super(database(value));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class SqliteSegmentStore extends SharedSqliteSegmentStore {
|
|
23
|
+
constructor(
|
|
24
|
+
value: SqliteDatabase | string = ':memory:',
|
|
25
|
+
options?: { ttlMs?: number },
|
|
26
|
+
) {
|
|
27
|
+
super(database(value), options);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class SqliteBlobStore extends SharedSqliteBlobStore {
|
|
32
|
+
constructor(value: SqliteDatabase | string = ':memory:') {
|
|
33
|
+
super(database(value));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class SqliteLeaseStore extends SharedSqliteLeaseStore {
|
|
38
|
+
constructor(
|
|
39
|
+
value: SqliteDatabase | string = ':memory:',
|
|
40
|
+
options?: { readonly leaseId?: () => string },
|
|
41
|
+
) {
|
|
42
|
+
super(database(value), options);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const buildSqliteImage: SqliteImageBuilder = (input) => {
|
|
47
|
+
const directory = mkdtempSync(join(tmpdir(), 'syncular-server-image-'));
|
|
48
|
+
const path = join(directory, 'segment.db');
|
|
49
|
+
const db = new NodeSqliteDatabase(path);
|
|
50
|
+
try {
|
|
51
|
+
try {
|
|
52
|
+
writeSqliteImage(db, input);
|
|
53
|
+
} finally {
|
|
54
|
+
db.close();
|
|
55
|
+
}
|
|
56
|
+
return new Uint8Array(readFileSync(path));
|
|
57
|
+
} finally {
|
|
58
|
+
rmSync(directory, { recursive: true, force: true });
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { NodeSqliteDatabase } from './sqlite-node-driver';
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SQLite-backed segment store
|
|
3
|
-
* dependency-free). Bun-specific by design: it imports `bun:sqlite` at the
|
|
4
|
-
* top level, so it lives in its own module — importing it opts into the Bun
|
|
5
|
-
* runtime. The runtime-neutral `SegmentStore` interface, `MemorySegmentStore`,
|
|
6
|
-
* and `segmentIdFor` stay in `segment-store.ts` so the Workers/edge core can
|
|
7
|
-
* import them without pulling in `bun:sqlite` (runtime neutrality is enforced
|
|
8
|
-
* by `test/runtime-neutrality.test.ts`).
|
|
2
|
+
* SQLite-backed segment store over the shared synchronous driver.
|
|
9
3
|
*/
|
|
10
|
-
import { Database } from 'bun:sqlite';
|
|
11
4
|
import {
|
|
12
5
|
DEFAULT_SEGMENT_TTL_MS,
|
|
13
6
|
type SegmentFindKey,
|
|
@@ -17,16 +10,23 @@ import {
|
|
|
17
10
|
type SegmentStoreStats,
|
|
18
11
|
segmentIdFor,
|
|
19
12
|
} from './segment-store';
|
|
13
|
+
import {
|
|
14
|
+
SqliteAdapterRequiredError,
|
|
15
|
+
type SqliteDatabase,
|
|
16
|
+
} from './sqlite-driver';
|
|
20
17
|
|
|
21
18
|
export class SqliteSegmentStore implements SegmentStore {
|
|
22
|
-
readonly db:
|
|
19
|
+
readonly db: SqliteDatabase;
|
|
23
20
|
#ttlMs: number;
|
|
24
21
|
|
|
25
22
|
constructor(
|
|
26
|
-
db:
|
|
23
|
+
db: SqliteDatabase | string = ':memory:',
|
|
27
24
|
options?: { ttlMs?: number },
|
|
28
25
|
) {
|
|
29
|
-
|
|
26
|
+
if (typeof db === 'string') {
|
|
27
|
+
throw new SqliteAdapterRequiredError();
|
|
28
|
+
}
|
|
29
|
+
this.db = db;
|
|
30
30
|
this.#ttlMs = options?.ttlMs ?? DEFAULT_SEGMENT_TTL_MS;
|
|
31
31
|
this.db.exec(`
|
|
32
32
|
CREATE TABLE IF NOT EXISTS sync_segments(
|