@syncular/client 0.90.0 → 0.92.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 +3 -1
- package/dist/apply.d.ts +14 -4
- package/dist/apply.js +30 -29
- package/dist/client.d.ts +7 -0
- package/dist/client.js +314 -263
- package/dist/encryption.js +5 -7
- package/dist/errors.js +4 -4
- package/dist/http.js +9 -8
- package/dist/multi-tab.d.ts +3 -2
- package/dist/multi-tab.js +3 -2
- package/dist/native-bridge.d.ts +1 -1
- package/dist/native-bridge.js +1 -1
- package/dist/reactive-store.js +7 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +8 -3
- package/dist/sync-scheduler.js +5 -1
- package/dist/worker-core.d.ts +0 -15
- package/dist/worker-core.js +44 -43
- package/package.json +3 -3
- package/src/apply.ts +52 -30
- package/src/client.ts +377 -330
- package/src/encryption.ts +5 -6
- package/src/errors.ts +4 -4
- package/src/http.ts +9 -15
- package/src/multi-tab.ts +3 -2
- package/src/native-bridge.ts +1 -1
- package/src/reactive-store.ts +6 -1
- package/src/schema.ts +2 -4
- package/src/sync-scheduler.ts +5 -1
- package/src/worker-core.ts +38 -44
package/dist/client.js
CHANGED
|
@@ -160,6 +160,11 @@ function isFinalPushResult(frame) {
|
|
|
160
160
|
result.code === 'sync.idempotency_cache_miss' &&
|
|
161
161
|
result.retryable));
|
|
162
162
|
}
|
|
163
|
+
function transportFlag(value) {
|
|
164
|
+
if (typeof value !== 'boolean')
|
|
165
|
+
throw invalidRequest('transportEnabled must be a boolean');
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
163
168
|
/** The stable `code` a thrown value carries, when it has one. */
|
|
164
169
|
function thrownCode(error) {
|
|
165
170
|
const code = error?.code;
|
|
@@ -291,6 +296,9 @@ export class SyncClient {
|
|
|
291
296
|
#queryFailures = new Map();
|
|
292
297
|
/** The batch accumulator; non-undefined only inside `#applyBatch`. */
|
|
293
298
|
#batch;
|
|
299
|
+
/** §1.4 rule 4: the change batches an open coalesced apply transaction
|
|
300
|
+
* publishes after its COMMIT, in revision order. */
|
|
301
|
+
#held;
|
|
294
302
|
/**
|
|
295
303
|
* Operation-serialization mutex (the core owns one loop). Every
|
|
296
304
|
* transaction-entering ASYNC operation — `sync`, the delta-apply body, and
|
|
@@ -312,11 +320,9 @@ export class SyncClient {
|
|
|
312
320
|
#protectedAsync = new Set();
|
|
313
321
|
#preflightBarrier;
|
|
314
322
|
constructor(config) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
this.#transportEnabled = config.transportEnabled !== false;
|
|
323
|
+
this.#transportEnabled =
|
|
324
|
+
config.transportEnabled === undefined ||
|
|
325
|
+
transportFlag(config.transportEnabled);
|
|
320
326
|
for (const name of [
|
|
321
327
|
'maxPushCommitsPerRequest',
|
|
322
328
|
'maxPushOperationsPerRequest',
|
|
@@ -547,12 +553,16 @@ export class SyncClient {
|
|
|
547
553
|
// earlier WITHOUT a schema bump.
|
|
548
554
|
this.#db.transaction(() => {
|
|
549
555
|
ensureLocalSyncedSchema(this.#db, this.#schema);
|
|
550
|
-
|
|
551
|
-
setMeta(this.#db, LOCAL_SCHEMA_VERSION_KEY, String(this.#schema.version));
|
|
552
|
-
setLocalSchemaDescriptor(this.#db, this.#schema);
|
|
553
|
-
saveUnitScopeSchema(this.#db, this.#schema);
|
|
556
|
+
this.#stampSchema(marker === undefined);
|
|
554
557
|
});
|
|
555
558
|
}
|
|
559
|
+
/** Write the schema marker (when `version`), descriptor and unit scopes. */
|
|
560
|
+
#stampSchema(version) {
|
|
561
|
+
if (version)
|
|
562
|
+
setMeta(this.#db, LOCAL_SCHEMA_VERSION_KEY, String(this.#schema.version));
|
|
563
|
+
setLocalSchemaDescriptor(this.#db, this.#schema);
|
|
564
|
+
saveUnitScopeSchema(this.#db, this.#schema);
|
|
565
|
+
}
|
|
556
566
|
/**
|
|
557
567
|
* §7.4.3 reset: whole-database local reset EXCEPT the outbox, clientId,
|
|
558
568
|
* leaseState, and the tables and units a bump keeps (equal generations).
|
|
@@ -579,9 +589,7 @@ export class SyncClient {
|
|
|
579
589
|
}
|
|
580
590
|
changed = resetLocalReplica(this.#db, this.#schema, true);
|
|
581
591
|
this.#replayOutbox();
|
|
582
|
-
|
|
583
|
-
setLocalSchemaDescriptor(this.#db, this.#schema);
|
|
584
|
-
saveUnitScopeSchema(this.#db, this.#schema);
|
|
592
|
+
this.#stampSchema(true);
|
|
585
593
|
});
|
|
586
594
|
// Every table the reset dropped changed (I1 eviction-shaped).
|
|
587
595
|
for (const table of changed)
|
|
@@ -650,7 +658,7 @@ export class SyncClient {
|
|
|
650
658
|
/** §2.1 reset after the server reports a different log continuity. */
|
|
651
659
|
#runLogEpochReset(logEpoch) {
|
|
652
660
|
if (getMeta(this.#db, LOG_EPOCH_META_KEY) === undefined) {
|
|
653
|
-
|
|
661
|
+
setMeta(this.#db, LOG_EPOCH_META_KEY, logEpoch);
|
|
654
662
|
this.#emitSyncNeeded('startup');
|
|
655
663
|
return [];
|
|
656
664
|
}
|
|
@@ -762,9 +770,7 @@ export class SyncClient {
|
|
|
762
770
|
/** Host-owned transport policy; the local replica remains available. */
|
|
763
771
|
setTransportEnabled(enabled) {
|
|
764
772
|
this.#requireStarted();
|
|
765
|
-
if (
|
|
766
|
-
throw invalidRequest('transportEnabled must be a boolean');
|
|
767
|
-
if (this.#transportEnabled === enabled)
|
|
773
|
+
if (this.#transportEnabled === transportFlag(enabled))
|
|
768
774
|
return;
|
|
769
775
|
this.#transportEnabled = enabled;
|
|
770
776
|
this.#transportEpoch += 1;
|
|
@@ -901,23 +907,16 @@ export class SyncClient {
|
|
|
901
907
|
return this.#authorityReads.snapshot(this.#db, this.#schema);
|
|
902
908
|
}
|
|
903
909
|
querySnapshot(spec) {
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
...(spec.coverage !== undefined ? { coverage: spec.coverage } : {}),
|
|
912
|
-
...(spec.owner !== undefined ? { owner: spec.owner } : {}),
|
|
913
|
-
});
|
|
914
|
-
const rows = read.queries[0];
|
|
915
|
-
if (read.queries.length !== 1 || rows === undefined) {
|
|
916
|
-
throw new ClientSyncError('client.failed', 'querySnapshot produced no statement result');
|
|
917
|
-
}
|
|
910
|
+
// A statement reads only `sql` and `params`; one statement yields one
|
|
911
|
+
// result.
|
|
912
|
+
const read = this.#readSnapshot(definedObject({
|
|
913
|
+
statements: [spec],
|
|
914
|
+
coverage: spec.coverage,
|
|
915
|
+
owner: spec.owner,
|
|
916
|
+
}));
|
|
918
917
|
return {
|
|
919
918
|
revision: read.revision,
|
|
920
|
-
rows,
|
|
919
|
+
rows: read.queries[0],
|
|
921
920
|
coverage: read.coverage,
|
|
922
921
|
};
|
|
923
922
|
}
|
|
@@ -1130,18 +1129,10 @@ export class SyncClient {
|
|
|
1130
1129
|
#readPreviousVersionRecord() {
|
|
1131
1130
|
return withPreviousVersionContainer(this.#db, readPreviousVersionContainer);
|
|
1132
1131
|
}
|
|
1133
|
-
/** RFC 0005 A2: presence for `statusSnapshot()`, without creating the file. */
|
|
1134
|
-
#previousVersionStatus() {
|
|
1135
|
-
const record = this.#readPreviousVersionRecord();
|
|
1136
|
-
if (record === undefined)
|
|
1137
|
-
return { present: this.#previousVersionExists() };
|
|
1138
|
-
return { present: true, createdAtMs: record.createdAtMs };
|
|
1139
|
-
}
|
|
1140
1132
|
#previousVersionLeaseInactive(nowMs) {
|
|
1141
1133
|
const lease = this.#leaseState;
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
return lease?.expiresAtMs !== undefined && lease.expiresAtMs <= nowMs;
|
|
1134
|
+
return (lease?.errorCode !== undefined ||
|
|
1135
|
+
(lease?.expiresAtMs ?? Infinity) <= nowMs);
|
|
1145
1136
|
}
|
|
1146
1137
|
/**
|
|
1147
1138
|
* RFC 0005 D7: the §7.4.5 replacement-coverage predicate: no active unit
|
|
@@ -1282,12 +1273,13 @@ export class SyncClient {
|
|
|
1282
1273
|
? 'offline'
|
|
1283
1274
|
: round?.status === 'succeeded'
|
|
1284
1275
|
? 'online'
|
|
1285
|
-
: round?.status
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1276
|
+
: round?.status !== 'failed'
|
|
1277
|
+
? 'unknown'
|
|
1278
|
+
: round.causeKind === 'network' || round.causeKind === 'connect'
|
|
1279
|
+
? 'offline'
|
|
1280
|
+
: round.httpStatus !== undefined
|
|
1281
|
+
? 'online'
|
|
1282
|
+
: 'unknown';
|
|
1291
1283
|
return definedObject({
|
|
1292
1284
|
version: CLIENT_DIAGNOSTICS_VERSION,
|
|
1293
1285
|
capturedAtMs,
|
|
@@ -1321,7 +1313,7 @@ export class SyncClient {
|
|
|
1321
1313
|
: [this.#diagnosticCode(unit.reasonCode)])),
|
|
1322
1314
|
].sort(),
|
|
1323
1315
|
},
|
|
1324
|
-
activeUnits: registered.
|
|
1316
|
+
activeUnits: registered.length - units.revoked - units.failed,
|
|
1325
1317
|
replicaCursor: replicaCursor(this.#db),
|
|
1326
1318
|
lastRound: this.#lastRound,
|
|
1327
1319
|
lastChange: this.#lastChange,
|
|
@@ -1347,8 +1339,8 @@ export class SyncClient {
|
|
|
1347
1339
|
}
|
|
1348
1340
|
#diagnosticStorage() {
|
|
1349
1341
|
try {
|
|
1350
|
-
const
|
|
1351
|
-
const
|
|
1342
|
+
const pragma = (name) => Number(this.#db.query(`PRAGMA ${name}`)[0]?.[name] ?? 0);
|
|
1343
|
+
const databaseBytes = pragma('page_count') * pragma('page_size');
|
|
1352
1344
|
// One statement for the outbox and outcome-journal sums keeps the
|
|
1353
1345
|
// snapshot at its pre-version-4 statement count.
|
|
1354
1346
|
const sums = this.#db.query(`SELECT (SELECT COALESCE(SUM(LENGTH(operations)),0) FROM ${OUTBOX_TABLE})AS outbox,COUNT(*)AS entries,COALESCE(SUM(LENGTH(results)+ COALESCE(LENGTH(operations),0)),0)AS bytes FROM ${OUTCOMES_TABLE}`)[0];
|
|
@@ -1359,7 +1351,7 @@ export class SyncClient {
|
|
|
1359
1351
|
const size = (value) => Math.max(0, Number(value ?? 0));
|
|
1360
1352
|
return {
|
|
1361
1353
|
status: pressure ? 'pressure' : 'healthy',
|
|
1362
|
-
databaseBytesApprox: Math.max(0,
|
|
1354
|
+
databaseBytesApprox: Math.max(0, databaseBytes),
|
|
1363
1355
|
pendingOutboxBytesApprox: size(sums?.outbox),
|
|
1364
1356
|
retainedOutcomeBytesApprox: size(sums?.bytes),
|
|
1365
1357
|
retainedOutcomeEntries: size(sums?.entries),
|
|
@@ -1403,6 +1395,8 @@ export class SyncClient {
|
|
|
1403
1395
|
return this.#statusSnapshot();
|
|
1404
1396
|
}
|
|
1405
1397
|
#statusSnapshot(outboxCount) {
|
|
1398
|
+
// RFC 0005 A2: previous-version presence, without creating the file.
|
|
1399
|
+
const record = this.#readPreviousVersionRecord();
|
|
1406
1400
|
return {
|
|
1407
1401
|
currentSchemaVersion: this.#config.schema.version,
|
|
1408
1402
|
outbox: outboxCount ?? countOutbox(this.#db),
|
|
@@ -1410,7 +1404,9 @@ export class SyncClient {
|
|
|
1410
1404
|
leaseState: this.#leaseState,
|
|
1411
1405
|
schemaFloor: this.#schemaFloor,
|
|
1412
1406
|
syncNeeded: this.#needsPull,
|
|
1413
|
-
previousVersionContext:
|
|
1407
|
+
previousVersionContext: record === undefined
|
|
1408
|
+
? { present: this.#previousVersionExists() }
|
|
1409
|
+
: { present: true, createdAtMs: record.createdAtMs },
|
|
1414
1410
|
securityLifecycle: this.#securityLifecycle,
|
|
1415
1411
|
};
|
|
1416
1412
|
}
|
|
@@ -1453,7 +1449,7 @@ export class SyncClient {
|
|
|
1453
1449
|
onRollback?.(error);
|
|
1454
1450
|
throw error;
|
|
1455
1451
|
}
|
|
1456
|
-
|
|
1452
|
+
const publish = () => {
|
|
1457
1453
|
const event = batch.finish(revision, status);
|
|
1458
1454
|
const tables = [...new Set(event.tables.map((entry) => entry.table))];
|
|
1459
1455
|
const windows = [
|
|
@@ -1476,7 +1472,12 @@ export class SyncClient {
|
|
|
1476
1472
|
if (legacy !== undefined)
|
|
1477
1473
|
this.#invalidation.emit(legacy);
|
|
1478
1474
|
this.#emitDiagnostics();
|
|
1479
|
-
}
|
|
1475
|
+
};
|
|
1476
|
+
if (revision !== undefined)
|
|
1477
|
+
if (this.#held)
|
|
1478
|
+
this.#held.push(publish);
|
|
1479
|
+
else
|
|
1480
|
+
publish();
|
|
1480
1481
|
return result;
|
|
1481
1482
|
}
|
|
1482
1483
|
/**
|
|
@@ -1516,24 +1517,23 @@ export class SyncClient {
|
|
|
1516
1517
|
*/
|
|
1517
1518
|
uploadBlob(bytes, options) {
|
|
1518
1519
|
const owned = bytes.slice();
|
|
1519
|
-
return this.#runProtectedAsync(() =>
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
name: options?.name,
|
|
1520
|
+
return this.#runProtectedAsync(async () => {
|
|
1521
|
+
if (this.#config.blobs === undefined) {
|
|
1522
|
+
throw invalidRequest('uploadBlob requires a blob transport');
|
|
1523
|
+
}
|
|
1524
|
+
const blobId = await computeBlobId(owned);
|
|
1525
|
+
const nowMs = this.#now();
|
|
1526
|
+
this.#db.transaction(() => {
|
|
1527
|
+
putCachedBlob(this.#db, blobId, owned, nowMs, options?.mediaType);
|
|
1528
|
+
recordPendingUpload(this.#db, blobId, nowMs, options?.mediaType);
|
|
1529
|
+
});
|
|
1530
|
+
this.#enforceBlobCacheCap();
|
|
1531
|
+
return definedObject({
|
|
1532
|
+
blobId,
|
|
1533
|
+
byteLength: owned.length,
|
|
1534
|
+
mediaType: options?.mediaType,
|
|
1535
|
+
name: options?.name,
|
|
1536
|
+
});
|
|
1537
1537
|
});
|
|
1538
1538
|
}
|
|
1539
1539
|
/** Serialize a BlobRef to the canonical string a `blob_ref` column holds. */
|
|
@@ -1547,60 +1547,59 @@ export class SyncClient {
|
|
|
1547
1547
|
* Accepts a raw `blob_ref` column string or a bare `blobId`.
|
|
1548
1548
|
*/
|
|
1549
1549
|
fetchBlob(blobIdOrRef) {
|
|
1550
|
-
return this.#runProtectedAsync(() =>
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
if (transport === undefined) {
|
|
1564
|
-
throw invalidRequest('fetchBlob requires a blob transport');
|
|
1565
|
-
}
|
|
1566
|
-
// §5.9.5: the authorized endpoint serves bytes inline OR (always-issue,
|
|
1567
|
-
// presign configured) a signed url the client fetches directly. On a url
|
|
1568
|
-
// arm the client MUST NOT attach host auth and MUST NOT fall through:
|
|
1569
|
-
// failure => re-request, the caller's next fetchBlob mints a fresh url.
|
|
1570
|
-
this.#requireTransport();
|
|
1571
|
-
const response = await transport.download(blobId, ref?.byteLength);
|
|
1572
|
-
let bytes;
|
|
1573
|
-
if (response.kind === 'url') {
|
|
1574
|
-
if (transport.fetchUrl === undefined) {
|
|
1575
|
-
throw invalidRequest('blob transport cannot fetch urls');
|
|
1550
|
+
return this.#runProtectedAsync(async () => {
|
|
1551
|
+
// A BlobRef declares the size that caps the download (§5.9.5); a bare
|
|
1552
|
+
// blobId declares none.
|
|
1553
|
+
const ref = blobIdOrRef.startsWith('sha256:')
|
|
1554
|
+
? undefined
|
|
1555
|
+
: parseBlobRef(blobIdOrRef);
|
|
1556
|
+
const blobId = ref?.blobId ?? blobIdOrRef;
|
|
1557
|
+
const cached = getCachedBlob(this.#db, blobId);
|
|
1558
|
+
if (cached !== undefined)
|
|
1559
|
+
return cached;
|
|
1560
|
+
const transport = this.#config.blobs;
|
|
1561
|
+
if (transport === undefined) {
|
|
1562
|
+
throw invalidRequest('fetchBlob requires a blob transport');
|
|
1576
1563
|
}
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1564
|
+
// §5.9.5: the authorized endpoint serves bytes inline OR (always-issue,
|
|
1565
|
+
// presign configured) a signed url the client fetches directly. On a url
|
|
1566
|
+
// arm the client MUST NOT attach host auth and MUST NOT fall through:
|
|
1567
|
+
// failure => re-request, the caller's next fetchBlob mints a fresh url.
|
|
1568
|
+
this.#requireTransport();
|
|
1569
|
+
const response = await transport.download(blobId, ref?.byteLength);
|
|
1570
|
+
let bytes;
|
|
1571
|
+
if (response.kind === 'url') {
|
|
1572
|
+
if (transport.fetchUrl === undefined) {
|
|
1573
|
+
throw invalidRequest('blob transport cannot fetch urls');
|
|
1574
|
+
}
|
|
1575
|
+
if (response.urlExpiresAtMs !== undefined &&
|
|
1576
|
+
response.urlExpiresAtMs <= this.#now()) {
|
|
1577
|
+
// §5.9.5: MUST NOT start a fetch at/past expiry — re-request recovers.
|
|
1578
|
+
throw new ClientSyncError('sync.segment_expired', 'blob url expired before fetch', true);
|
|
1579
|
+
}
|
|
1580
|
+
bytes = await transport.fetchUrl(response.url, ref?.byteLength);
|
|
1581
1581
|
}
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
}
|
|
1603
|
-
return stored;
|
|
1582
|
+
else {
|
|
1583
|
+
bytes = response.bytes;
|
|
1584
|
+
}
|
|
1585
|
+
// §5.9.5 inherits §5.1: the declared size caps the body and the content
|
|
1586
|
+
// address must match. On the url path either failure invalidates the
|
|
1587
|
+
// fetch (no fall-through); the next fetchBlob re-requests the endpoint.
|
|
1588
|
+
if (bytes.byteLength > (ref?.byteLength ?? bytes.byteLength)) {
|
|
1589
|
+
throw invalidRequest('blob exceeds its byteLength');
|
|
1590
|
+
}
|
|
1591
|
+
if ((await computeBlobId(bytes)) !== blobId) {
|
|
1592
|
+
throw invalidRequest('blob content address mismatch');
|
|
1593
|
+
}
|
|
1594
|
+
putCachedBlob(this.#db, blobId, bytes, this.#now());
|
|
1595
|
+
this.#db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
|
1596
|
+
this.#enforceBlobCacheCap();
|
|
1597
|
+
const stored = getCachedBlob(this.#db, blobId);
|
|
1598
|
+
if (stored === undefined) {
|
|
1599
|
+
throw invalidRequest('blob cache write failed');
|
|
1600
|
+
}
|
|
1601
|
+
return stored;
|
|
1602
|
+
});
|
|
1604
1603
|
}
|
|
1605
1604
|
/** §5.9.7 B1: trim the blob cache to the configured cap (no-op if unset). */
|
|
1606
1605
|
#enforceBlobCacheCap() {
|
|
@@ -1977,8 +1976,9 @@ export class SyncClient {
|
|
|
1977
1976
|
if (resolved === undefined) {
|
|
1978
1977
|
throw outcomeNotFound();
|
|
1979
1978
|
}
|
|
1980
|
-
|
|
1981
|
-
this.#
|
|
1979
|
+
const kept = (record) => record.clientCommitId !== input.clientCommitId;
|
|
1980
|
+
this.#conflicts = this.#conflicts.filter(kept);
|
|
1981
|
+
this.#rejections = this.#rejections.filter(kept);
|
|
1982
1982
|
pruneCommitOutcomes(this.#db, this.#outcomeRetentionMaxEntries);
|
|
1983
1983
|
this.#replayOutbox(scope, false);
|
|
1984
1984
|
for (const operation of current.operations ?? [])
|
|
@@ -2178,16 +2178,17 @@ export class SyncClient {
|
|
|
2178
2178
|
const baseKey = windowBaseKey(base);
|
|
2179
2179
|
const wanted = new Set(units);
|
|
2180
2180
|
const live = loadWindowUnits(this.#db, baseKey);
|
|
2181
|
-
// §7.8: a widen registers like `
|
|
2182
|
-
// running round.
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
continue;
|
|
2181
|
+
// §7.8: a widen registers its values like `subscribeMany`, in one
|
|
2182
|
+
// transaction, and never waits for a running round.
|
|
2183
|
+
const added = [...wanted].filter((value) => !live.some((entry) => entry.value === value));
|
|
2184
|
+
if (added.length > 0) {
|
|
2186
2185
|
this.#applyBatch((batch) => {
|
|
2187
|
-
const
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2186
|
+
for (const value of added) {
|
|
2187
|
+
const { id, changed: registered } = this.#holdUnit(windowUnitScopes(base, value), false);
|
|
2188
|
+
this.#db.exec(`INSERT OR REPLACE INTO ${WINDOWS_TABLE}(base,unit,sub_id)VALUES(?,?,?)`, [baseKey, value, id]);
|
|
2189
|
+
this.#needsPull ||= registered;
|
|
2190
|
+
batch.window(baseKey, value);
|
|
2191
|
+
}
|
|
2191
2192
|
batch.status();
|
|
2192
2193
|
});
|
|
2193
2194
|
changed = true;
|
|
@@ -2478,10 +2479,7 @@ export class SyncClient {
|
|
|
2478
2479
|
for (const operation of operations) {
|
|
2479
2480
|
if (operation.op !== 'upsert')
|
|
2480
2481
|
continue;
|
|
2481
|
-
const
|
|
2482
|
-
if (table === undefined)
|
|
2483
|
-
continue;
|
|
2484
|
-
for (const column of table.columns) {
|
|
2482
|
+
for (const column of this.#table(operation.table).columns) {
|
|
2485
2483
|
if (column.type !== 'blob_ref')
|
|
2486
2484
|
continue;
|
|
2487
2485
|
const raw = operation.values?.[column.name];
|
|
@@ -2812,10 +2810,6 @@ export class SyncClient {
|
|
|
2812
2810
|
if (next.errorCode !== undefined)
|
|
2813
2811
|
this.#discardPreviousVersion();
|
|
2814
2812
|
}
|
|
2815
|
-
/** The request-level lease error codes (§7.3.4): stop-and-surface. */
|
|
2816
|
-
#isLeaseErrorCode(code) {
|
|
2817
|
-
return (code === 'sync.auth_lease_required' || code === 'sync.auth_lease_revoked');
|
|
2818
|
-
}
|
|
2819
2813
|
/**
|
|
2820
2814
|
* §7.4.4: encode every pending outbox commit with the current codec. A
|
|
2821
2815
|
* commit that cannot re-encode under the new schema (an
|
|
@@ -2932,7 +2926,7 @@ export class SyncClient {
|
|
|
2932
2926
|
kind,
|
|
2933
2927
|
limit,
|
|
2934
2928
|
size,
|
|
2935
|
-
|
|
2929
|
+
clientCommitId,
|
|
2936
2930
|
}));
|
|
2937
2931
|
}
|
|
2938
2932
|
/**
|
|
@@ -3067,7 +3061,20 @@ export class SyncClient {
|
|
|
3067
3061
|
this.#lastRound = {
|
|
3068
3062
|
status: 'succeeded',
|
|
3069
3063
|
...round(),
|
|
3070
|
-
counters:
|
|
3064
|
+
counters: {
|
|
3065
|
+
pushed: summary.pushed,
|
|
3066
|
+
applied: summary.applied.length,
|
|
3067
|
+
rejected: summary.rejected.length,
|
|
3068
|
+
retryable: summary.retryable.length,
|
|
3069
|
+
conflicts: summary.conflicts.length,
|
|
3070
|
+
commitsApplied: summary.commitsApplied,
|
|
3071
|
+
segmentRowsApplied: summary.segmentRowsApplied,
|
|
3072
|
+
bootstrapping: summary.bootstrapping.length,
|
|
3073
|
+
resets: summary.resets.length,
|
|
3074
|
+
revoked: summary.revoked.length,
|
|
3075
|
+
failed: summary.failed.length,
|
|
3076
|
+
deferredCommits: summary.deferredCommits ?? 0,
|
|
3077
|
+
},
|
|
3071
3078
|
};
|
|
3072
3079
|
this.#emitDiagnostics();
|
|
3073
3080
|
return summary;
|
|
@@ -3130,22 +3137,6 @@ export class SyncClient {
|
|
|
3130
3137
|
this.#endDiagnosticsDeferral();
|
|
3131
3138
|
});
|
|
3132
3139
|
}
|
|
3133
|
-
#diagnosticRoundCounters(summary) {
|
|
3134
|
-
return {
|
|
3135
|
-
pushed: summary.pushed,
|
|
3136
|
-
applied: summary.applied.length,
|
|
3137
|
-
rejected: summary.rejected.length,
|
|
3138
|
-
retryable: summary.retryable.length,
|
|
3139
|
-
conflicts: summary.conflicts.length,
|
|
3140
|
-
commitsApplied: summary.commitsApplied,
|
|
3141
|
-
segmentRowsApplied: summary.segmentRowsApplied,
|
|
3142
|
-
bootstrapping: summary.bootstrapping.length,
|
|
3143
|
-
resets: summary.resets.length,
|
|
3144
|
-
revoked: summary.revoked.length,
|
|
3145
|
-
failed: summary.failed.length,
|
|
3146
|
-
deferredCommits: summary.deferredCommits ?? 0,
|
|
3147
|
-
};
|
|
3148
|
-
}
|
|
3149
3140
|
#diagnosticCode(code) {
|
|
3150
3141
|
return isStableCode(code) ? code : 'client.unknown_failure';
|
|
3151
3142
|
}
|
|
@@ -3200,7 +3191,13 @@ export class SyncClient {
|
|
|
3200
3191
|
echoDigest: new Uint8Array(),
|
|
3201
3192
|
};
|
|
3202
3193
|
const { pushFrames, outbox, frameBytes, deferred: encodedDeferred, } = logEpoch === undefined
|
|
3203
|
-
? {
|
|
3194
|
+
? {
|
|
3195
|
+
pushFrames: [],
|
|
3196
|
+
outbox: [],
|
|
3197
|
+
frameBytes: [],
|
|
3198
|
+
// §2.1: the outbox waits for the next, epoch-bound request.
|
|
3199
|
+
deferred: countOutbox(this.#db),
|
|
3200
|
+
}
|
|
3204
3201
|
: await this.#encodeOutboxForPush(header, pull, byteCap);
|
|
3205
3202
|
// §5.9.7 B4: upload pending blobs BEFORE pushing rows that reference
|
|
3206
3203
|
// them, so the server-side existence check (§6.6) passes.
|
|
@@ -3237,6 +3234,8 @@ export class SyncClient {
|
|
|
3237
3234
|
...(report !== undefined
|
|
3238
3235
|
? [{ type: 'ROUND_FAILURE', code: report }]
|
|
3239
3236
|
: []),
|
|
3237
|
+
// §2.1: a replica without an epoch holds nothing bound to one.
|
|
3238
|
+
...(logEpoch === undefined ? [{ type: 'ADOPT_EPOCH' }] : []),
|
|
3240
3239
|
pull,
|
|
3241
3240
|
...(travels
|
|
3242
3241
|
? registered.map((unit) => ({ type: 'UNIT', scopes: unit.scopes }))
|
|
@@ -3315,9 +3314,18 @@ export class SyncClient {
|
|
|
3315
3314
|
catch (caught) {
|
|
3316
3315
|
// §1.5: a round that ended without a complete, decodable response is a
|
|
3317
3316
|
// failure the server could not see — keep its code for the next request.
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3317
|
+
// An undecodable/truncated body, a rejected fetch, a failed body read,
|
|
3318
|
+
// or a socket round lost mid-flight; an HTTP-status rejection and an
|
|
3319
|
+
// in-band `ERROR` frame were recorded by the server and stay
|
|
3320
|
+
// unreported. The frame carries only codes matching its grammar; a
|
|
3321
|
+
// code that does not match would turn the whole request into a decode
|
|
3322
|
+
// error.
|
|
3323
|
+
if ((caught instanceof DecodeError ||
|
|
3324
|
+
(caught instanceof ClientSyncError &&
|
|
3325
|
+
caught.code === 'sync.transport_failed' &&
|
|
3326
|
+
caught.details?.['causeKind'] !== 'status')) &&
|
|
3327
|
+
isRoundFailureCode(caught.code))
|
|
3328
|
+
this.#roundFailureCode = caught.code;
|
|
3321
3329
|
// §7.8: a round the drive deadline aborted ends with the deadline's
|
|
3322
3330
|
// error, whichever transport call observed the abort.
|
|
3323
3331
|
const error = signal?.aborted ? signal.reason : caught;
|
|
@@ -3326,8 +3334,10 @@ export class SyncClient {
|
|
|
3326
3334
|
// silent retry: the app drives recovery to a live resolver. The
|
|
3327
3335
|
// error may arrive as a ClientSyncError or as a transport error
|
|
3328
3336
|
// carrying the server's `.code` (HTTP-JSON / loopback surface, §1.1).
|
|
3337
|
+
// The request-level lease error codes (§7.3.4).
|
|
3329
3338
|
const code = error.code;
|
|
3330
|
-
if (
|
|
3339
|
+
if (code === 'sync.auth_lease_required' ||
|
|
3340
|
+
code === 'sync.auth_lease_revoked') {
|
|
3331
3341
|
this.#setLeaseState({
|
|
3332
3342
|
...(this.#leaseState ?? {}),
|
|
3333
3343
|
errorCode: code,
|
|
@@ -3396,19 +3406,23 @@ export class SyncClient {
|
|
|
3396
3406
|
!(Number.isFinite(timeoutMs) && timeoutMs > 0))
|
|
3397
3407
|
throw invalidRequest('timeoutMs must be a positive number or Infinity');
|
|
3398
3408
|
const skipEmpty = typeof options === 'object' && options.skipEmpty === true;
|
|
3399
|
-
const
|
|
3409
|
+
const coalesceMs = (typeof options === 'object' && options.coalesceMs) || 0;
|
|
3410
|
+
// NaN and Infinity fail too.
|
|
3411
|
+
if (!(coalesceMs >= 0 && coalesceMs < Number.POSITIVE_INFINITY))
|
|
3412
|
+
throw invalidRequest('coalesceMs must be a non-negative finite number');
|
|
3413
|
+
const key = `${maxRounds ?? 'default'}:${timeoutMs}:${skipEmpty}:${coalesceMs}`;
|
|
3400
3414
|
const pending = this.#syncIdlePending.get(key);
|
|
3401
3415
|
if (pending !== undefined)
|
|
3402
3416
|
return pending;
|
|
3403
3417
|
const previous = this.#syncIdleTail;
|
|
3404
3418
|
let drive;
|
|
3405
3419
|
if (previous === undefined) {
|
|
3406
|
-
drive = this.#syncIdleRounds(maxRounds, timeoutMs, skipEmpty);
|
|
3420
|
+
drive = this.#syncIdleRounds(maxRounds, timeoutMs, skipEmpty, coalesceMs);
|
|
3407
3421
|
}
|
|
3408
3422
|
else {
|
|
3409
3423
|
drive = previous.then(() => {
|
|
3410
3424
|
this.#syncIdlePending.delete(key);
|
|
3411
|
-
return this.#syncIdleRounds(maxRounds, timeoutMs, skipEmpty);
|
|
3425
|
+
return this.#syncIdleRounds(maxRounds, timeoutMs, skipEmpty, coalesceMs);
|
|
3412
3426
|
});
|
|
3413
3427
|
this.#syncIdlePending.set(key, drive);
|
|
3414
3428
|
}
|
|
@@ -3421,7 +3435,7 @@ export class SyncClient {
|
|
|
3421
3435
|
return drive;
|
|
3422
3436
|
}
|
|
3423
3437
|
/** The rounds loop of one idle drive; `syncUntilIdle` owns coalescing. */
|
|
3424
|
-
async #syncIdleRounds(maxRounds, timeoutMs, skipEmpty) {
|
|
3438
|
+
async #syncIdleRounds(maxRounds, timeoutMs, skipEmpty, coalesceMs) {
|
|
3425
3439
|
const budget = maxRounds ?? 20;
|
|
3426
3440
|
const aggregate = {
|
|
3427
3441
|
...emptySummary(0),
|
|
@@ -3434,7 +3448,7 @@ export class SyncClient {
|
|
|
3434
3448
|
return {
|
|
3435
3449
|
...summary,
|
|
3436
3450
|
...(deferredCommits > 0 ? { deferredCommits } : {}),
|
|
3437
|
-
...(
|
|
3451
|
+
...definedObject({ schemaFloor }),
|
|
3438
3452
|
budgetExhausted,
|
|
3439
3453
|
};
|
|
3440
3454
|
};
|
|
@@ -3534,6 +3548,16 @@ export class SyncClient {
|
|
|
3534
3548
|
continue;
|
|
3535
3549
|
return result(false);
|
|
3536
3550
|
}
|
|
3551
|
+
// §8.4: the next round only confirms idle; claims and writes made in
|
|
3552
|
+
// reaction to the data this round applied join it.
|
|
3553
|
+
if (coalesceMs > 0 &&
|
|
3554
|
+
round + 1 < budget &&
|
|
3555
|
+
last.bootstrapping.length === 0 &&
|
|
3556
|
+
!aggregate.deferredCommits &&
|
|
3557
|
+
!this.#needsPull)
|
|
3558
|
+
await new Promise((resolve) => {
|
|
3559
|
+
this.#schedule(resolve, coalesceMs);
|
|
3560
|
+
});
|
|
3537
3561
|
}
|
|
3538
3562
|
return result(true);
|
|
3539
3563
|
}
|
|
@@ -3576,7 +3600,8 @@ export class SyncClient {
|
|
|
3576
3600
|
if (this.#realtimePolicy === 'required') {
|
|
3577
3601
|
// §8.8: the designated realtime path is down. Refuse the round; the
|
|
3578
3602
|
// HTTP binding is not an alternative for a `required` client.
|
|
3579
|
-
|
|
3603
|
+
this.#realtimeRetryDelayMs = this.#retryDelayMs;
|
|
3604
|
+
throw new RealtimeUnavailableError(this.#realtimeState, this.#realtimeReasonCode, this.#retryDelayMs);
|
|
3580
3605
|
}
|
|
3581
3606
|
return Promise.resolve()
|
|
3582
3607
|
.then(() => this.#config.transport(request, signal))
|
|
@@ -3590,25 +3615,6 @@ export class SyncClient {
|
|
|
3590
3615
|
});
|
|
3591
3616
|
});
|
|
3592
3617
|
}
|
|
3593
|
-
/**
|
|
3594
|
-
* §1.5: the code to report for a round that ended without a complete,
|
|
3595
|
-
* decodable response — an undecodable/truncated body, a rejected fetch, a
|
|
3596
|
-
* failed body read, or a socket round lost mid-flight. An HTTP-status
|
|
3597
|
-
* rejection and an in-band `ERROR` frame were recorded by the server and
|
|
3598
|
-
* stay unreported.
|
|
3599
|
-
*/
|
|
3600
|
-
#unseenRoundFailureCode(error) {
|
|
3601
|
-
const code = error instanceof DecodeError
|
|
3602
|
-
? error.code
|
|
3603
|
-
: error instanceof ClientSyncError &&
|
|
3604
|
-
error.code === 'sync.transport_failed' &&
|
|
3605
|
-
error.details?.['causeKind'] !== 'status'
|
|
3606
|
-
? error.code
|
|
3607
|
-
: undefined;
|
|
3608
|
-
// §1.5: the frame carries only codes matching its grammar; a code that
|
|
3609
|
-
// does not match would turn the whole request into a decode error.
|
|
3610
|
-
return code !== undefined && isRoundFailureCode(code) ? code : undefined;
|
|
3611
|
-
}
|
|
3612
3618
|
/** Abort the in-flight socket round (socket closed or disconnected). */
|
|
3613
3619
|
#abortPendingRound(reason) {
|
|
3614
3620
|
const round = this.#pendingRound;
|
|
@@ -3634,10 +3640,6 @@ export class SyncClient {
|
|
|
3634
3640
|
}
|
|
3635
3641
|
this.#emitDiagnostics();
|
|
3636
3642
|
}
|
|
3637
|
-
#realtimeUnavailableError() {
|
|
3638
|
-
this.#realtimeRetryDelayMs = this.#retryDelayMs;
|
|
3639
|
-
return new RealtimeUnavailableError(this.#realtimeState, this.#realtimeReasonCode, this.#retryDelayMs);
|
|
3640
|
-
}
|
|
3641
3643
|
connectRealtime() {
|
|
3642
3644
|
this.#requireActive();
|
|
3643
3645
|
this.#requireTransport();
|
|
@@ -3904,12 +3906,6 @@ export class SyncClient {
|
|
|
3904
3906
|
if (this.#transportEnabled)
|
|
3905
3907
|
this.#socket?.send(JSON.stringify({ type: 'ack', cursor }));
|
|
3906
3908
|
}
|
|
3907
|
-
/** §8.2: after a pull round, ack the replica cursor while it is `>= 0`. */
|
|
3908
|
-
#ackAfterPull() {
|
|
3909
|
-
const cursor = replicaCursor(this.#db);
|
|
3910
|
-
if (this.#socket !== undefined && cursor >= 0)
|
|
3911
|
-
this.#sendAck(cursor);
|
|
3912
|
-
}
|
|
3913
3909
|
// -- response processing ------------------------------------------------------
|
|
3914
3910
|
/**
|
|
3915
3911
|
* Apply one response (§1.6) or realtime delta (§8.2). `sentDigest` is the
|
|
@@ -3957,7 +3953,7 @@ export class SyncClient {
|
|
|
3957
3953
|
const currentLogEpoch = getMeta(this.#db, LOG_EPOCH_META_KEY);
|
|
3958
3954
|
if (header.resetRequired) {
|
|
3959
3955
|
if (mode !== 'pull' || message.frames.length !== 1) {
|
|
3960
|
-
throw invalidHostResponse('a log-epoch reset response
|
|
3956
|
+
throw invalidHostResponse('a log-epoch reset response carries frames after RESP_HEADER');
|
|
3961
3957
|
}
|
|
3962
3958
|
return {
|
|
3963
3959
|
...summary,
|
|
@@ -3965,7 +3961,11 @@ export class SyncClient {
|
|
|
3965
3961
|
bootstrapping: [],
|
|
3966
3962
|
};
|
|
3967
3963
|
}
|
|
3968
|
-
|
|
3964
|
+
// §2.1 adoption: the epoch is stored before any section applies.
|
|
3965
|
+
if (currentLogEpoch === undefined) {
|
|
3966
|
+
setMeta(this.#db, LOG_EPOCH_META_KEY, header.logEpoch);
|
|
3967
|
+
}
|
|
3968
|
+
else if (currentLogEpoch !== header.logEpoch) {
|
|
3969
3969
|
throw invalidHostResponse('the server changed logEpoch without requiring a reset');
|
|
3970
3970
|
}
|
|
3971
3971
|
let section;
|
|
@@ -4031,14 +4031,19 @@ export class SyncClient {
|
|
|
4031
4031
|
}
|
|
4032
4032
|
prefetch();
|
|
4033
4033
|
}
|
|
4034
|
-
//
|
|
4035
|
-
//
|
|
4036
|
-
|
|
4037
|
-
|
|
4034
|
+
// §1.4 rule 4: the frames of unit sections run in coalesced apply
|
|
4035
|
+
// transactions of at most 1,024 rows, each frame or block in its own
|
|
4036
|
+
// savepoint with its own revision, published after the COMMIT; the host
|
|
4037
|
+
// yields to local reads after each COMMIT. Every other frame runs outside
|
|
4038
|
+
// one, and downloads, decryption, images and COMMIT frames are awaited
|
|
4039
|
+
// outside any transaction.
|
|
4040
|
+
const steps = function* () {
|
|
4038
4041
|
for (let index = 1; index < message.frames.length; index += 1) {
|
|
4039
4042
|
const frame = message.frames[index];
|
|
4040
4043
|
if (frame === undefined)
|
|
4041
4044
|
break;
|
|
4045
|
+
if (!/^(SUB|SEGMENT|UNIT)_/.test(frame.type))
|
|
4046
|
+
yield -1;
|
|
4042
4047
|
switch (frame.type) {
|
|
4043
4048
|
case 'RESP_HEADER':
|
|
4044
4049
|
break;
|
|
@@ -4162,9 +4167,10 @@ export class SyncClient {
|
|
|
4162
4167
|
prefetched.delete(frame);
|
|
4163
4168
|
if (pending !== undefined)
|
|
4164
4169
|
reporting = frame;
|
|
4170
|
+
const { scopes } = section.unit;
|
|
4165
4171
|
try {
|
|
4166
|
-
bytes =
|
|
4167
|
-
this.#downloadSegment(frame,
|
|
4172
|
+
bytes = (yield () => pending ??
|
|
4173
|
+
this.#downloadSegment(frame, scopes, (bytesReceived) => this.#progress.update({ bytesReceived }), signal));
|
|
4168
4174
|
}
|
|
4169
4175
|
catch (error) {
|
|
4170
4176
|
// §5.5: access changed since the response. The unit stays
|
|
@@ -4192,12 +4198,12 @@ export class SyncClient {
|
|
|
4192
4198
|
bytesTotal: bytes.byteLength,
|
|
4193
4199
|
rowsTotal: segment.blocks.reduce((n, block) => n + block.length, 0),
|
|
4194
4200
|
});
|
|
4195
|
-
|
|
4201
|
+
yield* this.#applySegmentOrFail(section, summary, image?.table ?? segment.table, (table, clearFirst, effective, owners) => {
|
|
4196
4202
|
const options = this.#segmentApplyOptions(table, clearFirst, effective, owners, image !== undefined
|
|
4197
4203
|
? image.rowCount > 0
|
|
4198
|
-
: segment.blocks.some((block) => block.length > 0), resetEpoch
|
|
4204
|
+
: segment.blocks.some((block) => block.length > 0), resetEpoch);
|
|
4199
4205
|
return image !== undefined
|
|
4200
|
-
? applySqliteSegment(this.#db, this.#schema, table, bytes, image, {
|
|
4206
|
+
? () => applySqliteSegment(this.#db, this.#schema, table, bytes, image, {
|
|
4201
4207
|
...options,
|
|
4202
4208
|
appliedRows,
|
|
4203
4209
|
onChunk: (rows) => recordAppliedRows(this.#db, unitId, image.segmentId, rows),
|
|
@@ -4267,7 +4273,7 @@ export class SyncClient {
|
|
|
4267
4273
|
}));
|
|
4268
4274
|
});
|
|
4269
4275
|
if (changes.length > 0)
|
|
4270
|
-
|
|
4276
|
+
yield () => this.#applyCommit({ ...frame, changes }, summary, mode);
|
|
4271
4277
|
break;
|
|
4272
4278
|
}
|
|
4273
4279
|
case 'LIVE_END': {
|
|
@@ -4355,6 +4361,58 @@ export class SyncClient {
|
|
|
4355
4361
|
if (errorFrame !== undefined)
|
|
4356
4362
|
break;
|
|
4357
4363
|
}
|
|
4364
|
+
}.call(this);
|
|
4365
|
+
this.#beginDiagnosticsDeferral();
|
|
4366
|
+
try {
|
|
4367
|
+
for (let step = steps.next(); !step.done;) {
|
|
4368
|
+
const work = step.value;
|
|
4369
|
+
if (typeof work === 'function') {
|
|
4370
|
+
try {
|
|
4371
|
+
step = steps.next(await work());
|
|
4372
|
+
}
|
|
4373
|
+
catch (error) {
|
|
4374
|
+
step = steps.throw(error);
|
|
4375
|
+
}
|
|
4376
|
+
}
|
|
4377
|
+
else if (work < 0)
|
|
4378
|
+
step = steps.next();
|
|
4379
|
+
else {
|
|
4380
|
+
const held = (this.#held = []);
|
|
4381
|
+
const rejections = this.#rejections.length;
|
|
4382
|
+
let failure;
|
|
4383
|
+
try {
|
|
4384
|
+
failure = this.#db.transaction(() => {
|
|
4385
|
+
try {
|
|
4386
|
+
for (let rows = work;;) {
|
|
4387
|
+
step = steps.next();
|
|
4388
|
+
const next = step.value;
|
|
4389
|
+
if (typeof next !== 'number' ||
|
|
4390
|
+
next < 0 ||
|
|
4391
|
+
(rows > 0 && rows + next > 1024))
|
|
4392
|
+
return undefined;
|
|
4393
|
+
rows += next;
|
|
4394
|
+
}
|
|
4395
|
+
}
|
|
4396
|
+
catch (error) {
|
|
4397
|
+
// §1.4 rule 5: the frames before the failing one commit.
|
|
4398
|
+
return { error };
|
|
4399
|
+
}
|
|
4400
|
+
});
|
|
4401
|
+
}
|
|
4402
|
+
catch (error) {
|
|
4403
|
+
this.#rejections.length = rejections;
|
|
4404
|
+
throw error;
|
|
4405
|
+
}
|
|
4406
|
+
finally {
|
|
4407
|
+
this.#held = undefined;
|
|
4408
|
+
}
|
|
4409
|
+
for (const publish of held)
|
|
4410
|
+
publish();
|
|
4411
|
+
if (failure)
|
|
4412
|
+
throw failure.error;
|
|
4413
|
+
await yieldToHost();
|
|
4414
|
+
}
|
|
4415
|
+
}
|
|
4358
4416
|
}
|
|
4359
4417
|
finally {
|
|
4360
4418
|
this.#endDiagnosticsDeferral();
|
|
@@ -4370,7 +4428,10 @@ export class SyncClient {
|
|
|
4370
4428
|
}
|
|
4371
4429
|
}
|
|
4372
4430
|
else {
|
|
4373
|
-
|
|
4431
|
+
// §8.2: after a pull round, ack the replica cursor while it is `>= 0`.
|
|
4432
|
+
const cursor = replicaCursor(this.#db);
|
|
4433
|
+
if (this.#socket !== undefined && cursor >= 0)
|
|
4434
|
+
this.#sendAck(cursor);
|
|
4374
4435
|
}
|
|
4375
4436
|
const catchingUp = loadUnits(this.#db)
|
|
4376
4437
|
.filter((unit) => unit.status === 'active' && unit.cursor !== undefined)
|
|
@@ -4530,7 +4591,23 @@ export class SyncClient {
|
|
|
4530
4591
|
}
|
|
4531
4592
|
async #applyCommit(frame, summary, source) {
|
|
4532
4593
|
await applyCommitFrame(this.#db, this.#schema, frame, this.#encryption, (fn) => this.#applyBatch((batch) => {
|
|
4533
|
-
|
|
4594
|
+
// Record before + after scope keys while the transaction is open.
|
|
4595
|
+
for (const change of frame.changes) {
|
|
4596
|
+
const tableName = frame.tables[change.tableIndex];
|
|
4597
|
+
if (tableName === undefined)
|
|
4598
|
+
continue;
|
|
4599
|
+
const table = this.#schema.tables.get(tableName);
|
|
4600
|
+
if (table === undefined)
|
|
4601
|
+
continue;
|
|
4602
|
+
let precise = this.#recordStoredRowScopes(batch, table, change.rowId);
|
|
4603
|
+
for (const variable of Object.keys(change.scopes)) {
|
|
4604
|
+
if (table.scopePrefixByVariable.has(variable))
|
|
4605
|
+
precise = true;
|
|
4606
|
+
}
|
|
4607
|
+
batch.changeScopes(table, change.scopes);
|
|
4608
|
+
if (!precise)
|
|
4609
|
+
batch.table(tableName);
|
|
4610
|
+
}
|
|
4534
4611
|
const result = fn();
|
|
4535
4612
|
this.#replayOutbox(result, false);
|
|
4536
4613
|
for (const table of new Set(frame.changes.map((change) => frame.tables[change.tableIndex]))) {
|
|
@@ -4545,25 +4622,6 @@ export class SyncClient {
|
|
|
4545
4622
|
}));
|
|
4546
4623
|
summary.commitsApplied += 1;
|
|
4547
4624
|
}
|
|
4548
|
-
/** Record before + after scope keys while the commit transaction is open. */
|
|
4549
|
-
#recordCommitChanges(batch, frame) {
|
|
4550
|
-
for (const change of frame.changes) {
|
|
4551
|
-
const tableName = frame.tables[change.tableIndex];
|
|
4552
|
-
if (tableName === undefined)
|
|
4553
|
-
continue;
|
|
4554
|
-
const table = this.#schema.tables.get(tableName);
|
|
4555
|
-
if (table === undefined)
|
|
4556
|
-
continue;
|
|
4557
|
-
let precise = this.#recordStoredRowScopes(batch, table, change.rowId);
|
|
4558
|
-
for (const variable of Object.keys(change.scopes)) {
|
|
4559
|
-
if (table.scopePrefixByVariable.has(variable))
|
|
4560
|
-
precise = true;
|
|
4561
|
-
}
|
|
4562
|
-
batch.changeScopes(table, change.scopes);
|
|
4563
|
-
if (!precise)
|
|
4564
|
-
batch.table(tableName);
|
|
4565
|
-
}
|
|
4566
|
-
}
|
|
4567
4625
|
/** Add the currently materialized row's scope keys; returns whether known. */
|
|
4568
4626
|
#recordStoredRowScopes(batch, table, rowId) {
|
|
4569
4627
|
const mappings = [...table.scopeColumnByVariable].filter(([variable]) => table.scopePrefixByVariable.has(variable));
|
|
@@ -4590,7 +4648,7 @@ export class SyncClient {
|
|
|
4590
4648
|
this.#db.query(`SELECT 1 FROM ${quoteIdent(table.name)} WHERE ${scope.sql} LIMIT 1`, scope.params).length > 0);
|
|
4591
4649
|
}
|
|
4592
4650
|
/** Shared transaction, reset fence and progress for both segment formats. */
|
|
4593
|
-
#segmentApplyOptions(table, clearFirst, effective, owners, hasRows, resetEpoch
|
|
4651
|
+
#segmentApplyOptions(table, clearFirst, effective, owners, hasRows, resetEpoch) {
|
|
4594
4652
|
let writeMs = 0;
|
|
4595
4653
|
let commitMs = 0;
|
|
4596
4654
|
return {
|
|
@@ -4615,14 +4673,13 @@ export class SyncClient {
|
|
|
4615
4673
|
written = performance.now();
|
|
4616
4674
|
return result;
|
|
4617
4675
|
});
|
|
4618
|
-
// §7.7:
|
|
4676
|
+
// §7.7: a payload reports its import cost.
|
|
4619
4677
|
writeMs += written - started;
|
|
4620
4678
|
commitMs += performance.now() - written;
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
});
|
|
4679
|
+
this.#progress.update({
|
|
4680
|
+
importWriteMs: Math.round(writeMs),
|
|
4681
|
+
importCommitMs: Math.round(commitMs),
|
|
4682
|
+
});
|
|
4626
4683
|
return result;
|
|
4627
4684
|
},
|
|
4628
4685
|
};
|
|
@@ -4633,7 +4690,7 @@ export class SyncClient {
|
|
|
4633
4690
|
* pair's scope first (§5.6). A fail-closed error (no local scope-column
|
|
4634
4691
|
* mapping) marks the unit `failed` without failing the whole response.
|
|
4635
4692
|
*/
|
|
4636
|
-
|
|
4693
|
+
*#applySegmentOrFail(section, summary, tableName, apply) {
|
|
4637
4694
|
const { unit } = section;
|
|
4638
4695
|
const table = this.#table(tableName);
|
|
4639
4696
|
const effective = covers(table, unit.scopes)
|
|
@@ -4652,10 +4709,12 @@ export class SyncClient {
|
|
|
4652
4709
|
unit.id,
|
|
4653
4710
|
]);
|
|
4654
4711
|
try {
|
|
4655
|
-
|
|
4712
|
+
const work = apply(table, clearFirst, effective, () => (owners ??=
|
|
4656
4713
|
this.#db.query(`SELECT 1 FROM ${RETAINED_ROWS} WHERE tbl=? LIMIT 1`, [table.name]).length > 0 || this.#scopedRowsExist(table, effective, [])
|
|
4657
4714
|
? unitOwners(loadUnits(this.#db), table, unit.id)
|
|
4658
4715
|
: []));
|
|
4716
|
+
summary.segmentRowsApplied +=
|
|
4717
|
+
typeof work === 'function' ? (yield work) : yield* work;
|
|
4659
4718
|
section.seeded.add(tableName);
|
|
4660
4719
|
}
|
|
4661
4720
|
catch (error) {
|
|
@@ -4713,7 +4772,7 @@ export class SyncClient {
|
|
|
4713
4772
|
]))),
|
|
4714
4773
|
byteLength: frame.byteLength,
|
|
4715
4774
|
onProgress,
|
|
4716
|
-
...(
|
|
4775
|
+
...definedObject({ signal }),
|
|
4717
4776
|
});
|
|
4718
4777
|
}
|
|
4719
4778
|
// §5.4: the descriptor's byteLength caps the body; §5.1: verify the
|
|
@@ -4922,20 +4981,6 @@ export class SyncClient {
|
|
|
4922
4981
|
const values = table.columns.map((column) => jsonToRowValue(image.values?.[column.name] ?? null));
|
|
4923
4982
|
upsertLocalRow(this.#db, table, values, image.syncVersion);
|
|
4924
4983
|
}
|
|
4925
|
-
#legacyUndoOptimisticRows(commit, batch) {
|
|
4926
|
-
for (const operation of commit.operations) {
|
|
4927
|
-
if (operation.op !== 'upsert')
|
|
4928
|
-
continue;
|
|
4929
|
-
const table = this.#schema.tables.get(operation.table);
|
|
4930
|
-
if (table === undefined)
|
|
4931
|
-
continue;
|
|
4932
|
-
const row = this.#rowById(table, operation.rowId, `${quoteIdent(SYNC_VERSION_COLUMN)} AS v`);
|
|
4933
|
-
if (row !== undefined && row.v === OPTIMISTIC_VERSION) {
|
|
4934
|
-
batch.table(table.name);
|
|
4935
|
-
deleteLocalRow(this.#db, table, operation.rowId);
|
|
4936
|
-
}
|
|
4937
|
-
}
|
|
4938
|
-
}
|
|
4939
4984
|
#rollbackFailedCommit(commit, batch, classifiedFailures) {
|
|
4940
4985
|
const images = listOutboxBeforeImages(this.#db, commit.clientCommitId);
|
|
4941
4986
|
const imageByIndex = new Map(images.map((image) => [image.opIndex, image]));
|
|
@@ -4953,7 +4998,16 @@ export class SyncClient {
|
|
|
4953
4998
|
else {
|
|
4954
4999
|
// Pending commits written by older clients have no before-images.
|
|
4955
5000
|
// Preserve the old fail-closed behavior rather than guessing a base.
|
|
4956
|
-
|
|
5001
|
+
for (const operation of commit.operations) {
|
|
5002
|
+
if (operation.op !== 'upsert')
|
|
5003
|
+
continue;
|
|
5004
|
+
const table = this.#schema.tables.get(operation.table);
|
|
5005
|
+
if (table !== undefined &&
|
|
5006
|
+
this.#rowById(table, operation.rowId, `${quoteIdent(SYNC_VERSION_COLUMN)} AS v`)?.v === OPTIMISTIC_VERSION) {
|
|
5007
|
+
batch.table(table.name);
|
|
5008
|
+
deleteLocalRow(this.#db, table, operation.rowId);
|
|
5009
|
+
}
|
|
5010
|
+
}
|
|
4957
5011
|
}
|
|
4958
5012
|
deleteOutboxCommit(this.#db, commit.clientCommitId);
|
|
4959
5013
|
if (!complete)
|
|
@@ -5231,10 +5285,7 @@ export class SyncClient {
|
|
|
5231
5285
|
function requireRowId(value) {
|
|
5232
5286
|
if (typeof value === 'string' && value.length > 0)
|
|
5233
5287
|
return value;
|
|
5234
|
-
if (typeof value === '
|
|
5235
|
-
return String(value);
|
|
5236
|
-
}
|
|
5237
|
-
if (typeof value === 'boolean')
|
|
5288
|
+
if (Number.isSafeInteger(value) || typeof value === 'boolean')
|
|
5238
5289
|
return String(value);
|
|
5239
5290
|
throw invalidRequest('a mutation requires a non-empty primary key');
|
|
5240
5291
|
}
|