@syncular/client 0.53.1 → 0.55.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.
@@ -0,0 +1,505 @@
1
+ import { SyncClient } from './client.js';
2
+ import { withClientDiagnosticsHost, } from './diagnostics.js';
3
+ import { encryptionConfigFromKeyring } from './encryption.js';
4
+ import { classifySqliteFailure, ClientSyncError, STORAGE_BUSY_CODE, } from './errors.js';
5
+ import { httpBlobTransport, httpSegmentDownloader, httpSyncTransport, webSocketRealtimeConnector, } from './http.js';
6
+ import { openPersistentWasmDatabase } from './wasm-database.js';
7
+ import { LOCAL_AUTHORITY_UNCONFIGURED_CODE, WORKER_FAILED_CODE, WORKER_METHOD_UNAVAILABLE_CODE, } from './worker-protocol.js';
8
+ function toErrorShape(caught) {
9
+ const error = classifySqliteFailure(caught).error;
10
+ if (error instanceof ClientSyncError) {
11
+ return {
12
+ code: error.code,
13
+ message: error.message,
14
+ retryable: error.retryable,
15
+ ...(error.details !== undefined ? { details: error.details } : {}),
16
+ };
17
+ }
18
+ return {
19
+ code: WORKER_FAILED_CODE,
20
+ message: error instanceof Error ? error.message : String(error),
21
+ retryable: false,
22
+ };
23
+ }
24
+ /**
25
+ * ArrayBuffers safe to transfer out of a query result: whole-buffer
26
+ * views only (a view into a larger/pooled buffer is cloned instead —
27
+ * transferring would detach unrelated data), deduplicated because a
28
+ * buffer may only appear once in a transfer list.
29
+ */
30
+ function queryTransferables(rows) {
31
+ const buffers = new Set();
32
+ for (const row of rows) {
33
+ if (row === null || typeof row !== 'object')
34
+ continue;
35
+ for (const value of Object.values(row)) {
36
+ if (value instanceof Uint8Array &&
37
+ value.buffer instanceof ArrayBuffer &&
38
+ value.byteOffset === 0 &&
39
+ value.byteLength === value.buffer.byteLength) {
40
+ buffers.add(value.buffer);
41
+ }
42
+ }
43
+ }
44
+ return [...buffers];
45
+ }
46
+ /**
47
+ * Register the RPC server on the current worker scope. Call this once
48
+ * from a worker entry script; the main-thread handle drives everything
49
+ * else through `init`/`call` messages.
50
+ */
51
+ export function startSyncWorkerCore(overrides, createLocalAuthority) {
52
+ const scope = globalThis;
53
+ const post = (message, transfer) => {
54
+ if (transfer !== undefined && transfer.length > 0) {
55
+ scope.postMessage(message, transfer);
56
+ }
57
+ else {
58
+ scope.postMessage(message);
59
+ }
60
+ };
61
+ let client;
62
+ let database;
63
+ let offline = false;
64
+ let headers = {};
65
+ let closed = false;
66
+ // -- one sync loop: RPC-driven and auto-driven rounds serialize ----------
67
+ let syncChain = Promise.resolve();
68
+ function serializedSync(fn) {
69
+ const next = syncChain.then(fn, fn);
70
+ syncChain = next.then(() => undefined, () => undefined);
71
+ return next;
72
+ }
73
+ // -- SPEC §7.5 host loop: event-driven intents, no interactive polling --
74
+ let autoSync = true;
75
+ let autoSyncScheduled = false;
76
+ let backgroundTimer;
77
+ let backgroundDue = Number.POSITIVE_INFINITY;
78
+ function runAutoSync() {
79
+ if (closed ||
80
+ client === undefined ||
81
+ offline ||
82
+ client.securityLifecycle() === 'preflight') {
83
+ autoSyncScheduled = false;
84
+ return;
85
+ }
86
+ const running = client;
87
+ void serializedSync(() => {
88
+ // The queued round owns the flag from here. Clearing it at entry would
89
+ // let every wake-up that arrives while this round waits behind the
90
+ // running one queue its own full drive (§8.4 coalescing).
91
+ autoSyncScheduled = false;
92
+ return running.syncUntilIdle();
93
+ })
94
+ .then((summary) => {
95
+ if (!closed)
96
+ post({ t: 'event', event: { kind: 'synced', summary } });
97
+ if (summary.budgetExhausted)
98
+ consumeSyncIntent({ kind: 'interactive' });
99
+ })
100
+ .catch((error) => {
101
+ if (!closed) {
102
+ post({
103
+ t: 'event',
104
+ event: { kind: 'synced', error: toErrorShape(error) },
105
+ });
106
+ }
107
+ });
108
+ }
109
+ function consumeSyncIntent(intent) {
110
+ if (!autoSync ||
111
+ closed ||
112
+ client === undefined ||
113
+ offline ||
114
+ client.securityLifecycle() === 'preflight' ||
115
+ intent.kind === 'none') {
116
+ return;
117
+ }
118
+ if (intent.kind === 'background') {
119
+ if (autoSyncScheduled)
120
+ return;
121
+ const due = Date.now() + Math.max(0, intent.delayMs);
122
+ if (backgroundTimer !== undefined && due >= backgroundDue)
123
+ return;
124
+ if (backgroundTimer !== undefined)
125
+ clearTimeout(backgroundTimer);
126
+ backgroundDue = due;
127
+ backgroundTimer = setTimeout(() => {
128
+ backgroundTimer = undefined;
129
+ backgroundDue = Number.POSITIVE_INFINITY;
130
+ if (!autoSyncScheduled) {
131
+ autoSyncScheduled = true;
132
+ runAutoSync();
133
+ }
134
+ }, Math.max(0, due - Date.now()));
135
+ return;
136
+ }
137
+ if (backgroundTimer !== undefined) {
138
+ clearTimeout(backgroundTimer);
139
+ backgroundTimer = undefined;
140
+ backgroundDue = Number.POSITIVE_INFINITY;
141
+ }
142
+ if (!autoSync || autoSyncScheduled || closed || client === undefined) {
143
+ return;
144
+ }
145
+ autoSyncScheduled = true;
146
+ queueMicrotask(runAutoSync);
147
+ }
148
+ function requireClient() {
149
+ if (client === undefined) {
150
+ throw new ClientSyncError(WORKER_FAILED_CODE, 'the worker received a call before init completed');
151
+ }
152
+ return client;
153
+ }
154
+ const local = createLocalAuthority?.({
155
+ overrides,
156
+ requireClient,
157
+ serializedSync,
158
+ isOffline: () => offline,
159
+ wake: () => consumeSyncIntent({ kind: 'interactive' }),
160
+ });
161
+ async function init(config) {
162
+ if (config.localAuthority === true && local === undefined)
163
+ throw new ClientSyncError(LOCAL_AUTHORITY_UNCONFIGURED_CODE, 'this worker does not support a local authority');
164
+ if (config.authorityReads !== undefined && !overrides.createAuthorityReads)
165
+ throw new ClientSyncError('client.authority_read_forbidden', 'authorityReads requires startSyncWorker({ createAuthorityReads })');
166
+ if (client !== undefined) {
167
+ throw new ClientSyncError(WORKER_FAILED_CODE, 'the worker is already initialized');
168
+ }
169
+ autoSync = config.autoSync ?? true;
170
+ headers = config.headers ?? {};
171
+ const http = { headers: () => headers };
172
+ for (let attempt = 0;; attempt++) {
173
+ try {
174
+ database =
175
+ overrides.openDatabase !== undefined
176
+ ? await overrides.openDatabase(config)
177
+ : await defaultOpenDatabase(config);
178
+ break;
179
+ }
180
+ catch (error) {
181
+ if (config.database.mode !== 'persistent' ||
182
+ !(error instanceof ClientSyncError) ||
183
+ error.code !== STORAGE_BUSY_CODE ||
184
+ !error.retryable ||
185
+ attempt === 6) {
186
+ throw error;
187
+ }
188
+ // Retain the leader lease while the previous worker's OPFS handles
189
+ // drain. Only opening is retried; core initialization has not started.
190
+ const delayMs = Math.min(50 * 2 ** attempt, 1000);
191
+ if (overrides.waitForStorageRetry !== undefined) {
192
+ await overrides.waitForStorageRetry(delayMs);
193
+ }
194
+ else {
195
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
196
+ }
197
+ }
198
+ }
199
+ const remote = overrides.createTransport !== undefined
200
+ ? overrides.createTransport(config)
201
+ : httpSyncTransport(config.endpoints.syncUrl, http);
202
+ const remoteSegments = overrides.createSegments !== undefined
203
+ ? overrides.createSegments(config)
204
+ : config.endpoints.segmentsUrl !== undefined
205
+ ? httpSegmentDownloader(config.endpoints.segmentsUrl, http)
206
+ : undefined;
207
+ const bindings = config.localAuthority === true && local !== undefined
208
+ ? await local.open(config, database, remote, remoteSegments)
209
+ : { transport: remote, segments: remoteSegments };
210
+ // §5.9 blob transport: only when a blobs URL is configured.
211
+ const blobs = config.endpoints.blobsUrl !== undefined
212
+ ? httpBlobTransport(config.endpoints.blobsUrl, http)
213
+ : undefined;
214
+ // Realtime needs the (possibly persisted) clientId for the
215
+ // `{clientId}` URL placeholder — start the client first, then attach
216
+ // the connector through a mutable slot read at connect time.
217
+ let realtimeConnector;
218
+ offline = config.transportEnabled === false;
219
+ const started = new SyncClient({
220
+ transportEnabled: !offline,
221
+ database,
222
+ schema: config.schema,
223
+ transport: bindings.transport,
224
+ ...(bindings.segments !== undefined
225
+ ? { segments: bindings.segments }
226
+ : {}),
227
+ ...(blobs !== undefined ? { blobs } : {}),
228
+ realtime: (handlers) => {
229
+ // §12.2: a local authority has no realtime channel.
230
+ if (realtimeConnector === undefined || local?.active()) {
231
+ throw new ClientSyncError('sync.invalid_request', 'no realtime connector configured');
232
+ }
233
+ return realtimeConnector(handlers);
234
+ },
235
+ ...(config.clientId !== undefined ? { clientId: config.clientId } : {}),
236
+ ...(config.limits !== undefined ? { limits: config.limits } : {}),
237
+ ...(config.retainFailedCommits !== undefined
238
+ ? { retainFailedCommits: config.retainFailedCommits }
239
+ : {}),
240
+ ...(config.realtimePolicy !== undefined
241
+ ? { realtimePolicy: config.realtimePolicy }
242
+ : {}),
243
+ ...(config.encryption !== undefined
244
+ ? { encryption: encryptionConfigFromKeyring(config.encryption) }
245
+ : {}),
246
+ ...(config.authorityReads !== undefined
247
+ ? {
248
+ authorityReads: overrides.createAuthorityReads(config.authorityReads),
249
+ }
250
+ : {}),
251
+ ...(config.securityPreflight !== undefined
252
+ ? { securityPreflight: config.securityPreflight }
253
+ : {}),
254
+ ...(config.previousVersionContext !== undefined
255
+ ? { previousVersionContext: config.previousVersionContext }
256
+ : {}),
257
+ onSyncNeeded: (reason) => {
258
+ post({ t: 'event', event: { kind: 'sync-needed', reason } });
259
+ consumeSyncIntent({ kind: 'interactive' });
260
+ },
261
+ onSyncIntent: consumeSyncIntent,
262
+ onConflict: (conflict) => {
263
+ post({ t: 'event', event: { kind: 'conflict', conflict } });
264
+ },
265
+ onUpgrading: (upgrading) => {
266
+ // §7.4.5: surface the schema-bump reset + completion to the UI thread.
267
+ post({ t: 'event', event: { kind: 'upgrading', upgrading } });
268
+ },
269
+ onPresence: (scopeKey) => {
270
+ // §8.6: surface a presence change to the UI thread.
271
+ post({ t: 'event', event: { kind: 'presence', scopeKey } });
272
+ },
273
+ });
274
+ started.onProgress((progress) => {
275
+ if (!closed)
276
+ post({ t: 'event', event: { kind: 'progress', progress } });
277
+ });
278
+ started.onChange((batch) => {
279
+ if (!closed)
280
+ post({ t: 'event', event: { kind: 'change', batch } });
281
+ });
282
+ const postDiagnostics = (snapshot) => {
283
+ if (closed)
284
+ return;
285
+ post({
286
+ t: 'event',
287
+ event: {
288
+ kind: 'diagnostics',
289
+ snapshot: withClientDiagnosticsHost(snapshot, {
290
+ kind: 'worker',
291
+ role: 'leader',
292
+ connectivity: offline ? 'offline' : snapshot.host.connectivity,
293
+ realtime: snapshot.host.realtime,
294
+ }),
295
+ },
296
+ });
297
+ };
298
+ started.onDiagnostics(postDiagnostics);
299
+ await started.start();
300
+ realtimeConnector =
301
+ overrides.createRealtime !== undefined
302
+ ? overrides.createRealtime(config, started.clientId, http.headers)
303
+ : config.endpoints.realtimeUrl !== undefined
304
+ ? webSocketRealtimeConnector(config.endpoints.realtimeUrl.replace('{clientId}', encodeURIComponent(started.clientId)))
305
+ : undefined;
306
+ client = started;
307
+ // `start()` may discover persisted subscriptions/outbox work. Its callback
308
+ // fires before this worker publishes the initialized client, so consume
309
+ // the durable state once here as well; coalescing makes this a single task.
310
+ if (started.statusSnapshot().syncNeeded)
311
+ consumeSyncIntent({ kind: 'interactive' });
312
+ return { clientId: started.clientId };
313
+ }
314
+ // Forward every argument to core methods, including authority's zero-argument check.
315
+ const coreMethods = [
316
+ 'securityLifecycle',
317
+ 'subscribe',
318
+ 'subscribeMany',
319
+ 'unsubscribe',
320
+ 'windowState',
321
+ 'purgeLocalData',
322
+ 'rebootstrapLocalData',
323
+ 'previousVersionSnapshot',
324
+ 'previousVersionAudit',
325
+ 'previousVersionDiscard',
326
+ 'query',
327
+ 'authoritySnapshot',
328
+ 'querySnapshot',
329
+ 'snapshotRead',
330
+ 'statusSnapshot',
331
+ 'conflicts',
332
+ 'rejections',
333
+ 'commitOutcome',
334
+ 'commitOutcomes',
335
+ 'resolveCommitOutcome',
336
+ 'pendingCommits',
337
+ 'subscriptions',
338
+ 'subscription',
339
+ 'connectRealtime',
340
+ 'disconnectRealtime',
341
+ 'setPresence',
342
+ 'presence',
343
+ 'uploadBlob',
344
+ 'fetchBlob',
345
+ ];
346
+ const api = {
347
+ ...Object.fromEntries(coreMethods.map((method) => [
348
+ method,
349
+ (...args) => {
350
+ const running = requireClient();
351
+ return Reflect.apply(running[method], running, args);
352
+ },
353
+ ])),
354
+ beginSecurityPreflight: async () => {
355
+ if (backgroundTimer !== undefined)
356
+ clearTimeout(backgroundTimer);
357
+ backgroundTimer = undefined;
358
+ backgroundDue = Number.POSITIVE_INFINITY;
359
+ autoSyncScheduled = false;
360
+ await requireClient().beginSecurityPreflight();
361
+ },
362
+ activateSecurity: async (options = {}) => {
363
+ await requireClient().activateSecurity({
364
+ ...(options.encryption !== undefined
365
+ ? { encryption: encryptionConfigFromKeyring(options.encryption) }
366
+ : {}),
367
+ });
368
+ },
369
+ setWindow: async (base, units) => {
370
+ await requireClient().setWindowCommand(base, units);
371
+ },
372
+ mutate: (mutations) => {
373
+ return requireClient().mutateCommand(mutations).value;
374
+ },
375
+ patch: (table, rowId, partial, options) => {
376
+ // Same §8.4 rule as `mutate`: a local write must push without the app
377
+ // orchestrating sync, so consume the core's immediate intent.
378
+ const result = requireClient().patchCommand(table, rowId, partial, options);
379
+ return result.value;
380
+ },
381
+ sync: () => {
382
+ const running = requireClient();
383
+ return serializedSync(() => running.sync());
384
+ },
385
+ syncUntilIdle: (options) => {
386
+ const running = requireClient();
387
+ return serializedSync(() => running.syncUntilIdle(options));
388
+ },
389
+ proveCurrentBase: (outcome, options) => {
390
+ const running = requireClient();
391
+ return serializedSync(() => running.proveCurrentBase(outcome, options));
392
+ },
393
+ resetClientId: () => requireClient().resetClientId(),
394
+ localRevision: () => requireClient().localRevision,
395
+ diagnosticsSnapshot: (request) => {
396
+ const snapshot = requireClient().diagnosticsSnapshot(request);
397
+ return withClientDiagnosticsHost(snapshot, {
398
+ kind: 'worker',
399
+ role: 'leader',
400
+ connectivity: offline ? 'offline' : snapshot.host.connectivity,
401
+ realtime: snapshot.host.realtime,
402
+ });
403
+ },
404
+ setHeaders: (next) => {
405
+ headers = next;
406
+ },
407
+ setOffline: (value) => {
408
+ offline = value;
409
+ requireClient().setTransportEnabled(!value);
410
+ if (offline) {
411
+ if (backgroundTimer !== undefined)
412
+ clearTimeout(backgroundTimer);
413
+ backgroundTimer = undefined;
414
+ backgroundDue = Number.POSITIVE_INFINITY;
415
+ }
416
+ else if (client !== undefined) {
417
+ const snapshot = client.diagnosticsSnapshot();
418
+ post({
419
+ t: 'event',
420
+ event: {
421
+ kind: 'diagnostics',
422
+ snapshot: withClientDiagnosticsHost(snapshot, {
423
+ kind: 'worker',
424
+ role: 'leader',
425
+ connectivity: snapshot.host.connectivity,
426
+ realtime: snapshot.host.realtime,
427
+ }),
428
+ },
429
+ });
430
+ }
431
+ },
432
+ ...local?.methods,
433
+ close: async () => {
434
+ closed = true;
435
+ if (backgroundTimer !== undefined)
436
+ clearTimeout(backgroundTimer);
437
+ await client?.close();
438
+ local?.close();
439
+ database?.close();
440
+ client = undefined;
441
+ database = undefined;
442
+ },
443
+ };
444
+ // Local purge/rebootstrap/discard rewrite the same durable state an
445
+ // in-flight sync round captured at send time, and the RFC 0005 reads observe
446
+ // (and can themselves trigger the discard of) that same previous-version
447
+ // container. Running all of them on the sync chain orders them against RPC-
448
+ // and auto-driven rounds (the client core's reset fence covers hosts that
449
+ // call the core directly).
450
+ const syncChainMethods = new Set([
451
+ 'purgeLocalData',
452
+ 'rebootstrapLocalData',
453
+ 'previousVersionSnapshot',
454
+ 'previousVersionAudit',
455
+ 'previousVersionDiscard',
456
+ ]);
457
+ async function dispatch(message) {
458
+ const method = api[message.method];
459
+ if (!Object.hasOwn(api, message.method) || typeof method !== 'function')
460
+ throw new ClientSyncError(WORKER_METHOD_UNAVAILABLE_CODE, 'the worker method is not configured');
461
+ const invoke = () => Reflect.apply(method, api, message.args);
462
+ if (syncChainMethods.has(message.method)) {
463
+ return await serializedSync(async () => invoke());
464
+ }
465
+ return await invoke();
466
+ }
467
+ function run(id, fn, transfer) {
468
+ void (async () => {
469
+ try {
470
+ const value = await fn();
471
+ post({ t: 'result', id, value }, transfer !== undefined ? transfer(value) : undefined);
472
+ }
473
+ catch (error) {
474
+ post({ t: 'error', id, error: toErrorShape(error) });
475
+ }
476
+ })();
477
+ }
478
+ scope.addEventListener('message', (event) => {
479
+ const message = event.data;
480
+ if (message.t === 'init') {
481
+ run(message.id, () => init(message.config));
482
+ return;
483
+ }
484
+ if (message.t === 'call') {
485
+ run(message.id, () => dispatch(message), message.method === 'query'
486
+ ? (value) => queryTransferables(value)
487
+ : message.method === 'querySnapshot'
488
+ ? (value) => queryTransferables(value.rows)
489
+ : undefined);
490
+ }
491
+ });
492
+ post({ t: 'ready' });
493
+ }
494
+ async function defaultOpenDatabase(config) {
495
+ if (config.database.mode !== 'persistent') {
496
+ throw new ClientSyncError(WORKER_FAILED_CODE, "database mode 'custom' requires an openDatabase override in the " +
497
+ 'worker bootstrap (startSyncWorker({ openDatabase }))');
498
+ }
499
+ return openPersistentWasmDatabase(config.database.name, {
500
+ ...(config.database.directory !== undefined
501
+ ? { directory: config.database.directory }
502
+ : {}),
503
+ ...(config.takeOverStorage === true ? { takeOver: true } : {}),
504
+ });
505
+ }
@@ -1,56 +1,4 @@
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 type { defineAuthorityReads } from './authority.js';
17
- import type { ClientDatabase } from './database.js';
18
- import type { RealtimeConnector, SegmentDownloader, SyncTransport } from './transport.js';
19
- import type { ClientSchema } from './schema.js';
20
- import { type LocalAuthorityState, type WorkerInitConfig } from './worker-protocol.js';
21
- export interface SyncWorkerOverrides {
22
- /** Explicit authority reader factory. Required when init declares authorityReads. */
23
- readonly createAuthorityReads?: typeof defineAuthorityReads;
24
- /** Database factory indirection: tests inject bun:sqlite here. */
25
- readonly openDatabase?: (config: WorkerInitConfig) => Promise<ClientDatabase> | ClientDatabase;
26
- /** Retry deadline indirection for deterministic worker-host tests. */
27
- readonly waitForStorageRetry?: (delayMs: number) => Promise<void>;
28
- readonly createTransport?: (config: WorkerInitConfig) => SyncTransport;
29
- readonly createSegments?: (config: WorkerInitConfig) => SegmentDownloader | undefined;
30
- /**
31
- * Custom realtime connector, e.g. one that mints a fresh ticket per
32
- * connection attempt. `headers` reads the current host auth set.
33
- */
34
- readonly createRealtime?: (config: WorkerInitConfig, clientId: string, headers: () => Readonly<Record<string, string>>) => RealtimeConnector | undefined;
35
- /**
36
- * SPEC §12 local authority, required by init's `localAuthority: true`:
37
- * `(database, schema) => openLocalAuthority({ database:
38
- * clientSqliteDatabase(database), schema })` from `@syncular/server`. Only
39
- * a worker entry that passes it bundles the server code.
40
- */
41
- readonly openLocalAuthority?: (database: ClientDatabase, schema: ClientSchema) => Promise<WorkerLocalAuthority>;
42
- }
43
- /** The `LocalAuthority` surface of `@syncular/server` the worker drives (§12). */
44
- export interface WorkerLocalAuthority {
45
- readonly state: LocalAuthorityState;
46
- readonly sync: SyncTransport;
47
- readonly downloadSegment: SegmentDownloader;
48
- readonly attach: (transport: SyncTransport) => Promise<void>;
49
- readonly abandonAttach: (transport?: SyncTransport) => Promise<void>;
50
- }
51
- /**
52
- * Register the RPC server on the current worker scope. Call this once
53
- * from a worker entry script; the main-thread handle drives everything
54
- * else through `init`/`call` messages.
55
- */
1
+ import { type SyncWorkerOverrides } from './worker-core.js';
2
+ export type { SyncWorkerOverrides, WorkerLocalAuthority } from './worker-core.js';
3
+ /** Start a worker that supports the optional local authority (§12). */
56
4
  export declare function startSyncWorker(overrides?: SyncWorkerOverrides): void;