@syncular/server 0.94.0 → 0.96.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 -5
- package/dist/context.d.ts +4 -10
- package/dist/errors.js +8 -0
- package/dist/frame-bytes.js +1 -0
- package/dist/handler.d.ts +20 -2
- package/dist/handler.js +230 -57
- package/dist/postgres-storage.d.ts +3 -2
- package/dist/postgres-storage.js +18 -2
- package/dist/pull.d.ts +11 -3
- package/dist/pull.js +28 -21
- package/dist/push.js +41 -77
- package/dist/realtime.d.ts +5 -11
- package/dist/realtime.js +10 -23
- package/dist/relational-rows.d.ts +4 -2
- package/dist/relational-rows.js +8 -3
- package/dist/schema-window.js +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +2 -2
- package/dist/sqlite-dialect.d.ts +3 -1
- package/dist/sqlite-dialect.js +37 -5
- package/dist/storage.d.ts +12 -0
- package/dist/validate.d.ts +21 -39
- package/dist/validate.js +4 -3
- package/package.json +2 -2
- package/src/context.ts +4 -14
- package/src/errors.ts +8 -0
- package/src/frame-bytes.ts +1 -0
- package/src/handler.ts +286 -73
- package/src/postgres-storage.ts +32 -6
- package/src/pull.ts +62 -37
- package/src/push.ts +51 -129
- package/src/realtime.ts +10 -22
- package/src/relational-rows.ts +10 -3
- package/src/schema-window.ts +2 -2
- package/src/schema.ts +4 -4
- package/src/sqlite-dialect.ts +47 -6
- package/src/storage.ts +13 -0
- package/src/validate.ts +21 -48
package/dist/pull.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { PullSegmentSummary } from './events.js';
|
|
|
7
7
|
import { type CompiledSchema, type CompiledTable } from './schema.js';
|
|
8
8
|
import { type ResolvedScopes } from './scopes.js';
|
|
9
9
|
import type { SegmentRecord } from './segment-store.js';
|
|
10
|
-
import type { StoredCommit, StoredRow } from './storage.js';
|
|
10
|
+
import type { PartitionHorizons, StoredCommit, StoredRow } from './storage.js';
|
|
11
11
|
/** §4.2 accept bitmask. */
|
|
12
12
|
export declare const ACCEPT_INLINE_ROWS: number;
|
|
13
13
|
export declare const ACCEPT_EXTERNAL_ROWS: number;
|
|
@@ -67,6 +67,14 @@ interface TableStart {
|
|
|
67
67
|
readonly stored: SegmentRecord | undefined;
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* A section frame: a response frame, or a rows segment's `SEGMENT_REF`
|
|
72
|
+
* whose put is in flight. The consumer emits `ref` in its frame order.
|
|
73
|
+
*/
|
|
74
|
+
type SectionFrame = ResponseFrame | {
|
|
75
|
+
readonly type: 'PENDING_REF';
|
|
76
|
+
readonly ref: Promise<ResponseFrame>;
|
|
77
|
+
};
|
|
70
78
|
/** A bootstrapping unit's position and its table starts, read ahead (§4.7). */
|
|
71
79
|
export interface BootstrapPlan {
|
|
72
80
|
readonly unit: UnitPlan;
|
|
@@ -91,14 +99,14 @@ export interface BootstrapPlan {
|
|
|
91
99
|
* is awaited, so a storage that batches concurrent page reads (PostgreSQL,
|
|
92
100
|
* D1) answers them in one statement per table.
|
|
93
101
|
*/
|
|
94
|
-
export declare function planBootstrap(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, unit: UnitPlan, bootstrapState: string | undefined, maxSeq: number,
|
|
102
|
+
export declare function planBootstrap(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, unit: UnitPlan, bootstrapState: string | undefined, maxSeq: number, horizon: PartitionHorizons, logEpoch: string, keptCursor?: number): BootstrapPlan;
|
|
95
103
|
/**
|
|
96
104
|
* The segments of one bootstrap section (§4.7): tables in bootstrap order,
|
|
97
105
|
* each active pair's rows or image, up to `maxSnapshotPages` pages. Returns
|
|
98
106
|
* the resume token, or `undefined` once every table is exhausted. A pair the
|
|
99
107
|
* wildcard fence revoked receives no segment (§3.3).
|
|
100
108
|
*/
|
|
101
|
-
export declare function bootstrapSegments(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, plan: BootstrapPlan, trace: PullSectionTrace | undefined, logEpoch: string): AsyncGenerator<
|
|
109
|
+
export declare function bootstrapSegments(ctx: SyncRequestContext, schema: CompiledSchema, limits: PullLimits, plan: BootstrapPlan, trace: PullSectionTrace | undefined, logEpoch: string): AsyncGenerator<SectionFrame, string | undefined>;
|
|
102
110
|
export interface PublishSqliteImageInput {
|
|
103
111
|
/** The sync config the serving host runs, plus a `sqliteImageBuilder`. */
|
|
104
112
|
readonly config: SyncServerConfig;
|
package/dist/pull.js
CHANGED
|
@@ -264,9 +264,9 @@ async function readTableStart(ctx, schema, limits, unit, pair, startRowCursor, i
|
|
|
264
264
|
return { firstPage: await scan };
|
|
265
265
|
}
|
|
266
266
|
// §4.7: a sqlite-lane table pins at the newest commit carrying all of the
|
|
267
|
-
// unit's values (never below the horizon, never above
|
|
268
|
-
// stored image stays the reuse key's match until this
|
|
269
|
-
// (§5.3). The server computes the pin; a client token never supplies it.
|
|
267
|
+
// unit's values (never below the table's horizon, never above
|
|
268
|
+
// maxCommitSeq), so a stored image stays the reuse key's match until this
|
|
269
|
+
// scope changes (§5.3). The server computes the pin; a client token never supplies it.
|
|
270
270
|
const [firstPage, latest] = await Promise.all([
|
|
271
271
|
scan,
|
|
272
272
|
ctx.storage.latestScopedChangeSeq(ctx.partition, {
|
|
@@ -473,7 +473,17 @@ async function* tableSegments(ctx, schema, limits, pair, pin, startRowCursor, st
|
|
|
473
473
|
yield { type: 'SEGMENT_INLINE', payload: bytes };
|
|
474
474
|
}
|
|
475
475
|
else {
|
|
476
|
-
|
|
476
|
+
// The page's put runs while the next page is scanned; the caller keeps
|
|
477
|
+
// six puts in flight and emits the frames in order.
|
|
478
|
+
trace?.segments.push({
|
|
479
|
+
mediaType: 'rows',
|
|
480
|
+
delivery: 'ref',
|
|
481
|
+
origin: 'built',
|
|
482
|
+
bytes: bytes.length,
|
|
483
|
+
rows: pageRows.length,
|
|
484
|
+
});
|
|
485
|
+
const ref = segments
|
|
486
|
+
.put({
|
|
477
487
|
partition,
|
|
478
488
|
logEpoch,
|
|
479
489
|
table: pair.table.name,
|
|
@@ -484,15 +494,9 @@ async function* tableSegments(ctx, schema, limits, pair, pin, startRowCursor, st
|
|
|
484
494
|
rowCount: pageRows.length,
|
|
485
495
|
rowCursor,
|
|
486
496
|
nextRowCursor,
|
|
487
|
-
}, bytes, now)
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
delivery: 'ref',
|
|
491
|
-
origin: 'built',
|
|
492
|
-
bytes: record.byteLength,
|
|
493
|
-
rows: record.rowCount,
|
|
494
|
-
});
|
|
495
|
-
yield segmentRefFrame(record, digest, await signedUrlFields(ctx, limits, record.segmentId, digest, now));
|
|
497
|
+
}, bytes, now)
|
|
498
|
+
.then(async (record) => segmentRefFrame(record, digest, await signedUrlFields(ctx, limits, record.segmentId, digest, now)));
|
|
499
|
+
yield { type: 'PENDING_REF', ref };
|
|
496
500
|
}
|
|
497
501
|
if (!hasMore)
|
|
498
502
|
return { pages: page + 1, rows, rowCursor: null, image: false };
|
|
@@ -507,13 +511,14 @@ async function* tableSegments(ctx, schema, limits, pair, pin, startRowCursor, st
|
|
|
507
511
|
* is awaited, so a storage that batches concurrent page reads (PostgreSQL,
|
|
508
512
|
* D1) answers them in one statement per table.
|
|
509
513
|
*/
|
|
510
|
-
export function planBootstrap(ctx, schema, limits, unit, bootstrapState, maxSeq,
|
|
514
|
+
export function planBootstrap(ctx, schema, limits, unit, bootstrapState, maxSeq, horizon, logEpoch, keptCursor = 0) {
|
|
511
515
|
// §4.7: resume at the token's pin unless the token is unusable, or the pin
|
|
512
|
-
// fell behind the horizon
|
|
513
|
-
// restart from a fresh pin.
|
|
516
|
+
// fell behind the horizon, the table horizon of a bootstrap table, or a
|
|
517
|
+
// restricted bootstrap's kept cursor; then restart from a fresh pin.
|
|
514
518
|
const token = parseBootstrapToken(bootstrapState, unit.tables, maxSeq);
|
|
515
519
|
const resume = token !== undefined &&
|
|
516
|
-
token.asOfCommitSeq >=
|
|
520
|
+
token.asOfCommitSeq >=
|
|
521
|
+
Math.max(horizon.seq, keptCursor, ...unit.tables.map((table) => horizon.tables.get(table.name) ?? -1))
|
|
517
522
|
? token
|
|
518
523
|
: undefined;
|
|
519
524
|
const tableIndex = resume?.tableIndex ?? 0;
|
|
@@ -525,7 +530,7 @@ export function planBootstrap(ctx, schema, limits, unit, bootstrapState, maxSeq,
|
|
|
525
530
|
const effective = unit.pairs.get(table.name);
|
|
526
531
|
if (effective === undefined)
|
|
527
532
|
continue;
|
|
528
|
-
const start = readTableStart(ctx, schema, limits, unit, { table, effective }, index === tableIndex ? rowCursor : null, index === tableIndex && image, maxSeq,
|
|
533
|
+
const start = readTableStart(ctx, schema, limits, unit, { table, effective }, index === tableIndex ? rowCursor : null, index === tableIndex && image, maxSeq, Math.max(horizon.seq, horizon.tables.get(table.name) ?? -1), logEpoch);
|
|
529
534
|
// A section that stops early never awaits the later starts.
|
|
530
535
|
start.catch(() => undefined);
|
|
531
536
|
starts.set(index, start);
|
|
@@ -645,12 +650,14 @@ export async function publishSqliteImage(input) {
|
|
|
645
650
|
accept: ACCEPT_SQLITE,
|
|
646
651
|
};
|
|
647
652
|
const maxSeq = await config.storage.getMaxCommitSeq(partition);
|
|
648
|
-
const
|
|
653
|
+
const horizons = await config.storage.getHorizons(partition);
|
|
649
654
|
const trace = { segments: [] };
|
|
650
|
-
const plan = planBootstrap(ctx, schema, limits, unit, undefined, maxSeq,
|
|
655
|
+
const plan = planBootstrap(ctx, schema, limits, unit, undefined, maxSeq, horizons, logEpoch);
|
|
651
656
|
const refs = [];
|
|
652
657
|
for await (const frame of bootstrapSegments(ctx, schema, limits, plan, trace, logEpoch)) {
|
|
653
|
-
if (frame.type === '
|
|
658
|
+
if (frame.type === 'PENDING_REF')
|
|
659
|
+
await frame.ref;
|
|
660
|
+
else if (frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite') {
|
|
654
661
|
refs.push(frame);
|
|
655
662
|
}
|
|
656
663
|
}
|
package/dist/push.js
CHANGED
|
@@ -92,20 +92,23 @@ async function resolveBlobPresence(blobCtx, blobIds) {
|
|
|
92
92
|
}));
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
|
-
* §6.7: run the table's
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* keeps
|
|
95
|
+
* §6.7: run the table's row hook, if configured, on the row that will persist
|
|
96
|
+
* (post scope-strip, post CRDT-merge). With `present` (an accepted insert or
|
|
97
|
+
* update of a pushed commit or a registered command) the hook's returned
|
|
98
|
+
* values for server-owned columns land in `values` and are marked present, so
|
|
99
|
+
* their column versions advance; elsewhere (a delete, a rescope, a §6.11
|
|
100
|
+
* cascade) the server ignores them. Returns a terminating outcome iff the
|
|
101
|
+
* hook fails: a `ValidationRejection` keeps its host code, and any other
|
|
102
|
+
* throw, a returned column that is not server-owned, a value the column's
|
|
103
|
+
* type refuses or a changed scope column on an update is
|
|
104
|
+
* `sync.constraint_violation`.
|
|
102
105
|
*/
|
|
103
|
-
async function runValidator(validators, table, op, rowId, values, storedValues, opIndex,
|
|
106
|
+
async function runValidator(validators, tx, schema, table, op, rowId, values, storedValues, opIndex, partition, actorId, context, present) {
|
|
104
107
|
const validator = validators?.[table.name];
|
|
105
108
|
if (validator === undefined)
|
|
106
109
|
return undefined;
|
|
107
110
|
try {
|
|
108
|
-
await validator({
|
|
111
|
+
const output = await validator({
|
|
109
112
|
op,
|
|
110
113
|
table: table.name,
|
|
111
114
|
rowId,
|
|
@@ -115,61 +118,26 @@ async function runValidator(validators, table, op, rowId, values, storedValues,
|
|
|
115
118
|
stored: storedValues !== undefined
|
|
116
119
|
? toValidateRow(table.columns, storedValues)
|
|
117
120
|
: undefined,
|
|
118
|
-
}, {
|
|
119
|
-
actorId,
|
|
120
|
-
partition,
|
|
121
|
-
...(context !== undefined ? { context } : {}),
|
|
122
|
-
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
if (error instanceof ValidationRejection) {
|
|
127
|
-
return errorRecord(opIndex, error.code, error.message, false, error.details);
|
|
128
|
-
}
|
|
129
|
-
// §6.7: a non-ValidationRejection throw is still a rejection, mapped to
|
|
130
|
-
// the generic server-side constraint code (§10.2) — the validator's
|
|
131
|
-
// failure never crashes the request or leaks its message as a code.
|
|
132
|
-
return errorRecord(opIndex, 'sync.constraint_violation', 'write validator failed');
|
|
133
|
-
}
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* §6.12: complete an accepted upsert with the table's server column hook.
|
|
138
|
-
* The returned values land in `values` and are marked present, so their
|
|
139
|
-
* column versions advance. A `ValidationRejection` keeps its host code; any
|
|
140
|
-
* other failure (an undeclared key, a value the column's type refuses, a
|
|
141
|
-
* changed scope column on an update) is `sync.constraint_violation`.
|
|
142
|
-
*/
|
|
143
|
-
async function runServerColumns(hooks, tx, schema, table, rowId, values, storedValues, present, opIndex, partition, actorId, context) {
|
|
144
|
-
const hook = hooks?.[table.name];
|
|
145
|
-
if (hook === undefined)
|
|
146
|
-
return undefined;
|
|
147
|
-
try {
|
|
148
|
-
const output = await hook({
|
|
149
|
-
table: table.name,
|
|
150
|
-
rowId,
|
|
151
|
-
row: toValidateRow(table.columns, values),
|
|
152
|
-
stored: storedValues !== undefined
|
|
153
|
-
? toValidateRow(table.columns, storedValues)
|
|
154
|
-
: undefined,
|
|
155
121
|
}, {
|
|
156
122
|
actorId,
|
|
157
123
|
partition,
|
|
158
124
|
...(context !== undefined ? { context } : {}),
|
|
159
125
|
read: commitValidationReader(tx, schema),
|
|
160
126
|
});
|
|
127
|
+
if (output === undefined || present === undefined || values === undefined)
|
|
128
|
+
return undefined;
|
|
161
129
|
for (const [name, value] of Object.entries(output)) {
|
|
162
130
|
const index = table.columnIndex.get(name);
|
|
163
131
|
if (index === undefined ||
|
|
164
132
|
!table.serverOwnedColumnIndices.includes(index)) {
|
|
165
|
-
throw new Error('
|
|
133
|
+
throw new Error('write validator returned a column that is not server-owned');
|
|
166
134
|
}
|
|
167
135
|
// The column's own codec refuses a value of the wrong type.
|
|
168
136
|
encodeRow([table.columns[index]], [value]);
|
|
169
137
|
if (storedValues !== undefined &&
|
|
170
138
|
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
171
139
|
value !== (storedValues[index] ?? null)) {
|
|
172
|
-
throw new Error('
|
|
140
|
+
throw new Error('write validator changed a scope column on update');
|
|
173
141
|
}
|
|
174
142
|
values[index] = value;
|
|
175
143
|
present[index] = true;
|
|
@@ -179,7 +147,10 @@ async function runServerColumns(hooks, tx, schema, table, rowId, values, storedV
|
|
|
179
147
|
if (error instanceof ValidationRejection) {
|
|
180
148
|
return errorRecord(opIndex, error.code, error.message, false, error.details);
|
|
181
149
|
}
|
|
182
|
-
|
|
150
|
+
// §6.7: any other failure is still a rejection, mapped to the generic
|
|
151
|
+
// server-side constraint code (§10.2); the hook's failure never crashes
|
|
152
|
+
// the request or leaks its message.
|
|
153
|
+
return errorRecord(opIndex, 'sync.constraint_violation', 'write validator failed');
|
|
183
154
|
}
|
|
184
155
|
return undefined;
|
|
185
156
|
}
|
|
@@ -225,7 +196,7 @@ async function mergeCrdtColumns(table, values, storedValues, present, opIndex, m
|
|
|
225
196
|
}
|
|
226
197
|
return { changed };
|
|
227
198
|
}
|
|
228
|
-
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators,
|
|
199
|
+
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId, context,
|
|
229
200
|
/** §2.2: the partition's delete floor, raised by this commit's deletes. */
|
|
230
201
|
deleteFloor) {
|
|
231
202
|
const table = schema.tables.get(op.table);
|
|
@@ -293,10 +264,10 @@ deleteFloor) {
|
|
|
293
264
|
if (!authorizeWrite(table, extracted.scopes, resolved, true)) {
|
|
294
265
|
return errorRecord(opIndex, 'sync.forbidden', 'rescope denied by scope authorization of the new scopes (§3.4)');
|
|
295
266
|
}
|
|
296
|
-
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex,
|
|
267
|
+
const deleteReject = await runValidator(validators, tx, schema, table, 'delete', op.rowId, undefined, storedValues, opIndex, partition, actorId, context);
|
|
297
268
|
if (deleteReject !== undefined)
|
|
298
269
|
return deleteReject;
|
|
299
|
-
const upsertReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex,
|
|
270
|
+
const upsertReject = await runValidator(validators, tx, schema, table, 'upsert', op.rowId, values, storedValues, opIndex, partition, actorId, context);
|
|
300
271
|
if (upsertReject !== undefined)
|
|
301
272
|
return upsertReject;
|
|
302
273
|
const blobCheck = await checkAndRecordBlobs(tx, table, op.rowId, values, opIndex, blobCtx);
|
|
@@ -413,7 +384,7 @@ deleteFloor) {
|
|
|
413
384
|
// stored = the row about to be removed). Only reached for an existing
|
|
414
385
|
// row — an absent-row delete is an idempotent no-op above.
|
|
415
386
|
const storedValues = decodeRow(table.columns, stored.payload);
|
|
416
|
-
const deleteReject = await runValidator(validators, table, 'delete', op.rowId, undefined, storedValues, opIndex,
|
|
387
|
+
const deleteReject = await runValidator(validators, tx, schema, table, 'delete', op.rowId, undefined, storedValues, opIndex, partition, actorId, context);
|
|
417
388
|
if (deleteReject !== undefined)
|
|
418
389
|
return deleteReject;
|
|
419
390
|
await tx.deleteRow(op.table, op.rowId);
|
|
@@ -460,7 +431,7 @@ deleteFloor) {
|
|
|
460
431
|
catch (error) {
|
|
461
432
|
return errorRecord(opIndex, 'sync.invalid_request', `row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`);
|
|
462
433
|
}
|
|
463
|
-
// §6.
|
|
434
|
+
// §6.7: the server discards a pushed value of a server-owned column, so an
|
|
464
435
|
// older client that still writes one stays valid.
|
|
465
436
|
for (const index of table.serverOwnedColumnIndices) {
|
|
466
437
|
sparse[index] = stored === undefined ? null : undefined;
|
|
@@ -538,9 +509,11 @@ deleteFloor) {
|
|
|
538
509
|
const mergeOutcome = await mergeCrdtColumns(table, values, storedValues, present, opIndex, crdtMergers);
|
|
539
510
|
if ('kind' in mergeOutcome)
|
|
540
511
|
return mergeOutcome;
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
512
|
+
// §6.7: validate the merged row that will persist (for a crdt column the
|
|
513
|
+
// MERGED value, §5.10.3) and take its server-owned values.
|
|
514
|
+
const updateReject = await runValidator(validators, tx, schema, table, 'upsert', op.rowId, values, storedValues, opIndex, partition, actorId, context, present);
|
|
515
|
+
if (updateReject !== undefined)
|
|
516
|
+
return updateReject;
|
|
544
517
|
const newVersion = stored.serverVersion + 1;
|
|
545
518
|
const nextVersions = new Map();
|
|
546
519
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -559,12 +532,6 @@ deleteFloor) {
|
|
|
559
532
|
const blobCheck = await checkAndRecordBlobs(tx, table, op.rowId, values, opIndex, blobCtx);
|
|
560
533
|
if (blobCheck !== undefined)
|
|
561
534
|
return blobCheck;
|
|
562
|
-
// §6.7: validate the merged row that will persist — for a crdt column
|
|
563
|
-
// the validator sees the MERGED value (§5.10.3), the state the store
|
|
564
|
-
// holds, not the raw pushed update.
|
|
565
|
-
const updateReject = await runValidator(validators, table, 'upsert', op.rowId, values, storedValues, opIndex, tx, partition, actorId, context);
|
|
566
|
-
if (updateReject !== undefined)
|
|
567
|
-
return updateReject;
|
|
568
535
|
const columnVersions = encodeColumnVersions(nextVersions);
|
|
569
536
|
const newRow = {
|
|
570
537
|
rowId: op.rowId,
|
|
@@ -656,14 +623,17 @@ deleteFloor) {
|
|
|
656
623
|
const insertMerge = await mergeCrdtColumns(table, values, undefined, present, opIndex, crdtMergers);
|
|
657
624
|
if ('kind' in insertMerge)
|
|
658
625
|
return insertMerge;
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
626
|
+
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
627
|
+
// distinguish create from update; crdt columns are already merged against
|
|
628
|
+
// the empty document) and take its server-owned values.
|
|
629
|
+
const insertReject = await runValidator(validators, tx, schema, table, 'upsert', op.rowId, values, undefined, opIndex, partition, actorId, context, present);
|
|
630
|
+
if (insertReject !== undefined)
|
|
631
|
+
return insertReject;
|
|
632
|
+
// §6.7: the stored scopes and the change-scope index take the validator's
|
|
663
633
|
// values, so the row reaches a server-set scope's units in this commit.
|
|
664
634
|
const scoped = storedScopesForRow(table, values);
|
|
665
635
|
if ('missing' in scoped) {
|
|
666
|
-
return errorRecord(opIndex, 'sync.constraint_violation', '
|
|
636
|
+
return errorRecord(opIndex, 'sync.constraint_violation', 'write validator failed');
|
|
667
637
|
}
|
|
668
638
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
669
639
|
// present on an insert.
|
|
@@ -672,12 +642,6 @@ deleteFloor) {
|
|
|
672
642
|
const blobCheck = await checkAndRecordBlobs(tx, table, op.rowId, values, opIndex, blobCtx);
|
|
673
643
|
if (blobCheck !== undefined)
|
|
674
644
|
return blobCheck;
|
|
675
|
-
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
676
|
-
// distinguish create from update); crdt columns are already merged
|
|
677
|
-
// against the empty document.
|
|
678
|
-
const insertReject = await runValidator(validators, table, 'upsert', op.rowId, values, undefined, opIndex, tx, partition, actorId, context);
|
|
679
|
-
if (insertReject !== undefined)
|
|
680
|
-
return insertReject;
|
|
681
645
|
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
682
646
|
// tombstone or not; without a column-versions blob every column sits at
|
|
683
647
|
// that version.
|
|
@@ -1085,7 +1049,7 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
1085
1049
|
const syntheticIndex = clientOpCount + originByIndex.length;
|
|
1086
1050
|
const storedValues = decodeRow(child.columns, childRow.payload);
|
|
1087
1051
|
if (reference.onDelete === 'CASCADE') {
|
|
1088
|
-
const reject = await runValidator(validators, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex,
|
|
1052
|
+
const reject = await runValidator(validators, tx, schema, child, 'delete', childRow.rowId, undefined, storedValues, item.originOpIndex, partition, actorId, context);
|
|
1089
1053
|
if (reject !== undefined) {
|
|
1090
1054
|
return { kind: 'terminate', record: reject.record };
|
|
1091
1055
|
}
|
|
@@ -1110,7 +1074,7 @@ async function enforceReferences(tx, schema, resolved, staged, clientOpCount, ca
|
|
|
1110
1074
|
else {
|
|
1111
1075
|
const values = [...storedValues];
|
|
1112
1076
|
values[reference.columnIndex] = null;
|
|
1113
|
-
const reject = await runValidator(validators, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex,
|
|
1077
|
+
const reject = await runValidator(validators, tx, schema, child, 'upsert', childRow.rowId, values, storedValues, item.originOpIndex, partition, actorId, context);
|
|
1114
1078
|
if (reject !== undefined) {
|
|
1115
1079
|
return { kind: 'terminate', record: reject.record };
|
|
1116
1080
|
}
|
|
@@ -1468,7 +1432,7 @@ async function applyPushCommits(ctx, schema, resolved, clientId, commits, wireSc
|
|
|
1468
1432
|
const op = operations[opIndex];
|
|
1469
1433
|
if (op === undefined)
|
|
1470
1434
|
continue;
|
|
1471
|
-
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators,
|
|
1435
|
+
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, ctx.context, deleteFloor);
|
|
1472
1436
|
if (outcome.kind === 'terminate') {
|
|
1473
1437
|
terminated = outcome.record;
|
|
1474
1438
|
break;
|
package/dist/realtime.d.ts
CHANGED
|
@@ -304,9 +304,11 @@ export declare class RealtimeHub {
|
|
|
304
304
|
notifySubscriptions(partition: string, clientId: string): Promise<void>;
|
|
305
305
|
/**
|
|
306
306
|
* Host-initiated, fail-closed registration refresh (§8.7). A host calls this
|
|
307
|
-
* after it changes an actor's
|
|
308
|
-
*
|
|
309
|
-
* without waiting for
|
|
307
|
+
* after it changes an actor's grants, whether they shrank or grew. Each
|
|
308
|
+
* matching session re-resolves its registrations, so it stops using grants
|
|
309
|
+
* the host already revoked without waiting for its client to run a round,
|
|
310
|
+
* and then receives one `scopes` event, so its client's app runs its
|
|
311
|
+
* discovery and registers units it did not know.
|
|
310
312
|
*
|
|
311
313
|
* This differs deliberately from the round-end refresh
|
|
312
314
|
* (`RealtimeSession.refreshRegistrations`), which keeps the previous
|
|
@@ -316,14 +318,6 @@ export declare class RealtimeHub {
|
|
|
316
318
|
* on until the next round. Presence is reconciled exactly as at round end.
|
|
317
319
|
*/
|
|
318
320
|
refreshScopes(partition: string, actorId?: string): Promise<void>;
|
|
319
|
-
/**
|
|
320
|
-
* Host announcement (§8.7): tell the connected sessions of `actorIds` on
|
|
321
|
-
* the partition that their grant widened, so each client's app runs its
|
|
322
|
-
* discovery and registers the units it did not know. Sends one `scopes`
|
|
323
|
-
* event per session and changes no registration; a narrowed grant needs
|
|
324
|
-
* `refreshScopes`.
|
|
325
|
-
*/
|
|
326
|
-
announceScopes(partition: string, actorIds: readonly string[]): void;
|
|
327
321
|
/** Broadcast a wake-up (host-initiated resync, schema rollover, §8.3). */
|
|
328
322
|
wake(partition: string, reason: WakeReason): void;
|
|
329
323
|
/** Wake every session on every partition (fanout listener gap, §8.3). */
|
package/dist/realtime.js
CHANGED
|
@@ -531,8 +531,8 @@ export class RealtimeSession {
|
|
|
531
531
|
step = await iterator.next();
|
|
532
532
|
}
|
|
533
533
|
catch (error) {
|
|
534
|
-
// The
|
|
535
|
-
// refusal)
|
|
534
|
+
// The first `next()` waits until the response is built (and the
|
|
535
|
+
// read-verify refusal ran) or its first KEEPALIVE is due (§1.6).
|
|
536
536
|
// Nothing has escaped, so a failure there is delivered as the same
|
|
537
537
|
// §8.4 ERROR frame HTTP returns rather than a silent close; a
|
|
538
538
|
// non-catalog exception is reported and answers
|
|
@@ -1203,9 +1203,11 @@ export class RealtimeHub {
|
|
|
1203
1203
|
}
|
|
1204
1204
|
/**
|
|
1205
1205
|
* Host-initiated, fail-closed registration refresh (§8.7). A host calls this
|
|
1206
|
-
* after it changes an actor's
|
|
1207
|
-
*
|
|
1208
|
-
* without waiting for
|
|
1206
|
+
* after it changes an actor's grants, whether they shrank or grew. Each
|
|
1207
|
+
* matching session re-resolves its registrations, so it stops using grants
|
|
1208
|
+
* the host already revoked without waiting for its client to run a round,
|
|
1209
|
+
* and then receives one `scopes` event, so its client's app runs its
|
|
1210
|
+
* discovery and registers units it did not know.
|
|
1209
1211
|
*
|
|
1210
1212
|
* This differs deliberately from the round-end refresh
|
|
1211
1213
|
* (`RealtimeSession.refreshRegistrations`), which keeps the previous
|
|
@@ -1220,25 +1222,10 @@ export class RealtimeHub {
|
|
|
1220
1222
|
continue;
|
|
1221
1223
|
if (actorId !== undefined && session.actorId !== actorId)
|
|
1222
1224
|
continue;
|
|
1223
|
-
//
|
|
1224
|
-
//
|
|
1225
|
-
// this revocation from being overwritten by a slower round-end reload.
|
|
1225
|
+
// The session's generation guard keeps this revocation from being
|
|
1226
|
+
// overwritten by a slower round-end reload.
|
|
1226
1227
|
await session.refreshRegistrations(true);
|
|
1227
|
-
|
|
1228
|
-
}
|
|
1229
|
-
/**
|
|
1230
|
-
* Host announcement (§8.7): tell the connected sessions of `actorIds` on
|
|
1231
|
-
* the partition that their grant widened, so each client's app runs its
|
|
1232
|
-
* discovery and registers the units it did not know. Sends one `scopes`
|
|
1233
|
-
* event per session and changes no registration; a narrowed grant needs
|
|
1234
|
-
* `refreshScopes`.
|
|
1235
|
-
*/
|
|
1236
|
-
announceScopes(partition, actorIds) {
|
|
1237
|
-
for (const session of this.#sessions) {
|
|
1238
|
-
if (session.partition !== partition)
|
|
1239
|
-
continue;
|
|
1240
|
-
if (actorIds.includes(session.actorId))
|
|
1241
|
-
session.sendScopes();
|
|
1228
|
+
session.sendScopes();
|
|
1242
1229
|
}
|
|
1243
1230
|
}
|
|
1244
1231
|
/** Broadcast a wake-up (host-initiated resync, schema rollover, §8.3). */
|
|
@@ -346,8 +346,10 @@ export declare function changedScopePatterns(schema: CompiledSchema, storedJson:
|
|
|
346
346
|
export declare function assertStoredScopePatterns(schema: CompiledSchema, storedJson: string | null | undefined): boolean;
|
|
347
347
|
/**
|
|
348
348
|
* §3.1 re-key, before the row walk: drop the re-keyed tables' row scope index
|
|
349
|
-
* entries and tombstones
|
|
350
|
-
*
|
|
349
|
+
* entries and tombstones. The re-key takes a commit sequence number of its
|
|
350
|
+
* own in every partition and records it as each re-keyed table's horizon
|
|
351
|
+
* (§4.6), past the changes logged under the previous patterns. Empty when
|
|
352
|
+
* nothing re-keys.
|
|
351
353
|
*/
|
|
352
354
|
export declare function rekeyCleanupSql(tables: readonly string[]): string[];
|
|
353
355
|
/**
|
package/dist/relational-rows.js
CHANGED
|
@@ -816,8 +816,10 @@ export function assertStoredScopePatterns(schema, storedJson) {
|
|
|
816
816
|
}
|
|
817
817
|
/**
|
|
818
818
|
* §3.1 re-key, before the row walk: drop the re-keyed tables' row scope index
|
|
819
|
-
* entries and tombstones
|
|
820
|
-
*
|
|
819
|
+
* entries and tombstones. The re-key takes a commit sequence number of its
|
|
820
|
+
* own in every partition and records it as each re-keyed table's horizon
|
|
821
|
+
* (§4.6), past the changes logged under the previous patterns. Empty when
|
|
822
|
+
* nothing re-keys.
|
|
821
823
|
*/
|
|
822
824
|
export function rekeyCleanupSql(tables) {
|
|
823
825
|
if (tables.length === 0)
|
|
@@ -826,7 +828,10 @@ export function rekeyCleanupSql(tables) {
|
|
|
826
828
|
return [
|
|
827
829
|
`DELETE FROM sync_row_scopes WHERE tbl IN (${names.join(',')})`,
|
|
828
830
|
`DELETE FROM sync_tombstones WHERE tbl IN (${names.join(',')})`,
|
|
829
|
-
'UPDATE sync_partitions SET
|
|
831
|
+
'UPDATE sync_partitions SET max_commit_seq=max_commit_seq+1',
|
|
832
|
+
...names.map((name) => `INSERT INTO sync_table_horizons(partition, tbl, commit_seq)
|
|
833
|
+
SELECT partition, ${name}, max_commit_seq FROM sync_partitions WHERE true
|
|
834
|
+
ON CONFLICT(partition, tbl) DO UPDATE SET commit_seq=excluded.commit_seq`),
|
|
830
835
|
];
|
|
831
836
|
}
|
|
832
837
|
/**
|
package/dist/schema-window.js
CHANGED
|
@@ -19,7 +19,7 @@ export function schemaWindowOf(current, configured) {
|
|
|
19
19
|
const next = head.tables.get(table.name);
|
|
20
20
|
if (next === undefined ||
|
|
21
21
|
table.columns.length > next.columns.length ||
|
|
22
|
-
// §6.
|
|
22
|
+
// §6.7: a version may mark an existing column server-owned; the flag
|
|
23
23
|
// never reaches the wire.
|
|
24
24
|
table.columns.some((column, i) => JSON.stringify({ ...column, serverOwned: undefined }) !==
|
|
25
25
|
JSON.stringify({ ...next.columns[i], serverOwned: undefined })) ||
|
|
@@ -27,7 +27,7 @@ export function schemaWindowOf(current, configured) {
|
|
|
27
27
|
.slice(table.columns.length)
|
|
28
28
|
.some((column) => !column.nullable) ||
|
|
29
29
|
table.primaryKeyIndex !== next.primaryKeyIndex ||
|
|
30
|
-
// §6.
|
|
30
|
+
// §6.7: a pattern over an appended server-owned column is new to the
|
|
31
31
|
// current version; an older client neither holds nor writes it.
|
|
32
32
|
JSON.stringify(table.scopePatterns) !==
|
|
33
33
|
JSON.stringify(next.scopePatterns.filter((pattern) => pattern.columnIndex < table.columns.length ||
|
package/dist/schema.d.ts
CHANGED
|
@@ -141,8 +141,8 @@ export interface CompiledTable {
|
|
|
141
141
|
* image copies ciphertext wholesale with no per-row decrypt pass, so an
|
|
142
142
|
* encrypted table MUST be served via the rows lane. Empty ⇒ no restriction. */
|
|
143
143
|
readonly encryptedColumnIndices: readonly number[];
|
|
144
|
-
/** §6.
|
|
145
|
-
*
|
|
144
|
+
/** §6.7: column indices marked `serverOwned`. Empty ⇒ no server column
|
|
145
|
+
* values are accepted from the table's validator. */
|
|
146
146
|
readonly serverOwnedColumnIndices: readonly number[];
|
|
147
147
|
}
|
|
148
148
|
export interface CompiledSchema {
|
package/dist/schema.js
CHANGED
|
@@ -174,7 +174,7 @@ export function compileSchema(schema) {
|
|
|
174
174
|
// §5.11: an encrypted column carries wire type `bytes` + `encrypted`.
|
|
175
175
|
if (column.encrypted === true)
|
|
176
176
|
encryptedColumnIndices.push(index);
|
|
177
|
-
// §6.
|
|
177
|
+
// §6.7: a validator never changes who may write the row (owner set), and
|
|
178
178
|
// a client's optimistic row holds NULL until the server row arrives.
|
|
179
179
|
if (column.serverOwned === true) {
|
|
180
180
|
if (index === primaryKeyIndex ||
|
|
@@ -183,7 +183,7 @@ export function compileSchema(schema) {
|
|
|
183
183
|
column.encrypted === true ||
|
|
184
184
|
scopePatterns.some((pattern) => pattern.columnIndex === index &&
|
|
185
185
|
writeScopeSets[0].includes(pattern.variable))) {
|
|
186
|
-
throw new Error(`table ${table.name}: server-owned column ${JSON.stringify(column.name)} must be nullable and outside the primary key, the owner write set, crdt and encryption (§6.
|
|
186
|
+
throw new Error(`table ${table.name}: server-owned column ${JSON.stringify(column.name)} must be nullable and outside the primary key, the owner write set, crdt and encryption (§6.7)`);
|
|
187
187
|
}
|
|
188
188
|
serverOwnedColumnIndices.push(index);
|
|
189
189
|
}
|
package/dist/sqlite-dialect.d.ts
CHANGED
|
@@ -18,6 +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 PartitionHorizons } from './storage.js';
|
|
21
22
|
import type { AuthoritativeQueryRequest, ClientCursorInfo, ClientPushResult, ClientRecord, ClientRecordWriteOptions, CommitDetail, CommitMetadata, CommitMetadataQuery, CommitPruneQuery, CommitPruneResult, IndexRowScanQuery, LiveWindow, LiveWindowQuery, NewCommit, OutboxSequence, OutboxSettlement, NewReaction, PartitionRegistryEntry, PartitionTouch, PrunedReactionCounts, PushWatermark, ReactionClaimQuery, ReactionFailureUpdate, ReactionListQuery, ReactionPruneQuery, RowChangeRecord, RowChangesQuery, RowListQuery, RowScanQuery, ScopeActivityQuery, ScopeCommitActivity, ScopeValueHistoryQuery, ScopeValueQuery, StoredPushResult, StoredReaction, StoredRow, Tombstone } from './storage.js';
|
|
22
23
|
/**
|
|
23
24
|
* Schema DDL, one statement per `;`-delimited chunk. Native SQLite applies
|
|
@@ -44,7 +45,7 @@ export declare const SQLITE_UPGRADE_PROBE: SqliteStatementSpec;
|
|
|
44
45
|
/**
|
|
45
46
|
* The statements that bring the core tables of a database from an earlier
|
|
46
47
|
* release to the current DDL, given the `SQLITE_UPGRADE_PROBE` rows: missing
|
|
47
|
-
* columns, dropped columns, dropped indexes, then missing indexes. Empty for
|
|
48
|
+
* tables and columns, dropped columns, dropped indexes, then missing indexes. Empty for
|
|
48
49
|
* a current database. Run after `SQLITE_DDL`: on every `SqliteServerStorage`
|
|
49
50
|
* construction, and on D1 in `migrate()`, the core phase of `migrateSchema`
|
|
50
51
|
* and the first open of a database at the current schema version.
|
|
@@ -125,6 +126,7 @@ export declare abstract class SqliteFamilyStorage {
|
|
|
125
126
|
getMaxCommitSeq(partition: string): Promise<number>;
|
|
126
127
|
getPartitionLogEpoch(partition: string): Promise<string | undefined>;
|
|
127
128
|
getHorizonSeq(partition: string): Promise<number>;
|
|
129
|
+
getHorizons(partition: string): Promise<PartitionHorizons>;
|
|
128
130
|
setHorizonSeq(partition: string, seq: number): Promise<void>;
|
|
129
131
|
pruneCommitsThrough(partition: string, query: CommitPruneQuery): Promise<CommitPruneResult>;
|
|
130
132
|
getCommitSeqBefore(partition: string, createdBeforeMs: number): Promise<number>;
|