@syncular/server 0.95.0 → 0.97.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 -16
- package/dist/context.js +0 -3
- package/dist/d1-storage.js +1 -1
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +36 -0
- package/dist/frame-bytes.js +1 -0
- package/dist/handler.d.ts +20 -2
- package/dist/handler.js +199 -59
- package/dist/postgres-storage.d.ts +3 -2
- package/dist/postgres-storage.js +18 -2
- package/dist/pull.d.ts +2 -2
- package/dist/pull.js +22 -16
- 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 +23 -13
- package/dist/relational-rows.js +49 -30
- package/dist/schema-window.js +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +2 -2
- package/dist/segment-download.js +11 -4
- package/dist/sqlite-dialect.d.ts +3 -1
- package/dist/sqlite-dialect.js +37 -6
- package/dist/storage-query.d.ts +5 -2
- package/dist/storage-query.js +7 -4
- 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 -23
- package/src/d1-storage.ts +0 -1
- package/src/errors.ts +46 -0
- package/src/frame-bytes.ts +1 -0
- package/src/handler.ts +257 -73
- package/src/postgres-storage.ts +32 -6
- package/src/pull.ts +50 -35
- package/src/push.ts +51 -129
- package/src/realtime.ts +10 -22
- package/src/relational-rows.ts +51 -30
- package/src/schema-window.ts +2 -2
- package/src/schema.ts +4 -4
- package/src/segment-download.ts +19 -12
- package/src/sqlite-dialect.ts +47 -7
- package/src/storage-query.ts +7 -4
- package/src/storage.ts +13 -0
- package/src/validate.ts +21 -48
package/src/pull.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
} from '@syncular/core';
|
|
17
17
|
import type { SyncRequestContext, SyncServerConfig } from './context';
|
|
18
18
|
import { clockOf, limitsOf } from './context';
|
|
19
|
-
import { syncError } from './errors';
|
|
19
|
+
import { segmentStoreFailure, syncError } from './errors';
|
|
20
20
|
import type { PullSegmentSummary } from './events';
|
|
21
21
|
import {
|
|
22
22
|
type CompiledSchema,
|
|
@@ -26,7 +26,12 @@ import {
|
|
|
26
26
|
import { computeEffective, type ResolvedScopes, scopeDigest } from './scopes';
|
|
27
27
|
import type { SegmentRecord, SegmentStore } from './segment-store';
|
|
28
28
|
import { issueSegmentUrl } from './signed-url';
|
|
29
|
-
import type {
|
|
29
|
+
import type {
|
|
30
|
+
PartitionHorizons,
|
|
31
|
+
ServerStorage,
|
|
32
|
+
StoredCommit,
|
|
33
|
+
StoredRow,
|
|
34
|
+
} from './storage';
|
|
30
35
|
import { projectRowPayload, projectRowValues } from './schema-window';
|
|
31
36
|
|
|
32
37
|
// One artifact build per owning storage pair and complete immutable identity.
|
|
@@ -377,19 +382,21 @@ async function findImage(
|
|
|
377
382
|
logEpoch: string,
|
|
378
383
|
): Promise<SegmentRecord | undefined> {
|
|
379
384
|
if (probe.length <= limits.limitSnapshotRows) return undefined;
|
|
380
|
-
const stored = await ctx.segments
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
385
|
+
const stored = await ctx.segments
|
|
386
|
+
.find(
|
|
387
|
+
{
|
|
388
|
+
partition: ctx.partition,
|
|
389
|
+
logEpoch,
|
|
390
|
+
table: pair.table.name,
|
|
391
|
+
schemaVersion: schema.version,
|
|
392
|
+
mediaType: 'sqlite',
|
|
393
|
+
scopeDigest: await scopeDigest(pair.effective),
|
|
394
|
+
asOfCommitSeq: scopePin,
|
|
395
|
+
rowCursor,
|
|
396
|
+
},
|
|
397
|
+
clockOf(ctx)(),
|
|
398
|
+
)
|
|
399
|
+
.catch(segmentStoreFailure(ctx.onError, 'sync', 'find'));
|
|
393
400
|
return stored !== undefined &&
|
|
394
401
|
(stored.byteLength <= limitsOf(ctx).maxImageSegmentBytes ||
|
|
395
402
|
stored.rowCount === 1)
|
|
@@ -433,9 +440,9 @@ async function readTableStart(
|
|
|
433
440
|
return { firstPage: await scan };
|
|
434
441
|
}
|
|
435
442
|
// §4.7: a sqlite-lane table pins at the newest commit carrying all of the
|
|
436
|
-
// unit's values (never below the horizon, never above
|
|
437
|
-
// stored image stays the reuse key's match until this
|
|
438
|
-
// (§5.3). The server computes the pin; a client token never supplies it.
|
|
443
|
+
// unit's values (never below the table's horizon, never above
|
|
444
|
+
// maxCommitSeq), so a stored image stays the reuse key's match until this
|
|
445
|
+
// scope changes (§5.3). The server computes the pin; a client token never supplies it.
|
|
439
446
|
const [firstPage, latest] = await Promise.all([
|
|
440
447
|
scan,
|
|
441
448
|
ctx.storage.latestScopedChangeSeq(ctx.partition, {
|
|
@@ -578,15 +585,17 @@ async function* sqliteImageSegment(
|
|
|
578
585
|
throw new Error('sqliteImageBuilder consumed rows out of order');
|
|
579
586
|
}
|
|
580
587
|
const last = index < 0 ? beforeBatch : batch[index]!.rowId;
|
|
581
|
-
return segments
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
588
|
+
return segments
|
|
589
|
+
.put(
|
|
590
|
+
{
|
|
591
|
+
...identity,
|
|
592
|
+
rowCount: image.rowCount,
|
|
593
|
+
nextRowCursor: image.rowCount < yielded || !exhausted ? last : null,
|
|
594
|
+
},
|
|
595
|
+
image.bytes,
|
|
596
|
+
clockOf(ctx)(),
|
|
597
|
+
)
|
|
598
|
+
.catch(segmentStoreFailure(ctx.onError, 'sync', 'put'));
|
|
590
599
|
})();
|
|
591
600
|
builds.set(key, building);
|
|
592
601
|
}
|
|
@@ -734,7 +743,7 @@ async function* tableSegments(
|
|
|
734
743
|
yield { type: 'SEGMENT_INLINE', payload: bytes };
|
|
735
744
|
} else {
|
|
736
745
|
// The page's put runs while the next page is scanned; the caller keeps
|
|
737
|
-
//
|
|
746
|
+
// six puts in flight and emits the frames in order.
|
|
738
747
|
trace?.segments.push({
|
|
739
748
|
mediaType: 'rows',
|
|
740
749
|
delivery: 'ref',
|
|
@@ -759,6 +768,7 @@ async function* tableSegments(
|
|
|
759
768
|
bytes,
|
|
760
769
|
now,
|
|
761
770
|
)
|
|
771
|
+
.catch(segmentStoreFailure(ctx.onError, 'sync', 'put'))
|
|
762
772
|
.then(async (record) =>
|
|
763
773
|
segmentRefFrame(
|
|
764
774
|
record,
|
|
@@ -806,17 +816,22 @@ export function planBootstrap(
|
|
|
806
816
|
unit: UnitPlan,
|
|
807
817
|
bootstrapState: string | undefined,
|
|
808
818
|
maxSeq: number,
|
|
809
|
-
|
|
819
|
+
horizon: PartitionHorizons,
|
|
810
820
|
logEpoch: string,
|
|
811
821
|
keptCursor = 0,
|
|
812
822
|
): BootstrapPlan {
|
|
813
823
|
// §4.7: resume at the token's pin unless the token is unusable, or the pin
|
|
814
|
-
// fell behind the horizon
|
|
815
|
-
// restart from a fresh pin.
|
|
824
|
+
// fell behind the horizon, the table horizon of a bootstrap table, or a
|
|
825
|
+
// restricted bootstrap's kept cursor; then restart from a fresh pin.
|
|
816
826
|
const token = parseBootstrapToken(bootstrapState, unit.tables, maxSeq);
|
|
817
827
|
const resume =
|
|
818
828
|
token !== undefined &&
|
|
819
|
-
token.asOfCommitSeq >=
|
|
829
|
+
token.asOfCommitSeq >=
|
|
830
|
+
Math.max(
|
|
831
|
+
horizon.seq,
|
|
832
|
+
keptCursor,
|
|
833
|
+
...unit.tables.map((table) => horizon.tables.get(table.name) ?? -1),
|
|
834
|
+
)
|
|
820
835
|
? token
|
|
821
836
|
: undefined;
|
|
822
837
|
const tableIndex = resume?.tableIndex ?? 0;
|
|
@@ -840,7 +855,7 @@ export function planBootstrap(
|
|
|
840
855
|
index === tableIndex ? rowCursor : null,
|
|
841
856
|
index === tableIndex && image,
|
|
842
857
|
maxSeq,
|
|
843
|
-
|
|
858
|
+
Math.max(horizon.seq, horizon.tables.get(table.name) ?? -1),
|
|
844
859
|
logEpoch,
|
|
845
860
|
);
|
|
846
861
|
// A section that stops early never awaits the later starts.
|
|
@@ -1021,7 +1036,7 @@ export async function publishSqliteImage(
|
|
|
1021
1036
|
accept: ACCEPT_SQLITE,
|
|
1022
1037
|
};
|
|
1023
1038
|
const maxSeq = await config.storage.getMaxCommitSeq(partition);
|
|
1024
|
-
const
|
|
1039
|
+
const horizons = await config.storage.getHorizons(partition);
|
|
1025
1040
|
const trace: PullSectionTrace = { segments: [] };
|
|
1026
1041
|
const plan = planBootstrap(
|
|
1027
1042
|
ctx,
|
|
@@ -1030,7 +1045,7 @@ export async function publishSqliteImage(
|
|
|
1030
1045
|
unit,
|
|
1031
1046
|
undefined,
|
|
1032
1047
|
maxSeq,
|
|
1033
|
-
|
|
1048
|
+
horizons,
|
|
1034
1049
|
logEpoch,
|
|
1035
1050
|
);
|
|
1036
1051
|
const refs: SegmentRefFrame[] = [];
|
package/src/push.ts
CHANGED
|
@@ -71,7 +71,6 @@ import type {
|
|
|
71
71
|
CommitValidationRow,
|
|
72
72
|
CommitValidator,
|
|
73
73
|
ValidateCommitOperation,
|
|
74
|
-
ServerColumnsRegistry,
|
|
75
74
|
ValidateOpKind,
|
|
76
75
|
ValidatorRegistry,
|
|
77
76
|
} from './validate';
|
|
@@ -222,31 +221,36 @@ async function resolveBlobPresence(
|
|
|
222
221
|
}
|
|
223
222
|
|
|
224
223
|
/**
|
|
225
|
-
* §6.7: run the table's
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* keeps
|
|
224
|
+
* §6.7: run the table's row hook, if configured, on the row that will persist
|
|
225
|
+
* (post scope-strip, post CRDT-merge). With `present` (an accepted insert or
|
|
226
|
+
* update of a pushed commit or a registered command) the hook's returned
|
|
227
|
+
* values for server-owned columns land in `values` and are marked present, so
|
|
228
|
+
* their column versions advance; elsewhere (a delete, a rescope, a §6.11
|
|
229
|
+
* cascade) the server ignores them. Returns a terminating outcome iff the
|
|
230
|
+
* hook fails: a `ValidationRejection` keeps its host code, and any other
|
|
231
|
+
* throw, a returned column that is not server-owned, a value the column's
|
|
232
|
+
* type refuses or a changed scope column on an update is
|
|
233
|
+
* `sync.constraint_violation`.
|
|
232
234
|
*/
|
|
233
235
|
async function runValidator(
|
|
234
236
|
validators: ValidatorRegistry | undefined,
|
|
237
|
+
tx: StorageTransaction,
|
|
238
|
+
schema: CompiledSchema,
|
|
235
239
|
table: CompiledTable,
|
|
236
240
|
op: ValidateOpKind,
|
|
237
241
|
rowId: string,
|
|
238
|
-
values:
|
|
242
|
+
values: RowValue[] | undefined,
|
|
239
243
|
storedValues: readonly RowValue[] | undefined,
|
|
240
244
|
opIndex: number,
|
|
241
|
-
tx: StorageTransaction,
|
|
242
245
|
partition: string,
|
|
243
246
|
actorId: string,
|
|
244
247
|
context: string | undefined,
|
|
248
|
+
present?: boolean[],
|
|
245
249
|
): Promise<TerminatingOutcome | undefined> {
|
|
246
250
|
const validator = validators?.[table.name];
|
|
247
251
|
if (validator === undefined) return undefined;
|
|
248
252
|
try {
|
|
249
|
-
await validator(
|
|
253
|
+
const output = await validator(
|
|
250
254
|
{
|
|
251
255
|
op,
|
|
252
256
|
table: table.name,
|
|
@@ -260,69 +264,6 @@ async function runValidator(
|
|
|
260
264
|
? toValidateRow(table.columns, storedValues)
|
|
261
265
|
: undefined,
|
|
262
266
|
},
|
|
263
|
-
{
|
|
264
|
-
actorId,
|
|
265
|
-
partition,
|
|
266
|
-
...(context !== undefined ? { context } : {}),
|
|
267
|
-
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
268
|
-
},
|
|
269
|
-
);
|
|
270
|
-
} catch (error) {
|
|
271
|
-
if (error instanceof ValidationRejection) {
|
|
272
|
-
return errorRecord(
|
|
273
|
-
opIndex,
|
|
274
|
-
error.code,
|
|
275
|
-
error.message,
|
|
276
|
-
false,
|
|
277
|
-
error.details,
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
// §6.7: a non-ValidationRejection throw is still a rejection, mapped to
|
|
281
|
-
// the generic server-side constraint code (§10.2) — the validator's
|
|
282
|
-
// failure never crashes the request or leaks its message as a code.
|
|
283
|
-
return errorRecord(
|
|
284
|
-
opIndex,
|
|
285
|
-
'sync.constraint_violation',
|
|
286
|
-
'write validator failed',
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
return undefined;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* §6.12: complete an accepted upsert with the table's server column hook.
|
|
294
|
-
* The returned values land in `values` and are marked present, so their
|
|
295
|
-
* column versions advance. A `ValidationRejection` keeps its host code; any
|
|
296
|
-
* other failure (an undeclared key, a value the column's type refuses, a
|
|
297
|
-
* changed scope column on an update) is `sync.constraint_violation`.
|
|
298
|
-
*/
|
|
299
|
-
async function runServerColumns(
|
|
300
|
-
hooks: ServerColumnsRegistry | undefined,
|
|
301
|
-
tx: StorageTransaction,
|
|
302
|
-
schema: CompiledSchema,
|
|
303
|
-
table: CompiledTable,
|
|
304
|
-
rowId: string,
|
|
305
|
-
values: RowValue[],
|
|
306
|
-
storedValues: readonly RowValue[] | undefined,
|
|
307
|
-
present: boolean[],
|
|
308
|
-
opIndex: number,
|
|
309
|
-
partition: string,
|
|
310
|
-
actorId: string,
|
|
311
|
-
context: string | undefined,
|
|
312
|
-
): Promise<TerminatingOutcome | undefined> {
|
|
313
|
-
const hook = hooks?.[table.name];
|
|
314
|
-
if (hook === undefined) return undefined;
|
|
315
|
-
try {
|
|
316
|
-
const output = await hook(
|
|
317
|
-
{
|
|
318
|
-
table: table.name,
|
|
319
|
-
rowId,
|
|
320
|
-
row: toValidateRow(table.columns, values),
|
|
321
|
-
stored:
|
|
322
|
-
storedValues !== undefined
|
|
323
|
-
? toValidateRow(table.columns, storedValues)
|
|
324
|
-
: undefined,
|
|
325
|
-
},
|
|
326
267
|
{
|
|
327
268
|
actorId,
|
|
328
269
|
partition,
|
|
@@ -330,6 +271,8 @@ async function runServerColumns(
|
|
|
330
271
|
read: commitValidationReader(tx, schema),
|
|
331
272
|
},
|
|
332
273
|
);
|
|
274
|
+
if (output === undefined || present === undefined || values === undefined)
|
|
275
|
+
return undefined;
|
|
333
276
|
for (const [name, value] of Object.entries(output)) {
|
|
334
277
|
const index = table.columnIndex.get(name);
|
|
335
278
|
if (
|
|
@@ -337,7 +280,7 @@ async function runServerColumns(
|
|
|
337
280
|
!table.serverOwnedColumnIndices.includes(index)
|
|
338
281
|
) {
|
|
339
282
|
throw new Error(
|
|
340
|
-
'
|
|
283
|
+
'write validator returned a column that is not server-owned',
|
|
341
284
|
);
|
|
342
285
|
}
|
|
343
286
|
// The column's own codec refuses a value of the wrong type.
|
|
@@ -347,7 +290,7 @@ async function runServerColumns(
|
|
|
347
290
|
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
348
291
|
value !== (storedValues[index] ?? null)
|
|
349
292
|
) {
|
|
350
|
-
throw new Error('
|
|
293
|
+
throw new Error('write validator changed a scope column on update');
|
|
351
294
|
}
|
|
352
295
|
values[index] = value;
|
|
353
296
|
present[index] = true;
|
|
@@ -362,10 +305,13 @@ async function runServerColumns(
|
|
|
362
305
|
error.details,
|
|
363
306
|
);
|
|
364
307
|
}
|
|
308
|
+
// §6.7: any other failure is still a rejection, mapped to the generic
|
|
309
|
+
// server-side constraint code (§10.2); the hook's failure never crashes
|
|
310
|
+
// the request or leaks its message.
|
|
365
311
|
return errorRecord(
|
|
366
312
|
opIndex,
|
|
367
313
|
'sync.constraint_violation',
|
|
368
|
-
'
|
|
314
|
+
'write validator failed',
|
|
369
315
|
);
|
|
370
316
|
}
|
|
371
317
|
return undefined;
|
|
@@ -435,7 +381,6 @@ async function applyOperation(
|
|
|
435
381
|
blobCtx: BlobApplyContext,
|
|
436
382
|
crdtMergers: CrdtMergerRegistry | undefined,
|
|
437
383
|
validators: ValidatorRegistry | undefined,
|
|
438
|
-
serverColumns: ServerColumnsRegistry | undefined,
|
|
439
384
|
partition: string,
|
|
440
385
|
actorId: string,
|
|
441
386
|
context: string | undefined,
|
|
@@ -558,13 +503,14 @@ async function applyOperation(
|
|
|
558
503
|
}
|
|
559
504
|
const deleteReject = await runValidator(
|
|
560
505
|
validators,
|
|
506
|
+
tx,
|
|
507
|
+
schema,
|
|
561
508
|
table,
|
|
562
509
|
'delete',
|
|
563
510
|
op.rowId,
|
|
564
511
|
undefined,
|
|
565
512
|
storedValues,
|
|
566
513
|
opIndex,
|
|
567
|
-
tx,
|
|
568
514
|
partition,
|
|
569
515
|
actorId,
|
|
570
516
|
context,
|
|
@@ -572,13 +518,14 @@ async function applyOperation(
|
|
|
572
518
|
if (deleteReject !== undefined) return deleteReject;
|
|
573
519
|
const upsertReject = await runValidator(
|
|
574
520
|
validators,
|
|
521
|
+
tx,
|
|
522
|
+
schema,
|
|
575
523
|
table,
|
|
576
524
|
'upsert',
|
|
577
525
|
op.rowId,
|
|
578
526
|
values,
|
|
579
527
|
storedValues,
|
|
580
528
|
opIndex,
|
|
581
|
-
tx,
|
|
582
529
|
partition,
|
|
583
530
|
actorId,
|
|
584
531
|
context,
|
|
@@ -715,13 +662,14 @@ async function applyOperation(
|
|
|
715
662
|
const storedValues = decodeRow(table.columns, stored.payload);
|
|
716
663
|
const deleteReject = await runValidator(
|
|
717
664
|
validators,
|
|
665
|
+
tx,
|
|
666
|
+
schema,
|
|
718
667
|
table,
|
|
719
668
|
'delete',
|
|
720
669
|
op.rowId,
|
|
721
670
|
undefined,
|
|
722
671
|
storedValues,
|
|
723
672
|
opIndex,
|
|
724
|
-
tx,
|
|
725
673
|
partition,
|
|
726
674
|
actorId,
|
|
727
675
|
context,
|
|
@@ -783,7 +731,7 @@ async function applyOperation(
|
|
|
783
731
|
`row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`,
|
|
784
732
|
);
|
|
785
733
|
}
|
|
786
|
-
// §6.
|
|
734
|
+
// §6.7: the server discards a pushed value of a server-owned column, so an
|
|
787
735
|
// older client that still writes one stays valid.
|
|
788
736
|
for (const index of table.serverOwnedColumnIndices) {
|
|
789
737
|
sparse[index] = stored === undefined ? null : undefined;
|
|
@@ -897,21 +845,24 @@ async function applyOperation(
|
|
|
897
845
|
crdtMergers,
|
|
898
846
|
);
|
|
899
847
|
if ('kind' in mergeOutcome) return mergeOutcome;
|
|
900
|
-
|
|
901
|
-
|
|
848
|
+
// §6.7: validate the merged row that will persist (for a crdt column the
|
|
849
|
+
// MERGED value, §5.10.3) and take its server-owned values.
|
|
850
|
+
const updateReject = await runValidator(
|
|
851
|
+
validators,
|
|
902
852
|
tx,
|
|
903
853
|
schema,
|
|
904
854
|
table,
|
|
855
|
+
'upsert',
|
|
905
856
|
op.rowId,
|
|
906
857
|
values,
|
|
907
858
|
storedValues,
|
|
908
|
-
present,
|
|
909
859
|
opIndex,
|
|
910
860
|
partition,
|
|
911
861
|
actorId,
|
|
912
862
|
context,
|
|
863
|
+
present,
|
|
913
864
|
);
|
|
914
|
-
if (
|
|
865
|
+
if (updateReject !== undefined) return updateReject;
|
|
915
866
|
const newVersion = stored.serverVersion + 1;
|
|
916
867
|
const nextVersions = new Map<number, number>();
|
|
917
868
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -936,23 +887,6 @@ async function applyOperation(
|
|
|
936
887
|
blobCtx,
|
|
937
888
|
);
|
|
938
889
|
if (blobCheck !== undefined) return blobCheck;
|
|
939
|
-
// §6.7: validate the merged row that will persist — for a crdt column
|
|
940
|
-
// the validator sees the MERGED value (§5.10.3), the state the store
|
|
941
|
-
// holds, not the raw pushed update.
|
|
942
|
-
const updateReject = await runValidator(
|
|
943
|
-
validators,
|
|
944
|
-
table,
|
|
945
|
-
'upsert',
|
|
946
|
-
op.rowId,
|
|
947
|
-
values,
|
|
948
|
-
storedValues,
|
|
949
|
-
opIndex,
|
|
950
|
-
tx,
|
|
951
|
-
partition,
|
|
952
|
-
actorId,
|
|
953
|
-
context,
|
|
954
|
-
);
|
|
955
|
-
if (updateReject !== undefined) return updateReject;
|
|
956
890
|
const columnVersions = encodeColumnVersions(nextVersions);
|
|
957
891
|
const newRow = {
|
|
958
892
|
rowId: op.rowId,
|
|
@@ -1078,29 +1012,33 @@ async function applyOperation(
|
|
|
1078
1012
|
crdtMergers,
|
|
1079
1013
|
);
|
|
1080
1014
|
if ('kind' in insertMerge) return insertMerge;
|
|
1081
|
-
|
|
1082
|
-
|
|
1015
|
+
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
1016
|
+
// distinguish create from update; crdt columns are already merged against
|
|
1017
|
+
// the empty document) and take its server-owned values.
|
|
1018
|
+
const insertReject = await runValidator(
|
|
1019
|
+
validators,
|
|
1083
1020
|
tx,
|
|
1084
1021
|
schema,
|
|
1085
1022
|
table,
|
|
1023
|
+
'upsert',
|
|
1086
1024
|
op.rowId,
|
|
1087
1025
|
values,
|
|
1088
1026
|
undefined,
|
|
1089
|
-
present,
|
|
1090
1027
|
opIndex,
|
|
1091
1028
|
partition,
|
|
1092
1029
|
actorId,
|
|
1093
1030
|
context,
|
|
1031
|
+
present,
|
|
1094
1032
|
);
|
|
1095
|
-
if (
|
|
1096
|
-
// §6.
|
|
1033
|
+
if (insertReject !== undefined) return insertReject;
|
|
1034
|
+
// §6.7: the stored scopes and the change-scope index take the validator's
|
|
1097
1035
|
// values, so the row reaches a server-set scope's units in this commit.
|
|
1098
1036
|
const scoped = storedScopesForRow(table, values);
|
|
1099
1037
|
if ('missing' in scoped) {
|
|
1100
1038
|
return errorRecord(
|
|
1101
1039
|
opIndex,
|
|
1102
1040
|
'sync.constraint_violation',
|
|
1103
|
-
'
|
|
1041
|
+
'write validator failed',
|
|
1104
1042
|
);
|
|
1105
1043
|
}
|
|
1106
1044
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
@@ -1116,23 +1054,6 @@ async function applyOperation(
|
|
|
1116
1054
|
blobCtx,
|
|
1117
1055
|
);
|
|
1118
1056
|
if (blobCheck !== undefined) return blobCheck;
|
|
1119
|
-
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
1120
|
-
// distinguish create from update); crdt columns are already merged
|
|
1121
|
-
// against the empty document.
|
|
1122
|
-
const insertReject = await runValidator(
|
|
1123
|
-
validators,
|
|
1124
|
-
table,
|
|
1125
|
-
'upsert',
|
|
1126
|
-
op.rowId,
|
|
1127
|
-
values,
|
|
1128
|
-
undefined,
|
|
1129
|
-
opIndex,
|
|
1130
|
-
tx,
|
|
1131
|
-
partition,
|
|
1132
|
-
actorId,
|
|
1133
|
-
context,
|
|
1134
|
-
);
|
|
1135
|
-
if (insertReject !== undefined) return insertReject;
|
|
1136
1057
|
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
1137
1058
|
// tombstone or not; without a column-versions blob every column sits at
|
|
1138
1059
|
// that version.
|
|
@@ -1701,13 +1622,14 @@ async function enforceReferences(
|
|
|
1701
1622
|
if (reference.onDelete === 'CASCADE') {
|
|
1702
1623
|
const reject = await runValidator(
|
|
1703
1624
|
validators,
|
|
1625
|
+
tx,
|
|
1626
|
+
schema,
|
|
1704
1627
|
child,
|
|
1705
1628
|
'delete',
|
|
1706
1629
|
childRow.rowId,
|
|
1707
1630
|
undefined,
|
|
1708
1631
|
storedValues,
|
|
1709
1632
|
item.originOpIndex,
|
|
1710
|
-
tx,
|
|
1711
1633
|
partition,
|
|
1712
1634
|
actorId,
|
|
1713
1635
|
context,
|
|
@@ -1737,13 +1659,14 @@ async function enforceReferences(
|
|
|
1737
1659
|
values[reference.columnIndex] = null;
|
|
1738
1660
|
const reject = await runValidator(
|
|
1739
1661
|
validators,
|
|
1662
|
+
tx,
|
|
1663
|
+
schema,
|
|
1740
1664
|
child,
|
|
1741
1665
|
'upsert',
|
|
1742
1666
|
childRow.rowId,
|
|
1743
1667
|
values,
|
|
1744
1668
|
storedValues,
|
|
1745
1669
|
item.originOpIndex,
|
|
1746
|
-
tx,
|
|
1747
1670
|
partition,
|
|
1748
1671
|
actorId,
|
|
1749
1672
|
context,
|
|
@@ -2243,7 +2166,6 @@ async function applyPushCommits(
|
|
|
2243
2166
|
blobCtx,
|
|
2244
2167
|
crdtMergers,
|
|
2245
2168
|
validators,
|
|
2246
|
-
ctx.serverColumns,
|
|
2247
2169
|
partition,
|
|
2248
2170
|
ctx.actorId,
|
|
2249
2171
|
ctx.context,
|
package/src/realtime.ts
CHANGED
|
@@ -734,8 +734,8 @@ export class RealtimeSession {
|
|
|
734
734
|
try {
|
|
735
735
|
step = await iterator.next();
|
|
736
736
|
} catch (error) {
|
|
737
|
-
// The
|
|
738
|
-
// refusal)
|
|
737
|
+
// The first `next()` waits until the response is built (and the
|
|
738
|
+
// read-verify refusal ran) or its first KEEPALIVE is due (§1.6).
|
|
739
739
|
// Nothing has escaped, so a failure there is delivered as the same
|
|
740
740
|
// §8.4 ERROR frame HTTP returns rather than a silent close; a
|
|
741
741
|
// non-catalog exception is reported and answers
|
|
@@ -1563,9 +1563,11 @@ export class RealtimeHub {
|
|
|
1563
1563
|
|
|
1564
1564
|
/**
|
|
1565
1565
|
* Host-initiated, fail-closed registration refresh (§8.7). A host calls this
|
|
1566
|
-
* after it changes an actor's
|
|
1567
|
-
*
|
|
1568
|
-
* without waiting for
|
|
1566
|
+
* after it changes an actor's grants, whether they shrank or grew. Each
|
|
1567
|
+
* matching session re-resolves its registrations, so it stops using grants
|
|
1568
|
+
* the host already revoked without waiting for its client to run a round,
|
|
1569
|
+
* and then receives one `scopes` event, so its client's app runs its
|
|
1570
|
+
* discovery and registers units it did not know.
|
|
1569
1571
|
*
|
|
1570
1572
|
* This differs deliberately from the round-end refresh
|
|
1571
1573
|
* (`RealtimeSession.refreshRegistrations`), which keeps the previous
|
|
@@ -1578,24 +1580,10 @@ export class RealtimeHub {
|
|
|
1578
1580
|
for (const session of [...this.#sessions]) {
|
|
1579
1581
|
if (session.partition !== partition) continue;
|
|
1580
1582
|
if (actorId !== undefined && session.actorId !== actorId) continue;
|
|
1581
|
-
//
|
|
1582
|
-
//
|
|
1583
|
-
// this revocation from being overwritten by a slower round-end reload.
|
|
1583
|
+
// The session's generation guard keeps this revocation from being
|
|
1584
|
+
// overwritten by a slower round-end reload.
|
|
1584
1585
|
await session.refreshRegistrations(true);
|
|
1585
|
-
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
/**
|
|
1589
|
-
* Host announcement (§8.7): tell the connected sessions of `actorIds` on
|
|
1590
|
-
* the partition that their grant widened, so each client's app runs its
|
|
1591
|
-
* discovery and registers the units it did not know. Sends one `scopes`
|
|
1592
|
-
* event per session and changes no registration; a narrowed grant needs
|
|
1593
|
-
* `refreshScopes`.
|
|
1594
|
-
*/
|
|
1595
|
-
announceScopes(partition: string, actorIds: readonly string[]): void {
|
|
1596
|
-
for (const session of this.#sessions) {
|
|
1597
|
-
if (session.partition !== partition) continue;
|
|
1598
|
-
if (actorIds.includes(session.actorId)) session.sendScopes();
|
|
1586
|
+
session.sendScopes();
|
|
1599
1587
|
}
|
|
1600
1588
|
}
|
|
1601
1589
|
|