@syncular/client 0.67.3 → 0.69.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/apply.js +13 -1
- package/dist/client.d.ts +8 -3
- package/dist/client.js +71 -48
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +2 -2
- package/dist/failed-overlay.d.ts +0 -1
- package/dist/failed-overlay.js +3 -2
- package/dist/http.js +17 -21
- package/dist/native-bridge.d.ts +10 -8
- package/dist/native-bridge.js +23 -3
- package/dist/outbox.d.ts +6 -1
- package/dist/outbox.js +26 -15
- package/dist/outcomes.js +6 -5
- package/dist/progress.d.ts +4 -4
- package/dist/reactive-store.d.ts +8 -3
- package/dist/reactive-store.js +15 -6
- package/dist/worker-core.js +2 -2
- package/dist/worker-host.d.ts +7 -2
- package/dist/worker-host.js +6 -6
- package/dist/worker-protocol.d.ts +3 -1
- package/package.json +3 -3
- package/src/apply.ts +27 -1
- package/src/client.ts +104 -65
- package/src/errors.ts +7 -1
- package/src/failed-overlay.ts +25 -29
- package/src/http.ts +20 -25
- package/src/native-bridge.ts +32 -3
- package/src/outbox.ts +49 -29
- package/src/outcomes.ts +8 -6
- package/src/progress.ts +4 -4
- package/src/reactive-store.ts +23 -8
- package/src/worker-core.ts +2 -2
- package/src/worker-host.ts +15 -7
- package/src/worker-protocol.ts +4 -1
package/src/client.ts
CHANGED
|
@@ -62,6 +62,7 @@ import {
|
|
|
62
62
|
unitId,
|
|
63
63
|
type UnitScopes,
|
|
64
64
|
type WakeReason,
|
|
65
|
+
definedObject,
|
|
65
66
|
} from '@syncular/core';
|
|
66
67
|
import { EncryptError } from '@syncular/core';
|
|
67
68
|
import { EncodeError } from '@syncular/core';
|
|
@@ -161,6 +162,7 @@ import {
|
|
|
161
162
|
} from './local-rebootstrap-receipt';
|
|
162
163
|
import {
|
|
163
164
|
appendOutboxCommit,
|
|
165
|
+
chainsOn,
|
|
164
166
|
deleteOutboxCommit,
|
|
165
167
|
encodeOutboxCommit,
|
|
166
168
|
listOutbox,
|
|
@@ -910,6 +912,18 @@ export type ClientSnapshotReader = {
|
|
|
910
912
|
| Promise<ReturnType<ClientSnapshotMethods[Key]>>;
|
|
911
913
|
};
|
|
912
914
|
|
|
915
|
+
/** §7.7: progress fields of a payload; no payload is current. */
|
|
916
|
+
const NO_PAYLOAD: Partial<SyncProgress> = {
|
|
917
|
+
table: undefined,
|
|
918
|
+
segmentId: undefined,
|
|
919
|
+
bytesReceived: 0,
|
|
920
|
+
bytesTotal: undefined,
|
|
921
|
+
rowsProcessed: 0,
|
|
922
|
+
rowsTotal: undefined,
|
|
923
|
+
importWriteMs: undefined,
|
|
924
|
+
importCommitMs: undefined,
|
|
925
|
+
};
|
|
926
|
+
|
|
913
927
|
export class SyncClient {
|
|
914
928
|
readonly #config: SyncClientConfig;
|
|
915
929
|
readonly #authorityReads: AuthorityReadPolicy | undefined;
|
|
@@ -1276,21 +1290,17 @@ export class SyncClient {
|
|
|
1276
1290
|
throw classifySqliteFailure(error).error;
|
|
1277
1291
|
}
|
|
1278
1292
|
this.#started = true;
|
|
1293
|
+
// §4.8: an eviction a crash interrupted finishes before any read, with or
|
|
1294
|
+
// without a transport; a preflight replica finishes it at activation.
|
|
1295
|
+
if (this.#securityLifecycle === 'active')
|
|
1296
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
1279
1297
|
// A persisted active subscription needs one catch-up round on every open:
|
|
1280
1298
|
// realtime only covers changes after the socket connects, and an
|
|
1281
1299
|
// idempotent setWindow/subscribe call correctly creates no new command
|
|
1282
1300
|
// effect. Pending outbox work has the same restart requirement. Surface
|
|
1283
1301
|
// this as an exact core-owned intent so hosts never need a startup poll or
|
|
1284
1302
|
// an application-issued sync() call.
|
|
1285
|
-
|
|
1286
|
-
this.#schemaFloor === undefined &&
|
|
1287
|
-
(countOutbox(this.#db) > 0 ||
|
|
1288
|
-
this.#db.query(
|
|
1289
|
-
`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`,
|
|
1290
|
-
).length > 0 ||
|
|
1291
|
-
loadPendingEvictions(this.#db).length > 0 ||
|
|
1292
|
-
loadUnits(this.#db).some((unit) => unit.status === 'active'));
|
|
1293
|
-
if (startupWork && this.#securityLifecycle === 'active') {
|
|
1303
|
+
if (this.#securityLifecycle === 'active' && this.#hasStartupWork()) {
|
|
1294
1304
|
this.#needsPull = true;
|
|
1295
1305
|
this.#emitSyncNeeded('startup');
|
|
1296
1306
|
this.#emitSyncIntent({ kind: 'interactive' });
|
|
@@ -1667,22 +1677,29 @@ export class SyncClient {
|
|
|
1667
1677
|
);
|
|
1668
1678
|
}
|
|
1669
1679
|
await (this.#preflightBarrier ?? this.#opChain);
|
|
1680
|
+
// §4.8: an interrupted eviction finishes before protected reads open.
|
|
1681
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
1670
1682
|
this.#encryption = options.encryption;
|
|
1671
1683
|
this.#securityLifecycle = 'active';
|
|
1672
|
-
|
|
1684
|
+
if (this.#hasStartupWork()) {
|
|
1685
|
+
this.#setSyncNeeded(true);
|
|
1686
|
+
this.#emitSyncNeeded('startup');
|
|
1687
|
+
this.#emitSyncIntent({ kind: 'interactive' });
|
|
1688
|
+
}
|
|
1689
|
+
this.#emitDiagnostics();
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/** Persisted work an opened or activated replica owes one round (§8.4). */
|
|
1693
|
+
#hasStartupWork(): boolean {
|
|
1694
|
+
return (
|
|
1673
1695
|
this.#schemaFloor === undefined &&
|
|
1674
1696
|
(countOutbox(this.#db) > 0 ||
|
|
1675
1697
|
this.#db.query(
|
|
1676
1698
|
`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`,
|
|
1677
1699
|
).length > 0 ||
|
|
1678
1700
|
loadPendingEvictions(this.#db).length > 0 ||
|
|
1679
|
-
loadUnits(this.#db).some((unit) => unit.status === 'active'))
|
|
1680
|
-
|
|
1681
|
-
this.#setSyncNeeded(true);
|
|
1682
|
-
this.#emitSyncNeeded('startup');
|
|
1683
|
-
this.#emitSyncIntent({ kind: 'interactive' });
|
|
1684
|
-
}
|
|
1685
|
-
this.#emitDiagnostics();
|
|
1701
|
+
loadUnits(this.#db).some((unit) => unit.status === 'active'))
|
|
1702
|
+
);
|
|
1686
1703
|
}
|
|
1687
1704
|
|
|
1688
1705
|
// -- accessors ------------------------------------------------------------
|
|
@@ -1837,12 +1854,8 @@ export class SyncClient {
|
|
|
1837
1854
|
});
|
|
1838
1855
|
};
|
|
1839
1856
|
const deliveryOf = (id: string): CommitDelivery => {
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
`SELECT 1 FROM ${OUTBOX_TABLE} WHERE client_commit_id=? LIMIT 1`,
|
|
1843
|
-
[id],
|
|
1844
|
-
).length > 0;
|
|
1845
|
-
if (pending) return { status: 'pending', clientCommitId: id };
|
|
1857
|
+
if (this.#outboxCommitExists(id))
|
|
1858
|
+
return { status: 'pending', clientCommitId: id };
|
|
1846
1859
|
const outcome = persistedCommitOutcome(this.#db, id);
|
|
1847
1860
|
return outcome === undefined
|
|
1848
1861
|
? { status: 'unknown', clientCommitId: id }
|
|
@@ -3389,15 +3402,35 @@ export class SyncClient {
|
|
|
3389
3402
|
/**
|
|
3390
3403
|
* Record one atomic local commit (§7.1): appended to the outbox in
|
|
3391
3404
|
* schema-agnostic form and applied optimistically to the local mirror.
|
|
3392
|
-
* Returns the generated `clientCommitId
|
|
3405
|
+
* Returns the generated `clientCommitId`, or the caller's unused one
|
|
3406
|
+
* (`client.commit_id_reused` when this replica holds it).
|
|
3393
3407
|
*/
|
|
3394
|
-
mutate(
|
|
3395
|
-
|
|
3408
|
+
mutate(
|
|
3409
|
+
mutations: readonly MutationInput[],
|
|
3410
|
+
options?: { readonly clientCommitId?: string },
|
|
3411
|
+
): string {
|
|
3412
|
+
return this.#recordMutations(mutations, options?.clientCommitId);
|
|
3396
3413
|
}
|
|
3397
3414
|
|
|
3398
|
-
#recordMutations(
|
|
3415
|
+
#recordMutations(
|
|
3416
|
+
mutations: readonly MutationInput[],
|
|
3417
|
+
supplied?: string,
|
|
3418
|
+
): string {
|
|
3399
3419
|
this.#requireActive();
|
|
3400
|
-
|
|
3420
|
+
// §7.1: a supplied ID is non-empty (§6.1) and unknown to this replica.
|
|
3421
|
+
if (supplied === '') throw invalidRequest('clientCommitId is empty');
|
|
3422
|
+
if (
|
|
3423
|
+
supplied !== undefined &&
|
|
3424
|
+
(this.#outboxCommitExists(supplied) ||
|
|
3425
|
+
readCommitOutcome(this.#db, supplied))
|
|
3426
|
+
)
|
|
3427
|
+
throw new ClientSyncError(
|
|
3428
|
+
'client.commit_id_reused',
|
|
3429
|
+
'clientCommitId is held',
|
|
3430
|
+
false,
|
|
3431
|
+
{ clientCommitId: supplied },
|
|
3432
|
+
);
|
|
3433
|
+
const clientCommitId = supplied ?? crypto.randomUUID();
|
|
3401
3434
|
const operations: OutboxOperation[] = mutations.map((mutation) => {
|
|
3402
3435
|
const table = this.#table(mutation.table);
|
|
3403
3436
|
if (mutation.op === 'delete') {
|
|
@@ -3432,6 +3465,7 @@ export class SyncClient {
|
|
|
3432
3465
|
);
|
|
3433
3466
|
for (const column of table.columns) {
|
|
3434
3467
|
if (!normalized.has(column.name)) continue;
|
|
3468
|
+
const at = { table: table.name, column: column.name };
|
|
3435
3469
|
// §3.4 rule 5 / §6.2: scope columns are immutable on update. The
|
|
3436
3470
|
// server accepts a present scope column whose value equals the
|
|
3437
3471
|
// stored row and applies it as a no-op, so a decoded envelope
|
|
@@ -3457,7 +3491,9 @@ export class SyncClient {
|
|
|
3457
3491
|
)
|
|
3458
3492
|
) {
|
|
3459
3493
|
throw invalidRequest(
|
|
3460
|
-
|
|
3494
|
+
'patch cannot write a scope column (§3.4)',
|
|
3495
|
+
false,
|
|
3496
|
+
at,
|
|
3461
3497
|
);
|
|
3462
3498
|
}
|
|
3463
3499
|
continue;
|
|
@@ -3466,7 +3502,9 @@ export class SyncClient {
|
|
|
3466
3502
|
if (value === undefined || value === null) {
|
|
3467
3503
|
if (!column.nullable) {
|
|
3468
3504
|
throw invalidRequest(
|
|
3469
|
-
|
|
3505
|
+
'a non-nullable column is null (§6.1)',
|
|
3506
|
+
false,
|
|
3507
|
+
at,
|
|
3470
3508
|
);
|
|
3471
3509
|
}
|
|
3472
3510
|
json[column.name] = null;
|
|
@@ -3571,9 +3609,12 @@ export class SyncClient {
|
|
|
3571
3609
|
}
|
|
3572
3610
|
|
|
3573
3611
|
/** Host-facing mutation result with explicit network work intent (§7.5). */
|
|
3574
|
-
mutateCommand(
|
|
3612
|
+
mutateCommand(
|
|
3613
|
+
mutations: readonly MutationInput[],
|
|
3614
|
+
options?: { readonly clientCommitId?: string },
|
|
3615
|
+
): CommandResult<string> {
|
|
3575
3616
|
return {
|
|
3576
|
-
value: this.mutate(mutations),
|
|
3617
|
+
value: this.mutate(mutations, options),
|
|
3577
3618
|
effects: {
|
|
3578
3619
|
sync: { kind: this.#transportEnabled ? 'interactive' : 'none' },
|
|
3579
3620
|
},
|
|
@@ -3600,9 +3641,9 @@ export class SyncClient {
|
|
|
3600
3641
|
const normalized = normalizeRecordKeys(compiled, partial);
|
|
3601
3642
|
const pkOverride = normalized.get(pkColumn.name);
|
|
3602
3643
|
if (pkOverride !== undefined && String(pkOverride) !== rowId) {
|
|
3603
|
-
throw invalidRequest(
|
|
3604
|
-
|
|
3605
|
-
);
|
|
3644
|
+
throw invalidRequest('patch cannot change the primary key', false, {
|
|
3645
|
+
table,
|
|
3646
|
+
});
|
|
3606
3647
|
}
|
|
3607
3648
|
const pkType = localColumnType(pkColumn);
|
|
3608
3649
|
const typedPk =
|
|
@@ -3981,7 +4022,16 @@ export class SyncClient {
|
|
|
3981
4022
|
// §6.1/§7.1: the commit-count and operation caps are hard. A first
|
|
3982
4023
|
// commit that alone exceeds the operation cap is a typed capacity error
|
|
3983
4024
|
// and stays queued; a later one defers with the complete suffix.
|
|
3984
|
-
|
|
4025
|
+
// §7.1 chained base: a commit chained on an earlier commit of this
|
|
4026
|
+
// request waits for that commit's change, which supplies its version.
|
|
4027
|
+
if (
|
|
4028
|
+
(maxCommits !== undefined && pushFrames.length >= maxCommits) ||
|
|
4029
|
+
outbox.some((earlier) =>
|
|
4030
|
+
earlier.operations.some((prior) =>
|
|
4031
|
+
commit.operations.some((operation) => chainsOn(prior, operation)),
|
|
4032
|
+
),
|
|
4033
|
+
)
|
|
4034
|
+
) {
|
|
3985
4035
|
deferred = pendingCount - processed;
|
|
3986
4036
|
break;
|
|
3987
4037
|
}
|
|
@@ -4011,12 +4061,10 @@ export class SyncClient {
|
|
|
4011
4061
|
},
|
|
4012
4062
|
);
|
|
4013
4063
|
} catch (error) {
|
|
4014
|
-
if (
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
}
|
|
4019
|
-
if (error instanceof EncryptError) {
|
|
4064
|
+
if (
|
|
4065
|
+
error instanceof OutboxEncodeError ||
|
|
4066
|
+
error instanceof EncryptError
|
|
4067
|
+
) {
|
|
4020
4068
|
this.#dropIncompatibleCommit(commit, error.message, error.code);
|
|
4021
4069
|
processed += 1;
|
|
4022
4070
|
continue;
|
|
@@ -5406,6 +5454,12 @@ export class SyncClient {
|
|
|
5406
5454
|
skip: false,
|
|
5407
5455
|
}
|
|
5408
5456
|
: undefined;
|
|
5457
|
+
// §7.7: a section boundary ends the previous payload.
|
|
5458
|
+
this.#progress.update({
|
|
5459
|
+
...NO_PAYLOAD,
|
|
5460
|
+
phase: 'import',
|
|
5461
|
+
unitId: section?.unit.id,
|
|
5462
|
+
});
|
|
5409
5463
|
break;
|
|
5410
5464
|
}
|
|
5411
5465
|
case 'SEGMENT_INLINE':
|
|
@@ -5424,16 +5478,13 @@ export class SyncClient {
|
|
|
5424
5478
|
let bytes: Uint8Array;
|
|
5425
5479
|
if (frame.type === 'SEGMENT_REF') {
|
|
5426
5480
|
this.#progress.update({
|
|
5481
|
+
...NO_PAYLOAD,
|
|
5427
5482
|
phase: 'download',
|
|
5428
5483
|
unitId,
|
|
5429
5484
|
table: frame.table,
|
|
5430
5485
|
segmentId: frame.segmentId,
|
|
5431
|
-
bytesReceived: 0,
|
|
5432
5486
|
bytesTotal: frame.byteLength,
|
|
5433
|
-
rowsProcessed: 0,
|
|
5434
5487
|
rowsTotal: frame.rowCount,
|
|
5435
|
-
importWriteMs: undefined,
|
|
5436
|
-
importCommitMs: undefined,
|
|
5437
5488
|
});
|
|
5438
5489
|
prefetch();
|
|
5439
5490
|
const pending = prefetched.get(frame);
|
|
@@ -5467,19 +5518,16 @@ export class SyncClient {
|
|
|
5467
5518
|
image === undefined ? decodeRowsSegment(bytes) : undefined;
|
|
5468
5519
|
if (frame.type === 'SEGMENT_INLINE' && segment !== undefined)
|
|
5469
5520
|
this.#progress.update({
|
|
5521
|
+
...NO_PAYLOAD,
|
|
5470
5522
|
phase: 'import',
|
|
5471
5523
|
unitId,
|
|
5472
5524
|
table: segment.table,
|
|
5473
|
-
segmentId: undefined,
|
|
5474
5525
|
bytesReceived: bytes.byteLength,
|
|
5475
5526
|
bytesTotal: bytes.byteLength,
|
|
5476
|
-
rowsProcessed: 0,
|
|
5477
5527
|
rowsTotal: segment.blocks.reduce(
|
|
5478
5528
|
(n, block) => n + block.length,
|
|
5479
5529
|
0,
|
|
5480
5530
|
),
|
|
5481
|
-
importWriteMs: undefined,
|
|
5482
|
-
importCommitMs: undefined,
|
|
5483
5531
|
});
|
|
5484
5532
|
await this.#applySegmentOrFail(
|
|
5485
5533
|
section,
|
|
@@ -5529,6 +5577,12 @@ export class SyncClient {
|
|
|
5529
5577
|
)
|
|
5530
5578
|
this.#finishSection(section, frame, summary);
|
|
5531
5579
|
section = undefined;
|
|
5580
|
+
// The counters stay: `complete` and `failed` report them.
|
|
5581
|
+
this.#progress.update({
|
|
5582
|
+
unitId: undefined,
|
|
5583
|
+
table: undefined,
|
|
5584
|
+
segmentId: undefined,
|
|
5585
|
+
});
|
|
5532
5586
|
break;
|
|
5533
5587
|
}
|
|
5534
5588
|
case 'LIVE_START': {
|
|
@@ -6859,21 +6913,6 @@ export class SyncClient {
|
|
|
6859
6913
|
}
|
|
6860
6914
|
}
|
|
6861
6915
|
|
|
6862
|
-
type DefinedObject<T> = {
|
|
6863
|
-
[K in keyof T as undefined extends T[K] ? never : K]: T[K];
|
|
6864
|
-
} & {
|
|
6865
|
-
[K in keyof T as undefined extends T[K] ? K : never]?: Exclude<
|
|
6866
|
-
T[K],
|
|
6867
|
-
undefined
|
|
6868
|
-
>;
|
|
6869
|
-
};
|
|
6870
|
-
function definedObject<const T extends object>(record: T): DefinedObject<T> {
|
|
6871
|
-
for (const key of Object.keys(record) as (keyof T)[]) {
|
|
6872
|
-
if (record[key] === undefined) delete record[key];
|
|
6873
|
-
}
|
|
6874
|
-
return record as DefinedObject<T>;
|
|
6875
|
-
}
|
|
6876
|
-
|
|
6877
6916
|
function requireRowId(value: unknown): string {
|
|
6878
6917
|
if (typeof value === 'string' && value.length > 0) return value;
|
|
6879
6918
|
if (typeof value === 'number' && Number.isSafeInteger(value)) {
|
package/src/errors.ts
CHANGED
|
@@ -61,8 +61,14 @@ export class ClientSyncError extends Error {
|
|
|
61
61
|
export function invalidRequest(
|
|
62
62
|
message: string,
|
|
63
63
|
retryable = false,
|
|
64
|
+
details?: Readonly<Record<string, unknown>>,
|
|
64
65
|
): ClientSyncError {
|
|
65
|
-
return new ClientSyncError(
|
|
66
|
+
return new ClientSyncError(
|
|
67
|
+
'sync.invalid_request',
|
|
68
|
+
message,
|
|
69
|
+
retryable,
|
|
70
|
+
details,
|
|
71
|
+
);
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
/** Shared retryable `sync.transport_failed` construction for client transports. */
|
package/src/failed-overlay.ts
CHANGED
|
@@ -29,6 +29,8 @@ import type {
|
|
|
29
29
|
import { deleteLocalRow, upsertLocalRow } from './apply';
|
|
30
30
|
|
|
31
31
|
/** Protected failure state. The server base advances independently of intent. */
|
|
32
|
+
const SET_UNIQUE_CONFLICTS = `UPDATE ${RETAINED_ROWS} SET unique_conflicts=? WHERE commit_id=? AND idx=?`;
|
|
33
|
+
|
|
32
34
|
export function ensureFailedOverlaySchema(db: ClientDatabase): void {
|
|
33
35
|
db.exec(
|
|
34
36
|
`CREATE TABLE IF NOT EXISTS ${RETAINED_ROWS}(commit_id TEXT NOT NULL,idx INTEGER NOT NULL,tbl TEXT NOT NULL,id TEXT NOT NULL,at INTEGER NOT NULL,op TEXT NOT NULL,intent TEXT,base TEXT,version INTEGER,unique_conflicts TEXT,commit_seq INTEGER,PRIMARY KEY(commit_id,idx))`,
|
|
@@ -81,22 +83,19 @@ export function retainedBaseWrite(
|
|
|
81
83
|
})),
|
|
82
84
|
);
|
|
83
85
|
}
|
|
84
|
-
db.exec(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
Number(retained.idx),
|
|
98
|
-
],
|
|
99
|
-
);
|
|
86
|
+
db.exec(SET_UNIQUE_CONFLICTS, [
|
|
87
|
+
conflicts.length
|
|
88
|
+
? JSON.stringify(
|
|
89
|
+
conflicts.sort(
|
|
90
|
+
(a, b) =>
|
|
91
|
+
table.indexes.findIndex((index) => index.name === a.index) -
|
|
92
|
+
table.indexes.findIndex((index) => index.name === b.index),
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
: null,
|
|
96
|
+
retained.commit_id as string,
|
|
97
|
+
Number(retained.idx),
|
|
98
|
+
]);
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
101
|
|
|
@@ -272,19 +271,16 @@ export function failedOverlayCommits(
|
|
|
272
271
|
).length,
|
|
273
272
|
);
|
|
274
273
|
if (authorized.length)
|
|
275
|
-
db.exec(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
row.idx as number,
|
|
286
|
-
],
|
|
287
|
-
);
|
|
274
|
+
db.exec(SET_UNIQUE_CONFLICTS, [
|
|
275
|
+
JSON.stringify(
|
|
276
|
+
authorized.map((conflict) => ({
|
|
277
|
+
...conflict,
|
|
278
|
+
serverRow: mapRowValues(conflict.serverRow, rowValueToJson),
|
|
279
|
+
})),
|
|
280
|
+
),
|
|
281
|
+
row.commit_id as string,
|
|
282
|
+
row.idx as number,
|
|
283
|
+
]);
|
|
288
284
|
}
|
|
289
285
|
}
|
|
290
286
|
const prior = commits.get(id);
|
package/src/http.ts
CHANGED
|
@@ -411,15 +411,8 @@ export function httpBlobTransport(
|
|
|
411
411
|
} catch {
|
|
412
412
|
// Not JSON: inline bytes.
|
|
413
413
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
kind: 'url',
|
|
417
|
-
url: envelope.url,
|
|
418
|
-
...(typeof envelope.urlExpiresAtMs === 'number'
|
|
419
|
-
? { urlExpiresAtMs: envelope.urlExpiresAtMs }
|
|
420
|
-
: {}),
|
|
421
|
-
};
|
|
422
|
-
}
|
|
414
|
+
const grant = envelope && urlGrant(envelope);
|
|
415
|
+
if (grant) return grant;
|
|
423
416
|
}
|
|
424
417
|
return { kind: 'bytes', bytes };
|
|
425
418
|
},
|
|
@@ -461,22 +454,11 @@ export function httpBlobTransport(
|
|
|
461
454
|
'blob grant read failed',
|
|
462
455
|
() => response.json(),
|
|
463
456
|
response.status,
|
|
464
|
-
)) as {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
present
|
|
468
|
-
|
|
469
|
-
if (typeof body.url === 'string') {
|
|
470
|
-
return {
|
|
471
|
-
kind: 'url',
|
|
472
|
-
url: body.url,
|
|
473
|
-
...(typeof body.urlExpiresAtMs === 'number'
|
|
474
|
-
? { urlExpiresAtMs: body.urlExpiresAtMs }
|
|
475
|
-
: {}),
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
if (body.present === true) return { kind: 'present' };
|
|
479
|
-
return { kind: 'none' };
|
|
457
|
+
)) as { url?: unknown; urlExpiresAtMs?: unknown; present?: boolean };
|
|
458
|
+
return (
|
|
459
|
+
urlGrant(body) ??
|
|
460
|
+
(body.present === true ? { kind: 'present' } : { kind: 'none' })
|
|
461
|
+
);
|
|
480
462
|
},
|
|
481
463
|
// §5.9.3: direct-to-storage PUT — no host auth (the URL is the grant).
|
|
482
464
|
uploadToUrl: async (blobId, url, bytes, mediaType, signal) => {
|
|
@@ -569,3 +551,16 @@ export function webSocketRealtimeConnector(
|
|
|
569
551
|
};
|
|
570
552
|
});
|
|
571
553
|
}
|
|
554
|
+
|
|
555
|
+
/** §5.9.3, §5.9.5: a JSON body with a string `url` is a presigned grant. */
|
|
556
|
+
function urlGrant(body: { url?: unknown; urlExpiresAtMs?: unknown }) {
|
|
557
|
+
return typeof body.url === 'string'
|
|
558
|
+
? {
|
|
559
|
+
kind: 'url' as const,
|
|
560
|
+
url: body.url,
|
|
561
|
+
...(typeof body.urlExpiresAtMs === 'number'
|
|
562
|
+
? { urlExpiresAtMs: body.urlExpiresAtMs }
|
|
563
|
+
: {}),
|
|
564
|
+
}
|
|
565
|
+
: undefined;
|
|
566
|
+
}
|
package/src/native-bridge.ts
CHANGED
|
@@ -293,6 +293,9 @@ type OutcomeRowValue = ConflictRecord['serverRow'][string];
|
|
|
293
293
|
* returns a promise from one host round trip; React hooks accept the
|
|
294
294
|
* canonical snapshot interface directly.
|
|
295
295
|
*/
|
|
296
|
+
/** Native hosts whose one core a live bridge client of this page holds. */
|
|
297
|
+
const heldHosts = new WeakSet<object>();
|
|
298
|
+
|
|
296
299
|
export abstract class NativeBridgeClient implements PromiseMethods<
|
|
297
300
|
ClientSnapshotMethods & Pick<SyncClient, 'unit' | 'units' | 'unitCatchup'>
|
|
298
301
|
> {
|
|
@@ -308,6 +311,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
308
311
|
>();
|
|
309
312
|
#closed = false;
|
|
310
313
|
#closing: Promise<void> | undefined;
|
|
314
|
+
#host: object | undefined;
|
|
311
315
|
#securityLifecycle: SecurityLifecycle;
|
|
312
316
|
#preflightBarrier: Promise<void> | undefined;
|
|
313
317
|
|
|
@@ -371,14 +375,32 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
371
375
|
return reply.result;
|
|
372
376
|
}
|
|
373
377
|
|
|
374
|
-
/**
|
|
375
|
-
|
|
378
|
+
/**
|
|
379
|
+
* @internal Settle the native `create`; a failure detaches the listener.
|
|
380
|
+
* `host` is the page's native host: its one core serves one live bridge
|
|
381
|
+
* client, so a second `create` before `close()` settles is refused
|
|
382
|
+
* instead of replacing the first client's core.
|
|
383
|
+
*/
|
|
384
|
+
async __open(
|
|
385
|
+
create: () => Promise<NativeReply>,
|
|
386
|
+
host: object,
|
|
387
|
+
): Promise<void> {
|
|
388
|
+
if (heldHosts.has(host)) {
|
|
389
|
+
this.#release();
|
|
390
|
+
throw new this.#Error(
|
|
391
|
+
'client.native_client_exists',
|
|
392
|
+
'this page already holds the native client; close it before creating another',
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
heldHosts.add(host);
|
|
376
396
|
try {
|
|
377
397
|
await this.#reply(create);
|
|
378
398
|
} catch (error) {
|
|
399
|
+
heldHosts.delete(host);
|
|
379
400
|
this.#release();
|
|
380
401
|
throw error;
|
|
381
402
|
}
|
|
403
|
+
this.#host = host;
|
|
382
404
|
}
|
|
383
405
|
|
|
384
406
|
/** Dispatch a command through the closed check and the preflight gate. */
|
|
@@ -778,9 +800,15 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
778
800
|
});
|
|
779
801
|
}
|
|
780
802
|
|
|
781
|
-
async mutate(
|
|
803
|
+
async mutate(
|
|
804
|
+
mutations: readonly MutationInput[],
|
|
805
|
+
options?: { readonly clientCommitId?: string },
|
|
806
|
+
): Promise<string> {
|
|
782
807
|
const result = (await this.command('mutate', {
|
|
783
808
|
mutations: encodeValue(mutations),
|
|
809
|
+
...(options?.clientCommitId === undefined
|
|
810
|
+
? {}
|
|
811
|
+
: { clientCommitId: options.clientCommitId }),
|
|
784
812
|
})) as { clientCommitId: string };
|
|
785
813
|
return result.clientCommitId;
|
|
786
814
|
}
|
|
@@ -1163,6 +1191,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1163
1191
|
const shutdown = this.shutdown();
|
|
1164
1192
|
this.#closed = true;
|
|
1165
1193
|
this.#closing = shutdown.finally(() => {
|
|
1194
|
+
if (this.#host !== undefined) heldHosts.delete(this.#host);
|
|
1166
1195
|
this.#release();
|
|
1167
1196
|
this.#invalidationListeners.clear();
|
|
1168
1197
|
this.#progress.clear();
|