@syncular/server 0.82.3 → 0.84.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/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/sqlite-dialect.ts +18 -257
- package/src/sqlite-storage.ts +8 -6
- package/src/storage.ts +292 -0
package/src/sqlite-dialect.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* promises; the synchronous SQLite driver resolves them with rows it read at
|
|
15
15
|
* call time. Both classes run the identical `test/storage-contract.ts`.
|
|
16
16
|
*/
|
|
17
|
-
import type {
|
|
17
|
+
import type { UnitScopes } from '@syncular/core';
|
|
18
18
|
import {
|
|
19
19
|
bindAuthoritativePartition,
|
|
20
20
|
type BoundAuthoritativeQuery,
|
|
@@ -45,9 +45,18 @@ import type { CompiledTable } from './schema';
|
|
|
45
45
|
import { matchesEffective } from './scopes';
|
|
46
46
|
import type { SqliteValue } from './sqlite-driver';
|
|
47
47
|
import {
|
|
48
|
-
|
|
48
|
+
type ClientRowRecord,
|
|
49
|
+
CLIENT_RECORD_COLUMNS,
|
|
49
50
|
PARTITION_TOUCH_INTERVAL_MS,
|
|
51
|
+
pushResultFromJson,
|
|
52
|
+
pushResultToJson,
|
|
50
53
|
rowsLiveAfterDelete,
|
|
54
|
+
type StoredReactionRecord,
|
|
55
|
+
toClientRecord,
|
|
56
|
+
toStoredChange,
|
|
57
|
+
toStoredReaction,
|
|
58
|
+
toStoredRow,
|
|
59
|
+
toTombstone,
|
|
51
60
|
} from './storage';
|
|
52
61
|
import type {
|
|
53
62
|
AuthoritativeQueryRequest,
|
|
@@ -61,7 +70,6 @@ import type {
|
|
|
61
70
|
CommitMetadataQuery,
|
|
62
71
|
CommitPruneQuery,
|
|
63
72
|
CommitPruneResult,
|
|
64
|
-
DurableJsonValue,
|
|
65
73
|
IndexRowScanQuery,
|
|
66
74
|
LiveWindow,
|
|
67
75
|
LiveWindowQuery,
|
|
@@ -73,7 +81,6 @@ import type {
|
|
|
73
81
|
PrunedReactionCounts,
|
|
74
82
|
PushWatermark,
|
|
75
83
|
ReactionClaimQuery,
|
|
76
|
-
ReactionFailure,
|
|
77
84
|
ReactionFailureUpdate,
|
|
78
85
|
ReactionListQuery,
|
|
79
86
|
ReactionPruneQuery,
|
|
@@ -85,7 +92,6 @@ import type {
|
|
|
85
92
|
ScopeCommitActivity,
|
|
86
93
|
ScopeValueHistoryQuery,
|
|
87
94
|
ScopeValueQuery,
|
|
88
|
-
StoredChange,
|
|
89
95
|
StoredPushResult,
|
|
90
96
|
StoredReaction,
|
|
91
97
|
StoredRow,
|
|
@@ -350,124 +356,15 @@ export function placeholders(count: number): string {
|
|
|
350
356
|
|
|
351
357
|
// -- value (de)serialization, shared by both SQLite storages ----------------
|
|
352
358
|
|
|
353
|
-
/** Runtime-neutral base64 (no `Buffer` — Workers-safe) for push results. */
|
|
354
|
-
function bytesToBase64(bytes: Uint8Array): string {
|
|
355
|
-
let binary = '';
|
|
356
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
357
|
-
return btoa(binary);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
function base64ToBytes(text: string): Uint8Array {
|
|
361
|
-
const binary = atob(text);
|
|
362
|
-
const bytes = new Uint8Array(binary.length);
|
|
363
|
-
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
364
|
-
return bytes;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
interface SerializedResult {
|
|
368
|
-
opIndex: number;
|
|
369
|
-
status: string;
|
|
370
|
-
code?: string;
|
|
371
|
-
message?: string;
|
|
372
|
-
serverVersion?: number;
|
|
373
|
-
serverRow?: string;
|
|
374
|
-
conflictColumns?: string;
|
|
375
|
-
retryable?: boolean;
|
|
376
|
-
details?: import('@syncular/core').RejectionDetails;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
359
|
/** Serialize a push result to the JSON `TEXT` stored in `sync_push_results`. */
|
|
380
360
|
export function serializePushResult(result: StoredPushResult): string {
|
|
381
|
-
return JSON.stringify(
|
|
382
|
-
status: result.status,
|
|
383
|
-
...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
|
|
384
|
-
...(result.recordedAtMs !== undefined
|
|
385
|
-
? { recordedAtMs: result.recordedAtMs }
|
|
386
|
-
: {}),
|
|
387
|
-
...(result.cacheIdentity !== undefined
|
|
388
|
-
? { cacheIdentity: result.cacheIdentity }
|
|
389
|
-
: {}),
|
|
390
|
-
...(result.conflictAuthorization !== undefined
|
|
391
|
-
? { conflictAuthorization: result.conflictAuthorization }
|
|
392
|
-
: {}),
|
|
393
|
-
results: result.results.map((record) => {
|
|
394
|
-
if (record.status === 'conflict') {
|
|
395
|
-
return {
|
|
396
|
-
opIndex: record.opIndex,
|
|
397
|
-
status: record.status,
|
|
398
|
-
code: record.code,
|
|
399
|
-
message: record.message,
|
|
400
|
-
serverVersion: record.serverVersion,
|
|
401
|
-
serverRow: bytesToBase64(record.serverRow),
|
|
402
|
-
conflictColumns: bytesToBase64(record.conflictColumns),
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
if (record.status === 'error') {
|
|
406
|
-
return {
|
|
407
|
-
opIndex: record.opIndex,
|
|
408
|
-
status: record.status,
|
|
409
|
-
code: record.code,
|
|
410
|
-
message: record.message,
|
|
411
|
-
retryable: record.retryable,
|
|
412
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
return { opIndex: record.opIndex, status: record.status };
|
|
416
|
-
}),
|
|
417
|
-
});
|
|
361
|
+
return JSON.stringify(pushResultToJson(result));
|
|
418
362
|
}
|
|
419
363
|
|
|
420
364
|
/** Parse a stored push result; an unreadable record is a §6.3 cache miss. */
|
|
421
365
|
export function deserializePushResult(text: string): StoredPushResult {
|
|
422
366
|
try {
|
|
423
|
-
|
|
424
|
-
status: 'applied' | 'rejected';
|
|
425
|
-
commitSeq?: number;
|
|
426
|
-
recordedAtMs?: number;
|
|
427
|
-
cacheIdentity?: string;
|
|
428
|
-
conflictAuthorization?: StoredPushResult['conflictAuthorization'];
|
|
429
|
-
results: SerializedResult[];
|
|
430
|
-
};
|
|
431
|
-
const results: PushOperationResult[] = parsed.results.map((record) => {
|
|
432
|
-
if (record.status === 'conflict') {
|
|
433
|
-
return {
|
|
434
|
-
opIndex: record.opIndex,
|
|
435
|
-
status: 'conflict',
|
|
436
|
-
code: record.code ?? '',
|
|
437
|
-
message: record.message ?? '',
|
|
438
|
-
serverVersion: record.serverVersion ?? 0,
|
|
439
|
-
serverRow: base64ToBytes(record.serverRow ?? ''),
|
|
440
|
-
conflictColumns: base64ToBytes(record.conflictColumns ?? ''),
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
if (record.status === 'error') {
|
|
444
|
-
return {
|
|
445
|
-
opIndex: record.opIndex,
|
|
446
|
-
status: 'error',
|
|
447
|
-
code: record.code ?? '',
|
|
448
|
-
message: record.message ?? '',
|
|
449
|
-
retryable: record.retryable ?? false,
|
|
450
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
451
|
-
};
|
|
452
|
-
}
|
|
453
|
-
return { opIndex: record.opIndex, status: 'applied' };
|
|
454
|
-
});
|
|
455
|
-
return {
|
|
456
|
-
status: parsed.status,
|
|
457
|
-
...(parsed.commitSeq !== undefined
|
|
458
|
-
? { commitSeq: parsed.commitSeq }
|
|
459
|
-
: {}),
|
|
460
|
-
...(parsed.recordedAtMs !== undefined
|
|
461
|
-
? { recordedAtMs: parsed.recordedAtMs }
|
|
462
|
-
: {}),
|
|
463
|
-
...(parsed.cacheIdentity !== undefined
|
|
464
|
-
? { cacheIdentity: parsed.cacheIdentity }
|
|
465
|
-
: {}),
|
|
466
|
-
...(parsed.conflictAuthorization !== undefined
|
|
467
|
-
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
468
|
-
: {}),
|
|
469
|
-
results,
|
|
470
|
-
};
|
|
367
|
+
return pushResultFromJson(JSON.parse(text));
|
|
471
368
|
} catch {
|
|
472
369
|
throw syncError(
|
|
473
370
|
'sync.idempotency_cache_miss',
|
|
@@ -494,148 +391,12 @@ export interface SqliteChangeRecord {
|
|
|
494
391
|
payload: Uint8Array | null;
|
|
495
392
|
}
|
|
496
393
|
|
|
497
|
-
/** Native SQLite returns `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
498
|
-
export function asUint8Array(value: unknown): Uint8Array {
|
|
499
|
-
if (value instanceof Uint8Array) return value;
|
|
500
|
-
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
501
|
-
if (Array.isArray(value)) return new Uint8Array(value);
|
|
502
|
-
throw new Error(`expected a BLOB column, got ${typeof value}`);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export function toStoredRow(record: SqliteRowRecord): StoredRow {
|
|
506
|
-
return {
|
|
507
|
-
rowId: record.row_id,
|
|
508
|
-
serverVersion: record.server_version,
|
|
509
|
-
scopes: JSON.parse(record.scopes) as Record<string, string>,
|
|
510
|
-
payload: asUint8Array(record.payload),
|
|
511
|
-
...(record.column_versions !== null
|
|
512
|
-
? { columnVersions: asUint8Array(record.column_versions) }
|
|
513
|
-
: {}),
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
|
|
517
394
|
export interface SqliteTombstoneRecord {
|
|
518
395
|
commit_seq: number;
|
|
519
396
|
server_version: number | null;
|
|
520
397
|
scopes: string | null;
|
|
521
398
|
}
|
|
522
399
|
|
|
523
|
-
export function toTombstone(record: SqliteTombstoneRecord): Tombstone {
|
|
524
|
-
return {
|
|
525
|
-
commitSeq: record.commit_seq,
|
|
526
|
-
...(record.server_version !== null
|
|
527
|
-
? { serverVersion: record.server_version }
|
|
528
|
-
: {}),
|
|
529
|
-
...(record.scopes !== null
|
|
530
|
-
? { scopes: JSON.parse(record.scopes) as Record<string, string> }
|
|
531
|
-
: {}),
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
export function toStoredChange(record: SqliteChangeRecord): StoredChange {
|
|
536
|
-
return {
|
|
537
|
-
table: record.tbl,
|
|
538
|
-
rowId: record.row_id,
|
|
539
|
-
op: record.op === 1 ? 'upsert' : 'delete',
|
|
540
|
-
...(record.row_version !== null ? { rowVersion: record.row_version } : {}),
|
|
541
|
-
scopes: JSON.parse(record.scopes) as Record<string, string>,
|
|
542
|
-
...(record.payload !== null
|
|
543
|
-
? { payload: asUint8Array(record.payload) }
|
|
544
|
-
: {}),
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
interface SqliteReactionRecord {
|
|
549
|
-
idempotency_key: string;
|
|
550
|
-
type: string;
|
|
551
|
-
version: number;
|
|
552
|
-
payload: string;
|
|
553
|
-
source_client_id: string;
|
|
554
|
-
source_client_commit_id: string;
|
|
555
|
-
source_commit_seq: number;
|
|
556
|
-
created_at_ms: number;
|
|
557
|
-
available_at_ms: number;
|
|
558
|
-
status: StoredReaction['status'];
|
|
559
|
-
attempts: number;
|
|
560
|
-
max_attempts: number;
|
|
561
|
-
lease_owner: string | null;
|
|
562
|
-
lease_expires_at_ms: number | null;
|
|
563
|
-
completed_at_ms: number | null;
|
|
564
|
-
last_failure: string | null;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function toStoredReaction(record: SqliteReactionRecord): StoredReaction {
|
|
568
|
-
return {
|
|
569
|
-
idempotencyKey: record.idempotency_key,
|
|
570
|
-
type: record.type,
|
|
571
|
-
version: record.version,
|
|
572
|
-
payload: JSON.parse(record.payload) as DurableJsonValue,
|
|
573
|
-
sourceClientId: record.source_client_id,
|
|
574
|
-
sourceClientCommitId: record.source_client_commit_id,
|
|
575
|
-
sourceCommitSeq: record.source_commit_seq,
|
|
576
|
-
createdAtMs: record.created_at_ms,
|
|
577
|
-
maxAttempts: record.max_attempts,
|
|
578
|
-
status: record.status,
|
|
579
|
-
attempts: record.attempts,
|
|
580
|
-
availableAtMs: record.available_at_ms,
|
|
581
|
-
...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
|
|
582
|
-
...(record.lease_expires_at_ms !== null
|
|
583
|
-
? { leaseExpiresAtMs: record.lease_expires_at_ms }
|
|
584
|
-
: {}),
|
|
585
|
-
...(record.completed_at_ms !== null
|
|
586
|
-
? { completedAtMs: record.completed_at_ms }
|
|
587
|
-
: {}),
|
|
588
|
-
...(record.last_failure !== null
|
|
589
|
-
? { lastFailure: JSON.parse(record.last_failure) as ReactionFailure }
|
|
590
|
-
: {}),
|
|
591
|
-
};
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
interface SqliteClientRecord {
|
|
595
|
-
client_id: string;
|
|
596
|
-
actor_id: string;
|
|
597
|
-
wire_version: number;
|
|
598
|
-
cursor: number;
|
|
599
|
-
updated_at_ms: number;
|
|
600
|
-
schema_version: number | null;
|
|
601
|
-
units: string | null;
|
|
602
|
-
registration_digest: Uint8Array | null;
|
|
603
|
-
catching_up: string;
|
|
604
|
-
last_round_at_ms: number | null;
|
|
605
|
-
last_error_code: string | null;
|
|
606
|
-
consecutive_failures: number;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
const CLIENT_RECORD_COLUMNS =
|
|
610
|
-
'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';
|
|
611
|
-
|
|
612
|
-
function toClientRecord(record: SqliteClientRecord): ClientRecord {
|
|
613
|
-
return {
|
|
614
|
-
clientId: record.client_id,
|
|
615
|
-
actorId: record.actor_id,
|
|
616
|
-
wireVersion: record.wire_version,
|
|
617
|
-
cursor: record.cursor,
|
|
618
|
-
updatedAtMs: record.updated_at_ms,
|
|
619
|
-
...(record.schema_version !== null
|
|
620
|
-
? { schemaVersion: record.schema_version }
|
|
621
|
-
: {}),
|
|
622
|
-
...(record.units !== null && record.registration_digest !== null
|
|
623
|
-
? {
|
|
624
|
-
registration: {
|
|
625
|
-
units: JSON.parse(record.units) as UnitScopes[],
|
|
626
|
-
digest: asUint8Array(record.registration_digest),
|
|
627
|
-
},
|
|
628
|
-
}
|
|
629
|
-
: {}),
|
|
630
|
-
catchingUp: JSON.parse(record.catching_up) as ClientRecord['catchingUp'],
|
|
631
|
-
...clientRoundState(
|
|
632
|
-
record.last_round_at_ms,
|
|
633
|
-
record.last_error_code,
|
|
634
|
-
record.consecutive_failures,
|
|
635
|
-
),
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
|
|
639
400
|
interface SqlitePartitionRegistryRecord {
|
|
640
401
|
partition: string;
|
|
641
402
|
log_epoch: string;
|
|
@@ -978,7 +739,7 @@ export abstract class SqliteFamilyStorage {
|
|
|
978
739
|
if (query.types.length + 7 > this.maxBindParams) {
|
|
979
740
|
throw new Error('D1 reaction claim exceeds the bound-parameter limit');
|
|
980
741
|
}
|
|
981
|
-
const records = await this.all<
|
|
742
|
+
const records = await this.all<StoredReactionRecord>(
|
|
982
743
|
`UPDATE sync_reactions
|
|
983
744
|
SET status='leased', attempts=attempts+1,
|
|
984
745
|
lease_owner=?, lease_expires_at_ms=?, completed_at_ms=NULL
|
|
@@ -1092,7 +853,7 @@ export abstract class SqliteFamilyStorage {
|
|
|
1092
853
|
partition: string,
|
|
1093
854
|
idempotencyKey: string,
|
|
1094
855
|
): Promise<StoredReaction | undefined> {
|
|
1095
|
-
const [record] = await this.all<
|
|
856
|
+
const [record] = await this.all<StoredReactionRecord>(
|
|
1096
857
|
'SELECT * FROM sync_reactions WHERE partition=? AND idempotency_key=?',
|
|
1097
858
|
[partition, idempotencyKey],
|
|
1098
859
|
);
|
|
@@ -1117,7 +878,7 @@ export abstract class SqliteFamilyStorage {
|
|
|
1117
878
|
throw new Error('D1 reaction list exceeds the bound-parameter limit');
|
|
1118
879
|
}
|
|
1119
880
|
params.push(query.limit);
|
|
1120
|
-
const records = await this.all<
|
|
881
|
+
const records = await this.all<StoredReactionRecord>(
|
|
1121
882
|
`SELECT * FROM sync_reactions WHERE ${where.join(' AND ')}
|
|
1122
883
|
ORDER BY created_at_ms DESC, idempotency_key DESC LIMIT ?`,
|
|
1123
884
|
params,
|
|
@@ -1348,7 +1109,7 @@ export abstract class SqliteFamilyStorage {
|
|
|
1348
1109
|
partition: string,
|
|
1349
1110
|
clientId: string,
|
|
1350
1111
|
): Promise<ClientRecord | undefined> {
|
|
1351
|
-
const [record] = await this.all<
|
|
1112
|
+
const [record] = await this.all<ClientRowRecord>(
|
|
1352
1113
|
`SELECT ${CLIENT_RECORD_COLUMNS} FROM sync_clients WHERE partition=? AND client_id=?`,
|
|
1353
1114
|
[partition, clientId],
|
|
1354
1115
|
);
|
|
@@ -1582,7 +1343,7 @@ export abstract class SqliteFamilyStorage {
|
|
|
1582
1343
|
|
|
1583
1344
|
async listClientRecords(partition: string): Promise<ClientRecord[]> {
|
|
1584
1345
|
return (
|
|
1585
|
-
await this.all<
|
|
1346
|
+
await this.all<ClientRowRecord>(
|
|
1586
1347
|
`SELECT ${CLIENT_RECORD_COLUMNS} FROM sync_clients WHERE partition=? ORDER BY updated_at_ms DESC`,
|
|
1587
1348
|
[partition],
|
|
1588
1349
|
)
|
package/src/sqlite-storage.ts
CHANGED
|
@@ -511,9 +511,10 @@ END`);
|
|
|
511
511
|
)
|
|
512
512
|
.get();
|
|
513
513
|
if (marker !== null && marker.schema_version > schema.version) {
|
|
514
|
-
throw new
|
|
515
|
-
|
|
516
|
-
|
|
514
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
515
|
+
storedVersion: String(marker.schema_version),
|
|
516
|
+
configuredVersion: String(schema.version),
|
|
517
|
+
});
|
|
517
518
|
}
|
|
518
519
|
if (marker !== null && marker.schema_version === schema.version) {
|
|
519
520
|
assertStoredLayouts(schema, marker.layouts);
|
|
@@ -627,9 +628,10 @@ END`);
|
|
|
627
628
|
)
|
|
628
629
|
.get();
|
|
629
630
|
if (locked !== null && locked.schema_version > schema.version) {
|
|
630
|
-
throw new
|
|
631
|
-
|
|
632
|
-
|
|
631
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
632
|
+
storedVersion: String(locked.schema_version),
|
|
633
|
+
configuredVersion: String(schema.version),
|
|
634
|
+
});
|
|
633
635
|
}
|
|
634
636
|
if (locked !== null && locked.schema_version === schema.version) {
|
|
635
637
|
this.db.exec('COMMIT');
|
package/src/storage.ts
CHANGED
|
@@ -20,10 +20,12 @@ import type { AuthoritativeRelationPlan } from './authoritative-query';
|
|
|
20
20
|
*/
|
|
21
21
|
import type {
|
|
22
22
|
PushOperationResult,
|
|
23
|
+
RejectionDetails,
|
|
23
24
|
RowValue,
|
|
24
25
|
ScopeMap,
|
|
25
26
|
UnitScopes,
|
|
26
27
|
} from '@syncular/core';
|
|
28
|
+
import { asNumber } from './pg-executor';
|
|
27
29
|
import type { CompiledSchema } from './schema';
|
|
28
30
|
|
|
29
31
|
export interface CommitPruneQuery {
|
|
@@ -500,6 +502,296 @@ export function clientRoundState(
|
|
|
500
502
|
};
|
|
501
503
|
}
|
|
502
504
|
|
|
505
|
+
/** Runtime-neutral base64 (no `Buffer`, Workers-safe) for stored push results. */
|
|
506
|
+
function bytesToBase64(bytes: Uint8Array): string {
|
|
507
|
+
let binary = '';
|
|
508
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
509
|
+
return btoa(binary);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function base64ToBytes(text: string): Uint8Array {
|
|
513
|
+
const binary = atob(text);
|
|
514
|
+
const bytes = new Uint8Array(binary.length);
|
|
515
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
516
|
+
return bytes;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Some drivers return a JSON column already parsed (pglite, node-postgres);
|
|
521
|
+
* SQLite returns the `TEXT`.
|
|
522
|
+
*/
|
|
523
|
+
export function asJson<T>(value: unknown): T {
|
|
524
|
+
if (typeof value === 'string') return JSON.parse(value) as T;
|
|
525
|
+
return value as T;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/** Native SQLite and Postgres return `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
529
|
+
export function asUint8Array(value: unknown): Uint8Array {
|
|
530
|
+
if (value instanceof Uint8Array) return value;
|
|
531
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
532
|
+
if (Array.isArray(value)) return new Uint8Array(value);
|
|
533
|
+
throw new Error(`expected a BLOB column, got ${typeof value}`);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
interface SerializedResult {
|
|
537
|
+
opIndex: number;
|
|
538
|
+
status: string;
|
|
539
|
+
code?: string;
|
|
540
|
+
message?: string;
|
|
541
|
+
serverVersion?: number;
|
|
542
|
+
serverRow?: string;
|
|
543
|
+
conflictColumns?: string;
|
|
544
|
+
retryable?: boolean;
|
|
545
|
+
details?: RejectionDetails;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Convert a push result to the JSON-able object both SQL backends store.
|
|
550
|
+
* `serverRow` bytes are base64-encoded: JSON cannot hold raw bytes.
|
|
551
|
+
*/
|
|
552
|
+
export function pushResultToJson(result: StoredPushResult): unknown {
|
|
553
|
+
return {
|
|
554
|
+
status: result.status,
|
|
555
|
+
...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
|
|
556
|
+
...(result.recordedAtMs !== undefined
|
|
557
|
+
? { recordedAtMs: result.recordedAtMs }
|
|
558
|
+
: {}),
|
|
559
|
+
...(result.cacheIdentity !== undefined
|
|
560
|
+
? { cacheIdentity: result.cacheIdentity }
|
|
561
|
+
: {}),
|
|
562
|
+
...(result.conflictAuthorization !== undefined
|
|
563
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
564
|
+
: {}),
|
|
565
|
+
results: result.results.map((record) => {
|
|
566
|
+
if (record.status === 'conflict') {
|
|
567
|
+
return {
|
|
568
|
+
opIndex: record.opIndex,
|
|
569
|
+
status: record.status,
|
|
570
|
+
code: record.code,
|
|
571
|
+
message: record.message,
|
|
572
|
+
serverVersion: record.serverVersion,
|
|
573
|
+
serverRow: bytesToBase64(record.serverRow),
|
|
574
|
+
conflictColumns: bytesToBase64(record.conflictColumns),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
if (record.status === 'error') {
|
|
578
|
+
return {
|
|
579
|
+
opIndex: record.opIndex,
|
|
580
|
+
status: record.status,
|
|
581
|
+
code: record.code,
|
|
582
|
+
message: record.message,
|
|
583
|
+
retryable: record.retryable,
|
|
584
|
+
...(record.details !== undefined ? { details: record.details } : {}),
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
return { opIndex: record.opIndex, status: record.status };
|
|
588
|
+
}),
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export function pushResultFromJson(value: unknown): StoredPushResult {
|
|
593
|
+
const parsed = value as {
|
|
594
|
+
status: 'applied' | 'rejected';
|
|
595
|
+
commitSeq?: number;
|
|
596
|
+
recordedAtMs?: number;
|
|
597
|
+
cacheIdentity?: string;
|
|
598
|
+
conflictAuthorization?: StoredPushResult['conflictAuthorization'];
|
|
599
|
+
results: SerializedResult[];
|
|
600
|
+
};
|
|
601
|
+
const results: PushOperationResult[] = parsed.results.map((record) => {
|
|
602
|
+
if (record.status === 'conflict') {
|
|
603
|
+
return {
|
|
604
|
+
opIndex: record.opIndex,
|
|
605
|
+
status: 'conflict',
|
|
606
|
+
code: record.code ?? '',
|
|
607
|
+
message: record.message ?? '',
|
|
608
|
+
serverVersion: record.serverVersion ?? 0,
|
|
609
|
+
serverRow: base64ToBytes(record.serverRow ?? ''),
|
|
610
|
+
conflictColumns: base64ToBytes(record.conflictColumns ?? ''),
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
if (record.status === 'error') {
|
|
614
|
+
return {
|
|
615
|
+
opIndex: record.opIndex,
|
|
616
|
+
status: 'error',
|
|
617
|
+
code: record.code ?? '',
|
|
618
|
+
message: record.message ?? '',
|
|
619
|
+
retryable: record.retryable ?? false,
|
|
620
|
+
...(record.details !== undefined ? { details: record.details } : {}),
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return { opIndex: record.opIndex, status: 'applied' };
|
|
624
|
+
});
|
|
625
|
+
return {
|
|
626
|
+
status: parsed.status,
|
|
627
|
+
...(parsed.commitSeq !== undefined ? { commitSeq: parsed.commitSeq } : {}),
|
|
628
|
+
...(parsed.recordedAtMs !== undefined
|
|
629
|
+
? { recordedAtMs: parsed.recordedAtMs }
|
|
630
|
+
: {}),
|
|
631
|
+
...(parsed.cacheIdentity !== undefined
|
|
632
|
+
? { cacheIdentity: parsed.cacheIdentity }
|
|
633
|
+
: {}),
|
|
634
|
+
...(parsed.conflictAuthorization !== undefined
|
|
635
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
636
|
+
: {}),
|
|
637
|
+
results,
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** Row shapes the SQL backends' `SELECT`s return; values are driver-typed. */
|
|
642
|
+
export interface StoredRowRecord {
|
|
643
|
+
row_id: string;
|
|
644
|
+
server_version: unknown;
|
|
645
|
+
scopes: unknown;
|
|
646
|
+
payload: unknown;
|
|
647
|
+
column_versions: unknown;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
export interface StoredChangeRecord {
|
|
651
|
+
tbl: string;
|
|
652
|
+
row_id: string;
|
|
653
|
+
op: unknown;
|
|
654
|
+
row_version: unknown;
|
|
655
|
+
scopes: unknown;
|
|
656
|
+
payload: unknown;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
export interface StoredReactionRecord {
|
|
660
|
+
idempotency_key: string;
|
|
661
|
+
type: string;
|
|
662
|
+
version: unknown;
|
|
663
|
+
payload: unknown;
|
|
664
|
+
source_client_id: string;
|
|
665
|
+
source_client_commit_id: string;
|
|
666
|
+
source_commit_seq: unknown;
|
|
667
|
+
created_at_ms: unknown;
|
|
668
|
+
available_at_ms: unknown;
|
|
669
|
+
status: StoredReaction['status'];
|
|
670
|
+
attempts: unknown;
|
|
671
|
+
max_attempts: unknown;
|
|
672
|
+
lease_owner: string | null;
|
|
673
|
+
lease_expires_at_ms: unknown | null;
|
|
674
|
+
completed_at_ms: unknown | null;
|
|
675
|
+
last_failure: unknown | null;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
export interface ClientRowRecord {
|
|
679
|
+
client_id: string;
|
|
680
|
+
actor_id: string;
|
|
681
|
+
wire_version: unknown;
|
|
682
|
+
cursor: unknown;
|
|
683
|
+
updated_at_ms: unknown;
|
|
684
|
+
schema_version: unknown;
|
|
685
|
+
units: unknown;
|
|
686
|
+
registration_digest: unknown;
|
|
687
|
+
catching_up: unknown;
|
|
688
|
+
last_round_at_ms: unknown;
|
|
689
|
+
last_error_code: string | null;
|
|
690
|
+
consecutive_failures: unknown;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
export function toStoredRow(record: StoredRowRecord): StoredRow {
|
|
694
|
+
return {
|
|
695
|
+
rowId: record.row_id,
|
|
696
|
+
serverVersion: asNumber(record.server_version),
|
|
697
|
+
scopes: asJson<Record<string, string>>(record.scopes),
|
|
698
|
+
payload: asUint8Array(record.payload),
|
|
699
|
+
...(record.column_versions === null || record.column_versions === undefined
|
|
700
|
+
? {}
|
|
701
|
+
: { columnVersions: asUint8Array(record.column_versions) }),
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export function toTombstone(record: {
|
|
706
|
+
commit_seq: unknown;
|
|
707
|
+
server_version: unknown;
|
|
708
|
+
scopes: unknown;
|
|
709
|
+
}): Tombstone {
|
|
710
|
+
return {
|
|
711
|
+
commitSeq: asNumber(record.commit_seq),
|
|
712
|
+
...(record.server_version === null || record.server_version === undefined
|
|
713
|
+
? {}
|
|
714
|
+
: { serverVersion: asNumber(record.server_version) }),
|
|
715
|
+
...(record.scopes === null || record.scopes === undefined
|
|
716
|
+
? {}
|
|
717
|
+
: { scopes: asJson<Record<string, string>>(record.scopes) }),
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
export function toStoredChange(record: StoredChangeRecord): StoredChange {
|
|
722
|
+
return {
|
|
723
|
+
table: record.tbl,
|
|
724
|
+
rowId: record.row_id,
|
|
725
|
+
op: asNumber(record.op) === 1 ? 'upsert' : 'delete',
|
|
726
|
+
...(record.row_version !== null && record.row_version !== undefined
|
|
727
|
+
? { rowVersion: asNumber(record.row_version) }
|
|
728
|
+
: {}),
|
|
729
|
+
scopes: asJson<Record<string, string>>(record.scopes),
|
|
730
|
+
...(record.payload !== null && record.payload !== undefined
|
|
731
|
+
? { payload: asUint8Array(record.payload) }
|
|
732
|
+
: {}),
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
export function toStoredReaction(record: StoredReactionRecord): StoredReaction {
|
|
737
|
+
return {
|
|
738
|
+
idempotencyKey: record.idempotency_key,
|
|
739
|
+
type: record.type,
|
|
740
|
+
version: asNumber(record.version),
|
|
741
|
+
payload: asJson(record.payload) as DurableJsonValue,
|
|
742
|
+
sourceClientId: record.source_client_id,
|
|
743
|
+
sourceClientCommitId: record.source_client_commit_id,
|
|
744
|
+
sourceCommitSeq: asNumber(record.source_commit_seq),
|
|
745
|
+
createdAtMs: asNumber(record.created_at_ms),
|
|
746
|
+
maxAttempts: asNumber(record.max_attempts),
|
|
747
|
+
status: record.status,
|
|
748
|
+
attempts: asNumber(record.attempts),
|
|
749
|
+
availableAtMs: asNumber(record.available_at_ms),
|
|
750
|
+
...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
|
|
751
|
+
...(record.lease_expires_at_ms !== null
|
|
752
|
+
? { leaseExpiresAtMs: asNumber(record.lease_expires_at_ms) }
|
|
753
|
+
: {}),
|
|
754
|
+
...(record.completed_at_ms !== null
|
|
755
|
+
? { completedAtMs: asNumber(record.completed_at_ms) }
|
|
756
|
+
: {}),
|
|
757
|
+
...(record.last_failure !== null
|
|
758
|
+
? { lastFailure: asJson(record.last_failure) as ReactionFailure }
|
|
759
|
+
: {}),
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
export const CLIENT_RECORD_COLUMNS =
|
|
764
|
+
'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';
|
|
765
|
+
|
|
766
|
+
export function toClientRecord(record: ClientRowRecord): ClientRecord {
|
|
767
|
+
return {
|
|
768
|
+
clientId: record.client_id,
|
|
769
|
+
actorId: record.actor_id,
|
|
770
|
+
wireVersion: asNumber(record.wire_version),
|
|
771
|
+
cursor: asNumber(record.cursor),
|
|
772
|
+
updatedAtMs: asNumber(record.updated_at_ms),
|
|
773
|
+
...(record.schema_version === null
|
|
774
|
+
? {}
|
|
775
|
+
: { schemaVersion: asNumber(record.schema_version) }),
|
|
776
|
+
...(record.units === null || record.registration_digest === null
|
|
777
|
+
? {}
|
|
778
|
+
: {
|
|
779
|
+
registration: {
|
|
780
|
+
units: asJson<UnitScopes[]>(record.units),
|
|
781
|
+
digest: asUint8Array(record.registration_digest),
|
|
782
|
+
},
|
|
783
|
+
}),
|
|
784
|
+
catchingUp: asJson<ClientRecord['catchingUp']>(record.catching_up),
|
|
785
|
+
...clientRoundState(
|
|
786
|
+
record.last_round_at_ms === null
|
|
787
|
+
? null
|
|
788
|
+
: asNumber(record.last_round_at_ms),
|
|
789
|
+
record.last_error_code,
|
|
790
|
+
asNumber(record.consecutive_failures),
|
|
791
|
+
),
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
|
|
503
795
|
/** One unit of the §4.5 scan set. */
|
|
504
796
|
export interface LiveScanUnit {
|
|
505
797
|
/** The unit's scope map; its first variable (keys sorted) is its index key. */
|