@syncular/client 0.67.3 → 0.68.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/client.js +41 -20
- package/dist/native-bridge.d.ts +7 -7
- package/dist/native-bridge.js +19 -2
- 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-host.d.ts +4 -1
- package/dist/worker-host.js +4 -4
- package/package.json +3 -3
- package/src/client.ts +46 -25
- package/src/native-bridge.ts +25 -2
- package/src/progress.ts +4 -4
- package/src/reactive-store.ts +23 -8
- package/src/worker-host.ts +10 -5
package/dist/client.js
CHANGED
|
@@ -139,6 +139,17 @@ function realtimeErrorCode(error) {
|
|
|
139
139
|
const code = error?.code;
|
|
140
140
|
return typeof code === 'string' ? code : undefined;
|
|
141
141
|
}
|
|
142
|
+
/** §7.7: progress fields of a payload; no payload is current. */
|
|
143
|
+
const NO_PAYLOAD = {
|
|
144
|
+
table: undefined,
|
|
145
|
+
segmentId: undefined,
|
|
146
|
+
bytesReceived: 0,
|
|
147
|
+
bytesTotal: undefined,
|
|
148
|
+
rowsProcessed: 0,
|
|
149
|
+
rowsTotal: undefined,
|
|
150
|
+
importWriteMs: undefined,
|
|
151
|
+
importCommitMs: undefined,
|
|
152
|
+
};
|
|
142
153
|
export class SyncClient {
|
|
143
154
|
#config;
|
|
144
155
|
#authorityReads;
|
|
@@ -450,18 +461,17 @@ export class SyncClient {
|
|
|
450
461
|
throw classifySqliteFailure(error).error;
|
|
451
462
|
}
|
|
452
463
|
this.#started = true;
|
|
464
|
+
// §4.8: an eviction a crash interrupted finishes before any read, with or
|
|
465
|
+
// without a transport; a preflight replica finishes it at activation.
|
|
466
|
+
if (this.#securityLifecycle === 'active')
|
|
467
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
453
468
|
// A persisted active subscription needs one catch-up round on every open:
|
|
454
469
|
// realtime only covers changes after the socket connects, and an
|
|
455
470
|
// idempotent setWindow/subscribe call correctly creates no new command
|
|
456
471
|
// effect. Pending outbox work has the same restart requirement. Surface
|
|
457
472
|
// this as an exact core-owned intent so hosts never need a startup poll or
|
|
458
473
|
// an application-issued sync() call.
|
|
459
|
-
|
|
460
|
-
(countOutbox(this.#db) > 0 ||
|
|
461
|
-
this.#db.query(`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`).length > 0 ||
|
|
462
|
-
loadPendingEvictions(this.#db).length > 0 ||
|
|
463
|
-
loadUnits(this.#db).some((unit) => unit.status === 'active'));
|
|
464
|
-
if (startupWork && this.#securityLifecycle === 'active') {
|
|
474
|
+
if (this.#securityLifecycle === 'active' && this.#hasStartupWork()) {
|
|
465
475
|
this.#needsPull = true;
|
|
466
476
|
this.#emitSyncNeeded('startup');
|
|
467
477
|
this.#emitSyncIntent({ kind: 'interactive' });
|
|
@@ -792,20 +802,25 @@ export class SyncClient {
|
|
|
792
802
|
throw invalidRequest('activateSecurity requires the client to be in security preflight');
|
|
793
803
|
}
|
|
794
804
|
await (this.#preflightBarrier ?? this.#opChain);
|
|
805
|
+
// §4.8: an interrupted eviction finishes before protected reads open.
|
|
806
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
795
807
|
this.#encryption = options.encryption;
|
|
796
808
|
this.#securityLifecycle = 'active';
|
|
797
|
-
|
|
798
|
-
(countOutbox(this.#db) > 0 ||
|
|
799
|
-
this.#db.query(`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`).length > 0 ||
|
|
800
|
-
loadPendingEvictions(this.#db).length > 0 ||
|
|
801
|
-
loadUnits(this.#db).some((unit) => unit.status === 'active'));
|
|
802
|
-
if (startupWork) {
|
|
809
|
+
if (this.#hasStartupWork()) {
|
|
803
810
|
this.#setSyncNeeded(true);
|
|
804
811
|
this.#emitSyncNeeded('startup');
|
|
805
812
|
this.#emitSyncIntent({ kind: 'interactive' });
|
|
806
813
|
}
|
|
807
814
|
this.#emitDiagnostics();
|
|
808
815
|
}
|
|
816
|
+
/** Persisted work an opened or activated replica owes one round (§8.4). */
|
|
817
|
+
#hasStartupWork() {
|
|
818
|
+
return (this.#schemaFloor === undefined &&
|
|
819
|
+
(countOutbox(this.#db) > 0 ||
|
|
820
|
+
this.#db.query(`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`).length > 0 ||
|
|
821
|
+
loadPendingEvictions(this.#db).length > 0 ||
|
|
822
|
+
loadUnits(this.#db).some((unit) => unit.status === 'active')));
|
|
823
|
+
}
|
|
809
824
|
// -- accessors ------------------------------------------------------------
|
|
810
825
|
get clientId() {
|
|
811
826
|
return this.#clientId;
|
|
@@ -3892,6 +3907,12 @@ export class SyncClient {
|
|
|
3892
3907
|
skip: false,
|
|
3893
3908
|
}
|
|
3894
3909
|
: undefined;
|
|
3910
|
+
// §7.7: a section boundary ends the previous payload.
|
|
3911
|
+
this.#progress.update({
|
|
3912
|
+
...NO_PAYLOAD,
|
|
3913
|
+
phase: 'import',
|
|
3914
|
+
unitId: section?.unit.id,
|
|
3915
|
+
});
|
|
3895
3916
|
break;
|
|
3896
3917
|
}
|
|
3897
3918
|
case 'SEGMENT_INLINE':
|
|
@@ -3907,16 +3928,13 @@ export class SyncClient {
|
|
|
3907
3928
|
let bytes;
|
|
3908
3929
|
if (frame.type === 'SEGMENT_REF') {
|
|
3909
3930
|
this.#progress.update({
|
|
3931
|
+
...NO_PAYLOAD,
|
|
3910
3932
|
phase: 'download',
|
|
3911
3933
|
unitId,
|
|
3912
3934
|
table: frame.table,
|
|
3913
3935
|
segmentId: frame.segmentId,
|
|
3914
|
-
bytesReceived: 0,
|
|
3915
3936
|
bytesTotal: frame.byteLength,
|
|
3916
|
-
rowsProcessed: 0,
|
|
3917
3937
|
rowsTotal: frame.rowCount,
|
|
3918
|
-
importWriteMs: undefined,
|
|
3919
|
-
importCommitMs: undefined,
|
|
3920
3938
|
});
|
|
3921
3939
|
prefetch();
|
|
3922
3940
|
const pending = prefetched.get(frame);
|
|
@@ -3941,16 +3959,13 @@ export class SyncClient {
|
|
|
3941
3959
|
const segment = image === undefined ? decodeRowsSegment(bytes) : undefined;
|
|
3942
3960
|
if (frame.type === 'SEGMENT_INLINE' && segment !== undefined)
|
|
3943
3961
|
this.#progress.update({
|
|
3962
|
+
...NO_PAYLOAD,
|
|
3944
3963
|
phase: 'import',
|
|
3945
3964
|
unitId,
|
|
3946
3965
|
table: segment.table,
|
|
3947
|
-
segmentId: undefined,
|
|
3948
3966
|
bytesReceived: bytes.byteLength,
|
|
3949
3967
|
bytesTotal: bytes.byteLength,
|
|
3950
|
-
rowsProcessed: 0,
|
|
3951
3968
|
rowsTotal: segment.blocks.reduce((n, block) => n + block.length, 0),
|
|
3952
|
-
importWriteMs: undefined,
|
|
3953
|
-
importCommitMs: undefined,
|
|
3954
3969
|
});
|
|
3955
3970
|
await this.#applySegmentOrFail(section, summary, image?.table ?? segment.table, (table, clearFirst, effective, owners) => {
|
|
3956
3971
|
const options = this.#segmentApplyOptions(table, clearFirst, effective, owners, image !== undefined
|
|
@@ -3971,6 +3986,12 @@ export class SyncClient {
|
|
|
3971
3986
|
this.#localResetEpoch === resetEpoch)
|
|
3972
3987
|
this.#finishSection(section, frame, summary);
|
|
3973
3988
|
section = undefined;
|
|
3989
|
+
// The counters stay: `complete` and `failed` report them.
|
|
3990
|
+
this.#progress.update({
|
|
3991
|
+
unitId: undefined,
|
|
3992
|
+
table: undefined,
|
|
3993
|
+
segmentId: undefined,
|
|
3994
|
+
});
|
|
3974
3995
|
break;
|
|
3975
3996
|
}
|
|
3976
3997
|
case 'LIVE_START': {
|
package/dist/native-bridge.d.ts
CHANGED
|
@@ -84,11 +84,6 @@ export declare class NativeSyncError extends Error {
|
|
|
84
84
|
* touched.
|
|
85
85
|
*/
|
|
86
86
|
export declare function nativeCreateParams(config: NativeCreateConfig, ErrorType: typeof NativeSyncError): Record<string, unknown>;
|
|
87
|
-
/**
|
|
88
|
-
* The `SyncClientLike` proxy over a native core. Every snapshot method
|
|
89
|
-
* returns a promise from one host round trip; React hooks accept the
|
|
90
|
-
* canonical snapshot interface directly.
|
|
91
|
-
*/
|
|
92
87
|
export declare abstract class NativeBridgeClient implements PromiseMethods<ClientSnapshotMethods & Pick<SyncClient, 'unit' | 'units' | 'unitCatchup'>> {
|
|
93
88
|
#private;
|
|
94
89
|
/**
|
|
@@ -106,8 +101,13 @@ export declare abstract class NativeBridgeClient implements PromiseMethods<Clien
|
|
|
106
101
|
protected abstract sendHeaders(headers: Readonly<Record<string, string>>): Promise<NativeReply>;
|
|
107
102
|
/** Stop the native core. `close()` detaches listeners once it settles. */
|
|
108
103
|
protected abstract shutdown(): Promise<void>;
|
|
109
|
-
/**
|
|
110
|
-
|
|
104
|
+
/**
|
|
105
|
+
* @internal Settle the native `create`; a failure detaches the listener.
|
|
106
|
+
* `host` is the page's native host: its one core serves one live bridge
|
|
107
|
+
* client, so a second `create` before `close()` settles is refused
|
|
108
|
+
* instead of replacing the first client's core.
|
|
109
|
+
*/
|
|
110
|
+
__open(create: () => Promise<NativeReply>, host: object): Promise<void>;
|
|
111
111
|
/** Dispatch a command through the closed check and the preflight gate. */
|
|
112
112
|
protected command(method: string, params: Record<string, unknown>): Promise<unknown>;
|
|
113
113
|
/** Additional preflight operations a host subclass permits. */
|
package/dist/native-bridge.js
CHANGED
|
@@ -171,6 +171,8 @@ export function nativeCreateParams(config, ErrorType) {
|
|
|
171
171
|
* returns a promise from one host round trip; React hooks accept the
|
|
172
172
|
* canonical snapshot interface directly.
|
|
173
173
|
*/
|
|
174
|
+
/** Native hosts whose one core a live bridge client of this page holds. */
|
|
175
|
+
const heldHosts = new WeakSet();
|
|
174
176
|
export class NativeBridgeClient {
|
|
175
177
|
#kind;
|
|
176
178
|
#Error;
|
|
@@ -182,6 +184,7 @@ export class NativeBridgeClient {
|
|
|
182
184
|
#presenceListeners = new Set();
|
|
183
185
|
#closed = false;
|
|
184
186
|
#closing;
|
|
187
|
+
#host;
|
|
185
188
|
#securityLifecycle;
|
|
186
189
|
#preflightBarrier;
|
|
187
190
|
/**
|
|
@@ -208,15 +211,27 @@ export class NativeBridgeClient {
|
|
|
208
211
|
}
|
|
209
212
|
return reply.result;
|
|
210
213
|
}
|
|
211
|
-
/**
|
|
212
|
-
|
|
214
|
+
/**
|
|
215
|
+
* @internal Settle the native `create`; a failure detaches the listener.
|
|
216
|
+
* `host` is the page's native host: its one core serves one live bridge
|
|
217
|
+
* client, so a second `create` before `close()` settles is refused
|
|
218
|
+
* instead of replacing the first client's core.
|
|
219
|
+
*/
|
|
220
|
+
async __open(create, host) {
|
|
221
|
+
if (heldHosts.has(host)) {
|
|
222
|
+
this.#release();
|
|
223
|
+
throw new this.#Error('client.native_client_exists', 'this page already holds the native client; close it before creating another');
|
|
224
|
+
}
|
|
225
|
+
heldHosts.add(host);
|
|
213
226
|
try {
|
|
214
227
|
await this.#reply(create);
|
|
215
228
|
}
|
|
216
229
|
catch (error) {
|
|
230
|
+
heldHosts.delete(host);
|
|
217
231
|
this.#release();
|
|
218
232
|
throw error;
|
|
219
233
|
}
|
|
234
|
+
this.#host = host;
|
|
220
235
|
}
|
|
221
236
|
/** Dispatch a command through the closed check and the preflight gate. */
|
|
222
237
|
async command(method, params) {
|
|
@@ -764,6 +779,8 @@ export class NativeBridgeClient {
|
|
|
764
779
|
const shutdown = this.shutdown();
|
|
765
780
|
this.#closed = true;
|
|
766
781
|
this.#closing = shutdown.finally(() => {
|
|
782
|
+
if (this.#host !== undefined)
|
|
783
|
+
heldHosts.delete(this.#host);
|
|
767
784
|
this.#release();
|
|
768
785
|
this.#invalidationListeners.clear();
|
|
769
786
|
this.#progress.clear();
|
package/dist/progress.d.ts
CHANGED
|
@@ -4,13 +4,13 @@ export interface SyncProgress {
|
|
|
4
4
|
readonly attempt: number;
|
|
5
5
|
readonly state: 'running' | 'complete' | 'failed';
|
|
6
6
|
readonly phase: 'request' | 'download' | 'import';
|
|
7
|
-
readonly unitId?: string;
|
|
8
|
-
readonly table?: string;
|
|
7
|
+
readonly unitId?: string | undefined;
|
|
8
|
+
readonly table?: string | undefined;
|
|
9
9
|
readonly segmentId?: string | undefined;
|
|
10
10
|
readonly bytesReceived: number;
|
|
11
|
-
readonly bytesTotal?: number;
|
|
11
|
+
readonly bytesTotal?: number | undefined;
|
|
12
12
|
readonly rowsProcessed: number;
|
|
13
|
-
readonly rowsTotal?: number;
|
|
13
|
+
readonly rowsTotal?: number | undefined;
|
|
14
14
|
/** Time in this payload's import transactions before COMMIT, in ms. */
|
|
15
15
|
readonly importWriteMs?: number | undefined;
|
|
16
16
|
/** Time committing them and publishing their changes, in ms. */
|
package/dist/reactive-store.d.ts
CHANGED
|
@@ -49,8 +49,12 @@ export interface ExternalStoreEntry<T> {
|
|
|
49
49
|
export interface WindowRetention {
|
|
50
50
|
/** Resolves once this retained claim has reached the core window. */
|
|
51
51
|
readonly ready: Promise<void>;
|
|
52
|
-
/**
|
|
53
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Release only this retention owner's units from the composable union.
|
|
54
|
+
* Settles with the core window edit that applies the release, after its
|
|
55
|
+
* eviction (§4.8); rejects with that edit's error.
|
|
56
|
+
*/
|
|
57
|
+
readonly release: () => Promise<void>;
|
|
54
58
|
}
|
|
55
59
|
export interface StatusStoreSnapshot {
|
|
56
60
|
readonly status: SyncStatusSnapshot | undefined;
|
|
@@ -87,7 +91,8 @@ export declare class ReactiveClientStore {
|
|
|
87
91
|
retainWindow(base: WindowBase, units: readonly string[]): WindowRetention;
|
|
88
92
|
window(base: WindowBase): ExternalStoreEntry<WindowState>;
|
|
89
93
|
setWindowClaim(owner: symbol, base: WindowBase, units: readonly string[]): Promise<void>;
|
|
90
|
-
|
|
94
|
+
/** Settles once every core window edit that drops the owner's claims ran. */
|
|
95
|
+
releaseWindowClaims(owner: symbol): Promise<void>;
|
|
91
96
|
/** Retained observation counts for diagnostics and resource benchmarks. */
|
|
92
97
|
cacheStats(): {
|
|
93
98
|
queries: number;
|
package/dist/reactive-store.js
CHANGED
|
@@ -329,7 +329,9 @@ class QueryEntry {
|
|
|
329
329
|
this.#offStatus = undefined;
|
|
330
330
|
this.#offRound?.();
|
|
331
331
|
this.#offRound = undefined;
|
|
332
|
-
|
|
332
|
+
// A failed release leaves the group unapplied; the next claim on
|
|
333
|
+
// the base applies it again. Only a retention handle reports it.
|
|
334
|
+
this.store.releaseWindowClaims(this.#owner).catch(() => { });
|
|
333
335
|
}
|
|
334
336
|
};
|
|
335
337
|
};
|
|
@@ -401,7 +403,7 @@ class QueryEntry {
|
|
|
401
403
|
this.#offStatus = undefined;
|
|
402
404
|
this.#offRound?.();
|
|
403
405
|
this.#offRound = undefined;
|
|
404
|
-
this.store.releaseWindowClaims(this.#owner);
|
|
406
|
+
this.store.releaseWindowClaims(this.#owner).catch(() => { });
|
|
405
407
|
this.reset();
|
|
406
408
|
}
|
|
407
409
|
/** SPEC §7.8: a rejected claim is claimed again; its stored error would
|
|
@@ -894,9 +896,9 @@ export class ReactiveClientStore {
|
|
|
894
896
|
ready,
|
|
895
897
|
release: () => {
|
|
896
898
|
if (!active)
|
|
897
|
-
return;
|
|
899
|
+
return Promise.resolve();
|
|
898
900
|
active = false;
|
|
899
|
-
this.releaseWindowClaims(owner);
|
|
901
|
+
return this.releaseWindowClaims(owner);
|
|
900
902
|
},
|
|
901
903
|
};
|
|
902
904
|
}
|
|
@@ -939,11 +941,18 @@ export class ReactiveClientStore {
|
|
|
939
941
|
this.#scheduleWindow(group);
|
|
940
942
|
return result;
|
|
941
943
|
}
|
|
944
|
+
/** Settles once every core window edit that drops the owner's claims ran. */
|
|
942
945
|
releaseWindowClaims(owner) {
|
|
946
|
+
const edits = [];
|
|
943
947
|
for (const group of this.#windowClaims.values()) {
|
|
944
|
-
if (group.claims.delete(owner))
|
|
945
|
-
|
|
948
|
+
if (!group.claims.delete(owner))
|
|
949
|
+
continue;
|
|
950
|
+
edits.push(new Promise((resolve, reject) => {
|
|
951
|
+
group.waiters.push({ resolve, reject });
|
|
952
|
+
}));
|
|
953
|
+
this.#scheduleWindow(group);
|
|
946
954
|
}
|
|
955
|
+
return Promise.all(edits).then(() => undefined);
|
|
947
956
|
}
|
|
948
957
|
/** Apply every claimed window group again and claim again for query entries
|
|
949
958
|
* whose claim was rejected. An applied group compares equal and makes no
|
package/dist/worker-host.d.ts
CHANGED
|
@@ -111,7 +111,10 @@ export interface SyncClientHandleConfig {
|
|
|
111
111
|
readonly previousVersionContext?: SyncClientConfig['previousVersionContext'];
|
|
112
112
|
/** Worker-side host loop (§8.4); default true. */
|
|
113
113
|
readonly autoSync?: boolean;
|
|
114
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Default: Web Locks. A runtime without Web Locks refuses the handle with
|
|
116
|
+
* `client.web_locks_unavailable` unless the host injects a lock.
|
|
117
|
+
*/
|
|
115
118
|
readonly leaderLock?: LeaderLock;
|
|
116
119
|
readonly lockName?: string;
|
|
117
120
|
/** Shared by default; isolated derives the database/lock/channel tuple. */
|
package/dist/worker-host.js
CHANGED
|
@@ -26,7 +26,7 @@ import { serializeBlobRef, } from '@syncular/core';
|
|
|
26
26
|
import { ClientDiagnosticsEmitter, withClientDiagnosticsHost, } from './diagnostics.js';
|
|
27
27
|
import { ClientSyncError, invalidRequest } from './errors.js';
|
|
28
28
|
import { ChangeEmitter, InvalidationEmitter, invalidationFromChange, } from './invalidation.js';
|
|
29
|
-
import {
|
|
29
|
+
import { webLocksLeaderLock, } from './leader-lock.js';
|
|
30
30
|
import { decodeLocalDataRebootstrapResult, } from './local-rebootstrap.js';
|
|
31
31
|
import { createTabCoordinator, multiTabChannelName, TAKEOVER_REFUSED_CODE, } from './multi-tab.js';
|
|
32
32
|
import { decodePreviousVersionAuditResult, decodePreviousVersionDiscardResult, decodePreviousVersionSnapshot, } from './previous-version.js';
|
|
@@ -56,9 +56,9 @@ export function isolatedReplicaNames(options) {
|
|
|
56
56
|
}
|
|
57
57
|
function defaultLeaderLock() {
|
|
58
58
|
const nav = globalThis.navigator;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
if (nav?.locks === undefined)
|
|
60
|
+
throw new ClientSyncError('client.web_locks_unavailable', 'this runtime has no Web Locks; inject a leaderLock');
|
|
61
|
+
return webLocksLeaderLock(nav.locks);
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* The main-thread proxy: the same logical API as `SyncClient`, every method a
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.68.0",
|
|
4
4
|
"description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -121,11 +121,11 @@
|
|
|
121
121
|
},
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"@sqlite.org/sqlite-wasm": "^3.53.0-build1",
|
|
124
|
-
"@syncular/core": "0.
|
|
124
|
+
"@syncular/core": "0.68.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.68.0",
|
|
129
129
|
"playwright": "1.63.0",
|
|
130
130
|
"vite": "8.3.4"
|
|
131
131
|
}
|
package/src/client.ts
CHANGED
|
@@ -910,6 +910,18 @@ export type ClientSnapshotReader = {
|
|
|
910
910
|
| Promise<ReturnType<ClientSnapshotMethods[Key]>>;
|
|
911
911
|
};
|
|
912
912
|
|
|
913
|
+
/** §7.7: progress fields of a payload; no payload is current. */
|
|
914
|
+
const NO_PAYLOAD: Partial<SyncProgress> = {
|
|
915
|
+
table: undefined,
|
|
916
|
+
segmentId: undefined,
|
|
917
|
+
bytesReceived: 0,
|
|
918
|
+
bytesTotal: undefined,
|
|
919
|
+
rowsProcessed: 0,
|
|
920
|
+
rowsTotal: undefined,
|
|
921
|
+
importWriteMs: undefined,
|
|
922
|
+
importCommitMs: undefined,
|
|
923
|
+
};
|
|
924
|
+
|
|
913
925
|
export class SyncClient {
|
|
914
926
|
readonly #config: SyncClientConfig;
|
|
915
927
|
readonly #authorityReads: AuthorityReadPolicy | undefined;
|
|
@@ -1276,21 +1288,17 @@ export class SyncClient {
|
|
|
1276
1288
|
throw classifySqliteFailure(error).error;
|
|
1277
1289
|
}
|
|
1278
1290
|
this.#started = true;
|
|
1291
|
+
// §4.8: an eviction a crash interrupted finishes before any read, with or
|
|
1292
|
+
// without a transport; a preflight replica finishes it at activation.
|
|
1293
|
+
if (this.#securityLifecycle === 'active')
|
|
1294
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
1279
1295
|
// A persisted active subscription needs one catch-up round on every open:
|
|
1280
1296
|
// realtime only covers changes after the socket connects, and an
|
|
1281
1297
|
// idempotent setWindow/subscribe call correctly creates no new command
|
|
1282
1298
|
// effect. Pending outbox work has the same restart requirement. Surface
|
|
1283
1299
|
// this as an exact core-owned intent so hosts never need a startup poll or
|
|
1284
1300
|
// 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') {
|
|
1301
|
+
if (this.#securityLifecycle === 'active' && this.#hasStartupWork()) {
|
|
1294
1302
|
this.#needsPull = true;
|
|
1295
1303
|
this.#emitSyncNeeded('startup');
|
|
1296
1304
|
this.#emitSyncIntent({ kind: 'interactive' });
|
|
@@ -1667,22 +1675,29 @@ export class SyncClient {
|
|
|
1667
1675
|
);
|
|
1668
1676
|
}
|
|
1669
1677
|
await (this.#preflightBarrier ?? this.#opChain);
|
|
1678
|
+
// §4.8: an interrupted eviction finishes before protected reads open.
|
|
1679
|
+
await this.#serialize(() => this.#drainPendingEvictions());
|
|
1670
1680
|
this.#encryption = options.encryption;
|
|
1671
1681
|
this.#securityLifecycle = 'active';
|
|
1672
|
-
|
|
1682
|
+
if (this.#hasStartupWork()) {
|
|
1683
|
+
this.#setSyncNeeded(true);
|
|
1684
|
+
this.#emitSyncNeeded('startup');
|
|
1685
|
+
this.#emitSyncIntent({ kind: 'interactive' });
|
|
1686
|
+
}
|
|
1687
|
+
this.#emitDiagnostics();
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
/** Persisted work an opened or activated replica owes one round (§8.4). */
|
|
1691
|
+
#hasStartupWork(): boolean {
|
|
1692
|
+
return (
|
|
1673
1693
|
this.#schemaFloor === undefined &&
|
|
1674
1694
|
(countOutbox(this.#db) > 0 ||
|
|
1675
1695
|
this.#db.query(
|
|
1676
1696
|
`SELECT 1 FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL LIMIT 1`,
|
|
1677
1697
|
).length > 0 ||
|
|
1678
1698
|
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();
|
|
1699
|
+
loadUnits(this.#db).some((unit) => unit.status === 'active'))
|
|
1700
|
+
);
|
|
1686
1701
|
}
|
|
1687
1702
|
|
|
1688
1703
|
// -- accessors ------------------------------------------------------------
|
|
@@ -5406,6 +5421,12 @@ export class SyncClient {
|
|
|
5406
5421
|
skip: false,
|
|
5407
5422
|
}
|
|
5408
5423
|
: undefined;
|
|
5424
|
+
// §7.7: a section boundary ends the previous payload.
|
|
5425
|
+
this.#progress.update({
|
|
5426
|
+
...NO_PAYLOAD,
|
|
5427
|
+
phase: 'import',
|
|
5428
|
+
unitId: section?.unit.id,
|
|
5429
|
+
});
|
|
5409
5430
|
break;
|
|
5410
5431
|
}
|
|
5411
5432
|
case 'SEGMENT_INLINE':
|
|
@@ -5424,16 +5445,13 @@ export class SyncClient {
|
|
|
5424
5445
|
let bytes: Uint8Array;
|
|
5425
5446
|
if (frame.type === 'SEGMENT_REF') {
|
|
5426
5447
|
this.#progress.update({
|
|
5448
|
+
...NO_PAYLOAD,
|
|
5427
5449
|
phase: 'download',
|
|
5428
5450
|
unitId,
|
|
5429
5451
|
table: frame.table,
|
|
5430
5452
|
segmentId: frame.segmentId,
|
|
5431
|
-
bytesReceived: 0,
|
|
5432
5453
|
bytesTotal: frame.byteLength,
|
|
5433
|
-
rowsProcessed: 0,
|
|
5434
5454
|
rowsTotal: frame.rowCount,
|
|
5435
|
-
importWriteMs: undefined,
|
|
5436
|
-
importCommitMs: undefined,
|
|
5437
5455
|
});
|
|
5438
5456
|
prefetch();
|
|
5439
5457
|
const pending = prefetched.get(frame);
|
|
@@ -5467,19 +5485,16 @@ export class SyncClient {
|
|
|
5467
5485
|
image === undefined ? decodeRowsSegment(bytes) : undefined;
|
|
5468
5486
|
if (frame.type === 'SEGMENT_INLINE' && segment !== undefined)
|
|
5469
5487
|
this.#progress.update({
|
|
5488
|
+
...NO_PAYLOAD,
|
|
5470
5489
|
phase: 'import',
|
|
5471
5490
|
unitId,
|
|
5472
5491
|
table: segment.table,
|
|
5473
|
-
segmentId: undefined,
|
|
5474
5492
|
bytesReceived: bytes.byteLength,
|
|
5475
5493
|
bytesTotal: bytes.byteLength,
|
|
5476
|
-
rowsProcessed: 0,
|
|
5477
5494
|
rowsTotal: segment.blocks.reduce(
|
|
5478
5495
|
(n, block) => n + block.length,
|
|
5479
5496
|
0,
|
|
5480
5497
|
),
|
|
5481
|
-
importWriteMs: undefined,
|
|
5482
|
-
importCommitMs: undefined,
|
|
5483
5498
|
});
|
|
5484
5499
|
await this.#applySegmentOrFail(
|
|
5485
5500
|
section,
|
|
@@ -5529,6 +5544,12 @@ export class SyncClient {
|
|
|
5529
5544
|
)
|
|
5530
5545
|
this.#finishSection(section, frame, summary);
|
|
5531
5546
|
section = undefined;
|
|
5547
|
+
// The counters stay: `complete` and `failed` report them.
|
|
5548
|
+
this.#progress.update({
|
|
5549
|
+
unitId: undefined,
|
|
5550
|
+
table: undefined,
|
|
5551
|
+
segmentId: undefined,
|
|
5552
|
+
});
|
|
5532
5553
|
break;
|
|
5533
5554
|
}
|
|
5534
5555
|
case 'LIVE_START': {
|
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. */
|
|
@@ -1163,6 +1185,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1163
1185
|
const shutdown = this.shutdown();
|
|
1164
1186
|
this.#closed = true;
|
|
1165
1187
|
this.#closing = shutdown.finally(() => {
|
|
1188
|
+
if (this.#host !== undefined) heldHosts.delete(this.#host);
|
|
1166
1189
|
this.#release();
|
|
1167
1190
|
this.#invalidationListeners.clear();
|
|
1168
1191
|
this.#progress.clear();
|
package/src/progress.ts
CHANGED
|
@@ -5,13 +5,13 @@ export interface SyncProgress {
|
|
|
5
5
|
readonly attempt: number;
|
|
6
6
|
readonly state: 'running' | 'complete' | 'failed';
|
|
7
7
|
readonly phase: 'request' | 'download' | 'import';
|
|
8
|
-
readonly unitId?: string;
|
|
9
|
-
readonly table?: string;
|
|
8
|
+
readonly unitId?: string | undefined;
|
|
9
|
+
readonly table?: string | undefined;
|
|
10
10
|
readonly segmentId?: string | undefined;
|
|
11
11
|
readonly bytesReceived: number;
|
|
12
|
-
readonly bytesTotal?: number;
|
|
12
|
+
readonly bytesTotal?: number | undefined;
|
|
13
13
|
readonly rowsProcessed: number;
|
|
14
|
-
readonly rowsTotal?: number;
|
|
14
|
+
readonly rowsTotal?: number | undefined;
|
|
15
15
|
/** Time in this payload's import transactions before COMMIT, in ms. */
|
|
16
16
|
readonly importWriteMs?: number | undefined;
|
|
17
17
|
/** Time committing them and publishing their changes, in ms. */
|
package/src/reactive-store.ts
CHANGED
|
@@ -81,8 +81,12 @@ export interface ExternalStoreEntry<T> {
|
|
|
81
81
|
export interface WindowRetention {
|
|
82
82
|
/** Resolves once this retained claim has reached the core window. */
|
|
83
83
|
readonly ready: Promise<void>;
|
|
84
|
-
/**
|
|
85
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Release only this retention owner's units from the composable union.
|
|
86
|
+
* Settles with the core window edit that applies the release, after its
|
|
87
|
+
* eviction (§4.8); rejects with that edit's error.
|
|
88
|
+
*/
|
|
89
|
+
readonly release: () => Promise<void>;
|
|
86
90
|
}
|
|
87
91
|
|
|
88
92
|
export interface StatusStoreSnapshot {
|
|
@@ -465,7 +469,9 @@ class QueryEntry<Row> implements ExternalStoreEntry<LiveQueryResult<Row>> {
|
|
|
465
469
|
this.#offStatus = undefined;
|
|
466
470
|
this.#offRound?.();
|
|
467
471
|
this.#offRound = undefined;
|
|
468
|
-
|
|
472
|
+
// A failed release leaves the group unapplied; the next claim on
|
|
473
|
+
// the base applies it again. Only a retention handle reports it.
|
|
474
|
+
this.store.releaseWindowClaims(this.#owner).catch(() => {});
|
|
469
475
|
}
|
|
470
476
|
};
|
|
471
477
|
};
|
|
@@ -541,7 +547,7 @@ class QueryEntry<Row> implements ExternalStoreEntry<LiveQueryResult<Row>> {
|
|
|
541
547
|
this.#offStatus = undefined;
|
|
542
548
|
this.#offRound?.();
|
|
543
549
|
this.#offRound = undefined;
|
|
544
|
-
this.store.releaseWindowClaims(this.#owner);
|
|
550
|
+
this.store.releaseWindowClaims(this.#owner).catch(() => {});
|
|
545
551
|
this.reset();
|
|
546
552
|
}
|
|
547
553
|
|
|
@@ -1069,9 +1075,9 @@ export class ReactiveClientStore {
|
|
|
1069
1075
|
return {
|
|
1070
1076
|
ready,
|
|
1071
1077
|
release: () => {
|
|
1072
|
-
if (!active) return;
|
|
1078
|
+
if (!active) return Promise.resolve();
|
|
1073
1079
|
active = false;
|
|
1074
|
-
this.releaseWindowClaims(owner);
|
|
1080
|
+
return this.releaseWindowClaims(owner);
|
|
1075
1081
|
},
|
|
1076
1082
|
};
|
|
1077
1083
|
}
|
|
@@ -1125,10 +1131,19 @@ export class ReactiveClientStore {
|
|
|
1125
1131
|
return result;
|
|
1126
1132
|
}
|
|
1127
1133
|
|
|
1128
|
-
|
|
1134
|
+
/** Settles once every core window edit that drops the owner's claims ran. */
|
|
1135
|
+
releaseWindowClaims(owner: symbol): Promise<void> {
|
|
1136
|
+
const edits: Promise<void>[] = [];
|
|
1129
1137
|
for (const group of this.#windowClaims.values()) {
|
|
1130
|
-
if (group.claims.delete(owner))
|
|
1138
|
+
if (!group.claims.delete(owner)) continue;
|
|
1139
|
+
edits.push(
|
|
1140
|
+
new Promise<void>((resolve, reject) => {
|
|
1141
|
+
group.waiters.push({ resolve, reject });
|
|
1142
|
+
}),
|
|
1143
|
+
);
|
|
1144
|
+
this.#scheduleWindow(group);
|
|
1131
1145
|
}
|
|
1146
|
+
return Promise.all(edits).then(() => undefined);
|
|
1132
1147
|
}
|
|
1133
1148
|
|
|
1134
1149
|
/** Apply every claimed window group again and claim again for query entries
|
package/src/worker-host.ts
CHANGED
|
@@ -74,7 +74,6 @@ import {
|
|
|
74
74
|
import {
|
|
75
75
|
type LeaderLease,
|
|
76
76
|
type LeaderLock,
|
|
77
|
-
singleOwnerLock,
|
|
78
77
|
webLocksLeaderLock,
|
|
79
78
|
} from './leader-lock';
|
|
80
79
|
import type { LocalDataPurgeInput, LocalDataPurgeResult } from './local-purge';
|
|
@@ -224,7 +223,10 @@ export interface SyncClientHandleConfig {
|
|
|
224
223
|
readonly previousVersionContext?: SyncClientConfig['previousVersionContext'];
|
|
225
224
|
/** Worker-side host loop (§8.4); default true. */
|
|
226
225
|
readonly autoSync?: boolean;
|
|
227
|
-
/**
|
|
226
|
+
/**
|
|
227
|
+
* Default: Web Locks. A runtime without Web Locks refuses the handle with
|
|
228
|
+
* `client.web_locks_unavailable` unless the host injects a lock.
|
|
229
|
+
*/
|
|
228
230
|
readonly leaderLock?: LeaderLock;
|
|
229
231
|
readonly lockName?: string;
|
|
230
232
|
/** Shared by default; isolated derives the database/lock/channel tuple. */
|
|
@@ -287,9 +289,12 @@ interface Pending {
|
|
|
287
289
|
|
|
288
290
|
function defaultLeaderLock(): LeaderLock {
|
|
289
291
|
const nav = (globalThis as { navigator?: { locks?: LockManager } }).navigator;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
292
|
+
if (nav?.locks === undefined)
|
|
293
|
+
throw new ClientSyncError(
|
|
294
|
+
'client.web_locks_unavailable',
|
|
295
|
+
'this runtime has no Web Locks; inject a leaderLock',
|
|
296
|
+
);
|
|
297
|
+
return webLocksLeaderLock(nav.locks);
|
|
293
298
|
}
|
|
294
299
|
|
|
295
300
|
/**
|