@syncular/server 0.93.0 → 0.95.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 +7 -2
- package/dist/context.d.ts +15 -1
- package/dist/context.js +3 -0
- package/dist/d1-storage.js +10 -3
- package/dist/handler.js +38 -2
- package/dist/postgres-storage.js +4 -0
- package/dist/pull.d.ts +9 -1
- package/dist/pull.js +17 -11
- package/dist/push.js +71 -4
- package/dist/relational-rows.d.ts +31 -2
- package/dist/relational-rows.js +57 -6
- package/dist/schema-window.js +8 -2
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +15 -0
- package/dist/sqlite-storage.js +14 -1
- package/dist/validate.d.ts +25 -0
- package/package.json +2 -2
- package/src/context.ts +22 -1
- package/src/d1-storage.ts +20 -3
- package/src/handler.ts +38 -4
- package/src/postgres-storage.ts +6 -0
- package/src/pull.ts +41 -26
- package/src/push.ts +132 -2
- package/src/relational-rows.ts +83 -7
- package/src/schema-window.ts +13 -2
- package/src/schema.ts +25 -0
- package/src/sqlite-storage.ts +23 -0
- package/src/validate.ts +32 -0
package/dist/sqlite-storage.js
CHANGED
|
@@ -13,7 +13,7 @@ import { decodeRow } from '@syncular/core';
|
|
|
13
13
|
import { isSqliteConstraintError, retainRollbackFailure, rollbackAfterError, StorageConstraintError, StorageQueryError, } from './storage-errors.js';
|
|
14
14
|
import { commitDeleteFloor, serveGateRefusal } from './storage.js';
|
|
15
15
|
import { serveNotReadyError } from './readiness.js';
|
|
16
|
-
import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, layoutsOf, migratePayload, parseLayouts, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
|
|
16
|
+
import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, layoutsOf, migratePayload, parseLayouts, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, SQLITE_TABLE_REBUILD_SQL, sqliteRelaxRebuildDdl, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
|
|
17
17
|
import { SQLITE_DDL, SQLITE_UPGRADE_PROBE, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteUpgradeDdl, } from './sqlite-dialect.js';
|
|
18
18
|
import { SqliteAdapterRequiredError, } from './sqlite-driver.js';
|
|
19
19
|
function toStoredCheckpoint(record) {
|
|
@@ -434,6 +434,19 @@ END`);
|
|
|
434
434
|
.run(tableName);
|
|
435
435
|
this.db.exec(dropTableDdl(tableName));
|
|
436
436
|
}
|
|
437
|
+
// A relaxed column loses its NOT NULL before the bump's other DDL
|
|
438
|
+
// (SPEC §2.4), so the index diff below sees the rebuilt table.
|
|
439
|
+
for (const table of schema.tables.values()) {
|
|
440
|
+
const { relaxed } = rewritePlan(table, layouts[table.name], existing.get(table.name));
|
|
441
|
+
if (relaxed.length === 0)
|
|
442
|
+
continue;
|
|
443
|
+
const columns = this.db
|
|
444
|
+
.query(SQLITE_TABLE_REBUILD_SQL)
|
|
445
|
+
.all(table.name);
|
|
446
|
+
for (const statement of sqliteRelaxRebuildDdl(table.name, columns, relaxed)) {
|
|
447
|
+
this.db.exec(statement);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
437
450
|
for (const statement of schemaDdl(schema, existing, 'sqlite', existingIndexes)) {
|
|
438
451
|
this.db.exec(statement);
|
|
439
452
|
}
|
package/dist/validate.d.ts
CHANGED
|
@@ -222,3 +222,28 @@ export declare class CommitAuthorizationRejection extends SyncError {
|
|
|
222
222
|
}
|
|
223
223
|
/** Build the column-keyed row object a validator inspects (§6.7). */
|
|
224
224
|
export declare function toValidateRow(columns: readonly RowColumn[], values: readonly RowValue[]): ValidateRow;
|
|
225
|
+
/** §6.12: the accepted upsert a server column hook completes. `row` is the
|
|
226
|
+
* merged row (present columns over stored values, crdt columns merged) with
|
|
227
|
+
* every server-owned column as stored, or NULL on an insert. */
|
|
228
|
+
export interface ServerColumnsInput {
|
|
229
|
+
readonly table: string;
|
|
230
|
+
readonly rowId: string;
|
|
231
|
+
readonly row: ValidateRow;
|
|
232
|
+
readonly stored: ValidateRow | undefined;
|
|
233
|
+
}
|
|
234
|
+
export interface ServerColumnsContext {
|
|
235
|
+
readonly actorId: string;
|
|
236
|
+
readonly partition: string;
|
|
237
|
+
readonly context?: string;
|
|
238
|
+
/** The §6.8 candidate-state reader of the push transaction. */
|
|
239
|
+
readonly read: CommitValidationReader;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* §6.12: returns values for the table's server-owned columns; a column it
|
|
243
|
+
* omits keeps its value in `row`. A key that names no server-owned column, a
|
|
244
|
+
* value the column's type refuses, or a changed scope column on an update
|
|
245
|
+
* rejects the commit with `sync.constraint_violation`, like any other throw
|
|
246
|
+
* that is not a {@link ValidationRejection}.
|
|
247
|
+
*/
|
|
248
|
+
export type ServerColumnsHook = (input: ServerColumnsInput, ctx: ServerColumnsContext) => ValidateRow | Promise<ValidateRow>;
|
|
249
|
+
export type ServerColumnsRegistry = Readonly<Record<string, ServerColumnsHook>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.95.0",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.95.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/context.ts
CHANGED
|
@@ -25,7 +25,11 @@ import type {
|
|
|
25
25
|
ServerStorage,
|
|
26
26
|
StoredCommit,
|
|
27
27
|
} from './storage';
|
|
28
|
-
import type {
|
|
28
|
+
import type {
|
|
29
|
+
CommitValidator,
|
|
30
|
+
ServerColumnsRegistry,
|
|
31
|
+
ValidatorRegistry,
|
|
32
|
+
} from './validate';
|
|
29
33
|
|
|
30
34
|
/** SSP2 body content type (§1.1). */
|
|
31
35
|
export const SSP2_CONTENT_TYPE = 'application/vnd.syncular.sync.v2';
|
|
@@ -120,6 +124,12 @@ export interface ServerLimits {
|
|
|
120
124
|
* bound gets an image of its own.
|
|
121
125
|
*/
|
|
122
126
|
readonly maxImageSegmentBytes: number;
|
|
127
|
+
/**
|
|
128
|
+
* Segment puts one pull keeps in flight. The pull scans and encodes its
|
|
129
|
+
* pages in order and stores up to this many at once, so it holds this
|
|
130
|
+
* many encoded segments in memory.
|
|
131
|
+
*/
|
|
132
|
+
readonly maxConcurrentSegmentPuts: number;
|
|
123
133
|
}
|
|
124
134
|
|
|
125
135
|
export const DEFAULT_LIMITS: ServerLimits = {
|
|
@@ -136,6 +146,9 @@ export const DEFAULT_LIMITS: ServerLimits = {
|
|
|
136
146
|
maxLiveSectionBytes: 16 * 1024 * 1024,
|
|
137
147
|
// §5.3 DECISION (2026-11-17): a client holds about three images at once.
|
|
138
148
|
maxImageSegmentBytes: 32 * 1024 * 1024,
|
|
149
|
+
// A Workers invocation holds six connections open at once, and an R2 put
|
|
150
|
+
// uses one; past six the runtime queues the put.
|
|
151
|
+
maxConcurrentSegmentPuts: 6,
|
|
139
152
|
};
|
|
140
153
|
|
|
141
154
|
/** Receives applied commits for realtime fanout (§8.2). */
|
|
@@ -192,6 +205,14 @@ export interface SyncServerConfig {
|
|
|
192
205
|
* process, never on the wire.
|
|
193
206
|
*/
|
|
194
207
|
readonly validators?: ValidatorRegistry;
|
|
208
|
+
/**
|
|
209
|
+
* §6.12 per-table server column hooks (`table` → hook). Each accepted
|
|
210
|
+
* upsert on a listed table runs its hook inside the push transaction,
|
|
211
|
+
* after §3.4 authorization and before §6.7 validation; the returned values
|
|
212
|
+
* of the table's server-owned columns are stored and fanned out in the
|
|
213
|
+
* same commit.
|
|
214
|
+
*/
|
|
215
|
+
readonly serverColumns?: ServerColumnsRegistry;
|
|
195
216
|
/**
|
|
196
217
|
* §6.8 whole-commit validator. When present, the storage serializes this
|
|
197
218
|
* partition before operation reads/writes, then invokes the callback once
|
package/src/d1-storage.ts
CHANGED
|
@@ -59,6 +59,9 @@ import {
|
|
|
59
59
|
rekeyRowScopesSql,
|
|
60
60
|
retiredTableNames,
|
|
61
61
|
rewritePlan,
|
|
62
|
+
SQLITE_TABLE_REBUILD_SQL,
|
|
63
|
+
type SqliteRebuildColumnRow,
|
|
64
|
+
sqliteRelaxRebuildDdl,
|
|
62
65
|
rewriteRowSql,
|
|
63
66
|
rewriteValues,
|
|
64
67
|
SCHEMA_META_DDL_SQLITE,
|
|
@@ -870,6 +873,9 @@ interface D1MigrationState {
|
|
|
870
873
|
columns: Record<string, string[]>;
|
|
871
874
|
indexes: Record<string, Record<string, string>>;
|
|
872
875
|
ddl: string[];
|
|
876
|
+
/** Rebuilds of tables with a relaxed column (SPEC §2.4), run before the
|
|
877
|
+
* other DDL. */
|
|
878
|
+
rebuilds?: string[];
|
|
873
879
|
rewrites: Array<{
|
|
874
880
|
table: string;
|
|
875
881
|
oldLayout?: readonly StoredColumnLayout[];
|
|
@@ -1350,14 +1356,15 @@ export class D1ServerStorage
|
|
|
1350
1356
|
}
|
|
1351
1357
|
statementsExecuted += 2;
|
|
1352
1358
|
const columns = await this.#db
|
|
1353
|
-
.prepare(
|
|
1354
|
-
.
|
|
1359
|
+
.prepare(SQLITE_TABLE_REBUILD_SQL)
|
|
1360
|
+
.bind(table.name)
|
|
1361
|
+
.all<SqliteRebuildColumnRow>();
|
|
1355
1362
|
const indexes = await this.#db
|
|
1356
1363
|
.prepare(SQLITE_TABLE_INDEXES_SQL)
|
|
1357
1364
|
.bind(table.name)
|
|
1358
1365
|
.all<SqliteTableIndexRow>();
|
|
1359
1366
|
// Validate the old codec before any application DDL or row rewrite.
|
|
1360
|
-
rewritePlan(
|
|
1367
|
+
const { relaxed } = rewritePlan(
|
|
1361
1368
|
table,
|
|
1362
1369
|
sourceLayouts[table.name],
|
|
1363
1370
|
columns.results.length > 0
|
|
@@ -1368,6 +1375,14 @@ export class D1ServerStorage
|
|
|
1368
1375
|
!(await save(
|
|
1369
1376
|
{
|
|
1370
1377
|
...state,
|
|
1378
|
+
rebuilds: [
|
|
1379
|
+
...(state.rebuilds ?? []),
|
|
1380
|
+
...sqliteRelaxRebuildDdl(
|
|
1381
|
+
table.name,
|
|
1382
|
+
columns.results,
|
|
1383
|
+
relaxed,
|
|
1384
|
+
),
|
|
1385
|
+
],
|
|
1371
1386
|
offset: state.offset + 1,
|
|
1372
1387
|
afterPartition: '',
|
|
1373
1388
|
afterRowId: '',
|
|
@@ -1429,6 +1444,7 @@ export class D1ServerStorage
|
|
|
1429
1444
|
`DELETE FROM sync_blob_refs WHERE tbl='${name.replaceAll("'", "''")}'`,
|
|
1430
1445
|
dropTableDdl(name),
|
|
1431
1446
|
]),
|
|
1447
|
+
...(state.rebuilds ?? []),
|
|
1432
1448
|
...schemaDdl(schema, columns, 'sqlite', indexes),
|
|
1433
1449
|
...rekeyCleanupSql(rekeyed),
|
|
1434
1450
|
];
|
|
@@ -1439,6 +1455,7 @@ export class D1ServerStorage
|
|
|
1439
1455
|
phase: 'ddl',
|
|
1440
1456
|
offset: 0,
|
|
1441
1457
|
ddl,
|
|
1458
|
+
rebuilds: [],
|
|
1442
1459
|
rewrites,
|
|
1443
1460
|
columns: {},
|
|
1444
1461
|
indexes: {},
|
package/src/handler.ts
CHANGED
|
@@ -886,7 +886,7 @@ async function* streamResponse(
|
|
|
886
886
|
/** The join cursor (§4.7): the pin, or the kept cursor. */
|
|
887
887
|
pin: number;
|
|
888
888
|
restricted: boolean;
|
|
889
|
-
frames: ResponseFrame[];
|
|
889
|
+
frames: (ResponseFrame | Promise<ResponseFrame>)[];
|
|
890
890
|
token: string | undefined;
|
|
891
891
|
};
|
|
892
892
|
const states: UnitState[] = units.map(
|
|
@@ -941,6 +941,12 @@ async function* streamResponse(
|
|
|
941
941
|
1,
|
|
942
942
|
Math.floor(100_000 / (limits.limitSnapshotRows + 1)),
|
|
943
943
|
);
|
|
944
|
+
// The segment puts of every bootstrap section, in frame order. A
|
|
945
|
+
// pull holds the encoded bytes of the puts in flight.
|
|
946
|
+
const puts: Promise<ResponseFrame>[] = [];
|
|
947
|
+
const inFlight = new Set<Promise<void>>();
|
|
948
|
+
const maxPuts = limitsOf(ctx).maxConcurrentSegmentPuts;
|
|
949
|
+
let failed = false;
|
|
944
950
|
for (let next = 0; next < bootstrapping.length;) {
|
|
945
951
|
const window: number[] = [];
|
|
946
952
|
let pages = 0;
|
|
@@ -986,7 +992,7 @@ async function* streamResponse(
|
|
|
986
992
|
};
|
|
987
993
|
});
|
|
988
994
|
for (const { index, plan: bootstrap } of batch) {
|
|
989
|
-
const frames: ResponseFrame[] = [];
|
|
995
|
+
const frames: (ResponseFrame | Promise<ResponseFrame>)[] = [];
|
|
990
996
|
const segments = bootstrapSegments(
|
|
991
997
|
ctx,
|
|
992
998
|
schema,
|
|
@@ -997,7 +1003,29 @@ async function* streamResponse(
|
|
|
997
1003
|
);
|
|
998
1004
|
let step = await segments.next();
|
|
999
1005
|
while (!step.done) {
|
|
1000
|
-
|
|
1006
|
+
const frame = step.value;
|
|
1007
|
+
if (frame.type === 'PENDING_REF') {
|
|
1008
|
+
// A rows segment's put runs while later pages are scanned;
|
|
1009
|
+
// the next page waits while `maxPuts` puts are in flight.
|
|
1010
|
+
const settled: Promise<void> = frame.ref.then(
|
|
1011
|
+
() => void inFlight.delete(settled),
|
|
1012
|
+
() => {
|
|
1013
|
+
failed = true;
|
|
1014
|
+
inFlight.delete(settled);
|
|
1015
|
+
},
|
|
1016
|
+
);
|
|
1017
|
+
inFlight.add(settled);
|
|
1018
|
+
puts.push(frame.ref);
|
|
1019
|
+
frames.push(frame.ref);
|
|
1020
|
+
while (inFlight.size >= maxPuts) {
|
|
1021
|
+
await Promise.race(inFlight);
|
|
1022
|
+
if (failed) break;
|
|
1023
|
+
}
|
|
1024
|
+
// A failed put ends the pull before it scans another page.
|
|
1025
|
+
if (failed) for (const put of puts) await put;
|
|
1026
|
+
} else {
|
|
1027
|
+
frames.push(frame);
|
|
1028
|
+
}
|
|
1001
1029
|
step = await segments.next();
|
|
1002
1030
|
}
|
|
1003
1031
|
const restrict = units[index]!.restrict;
|
|
@@ -1010,6 +1038,12 @@ async function* streamResponse(
|
|
|
1010
1038
|
};
|
|
1011
1039
|
}
|
|
1012
1040
|
}
|
|
1041
|
+
// Every put completes before the live read, as in a pull with one put
|
|
1042
|
+
// in flight. A failed put fails the pull with the error of the first
|
|
1043
|
+
// failed page, and the puts still in flight finish unobserved: each
|
|
1044
|
+
// stores its bytes before its record, so no record points at missing
|
|
1045
|
+
// bytes.
|
|
1046
|
+
for (const put of puts) await put;
|
|
1013
1047
|
const bound = (state: UnitState): number | undefined =>
|
|
1014
1048
|
state.kind === 'live'
|
|
1015
1049
|
? R
|
|
@@ -1107,7 +1141,7 @@ async function* streamResponse(
|
|
|
1107
1141
|
);
|
|
1108
1142
|
if (state.kind === 'bootstrap') {
|
|
1109
1143
|
for (const frame of state.frames) {
|
|
1110
|
-
yield encodeResponseFrame(frame, plan.wireVersion);
|
|
1144
|
+
yield encodeResponseFrame(await frame, plan.wireVersion);
|
|
1111
1145
|
}
|
|
1112
1146
|
}
|
|
1113
1147
|
yield encodeResponseFrame(
|
package/src/postgres-storage.ts
CHANGED
|
@@ -2516,6 +2516,12 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2516
2516
|
for (const table of schema.tables.values()) {
|
|
2517
2517
|
const oldLayout = layouts[table.name];
|
|
2518
2518
|
const plan = rewritePlan(table, oldLayout, existing.get(table.name));
|
|
2519
|
+
// SPEC §2.4: payload bytes are unchanged; only the constraint goes.
|
|
2520
|
+
for (const column of plan.relaxed) {
|
|
2521
|
+
await client.query(
|
|
2522
|
+
`ALTER TABLE ${quoteIdent(table.name)} ALTER COLUMN ${quoteIdent(column)} DROP NOT NULL`,
|
|
2523
|
+
);
|
|
2524
|
+
}
|
|
2519
2525
|
const rekey = rekeyed.includes(table.name);
|
|
2520
2526
|
if (!plan.migrate && !plan.backfill && !rekey) continue;
|
|
2521
2527
|
await rewriteRowsOn(
|
package/src/pull.ts
CHANGED
|
@@ -617,6 +617,14 @@ async function* sqliteImageSegment(
|
|
|
617
617
|
return record;
|
|
618
618
|
}
|
|
619
619
|
|
|
620
|
+
/**
|
|
621
|
+
* A section frame: a response frame, or a rows segment's `SEGMENT_REF`
|
|
622
|
+
* whose put is in flight. The consumer emits `ref` in its frame order.
|
|
623
|
+
*/
|
|
624
|
+
type SectionFrame =
|
|
625
|
+
| ResponseFrame
|
|
626
|
+
| { readonly type: 'PENDING_REF'; readonly ref: Promise<ResponseFrame> };
|
|
627
|
+
|
|
620
628
|
/**
|
|
621
629
|
* One table's segments within the page budget: range images while the table
|
|
622
630
|
* is on the image lane, then rows pages. Returns the pages it used, the row
|
|
@@ -635,7 +643,7 @@ async function* tableSegments(
|
|
|
635
643
|
trace: PullSectionTrace | undefined,
|
|
636
644
|
logEpoch: string,
|
|
637
645
|
): AsyncGenerator<
|
|
638
|
-
|
|
646
|
+
SectionFrame,
|
|
639
647
|
{ pages: number; rows: number; rowCursor: string | null; image: boolean }
|
|
640
648
|
> {
|
|
641
649
|
const { storage, segments, partition } = ctx;
|
|
@@ -725,34 +733,40 @@ async function* tableSegments(
|
|
|
725
733
|
});
|
|
726
734
|
yield { type: 'SEGMENT_INLINE', payload: bytes };
|
|
727
735
|
} else {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
partition,
|
|
731
|
-
logEpoch,
|
|
732
|
-
table: pair.table.name,
|
|
733
|
-
schemaVersion: schema.version,
|
|
734
|
-
mediaType: 'rows',
|
|
735
|
-
scopeDigest: digest,
|
|
736
|
-
asOfCommitSeq: pin,
|
|
737
|
-
rowCount: pageRows.length,
|
|
738
|
-
rowCursor,
|
|
739
|
-
nextRowCursor,
|
|
740
|
-
},
|
|
741
|
-
bytes,
|
|
742
|
-
now,
|
|
743
|
-
);
|
|
736
|
+
// The page's put runs while the next page is scanned; the caller keeps
|
|
737
|
+
// `maxConcurrentSegmentPuts` in flight and emits the frames in order.
|
|
744
738
|
trace?.segments.push({
|
|
745
739
|
mediaType: 'rows',
|
|
746
740
|
delivery: 'ref',
|
|
747
741
|
origin: 'built',
|
|
748
|
-
bytes:
|
|
749
|
-
rows:
|
|
742
|
+
bytes: bytes.length,
|
|
743
|
+
rows: pageRows.length,
|
|
750
744
|
});
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
745
|
+
const ref = segments
|
|
746
|
+
.put(
|
|
747
|
+
{
|
|
748
|
+
partition,
|
|
749
|
+
logEpoch,
|
|
750
|
+
table: pair.table.name,
|
|
751
|
+
schemaVersion: schema.version,
|
|
752
|
+
mediaType: 'rows',
|
|
753
|
+
scopeDigest: digest,
|
|
754
|
+
asOfCommitSeq: pin,
|
|
755
|
+
rowCount: pageRows.length,
|
|
756
|
+
rowCursor,
|
|
757
|
+
nextRowCursor,
|
|
758
|
+
},
|
|
759
|
+
bytes,
|
|
760
|
+
now,
|
|
761
|
+
)
|
|
762
|
+
.then(async (record) =>
|
|
763
|
+
segmentRefFrame(
|
|
764
|
+
record,
|
|
765
|
+
digest,
|
|
766
|
+
await signedUrlFields(ctx, limits, record.segmentId, digest, now),
|
|
767
|
+
),
|
|
768
|
+
);
|
|
769
|
+
yield { type: 'PENDING_REF', ref };
|
|
756
770
|
}
|
|
757
771
|
if (!hasMore)
|
|
758
772
|
return { pages: page + 1, rows, rowCursor: null, image: false };
|
|
@@ -876,7 +890,7 @@ export async function* bootstrapSegments(
|
|
|
876
890
|
plan: BootstrapPlan,
|
|
877
891
|
trace: PullSectionTrace | undefined,
|
|
878
892
|
logEpoch: string,
|
|
879
|
-
): AsyncGenerator<
|
|
893
|
+
): AsyncGenerator<SectionFrame, string | undefined> {
|
|
880
894
|
const { unit, pin } = plan;
|
|
881
895
|
let { tableIndex, rowCursor, image } = plan;
|
|
882
896
|
let pages = 0;
|
|
@@ -1028,7 +1042,8 @@ export async function publishSqliteImage(
|
|
|
1028
1042
|
trace,
|
|
1029
1043
|
logEpoch,
|
|
1030
1044
|
)) {
|
|
1031
|
-
if (frame.type === '
|
|
1045
|
+
if (frame.type === 'PENDING_REF') await frame.ref;
|
|
1046
|
+
else if (frame.type === 'SEGMENT_REF' && frame.mediaType === 'sqlite') {
|
|
1032
1047
|
refs.push(frame);
|
|
1033
1048
|
}
|
|
1034
1049
|
}
|
package/src/push.ts
CHANGED
|
@@ -71,6 +71,7 @@ import type {
|
|
|
71
71
|
CommitValidationRow,
|
|
72
72
|
CommitValidator,
|
|
73
73
|
ValidateCommitOperation,
|
|
74
|
+
ServerColumnsRegistry,
|
|
74
75
|
ValidateOpKind,
|
|
75
76
|
ValidatorRegistry,
|
|
76
77
|
} from './validate';
|
|
@@ -288,6 +289,88 @@ async function runValidator(
|
|
|
288
289
|
return undefined;
|
|
289
290
|
}
|
|
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
|
+
{
|
|
327
|
+
actorId,
|
|
328
|
+
partition,
|
|
329
|
+
...(context !== undefined ? { context } : {}),
|
|
330
|
+
read: commitValidationReader(tx, schema),
|
|
331
|
+
},
|
|
332
|
+
);
|
|
333
|
+
for (const [name, value] of Object.entries(output)) {
|
|
334
|
+
const index = table.columnIndex.get(name);
|
|
335
|
+
if (
|
|
336
|
+
index === undefined ||
|
|
337
|
+
!table.serverOwnedColumnIndices.includes(index)
|
|
338
|
+
) {
|
|
339
|
+
throw new Error(
|
|
340
|
+
'server column hook returned a column that is not server-owned',
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
// The column's own codec refuses a value of the wrong type.
|
|
344
|
+
encodeRow([table.columns[index]!], [value]);
|
|
345
|
+
if (
|
|
346
|
+
storedValues !== undefined &&
|
|
347
|
+
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
348
|
+
value !== (storedValues[index] ?? null)
|
|
349
|
+
) {
|
|
350
|
+
throw new Error('server column hook changed a scope column on update');
|
|
351
|
+
}
|
|
352
|
+
values[index] = value;
|
|
353
|
+
present[index] = true;
|
|
354
|
+
}
|
|
355
|
+
} catch (error) {
|
|
356
|
+
if (error instanceof ValidationRejection) {
|
|
357
|
+
return errorRecord(
|
|
358
|
+
opIndex,
|
|
359
|
+
error.code,
|
|
360
|
+
error.message,
|
|
361
|
+
false,
|
|
362
|
+
error.details,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
return errorRecord(
|
|
366
|
+
opIndex,
|
|
367
|
+
'sync.constraint_violation',
|
|
368
|
+
'server column hook failed',
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
291
374
|
/**
|
|
292
375
|
* §5.10.3: merge the row's `crdt` columns in place. For each crdt column,
|
|
293
376
|
* replace the incoming value with `merge(stored, incoming)` (§5.10.2) —
|
|
@@ -352,6 +435,7 @@ async function applyOperation(
|
|
|
352
435
|
blobCtx: BlobApplyContext,
|
|
353
436
|
crdtMergers: CrdtMergerRegistry | undefined,
|
|
354
437
|
validators: ValidatorRegistry | undefined,
|
|
438
|
+
serverColumns: ServerColumnsRegistry | undefined,
|
|
355
439
|
partition: string,
|
|
356
440
|
actorId: string,
|
|
357
441
|
context: string | undefined,
|
|
@@ -699,6 +783,11 @@ async function applyOperation(
|
|
|
699
783
|
`row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`,
|
|
700
784
|
);
|
|
701
785
|
}
|
|
786
|
+
// §6.12: the server discards a pushed value of a server-owned column, so an
|
|
787
|
+
// older client that still writes one stays valid.
|
|
788
|
+
for (const index of table.serverOwnedColumnIndices) {
|
|
789
|
+
sparse[index] = stored === undefined ? null : undefined;
|
|
790
|
+
}
|
|
702
791
|
const pkValue = renderScopeValue(sparse[table.primaryKeyIndex] ?? null);
|
|
703
792
|
if (pkValue !== op.rowId) {
|
|
704
793
|
return errorRecord(
|
|
@@ -808,6 +897,21 @@ async function applyOperation(
|
|
|
808
897
|
crdtMergers,
|
|
809
898
|
);
|
|
810
899
|
if ('kind' in mergeOutcome) return mergeOutcome;
|
|
900
|
+
const serverReject = await runServerColumns(
|
|
901
|
+
serverColumns,
|
|
902
|
+
tx,
|
|
903
|
+
schema,
|
|
904
|
+
table,
|
|
905
|
+
op.rowId,
|
|
906
|
+
values,
|
|
907
|
+
storedValues,
|
|
908
|
+
present,
|
|
909
|
+
opIndex,
|
|
910
|
+
partition,
|
|
911
|
+
actorId,
|
|
912
|
+
context,
|
|
913
|
+
);
|
|
914
|
+
if (serverReject !== undefined) return serverReject;
|
|
811
915
|
const newVersion = stored.serverVersion + 1;
|
|
812
916
|
const nextVersions = new Map<number, number>();
|
|
813
917
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -974,6 +1078,31 @@ async function applyOperation(
|
|
|
974
1078
|
crdtMergers,
|
|
975
1079
|
);
|
|
976
1080
|
if ('kind' in insertMerge) return insertMerge;
|
|
1081
|
+
const serverReject = await runServerColumns(
|
|
1082
|
+
serverColumns,
|
|
1083
|
+
tx,
|
|
1084
|
+
schema,
|
|
1085
|
+
table,
|
|
1086
|
+
op.rowId,
|
|
1087
|
+
values,
|
|
1088
|
+
undefined,
|
|
1089
|
+
present,
|
|
1090
|
+
opIndex,
|
|
1091
|
+
partition,
|
|
1092
|
+
actorId,
|
|
1093
|
+
context,
|
|
1094
|
+
);
|
|
1095
|
+
if (serverReject !== undefined) return serverReject;
|
|
1096
|
+
// §6.12: the stored scopes and the change-scope index take the hook's
|
|
1097
|
+
// values, so the row reaches a server-set scope's units in this commit.
|
|
1098
|
+
const scoped = storedScopesForRow(table, values);
|
|
1099
|
+
if ('missing' in scoped) {
|
|
1100
|
+
return errorRecord(
|
|
1101
|
+
opIndex,
|
|
1102
|
+
'sync.constraint_violation',
|
|
1103
|
+
'server column hook failed',
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
977
1106
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
978
1107
|
// present on an insert.
|
|
979
1108
|
const insertPayload = encodeRow(table.columns, values);
|
|
@@ -1012,7 +1141,7 @@ async function applyOperation(
|
|
|
1012
1141
|
const newRow = {
|
|
1013
1142
|
rowId: op.rowId,
|
|
1014
1143
|
serverVersion,
|
|
1015
|
-
scopes:
|
|
1144
|
+
scopes: scoped.scopes,
|
|
1016
1145
|
payload: insertPayload,
|
|
1017
1146
|
};
|
|
1018
1147
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -1024,7 +1153,7 @@ async function applyOperation(
|
|
|
1024
1153
|
rowId: op.rowId,
|
|
1025
1154
|
op: 'upsert',
|
|
1026
1155
|
rowVersion: serverVersion,
|
|
1027
|
-
scopes:
|
|
1156
|
+
scopes: scoped.scopes,
|
|
1028
1157
|
payload: insertPayload,
|
|
1029
1158
|
},
|
|
1030
1159
|
],
|
|
@@ -2114,6 +2243,7 @@ async function applyPushCommits(
|
|
|
2114
2243
|
blobCtx,
|
|
2115
2244
|
crdtMergers,
|
|
2116
2245
|
validators,
|
|
2246
|
+
ctx.serverColumns,
|
|
2117
2247
|
partition,
|
|
2118
2248
|
ctx.actorId,
|
|
2119
2249
|
ctx.context,
|