@syncular/client 0.91.0 → 0.93.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 +3 -1
- package/dist/apply.d.ts +14 -4
- package/dist/apply.js +24 -26
- package/dist/client.js +296 -259
- package/dist/encryption.js +5 -7
- package/dist/errors.js +4 -4
- package/dist/http.js +9 -8
- package/dist/multi-tab.d.ts +3 -2
- package/dist/multi-tab.js +3 -2
- package/dist/reactive-store.d.ts +2 -0
- package/dist/reactive-store.js +71 -3
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +8 -3
- package/dist/worker-core.d.ts +0 -15
- package/dist/worker-core.js +36 -41
- package/package.json +3 -3
- package/src/apply.ts +50 -34
- package/src/client.ts +345 -327
- package/src/encryption.ts +5 -6
- package/src/errors.ts +4 -4
- package/src/http.ts +9 -15
- package/src/multi-tab.ts +3 -2
- package/src/reactive-store.ts +76 -3
- package/src/schema.ts +2 -4
- package/src/worker-core.ts +26 -42
package/dist/encryption.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* bridge between the positional `RowValue[]` and the `@syncular/core` §5.11
|
|
9
9
|
* envelope primitives.
|
|
10
10
|
*/
|
|
11
|
-
import { DecryptError, decryptValue, EncryptError, encryptValue, KEY_LENGTH, } from '@syncular/core';
|
|
11
|
+
import { DecryptError, decryptValue, EncryptError, encryptValue, KEY_LENGTH, definedObject, } from '@syncular/core';
|
|
12
12
|
import { invalidRequest } from './errors.js';
|
|
13
13
|
/**
|
|
14
14
|
* Convert a portable keyring into the direct-client encryption contract.
|
|
@@ -27,12 +27,10 @@ export function encryptionConfigFromKeyring(keyring) {
|
|
|
27
27
|
// a prototype-chain name like "constructor" must read as a missing key
|
|
28
28
|
// and take the clean DecryptError path.
|
|
29
29
|
keyProvider: (keyId) => Object.hasOwn(keyring.keys, keyId) ? keyring.keys[keyId] : undefined,
|
|
30
|
-
...(
|
|
31
|
-
|
|
32
|
-
:
|
|
33
|
-
|
|
34
|
-
? { missingKey: keyring.missingKey }
|
|
35
|
-
: {}),
|
|
30
|
+
...definedObject({
|
|
31
|
+
keyIdColumns: keyring.keyIdColumns,
|
|
32
|
+
missingKey: keyring.missingKey,
|
|
33
|
+
}),
|
|
36
34
|
};
|
|
37
35
|
}
|
|
38
36
|
function encryptionKeyId(config, table, rowId, values) {
|
package/dist/errors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DecryptError } from '@syncular/core';
|
|
1
|
+
import { DecryptError, definedObject } from '@syncular/core';
|
|
2
2
|
import { WORKER_FAILED_CODE } from './worker-protocol.js';
|
|
3
3
|
/** A persistent local store is temporarily owned by another live engine. */
|
|
4
4
|
export const STORAGE_BUSY_CODE = 'client.storage_busy';
|
|
@@ -192,12 +192,12 @@ export function toErrorShape(caught) {
|
|
|
192
192
|
const details = error instanceof ClientSyncError || error instanceof DecryptError
|
|
193
193
|
? error.details
|
|
194
194
|
: undefined;
|
|
195
|
-
return {
|
|
195
|
+
return definedObject({
|
|
196
196
|
code: error.code,
|
|
197
197
|
message: error.message,
|
|
198
198
|
retryable: 'retryable' in error && error.retryable === true,
|
|
199
|
-
|
|
200
|
-
};
|
|
199
|
+
details,
|
|
200
|
+
});
|
|
201
201
|
}
|
|
202
202
|
return {
|
|
203
203
|
code: WORKER_FAILED_CODE,
|
package/dist/http.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { definedObject } from '@syncular/core';
|
|
1
2
|
import { SSP2_CONTENT_TYPE } from './content-type.js';
|
|
2
3
|
import { ClientSyncError, transportFailed } from './errors.js';
|
|
3
4
|
import { hexToBytes } from './schema.js';
|
|
@@ -19,7 +20,7 @@ async function transfer(message, operation, httpStatus) {
|
|
|
19
20
|
: 'unknown';
|
|
20
21
|
throw transportFailed(message, {
|
|
21
22
|
causeKind,
|
|
22
|
-
...(
|
|
23
|
+
...definedObject({ httpStatus }),
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
26
|
}
|
|
@@ -73,7 +74,7 @@ export function httpSyncTransport(syncUrl, options) {
|
|
|
73
74
|
...hostHeaders(options),
|
|
74
75
|
},
|
|
75
76
|
body: request.slice().buffer,
|
|
76
|
-
...(
|
|
77
|
+
...definedObject({ signal }),
|
|
77
78
|
}));
|
|
78
79
|
if (!response.ok)
|
|
79
80
|
await throwHttpError(response);
|
|
@@ -211,10 +212,10 @@ export function httpSegmentDownloader(segmentsBaseUrl, options) {
|
|
|
211
212
|
'X-Syncular-Scopes': request.requestedScopesJson,
|
|
212
213
|
...hostHeaders(options),
|
|
213
214
|
},
|
|
214
|
-
...(
|
|
215
|
+
...definedObject({ signal: request.signal }),
|
|
215
216
|
}, request.onProgress, false, request.byteLength);
|
|
216
217
|
// Deliberately headerless: the URL is the bearer grant (§5.4).
|
|
217
|
-
const fetchUrl = (url, onProgress, signal, byteLength) => download(url, signal
|
|
218
|
+
const fetchUrl = (url, onProgress, signal, byteLength) => download(url, signal && { signal }, onProgress, true, byteLength);
|
|
218
219
|
return Object.assign(direct, { fetchUrl });
|
|
219
220
|
}
|
|
220
221
|
/**
|
|
@@ -235,7 +236,7 @@ export function httpBlobTransport(blobsBaseUrl, options) {
|
|
|
235
236
|
...hostHeaders(options),
|
|
236
237
|
},
|
|
237
238
|
body: bytes.slice().buffer,
|
|
238
|
-
...(
|
|
239
|
+
...definedObject({ signal }),
|
|
239
240
|
}));
|
|
240
241
|
if (!response.ok)
|
|
241
242
|
await throwHttpError(response);
|
|
@@ -291,9 +292,9 @@ export function httpBlobTransport(blobsBaseUrl, options) {
|
|
|
291
292
|
},
|
|
292
293
|
body: JSON.stringify({
|
|
293
294
|
byteLength,
|
|
294
|
-
|
|
295
|
+
mediaType,
|
|
295
296
|
}),
|
|
296
|
-
...(
|
|
297
|
+
...definedObject({ signal }),
|
|
297
298
|
}));
|
|
298
299
|
if (!response.ok)
|
|
299
300
|
await throwHttpError(response);
|
|
@@ -319,7 +320,7 @@ export function httpBlobTransport(blobsBaseUrl, options) {
|
|
|
319
320
|
'x-amz-checksum-sha256': checksum,
|
|
320
321
|
},
|
|
321
322
|
body,
|
|
322
|
-
...(
|
|
323
|
+
...definedObject({ signal }),
|
|
323
324
|
}));
|
|
324
325
|
if (!response.ok) {
|
|
325
326
|
throw transportFailed('signed blob upload failed', {
|
package/dist/multi-tab.d.ts
CHANGED
|
@@ -86,9 +86,10 @@ export declare const TAKEOVER_REFUSED_CODE = "client.takeover_refused";
|
|
|
86
86
|
/**
|
|
87
87
|
* Version of the leader/follower wire and the forwarded handle API. Bump it
|
|
88
88
|
* with every change to a message shape or to a forwarded method's arguments
|
|
89
|
-
* or result, so tabs of different builds never serve each other
|
|
89
|
+
* or result, so tabs of different builds never serve each other;
|
|
90
|
+
* test/multi-tab-protocol.test.ts pins the surface's fingerprint to it.
|
|
90
91
|
*/
|
|
91
|
-
export declare const MULTI_TAB_PROTOCOL_VERSION =
|
|
92
|
+
export declare const MULTI_TAB_PROTOCOL_VERSION = 4;
|
|
92
93
|
/** What a tab must share with its leader to be served by it. */
|
|
93
94
|
export interface TabIdentity {
|
|
94
95
|
readonly protocol: number;
|
package/dist/multi-tab.js
CHANGED
|
@@ -86,9 +86,10 @@ export const TAKEOVER_REFUSED_CODE = 'client.takeover_refused';
|
|
|
86
86
|
/**
|
|
87
87
|
* Version of the leader/follower wire and the forwarded handle API. Bump it
|
|
88
88
|
* with every change to a message shape or to a forwarded method's arguments
|
|
89
|
-
* or result, so tabs of different builds never serve each other
|
|
89
|
+
* or result, so tabs of different builds never serve each other;
|
|
90
|
+
* test/multi-tab-protocol.test.ts pins the surface's fingerprint to it.
|
|
90
91
|
*/
|
|
91
|
-
export const MULTI_TAB_PROTOCOL_VERSION =
|
|
92
|
+
export const MULTI_TAB_PROTOCOL_VERSION = 4;
|
|
92
93
|
/**
|
|
93
94
|
* Order a peer's identity against our own: -1 older, 0 same, 1 newer. A peer
|
|
94
95
|
* from before protocol 1 sends no identity and is older.
|
package/dist/reactive-store.d.ts
CHANGED
|
@@ -86,6 +86,8 @@ export declare class ReactiveClientStore {
|
|
|
86
86
|
readonly outcomes: ExternalStoreEntry<OutcomeStoreSnapshot>;
|
|
87
87
|
/** The latest sync attempt's failure until the next attempt starts. */
|
|
88
88
|
readonly roundFailure: ExternalStoreEntry<SyncRoundFailedError | undefined>;
|
|
89
|
+
/** SPEC §7.8: a running sync attempt downloads or imports a response. */
|
|
90
|
+
get importing(): boolean;
|
|
89
91
|
constructor(client: ReactiveQueryClient);
|
|
90
92
|
query<Row>(spec: ReactiveQuerySpec<Row>): ExternalStoreEntry<LiveQueryResult<Row>>;
|
|
91
93
|
availabilitySnapshot(): SyncAvailability;
|
package/dist/reactive-store.js
CHANGED
|
@@ -241,6 +241,10 @@ class ObservationCache {
|
|
|
241
241
|
for (const entry of this.#active)
|
|
242
242
|
entry.reread();
|
|
243
243
|
}
|
|
244
|
+
imported() {
|
|
245
|
+
for (const entry of this.#active)
|
|
246
|
+
entry.imported?.();
|
|
247
|
+
}
|
|
244
248
|
clear() {
|
|
245
249
|
for (const entry of this.#entries.values())
|
|
246
250
|
entry.dispose();
|
|
@@ -299,6 +303,14 @@ class QueryEntry {
|
|
|
299
303
|
#running = false;
|
|
300
304
|
#requested = false;
|
|
301
305
|
#desiredRevision = 0n;
|
|
306
|
+
/** SPEC §7.8: the newest matching revision an incomplete answer may not lag. */
|
|
307
|
+
#currentRevision = 0n;
|
|
308
|
+
#complete = false;
|
|
309
|
+
/** Incomplete units of the last answer no window change has named since. */
|
|
310
|
+
#waiting = new Set();
|
|
311
|
+
/** Units window changes named since the running read started. */
|
|
312
|
+
#named = new Set();
|
|
313
|
+
#deferred = false;
|
|
302
314
|
#claimError;
|
|
303
315
|
#claimPending = false;
|
|
304
316
|
#claimGated = false;
|
|
@@ -405,6 +417,11 @@ class QueryEntry {
|
|
|
405
417
|
this.#running = false;
|
|
406
418
|
this.#requested = false;
|
|
407
419
|
this.#desiredRevision = 0n;
|
|
420
|
+
this.#currentRevision = 0n;
|
|
421
|
+
this.#complete = false;
|
|
422
|
+
this.#waiting = new Set();
|
|
423
|
+
this.#named = new Set();
|
|
424
|
+
this.#deferred = false;
|
|
408
425
|
this.#claimError = undefined;
|
|
409
426
|
this.#claimPending = false;
|
|
410
427
|
this.#claimGated = false;
|
|
@@ -437,12 +454,32 @@ class QueryEntry {
|
|
|
437
454
|
else
|
|
438
455
|
this.#requestRead();
|
|
439
456
|
}
|
|
457
|
+
imported() {
|
|
458
|
+
if (!this.#deferred)
|
|
459
|
+
return;
|
|
460
|
+
this.#deferred = false;
|
|
461
|
+
this.#requestRead();
|
|
462
|
+
}
|
|
440
463
|
onChange(batch) {
|
|
441
464
|
if (!batchMatches(batch, this.spec))
|
|
442
465
|
return;
|
|
443
466
|
if (batch.revision > this.#desiredRevision) {
|
|
444
467
|
this.#desiredRevision = batch.revision;
|
|
445
468
|
}
|
|
469
|
+
// SPEC §7.8: while an attempt imports, an incomplete answer waits for
|
|
470
|
+
// the import unless the batch carries status (a local write) or names
|
|
471
|
+
// the last of its incomplete units.
|
|
472
|
+
for (const change of batch.windows)
|
|
473
|
+
for (const unit of change.units) {
|
|
474
|
+
this.#waiting.delete(`${change.baseKey}\0${unit}`);
|
|
475
|
+
this.#named.add(`${change.baseKey}\0${unit}`);
|
|
476
|
+
}
|
|
477
|
+
const lag = this.store.importing &&
|
|
478
|
+
!this.#complete &&
|
|
479
|
+
batch.status === undefined &&
|
|
480
|
+
(batch.windows.length === 0 || this.#waiting.size > 0);
|
|
481
|
+
if (!lag && batch.revision > this.#currentRevision)
|
|
482
|
+
this.#currentRevision = batch.revision;
|
|
446
483
|
if (this.#listeners.size === 0)
|
|
447
484
|
return;
|
|
448
485
|
// A committed row change of a dependency claims again; a window-only
|
|
@@ -450,7 +487,10 @@ class QueryEntry {
|
|
|
450
487
|
const rowChange = batch.tables.some((change) => this.spec.dependencies.some((dependency) => dependency.table === change.table));
|
|
451
488
|
if (rowChange && this.#claimError !== undefined)
|
|
452
489
|
this.reclaim();
|
|
453
|
-
else
|
|
490
|
+
else if (lag)
|
|
491
|
+
this.#deferred ||= !this.#running;
|
|
492
|
+
// A running read reads again only when its snapshot predates the batch.
|
|
493
|
+
else if (!this.#running)
|
|
454
494
|
this.#requestRead();
|
|
455
495
|
}
|
|
456
496
|
#publish(next) {
|
|
@@ -582,9 +622,13 @@ class QueryEntry {
|
|
|
582
622
|
return;
|
|
583
623
|
const generation = this.#generation;
|
|
584
624
|
this.#running = true;
|
|
625
|
+
let asked = this.#desiredRevision;
|
|
585
626
|
try {
|
|
586
627
|
do {
|
|
587
628
|
this.#requested = false;
|
|
629
|
+
this.#deferred = false;
|
|
630
|
+
this.#named.clear();
|
|
631
|
+
asked = this.#desiredRevision;
|
|
588
632
|
if (localReadsBlocked(this.store.availabilitySnapshot()))
|
|
589
633
|
break;
|
|
590
634
|
if (generation !== this.#generation || this.#listeners.size === 0)
|
|
@@ -620,9 +664,18 @@ class QueryEntry {
|
|
|
620
664
|
break;
|
|
621
665
|
}
|
|
622
666
|
if (snapshot.revision < this.#desiredRevision) {
|
|
623
|
-
|
|
624
|
-
|
|
667
|
+
if (snapshot.coverage.complete ||
|
|
668
|
+
snapshot.revision < this.#currentRevision ||
|
|
669
|
+
!this.store.importing) {
|
|
670
|
+
this.#requested = true;
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
this.#deferred = true;
|
|
625
674
|
}
|
|
675
|
+
this.#complete = snapshot.coverage.complete;
|
|
676
|
+
this.#waiting = new Set([...snapshot.coverage.pending, ...snapshot.coverage.missing]
|
|
677
|
+
.map((ref) => `${ref.baseKey}\0${ref.unit}`)
|
|
678
|
+
.filter((key) => !this.#named.has(key)));
|
|
626
679
|
if (!snapshot.coverage.complete &&
|
|
627
680
|
claimWasPending &&
|
|
628
681
|
!this.#claimPending)
|
|
@@ -659,6 +712,9 @@ class QueryEntry {
|
|
|
659
712
|
this.#gate();
|
|
660
713
|
return;
|
|
661
714
|
}
|
|
715
|
+
// A batch that arrived during the failed read reads again.
|
|
716
|
+
if (this.#desiredRevision > asked)
|
|
717
|
+
this.#requested = true;
|
|
662
718
|
// SPEC §7.5: a failed latest read is never `ready`; the last
|
|
663
719
|
// successful rows and revision stay available beside the error.
|
|
664
720
|
this.#publish({
|
|
@@ -847,7 +903,12 @@ export class ReactiveClientStore {
|
|
|
847
903
|
outcomes;
|
|
848
904
|
/** The latest sync attempt's failure until the next attempt starts. */
|
|
849
905
|
roundFailure;
|
|
906
|
+
#importing = false;
|
|
850
907
|
#offProgress;
|
|
908
|
+
/** SPEC §7.8: a running sync attempt downloads or imports a response. */
|
|
909
|
+
get importing() {
|
|
910
|
+
return this.#importing;
|
|
911
|
+
}
|
|
851
912
|
constructor(client) {
|
|
852
913
|
this.client = client;
|
|
853
914
|
const status = new ValueEntry({
|
|
@@ -1137,6 +1198,12 @@ export class ReactiveClientStore {
|
|
|
1137
1198
|
const roundFailure = this.roundFailure;
|
|
1138
1199
|
let claimedAttempt;
|
|
1139
1200
|
this.#offProgress = this.client.onProgress((progress) => {
|
|
1201
|
+
const importing = progress.state === 'running' && progress.phase !== 'request';
|
|
1202
|
+
if (this.#importing !== importing) {
|
|
1203
|
+
this.#importing = importing;
|
|
1204
|
+
if (!importing)
|
|
1205
|
+
this.#queries.imported();
|
|
1206
|
+
}
|
|
1140
1207
|
// A new sync attempt is the client's retry schedule (§7.7): window
|
|
1141
1208
|
// claims that failed are claimed again once per attempt.
|
|
1142
1209
|
if (progress.state === 'running' && progress.attempt !== claimedAttempt) {
|
|
@@ -1188,6 +1255,7 @@ export class ReactiveClientStore {
|
|
|
1188
1255
|
this.#offLeadership = undefined;
|
|
1189
1256
|
this.#offProgress?.();
|
|
1190
1257
|
this.#offProgress = undefined;
|
|
1258
|
+
this.#importing = false;
|
|
1191
1259
|
this.roundFailure.set(undefined);
|
|
1192
1260
|
for (const group of this.#windowClaims.values()) {
|
|
1193
1261
|
for (const waiter of group.waiters.splice(0)) {
|
package/dist/schema.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* DDL, the generated row codec, mutation helpers, and the §3.3 purge
|
|
5
5
|
* mapping (scope variable → local column).
|
|
6
6
|
*/
|
|
7
|
-
import type
|
|
7
|
+
import { type RowColumn, type RowValue } from '@syncular/core';
|
|
8
8
|
import type { ClientDatabase, SqlRow, SqlValue } from './database.js';
|
|
9
9
|
/** `'prefix:{variable}'` shorthand (column name = variable) or explicit. */
|
|
10
10
|
export type ScopePatternSpec = string | {
|
package/dist/schema.js
CHANGED
|
@@ -5,6 +5,13 @@ import { OUTBOX_TABLE, OUTBOX_IMAGES } from './outbox.js';
|
|
|
5
5
|
import { OUTCOMES_TABLE } from './outcomes.js';
|
|
6
6
|
import { META_TABLE, pruneUnknownUnits, resetUnits, UNITS_TABLE, } from './state.js';
|
|
7
7
|
import { ensureFailedOverlaySchema, RETAINED_ROWS } from './failed-overlay.js';
|
|
8
|
+
/**
|
|
9
|
+
* Client schema IR (SPEC.md §2.4, §3.1) — the same shape the server
|
|
10
|
+
* compiles and is emitted by codegen. Drives local table
|
|
11
|
+
* DDL, the generated row codec, mutation helpers, and the §3.3 purge
|
|
12
|
+
* mapping (scope variable → local column).
|
|
13
|
+
*/
|
|
14
|
+
import { definedObject } from '@syncular/core';
|
|
8
15
|
import { invalidRequest, localStateCorrupt } from './errors.js';
|
|
9
16
|
import { snakeToCamel } from './naming.js';
|
|
10
17
|
const PATTERN_RE = /^([^{}]+):\{([^{}:]+)\}$/;
|
|
@@ -127,9 +134,7 @@ export function compileClientSchema(schema) {
|
|
|
127
134
|
}
|
|
128
135
|
tables.set(table.name, {
|
|
129
136
|
name: table.name,
|
|
130
|
-
...(table.generation
|
|
131
|
-
? { generation: table.generation }
|
|
132
|
-
: {}),
|
|
137
|
+
...definedObject({ generation: table.generation }),
|
|
133
138
|
columns: table.columns,
|
|
134
139
|
primaryKey: table.primaryKey,
|
|
135
140
|
primaryKeyIndex,
|
package/dist/worker-core.d.ts
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Worker-side bootstrap: constructs the WHOLE
|
|
3
|
-
* client core — SyncClient + transports + realtime + SQLite — inside the
|
|
4
|
-
* worker and serves the `worker-protocol` RPC over postMessage.
|
|
5
|
-
*
|
|
6
|
-
* Browsers get the defaults: opfs-sahpool persistence and the fetch/WS
|
|
7
|
-
* transports from `./http`, wired from `WorkerInitConfig.endpoints`.
|
|
8
|
-
* Tests inject a database factory (bun:sqlite) through `overrides` — the
|
|
9
|
-
* same bootstrap, the same RPC, a different SQLite.
|
|
10
|
-
*
|
|
11
|
-
* The §8.4 host loop lives HERE: wake-ups (hello/`sync` events, delta
|
|
12
|
-
* drops) coalesce into one intent-driven `syncUntilIdle` round in the worker;
|
|
13
|
-
* RPC-driven and auto-driven sync rounds serialize on one queue because
|
|
14
|
-
* the core owns exactly one loop.
|
|
15
|
-
*/
|
|
16
1
|
import type { defineAuthorityReads } from './authority.js';
|
|
17
2
|
import { SyncClient } from './client.js';
|
|
18
3
|
import type { ClientDatabase } from './database.js';
|
package/dist/worker-core.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker-side bootstrap: constructs the WHOLE
|
|
3
|
+
* client core — SyncClient + transports + realtime + SQLite — inside the
|
|
4
|
+
* worker and serves the `worker-protocol` RPC over postMessage.
|
|
5
|
+
*
|
|
6
|
+
* Browsers get the defaults: opfs-sahpool persistence and the fetch/WS
|
|
7
|
+
* transports from `./http`, wired from `WorkerInitConfig.endpoints`.
|
|
8
|
+
* Tests inject a database factory (bun:sqlite) through `overrides` — the
|
|
9
|
+
* same bootstrap, the same RPC, a different SQLite.
|
|
10
|
+
*
|
|
11
|
+
* The §8.4 host loop lives HERE: wake-ups (hello/`sync` events, delta
|
|
12
|
+
* drops) coalesce into one intent-driven `syncUntilIdle` round in the worker;
|
|
13
|
+
* RPC-driven and auto-driven sync rounds serialize on one queue because
|
|
14
|
+
* the core owns exactly one loop.
|
|
15
|
+
*/
|
|
16
|
+
import { definedObject } from '@syncular/core';
|
|
1
17
|
import { SyncClient } from './client.js';
|
|
2
18
|
import { withClientDiagnosticsHost, } from './diagnostics.js';
|
|
3
19
|
import { encryptionConfigFromKeyring } from './encryption.js';
|
|
@@ -219,12 +235,8 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
219
235
|
// Retain the leader lease while the previous worker's OPFS handles
|
|
220
236
|
// drain. Only opening is retried; core initialization has not started.
|
|
221
237
|
const delayMs = Math.min(50 * 2 ** attempt, 1000);
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
227
|
-
}
|
|
238
|
+
await (overrides.waitForStorageRetry?.(delayMs) ??
|
|
239
|
+
new Promise((resolve) => setTimeout(resolve, delayMs)));
|
|
228
240
|
}
|
|
229
241
|
}
|
|
230
242
|
const remote = overrides.createTransport !== undefined
|
|
@@ -248,14 +260,24 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
248
260
|
let realtimeConnector;
|
|
249
261
|
offline = config.transportEnabled === false;
|
|
250
262
|
const started = new SyncClient({
|
|
263
|
+
...definedObject({
|
|
264
|
+
segments: bindings.segments,
|
|
265
|
+
blobs,
|
|
266
|
+
clientId: config.clientId,
|
|
267
|
+
limits: config.limits,
|
|
268
|
+
retainFailedCommits: config.retainFailedCommits,
|
|
269
|
+
realtimePolicy: config.realtimePolicy,
|
|
270
|
+
realtimeHeartbeatMs: config.realtimeHeartbeatMs,
|
|
271
|
+
encryption: config.encryption && encryptionConfigFromKeyring(config.encryption),
|
|
272
|
+
authorityReads: config.authorityReads &&
|
|
273
|
+
overrides.createAuthorityReads(config.authorityReads),
|
|
274
|
+
securityPreflight: config.securityPreflight,
|
|
275
|
+
previousVersionContext: config.previousVersionContext,
|
|
276
|
+
}),
|
|
251
277
|
transportEnabled: !offline,
|
|
252
278
|
database,
|
|
253
279
|
schema: config.schema,
|
|
254
280
|
transport: bindings.transport,
|
|
255
|
-
...(bindings.segments !== undefined
|
|
256
|
-
? { segments: bindings.segments }
|
|
257
|
-
: {}),
|
|
258
|
-
...(blobs !== undefined ? { blobs } : {}),
|
|
259
281
|
realtime: (handlers, clientId) => {
|
|
260
282
|
// §12.2: a local authority has no realtime channel.
|
|
261
283
|
if (realtimeConnector === undefined || local?.active()) {
|
|
@@ -263,32 +285,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
263
285
|
}
|
|
264
286
|
return realtimeConnector(handlers, clientId);
|
|
265
287
|
},
|
|
266
|
-
...(config.clientId !== undefined ? { clientId: config.clientId } : {}),
|
|
267
|
-
...(config.limits !== undefined ? { limits: config.limits } : {}),
|
|
268
|
-
...(config.retainFailedCommits !== undefined
|
|
269
|
-
? { retainFailedCommits: config.retainFailedCommits }
|
|
270
|
-
: {}),
|
|
271
|
-
...(config.realtimePolicy !== undefined
|
|
272
|
-
? { realtimePolicy: config.realtimePolicy }
|
|
273
|
-
: {}),
|
|
274
|
-
...(config.realtimeHeartbeatMs !== undefined
|
|
275
|
-
? { realtimeHeartbeatMs: config.realtimeHeartbeatMs }
|
|
276
|
-
: {}),
|
|
277
288
|
schedule: scheduleBackground,
|
|
278
|
-
...(config.encryption !== undefined
|
|
279
|
-
? { encryption: encryptionConfigFromKeyring(config.encryption) }
|
|
280
|
-
: {}),
|
|
281
|
-
...(config.authorityReads !== undefined
|
|
282
|
-
? {
|
|
283
|
-
authorityReads: overrides.createAuthorityReads(config.authorityReads),
|
|
284
|
-
}
|
|
285
|
-
: {}),
|
|
286
|
-
...(config.securityPreflight !== undefined
|
|
287
|
-
? { securityPreflight: config.securityPreflight }
|
|
288
|
-
: {}),
|
|
289
|
-
...(config.previousVersionContext !== undefined
|
|
290
|
-
? { previousVersionContext: config.previousVersionContext }
|
|
291
|
-
: {}),
|
|
292
289
|
onSyncNeeded: (reason) => {
|
|
293
290
|
emit({ kind: 'sync-needed', reason });
|
|
294
291
|
consumeSyncIntent({ kind: 'interactive' });
|
|
@@ -501,10 +498,8 @@ async function defaultOpenDatabase(config) {
|
|
|
501
498
|
if (config.database.mode !== 'persistent') {
|
|
502
499
|
throw new ClientSyncError(WORKER_FAILED_CODE, "database mode 'custom' requires startSyncWorker({ openDatabase })");
|
|
503
500
|
}
|
|
504
|
-
return openPersistentWasmDatabase(config.database.name, {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
...(config.takeOverStorage === true ? { takeOver: true } : {}),
|
|
509
|
-
});
|
|
501
|
+
return openPersistentWasmDatabase(config.database.name, definedObject({
|
|
502
|
+
directory: config.database.directory,
|
|
503
|
+
takeOver: config.takeOverStorage || undefined,
|
|
504
|
+
}));
|
|
510
505
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.93.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.93.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.93.0",
|
|
129
129
|
"playwright": "1.63.0",
|
|
130
130
|
"vite": "8.3.4"
|
|
131
131
|
}
|
package/src/apply.ts
CHANGED
|
@@ -41,6 +41,14 @@ import {
|
|
|
41
41
|
/** Lets the client wrap the physical write in its revision transaction. */
|
|
42
42
|
export type ApplyTransaction = (fn: () => OverlayScope) => unknown;
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* A step of a generator-driven apply (§1.4 rule 4): the row count of the
|
|
46
|
+
* block it writes next, a negative boundary before work that runs outside a
|
|
47
|
+
* coalesced apply transaction, or async work the driver awaits outside any
|
|
48
|
+
* transaction and whose result it passes back.
|
|
49
|
+
*/
|
|
50
|
+
export type ApplyStep = number | (() => Promise<unknown>);
|
|
51
|
+
|
|
44
52
|
function upsertSql(table: CompiledClientTable): string {
|
|
45
53
|
const names = [
|
|
46
54
|
...table.columns.map((column) => quoteIdent(column.name)),
|
|
@@ -601,13 +609,13 @@ export async function applySqliteSegment(
|
|
|
601
609
|
if (count !== descriptor.rowCount)
|
|
602
610
|
imageInvalid('image row count does not match descriptor');
|
|
603
611
|
const names = table.columns.map((column) => quoteIdent(column.name));
|
|
604
|
-
const
|
|
612
|
+
const version = quoteIdent(SYNC_VERSION_COLUMN);
|
|
613
|
+
const insertNames = [...names, version];
|
|
605
614
|
const primaryKey = quoteIdent(table.primaryKey);
|
|
606
615
|
const updates = insertNames
|
|
607
616
|
.filter((name) => name !== primaryKey)
|
|
608
617
|
.map((name) => `${name}=excluded.${name}`)
|
|
609
618
|
.join(', ');
|
|
610
|
-
const version = quoteIdent(SYNC_VERSION_COLUMN);
|
|
611
619
|
// A statement copies the `rows` rows after `from`; NULL keys cannot
|
|
612
620
|
// hide between statements.
|
|
613
621
|
const range = (from: SqlValue | undefined, rows: number) => {
|
|
@@ -677,7 +685,7 @@ export async function applySqliteSegment(
|
|
|
677
685
|
);
|
|
678
686
|
for (;;) {
|
|
679
687
|
db.exec(
|
|
680
|
-
`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${
|
|
688
|
+
`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${names.join(', ')}, "_syncular_version" FROM ${source} AS image WHERE ${where} AND NOT EXISTS(SELECT 1 FROM ${quoteIdent(table.name)} AS held WHERE held.${primaryKey}=image.${primaryKey} AND held.${version}>=image._syncular_version) ON CONFLICT(${primaryKey})DO UPDATE SET ${updates}`,
|
|
681
689
|
params,
|
|
682
690
|
);
|
|
683
691
|
const copied = Number(
|
|
@@ -736,14 +744,17 @@ export async function applySqliteSegment(
|
|
|
736
744
|
}
|
|
737
745
|
|
|
738
746
|
/**
|
|
739
|
-
* Apply a decoded rows segment: each block in
|
|
747
|
+
* Apply a decoded rows segment: each block in its own transaction call
|
|
740
748
|
* (§5.2/§1.4); `clearFirst` implements the §5.6 fresh-bootstrap first-page
|
|
741
|
-
* delete inside the first block's
|
|
749
|
+
* delete inside the first block's. Each row record carries its
|
|
742
750
|
* `serverVersion` (§5.2), which lands in `_sync_version` exactly like a
|
|
743
751
|
* `COMMIT` change's `rowVersion` (§5.6) — bootstrapped rows seed §6.2
|
|
744
|
-
* `baseVersion` conflict detection immediately.
|
|
752
|
+
* `baseVersion` conflict detection immediately. The generator yields each
|
|
753
|
+
* block's row count before its write, so the caller can commit and yield to
|
|
754
|
+
* local reads there (§1.4 rule 4), and yields the async decryption as a step
|
|
755
|
+
* the caller awaits outside any transaction.
|
|
745
756
|
*/
|
|
746
|
-
export
|
|
757
|
+
export function* applyRowsSegment(
|
|
747
758
|
db: ClientDatabase,
|
|
748
759
|
schema: CompiledClientSchema,
|
|
749
760
|
table: CompiledClientTable,
|
|
@@ -759,41 +770,47 @@ export async function applyRowsSegment(
|
|
|
759
770
|
readonly isCurrent?: () => boolean;
|
|
760
771
|
},
|
|
761
772
|
encryption?: EncryptionConfig,
|
|
762
|
-
):
|
|
773
|
+
): Generator<ApplyStep, number, unknown> {
|
|
763
774
|
validateSegmentColumns(schema, table, segment);
|
|
764
775
|
let applied = 0;
|
|
765
776
|
let first = true;
|
|
766
|
-
|
|
777
|
+
type Row = SegmentRow & { readonly sealed?: SealedRow | undefined };
|
|
778
|
+
let blocks: readonly (readonly Row[])[] =
|
|
767
779
|
segment.blocks.length > 0 ? segment.blocks : [[]];
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
// The rows decrypt concurrently; the first failing row in block order
|
|
774
|
-
// decides the error.
|
|
775
|
-
let rows: readonly {
|
|
776
|
-
values: readonly RowValue[];
|
|
777
|
-
serverVersion: number;
|
|
778
|
-
sealed?: SealedRow | undefined;
|
|
779
|
-
}[] = block;
|
|
780
|
-
if (table.hasEncryptedColumns) {
|
|
780
|
+
// §5.11: decrypt the segment's rows before any write (WebCrypto is async;
|
|
781
|
+
// the SQLite transaction is not). The rows decrypt concurrently; the first
|
|
782
|
+
// failing row in segment order decides the error, before any block writes.
|
|
783
|
+
if (table.hasEncryptedColumns)
|
|
784
|
+
blocks = (yield async () => {
|
|
781
785
|
const { decryptOrSealRowValues } = await import('./encryption');
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
786
|
+
return (
|
|
787
|
+
await Promise.all(
|
|
788
|
+
blocks.map((block) =>
|
|
789
|
+
Promise.allSettled(
|
|
790
|
+
block.map(async (row) => ({
|
|
791
|
+
...(await decryptOrSealRowValues(
|
|
792
|
+
encryption,
|
|
793
|
+
table,
|
|
794
|
+
row.values,
|
|
795
|
+
)),
|
|
796
|
+
serverVersion: row.serverVersion,
|
|
797
|
+
})),
|
|
798
|
+
),
|
|
799
|
+
),
|
|
800
|
+
)
|
|
801
|
+
).map((block) =>
|
|
802
|
+
block.map((result) => {
|
|
803
|
+
if (result.status === 'rejected') throw result.reason;
|
|
804
|
+
return result.value;
|
|
805
|
+
}),
|
|
787
806
|
);
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
return result.value;
|
|
791
|
-
});
|
|
792
|
-
}
|
|
793
|
-
if (options.isCurrent?.() === false) break;
|
|
807
|
+
}) as readonly (readonly Row[])[];
|
|
808
|
+
for (const rows of blocks) {
|
|
794
809
|
const clearThisBlock = first && options.clearFirst;
|
|
795
810
|
first = false;
|
|
796
811
|
if (!clearThisBlock && rows.length === 0) continue;
|
|
812
|
+
yield rows.length;
|
|
813
|
+
if (options.isCurrent?.() === false) break;
|
|
797
814
|
(options.transaction ?? ((fn) => db.transaction(fn)))(() => {
|
|
798
815
|
const scope: OverlayScope = [
|
|
799
816
|
...(hasUniqueIndex(table) ? [JSON.stringify([table.name])] : []),
|
|
@@ -828,7 +845,6 @@ export async function applyRowsSegment(
|
|
|
828
845
|
return rebaseScope(scope, kept, skipped);
|
|
829
846
|
});
|
|
830
847
|
options.onProgress?.(applied);
|
|
831
|
-
await yieldToHost();
|
|
832
848
|
}
|
|
833
849
|
options.onProgress?.(applied);
|
|
834
850
|
return applied;
|