@syncular/server 0.83.0 → 0.85.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/dist/d1-storage.js +2 -2
- package/dist/postgres-storage.js +23 -195
- package/dist/push.js +5 -22
- package/dist/schema.d.ts +5 -3
- package/dist/schema.js +16 -7
- package/dist/scopes.d.ts +7 -5
- package/dist/scopes.js +21 -22
- package/dist/sqlite-dialect.d.ts +1 -6
- package/dist/sqlite-dialect.js +3 -186
- package/dist/sqlite-storage.js +8 -2
- package/dist/storage.d.ts +71 -0
- package/dist/storage.js +205 -0
- package/package.json +2 -2
- package/src/d1-storage.ts +6 -4
- package/src/postgres-storage.ts +48 -308
- package/src/push.ts +5 -33
- package/src/schema.ts +32 -10
- package/src/scopes.ts +30 -19
- package/src/sqlite-dialect.ts +18 -257
- package/src/sqlite-storage.ts +8 -6
- package/src/storage.ts +292 -0
package/dist/sqlite-dialect.js
CHANGED
|
@@ -4,7 +4,7 @@ import { validateCommitPruneQuery } from './prune.js';
|
|
|
4
4
|
import { StorageQueryError } from './storage-errors.js';
|
|
5
5
|
import { deleteRowSql, deleteSqliteRowScopesSql, indexRowPageStatement, quoteIdent, scanProbeSql, scanRowPageSql, rowsReferencingBlobSql, selectRowSql, SYNC_COLUMN_VERSIONS_COLUMN, SYNC_PARTITION_COLUMN, SYNC_PAYLOAD_COLUMN, SYNC_ROW_ID_COLUMN, SYNC_SCOPES_COLUMN, SYNC_VERSION_COLUMN, upsertSql, upsertValues, } from './relational-rows.js';
|
|
6
6
|
import { matchesEffective } from './scopes.js';
|
|
7
|
-
import {
|
|
7
|
+
import { CLIENT_RECORD_COLUMNS, PARTITION_TOUCH_INTERVAL_MS, pushResultFromJson, pushResultToJson, rowsLiveAfterDelete, toClientRecord, toStoredChange, toStoredReaction, toStoredRow, toTombstone, } from './storage.js';
|
|
8
8
|
import { chooseScanVariable, readLiveWindowPages, resolveIndexRowScan, } from './storage-query.js';
|
|
9
9
|
/**
|
|
10
10
|
* Schema DDL, one statement per `;`-delimited chunk. Native SQLite applies
|
|
@@ -233,202 +233,19 @@ export function placeholders(count) {
|
|
|
233
233
|
return new Array(count).fill('?').join(',');
|
|
234
234
|
}
|
|
235
235
|
// -- value (de)serialization, shared by both SQLite storages ----------------
|
|
236
|
-
/** Runtime-neutral base64 (no `Buffer` — Workers-safe) for push results. */
|
|
237
|
-
function bytesToBase64(bytes) {
|
|
238
|
-
let binary = '';
|
|
239
|
-
for (const byte of bytes)
|
|
240
|
-
binary += String.fromCharCode(byte);
|
|
241
|
-
return btoa(binary);
|
|
242
|
-
}
|
|
243
|
-
function base64ToBytes(text) {
|
|
244
|
-
const binary = atob(text);
|
|
245
|
-
const bytes = new Uint8Array(binary.length);
|
|
246
|
-
for (let i = 0; i < binary.length; i++)
|
|
247
|
-
bytes[i] = binary.charCodeAt(i);
|
|
248
|
-
return bytes;
|
|
249
|
-
}
|
|
250
236
|
/** Serialize a push result to the JSON `TEXT` stored in `sync_push_results`. */
|
|
251
237
|
export function serializePushResult(result) {
|
|
252
|
-
return JSON.stringify(
|
|
253
|
-
status: result.status,
|
|
254
|
-
...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
|
|
255
|
-
...(result.recordedAtMs !== undefined
|
|
256
|
-
? { recordedAtMs: result.recordedAtMs }
|
|
257
|
-
: {}),
|
|
258
|
-
...(result.cacheIdentity !== undefined
|
|
259
|
-
? { cacheIdentity: result.cacheIdentity }
|
|
260
|
-
: {}),
|
|
261
|
-
...(result.conflictAuthorization !== undefined
|
|
262
|
-
? { conflictAuthorization: result.conflictAuthorization }
|
|
263
|
-
: {}),
|
|
264
|
-
results: result.results.map((record) => {
|
|
265
|
-
if (record.status === 'conflict') {
|
|
266
|
-
return {
|
|
267
|
-
opIndex: record.opIndex,
|
|
268
|
-
status: record.status,
|
|
269
|
-
code: record.code,
|
|
270
|
-
message: record.message,
|
|
271
|
-
serverVersion: record.serverVersion,
|
|
272
|
-
serverRow: bytesToBase64(record.serverRow),
|
|
273
|
-
conflictColumns: bytesToBase64(record.conflictColumns),
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
if (record.status === 'error') {
|
|
277
|
-
return {
|
|
278
|
-
opIndex: record.opIndex,
|
|
279
|
-
status: record.status,
|
|
280
|
-
code: record.code,
|
|
281
|
-
message: record.message,
|
|
282
|
-
retryable: record.retryable,
|
|
283
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
return { opIndex: record.opIndex, status: record.status };
|
|
287
|
-
}),
|
|
288
|
-
});
|
|
238
|
+
return JSON.stringify(pushResultToJson(result));
|
|
289
239
|
}
|
|
290
240
|
/** Parse a stored push result; an unreadable record is a §6.3 cache miss. */
|
|
291
241
|
export function deserializePushResult(text) {
|
|
292
242
|
try {
|
|
293
|
-
|
|
294
|
-
const results = parsed.results.map((record) => {
|
|
295
|
-
if (record.status === 'conflict') {
|
|
296
|
-
return {
|
|
297
|
-
opIndex: record.opIndex,
|
|
298
|
-
status: 'conflict',
|
|
299
|
-
code: record.code ?? '',
|
|
300
|
-
message: record.message ?? '',
|
|
301
|
-
serverVersion: record.serverVersion ?? 0,
|
|
302
|
-
serverRow: base64ToBytes(record.serverRow ?? ''),
|
|
303
|
-
conflictColumns: base64ToBytes(record.conflictColumns ?? ''),
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
if (record.status === 'error') {
|
|
307
|
-
return {
|
|
308
|
-
opIndex: record.opIndex,
|
|
309
|
-
status: 'error',
|
|
310
|
-
code: record.code ?? '',
|
|
311
|
-
message: record.message ?? '',
|
|
312
|
-
retryable: record.retryable ?? false,
|
|
313
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
return { opIndex: record.opIndex, status: 'applied' };
|
|
317
|
-
});
|
|
318
|
-
return {
|
|
319
|
-
status: parsed.status,
|
|
320
|
-
...(parsed.commitSeq !== undefined
|
|
321
|
-
? { commitSeq: parsed.commitSeq }
|
|
322
|
-
: {}),
|
|
323
|
-
...(parsed.recordedAtMs !== undefined
|
|
324
|
-
? { recordedAtMs: parsed.recordedAtMs }
|
|
325
|
-
: {}),
|
|
326
|
-
...(parsed.cacheIdentity !== undefined
|
|
327
|
-
? { cacheIdentity: parsed.cacheIdentity }
|
|
328
|
-
: {}),
|
|
329
|
-
...(parsed.conflictAuthorization !== undefined
|
|
330
|
-
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
331
|
-
: {}),
|
|
332
|
-
results,
|
|
333
|
-
};
|
|
243
|
+
return pushResultFromJson(JSON.parse(text));
|
|
334
244
|
}
|
|
335
245
|
catch {
|
|
336
246
|
throw syncError('sync.idempotency_cache_miss', 'persisted push result unreadable (§6.3)');
|
|
337
247
|
}
|
|
338
248
|
}
|
|
339
|
-
/** Native SQLite returns `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
340
|
-
export function asUint8Array(value) {
|
|
341
|
-
if (value instanceof Uint8Array)
|
|
342
|
-
return value;
|
|
343
|
-
if (value instanceof ArrayBuffer)
|
|
344
|
-
return new Uint8Array(value);
|
|
345
|
-
if (Array.isArray(value))
|
|
346
|
-
return new Uint8Array(value);
|
|
347
|
-
throw new Error(`expected a BLOB column, got ${typeof value}`);
|
|
348
|
-
}
|
|
349
|
-
export function toStoredRow(record) {
|
|
350
|
-
return {
|
|
351
|
-
rowId: record.row_id,
|
|
352
|
-
serverVersion: record.server_version,
|
|
353
|
-
scopes: JSON.parse(record.scopes),
|
|
354
|
-
payload: asUint8Array(record.payload),
|
|
355
|
-
...(record.column_versions !== null
|
|
356
|
-
? { columnVersions: asUint8Array(record.column_versions) }
|
|
357
|
-
: {}),
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
export function toTombstone(record) {
|
|
361
|
-
return {
|
|
362
|
-
commitSeq: record.commit_seq,
|
|
363
|
-
...(record.server_version !== null
|
|
364
|
-
? { serverVersion: record.server_version }
|
|
365
|
-
: {}),
|
|
366
|
-
...(record.scopes !== null
|
|
367
|
-
? { scopes: JSON.parse(record.scopes) }
|
|
368
|
-
: {}),
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
export function toStoredChange(record) {
|
|
372
|
-
return {
|
|
373
|
-
table: record.tbl,
|
|
374
|
-
rowId: record.row_id,
|
|
375
|
-
op: record.op === 1 ? 'upsert' : 'delete',
|
|
376
|
-
...(record.row_version !== null ? { rowVersion: record.row_version } : {}),
|
|
377
|
-
scopes: JSON.parse(record.scopes),
|
|
378
|
-
...(record.payload !== null
|
|
379
|
-
? { payload: asUint8Array(record.payload) }
|
|
380
|
-
: {}),
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
function toStoredReaction(record) {
|
|
384
|
-
return {
|
|
385
|
-
idempotencyKey: record.idempotency_key,
|
|
386
|
-
type: record.type,
|
|
387
|
-
version: record.version,
|
|
388
|
-
payload: JSON.parse(record.payload),
|
|
389
|
-
sourceClientId: record.source_client_id,
|
|
390
|
-
sourceClientCommitId: record.source_client_commit_id,
|
|
391
|
-
sourceCommitSeq: record.source_commit_seq,
|
|
392
|
-
createdAtMs: record.created_at_ms,
|
|
393
|
-
maxAttempts: record.max_attempts,
|
|
394
|
-
status: record.status,
|
|
395
|
-
attempts: record.attempts,
|
|
396
|
-
availableAtMs: record.available_at_ms,
|
|
397
|
-
...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
|
|
398
|
-
...(record.lease_expires_at_ms !== null
|
|
399
|
-
? { leaseExpiresAtMs: record.lease_expires_at_ms }
|
|
400
|
-
: {}),
|
|
401
|
-
...(record.completed_at_ms !== null
|
|
402
|
-
? { completedAtMs: record.completed_at_ms }
|
|
403
|
-
: {}),
|
|
404
|
-
...(record.last_failure !== null
|
|
405
|
-
? { lastFailure: JSON.parse(record.last_failure) }
|
|
406
|
-
: {}),
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
const CLIENT_RECORD_COLUMNS = 'client_id, actor_id, wire_version, cursor, updated_at_ms, schema_version, units, registration_digest, catching_up, last_round_at_ms, last_error_code, consecutive_failures';
|
|
410
|
-
function toClientRecord(record) {
|
|
411
|
-
return {
|
|
412
|
-
clientId: record.client_id,
|
|
413
|
-
actorId: record.actor_id,
|
|
414
|
-
wireVersion: record.wire_version,
|
|
415
|
-
cursor: record.cursor,
|
|
416
|
-
updatedAtMs: record.updated_at_ms,
|
|
417
|
-
...(record.schema_version !== null
|
|
418
|
-
? { schemaVersion: record.schema_version }
|
|
419
|
-
: {}),
|
|
420
|
-
...(record.units !== null && record.registration_digest !== null
|
|
421
|
-
? {
|
|
422
|
-
registration: {
|
|
423
|
-
units: JSON.parse(record.units),
|
|
424
|
-
digest: asUint8Array(record.registration_digest),
|
|
425
|
-
},
|
|
426
|
-
}
|
|
427
|
-
: {}),
|
|
428
|
-
catchingUp: JSON.parse(record.catching_up),
|
|
429
|
-
...clientRoundState(record.last_round_at_ms, record.last_error_code, record.consecutive_failures),
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
249
|
function toPartitionRegistryEntry(row) {
|
|
433
250
|
return {
|
|
434
251
|
partition: row.partition,
|
package/dist/sqlite-storage.js
CHANGED
|
@@ -328,7 +328,10 @@ END`);
|
|
|
328
328
|
.query('SELECT schema_version, layouts FROM sync_schema_meta WHERE id=1')
|
|
329
329
|
.get();
|
|
330
330
|
if (marker !== null && marker.schema_version > schema.version) {
|
|
331
|
-
throw new
|
|
331
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
332
|
+
storedVersion: String(marker.schema_version),
|
|
333
|
+
configuredVersion: String(schema.version),
|
|
334
|
+
});
|
|
332
335
|
}
|
|
333
336
|
if (marker !== null && marker.schema_version === schema.version) {
|
|
334
337
|
assertStoredLayouts(schema, marker.layouts);
|
|
@@ -407,7 +410,10 @@ END`);
|
|
|
407
410
|
.query('SELECT schema_version, layouts FROM sync_schema_meta WHERE id=1')
|
|
408
411
|
.get();
|
|
409
412
|
if (locked !== null && locked.schema_version > schema.version) {
|
|
410
|
-
throw new
|
|
413
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
414
|
+
storedVersion: String(locked.schema_version),
|
|
415
|
+
configuredVersion: String(schema.version),
|
|
416
|
+
});
|
|
411
417
|
}
|
|
412
418
|
if (locked !== null && locked.schema_version === schema.version) {
|
|
413
419
|
this.db.exec('COMMIT');
|
package/dist/storage.d.ts
CHANGED
|
@@ -369,6 +369,77 @@ export interface ClientRecordWriteOptions {
|
|
|
369
369
|
}
|
|
370
370
|
/** The round-state fields of a stored `sync_clients` row. */
|
|
371
371
|
export declare function clientRoundState(lastRoundAtMs: number | null, lastErrorCode: string | null, consecutiveFailures: number): Pick<ClientRecord, 'lastRoundAtMs' | 'lastErrorCode' | 'consecutiveFailures'>;
|
|
372
|
+
/**
|
|
373
|
+
* Some drivers return a JSON column already parsed (pglite, node-postgres);
|
|
374
|
+
* SQLite returns the `TEXT`.
|
|
375
|
+
*/
|
|
376
|
+
export declare function asJson<T>(value: unknown): T;
|
|
377
|
+
/** Native SQLite and Postgres return `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
378
|
+
export declare function asUint8Array(value: unknown): Uint8Array;
|
|
379
|
+
/**
|
|
380
|
+
* Convert a push result to the JSON-able object both SQL backends store.
|
|
381
|
+
* `serverRow` bytes are base64-encoded: JSON cannot hold raw bytes.
|
|
382
|
+
*/
|
|
383
|
+
export declare function pushResultToJson(result: StoredPushResult): unknown;
|
|
384
|
+
export declare function pushResultFromJson(value: unknown): StoredPushResult;
|
|
385
|
+
/** Row shapes the SQL backends' `SELECT`s return; values are driver-typed. */
|
|
386
|
+
export interface StoredRowRecord {
|
|
387
|
+
row_id: string;
|
|
388
|
+
server_version: unknown;
|
|
389
|
+
scopes: unknown;
|
|
390
|
+
payload: unknown;
|
|
391
|
+
column_versions: unknown;
|
|
392
|
+
}
|
|
393
|
+
export interface StoredChangeRecord {
|
|
394
|
+
tbl: string;
|
|
395
|
+
row_id: string;
|
|
396
|
+
op: unknown;
|
|
397
|
+
row_version: unknown;
|
|
398
|
+
scopes: unknown;
|
|
399
|
+
payload: unknown;
|
|
400
|
+
}
|
|
401
|
+
export interface StoredReactionRecord {
|
|
402
|
+
idempotency_key: string;
|
|
403
|
+
type: string;
|
|
404
|
+
version: unknown;
|
|
405
|
+
payload: unknown;
|
|
406
|
+
source_client_id: string;
|
|
407
|
+
source_client_commit_id: string;
|
|
408
|
+
source_commit_seq: unknown;
|
|
409
|
+
created_at_ms: unknown;
|
|
410
|
+
available_at_ms: unknown;
|
|
411
|
+
status: StoredReaction['status'];
|
|
412
|
+
attempts: unknown;
|
|
413
|
+
max_attempts: unknown;
|
|
414
|
+
lease_owner: string | null;
|
|
415
|
+
lease_expires_at_ms: unknown | null;
|
|
416
|
+
completed_at_ms: unknown | null;
|
|
417
|
+
last_failure: unknown | null;
|
|
418
|
+
}
|
|
419
|
+
export interface ClientRowRecord {
|
|
420
|
+
client_id: string;
|
|
421
|
+
actor_id: string;
|
|
422
|
+
wire_version: unknown;
|
|
423
|
+
cursor: unknown;
|
|
424
|
+
updated_at_ms: unknown;
|
|
425
|
+
schema_version: unknown;
|
|
426
|
+
units: unknown;
|
|
427
|
+
registration_digest: unknown;
|
|
428
|
+
catching_up: unknown;
|
|
429
|
+
last_round_at_ms: unknown;
|
|
430
|
+
last_error_code: string | null;
|
|
431
|
+
consecutive_failures: unknown;
|
|
432
|
+
}
|
|
433
|
+
export declare function toStoredRow(record: StoredRowRecord): StoredRow;
|
|
434
|
+
export declare function toTombstone(record: {
|
|
435
|
+
commit_seq: unknown;
|
|
436
|
+
server_version: unknown;
|
|
437
|
+
scopes: unknown;
|
|
438
|
+
}): Tombstone;
|
|
439
|
+
export declare function toStoredChange(record: StoredChangeRecord): StoredChange;
|
|
440
|
+
export declare function toStoredReaction(record: StoredReactionRecord): StoredReaction;
|
|
441
|
+
export declare const CLIENT_RECORD_COLUMNS = "client_id, actor_id, wire_version, cursor, updated_at_ms, schema_version, units, registration_digest, catching_up, last_round_at_ms, last_error_code, consecutive_failures";
|
|
442
|
+
export declare function toClientRecord(record: ClientRowRecord): ClientRecord;
|
|
372
443
|
/** One unit of the §4.5 scan set. */
|
|
373
444
|
export interface LiveScanUnit {
|
|
374
445
|
/** The unit's scope map; its first variable (keys sorted) is its index key. */
|
package/dist/storage.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { asNumber } from './pg-executor.js';
|
|
1
2
|
/**
|
|
2
3
|
* Evaluate the RFC 0007 predicate against a gate token. A process that
|
|
3
4
|
* declares the pending checkpoint may serve while it backfills; any other
|
|
@@ -87,6 +88,210 @@ export function clientRoundState(lastRoundAtMs, lastErrorCode, consecutiveFailur
|
|
|
87
88
|
...(consecutiveFailures > 0 ? { consecutiveFailures } : {}),
|
|
88
89
|
};
|
|
89
90
|
}
|
|
91
|
+
/** Runtime-neutral base64 (no `Buffer`, Workers-safe) for stored push results. */
|
|
92
|
+
function bytesToBase64(bytes) {
|
|
93
|
+
let binary = '';
|
|
94
|
+
for (const byte of bytes)
|
|
95
|
+
binary += String.fromCharCode(byte);
|
|
96
|
+
return btoa(binary);
|
|
97
|
+
}
|
|
98
|
+
function base64ToBytes(text) {
|
|
99
|
+
const binary = atob(text);
|
|
100
|
+
const bytes = new Uint8Array(binary.length);
|
|
101
|
+
for (let i = 0; i < binary.length; i++)
|
|
102
|
+
bytes[i] = binary.charCodeAt(i);
|
|
103
|
+
return bytes;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Some drivers return a JSON column already parsed (pglite, node-postgres);
|
|
107
|
+
* SQLite returns the `TEXT`.
|
|
108
|
+
*/
|
|
109
|
+
export function asJson(value) {
|
|
110
|
+
if (typeof value === 'string')
|
|
111
|
+
return JSON.parse(value);
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
/** Native SQLite and Postgres return `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
115
|
+
export function asUint8Array(value) {
|
|
116
|
+
if (value instanceof Uint8Array)
|
|
117
|
+
return value;
|
|
118
|
+
if (value instanceof ArrayBuffer)
|
|
119
|
+
return new Uint8Array(value);
|
|
120
|
+
if (Array.isArray(value))
|
|
121
|
+
return new Uint8Array(value);
|
|
122
|
+
throw new Error(`expected a BLOB column, got ${typeof value}`);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Convert a push result to the JSON-able object both SQL backends store.
|
|
126
|
+
* `serverRow` bytes are base64-encoded: JSON cannot hold raw bytes.
|
|
127
|
+
*/
|
|
128
|
+
export function pushResultToJson(result) {
|
|
129
|
+
return {
|
|
130
|
+
status: result.status,
|
|
131
|
+
...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
|
|
132
|
+
...(result.recordedAtMs !== undefined
|
|
133
|
+
? { recordedAtMs: result.recordedAtMs }
|
|
134
|
+
: {}),
|
|
135
|
+
...(result.cacheIdentity !== undefined
|
|
136
|
+
? { cacheIdentity: result.cacheIdentity }
|
|
137
|
+
: {}),
|
|
138
|
+
...(result.conflictAuthorization !== undefined
|
|
139
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
140
|
+
: {}),
|
|
141
|
+
results: result.results.map((record) => {
|
|
142
|
+
if (record.status === 'conflict') {
|
|
143
|
+
return {
|
|
144
|
+
opIndex: record.opIndex,
|
|
145
|
+
status: record.status,
|
|
146
|
+
code: record.code,
|
|
147
|
+
message: record.message,
|
|
148
|
+
serverVersion: record.serverVersion,
|
|
149
|
+
serverRow: bytesToBase64(record.serverRow),
|
|
150
|
+
conflictColumns: bytesToBase64(record.conflictColumns),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (record.status === 'error') {
|
|
154
|
+
return {
|
|
155
|
+
opIndex: record.opIndex,
|
|
156
|
+
status: record.status,
|
|
157
|
+
code: record.code,
|
|
158
|
+
message: record.message,
|
|
159
|
+
retryable: record.retryable,
|
|
160
|
+
...(record.details !== undefined ? { details: record.details } : {}),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
return { opIndex: record.opIndex, status: record.status };
|
|
164
|
+
}),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function pushResultFromJson(value) {
|
|
168
|
+
const parsed = value;
|
|
169
|
+
const results = parsed.results.map((record) => {
|
|
170
|
+
if (record.status === 'conflict') {
|
|
171
|
+
return {
|
|
172
|
+
opIndex: record.opIndex,
|
|
173
|
+
status: 'conflict',
|
|
174
|
+
code: record.code ?? '',
|
|
175
|
+
message: record.message ?? '',
|
|
176
|
+
serverVersion: record.serverVersion ?? 0,
|
|
177
|
+
serverRow: base64ToBytes(record.serverRow ?? ''),
|
|
178
|
+
conflictColumns: base64ToBytes(record.conflictColumns ?? ''),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
if (record.status === 'error') {
|
|
182
|
+
return {
|
|
183
|
+
opIndex: record.opIndex,
|
|
184
|
+
status: 'error',
|
|
185
|
+
code: record.code ?? '',
|
|
186
|
+
message: record.message ?? '',
|
|
187
|
+
retryable: record.retryable ?? false,
|
|
188
|
+
...(record.details !== undefined ? { details: record.details } : {}),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return { opIndex: record.opIndex, status: 'applied' };
|
|
192
|
+
});
|
|
193
|
+
return {
|
|
194
|
+
status: parsed.status,
|
|
195
|
+
...(parsed.commitSeq !== undefined ? { commitSeq: parsed.commitSeq } : {}),
|
|
196
|
+
...(parsed.recordedAtMs !== undefined
|
|
197
|
+
? { recordedAtMs: parsed.recordedAtMs }
|
|
198
|
+
: {}),
|
|
199
|
+
...(parsed.cacheIdentity !== undefined
|
|
200
|
+
? { cacheIdentity: parsed.cacheIdentity }
|
|
201
|
+
: {}),
|
|
202
|
+
...(parsed.conflictAuthorization !== undefined
|
|
203
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
204
|
+
: {}),
|
|
205
|
+
results,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
export function toStoredRow(record) {
|
|
209
|
+
return {
|
|
210
|
+
rowId: record.row_id,
|
|
211
|
+
serverVersion: asNumber(record.server_version),
|
|
212
|
+
scopes: asJson(record.scopes),
|
|
213
|
+
payload: asUint8Array(record.payload),
|
|
214
|
+
...(record.column_versions === null || record.column_versions === undefined
|
|
215
|
+
? {}
|
|
216
|
+
: { columnVersions: asUint8Array(record.column_versions) }),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
export function toTombstone(record) {
|
|
220
|
+
return {
|
|
221
|
+
commitSeq: asNumber(record.commit_seq),
|
|
222
|
+
...(record.server_version === null || record.server_version === undefined
|
|
223
|
+
? {}
|
|
224
|
+
: { serverVersion: asNumber(record.server_version) }),
|
|
225
|
+
...(record.scopes === null || record.scopes === undefined
|
|
226
|
+
? {}
|
|
227
|
+
: { scopes: asJson(record.scopes) }),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
export function toStoredChange(record) {
|
|
231
|
+
return {
|
|
232
|
+
table: record.tbl,
|
|
233
|
+
rowId: record.row_id,
|
|
234
|
+
op: asNumber(record.op) === 1 ? 'upsert' : 'delete',
|
|
235
|
+
...(record.row_version !== null && record.row_version !== undefined
|
|
236
|
+
? { rowVersion: asNumber(record.row_version) }
|
|
237
|
+
: {}),
|
|
238
|
+
scopes: asJson(record.scopes),
|
|
239
|
+
...(record.payload !== null && record.payload !== undefined
|
|
240
|
+
? { payload: asUint8Array(record.payload) }
|
|
241
|
+
: {}),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
export function toStoredReaction(record) {
|
|
245
|
+
return {
|
|
246
|
+
idempotencyKey: record.idempotency_key,
|
|
247
|
+
type: record.type,
|
|
248
|
+
version: asNumber(record.version),
|
|
249
|
+
payload: asJson(record.payload),
|
|
250
|
+
sourceClientId: record.source_client_id,
|
|
251
|
+
sourceClientCommitId: record.source_client_commit_id,
|
|
252
|
+
sourceCommitSeq: asNumber(record.source_commit_seq),
|
|
253
|
+
createdAtMs: asNumber(record.created_at_ms),
|
|
254
|
+
maxAttempts: asNumber(record.max_attempts),
|
|
255
|
+
status: record.status,
|
|
256
|
+
attempts: asNumber(record.attempts),
|
|
257
|
+
availableAtMs: asNumber(record.available_at_ms),
|
|
258
|
+
...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
|
|
259
|
+
...(record.lease_expires_at_ms !== null
|
|
260
|
+
? { leaseExpiresAtMs: asNumber(record.lease_expires_at_ms) }
|
|
261
|
+
: {}),
|
|
262
|
+
...(record.completed_at_ms !== null
|
|
263
|
+
? { completedAtMs: asNumber(record.completed_at_ms) }
|
|
264
|
+
: {}),
|
|
265
|
+
...(record.last_failure !== null
|
|
266
|
+
? { lastFailure: asJson(record.last_failure) }
|
|
267
|
+
: {}),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
export const CLIENT_RECORD_COLUMNS = 'client_id, actor_id, wire_version, cursor, updated_at_ms, schema_version, units, registration_digest, catching_up, last_round_at_ms, last_error_code, consecutive_failures';
|
|
271
|
+
export function toClientRecord(record) {
|
|
272
|
+
return {
|
|
273
|
+
clientId: record.client_id,
|
|
274
|
+
actorId: record.actor_id,
|
|
275
|
+
wireVersion: asNumber(record.wire_version),
|
|
276
|
+
cursor: asNumber(record.cursor),
|
|
277
|
+
updatedAtMs: asNumber(record.updated_at_ms),
|
|
278
|
+
...(record.schema_version === null
|
|
279
|
+
? {}
|
|
280
|
+
: { schemaVersion: asNumber(record.schema_version) }),
|
|
281
|
+
...(record.units === null || record.registration_digest === null
|
|
282
|
+
? {}
|
|
283
|
+
: {
|
|
284
|
+
registration: {
|
|
285
|
+
units: asJson(record.units),
|
|
286
|
+
digest: asUint8Array(record.registration_digest),
|
|
287
|
+
},
|
|
288
|
+
}),
|
|
289
|
+
catchingUp: asJson(record.catching_up),
|
|
290
|
+
...clientRoundState(record.last_round_at_ms === null
|
|
291
|
+
? null
|
|
292
|
+
: asNumber(record.last_round_at_ms), record.last_error_code, asNumber(record.consecutive_failures)),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
90
295
|
/** Durable partition identity refreshed after host authentication (§2.1). */
|
|
91
296
|
/** Longest age of `lastAuthenticatedAtMs` that `touchPartition` leaves unwritten. */
|
|
92
297
|
export const PARTITION_TOUCH_INTERVAL_MS = 60_000;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.85.0",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.85.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/d1-storage.ts
CHANGED
|
@@ -79,7 +79,6 @@ import {
|
|
|
79
79
|
import type { CompiledSchema, CompiledTable } from './schema';
|
|
80
80
|
import { matchesEffective } from './scopes';
|
|
81
81
|
import {
|
|
82
|
-
asUint8Array,
|
|
83
82
|
deserializePushResult,
|
|
84
83
|
placeholders,
|
|
85
84
|
type SqliteRowRecord,
|
|
@@ -90,8 +89,6 @@ import {
|
|
|
90
89
|
sqliteDdlStatements,
|
|
91
90
|
SQLITE_UPGRADE_PROBE,
|
|
92
91
|
sqliteUpgradeDdl,
|
|
93
|
-
toStoredRow,
|
|
94
|
-
toTombstone,
|
|
95
92
|
} from './sqlite-dialect';
|
|
96
93
|
import type { SqliteValue } from './sqlite-driver';
|
|
97
94
|
import type {
|
|
@@ -116,7 +113,12 @@ import type {
|
|
|
116
113
|
StoredRow,
|
|
117
114
|
Tombstone,
|
|
118
115
|
} from './storage';
|
|
119
|
-
import {
|
|
116
|
+
import {
|
|
117
|
+
asUint8Array,
|
|
118
|
+
commitDeleteFloor,
|
|
119
|
+
toStoredRow,
|
|
120
|
+
toTombstone,
|
|
121
|
+
} from './storage';
|
|
120
122
|
import {
|
|
121
123
|
isD1ConstraintError,
|
|
122
124
|
StorageConstraintError,
|