@syncular/server 0.83.0 → 0.85.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/d1-storage.js +2 -2
- package/dist/postgres-storage.js +23 -195
- package/dist/push.js +5 -22
- package/dist/schema.d.ts +5 -3
- package/dist/schema.js +16 -7
- package/dist/scopes.d.ts +7 -5
- package/dist/scopes.js +21 -22
- package/dist/sqlite-dialect.d.ts +1 -6
- package/dist/sqlite-dialect.js +3 -186
- package/dist/sqlite-storage.js +8 -2
- package/dist/storage.d.ts +71 -0
- package/dist/storage.js +205 -0
- package/package.json +2 -2
- package/src/d1-storage.ts +6 -4
- package/src/postgres-storage.ts +48 -308
- package/src/push.ts +5 -33
- package/src/schema.ts +32 -10
- package/src/scopes.ts +30 -19
- package/src/sqlite-dialect.ts +18 -257
- package/src/sqlite-storage.ts +8 -6
- package/src/storage.ts +292 -0
package/dist/d1-storage.js
CHANGED
|
@@ -44,8 +44,8 @@ var _a;
|
|
|
44
44
|
import { decodeRow } from '@syncular/core';
|
|
45
45
|
import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanProbeSql, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
|
|
46
46
|
import { matchesEffective } from './scopes.js';
|
|
47
|
-
import {
|
|
48
|
-
import { commitDeleteFloor } from './storage.js';
|
|
47
|
+
import { deserializePushResult, placeholders, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteDdlStatements, SQLITE_UPGRADE_PROBE, sqliteUpgradeDdl, } from './sqlite-dialect.js';
|
|
48
|
+
import { asUint8Array, commitDeleteFloor, toStoredRow, toTombstone, } from './storage.js';
|
|
49
49
|
import { isD1ConstraintError, StorageConstraintError, StorageQueryError, } from './storage-errors.js';
|
|
50
50
|
import { chooseScanVariable, resolveIndexRowScan } from './storage-query.js';
|
|
51
51
|
/** The rows of one `db.batch` result entry (`{ results }` per statement). */
|
package/dist/postgres-storage.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { validateCommitPruneQuery } from './prune.js';
|
|
2
2
|
import { StorageQueryError } from './storage-errors.js';
|
|
3
|
-
import {
|
|
3
|
+
import { asJson, CLIENT_RECORD_COLUMNS, pushResultFromJson, pushResultToJson, toClientRecord, toStoredChange, toStoredReaction, toStoredRow, toTombstone, PARTITION_TOUCH_INTERVAL_MS, commitDeleteFloor, rowsLiveAfterDelete, serveGateRefusal, } from './storage.js';
|
|
4
4
|
import { serveNotReadyError } from './readiness.js';
|
|
5
5
|
/**
|
|
6
6
|
* Postgres server storage: the production database path.
|
|
@@ -39,7 +39,7 @@ import { serveNotReadyError } from './readiness.js';
|
|
|
39
39
|
* would leave gaps on rollback, which the pull-window arithmetic in §4.5
|
|
40
40
|
* does not tolerate). Cross-partition pushes never contend.
|
|
41
41
|
*/
|
|
42
|
-
import { decodeRow
|
|
42
|
+
import { decodeRow } from '@syncular/core';
|
|
43
43
|
import { bindAuthoritativePartition, postgresPlaceholders, prepareAuthoritativeQuery, } from './authoritative-query.js';
|
|
44
44
|
import { syncError } from './errors.js';
|
|
45
45
|
import { asBytes, asNumber, } from './pg-executor.js';
|
|
@@ -462,9 +462,6 @@ async function hasSourceChangesAboveOn(client, partition, tables, seq) {
|
|
|
462
462
|
function toBase64(bytes) {
|
|
463
463
|
return Buffer.from(bytes).toString('base64');
|
|
464
464
|
}
|
|
465
|
-
function fromBase64(text) {
|
|
466
|
-
return new Uint8Array(Buffer.from(text, 'base64'));
|
|
467
|
-
}
|
|
468
465
|
function toStoredCheckpoint(record) {
|
|
469
466
|
return {
|
|
470
467
|
partition: record.partition,
|
|
@@ -477,188 +474,6 @@ function toStoredCheckpoint(record) {
|
|
|
477
474
|
updatedAtMs: asNumber(record.updated_at_ms),
|
|
478
475
|
};
|
|
479
476
|
}
|
|
480
|
-
function toStoredReaction(record) {
|
|
481
|
-
return {
|
|
482
|
-
idempotencyKey: record.idempotency_key,
|
|
483
|
-
type: record.type,
|
|
484
|
-
version: asNumber(record.version),
|
|
485
|
-
payload: asJson(record.payload),
|
|
486
|
-
sourceClientId: record.source_client_id,
|
|
487
|
-
sourceClientCommitId: record.source_client_commit_id,
|
|
488
|
-
sourceCommitSeq: asNumber(record.source_commit_seq),
|
|
489
|
-
createdAtMs: asNumber(record.created_at_ms),
|
|
490
|
-
maxAttempts: asNumber(record.max_attempts),
|
|
491
|
-
status: record.status,
|
|
492
|
-
attempts: asNumber(record.attempts),
|
|
493
|
-
availableAtMs: asNumber(record.available_at_ms),
|
|
494
|
-
...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
|
|
495
|
-
...(record.lease_expires_at_ms !== null
|
|
496
|
-
? { leaseExpiresAtMs: asNumber(record.lease_expires_at_ms) }
|
|
497
|
-
: {}),
|
|
498
|
-
...(record.completed_at_ms !== null
|
|
499
|
-
? { completedAtMs: asNumber(record.completed_at_ms) }
|
|
500
|
-
: {}),
|
|
501
|
-
...(record.last_failure !== null
|
|
502
|
-
? { lastFailure: asJson(record.last_failure) }
|
|
503
|
-
: {}),
|
|
504
|
-
};
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Serialize a push result to a JSON-able object (stored in a JSONB column).
|
|
508
|
-
* `serverRow` bytes are base64-encoded — JSONB cannot hold raw bytes.
|
|
509
|
-
*/
|
|
510
|
-
function serializePushResult(result) {
|
|
511
|
-
return {
|
|
512
|
-
status: result.status,
|
|
513
|
-
...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
|
|
514
|
-
...(result.recordedAtMs !== undefined
|
|
515
|
-
? { recordedAtMs: result.recordedAtMs }
|
|
516
|
-
: {}),
|
|
517
|
-
...(result.cacheIdentity !== undefined
|
|
518
|
-
? { cacheIdentity: result.cacheIdentity }
|
|
519
|
-
: {}),
|
|
520
|
-
...(result.conflictAuthorization !== undefined
|
|
521
|
-
? { conflictAuthorization: result.conflictAuthorization }
|
|
522
|
-
: {}),
|
|
523
|
-
results: result.results.map((record) => {
|
|
524
|
-
if (record.status === 'conflict') {
|
|
525
|
-
return {
|
|
526
|
-
opIndex: record.opIndex,
|
|
527
|
-
status: record.status,
|
|
528
|
-
code: record.code,
|
|
529
|
-
message: record.message,
|
|
530
|
-
serverVersion: record.serverVersion,
|
|
531
|
-
serverRow: toBase64(record.serverRow),
|
|
532
|
-
conflictColumns: toBase64(record.conflictColumns),
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
if (record.status === 'error') {
|
|
536
|
-
return {
|
|
537
|
-
opIndex: record.opIndex,
|
|
538
|
-
status: record.status,
|
|
539
|
-
code: record.code,
|
|
540
|
-
message: record.message,
|
|
541
|
-
retryable: record.retryable,
|
|
542
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
return { opIndex: record.opIndex, status: record.status };
|
|
546
|
-
}),
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
function deserializePushResult(value) {
|
|
550
|
-
const parsed = value;
|
|
551
|
-
const results = parsed.results.map((record) => {
|
|
552
|
-
if (record.status === 'conflict') {
|
|
553
|
-
return {
|
|
554
|
-
opIndex: record.opIndex,
|
|
555
|
-
status: 'conflict',
|
|
556
|
-
code: record.code ?? '',
|
|
557
|
-
message: record.message ?? '',
|
|
558
|
-
serverVersion: record.serverVersion ?? 0,
|
|
559
|
-
serverRow: fromBase64(record.serverRow ?? ''),
|
|
560
|
-
conflictColumns: fromBase64(record.conflictColumns ?? ''),
|
|
561
|
-
};
|
|
562
|
-
}
|
|
563
|
-
if (record.status === 'error') {
|
|
564
|
-
return {
|
|
565
|
-
opIndex: record.opIndex,
|
|
566
|
-
status: 'error',
|
|
567
|
-
code: record.code ?? '',
|
|
568
|
-
message: record.message ?? '',
|
|
569
|
-
retryable: record.retryable ?? false,
|
|
570
|
-
...(record.details !== undefined ? { details: record.details } : {}),
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
|
-
return { opIndex: record.opIndex, status: 'applied' };
|
|
574
|
-
});
|
|
575
|
-
return {
|
|
576
|
-
status: parsed.status,
|
|
577
|
-
...(parsed.commitSeq !== undefined ? { commitSeq: parsed.commitSeq } : {}),
|
|
578
|
-
...(parsed.recordedAtMs !== undefined
|
|
579
|
-
? { recordedAtMs: parsed.recordedAtMs }
|
|
580
|
-
: {}),
|
|
581
|
-
...(parsed.cacheIdentity !== undefined
|
|
582
|
-
? { cacheIdentity: parsed.cacheIdentity }
|
|
583
|
-
: {}),
|
|
584
|
-
...(parsed.conflictAuthorization !== undefined
|
|
585
|
-
? { conflictAuthorization: parsed.conflictAuthorization }
|
|
586
|
-
: {}),
|
|
587
|
-
results,
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Some drivers return a JSONB column already parsed (pglite, node-postgres);
|
|
592
|
-
* accept a string too for defensiveness.
|
|
593
|
-
*/
|
|
594
|
-
function asJson(value) {
|
|
595
|
-
if (typeof value === 'string')
|
|
596
|
-
return JSON.parse(value);
|
|
597
|
-
return value;
|
|
598
|
-
}
|
|
599
|
-
function toStoredRow(record) {
|
|
600
|
-
return {
|
|
601
|
-
rowId: record.row_id,
|
|
602
|
-
serverVersion: asNumber(record.server_version),
|
|
603
|
-
scopes: asJson(record.scopes),
|
|
604
|
-
payload: asBytes(record.payload),
|
|
605
|
-
...(record.column_versions === null || record.column_versions === undefined
|
|
606
|
-
? {}
|
|
607
|
-
: { columnVersions: asBytes(record.column_versions) }),
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
function toTombstone(seq, version, scopes) {
|
|
611
|
-
if (seq === null || seq === undefined)
|
|
612
|
-
return null;
|
|
613
|
-
return {
|
|
614
|
-
commitSeq: asNumber(seq),
|
|
615
|
-
...(version === null || version === undefined
|
|
616
|
-
? {}
|
|
617
|
-
: { serverVersion: asNumber(version) }),
|
|
618
|
-
...(scopes === null || scopes === undefined
|
|
619
|
-
? {}
|
|
620
|
-
: { scopes: asJson(scopes) }),
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
const CLIENT_RECORD_COLUMNS = 'client_id, actor_id, wire_version, cursor, updated_at_ms, schema_version, units, registration_digest, catching_up, last_round_at_ms, last_error_code, consecutive_failures';
|
|
624
|
-
function toClientRecord(record) {
|
|
625
|
-
return {
|
|
626
|
-
clientId: record.client_id,
|
|
627
|
-
actorId: record.actor_id,
|
|
628
|
-
wireVersion: asNumber(record.wire_version),
|
|
629
|
-
cursor: asNumber(record.cursor),
|
|
630
|
-
updatedAtMs: asNumber(record.updated_at_ms),
|
|
631
|
-
...(record.schema_version === null
|
|
632
|
-
? {}
|
|
633
|
-
: { schemaVersion: asNumber(record.schema_version) }),
|
|
634
|
-
...(record.units === null || record.registration_digest === null
|
|
635
|
-
? {}
|
|
636
|
-
: {
|
|
637
|
-
registration: {
|
|
638
|
-
units: asJson(record.units),
|
|
639
|
-
digest: asBytes(record.registration_digest),
|
|
640
|
-
},
|
|
641
|
-
}),
|
|
642
|
-
catchingUp: asJson(record.catching_up),
|
|
643
|
-
...clientRoundState(record.last_round_at_ms === null
|
|
644
|
-
? null
|
|
645
|
-
: asNumber(record.last_round_at_ms), record.last_error_code, asNumber(record.consecutive_failures)),
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
function toStoredChange(record) {
|
|
649
|
-
return {
|
|
650
|
-
table: record.tbl,
|
|
651
|
-
rowId: record.row_id,
|
|
652
|
-
op: asNumber(record.op) === 1 ? 'upsert' : 'delete',
|
|
653
|
-
...(record.row_version !== null && record.row_version !== undefined
|
|
654
|
-
? { rowVersion: asNumber(record.row_version) }
|
|
655
|
-
: {}),
|
|
656
|
-
scopes: asJson(record.scopes),
|
|
657
|
-
...(record.payload !== null && record.payload !== undefined
|
|
658
|
-
? { payload: asBytes(record.payload) }
|
|
659
|
-
: {}),
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
477
|
/**
|
|
663
478
|
* Migration rewrite: keyset-paged walk of a row table inside the migration
|
|
664
479
|
* transaction. When `oldLayout` is
|
|
@@ -948,7 +763,7 @@ function toPersistedPushResult(value) {
|
|
|
948
763
|
if (value === null || value === undefined)
|
|
949
764
|
return undefined;
|
|
950
765
|
try {
|
|
951
|
-
return
|
|
766
|
+
return pushResultFromJson(asJson(value));
|
|
952
767
|
}
|
|
953
768
|
catch {
|
|
954
769
|
throw syncError('sync.idempotency_cache_miss', 'persisted push result unreadable (§6.3)');
|
|
@@ -1244,7 +1059,13 @@ class PostgresTransaction {
|
|
|
1244
1059
|
this.#prefetchedRows.set(key, record.row_id === null || record.row_id === undefined
|
|
1245
1060
|
? null
|
|
1246
1061
|
: toStoredRow({ ...record, row_id: record.row_id }));
|
|
1247
|
-
this.#prefetchedTombstones.set(key,
|
|
1062
|
+
this.#prefetchedTombstones.set(key, record.tombstone_seq === null || record.tombstone_seq === undefined
|
|
1063
|
+
? null
|
|
1064
|
+
: toTombstone({
|
|
1065
|
+
commit_seq: record.tombstone_seq,
|
|
1066
|
+
server_version: record.tombstone_version,
|
|
1067
|
+
scopes: record.tombstone_scopes,
|
|
1068
|
+
}));
|
|
1248
1069
|
}
|
|
1249
1070
|
}
|
|
1250
1071
|
async getTombstone(table, rowId) {
|
|
@@ -1254,8 +1075,9 @@ class PostgresTransaction {
|
|
|
1254
1075
|
return prefetched ?? undefined;
|
|
1255
1076
|
const { rows } = await this.#readOf([table]).query('SELECT commit_seq, server_version, scopes FROM sync_tombstones WHERE partition=$1 AND tbl=$2 AND row_id=$3', [this.#partition, table, rowId]);
|
|
1256
1077
|
const row = rows[0];
|
|
1257
|
-
return
|
|
1258
|
-
undefined
|
|
1078
|
+
return row === undefined || row.commit_seq === null
|
|
1079
|
+
? undefined
|
|
1080
|
+
: toTombstone(row);
|
|
1259
1081
|
}
|
|
1260
1082
|
async clearTombstone(table, rowId) {
|
|
1261
1083
|
this.#assertOpen();
|
|
@@ -1420,7 +1242,7 @@ class PostgresTransaction {
|
|
|
1420
1242
|
this.#pushResults.push({
|
|
1421
1243
|
client_id: clientId,
|
|
1422
1244
|
client_commit_id: clientCommitId,
|
|
1423
|
-
result:
|
|
1245
|
+
result: pushResultToJson(result),
|
|
1424
1246
|
outbox_id: sequence?.outboxId ?? null,
|
|
1425
1247
|
client_seq: sequence?.clientSeq ?? null,
|
|
1426
1248
|
});
|
|
@@ -1816,7 +1638,10 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
1816
1638
|
? undefined
|
|
1817
1639
|
: asNumber(marker.schema_version);
|
|
1818
1640
|
if (stored !== undefined && stored > schema.version) {
|
|
1819
|
-
throw new
|
|
1641
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
1642
|
+
storedVersion: String(stored),
|
|
1643
|
+
configuredVersion: String(schema.version),
|
|
1644
|
+
});
|
|
1820
1645
|
}
|
|
1821
1646
|
if (stored !== undefined && stored === schema.version) {
|
|
1822
1647
|
const storedLayouts = typeof marker.layouts === 'string' ? marker.layouts : undefined;
|
|
@@ -1880,7 +1705,10 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
1880
1705
|
? undefined
|
|
1881
1706
|
: asNumber(lockedMarker[0].schema_version);
|
|
1882
1707
|
if (lockedVersion !== undefined && lockedVersion > schema.version) {
|
|
1883
|
-
throw new
|
|
1708
|
+
throw new StorageQueryError('sync.storage.schema_changed', {
|
|
1709
|
+
storedVersion: String(lockedVersion),
|
|
1710
|
+
configuredVersion: String(schema.version),
|
|
1711
|
+
});
|
|
1884
1712
|
}
|
|
1885
1713
|
if (lockedVersion === schema.version)
|
|
1886
1714
|
return;
|
|
@@ -2776,7 +2604,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2776
2604
|
try {
|
|
2777
2605
|
return {
|
|
2778
2606
|
clientId: row.client_id,
|
|
2779
|
-
result:
|
|
2607
|
+
result: pushResultFromJson(asJson(row.result)),
|
|
2780
2608
|
};
|
|
2781
2609
|
}
|
|
2782
2610
|
catch {
|
package/dist/push.js
CHANGED
|
@@ -199,13 +199,9 @@ deleteFloor) {
|
|
|
199
199
|
if (stored === undefined) {
|
|
200
200
|
return errorRecord(opIndex, 'sync.row_missing', 'rescope targets an absent row (§2.2)');
|
|
201
201
|
}
|
|
202
|
-
if (!authorizeWrite(table, stored.scopes, resolved)) {
|
|
202
|
+
if (!authorizeWrite(table, stored.scopes, resolved, true)) {
|
|
203
203
|
return errorRecord(opIndex, 'sync.forbidden', 'rescope denied by scope authorization (§3.4)');
|
|
204
204
|
}
|
|
205
|
-
const missing = missingScopeVariable(table, stored.scopes);
|
|
206
|
-
if (missing !== undefined) {
|
|
207
|
-
return errorRecord(opIndex, 'sync.missing_scopes', `stored row lacks scope variable ${JSON.stringify(missing)} (§3.1)`);
|
|
208
|
-
}
|
|
209
205
|
let sparse;
|
|
210
206
|
try {
|
|
211
207
|
sparse = decodeSparseRow(wireTable.columns, wireTable.primaryKeyIndex, op.payload);
|
|
@@ -244,7 +240,7 @@ deleteFloor) {
|
|
|
244
240
|
if ('missing' in extracted) {
|
|
245
241
|
return errorRecord(opIndex, 'sync.forbidden', `rescope missing scope column value for ${JSON.stringify(extracted.missing)} (§3.4)`);
|
|
246
242
|
}
|
|
247
|
-
if (!authorizeWrite(table, extracted.scopes, resolved)) {
|
|
243
|
+
if (!authorizeWrite(table, extracted.scopes, resolved, true)) {
|
|
248
244
|
return errorRecord(opIndex, 'sync.forbidden', 'rescope denied by scope authorization of the new scopes (§3.4)');
|
|
249
245
|
}
|
|
250
246
|
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex, tx, partition, actorId, context);
|
|
@@ -363,10 +359,6 @@ deleteFloor) {
|
|
|
363
359
|
if (!authorizeWrite(table, stored.scopes, resolved)) {
|
|
364
360
|
return errorRecord(opIndex, 'sync.forbidden', 'delete denied by scope authorization (§3.4)');
|
|
365
361
|
}
|
|
366
|
-
const missing = missingScopeVariable(table, stored.scopes);
|
|
367
|
-
if (missing !== undefined) {
|
|
368
|
-
return errorRecord(opIndex, 'sync.missing_scopes', `stored row lacks scope variable ${JSON.stringify(missing)} (§3.1)`);
|
|
369
|
-
}
|
|
370
362
|
// §6.7: validate the delete against the stored row (row = undefined,
|
|
371
363
|
// stored = the row about to be removed). Only reached for an existing
|
|
372
364
|
// row — an absent-row delete is an idempotent no-op above.
|
|
@@ -559,14 +551,13 @@ deleteFloor) {
|
|
|
559
551
|
},
|
|
560
552
|
};
|
|
561
553
|
}
|
|
562
|
-
// Insert path: no stored row (§5.2). The
|
|
563
|
-
// plus the primary key (§3.4
|
|
564
|
-
// ⇒ deny.
|
|
554
|
+
// Insert path: no stored row (§5.2). The owner set authorizes the payload
|
|
555
|
+
// plus the primary key (§3.4 steps 2 and 3).
|
|
565
556
|
const values = sparse.map((value) => value ?? null);
|
|
566
557
|
const extracted = storedScopesForRow(table, values);
|
|
567
558
|
const insert = 'missing' in extracted
|
|
568
559
|
? errorRecord(opIndex, 'sync.forbidden', 'insert missing scope column value (§3.4)')
|
|
569
|
-
: authorizeWrite(table, extracted.scopes, resolved)
|
|
560
|
+
: authorizeWrite(table, extracted.scopes, resolved, true)
|
|
570
561
|
? extracted
|
|
571
562
|
: errorRecord(opIndex, 'sync.forbidden', 'insert denied by scope authorization (§3.4)');
|
|
572
563
|
if (op.baseVersion !== undefined && op.baseVersion !== 0) {
|
|
@@ -695,14 +686,6 @@ async function checkAndRecordBlobs(tx, table, rowId, values, opIndex, blobCtx) {
|
|
|
695
686
|
}
|
|
696
687
|
return undefined;
|
|
697
688
|
}
|
|
698
|
-
function missingScopeVariable(table, scopes) {
|
|
699
|
-
for (const pattern of table.scopePatterns) {
|
|
700
|
-
const value = scopes[pattern.variable];
|
|
701
|
-
if (value === undefined || value.length === 0)
|
|
702
|
-
return pattern.variable;
|
|
703
|
-
}
|
|
704
|
-
return undefined;
|
|
705
|
-
}
|
|
706
689
|
function commitValidationReader(tx, schema) {
|
|
707
690
|
const tableFor = (name) => {
|
|
708
691
|
const table = schema.tables.get(name);
|
package/dist/schema.d.ts
CHANGED
|
@@ -67,8 +67,9 @@ 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
|
-
/**
|
|
71
|
-
|
|
70
|
+
/** Write sets (§3.4): one list of variables, or alternative lists whose
|
|
71
|
+
* first is the owner set. Defaults to one set of every scope variable. */
|
|
72
|
+
readonly writeScopeVariables?: readonly string[] | readonly (readonly string[])[];
|
|
72
73
|
/** User indexes (optional) — created on the server's relational tables. */
|
|
73
74
|
readonly indexes?: readonly IndexSchema[];
|
|
74
75
|
/** Server-only indexes (optional); names share the namespace of `indexes`. */
|
|
@@ -111,7 +112,8 @@ export interface CompiledTable {
|
|
|
111
112
|
readonly columns: readonly RowColumn[];
|
|
112
113
|
readonly primaryKeyIndex: number;
|
|
113
114
|
readonly scopePatterns: readonly CompiledScopePattern[];
|
|
114
|
-
|
|
115
|
+
/** §3.4 write sets; the first is the owner set. */
|
|
116
|
+
readonly writeScopeSets: readonly (readonly string[])[];
|
|
115
117
|
/** User indexes (validated: unique names, existing columns). */
|
|
116
118
|
readonly indexes: readonly IndexSchema[];
|
|
117
119
|
/** Server-only indexes (validated like `indexes`). */
|
package/dist/schema.js
CHANGED
|
@@ -135,12 +135,21 @@ export function compileSchema(schema) {
|
|
|
135
135
|
variables.add(pattern.variable);
|
|
136
136
|
declaredVariables.add(pattern.variable);
|
|
137
137
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
138
|
+
// §3.4: one list is one set; a list of lists holds alternative sets.
|
|
139
|
+
const declaredSets = table.writeScopeVariables ?? [[...variables]];
|
|
140
|
+
const flatSet = declaredSets.filter((entry) => typeof entry === 'string');
|
|
141
|
+
const writeScopeSets = flatSet.length > 0
|
|
142
|
+
? [flatSet]
|
|
143
|
+
: declaredSets.filter((entry) => typeof entry !== 'string');
|
|
144
|
+
const setKeys = new Set(writeScopeSets.map((set) => JSON.stringify([...set].sort())));
|
|
145
|
+
if ((flatSet.length > 0 && flatSet.length !== declaredSets.length) ||
|
|
146
|
+
writeScopeSets.length === 0 ||
|
|
147
|
+
setKeys.size !== writeScopeSets.length ||
|
|
148
|
+
writeScopeSets.some((set) => !Array.isArray(set) ||
|
|
149
|
+
set.length === 0 ||
|
|
150
|
+
new Set(set).size !== set.length ||
|
|
151
|
+
set.some((variable) => !variables.has(variable)))) {
|
|
152
|
+
throw new Error(`table ${table.name}: writeScopeVariables must be one list or distinct lists of distinct declared scope variables (§3.4)`);
|
|
144
153
|
}
|
|
145
154
|
const blobRefColumnIndices = [];
|
|
146
155
|
const crdtColumns = [];
|
|
@@ -187,7 +196,7 @@ export function compileSchema(schema) {
|
|
|
187
196
|
columns: table.columns,
|
|
188
197
|
primaryKeyIndex,
|
|
189
198
|
scopePatterns,
|
|
190
|
-
|
|
199
|
+
writeScopeSets,
|
|
191
200
|
indexes,
|
|
192
201
|
serverIndexes,
|
|
193
202
|
references,
|
package/dist/scopes.d.ts
CHANGED
|
@@ -31,8 +31,9 @@ export declare function computeEffective(declared: ReadonlySet<string>, requeste
|
|
|
31
31
|
/** Render a stored scope column value; `undefined` means missing/empty. */
|
|
32
32
|
export declare function renderScopeValue(value: RowValue | undefined): string | undefined;
|
|
33
33
|
/**
|
|
34
|
-
* Extract the stored scopes (§3.1) from a decoded row.
|
|
35
|
-
*
|
|
34
|
+
* Extract the stored scopes (§3.1) from a decoded row. A NULL column outside
|
|
35
|
+
* the owner set leaves its variable without a stored scope. Returns the
|
|
36
|
+
* variable name when a column is empty, or NULL in the owner set.
|
|
36
37
|
*/
|
|
37
38
|
export declare function storedScopesForRow(table: CompiledTable, values: readonly RowValue[]): {
|
|
38
39
|
scopes: StoredScopes;
|
|
@@ -40,10 +41,11 @@ export declare function storedScopesForRow(table: CompiledTable, values: readonl
|
|
|
40
41
|
missing: string;
|
|
41
42
|
};
|
|
42
43
|
/**
|
|
43
|
-
* Write-path authorization (§3.4
|
|
44
|
-
*
|
|
44
|
+
* Write-path authorization (§3.4 step 3): a write set admits the row when
|
|
45
|
+
* each of its variables has a value the actor is allowed (or `'*'`). An
|
|
46
|
+
* insert and a rescope need the owner set; an existing row needs any set.
|
|
45
47
|
*/
|
|
46
|
-
export declare function authorizeWrite(table: CompiledTable, rowScopes: StoredScopes, resolved: ResolvedScopes): boolean;
|
|
48
|
+
export declare function authorizeWrite(table: CompiledTable, rowScopes: StoredScopes, resolved: ResolvedScopes, ownerOnly?: boolean): boolean;
|
|
47
49
|
/** Does a change's stored scopes match the effective scopes (§3.2)? */
|
|
48
50
|
export declare function matchesEffective(storedScopes: StoredScopes, effective: ScopeMap): boolean;
|
|
49
51
|
export declare function sha256Hex(bytes: Uint8Array): Promise<string>;
|
package/dist/scopes.js
CHANGED
|
@@ -61,13 +61,19 @@ export function renderScopeValue(value) {
|
|
|
61
61
|
return rendered.length === 0 ? undefined : rendered;
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
|
-
* Extract the stored scopes (§3.1) from a decoded row.
|
|
65
|
-
*
|
|
64
|
+
* Extract the stored scopes (§3.1) from a decoded row. A NULL column outside
|
|
65
|
+
* the owner set leaves its variable without a stored scope. Returns the
|
|
66
|
+
* variable name when a column is empty, or NULL in the owner set.
|
|
66
67
|
*/
|
|
67
68
|
export function storedScopesForRow(table, values) {
|
|
68
69
|
const scopes = {};
|
|
69
70
|
for (const pattern of table.scopePatterns) {
|
|
70
|
-
const
|
|
71
|
+
const raw = values[pattern.columnIndex];
|
|
72
|
+
if ((raw === null || raw === undefined) &&
|
|
73
|
+
!table.writeScopeSets[0].includes(pattern.variable)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const value = renderScopeValue(raw);
|
|
71
77
|
if (value === undefined)
|
|
72
78
|
return { missing: pattern.variable };
|
|
73
79
|
scopes[pattern.variable] = value;
|
|
@@ -75,29 +81,22 @@ export function storedScopesForRow(table, values) {
|
|
|
75
81
|
return { scopes };
|
|
76
82
|
}
|
|
77
83
|
/**
|
|
78
|
-
* Write-path authorization (§3.4
|
|
79
|
-
*
|
|
84
|
+
* Write-path authorization (§3.4 step 3): a write set admits the row when
|
|
85
|
+
* each of its variables has a value the actor is allowed (or `'*'`). An
|
|
86
|
+
* insert and a rescope need the owner set; an existing row needs any set.
|
|
80
87
|
*/
|
|
81
|
-
export function authorizeWrite(table, rowScopes, resolved) {
|
|
88
|
+
export function authorizeWrite(table, rowScopes, resolved, ownerOnly = false) {
|
|
82
89
|
if (!resolved.ok)
|
|
83
90
|
return false;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (value === undefined || value.length === 0)
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
for (const variable of table.writeScopeVariables) {
|
|
91
|
+
const { allowed } = resolved;
|
|
92
|
+
return (ownerOnly ? table.writeScopeSets.slice(0, 1) : table.writeScopeSets).some((set) => set.every((variable) => {
|
|
90
93
|
const value = rowScopes[variable];
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return true;
|
|
94
|
+
const values = allowed[variable];
|
|
95
|
+
return (value !== undefined &&
|
|
96
|
+
value.length > 0 &&
|
|
97
|
+
values !== undefined &&
|
|
98
|
+
(values.includes('*') || values.includes(value)));
|
|
99
|
+
}));
|
|
101
100
|
}
|
|
102
101
|
/** Does a change's stored scopes match the effective scopes (§3.2)? */
|
|
103
102
|
export function matchesEffective(storedScopes, effective) {
|
package/dist/sqlite-dialect.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type { UnitScopes } from '@syncular/core';
|
|
|
18
18
|
import { type BoundAuthoritativeQuery } from './authoritative-query.js';
|
|
19
19
|
import type { CompiledTable } from './schema.js';
|
|
20
20
|
import type { SqliteValue } from './sqlite-driver.js';
|
|
21
|
-
import type { AuthoritativeQueryRequest, ClientCursorInfo, ClientPushResult, ClientRecord, ClientRecordWriteOptions, CommitDetail, CommitMetadata, CommitMetadataQuery, CommitPruneQuery, CommitPruneResult, IndexRowScanQuery, LiveWindow, LiveWindowQuery, NewCommit, OutboxSequence, OutboxSettlement, NewReaction, PartitionRegistryEntry, PrunedReactionCounts, PushWatermark, ReactionClaimQuery, ReactionFailureUpdate, ReactionListQuery, ReactionPruneQuery, RowChangeRecord, RowChangesQuery, RowListQuery, RowScanQuery, ScopeActivityQuery, ScopeCommitActivity, ScopeValueHistoryQuery, ScopeValueQuery,
|
|
21
|
+
import type { AuthoritativeQueryRequest, ClientCursorInfo, ClientPushResult, ClientRecord, ClientRecordWriteOptions, CommitDetail, CommitMetadata, CommitMetadataQuery, CommitPruneQuery, CommitPruneResult, IndexRowScanQuery, LiveWindow, LiveWindowQuery, NewCommit, OutboxSequence, OutboxSettlement, NewReaction, PartitionRegistryEntry, PrunedReactionCounts, PushWatermark, ReactionClaimQuery, ReactionFailureUpdate, ReactionListQuery, ReactionPruneQuery, RowChangeRecord, RowChangesQuery, RowListQuery, RowScanQuery, ScopeActivityQuery, ScopeCommitActivity, ScopeValueHistoryQuery, ScopeValueQuery, StoredPushResult, StoredReaction, StoredRow, Tombstone } from './storage.js';
|
|
22
22
|
/**
|
|
23
23
|
* Schema DDL, one statement per `;`-delimited chunk. Native SQLite applies
|
|
24
24
|
* the whole string via `db.exec(SQLITE_DDL)`; D1 applies each statement
|
|
@@ -77,16 +77,11 @@ export interface SqliteChangeRecord {
|
|
|
77
77
|
scopes: string;
|
|
78
78
|
payload: Uint8Array | null;
|
|
79
79
|
}
|
|
80
|
-
/** Native SQLite returns `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
|
|
81
|
-
export declare function asUint8Array(value: unknown): Uint8Array;
|
|
82
|
-
export declare function toStoredRow(record: SqliteRowRecord): StoredRow;
|
|
83
80
|
export interface SqliteTombstoneRecord {
|
|
84
81
|
commit_seq: number;
|
|
85
82
|
server_version: number | null;
|
|
86
83
|
scopes: string | null;
|
|
87
84
|
}
|
|
88
|
-
export declare function toTombstone(record: SqliteTombstoneRecord): Tombstone;
|
|
89
|
-
export declare function toStoredChange(record: SqliteChangeRecord): StoredChange;
|
|
90
85
|
/** One statement of an atomic unit: its SQL text and bound parameters. */
|
|
91
86
|
export interface SqliteStatementSpec {
|
|
92
87
|
readonly sql: string;
|