@syncular/server 0.76.0 → 0.77.1
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/push.js +16 -14
- package/dist/validate.d.ts +4 -0
- package/package.json +2 -2
- package/src/push.ts +16 -0
- package/src/validate.ts +4 -0
package/dist/push.js
CHANGED
|
@@ -99,7 +99,7 @@ async function resolveBlobPresence(blobCtx, blobIds) {
|
|
|
99
99
|
* A no-op for tables with no validator — the `undefined` short-circuit
|
|
100
100
|
* keeps the feature zero-cost when off.
|
|
101
101
|
*/
|
|
102
|
-
async function runValidator(validators, table, op, rowId, values, storedValues, opIndex, tx, partition, actorId) {
|
|
102
|
+
async function runValidator(validators, table, op, rowId, values, storedValues, opIndex, tx, partition, actorId, context) {
|
|
103
103
|
const validator = validators?.[table.name];
|
|
104
104
|
if (validator === undefined)
|
|
105
105
|
return undefined;
|
|
@@ -117,6 +117,7 @@ async function runValidator(validators, table, op, rowId, values, storedValues,
|
|
|
117
117
|
}, {
|
|
118
118
|
actorId,
|
|
119
119
|
partition,
|
|
120
|
+
...(context !== undefined ? { context } : {}),
|
|
120
121
|
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
121
122
|
});
|
|
122
123
|
}
|
|
@@ -173,7 +174,7 @@ async function mergeCrdtColumns(table, values, storedValues, present, opIndex, m
|
|
|
173
174
|
}
|
|
174
175
|
return { changed };
|
|
175
176
|
}
|
|
176
|
-
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId,
|
|
177
|
+
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId, context,
|
|
177
178
|
/** §2.2: the partition's delete floor, raised by this commit's deletes. */
|
|
178
179
|
deleteFloor) {
|
|
179
180
|
const table = schema.tables.get(op.table);
|
|
@@ -245,10 +246,10 @@ deleteFloor) {
|
|
|
245
246
|
if (!authorizeWrite(table, extracted.scopes, resolved)) {
|
|
246
247
|
return errorRecord(opIndex, 'sync.forbidden', 'rescope denied by scope authorization of the new scopes (§3.4)');
|
|
247
248
|
}
|
|
248
|
-
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex, tx, partition, actorId);
|
|
249
|
+
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex, tx, partition, actorId, context);
|
|
249
250
|
if (deleteReject !== undefined)
|
|
250
251
|
return deleteReject;
|
|
251
|
-
const upsertReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex, tx, partition, actorId);
|
|
252
|
+
const upsertReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex, tx, partition, actorId, context);
|
|
252
253
|
if (upsertReject !== undefined)
|
|
253
254
|
return upsertReject;
|
|
254
255
|
const blobCheck = await checkAndRecordBlobs(tx, table, op.rowId, values, opIndex, blobCtx);
|
|
@@ -369,7 +370,7 @@ deleteFloor) {
|
|
|
369
370
|
// stored = the row about to be removed). Only reached for an existing
|
|
370
371
|
// row — an absent-row delete is an idempotent no-op above.
|
|
371
372
|
const storedValues = decodeRow(table.columns, stored.payload);
|
|
372
|
-
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex, tx, partition, actorId);
|
|
373
|
+
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex, tx, partition, actorId, context);
|
|
373
374
|
if (deleteReject !== undefined)
|
|
374
375
|
return deleteReject;
|
|
375
376
|
await tx.deleteRow(op.table, op.rowId);
|
|
@@ -510,7 +511,7 @@ deleteFloor) {
|
|
|
510
511
|
// §6.7: validate the merged row that will persist — for a crdt column
|
|
511
512
|
// the validator sees the MERGED value (§5.10.3), the state the store
|
|
512
513
|
// holds, not the raw pushed update.
|
|
513
|
-
const updateReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex, tx, partition, actorId);
|
|
514
|
+
const updateReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex, tx, partition, actorId, context);
|
|
514
515
|
if (updateReject !== undefined)
|
|
515
516
|
return updateReject;
|
|
516
517
|
const columnVersions = encodeColumnVersions(nextVersions);
|
|
@@ -615,7 +616,7 @@ deleteFloor) {
|
|
|
615
616
|
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
616
617
|
// distinguish create from update); crdt columns are already merged
|
|
617
618
|
// against the empty document.
|
|
618
|
-
const insertReject = await runValidator(validators, table, 'upsert', op.rowId, values, undefined, opIndex, tx, partition, actorId);
|
|
619
|
+
const insertReject = await runValidator(validators, table, 'upsert', op.rowId, values, undefined, opIndex, tx, partition, actorId, context);
|
|
619
620
|
if (insertReject !== undefined)
|
|
620
621
|
return insertReject;
|
|
621
622
|
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
@@ -774,7 +775,7 @@ function commitValidationReader(tx, schema) {
|
|
|
774
775
|
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
775
776
|
};
|
|
776
777
|
}
|
|
777
|
-
async function runCommitValidator(validator, onError, tx, schema, clientId, clientCommitId, actorId, partition, operations) {
|
|
778
|
+
async function runCommitValidator(validator, onError, tx, schema, clientId, clientCommitId, actorId, partition, operations, context) {
|
|
778
779
|
if (validator === undefined)
|
|
779
780
|
return undefined;
|
|
780
781
|
try {
|
|
@@ -783,6 +784,7 @@ async function runCommitValidator(validator, onError, tx, schema, clientId, clie
|
|
|
783
784
|
clientCommitId,
|
|
784
785
|
actorId,
|
|
785
786
|
partition,
|
|
787
|
+
...(context !== undefined ? { context } : {}),
|
|
786
788
|
operations,
|
|
787
789
|
read: commitValidationReader(tx, schema),
|
|
788
790
|
});
|
|
@@ -859,7 +861,7 @@ function referenceRejection(opIndex, message, details) {
|
|
|
859
861
|
* operation count) and emit ordinary changes carrying the child's stored
|
|
860
862
|
* scopes; a rejection attributes to the originating client delete.
|
|
861
863
|
*/
|
|
862
|
-
async function enforceReferences(tx, schema, resolved, staged, clientOpCount, cascadeLimit, validators, partition, actorId) {
|
|
864
|
+
async function enforceReferences(tx, schema, resolved, staged, clientOpCount, cascadeLimit, validators, partition, actorId, context) {
|
|
863
865
|
const changes = [];
|
|
864
866
|
const operations = [];
|
|
865
867
|
const originByIndex = [];
|
|
@@ -1032,7 +1034,7 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
1032
1034
|
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1033
1035
|
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1034
1036
|
if (reference.onDelete === 'CASCADE') {
|
|
1035
|
-
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1037
|
+
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, tx, partition, actorId, context);
|
|
1036
1038
|
if (reject !== undefined) {
|
|
1037
1039
|
return { kind: 'terminate', record: reject.record };
|
|
1038
1040
|
}
|
|
@@ -1057,7 +1059,7 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
1057
1059
|
else {
|
|
1058
1060
|
const values = [...storedValues];
|
|
1059
1061
|
values[reference.columnIndex] = null;
|
|
1060
|
-
const reject = await runValidator(validators, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex, tx, partition, actorId);
|
|
1062
|
+
const reject = await runValidator(validators, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex, tx, partition, actorId, context);
|
|
1061
1063
|
if (reject !== undefined) {
|
|
1062
1064
|
return { kind: 'terminate', record: reject.record };
|
|
1063
1065
|
}
|
|
@@ -1364,7 +1366,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1364
1366
|
const op = operations[opIndex];
|
|
1365
1367
|
if (op === undefined)
|
|
1366
1368
|
continue;
|
|
1367
|
-
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, deleteFloor);
|
|
1369
|
+
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, ctx.context, deleteFloor);
|
|
1368
1370
|
if (outcome.kind === 'terminate') {
|
|
1369
1371
|
terminated = outcome.record;
|
|
1370
1372
|
break;
|
|
@@ -1388,7 +1390,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1388
1390
|
// and before whole-commit validation.
|
|
1389
1391
|
let appendedOrigin = [];
|
|
1390
1392
|
if (terminated === undefined) {
|
|
1391
|
-
const referencePass = await enforceReferences(tx, schema, resolved, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId);
|
|
1393
|
+
const referencePass = await enforceReferences(tx, schema, resolved, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId, ctx.context);
|
|
1392
1394
|
if (referencePass.kind === 'terminate') {
|
|
1393
1395
|
terminated = referencePass.record;
|
|
1394
1396
|
}
|
|
@@ -1399,7 +1401,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1399
1401
|
}
|
|
1400
1402
|
}
|
|
1401
1403
|
if (terminated === undefined) {
|
|
1402
|
-
const commitReject = await runCommitValidator(commitValidator, ctx.onError, tx, schema, clientId, clientCommitId, ctx.actorId, partition, validatedOperations);
|
|
1404
|
+
const commitReject = await runCommitValidator(commitValidator, ctx.onError, tx, schema, clientId, clientCommitId, ctx.actorId, partition, validatedOperations, ctx.context);
|
|
1403
1405
|
if (commitReject?.kind === 'terminate') {
|
|
1404
1406
|
// An appended §6.11 cascade operation carries a synthetic opIndex;
|
|
1405
1407
|
// surface the originating client operation instead.
|
package/dist/validate.d.ts
CHANGED
|
@@ -132,6 +132,8 @@ export interface ValidateCommitInput {
|
|
|
132
132
|
readonly clientCommitId: string;
|
|
133
133
|
readonly actorId: string;
|
|
134
134
|
readonly partition: string;
|
|
135
|
+
/** The context `authenticate` returned for this request (§1.1); absent without one. */
|
|
136
|
+
readonly context?: string;
|
|
135
137
|
readonly operations: readonly ValidateCommitOperation[];
|
|
136
138
|
readonly read: CommitValidationReader;
|
|
137
139
|
}
|
|
@@ -156,6 +158,8 @@ export interface ValidateContext {
|
|
|
156
158
|
readonly actorId: string;
|
|
157
159
|
/** The partition (§1.1) the commit targets. */
|
|
158
160
|
readonly partition: string;
|
|
161
|
+
/** The context `authenticate` returned for this request (§1.1); absent without one. */
|
|
162
|
+
readonly context?: string;
|
|
159
163
|
/**
|
|
160
164
|
* Run a generated registered query on the push transaction (§6.7). The
|
|
161
165
|
* request has the shape `ServerStorage.queryAuthoritative` takes; the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.77.1",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.77.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/push.ts
CHANGED
|
@@ -234,6 +234,7 @@ async function runValidator(
|
|
|
234
234
|
tx: StorageTransaction,
|
|
235
235
|
partition: string,
|
|
236
236
|
actorId: string,
|
|
237
|
+
context: string | undefined,
|
|
237
238
|
): Promise<TerminatingOutcome | undefined> {
|
|
238
239
|
const validator = validators?.[table.name];
|
|
239
240
|
if (validator === undefined) return undefined;
|
|
@@ -255,6 +256,7 @@ async function runValidator(
|
|
|
255
256
|
{
|
|
256
257
|
actorId,
|
|
257
258
|
partition,
|
|
259
|
+
...(context !== undefined ? { context } : {}),
|
|
258
260
|
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
259
261
|
},
|
|
260
262
|
);
|
|
@@ -346,6 +348,7 @@ async function applyOperation(
|
|
|
346
348
|
validators: ValidatorRegistry | undefined,
|
|
347
349
|
partition: string,
|
|
348
350
|
actorId: string,
|
|
351
|
+
context: string | undefined,
|
|
349
352
|
/** §2.2: the partition's delete floor, raised by this commit's deletes. */
|
|
350
353
|
deleteFloor: number,
|
|
351
354
|
): Promise<OperationOutcome> {
|
|
@@ -482,6 +485,7 @@ async function applyOperation(
|
|
|
482
485
|
tx,
|
|
483
486
|
partition,
|
|
484
487
|
actorId,
|
|
488
|
+
context,
|
|
485
489
|
);
|
|
486
490
|
if (deleteReject !== undefined) return deleteReject;
|
|
487
491
|
const upsertReject = await runValidator(
|
|
@@ -495,6 +499,7 @@ async function applyOperation(
|
|
|
495
499
|
tx,
|
|
496
500
|
partition,
|
|
497
501
|
actorId,
|
|
502
|
+
context,
|
|
498
503
|
);
|
|
499
504
|
if (upsertReject !== undefined) return upsertReject;
|
|
500
505
|
const blobCheck = await checkAndRecordBlobs(
|
|
@@ -645,6 +650,7 @@ async function applyOperation(
|
|
|
645
650
|
tx,
|
|
646
651
|
partition,
|
|
647
652
|
actorId,
|
|
653
|
+
context,
|
|
648
654
|
);
|
|
649
655
|
if (deleteReject !== undefined) return deleteReject;
|
|
650
656
|
await tx.deleteRow(op.table, op.rowId);
|
|
@@ -850,6 +856,7 @@ async function applyOperation(
|
|
|
850
856
|
tx,
|
|
851
857
|
partition,
|
|
852
858
|
actorId,
|
|
859
|
+
context,
|
|
853
860
|
);
|
|
854
861
|
if (updateReject !== undefined) return updateReject;
|
|
855
862
|
const columnVersions = encodeColumnVersions(nextVersions);
|
|
@@ -1005,6 +1012,7 @@ async function applyOperation(
|
|
|
1005
1012
|
tx,
|
|
1006
1013
|
partition,
|
|
1007
1014
|
actorId,
|
|
1015
|
+
context,
|
|
1008
1016
|
);
|
|
1009
1017
|
if (insertReject !== undefined) return insertReject;
|
|
1010
1018
|
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
@@ -1224,6 +1232,7 @@ async function runCommitValidator(
|
|
|
1224
1232
|
actorId: string,
|
|
1225
1233
|
partition: string,
|
|
1226
1234
|
operations: readonly ValidateCommitOperation[],
|
|
1235
|
+
context: string | undefined,
|
|
1227
1236
|
): Promise<OperationOutcome | undefined> {
|
|
1228
1237
|
if (validator === undefined) return undefined;
|
|
1229
1238
|
try {
|
|
@@ -1232,6 +1241,7 @@ async function runCommitValidator(
|
|
|
1232
1241
|
clientCommitId,
|
|
1233
1242
|
actorId,
|
|
1234
1243
|
partition,
|
|
1244
|
+
...(context !== undefined ? { context } : {}),
|
|
1235
1245
|
operations,
|
|
1236
1246
|
read: commitValidationReader(tx, schema),
|
|
1237
1247
|
});
|
|
@@ -1352,6 +1362,7 @@ async function enforceReferences(
|
|
|
1352
1362
|
validators: ValidatorRegistry | undefined,
|
|
1353
1363
|
partition: string,
|
|
1354
1364
|
actorId: string,
|
|
1365
|
+
context: string | undefined,
|
|
1355
1366
|
): Promise<
|
|
1356
1367
|
| {
|
|
1357
1368
|
readonly kind: 'ok';
|
|
@@ -1592,6 +1603,7 @@ async function enforceReferences(
|
|
|
1592
1603
|
tx,
|
|
1593
1604
|
partition,
|
|
1594
1605
|
actorId,
|
|
1606
|
+
context,
|
|
1595
1607
|
);
|
|
1596
1608
|
if (reject !== undefined) {
|
|
1597
1609
|
return { kind: 'terminate', record: reject.record };
|
|
@@ -1627,6 +1639,7 @@ async function enforceReferences(
|
|
|
1627
1639
|
tx,
|
|
1628
1640
|
partition,
|
|
1629
1641
|
actorId,
|
|
1642
|
+
context,
|
|
1630
1643
|
);
|
|
1631
1644
|
if (reject !== undefined) {
|
|
1632
1645
|
return { kind: 'terminate', record: reject.record };
|
|
@@ -2074,6 +2087,7 @@ export async function processPushOperationsWithTrace(
|
|
|
2074
2087
|
validators,
|
|
2075
2088
|
partition,
|
|
2076
2089
|
ctx.actorId,
|
|
2090
|
+
ctx.context,
|
|
2077
2091
|
deleteFloor,
|
|
2078
2092
|
);
|
|
2079
2093
|
if (outcome.kind === 'terminate') {
|
|
@@ -2110,6 +2124,7 @@ export async function processPushOperationsWithTrace(
|
|
|
2110
2124
|
validators,
|
|
2111
2125
|
partition,
|
|
2112
2126
|
ctx.actorId,
|
|
2127
|
+
ctx.context,
|
|
2113
2128
|
);
|
|
2114
2129
|
if (referencePass.kind === 'terminate') {
|
|
2115
2130
|
terminated = referencePass.record;
|
|
@@ -2131,6 +2146,7 @@ export async function processPushOperationsWithTrace(
|
|
|
2131
2146
|
ctx.actorId,
|
|
2132
2147
|
partition,
|
|
2133
2148
|
validatedOperations,
|
|
2149
|
+
ctx.context,
|
|
2134
2150
|
);
|
|
2135
2151
|
if (commitReject?.kind === 'terminate') {
|
|
2136
2152
|
// An appended §6.11 cascade operation carries a synthetic opIndex;
|
package/src/validate.ts
CHANGED
|
@@ -165,6 +165,8 @@ export interface ValidateCommitInput {
|
|
|
165
165
|
readonly clientCommitId: string;
|
|
166
166
|
readonly actorId: string;
|
|
167
167
|
readonly partition: string;
|
|
168
|
+
/** The context `authenticate` returned for this request (§1.1); absent without one. */
|
|
169
|
+
readonly context?: string;
|
|
168
170
|
readonly operations: readonly ValidateCommitOperation[];
|
|
169
171
|
readonly read: CommitValidationReader;
|
|
170
172
|
}
|
|
@@ -193,6 +195,8 @@ export interface ValidateContext {
|
|
|
193
195
|
readonly actorId: string;
|
|
194
196
|
/** The partition (§1.1) the commit targets. */
|
|
195
197
|
readonly partition: string;
|
|
198
|
+
/** The context `authenticate` returned for this request (§1.1); absent without one. */
|
|
199
|
+
readonly context?: string;
|
|
196
200
|
/**
|
|
197
201
|
* Run a generated registered query on the push transaction (§6.7). The
|
|
198
202
|
* request has the shape `ServerStorage.queryAuthoritative` takes; the
|