@syncular/server 0.58.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -22
- package/dist/admin.js +8 -4
- package/dist/blob-handlers.d.ts +25 -31
- package/dist/blob-handlers.js +63 -41
- package/dist/blob-store.d.ts +42 -24
- package/dist/blob-store.js +0 -0
- package/dist/bucket-blob-store.d.ts +12 -8
- package/dist/bucket-blob-store.js +54 -45
- package/dist/bucket-segment-store.js +18 -16
- package/dist/content-encoding.d.ts +5 -2
- package/dist/content-encoding.js +42 -13
- package/dist/context.d.ts +16 -0
- package/dist/context.js +21 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +223 -1111
- package/dist/errors.js +8 -0
- package/dist/events.d.ts +1 -1
- package/dist/handler.js +135 -103
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +17 -3
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- package/dist/pg-executor-pglite.d.ts +5 -6
- package/dist/pg-executor-pglite.js +22 -14
- package/dist/pg-executor.d.ts +4 -1
- package/dist/postgres-storage.d.ts +1 -1
- package/dist/postgres-storage.js +182 -105
- package/dist/pull.d.ts +28 -20
- package/dist/pull.js +85 -87
- package/dist/push.js +272 -147
- package/dist/realtime.d.ts +68 -28
- package/dist/realtime.js +297 -136
- package/dist/relational-rows.d.ts +27 -15
- package/dist/relational-rows.js +38 -22
- package/dist/s3-blob-store.d.ts +11 -12
- package/dist/s3-blob-store.js +20 -13
- package/dist/s3-object-bucket.d.ts +8 -1
- package/dist/s3-object-bucket.js +16 -3
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +23 -0
- package/dist/scopes.d.ts +6 -2
- package/dist/scopes.js +22 -4
- package/dist/seed.js +1 -2
- package/dist/segment-download.js +18 -4
- package/dist/signed-url.d.ts +2 -2
- package/dist/signed-url.js +9 -2
- package/dist/sigv4.d.ts +8 -2
- package/dist/sigv4.js +8 -4
- package/dist/sqlite-blob-store.d.ts +1 -1
- package/dist/sqlite-blob-store.js +23 -22
- package/dist/sqlite-dialect.d.ts +150 -47
- package/dist/sqlite-dialect.js +987 -88
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +293 -1117
- package/dist/storage.d.ts +32 -12
- package/package.json +2 -2
- package/src/admin.ts +16 -2
- package/src/blob-handlers.ts +91 -72
- package/src/blob-store.ts +0 -0
- package/src/bucket-blob-store.ts +75 -48
- package/src/bucket-segment-store.ts +20 -15
- package/src/content-encoding.ts +48 -16
- package/src/context.ts +32 -1
- package/src/d1-storage.ts +326 -1692
- package/src/errors.ts +8 -0
- package/src/events.ts +1 -1
- package/src/handler.ts +178 -127
- package/src/local-authority.ts +17 -3
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +231 -138
- package/src/pull.ts +133 -147
- package/src/push.ts +356 -205
- package/src/realtime.ts +380 -179
- package/src/relational-rows.ts +54 -27
- package/src/s3-blob-store.ts +23 -13
- package/src/s3-object-bucket.ts +20 -2
- package/src/schema.ts +31 -0
- package/src/scopes.ts +23 -3
- package/src/seed.ts +1 -2
- package/src/segment-download.ts +25 -4
- package/src/signed-url.ts +10 -4
- package/src/sigv4.ts +17 -4
- package/src/sqlite-blob-store.ts +27 -26
- package/src/sqlite-dialect.ts +1607 -114
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +360 -1811
- package/src/storage.ts +33 -12
package/src/push.ts
CHANGED
|
@@ -99,6 +99,8 @@ interface StagedWrite {
|
|
|
99
99
|
readonly opIndex: number;
|
|
100
100
|
/** Final post-merge, post-scope-strip values (upsert only). */
|
|
101
101
|
readonly values?: readonly RowValue[];
|
|
102
|
+
/** Columns the operation presents (upsert only, §6.11 present reference). */
|
|
103
|
+
readonly present?: readonly boolean[];
|
|
102
104
|
/** Stored row observed immediately before the write; undefined on insert. */
|
|
103
105
|
readonly stored: StoredRow | undefined;
|
|
104
106
|
}
|
|
@@ -188,6 +190,26 @@ function conflictColumnsBitmap(
|
|
|
188
190
|
interface BlobApplyContext {
|
|
189
191
|
readonly store: BlobStore | undefined;
|
|
190
192
|
readonly partition: string;
|
|
193
|
+
/** §6.6 per-commit existence answers, keyed by blobId. */
|
|
194
|
+
readonly presence: Map<string, boolean>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** §6.6: answer every not-yet-known blobId with one concurrent round of
|
|
198
|
+
* existence lookups and remember the answers for the rest of the commit. */
|
|
199
|
+
async function resolveBlobPresence(
|
|
200
|
+
blobCtx: BlobApplyContext,
|
|
201
|
+
blobIds: Iterable<string>,
|
|
202
|
+
): Promise<void> {
|
|
203
|
+
const store = blobCtx.store;
|
|
204
|
+
if (store === undefined) return;
|
|
205
|
+
const unknown = [...new Set(blobIds)].filter(
|
|
206
|
+
(blobId) => !blobCtx.presence.has(blobId),
|
|
207
|
+
);
|
|
208
|
+
await Promise.all(
|
|
209
|
+
unknown.map(async (blobId) => {
|
|
210
|
+
blobCtx.presence.set(blobId, await store.has(blobCtx.partition, blobId));
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
191
213
|
}
|
|
192
214
|
|
|
193
215
|
/**
|
|
@@ -339,6 +361,10 @@ async function applyOperation(
|
|
|
339
361
|
'scope resolution failed (§3.4 step 4)',
|
|
340
362
|
);
|
|
341
363
|
}
|
|
364
|
+
if (op.rowId.includes('\u0000')) {
|
|
365
|
+
// §6.1: no store sees a rowId holding U+0000; PostgreSQL refuses it.
|
|
366
|
+
return errorRecord(opIndex, 'sync.invalid_request', 'rowId holds U+0000');
|
|
367
|
+
}
|
|
342
368
|
const stored = await tx.getRow(op.table, op.rowId);
|
|
343
369
|
|
|
344
370
|
if (op.op === 'rescope') {
|
|
@@ -538,6 +564,7 @@ async function applyOperation(
|
|
|
538
564
|
rowId: op.rowId,
|
|
539
565
|
opIndex,
|
|
540
566
|
values,
|
|
567
|
+
present: table.columns.map(() => true),
|
|
541
568
|
stored,
|
|
542
569
|
},
|
|
543
570
|
};
|
|
@@ -608,6 +635,7 @@ async function applyOperation(
|
|
|
608
635
|
table: op.table,
|
|
609
636
|
rowId: op.rowId,
|
|
610
637
|
op: 'delete',
|
|
638
|
+
deletedVersion: stored.serverVersion,
|
|
611
639
|
scopes: stored.scopes,
|
|
612
640
|
},
|
|
613
641
|
],
|
|
@@ -835,20 +863,41 @@ async function applyOperation(
|
|
|
835
863
|
nextServerVersion: newVersion,
|
|
836
864
|
},
|
|
837
865
|
],
|
|
838
|
-
staged: {
|
|
866
|
+
staged: {
|
|
867
|
+
table,
|
|
868
|
+
op: 'upsert',
|
|
869
|
+
rowId: op.rowId,
|
|
870
|
+
opIndex,
|
|
871
|
+
values,
|
|
872
|
+
present,
|
|
873
|
+
stored,
|
|
874
|
+
},
|
|
839
875
|
};
|
|
840
876
|
}
|
|
841
877
|
|
|
842
|
-
// Insert path: no stored row (§5.2).
|
|
878
|
+
// Insert path: no stored row (§5.2). The authorization row is the payload
|
|
879
|
+
// plus the primary key (§3.4 step 2); a missing or empty scope column value
|
|
880
|
+
// ⇒ deny.
|
|
843
881
|
const values: RowValue[] = sparse.map((value) => value ?? null);
|
|
882
|
+
const extracted = storedScopesForRow(table, values);
|
|
883
|
+
const insert =
|
|
884
|
+
'missing' in extracted
|
|
885
|
+
? errorRecord(
|
|
886
|
+
opIndex,
|
|
887
|
+
'sync.forbidden',
|
|
888
|
+
'insert missing scope column value (§3.4)',
|
|
889
|
+
)
|
|
890
|
+
: authorizeWrite(table, extracted.scopes, resolved)
|
|
891
|
+
? extracted
|
|
892
|
+
: errorRecord(
|
|
893
|
+
opIndex,
|
|
894
|
+
'sync.forbidden',
|
|
895
|
+
'insert denied by scope authorization (§3.4)',
|
|
896
|
+
);
|
|
844
897
|
if (op.baseVersion !== undefined && op.baseVersion !== 0) {
|
|
845
898
|
// Authorize the payload first so absence is not disclosed to actors
|
|
846
899
|
// without the scope; then §6.2: baseVersion ≠ 0, row absent.
|
|
847
|
-
|
|
848
|
-
if (
|
|
849
|
-
'missing' in extractedFirst ||
|
|
850
|
-
!authorizeWrite(table, extractedFirst.scopes, resolved)
|
|
851
|
-
) {
|
|
900
|
+
if ('kind' in insert) {
|
|
852
901
|
return errorRecord(
|
|
853
902
|
opIndex,
|
|
854
903
|
'sync.forbidden',
|
|
@@ -861,17 +910,31 @@ async function applyOperation(
|
|
|
861
910
|
'upsert with baseVersion targets an absent row (§6.2)',
|
|
862
911
|
);
|
|
863
912
|
}
|
|
864
|
-
|
|
913
|
+
// §6.2: a payload that presents every scope column is authorized before
|
|
914
|
+
// the tombstone read, so the answer never discloses a deletion to an actor
|
|
915
|
+
// who could not write the row.
|
|
916
|
+
if (
|
|
917
|
+
op.baseVersion === undefined &&
|
|
918
|
+
'kind' in insert &&
|
|
919
|
+
table.scopePatterns.every((pattern) => present[pattern.columnIndex])
|
|
920
|
+
) {
|
|
921
|
+
return insert;
|
|
922
|
+
}
|
|
923
|
+
// §6.2: a tombstone whose stored scopes the actor fails reads as absent.
|
|
924
|
+
const tombstone = await tx.getTombstone(op.table, op.rowId);
|
|
925
|
+
const deleted =
|
|
926
|
+
tombstone?.scopes === undefined ||
|
|
927
|
+
authorizeWrite(table, tombstone.scopes, resolved)
|
|
928
|
+
? tombstone
|
|
929
|
+
: undefined;
|
|
930
|
+
if (op.baseVersion === undefined && deleted !== undefined) {
|
|
865
931
|
// §5.2: a delete within the retention horizon beats an unversioned
|
|
866
932
|
// upsert; an explicit insert (baseVersion 0) recreates instead.
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)',
|
|
873
|
-
);
|
|
874
|
-
}
|
|
933
|
+
return errorRecord(
|
|
934
|
+
opIndex,
|
|
935
|
+
'sync.row_deleted',
|
|
936
|
+
'a deleted row rejects an unversioned upsert within the retention horizon (§5.2)',
|
|
937
|
+
);
|
|
875
938
|
}
|
|
876
939
|
if (presentCount < table.columns.length) {
|
|
877
940
|
// §6.3: an insert requires every column present.
|
|
@@ -881,22 +944,7 @@ async function applyOperation(
|
|
|
881
944
|
'a partial payload targets an absent row (§6.3)',
|
|
882
945
|
);
|
|
883
946
|
}
|
|
884
|
-
|
|
885
|
-
if ('missing' in extracted) {
|
|
886
|
-
// §3.4 step 2: a missing or empty scope column value ⇒ deny.
|
|
887
|
-
return errorRecord(
|
|
888
|
-
opIndex,
|
|
889
|
-
'sync.forbidden',
|
|
890
|
-
`insert missing scope column value for ${JSON.stringify(extracted.missing)} (§3.4)`,
|
|
891
|
-
);
|
|
892
|
-
}
|
|
893
|
-
if (!authorizeWrite(table, extracted.scopes, resolved)) {
|
|
894
|
-
return errorRecord(
|
|
895
|
-
opIndex,
|
|
896
|
-
'sync.forbidden',
|
|
897
|
-
'insert denied by scope authorization (§3.4)',
|
|
898
|
-
);
|
|
899
|
-
}
|
|
947
|
+
if ('kind' in insert) return insert;
|
|
900
948
|
// §5.10.3: on insert a crdt column merges against the empty document
|
|
901
949
|
// (stored = null) — normalizes the initial state through the merger.
|
|
902
950
|
const insertMerge = await mergeCrdtColumns(
|
|
@@ -937,16 +985,15 @@ async function applyOperation(
|
|
|
937
985
|
actorId,
|
|
938
986
|
);
|
|
939
987
|
if (insertReject !== undefined) return insertReject;
|
|
940
|
-
// §
|
|
941
|
-
//
|
|
942
|
-
//
|
|
943
|
-
if (
|
|
944
|
-
|
|
945
|
-
}
|
|
988
|
+
// §6.2: an insert recreates over the tombstone and starts above the
|
|
989
|
+
// deleted row's version; without a column-versions blob every column sits
|
|
990
|
+
// at that version.
|
|
991
|
+
if (tombstone !== undefined) await tx.clearTombstone(op.table, op.rowId);
|
|
992
|
+
const serverVersion = (deleted?.serverVersion ?? 0) + 1;
|
|
946
993
|
const newRow = {
|
|
947
994
|
rowId: op.rowId,
|
|
948
|
-
serverVersion
|
|
949
|
-
scopes:
|
|
995
|
+
serverVersion,
|
|
996
|
+
scopes: insert.scopes,
|
|
950
997
|
payload: insertPayload,
|
|
951
998
|
};
|
|
952
999
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -957,8 +1004,8 @@ async function applyOperation(
|
|
|
957
1004
|
table: op.table,
|
|
958
1005
|
rowId: op.rowId,
|
|
959
1006
|
op: 'upsert',
|
|
960
|
-
rowVersion:
|
|
961
|
-
scopes:
|
|
1007
|
+
rowVersion: serverVersion,
|
|
1008
|
+
scopes: insert.scopes,
|
|
962
1009
|
payload: insertPayload,
|
|
963
1010
|
},
|
|
964
1011
|
],
|
|
@@ -970,7 +1017,7 @@ async function applyOperation(
|
|
|
970
1017
|
rowId: op.rowId,
|
|
971
1018
|
row: toValidateRow(table.columns, values),
|
|
972
1019
|
stored: undefined,
|
|
973
|
-
nextServerVersion:
|
|
1020
|
+
nextServerVersion: serverVersion,
|
|
974
1021
|
},
|
|
975
1022
|
],
|
|
976
1023
|
staged: {
|
|
@@ -979,6 +1026,7 @@ async function applyOperation(
|
|
|
979
1026
|
rowId: op.rowId,
|
|
980
1027
|
opIndex,
|
|
981
1028
|
values,
|
|
1029
|
+
present,
|
|
982
1030
|
stored: undefined,
|
|
983
1031
|
},
|
|
984
1032
|
};
|
|
@@ -1009,14 +1057,15 @@ async function checkAndRecordBlobs(
|
|
|
1009
1057
|
'row references a blob but the server has no blob store (§5.9.6)',
|
|
1010
1058
|
);
|
|
1011
1059
|
}
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1060
|
+
// Pushed blobIds were answered before the operation loop; a blobId the
|
|
1061
|
+
// row keeps from its stored values is answered here.
|
|
1062
|
+
await resolveBlobPresence(blobCtx, blobIds);
|
|
1063
|
+
if (blobIds.some((blobId) => blobCtx.presence.get(blobId) !== true)) {
|
|
1064
|
+
return errorRecord(
|
|
1065
|
+
opIndex,
|
|
1066
|
+
'blob.not_found',
|
|
1067
|
+
'push references a blob that has not been uploaded (§5.9.6)',
|
|
1068
|
+
);
|
|
1020
1069
|
}
|
|
1021
1070
|
}
|
|
1022
1071
|
// Update the reference index for this row (empty set clears it, §5.9.4).
|
|
@@ -1295,16 +1344,29 @@ async function enforceReferences(
|
|
|
1295
1344
|
const operations: ValidateCommitOperation[] = [];
|
|
1296
1345
|
const originByIndex: number[] = [];
|
|
1297
1346
|
|
|
1347
|
+
// A reference column counts as present when any staged upsert of the row
|
|
1348
|
+
// presents it; its value is the row's final value (§6.11).
|
|
1298
1349
|
const finalByRow = new Map<
|
|
1299
1350
|
string,
|
|
1300
|
-
{
|
|
1351
|
+
{
|
|
1352
|
+
readonly first: StagedWrite;
|
|
1353
|
+
readonly last: StagedWrite;
|
|
1354
|
+
readonly presentReferences: ReadonlySet<number>;
|
|
1355
|
+
}
|
|
1301
1356
|
>();
|
|
1302
1357
|
for (const write of staged) {
|
|
1303
1358
|
const rowKey = `${write.table.name}\u0000${write.rowId}`;
|
|
1304
1359
|
const existing = finalByRow.get(rowKey);
|
|
1360
|
+
const presentReferences = new Set(existing?.presentReferences);
|
|
1361
|
+
for (const reference of write.table.references) {
|
|
1362
|
+
if (write.present?.[reference.columnIndex] === true) {
|
|
1363
|
+
presentReferences.add(reference.columnIndex);
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1305
1366
|
finalByRow.set(rowKey, {
|
|
1306
1367
|
first: existing?.first ?? write,
|
|
1307
1368
|
last: write,
|
|
1369
|
+
presentReferences,
|
|
1308
1370
|
});
|
|
1309
1371
|
}
|
|
1310
1372
|
|
|
@@ -1313,9 +1375,10 @@ async function enforceReferences(
|
|
|
1313
1375
|
// combatants, its chronicle) reads that parent once. Postgres binds the
|
|
1314
1376
|
// ids as one array per table; SQLite and D1 answer with one lookup per id.
|
|
1315
1377
|
const parentIds = new Map<string, Set<string>>();
|
|
1316
|
-
for (const { last: write } of finalByRow.values()) {
|
|
1378
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
1317
1379
|
if (write.op !== 'upsert' || write.values === undefined) continue;
|
|
1318
1380
|
for (const reference of write.table.references) {
|
|
1381
|
+
if (!presentReferences.has(reference.columnIndex)) continue;
|
|
1319
1382
|
const value = write.values[reference.columnIndex];
|
|
1320
1383
|
if (value === null || value === undefined) continue;
|
|
1321
1384
|
const parentRowId = renderScopeValue(value);
|
|
@@ -1334,14 +1397,23 @@ async function enforceReferences(
|
|
|
1334
1397
|
);
|
|
1335
1398
|
}
|
|
1336
1399
|
|
|
1337
|
-
for (const { last: write } of finalByRow.values()) {
|
|
1400
|
+
for (const { last: write, presentReferences } of finalByRow.values()) {
|
|
1338
1401
|
if (write.op !== 'upsert' || write.values === undefined) continue;
|
|
1339
1402
|
for (const reference of write.table.references) {
|
|
1403
|
+
if (!presentReferences.has(reference.columnIndex)) continue;
|
|
1340
1404
|
const value = write.values[reference.columnIndex];
|
|
1341
1405
|
if (value === null || value === undefined) continue;
|
|
1342
1406
|
const parentRowId = renderScopeValue(value);
|
|
1343
1407
|
if (parentRowId === undefined) continue;
|
|
1344
|
-
|
|
1408
|
+
// A parent the actor cannot authorize (§3.4, on its stored scopes) is
|
|
1409
|
+
// an absent parent: the rejection below is identical either way.
|
|
1410
|
+
const parent = parents.get(reference.parentTable)?.get(parentRowId);
|
|
1411
|
+
const parentTable = schema.tables.get(reference.parentTable);
|
|
1412
|
+
if (
|
|
1413
|
+
parent !== undefined &&
|
|
1414
|
+
parentTable !== undefined &&
|
|
1415
|
+
authorizeWrite(parentTable, parent.scopes, resolved)
|
|
1416
|
+
) {
|
|
1345
1417
|
continue;
|
|
1346
1418
|
}
|
|
1347
1419
|
return {
|
|
@@ -1409,22 +1481,16 @@ async function enforceReferences(
|
|
|
1409
1481
|
'storage transaction does not support declared-reference enforcement (scanRowsByIndex)',
|
|
1410
1482
|
);
|
|
1411
1483
|
}
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
const page = await tx.scanRowsByIndex({
|
|
1484
|
+
if (reference.onDelete === 'RESTRICT') {
|
|
1485
|
+
// One remaining child decides RESTRICT.
|
|
1486
|
+
const blocking = await tx.scanRowsByIndex({
|
|
1416
1487
|
table: reference.table,
|
|
1417
1488
|
index: reference.index,
|
|
1418
1489
|
values: [parentKeyValue],
|
|
1419
|
-
afterRowId,
|
|
1420
|
-
limit:
|
|
1490
|
+
afterRowId: null,
|
|
1491
|
+
limit: 1,
|
|
1421
1492
|
});
|
|
1422
|
-
|
|
1423
|
-
if (page.length < 1_000) break;
|
|
1424
|
-
afterRowId = page[page.length - 1]?.rowId ?? null;
|
|
1425
|
-
}
|
|
1426
|
-
if (children.length === 0) continue;
|
|
1427
|
-
if (reference.onDelete === 'RESTRICT') {
|
|
1493
|
+
if (blocking.length === 0) continue;
|
|
1428
1494
|
return {
|
|
1429
1495
|
kind: 'terminate',
|
|
1430
1496
|
record: referenceRejection(
|
|
@@ -1443,141 +1509,161 @@ async function enforceReferences(
|
|
|
1443
1509
|
`unreachable: reference child table ${reference.table} is not compiled`,
|
|
1444
1510
|
);
|
|
1445
1511
|
}
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
) {
|
|
1464
|
-
continue;
|
|
1465
|
-
}
|
|
1466
|
-
} else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
|
|
1467
|
-
continue;
|
|
1468
|
-
}
|
|
1469
|
-
if (originByIndex.length >= cascadeLimit) {
|
|
1470
|
-
return {
|
|
1471
|
-
kind: 'terminate',
|
|
1472
|
-
record: referenceRejection(
|
|
1512
|
+
// Each page reads no more than the remaining cascade budget plus one
|
|
1513
|
+
// child, so a cascade past the cap stops after one extra row. A page is
|
|
1514
|
+
// applied before the next is read; keyset pagination by rowId is
|
|
1515
|
+
// unaffected by deleting or nulling rows already read.
|
|
1516
|
+
let afterRowId: string | null = null;
|
|
1517
|
+
for (;;) {
|
|
1518
|
+
const limit = Math.min(1_000, cascadeLimit - originByIndex.length + 1);
|
|
1519
|
+
const page = await tx.scanRowsByIndex({
|
|
1520
|
+
table: reference.table,
|
|
1521
|
+
index: reference.index,
|
|
1522
|
+
values: [parentKeyValue],
|
|
1523
|
+
afterRowId,
|
|
1524
|
+
limit,
|
|
1525
|
+
});
|
|
1526
|
+
for (const childRow of page) {
|
|
1527
|
+
if (!authorizeWrite(child, childRow.scopes, resolved)) {
|
|
1528
|
+
return errorRecord(
|
|
1473
1529
|
item.originOpIndex,
|
|
1474
|
-
'
|
|
1475
|
-
|
|
1476
|
-
)
|
|
1477
|
-
};
|
|
1478
|
-
}
|
|
1479
|
-
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1480
|
-
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1481
|
-
if (reference.onDelete === 'CASCADE') {
|
|
1482
|
-
const reject = await runValidator(
|
|
1483
|
-
validators,
|
|
1484
|
-
child,
|
|
1485
|
-
'delete',
|
|
1486
|
-
childRow.rowId,
|
|
1487
|
-
undefined,
|
|
1488
|
-
storedValues,
|
|
1489
|
-
item.originOpIndex,
|
|
1490
|
-
tx,
|
|
1491
|
-
partition,
|
|
1492
|
-
actorId,
|
|
1493
|
-
);
|
|
1494
|
-
if (reject !== undefined) {
|
|
1495
|
-
return { kind: 'terminate', record: reject.record };
|
|
1530
|
+
'sync.forbidden',
|
|
1531
|
+
'cascade denied by scope authorization (§3.4)',
|
|
1532
|
+
);
|
|
1496
1533
|
}
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
storedServerVersion: childRow.serverVersion,
|
|
1512
|
-
});
|
|
1513
|
-
} else {
|
|
1514
|
-
const values = [...storedValues];
|
|
1515
|
-
values[reference.columnIndex] = null;
|
|
1516
|
-
const reject = await runValidator(
|
|
1517
|
-
validators,
|
|
1518
|
-
child,
|
|
1519
|
-
'upsert',
|
|
1520
|
-
childRow.rowId,
|
|
1521
|
-
values,
|
|
1522
|
-
storedValues,
|
|
1523
|
-
item.originOpIndex,
|
|
1524
|
-
tx,
|
|
1525
|
-
partition,
|
|
1526
|
-
actorId,
|
|
1527
|
-
);
|
|
1528
|
-
if (reject !== undefined) {
|
|
1529
|
-
return { kind: 'terminate', record: reject.record };
|
|
1534
|
+
if (reference.onDelete === 'CASCADE') {
|
|
1535
|
+
if (
|
|
1536
|
+
!schedule(
|
|
1537
|
+
child,
|
|
1538
|
+
childRow.rowId,
|
|
1539
|
+
item.originOpIndex,
|
|
1540
|
+
scheduled,
|
|
1541
|
+
queue,
|
|
1542
|
+
)
|
|
1543
|
+
) {
|
|
1544
|
+
continue;
|
|
1545
|
+
}
|
|
1546
|
+
} else if (scheduled.has(`${child.name}\u0000${childRow.rowId}`)) {
|
|
1547
|
+
continue;
|
|
1530
1548
|
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
: (storedVersions.get(i) ??
|
|
1541
|
-
(childRow.columnVersions === undefined
|
|
1542
|
-
? childRow.serverVersion
|
|
1543
|
-
: 1));
|
|
1544
|
-
if (version > 1) nextVersions.set(i, version);
|
|
1549
|
+
if (originByIndex.length >= cascadeLimit) {
|
|
1550
|
+
return {
|
|
1551
|
+
kind: 'terminate',
|
|
1552
|
+
record: referenceRejection(
|
|
1553
|
+
item.originOpIndex,
|
|
1554
|
+
'declared-reference cascade exceeded the per-commit operation cap (§6.11)',
|
|
1555
|
+
{ reason: 'cascade_limit' },
|
|
1556
|
+
),
|
|
1557
|
+
};
|
|
1545
1558
|
}
|
|
1546
|
-
const
|
|
1547
|
-
const
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1559
|
+
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1560
|
+
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1561
|
+
if (reference.onDelete === 'CASCADE') {
|
|
1562
|
+
const reject = await runValidator(
|
|
1563
|
+
validators,
|
|
1564
|
+
child,
|
|
1565
|
+
'delete',
|
|
1566
|
+
childRow.rowId,
|
|
1567
|
+
undefined,
|
|
1568
|
+
storedValues,
|
|
1569
|
+
item.originOpIndex,
|
|
1570
|
+
tx,
|
|
1571
|
+
partition,
|
|
1572
|
+
actorId,
|
|
1573
|
+
);
|
|
1574
|
+
if (reject !== undefined) {
|
|
1575
|
+
return { kind: 'terminate', record: reject.record };
|
|
1576
|
+
}
|
|
1577
|
+
await tx.deleteRow(child.name, childRow.rowId);
|
|
1578
|
+
changes.push({
|
|
1579
|
+
table: child.name,
|
|
1580
|
+
rowId: childRow.rowId,
|
|
1581
|
+
op: 'delete',
|
|
1582
|
+
deletedVersion: childRow.serverVersion,
|
|
1583
|
+
scopes: childRow.scopes,
|
|
1584
|
+
});
|
|
1585
|
+
operations.push({
|
|
1586
|
+
opIndex: syntheticIndex,
|
|
1587
|
+
op: 'delete',
|
|
1588
|
+
table: child.name,
|
|
1551
1589
|
rowId: childRow.rowId,
|
|
1552
|
-
|
|
1590
|
+
row: undefined,
|
|
1591
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1592
|
+
storedServerVersion: childRow.serverVersion,
|
|
1593
|
+
});
|
|
1594
|
+
} else {
|
|
1595
|
+
const values = [...storedValues];
|
|
1596
|
+
values[reference.columnIndex] = null;
|
|
1597
|
+
const reject = await runValidator(
|
|
1598
|
+
validators,
|
|
1599
|
+
child,
|
|
1600
|
+
'upsert',
|
|
1601
|
+
childRow.rowId,
|
|
1602
|
+
values,
|
|
1603
|
+
storedValues,
|
|
1604
|
+
item.originOpIndex,
|
|
1605
|
+
tx,
|
|
1606
|
+
partition,
|
|
1607
|
+
actorId,
|
|
1608
|
+
);
|
|
1609
|
+
if (reject !== undefined) {
|
|
1610
|
+
return { kind: 'terminate', record: reject.record };
|
|
1611
|
+
}
|
|
1612
|
+
const nextServerVersion = childRow.serverVersion + 1;
|
|
1613
|
+
// §6.2: the SET NULL upsert writes only the reference column, so
|
|
1614
|
+
// only that column's version moves.
|
|
1615
|
+
const storedVersions = decodeColumnVersions(
|
|
1616
|
+
childRow.columnVersions,
|
|
1617
|
+
);
|
|
1618
|
+
const nextVersions = new Map<number, number>();
|
|
1619
|
+
for (let i = 0; i < child.columns.length; i++) {
|
|
1620
|
+
const version =
|
|
1621
|
+
i === reference.columnIndex
|
|
1622
|
+
? nextServerVersion
|
|
1623
|
+
: (storedVersions.get(i) ??
|
|
1624
|
+
(childRow.columnVersions === undefined
|
|
1625
|
+
? childRow.serverVersion
|
|
1626
|
+
: 1));
|
|
1627
|
+
if (version > 1) nextVersions.set(i, version);
|
|
1628
|
+
}
|
|
1629
|
+
const payload = encodeRow(child.columns, values);
|
|
1630
|
+
const cascadeVersions = encodeColumnVersions(nextVersions);
|
|
1631
|
+
await tx.upsertRow(
|
|
1632
|
+
child.name,
|
|
1633
|
+
{
|
|
1634
|
+
rowId: childRow.rowId,
|
|
1635
|
+
serverVersion: nextServerVersion,
|
|
1636
|
+
scopes: childRow.scopes,
|
|
1637
|
+
payload,
|
|
1638
|
+
...(cascadeVersions !== undefined
|
|
1639
|
+
? { columnVersions: cascadeVersions }
|
|
1640
|
+
: {}),
|
|
1641
|
+
},
|
|
1642
|
+
{ opIndex: syntheticIndex },
|
|
1643
|
+
);
|
|
1644
|
+
changes.push({
|
|
1645
|
+
table: child.name,
|
|
1646
|
+
rowId: childRow.rowId,
|
|
1647
|
+
op: 'upsert',
|
|
1648
|
+
rowVersion: nextServerVersion,
|
|
1553
1649
|
scopes: childRow.scopes,
|
|
1554
1650
|
payload,
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
});
|
|
1569
|
-
operations.push({
|
|
1570
|
-
opIndex: syntheticIndex,
|
|
1571
|
-
op: 'upsert',
|
|
1572
|
-
table: child.name,
|
|
1573
|
-
rowId: childRow.rowId,
|
|
1574
|
-
row: toValidateRow(child.columns, values),
|
|
1575
|
-
stored: toValidateRow(child.columns, storedValues),
|
|
1576
|
-
storedServerVersion: childRow.serverVersion,
|
|
1577
|
-
nextServerVersion,
|
|
1578
|
-
});
|
|
1651
|
+
});
|
|
1652
|
+
operations.push({
|
|
1653
|
+
opIndex: syntheticIndex,
|
|
1654
|
+
op: 'upsert',
|
|
1655
|
+
table: child.name,
|
|
1656
|
+
rowId: childRow.rowId,
|
|
1657
|
+
row: toValidateRow(child.columns, values),
|
|
1658
|
+
stored: toValidateRow(child.columns, storedValues),
|
|
1659
|
+
storedServerVersion: childRow.serverVersion,
|
|
1660
|
+
nextServerVersion,
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
originByIndex.push(item.originOpIndex);
|
|
1579
1664
|
}
|
|
1580
|
-
|
|
1665
|
+
if (page.length < limit) break;
|
|
1666
|
+
afterRowId = page[page.length - 1]?.rowId ?? null;
|
|
1581
1667
|
}
|
|
1582
1668
|
}
|
|
1583
1669
|
}
|
|
@@ -1696,7 +1782,9 @@ export async function processPushCommitWithTrace(
|
|
|
1696
1782
|
// prefetch statement.
|
|
1697
1783
|
const targets = new Map<string, string[]>();
|
|
1698
1784
|
for (const op of frame.operations) {
|
|
1699
|
-
|
|
1785
|
+
// A rowId holding U+0000 is rejected per operation (§6.1) before any
|
|
1786
|
+
// store reads it.
|
|
1787
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000')) continue;
|
|
1700
1788
|
const ids = targets.get(op.table) ?? [];
|
|
1701
1789
|
ids.push(op.rowId);
|
|
1702
1790
|
targets.set(op.table, ids);
|
|
@@ -1737,7 +1825,11 @@ export async function processPushOperationsWithTrace(
|
|
|
1737
1825
|
): Promise<ProcessedPushCommit> {
|
|
1738
1826
|
const { partition } = ctx;
|
|
1739
1827
|
const createdAtMs = clockOf(ctx)();
|
|
1740
|
-
const blobCtx: BlobApplyContext = {
|
|
1828
|
+
const blobCtx: BlobApplyContext = {
|
|
1829
|
+
store: ctx.blobs,
|
|
1830
|
+
partition,
|
|
1831
|
+
presence: new Map(),
|
|
1832
|
+
};
|
|
1741
1833
|
const crdtMergers = ctx.crdtMergers;
|
|
1742
1834
|
const validators = ctx.validators;
|
|
1743
1835
|
const commitValidator = ctx.commitValidator;
|
|
@@ -1833,7 +1925,8 @@ export async function processPushOperationsWithTrace(
|
|
|
1833
1925
|
if (tx.prefetchRows !== undefined) {
|
|
1834
1926
|
const targets = new Map<string, string[]>();
|
|
1835
1927
|
for (const op of operations) {
|
|
1836
|
-
if (!schema.tables.has(op.table))
|
|
1928
|
+
if (!schema.tables.has(op.table) || op.rowId.includes('\u0000'))
|
|
1929
|
+
continue;
|
|
1837
1930
|
const ids = targets.get(op.table) ?? [];
|
|
1838
1931
|
ids.push(op.rowId);
|
|
1839
1932
|
targets.set(op.table, ids);
|
|
@@ -1844,6 +1937,42 @@ export async function processPushOperationsWithTrace(
|
|
|
1844
1937
|
);
|
|
1845
1938
|
}
|
|
1846
1939
|
}
|
|
1940
|
+
// §6.6: the blobIds the commit pushes are checked once, concurrently,
|
|
1941
|
+
// before the operation loop. A payload that fails decode is skipped here
|
|
1942
|
+
// and rejected by its operation.
|
|
1943
|
+
const pushedBlobIds: string[] = [];
|
|
1944
|
+
for (const op of operations) {
|
|
1945
|
+
const table = schema.tables.get(op.table);
|
|
1946
|
+
const wireTable = wireSchema.tables.get(op.table);
|
|
1947
|
+
if (
|
|
1948
|
+
op.payload === undefined ||
|
|
1949
|
+
table === undefined ||
|
|
1950
|
+
wireTable === undefined ||
|
|
1951
|
+
table.blobRefColumnIndices.length === 0
|
|
1952
|
+
) {
|
|
1953
|
+
continue;
|
|
1954
|
+
}
|
|
1955
|
+
let sparse: SparseRowValue[];
|
|
1956
|
+
try {
|
|
1957
|
+
sparse = decodeSparseRow(
|
|
1958
|
+
wireTable.columns,
|
|
1959
|
+
wireTable.primaryKeyIndex,
|
|
1960
|
+
op.payload,
|
|
1961
|
+
);
|
|
1962
|
+
} catch {
|
|
1963
|
+
continue;
|
|
1964
|
+
}
|
|
1965
|
+
for (const index of table.blobRefColumnIndices) {
|
|
1966
|
+
const value = sparse[index];
|
|
1967
|
+
if (typeof value === 'string') {
|
|
1968
|
+
pushedBlobIds.push(parseBlobRef(value).blobId);
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
await resolveBlobPresence(blobCtx, pushedBlobIds);
|
|
1973
|
+
// §6.4: the row each touched (table, rowId) held before this commit, so a
|
|
1974
|
+
// conflict reports the row as stored once the commit rolls back.
|
|
1975
|
+
const storedBeforeCommit = new Map<string, StoredRow | undefined>();
|
|
1847
1976
|
for (let opIndex = 0; opIndex < operations.length; opIndex++) {
|
|
1848
1977
|
const op = operations[opIndex];
|
|
1849
1978
|
if (op === undefined) continue;
|
|
@@ -1864,6 +1993,10 @@ export async function processPushOperationsWithTrace(
|
|
|
1864
1993
|
terminated = outcome.record;
|
|
1865
1994
|
break;
|
|
1866
1995
|
}
|
|
1996
|
+
const rowKey = `${op.table}\u0000${op.rowId}`;
|
|
1997
|
+
if (!storedBeforeCommit.has(rowKey)) {
|
|
1998
|
+
storedBeforeCommit.set(rowKey, outcome.staged.stored);
|
|
1999
|
+
}
|
|
1867
2000
|
results.push({ opIndex, status: 'applied' });
|
|
1868
2001
|
validatedOperations.push(...outcome.operations);
|
|
1869
2002
|
stagedWrites.push(outcome.staged);
|
|
@@ -1941,19 +2074,37 @@ export async function processPushOperationsWithTrace(
|
|
|
1941
2074
|
terminated.status === 'conflict'
|
|
1942
2075
|
? operations[terminated.opIndex]
|
|
1943
2076
|
: undefined;
|
|
1944
|
-
|
|
2077
|
+
// §6.4: a conflict only arises in the operation loop, so a row no
|
|
2078
|
+
// earlier operation touched still reads as stored before the commit.
|
|
2079
|
+
const conflictKey =
|
|
1945
2080
|
conflictOperation === undefined
|
|
1946
2081
|
? undefined
|
|
1947
|
-
:
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
2082
|
+
: `${conflictOperation.table}\u0000${conflictOperation.rowId}`;
|
|
2083
|
+
const conflictRow =
|
|
2084
|
+
conflictOperation === undefined || conflictKey === undefined
|
|
2085
|
+
? undefined
|
|
2086
|
+
: storedBeforeCommit.has(conflictKey)
|
|
2087
|
+
? storedBeforeCommit.get(conflictKey)
|
|
2088
|
+
: await tx.getRow(conflictOperation.table, conflictOperation.rowId);
|
|
2089
|
+
if (terminated.status === 'conflict') {
|
|
2090
|
+
terminated =
|
|
2091
|
+
conflictRow === undefined
|
|
2092
|
+
? errorRecord(
|
|
2093
|
+
terminated.opIndex,
|
|
2094
|
+
'sync.row_missing',
|
|
2095
|
+
'the conflicting row exists only through an earlier operation of the same commit (§6.4)',
|
|
2096
|
+
).record
|
|
2097
|
+
: {
|
|
2098
|
+
...terminated,
|
|
2099
|
+
serverVersion: conflictRow.serverVersion,
|
|
2100
|
+
serverRow: conflictRow.payload,
|
|
2101
|
+
};
|
|
1953
2102
|
}
|
|
1954
2103
|
const stored = newStoredPushResult(createdAtMs, {
|
|
1955
2104
|
status: 'rejected',
|
|
1956
|
-
...(
|
|
2105
|
+
...(terminated.status === 'conflict' &&
|
|
2106
|
+
conflictOperation !== undefined &&
|
|
2107
|
+
conflictRow !== undefined
|
|
1957
2108
|
? {
|
|
1958
2109
|
conflictAuthorization: {
|
|
1959
2110
|
table: conflictOperation.table,
|