@syncular/server 0.49.2 → 0.50.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 +6 -4
- package/dist/postgres-storage.js +6 -0
- package/dist/push.js +46 -2
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +8 -0
- package/dist/scopes.d.ts +2 -3
- package/dist/scopes.js +8 -4
- package/dist/sqlite-dialect.js +6 -0
- package/dist/storage.d.ts +6 -0
- package/package.json +2 -2
- package/src/postgres-storage.ts +7 -0
- package/src/push.ts +62 -0
- package/src/schema.ts +15 -0
- package/src/scopes.ts +7 -4
- package/src/sqlite-dialect.ts +7 -0
- package/src/storage.ts +6 -0
package/README.md
CHANGED
|
@@ -291,8 +291,10 @@ explicit server-authoritative commands.
|
|
|
291
291
|
The idempotency key for a push commit is the triple
|
|
292
292
|
**(`partition`, `clientId`, `clientCommitId`)**. The server persists the full
|
|
293
293
|
commit result in the same transaction as the writes, and a replay of the same
|
|
294
|
-
key returns that result
|
|
295
|
-
|
|
294
|
+
key returns that result: an applied commit replays as `cached`, and a rejected
|
|
295
|
+
commit replays as `rejected` (§2.3, §6.3). The server reauthorizes a cached
|
|
296
|
+
conflict snapshot against current write scopes before returning its row. A
|
|
297
|
+
denied replay returns `sync.forbidden` and leaves the cached result intact.
|
|
296
298
|
|
|
297
299
|
The key contains the commit ID and nothing about the operations. Host
|
|
298
300
|
authentication, request-envelope validation, and the §1.5 clientId-actor
|
|
@@ -301,8 +303,8 @@ binding run before the lookup.
|
|
|
301
303
|
not proof of an authenticated device. When a retained result exists for the
|
|
302
304
|
key, the server skips `buildOperations`, the commit and write validators, and
|
|
303
305
|
the apply transaction: a retry that reuses the key with different operations
|
|
304
|
-
returns the persisted result
|
|
305
|
-
or applied. `packages/server/test/push.test.ts` pins this.
|
|
306
|
+
returns the persisted result subject to conflict reauthorization, and those
|
|
307
|
+
operations are never built, validated, or applied. `packages/server/test/push.test.ts` pins this.
|
|
306
308
|
|
|
307
309
|
A client MUST NOT reuse a `clientCommitId` for changed intent, including after
|
|
308
310
|
the persisted result has been pruned. One ID identifies one logical commit
|
package/dist/postgres-storage.js
CHANGED
|
@@ -376,6 +376,9 @@ function serializePushResult(result) {
|
|
|
376
376
|
...(result.cacheIdentity !== undefined
|
|
377
377
|
? { cacheIdentity: result.cacheIdentity }
|
|
378
378
|
: {}),
|
|
379
|
+
...(result.conflictAuthorization !== undefined
|
|
380
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
381
|
+
: {}),
|
|
379
382
|
results: result.results.map((record) => {
|
|
380
383
|
if (record.status === 'conflict') {
|
|
381
384
|
return {
|
|
@@ -437,6 +440,9 @@ function deserializePushResult(value) {
|
|
|
437
440
|
...(parsed.cacheIdentity !== undefined
|
|
438
441
|
? { cacheIdentity: parsed.cacheIdentity }
|
|
439
442
|
: {}),
|
|
443
|
+
...(parsed.conflictAuthorization !== undefined
|
|
444
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
445
|
+
: {}),
|
|
440
446
|
results,
|
|
441
447
|
};
|
|
442
448
|
}
|
package/dist/push.js
CHANGED
|
@@ -795,7 +795,7 @@ function referenceRejection(opIndex, message, details) {
|
|
|
795
795
|
* operation count) and emit ordinary changes carrying the child's stored
|
|
796
796
|
* scopes; a rejection attributes to the originating client delete.
|
|
797
797
|
*/
|
|
798
|
-
async function enforceReferences(tx, schema, staged, clientOpCount, cascadeLimit, validators, partition, actorId) {
|
|
798
|
+
async function enforceReferences(tx, schema, resolved, staged, clientOpCount, cascadeLimit, validators, partition, actorId) {
|
|
799
799
|
const changes = [];
|
|
800
800
|
const operations = [];
|
|
801
801
|
const originByIndex = [];
|
|
@@ -922,6 +922,9 @@ async function enforceReferences(tx, schema, staged, clientOpCount, cascadeLimit
|
|
|
922
922
|
throw new Error(`unreachable: reference child table ${reference.table} is not compiled`);
|
|
923
923
|
}
|
|
924
924
|
for (const childRow of children) {
|
|
925
|
+
if (!authorizeWrite(child, childRow.scopes, resolved)) {
|
|
926
|
+
return errorRecord(item.originOpIndex, 'sync.forbidden', 'cascade denied by scope authorization (§3.4)');
|
|
927
|
+
}
|
|
925
928
|
if (reference.onDelete === 'CASCADE') {
|
|
926
929
|
if (!schedule(child, childRow.rowId, item.originOpIndex, scheduled, queue)) {
|
|
927
930
|
continue;
|
|
@@ -1143,6 +1146,28 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1143
1146
|
throw serveNotReadyError(refusal);
|
|
1144
1147
|
}
|
|
1145
1148
|
if (prepared.persisted !== undefined) {
|
|
1149
|
+
const conflict = prepared.persisted.results.find((result) => result.status === 'conflict');
|
|
1150
|
+
const authorization = prepared.persisted.conflictAuthorization;
|
|
1151
|
+
const table = authorization && schema.tables.get(authorization.table);
|
|
1152
|
+
if (conflict !== undefined &&
|
|
1153
|
+
(authorization === undefined ||
|
|
1154
|
+
authorization.schemaVersion !== schema.version ||
|
|
1155
|
+
table === undefined ||
|
|
1156
|
+
!authorizeWrite(table, authorization.scopes, resolved))) {
|
|
1157
|
+
await tx.rollback();
|
|
1158
|
+
return processedPushCommit(clientCommitId, {
|
|
1159
|
+
...prepared.persisted,
|
|
1160
|
+
results: [
|
|
1161
|
+
{
|
|
1162
|
+
opIndex: conflict.opIndex,
|
|
1163
|
+
status: 'error',
|
|
1164
|
+
code: 'sync.forbidden',
|
|
1165
|
+
message: 'conflict replay denied by scope authorization (§3.4)',
|
|
1166
|
+
retryable: false,
|
|
1167
|
+
},
|
|
1168
|
+
],
|
|
1169
|
+
}, true);
|
|
1170
|
+
}
|
|
1146
1171
|
await tx.rollback();
|
|
1147
1172
|
return processedPushCommit(clientCommitId, prepared.persisted, true);
|
|
1148
1173
|
}
|
|
@@ -1184,7 +1209,7 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1184
1209
|
// and before whole-commit validation.
|
|
1185
1210
|
let appendedOrigin = [];
|
|
1186
1211
|
if (terminated === undefined) {
|
|
1187
|
-
const referencePass = await enforceReferences(tx, schema, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId);
|
|
1212
|
+
const referencePass = await enforceReferences(tx, schema, resolved, stagedWrites, operations.length, limitsOf(ctx).maxCascadeOperationsPerCommit, validators, partition, ctx.actorId);
|
|
1188
1213
|
if (referencePass.kind === 'terminate') {
|
|
1189
1214
|
terminated = referencePass.record;
|
|
1190
1215
|
}
|
|
@@ -1223,8 +1248,27 @@ export async function processPushOperationsWithTrace(ctx, schema, resolved, clie
|
|
|
1223
1248
|
if (terminated !== undefined) {
|
|
1224
1249
|
// §6.3 rejected: only the terminating operation's record; §6.4:
|
|
1225
1250
|
// every write of the commit rolls back.
|
|
1251
|
+
const conflictOperation = terminated.status === 'conflict'
|
|
1252
|
+
? operations[terminated.opIndex]
|
|
1253
|
+
: undefined;
|
|
1254
|
+
const conflictRow = conflictOperation === undefined
|
|
1255
|
+
? undefined
|
|
1256
|
+
: await tx.getRow(conflictOperation.table, conflictOperation.rowId);
|
|
1257
|
+
if (terminated.status === 'conflict' &&
|
|
1258
|
+
(conflictOperation === undefined || conflictRow === undefined)) {
|
|
1259
|
+
throw new Error('conflict snapshot has no authorization row');
|
|
1260
|
+
}
|
|
1226
1261
|
const stored = newStoredPushResult(createdAtMs, {
|
|
1227
1262
|
status: 'rejected',
|
|
1263
|
+
...(conflictOperation !== undefined && conflictRow !== undefined
|
|
1264
|
+
? {
|
|
1265
|
+
conflictAuthorization: {
|
|
1266
|
+
table: conflictOperation.table,
|
|
1267
|
+
schemaVersion: schema.version,
|
|
1268
|
+
scopes: conflictRow.scopes,
|
|
1269
|
+
},
|
|
1270
|
+
}
|
|
1271
|
+
: {}),
|
|
1228
1272
|
results: [terminated],
|
|
1229
1273
|
});
|
|
1230
1274
|
// Discard candidates and persist the rejection while retaining the same
|
package/dist/schema.d.ts
CHANGED
|
@@ -67,6 +67,8 @@ export interface TableSchema {
|
|
|
67
67
|
readonly primaryKey: string;
|
|
68
68
|
/** Scope patterns (§3.1). Every synced table declares at least one. */
|
|
69
69
|
readonly scopes: readonly ScopePatternSpec[];
|
|
70
|
+
/** Variables required together for writes; defaults to every scope variable. */
|
|
71
|
+
readonly writeScopeVariables?: readonly string[];
|
|
70
72
|
/** User indexes (optional) — created on the server's relational tables. */
|
|
71
73
|
readonly indexes?: readonly IndexSchema[];
|
|
72
74
|
/** Server-only indexes (optional); names share the namespace of `indexes`. */
|
|
@@ -109,6 +111,7 @@ export interface CompiledTable {
|
|
|
109
111
|
readonly columns: readonly RowColumn[];
|
|
110
112
|
readonly primaryKeyIndex: number;
|
|
111
113
|
readonly scopePatterns: readonly CompiledScopePattern[];
|
|
114
|
+
readonly writeScopeVariables: readonly string[];
|
|
112
115
|
/** User indexes (validated: unique names, existing columns). */
|
|
113
116
|
readonly indexes: readonly IndexSchema[];
|
|
114
117
|
/** Server-only indexes (validated like `indexes`). */
|
package/dist/schema.js
CHANGED
|
@@ -117,6 +117,13 @@ export function compileSchema(schema) {
|
|
|
117
117
|
variables.add(pattern.variable);
|
|
118
118
|
declaredVariables.add(pattern.variable);
|
|
119
119
|
}
|
|
120
|
+
const writeScopeVariables = table.writeScopeVariables ?? [...variables];
|
|
121
|
+
if (!Array.isArray(writeScopeVariables) ||
|
|
122
|
+
writeScopeVariables.length === 0 ||
|
|
123
|
+
new Set(writeScopeVariables).size !== writeScopeVariables.length ||
|
|
124
|
+
writeScopeVariables.some((variable) => !variables.has(variable))) {
|
|
125
|
+
throw new Error(`table ${table.name}: writeScopeVariables must contain distinct declared scope variables (§3.4)`);
|
|
126
|
+
}
|
|
120
127
|
const blobRefColumnIndices = [];
|
|
121
128
|
const crdtColumns = [];
|
|
122
129
|
const encryptedColumnIndices = [];
|
|
@@ -162,6 +169,7 @@ export function compileSchema(schema) {
|
|
|
162
169
|
columns: table.columns,
|
|
163
170
|
primaryKeyIndex,
|
|
164
171
|
scopePatterns,
|
|
172
|
+
writeScopeVariables,
|
|
165
173
|
indexes,
|
|
166
174
|
serverIndexes,
|
|
167
175
|
references,
|
package/dist/scopes.d.ts
CHANGED
|
@@ -36,9 +36,8 @@ export declare function storedScopesForRow(table: CompiledTable, values: readonl
|
|
|
36
36
|
missing: string;
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
|
-
* Write-path authorization (§3.4 steps 2–3):
|
|
40
|
-
*
|
|
41
|
-
* `'*'`). All declared keys are required — there is no partial pass.
|
|
39
|
+
* Write-path authorization (§3.4 steps 2–3): all stored values must exist;
|
|
40
|
+
* the declared write variables must be allowed (or hold `'*'`).
|
|
42
41
|
*/
|
|
43
42
|
export declare function authorizeWrite(table: CompiledTable, rowScopes: StoredScopes, resolved: ResolvedScopes): boolean;
|
|
44
43
|
/** Does a change's stored scopes match the effective scopes (§3.2)? */
|
package/dist/scopes.js
CHANGED
|
@@ -57,9 +57,8 @@ export function storedScopesForRow(table, values) {
|
|
|
57
57
|
return { scopes };
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
|
-
* Write-path authorization (§3.4 steps 2–3):
|
|
61
|
-
*
|
|
62
|
-
* `'*'`). All declared keys are required — there is no partial pass.
|
|
60
|
+
* Write-path authorization (§3.4 steps 2–3): all stored values must exist;
|
|
61
|
+
* the declared write variables must be allowed (or hold `'*'`).
|
|
63
62
|
*/
|
|
64
63
|
export function authorizeWrite(table, rowScopes, resolved) {
|
|
65
64
|
if (!resolved.ok)
|
|
@@ -68,7 +67,12 @@ export function authorizeWrite(table, rowScopes, resolved) {
|
|
|
68
67
|
const value = rowScopes[pattern.variable];
|
|
69
68
|
if (value === undefined || value.length === 0)
|
|
70
69
|
return false;
|
|
71
|
-
|
|
70
|
+
}
|
|
71
|
+
for (const variable of table.writeScopeVariables) {
|
|
72
|
+
const value = rowScopes[variable];
|
|
73
|
+
if (value === undefined)
|
|
74
|
+
return false;
|
|
75
|
+
const allowedValues = resolved.allowed[variable];
|
|
72
76
|
if (allowedValues === undefined)
|
|
73
77
|
return false;
|
|
74
78
|
if (!allowedValues.includes('*') && !allowedValues.includes(value)) {
|
package/dist/sqlite-dialect.js
CHANGED
|
@@ -197,6 +197,9 @@ export function serializePushResult(result) {
|
|
|
197
197
|
...(result.cacheIdentity !== undefined
|
|
198
198
|
? { cacheIdentity: result.cacheIdentity }
|
|
199
199
|
: {}),
|
|
200
|
+
...(result.conflictAuthorization !== undefined
|
|
201
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
202
|
+
: {}),
|
|
200
203
|
results: result.results.map((record) => {
|
|
201
204
|
if (record.status === 'conflict') {
|
|
202
205
|
return {
|
|
@@ -258,6 +261,9 @@ export function deserializePushResult(text) {
|
|
|
258
261
|
...(parsed.cacheIdentity !== undefined
|
|
259
262
|
? { cacheIdentity: parsed.cacheIdentity }
|
|
260
263
|
: {}),
|
|
264
|
+
...(parsed.conflictAuthorization !== undefined
|
|
265
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
266
|
+
: {}),
|
|
261
267
|
results,
|
|
262
268
|
};
|
|
263
269
|
}
|
package/dist/storage.d.ts
CHANGED
|
@@ -234,6 +234,12 @@ export interface StoredPushResult {
|
|
|
234
234
|
readonly recordedAtMs?: number;
|
|
235
235
|
/** Privacy-safe identity used to distinguish this stored outcome from a race. */
|
|
236
236
|
readonly cacheIdentity?: string;
|
|
237
|
+
/** The scope of a persisted conflict snapshot, for replay authorization. */
|
|
238
|
+
readonly conflictAuthorization?: {
|
|
239
|
+
readonly table: string;
|
|
240
|
+
readonly schemaVersion: number;
|
|
241
|
+
readonly scopes: Readonly<Record<string, string>>;
|
|
242
|
+
};
|
|
237
243
|
readonly results: readonly PushOperationResult[];
|
|
238
244
|
}
|
|
239
245
|
export interface ClientSubscription {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.50.0",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.50.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/postgres-storage.ts
CHANGED
|
@@ -678,6 +678,9 @@ function serializePushResult(result: StoredPushResult): unknown {
|
|
|
678
678
|
...(result.cacheIdentity !== undefined
|
|
679
679
|
? { cacheIdentity: result.cacheIdentity }
|
|
680
680
|
: {}),
|
|
681
|
+
...(result.conflictAuthorization !== undefined
|
|
682
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
683
|
+
: {}),
|
|
681
684
|
results: result.results.map((record) => {
|
|
682
685
|
if (record.status === 'conflict') {
|
|
683
686
|
return {
|
|
@@ -711,6 +714,7 @@ function deserializePushResult(value: unknown): StoredPushResult {
|
|
|
711
714
|
commitSeq?: number;
|
|
712
715
|
recordedAtMs?: number;
|
|
713
716
|
cacheIdentity?: string;
|
|
717
|
+
conflictAuthorization?: StoredPushResult['conflictAuthorization'];
|
|
714
718
|
results: SerializedResult[];
|
|
715
719
|
};
|
|
716
720
|
const results: PushOperationResult[] = parsed.results.map((record) => {
|
|
@@ -746,6 +750,9 @@ function deserializePushResult(value: unknown): StoredPushResult {
|
|
|
746
750
|
...(parsed.cacheIdentity !== undefined
|
|
747
751
|
? { cacheIdentity: parsed.cacheIdentity }
|
|
748
752
|
: {}),
|
|
753
|
+
...(parsed.conflictAuthorization !== undefined
|
|
754
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
755
|
+
: {}),
|
|
749
756
|
results,
|
|
750
757
|
};
|
|
751
758
|
}
|
package/src/push.ts
CHANGED
|
@@ -1263,6 +1263,7 @@ function referenceRejection(
|
|
|
1263
1263
|
async function enforceReferences(
|
|
1264
1264
|
tx: StorageTransaction,
|
|
1265
1265
|
schema: CompiledSchema,
|
|
1266
|
+
resolved: ResolvedScopes,
|
|
1266
1267
|
staged: readonly StagedWrite[],
|
|
1267
1268
|
clientOpCount: number,
|
|
1268
1269
|
cascadeLimit: number,
|
|
@@ -1432,6 +1433,13 @@ async function enforceReferences(
|
|
|
1432
1433
|
);
|
|
1433
1434
|
}
|
|
1434
1435
|
for (const childRow of children) {
|
|
1436
|
+
if (!authorizeWrite(child, childRow.scopes, resolved)) {
|
|
1437
|
+
return errorRecord(
|
|
1438
|
+
item.originOpIndex,
|
|
1439
|
+
'sync.forbidden',
|
|
1440
|
+
'cascade denied by scope authorization (§3.4)',
|
|
1441
|
+
);
|
|
1442
|
+
}
|
|
1435
1443
|
if (reference.onDelete === 'CASCADE') {
|
|
1436
1444
|
if (
|
|
1437
1445
|
!schedule(
|
|
@@ -1772,6 +1780,36 @@ export async function processPushOperationsWithTrace(
|
|
|
1772
1780
|
throw serveNotReadyError(refusal);
|
|
1773
1781
|
}
|
|
1774
1782
|
if (prepared.persisted !== undefined) {
|
|
1783
|
+
const conflict = prepared.persisted.results.find(
|
|
1784
|
+
(result) => result.status === 'conflict',
|
|
1785
|
+
);
|
|
1786
|
+
const authorization = prepared.persisted.conflictAuthorization;
|
|
1787
|
+
const table = authorization && schema.tables.get(authorization.table);
|
|
1788
|
+
if (
|
|
1789
|
+
conflict !== undefined &&
|
|
1790
|
+
(authorization === undefined ||
|
|
1791
|
+
authorization.schemaVersion !== schema.version ||
|
|
1792
|
+
table === undefined ||
|
|
1793
|
+
!authorizeWrite(table, authorization.scopes, resolved))
|
|
1794
|
+
) {
|
|
1795
|
+
await tx.rollback();
|
|
1796
|
+
return processedPushCommit(
|
|
1797
|
+
clientCommitId,
|
|
1798
|
+
{
|
|
1799
|
+
...prepared.persisted,
|
|
1800
|
+
results: [
|
|
1801
|
+
{
|
|
1802
|
+
opIndex: conflict.opIndex,
|
|
1803
|
+
status: 'error',
|
|
1804
|
+
code: 'sync.forbidden',
|
|
1805
|
+
message: 'conflict replay denied by scope authorization (§3.4)',
|
|
1806
|
+
retryable: false,
|
|
1807
|
+
},
|
|
1808
|
+
],
|
|
1809
|
+
},
|
|
1810
|
+
true,
|
|
1811
|
+
);
|
|
1812
|
+
}
|
|
1775
1813
|
await tx.rollback();
|
|
1776
1814
|
return processedPushCommit(clientCommitId, prepared.persisted, true);
|
|
1777
1815
|
}
|
|
@@ -1829,6 +1867,7 @@ export async function processPushOperationsWithTrace(
|
|
|
1829
1867
|
const referencePass = await enforceReferences(
|
|
1830
1868
|
tx,
|
|
1831
1869
|
schema,
|
|
1870
|
+
resolved,
|
|
1832
1871
|
stagedWrites,
|
|
1833
1872
|
operations.length,
|
|
1834
1873
|
limitsOf(ctx).maxCascadeOperationsPerCommit,
|
|
@@ -1887,8 +1926,31 @@ export async function processPushOperationsWithTrace(
|
|
|
1887
1926
|
if (terminated !== undefined) {
|
|
1888
1927
|
// §6.3 rejected: only the terminating operation's record; §6.4:
|
|
1889
1928
|
// every write of the commit rolls back.
|
|
1929
|
+
const conflictOperation =
|
|
1930
|
+
terminated.status === 'conflict'
|
|
1931
|
+
? operations[terminated.opIndex]
|
|
1932
|
+
: undefined;
|
|
1933
|
+
const conflictRow =
|
|
1934
|
+
conflictOperation === undefined
|
|
1935
|
+
? undefined
|
|
1936
|
+
: await tx.getRow(conflictOperation.table, conflictOperation.rowId);
|
|
1937
|
+
if (
|
|
1938
|
+
terminated.status === 'conflict' &&
|
|
1939
|
+
(conflictOperation === undefined || conflictRow === undefined)
|
|
1940
|
+
) {
|
|
1941
|
+
throw new Error('conflict snapshot has no authorization row');
|
|
1942
|
+
}
|
|
1890
1943
|
const stored = newStoredPushResult(createdAtMs, {
|
|
1891
1944
|
status: 'rejected',
|
|
1945
|
+
...(conflictOperation !== undefined && conflictRow !== undefined
|
|
1946
|
+
? {
|
|
1947
|
+
conflictAuthorization: {
|
|
1948
|
+
table: conflictOperation.table,
|
|
1949
|
+
schemaVersion: schema.version,
|
|
1950
|
+
scopes: conflictRow.scopes,
|
|
1951
|
+
},
|
|
1952
|
+
}
|
|
1953
|
+
: {}),
|
|
1892
1954
|
results: [terminated],
|
|
1893
1955
|
});
|
|
1894
1956
|
// Discard candidates and persist the rejection while retaining the same
|
package/src/schema.ts
CHANGED
|
@@ -75,6 +75,8 @@ export interface TableSchema {
|
|
|
75
75
|
readonly primaryKey: string;
|
|
76
76
|
/** Scope patterns (§3.1). Every synced table declares at least one. */
|
|
77
77
|
readonly scopes: readonly ScopePatternSpec[];
|
|
78
|
+
/** Variables required together for writes; defaults to every scope variable. */
|
|
79
|
+
readonly writeScopeVariables?: readonly string[];
|
|
78
80
|
/** User indexes (optional) — created on the server's relational tables. */
|
|
79
81
|
readonly indexes?: readonly IndexSchema[];
|
|
80
82
|
/** Server-only indexes (optional); names share the namespace of `indexes`. */
|
|
@@ -120,6 +122,7 @@ export interface CompiledTable {
|
|
|
120
122
|
readonly columns: readonly RowColumn[];
|
|
121
123
|
readonly primaryKeyIndex: number;
|
|
122
124
|
readonly scopePatterns: readonly CompiledScopePattern[];
|
|
125
|
+
readonly writeScopeVariables: readonly string[];
|
|
123
126
|
/** User indexes (validated: unique names, existing columns). */
|
|
124
127
|
readonly indexes: readonly IndexSchema[];
|
|
125
128
|
/** Server-only indexes (validated like `indexes`). */
|
|
@@ -318,6 +321,17 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
318
321
|
variables.add(pattern.variable);
|
|
319
322
|
declaredVariables.add(pattern.variable);
|
|
320
323
|
}
|
|
324
|
+
const writeScopeVariables = table.writeScopeVariables ?? [...variables];
|
|
325
|
+
if (
|
|
326
|
+
!Array.isArray(writeScopeVariables) ||
|
|
327
|
+
writeScopeVariables.length === 0 ||
|
|
328
|
+
new Set(writeScopeVariables).size !== writeScopeVariables.length ||
|
|
329
|
+
writeScopeVariables.some((variable) => !variables.has(variable))
|
|
330
|
+
) {
|
|
331
|
+
throw new Error(
|
|
332
|
+
`table ${table.name}: writeScopeVariables must contain distinct declared scope variables (§3.4)`,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
321
335
|
const blobRefColumnIndices: number[] = [];
|
|
322
336
|
const crdtColumns: { index: number; crdtType: string }[] = [];
|
|
323
337
|
const encryptedColumnIndices: number[] = [];
|
|
@@ -372,6 +386,7 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
372
386
|
columns: table.columns,
|
|
373
387
|
primaryKeyIndex,
|
|
374
388
|
scopePatterns,
|
|
389
|
+
writeScopeVariables,
|
|
375
390
|
indexes,
|
|
376
391
|
serverIndexes,
|
|
377
392
|
references,
|
package/src/scopes.ts
CHANGED
|
@@ -77,9 +77,8 @@ export function storedScopesForRow(
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
|
-
* Write-path authorization (§3.4 steps 2–3):
|
|
81
|
-
*
|
|
82
|
-
* `'*'`). All declared keys are required — there is no partial pass.
|
|
80
|
+
* Write-path authorization (§3.4 steps 2–3): all stored values must exist;
|
|
81
|
+
* the declared write variables must be allowed (or hold `'*'`).
|
|
83
82
|
*/
|
|
84
83
|
export function authorizeWrite(
|
|
85
84
|
table: CompiledTable,
|
|
@@ -90,7 +89,11 @@ export function authorizeWrite(
|
|
|
90
89
|
for (const pattern of table.scopePatterns) {
|
|
91
90
|
const value = rowScopes[pattern.variable];
|
|
92
91
|
if (value === undefined || value.length === 0) return false;
|
|
93
|
-
|
|
92
|
+
}
|
|
93
|
+
for (const variable of table.writeScopeVariables) {
|
|
94
|
+
const value = rowScopes[variable];
|
|
95
|
+
if (value === undefined) return false;
|
|
96
|
+
const allowedValues = resolved.allowed[variable];
|
|
94
97
|
if (allowedValues === undefined) return false;
|
|
95
98
|
if (!allowedValues.includes('*') && !allowedValues.includes(value)) {
|
|
96
99
|
return false;
|
package/src/sqlite-dialect.ts
CHANGED
|
@@ -247,6 +247,9 @@ export function serializePushResult(result: StoredPushResult): string {
|
|
|
247
247
|
...(result.cacheIdentity !== undefined
|
|
248
248
|
? { cacheIdentity: result.cacheIdentity }
|
|
249
249
|
: {}),
|
|
250
|
+
...(result.conflictAuthorization !== undefined
|
|
251
|
+
? { conflictAuthorization: result.conflictAuthorization }
|
|
252
|
+
: {}),
|
|
250
253
|
results: result.results.map((record) => {
|
|
251
254
|
if (record.status === 'conflict') {
|
|
252
255
|
return {
|
|
@@ -280,6 +283,7 @@ export function deserializePushResult(text: string): StoredPushResult {
|
|
|
280
283
|
commitSeq?: number;
|
|
281
284
|
recordedAtMs?: number;
|
|
282
285
|
cacheIdentity?: string;
|
|
286
|
+
conflictAuthorization?: StoredPushResult['conflictAuthorization'];
|
|
283
287
|
results: SerializedResult[];
|
|
284
288
|
};
|
|
285
289
|
const results: PushOperationResult[] = parsed.results.map((record) => {
|
|
@@ -315,6 +319,9 @@ export function deserializePushResult(text: string): StoredPushResult {
|
|
|
315
319
|
...(parsed.cacheIdentity !== undefined
|
|
316
320
|
? { cacheIdentity: parsed.cacheIdentity }
|
|
317
321
|
: {}),
|
|
322
|
+
...(parsed.conflictAuthorization !== undefined
|
|
323
|
+
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
324
|
+
: {}),
|
|
318
325
|
results,
|
|
319
326
|
};
|
|
320
327
|
}
|
package/src/storage.ts
CHANGED
|
@@ -323,6 +323,12 @@ export interface StoredPushResult {
|
|
|
323
323
|
readonly recordedAtMs?: number;
|
|
324
324
|
/** Privacy-safe identity used to distinguish this stored outcome from a race. */
|
|
325
325
|
readonly cacheIdentity?: string;
|
|
326
|
+
/** The scope of a persisted conflict snapshot, for replay authorization. */
|
|
327
|
+
readonly conflictAuthorization?: {
|
|
328
|
+
readonly table: string;
|
|
329
|
+
readonly schemaVersion: number;
|
|
330
|
+
readonly scopes: Readonly<Record<string, string>>;
|
|
331
|
+
};
|
|
326
332
|
readonly results: readonly PushOperationResult[];
|
|
327
333
|
}
|
|
328
334
|
|