@syncular/client 0.90.0 → 0.92.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 +30 -29
- package/dist/client.d.ts +7 -0
- package/dist/client.js +314 -263
- 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/native-bridge.d.ts +1 -1
- package/dist/native-bridge.js +1 -1
- package/dist/reactive-store.js +7 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +8 -3
- package/dist/sync-scheduler.js +5 -1
- package/dist/worker-core.d.ts +0 -15
- package/dist/worker-core.js +44 -43
- package/package.json +3 -3
- package/src/apply.ts +52 -30
- package/src/client.ts +377 -330
- 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/native-bridge.ts +1 -1
- package/src/reactive-store.ts +6 -1
- package/src/schema.ts +2 -4
- package/src/sync-scheduler.ts +5 -1
- package/src/worker-core.ts +38 -44
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/native-bridge.d.ts
CHANGED
|
@@ -219,7 +219,7 @@ export declare abstract class NativeBridgeClient implements PromiseMethods<Clien
|
|
|
219
219
|
setWindow(base: WindowBase, units: readonly string[]): Promise<void>;
|
|
220
220
|
windowState(base: WindowBase): Promise<WindowState>;
|
|
221
221
|
sync(): Promise<unknown>;
|
|
222
|
-
/** The Rust core has no §8.4
|
|
222
|
+
/** The Rust core has no §8.4 host policy: `skipEmpty` and `coalesceMs` are not sent. */
|
|
223
223
|
syncUntilIdle(options?: number | SyncIdleOptions, timeoutMs?: number): Promise<unknown>;
|
|
224
224
|
proveCurrentBase(outcome: Pick<CommitOutcome, 'clientCommitId' | 'sequence'>, maxRounds?: number): Promise<CurrentBaseProof>;
|
|
225
225
|
conflicts(): Promise<readonly ConflictRecord[]>;
|
package/dist/native-bridge.js
CHANGED
|
@@ -722,7 +722,7 @@ export class NativeBridgeClient {
|
|
|
722
722
|
async sync() {
|
|
723
723
|
return this.command('sync', {});
|
|
724
724
|
}
|
|
725
|
-
/** The Rust core has no §8.4
|
|
725
|
+
/** The Rust core has no §8.4 host policy: `skipEmpty` and `coalesceMs` are not sent. */
|
|
726
726
|
async syncUntilIdle(options, timeoutMs) {
|
|
727
727
|
const maxRounds = typeof options === 'object' ? options.maxRounds : options;
|
|
728
728
|
if (typeof options === 'object')
|
package/dist/reactive-store.js
CHANGED
|
@@ -450,7 +450,8 @@ class QueryEntry {
|
|
|
450
450
|
const rowChange = batch.tables.some((change) => this.spec.dependencies.some((dependency) => dependency.table === change.table));
|
|
451
451
|
if (rowChange && this.#claimError !== undefined)
|
|
452
452
|
this.reclaim();
|
|
453
|
-
|
|
453
|
+
// A running read reads again only when its snapshot predates the batch.
|
|
454
|
+
else if (!this.#running)
|
|
454
455
|
this.#requestRead();
|
|
455
456
|
}
|
|
456
457
|
#publish(next) {
|
|
@@ -582,9 +583,11 @@ class QueryEntry {
|
|
|
582
583
|
return;
|
|
583
584
|
const generation = this.#generation;
|
|
584
585
|
this.#running = true;
|
|
586
|
+
let asked = this.#desiredRevision;
|
|
585
587
|
try {
|
|
586
588
|
do {
|
|
587
589
|
this.#requested = false;
|
|
590
|
+
asked = this.#desiredRevision;
|
|
588
591
|
if (localReadsBlocked(this.store.availabilitySnapshot()))
|
|
589
592
|
break;
|
|
590
593
|
if (generation !== this.#generation || this.#listeners.size === 0)
|
|
@@ -659,6 +662,9 @@ class QueryEntry {
|
|
|
659
662
|
this.#gate();
|
|
660
663
|
return;
|
|
661
664
|
}
|
|
665
|
+
// A batch that arrived during the failed read reads again.
|
|
666
|
+
if (this.#desiredRevision > asked)
|
|
667
|
+
this.#requested = true;
|
|
662
668
|
// SPEC §7.5: a failed latest read is never `ready`; the last
|
|
663
669
|
// successful rows and revision stay available beside the error.
|
|
664
670
|
this.#publish({
|
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/sync-scheduler.js
CHANGED
|
@@ -64,7 +64,11 @@ export function installSyncScheduler(client, options = {}) {
|
|
|
64
64
|
running = true;
|
|
65
65
|
backgroundReady = false;
|
|
66
66
|
void client
|
|
67
|
-
.syncUntilIdle({
|
|
67
|
+
.syncUntilIdle({
|
|
68
|
+
maxRounds: options.maxRounds,
|
|
69
|
+
skipEmpty: true,
|
|
70
|
+
coalesceMs,
|
|
71
|
+
})
|
|
68
72
|
.then((result) => {
|
|
69
73
|
retryDelayMs = 250;
|
|
70
74
|
if (result !== null &&
|
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';
|
|
@@ -105,7 +121,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
105
121
|
// let every wake-up that arrives while this round waits behind the
|
|
106
122
|
// running one queue its own full drive (§8.4 coalescing).
|
|
107
123
|
autoSyncScheduled = false;
|
|
108
|
-
return running.syncUntilIdle({ skipEmpty: true });
|
|
124
|
+
return running.syncUntilIdle({ skipEmpty: true, coalesceMs });
|
|
109
125
|
})
|
|
110
126
|
.then((summary) => {
|
|
111
127
|
retryDelayMs = 250;
|
|
@@ -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' });
|
|
@@ -399,7 +396,13 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
399
396
|
},
|
|
400
397
|
syncUntilIdle: (options) => {
|
|
401
398
|
const running = requireClient();
|
|
402
|
-
|
|
399
|
+
// §8.4: a host drive through this handle (the realtime supervisor's)
|
|
400
|
+
// takes the handle's coalesceMs unless it names its own.
|
|
401
|
+
return drive(() => running.syncUntilIdle(typeof options === 'object' &&
|
|
402
|
+
options.skipEmpty === true &&
|
|
403
|
+
options.coalesceMs === undefined
|
|
404
|
+
? { ...options, coalesceMs }
|
|
405
|
+
: options));
|
|
403
406
|
},
|
|
404
407
|
proveCurrentBase: (outcome, options) => {
|
|
405
408
|
const running = requireClient();
|
|
@@ -495,10 +498,8 @@ async function defaultOpenDatabase(config) {
|
|
|
495
498
|
if (config.database.mode !== 'persistent') {
|
|
496
499
|
throw new ClientSyncError(WORKER_FAILED_CODE, "database mode 'custom' requires startSyncWorker({ openDatabase })");
|
|
497
500
|
}
|
|
498
|
-
return openPersistentWasmDatabase(config.database.name, {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
...(config.takeOverStorage === true ? { takeOver: true } : {}),
|
|
503
|
-
});
|
|
501
|
+
return openPersistentWasmDatabase(config.database.name, definedObject({
|
|
502
|
+
directory: config.database.directory,
|
|
503
|
+
takeOver: config.takeOverStorage || undefined,
|
|
504
|
+
}));
|
|
504
505
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.92.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.92.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.92.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,35 +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
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
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 () => {
|
|
785
|
+
const { decryptOrSealRowValues } = await import('./encryption');
|
|
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
|
+
}),
|
|
806
|
+
);
|
|
807
|
+
}) as readonly (readonly Row[])[];
|
|
808
|
+
for (const rows of blocks) {
|
|
788
809
|
const clearThisBlock = first && options.clearFirst;
|
|
789
810
|
first = false;
|
|
790
811
|
if (!clearThisBlock && rows.length === 0) continue;
|
|
812
|
+
yield rows.length;
|
|
813
|
+
if (options.isCurrent?.() === false) break;
|
|
791
814
|
(options.transaction ?? ((fn) => db.transaction(fn)))(() => {
|
|
792
815
|
const scope: OverlayScope = [
|
|
793
816
|
...(hasUniqueIndex(table) ? [JSON.stringify([table.name])] : []),
|
|
@@ -822,7 +845,6 @@ export async function applyRowsSegment(
|
|
|
822
845
|
return rebaseScope(scope, kept, skipped);
|
|
823
846
|
});
|
|
824
847
|
options.onProgress?.(applied);
|
|
825
|
-
await yieldToHost();
|
|
826
848
|
}
|
|
827
849
|
options.onProgress?.(applied);
|
|
828
850
|
return applied;
|