@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.
@@ -1,747 +1,9 @@
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';
17
- import { SyncClient } from './client';
18
- import type { ClientDatabase, SiblingDatabase } from './database';
19
- import {
20
- type ClientDiagnosticsSnapshot,
21
- withClientDiagnosticsHost,
22
- } from './diagnostics';
23
- import { encryptionConfigFromKeyring } from './encryption';
24
- import {
25
- classifySqliteFailure,
26
- ClientSyncError,
27
- STORAGE_BUSY_CODE,
28
- } from './errors';
29
- import {
30
- httpBlobTransport,
31
- httpSegmentDownloader,
32
- httpSyncTransport,
33
- webSocketRealtimeConnector,
34
- } from './http';
35
- import type { SyncIntent } from './invalidation';
36
- import type {
37
- RealtimeConnector,
38
- SegmentDownloader,
39
- SyncTransport,
40
- } from './transport';
41
- import type { ClientSchema } from './schema';
42
- import { openPersistentWasmDatabase } from './wasm-database';
43
- import {
44
- LOCAL_AUTHORITY_UNCONFIGURED_CODE,
45
- type LocalAuthorityState,
46
- type MainToWorkerMessage,
47
- WORKER_FAILED_CODE,
48
- type WorkerApi,
49
- type WorkerCallMessage,
50
- type WorkerErrorShape,
51
- type WorkerInitConfig,
52
- type WorkerInitResult,
53
- type WorkerMethod,
54
- type WorkerToMainMessage,
55
- } from './worker-protocol';
1
+ import { startSyncWorkerCore, type SyncWorkerOverrides } from './worker-core';
2
+ import { createWorkerLocalAuthority } from './worker-local-authority';
56
3
 
57
- export interface SyncWorkerOverrides {
58
- /** Explicit authority reader factory. Required when init declares authorityReads. */
59
- readonly createAuthorityReads?: typeof defineAuthorityReads;
60
- /** Database factory indirection: tests inject bun:sqlite here. */
61
- readonly openDatabase?: (
62
- config: WorkerInitConfig,
63
- ) => Promise<ClientDatabase> | ClientDatabase;
64
- /** Retry deadline indirection for deterministic worker-host tests. */
65
- readonly waitForStorageRetry?: (delayMs: number) => Promise<void>;
66
- readonly createTransport?: (config: WorkerInitConfig) => SyncTransport;
67
- readonly createSegments?: (
68
- config: WorkerInitConfig,
69
- ) => SegmentDownloader | undefined;
70
- /**
71
- * Custom realtime connector, e.g. one that mints a fresh ticket per
72
- * connection attempt. `headers` reads the current host auth set.
73
- */
74
- readonly createRealtime?: (
75
- config: WorkerInitConfig,
76
- clientId: string,
77
- headers: () => Readonly<Record<string, string>>,
78
- ) => RealtimeConnector | undefined;
79
- /**
80
- * SPEC §12 local authority, required by init's `localAuthority: true`:
81
- * `(database, schema) => openLocalAuthority({ database:
82
- * clientSqliteDatabase(database), schema })` from `@syncular/server`. Only
83
- * a worker entry that passes it bundles the server code.
84
- */
85
- readonly openLocalAuthority?: (
86
- database: ClientDatabase,
87
- schema: ClientSchema,
88
- ) => Promise<WorkerLocalAuthority>;
89
- }
90
-
91
- /** The `LocalAuthority` surface of `@syncular/server` the worker drives (§12). */
92
- export interface WorkerLocalAuthority {
93
- readonly state: LocalAuthorityState;
94
- readonly sync: SyncTransport;
95
- readonly downloadSegment: SegmentDownloader;
96
- readonly attach: (transport: SyncTransport) => Promise<void>;
97
- readonly abandonAttach: (transport?: SyncTransport) => Promise<void>;
98
- }
99
-
100
- /** §12: the worker's sibling file for the local authority. */
101
- const LOCAL_AUTHORITY_SIBLING = 'local-authority';
102
-
103
- /** Minimal structural view of the dedicated-worker global scope. */
104
- interface WorkerScope {
105
- postMessage(message: unknown, transfer?: Transferable[]): void;
106
- addEventListener(
107
- type: 'message',
108
- listener: (event: MessageEvent) => void,
109
- ): void;
110
- }
111
-
112
- function toErrorShape(caught: unknown): WorkerErrorShape {
113
- const error = classifySqliteFailure(caught).error;
114
- if (error instanceof ClientSyncError) {
115
- return {
116
- code: error.code,
117
- message: error.message,
118
- retryable: error.retryable,
119
- ...(error.details !== undefined ? { details: error.details } : {}),
120
- };
121
- }
122
- return {
123
- code: WORKER_FAILED_CODE,
124
- message: error instanceof Error ? error.message : String(error),
125
- retryable: false,
126
- };
127
- }
128
-
129
- /**
130
- * ArrayBuffers safe to transfer out of a query result: whole-buffer
131
- * views only (a view into a larger/pooled buffer is cloned instead —
132
- * transferring would detach unrelated data), deduplicated because a
133
- * buffer may only appear once in a transfer list.
134
- */
135
- function queryTransferables(rows: readonly unknown[]): Transferable[] {
136
- const buffers = new Set<ArrayBuffer>();
137
- for (const row of rows) {
138
- if (row === null || typeof row !== 'object') continue;
139
- for (const value of Object.values(row)) {
140
- if (
141
- value instanceof Uint8Array &&
142
- value.buffer instanceof ArrayBuffer &&
143
- value.byteOffset === 0 &&
144
- value.byteLength === value.buffer.byteLength
145
- ) {
146
- buffers.add(value.buffer);
147
- }
148
- }
149
- }
150
- return [...buffers];
151
- }
4
+ export type { SyncWorkerOverrides, WorkerLocalAuthority } from './worker-core';
152
5
 
153
- /**
154
- * Register the RPC server on the current worker scope. Call this once
155
- * from a worker entry script; the main-thread handle drives everything
156
- * else through `init`/`call` messages.
157
- */
6
+ /** Start a worker that supports the optional local authority (§12). */
158
7
  export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
159
- const scope = globalThis as unknown as WorkerScope;
160
- const post = (message: WorkerToMainMessage, transfer?: Transferable[]) => {
161
- if (transfer !== undefined && transfer.length > 0) {
162
- scope.postMessage(message, transfer);
163
- } else {
164
- scope.postMessage(message);
165
- }
166
- };
167
-
168
- let client: SyncClient | undefined;
169
- let database: ClientDatabase | undefined;
170
- let authority: WorkerLocalAuthority | undefined;
171
- let authoritySibling: SiblingDatabase | undefined;
172
- let remoteTransport: SyncTransport | undefined;
173
- let offline = false;
174
- let headers: Readonly<Record<string, string>> = {};
175
- let closed = false;
176
-
177
- // -- one sync loop: RPC-driven and auto-driven rounds serialize ----------
178
- let syncChain: Promise<unknown> = Promise.resolve();
179
- function serializedSync<T>(fn: () => Promise<T>): Promise<T> {
180
- const next = syncChain.then(fn, fn);
181
- syncChain = next.then(
182
- () => undefined,
183
- () => undefined,
184
- );
185
- return next;
186
- }
187
-
188
- // -- SPEC §7.5 host loop: event-driven intents, no interactive polling --
189
- let autoSync = true;
190
- let autoSyncScheduled = false;
191
- let backgroundTimer: ReturnType<typeof setTimeout> | undefined;
192
- let backgroundDue = Number.POSITIVE_INFINITY;
193
-
194
- function runAutoSync(): void {
195
- if (
196
- closed ||
197
- client === undefined ||
198
- offline ||
199
- client.securityLifecycle() === 'preflight'
200
- ) {
201
- autoSyncScheduled = false;
202
- return;
203
- }
204
- const running = client;
205
- void serializedSync(() => {
206
- // The queued round owns the flag from here. Clearing it at entry would
207
- // let every wake-up that arrives while this round waits behind the
208
- // running one queue its own full drive (§8.4 coalescing).
209
- autoSyncScheduled = false;
210
- return running.syncUntilIdle();
211
- })
212
- .then((summary) => {
213
- if (!closed) post({ t: 'event', event: { kind: 'synced', summary } });
214
- if (summary.budgetExhausted) consumeSyncIntent({ kind: 'interactive' });
215
- })
216
- .catch((error: unknown) => {
217
- if (!closed) {
218
- post({
219
- t: 'event',
220
- event: { kind: 'synced', error: toErrorShape(error) },
221
- });
222
- }
223
- });
224
- }
225
-
226
- function consumeSyncIntent(intent: SyncIntent): void {
227
- if (
228
- !autoSync ||
229
- closed ||
230
- client === undefined ||
231
- offline ||
232
- client.securityLifecycle() === 'preflight' ||
233
- intent.kind === 'none'
234
- ) {
235
- return;
236
- }
237
- if (intent.kind === 'background') {
238
- if (autoSyncScheduled) return;
239
- const due = Date.now() + Math.max(0, intent.delayMs);
240
- if (backgroundTimer !== undefined && due >= backgroundDue) return;
241
- if (backgroundTimer !== undefined) clearTimeout(backgroundTimer);
242
- backgroundDue = due;
243
- backgroundTimer = setTimeout(
244
- () => {
245
- backgroundTimer = undefined;
246
- backgroundDue = Number.POSITIVE_INFINITY;
247
- if (!autoSyncScheduled) {
248
- autoSyncScheduled = true;
249
- runAutoSync();
250
- }
251
- },
252
- Math.max(0, due - Date.now()),
253
- );
254
- return;
255
- }
256
- if (backgroundTimer !== undefined) {
257
- clearTimeout(backgroundTimer);
258
- backgroundTimer = undefined;
259
- backgroundDue = Number.POSITIVE_INFINITY;
260
- }
261
- if (!autoSync || autoSyncScheduled || closed || client === undefined) {
262
- return;
263
- }
264
- autoSyncScheduled = true;
265
- queueMicrotask(runAutoSync);
266
- }
267
-
268
- /**
269
- * §12.3: attach or abandon with the client's rounds held, so no round
270
- * reaches the authority while it uploads or rolls back.
271
- */
272
- async function driveLocalAuthority(
273
- run: (local: WorkerLocalAuthority, remote: SyncTransport) => Promise<void>,
274
- ): Promise<void> {
275
- const running = requireClient();
276
- const local = authority;
277
- const remote = remoteTransport;
278
- if (local === undefined || remote === undefined) {
279
- throw new ClientSyncError(
280
- LOCAL_AUTHORITY_UNCONFIGURED_CODE,
281
- 'init did not enable the local authority',
282
- );
283
- }
284
- running.setTransportEnabled(false);
285
- try {
286
- await serializedSync(() => run(local, remote));
287
- } catch (error) {
288
- // `sync.attach_rejected` keeps its code and details across the RPC.
289
- if (
290
- error instanceof Error &&
291
- !(error instanceof ClientSyncError) &&
292
- 'code' in error &&
293
- typeof error.code === 'string'
294
- ) {
295
- throw new ClientSyncError(
296
- error.code,
297
- error.message,
298
- false,
299
- 'details' in error &&
300
- typeof error.details === 'object' &&
301
- error.details !== null
302
- ? { ...error.details }
303
- : undefined,
304
- );
305
- }
306
- throw error;
307
- } finally {
308
- running.setTransportEnabled(!offline);
309
- }
310
- consumeSyncIntent({ kind: 'interactive' });
311
- }
312
-
313
- function requireClient(): SyncClient {
314
- if (client === undefined) {
315
- throw new ClientSyncError(
316
- WORKER_FAILED_CODE,
317
- 'the worker received a call before init completed',
318
- );
319
- }
320
- return client;
321
- }
322
-
323
- async function init(config: WorkerInitConfig): Promise<WorkerInitResult> {
324
- if (config.authorityReads !== undefined && !overrides.createAuthorityReads)
325
- throw new ClientSyncError(
326
- 'client.authority_read_forbidden',
327
- 'authorityReads requires startSyncWorker({ createAuthorityReads })',
328
- );
329
- if (client !== undefined) {
330
- throw new ClientSyncError(
331
- WORKER_FAILED_CODE,
332
- 'the worker is already initialized',
333
- );
334
- }
335
- autoSync = config.autoSync ?? true;
336
- headers = config.headers ?? {};
337
- const http = { headers: () => headers };
338
-
339
- for (let attempt = 0; ; attempt++) {
340
- try {
341
- database =
342
- overrides.openDatabase !== undefined
343
- ? await overrides.openDatabase(config)
344
- : await defaultOpenDatabase(config);
345
- break;
346
- } catch (error) {
347
- if (
348
- config.database.mode !== 'persistent' ||
349
- !(error instanceof ClientSyncError) ||
350
- error.code !== STORAGE_BUSY_CODE ||
351
- !error.retryable ||
352
- attempt === 6
353
- ) {
354
- throw error;
355
- }
356
- // Retain the leader lease while the previous worker's OPFS handles
357
- // drain. Only opening is retried; core initialization has not started.
358
- const delayMs = Math.min(50 * 2 ** attempt, 1000);
359
- if (overrides.waitForStorageRetry !== undefined) {
360
- await overrides.waitForStorageRetry(delayMs);
361
- } else {
362
- await new Promise<void>((resolve) => setTimeout(resolve, delayMs));
363
- }
364
- }
365
- }
366
- const remote =
367
- overrides.createTransport !== undefined
368
- ? overrides.createTransport(config)
369
- : httpSyncTransport(config.endpoints.syncUrl, http);
370
- remoteTransport = remote;
371
- const remoteSegments =
372
- overrides.createSegments !== undefined
373
- ? overrides.createSegments(config)
374
- : config.endpoints.segmentsUrl !== undefined
375
- ? httpSegmentDownloader(config.endpoints.segmentsUrl, http)
376
- : undefined;
377
- if (config.localAuthority === true) {
378
- if (overrides.openLocalAuthority === undefined) {
379
- throw new ClientSyncError(
380
- LOCAL_AUTHORITY_UNCONFIGURED_CODE,
381
- 'localAuthority requires startSyncWorker({ openLocalAuthority })',
382
- );
383
- }
384
- authoritySibling = database.openSibling?.(LOCAL_AUTHORITY_SIBLING);
385
- if (authoritySibling === undefined) {
386
- throw new ClientSyncError(
387
- LOCAL_AUTHORITY_UNCONFIGURED_CODE,
388
- 'localAuthority requires a database with sibling files',
389
- );
390
- }
391
- authority = await overrides.openLocalAuthority(
392
- authoritySibling.database,
393
- config.schema,
394
- );
395
- }
396
- // §12.4: rounds reach the local authority until attach completes, then
397
- // the remote server; the replica rebinds through the §2.1 epoch reset.
398
- const localActive = () =>
399
- authority !== undefined && authority.state !== 'attached';
400
- const transport: SyncTransport = (request) =>
401
- localActive() ? authority!.sync(request) : remote(request);
402
- const segments: SegmentDownloader | undefined =
403
- authority === undefined
404
- ? remoteSegments
405
- : (request) => {
406
- if (localActive()) return authority!.downloadSegment(request);
407
- if (remoteSegments === undefined) {
408
- throw new ClientSyncError(
409
- 'sync.invalid_request',
410
- 'no segment endpoint configured',
411
- );
412
- }
413
- return remoteSegments(request);
414
- };
415
- // §5.9 blob transport: only when a blobs URL is configured.
416
- const blobs =
417
- config.endpoints.blobsUrl !== undefined
418
- ? httpBlobTransport(config.endpoints.blobsUrl, http)
419
- : undefined;
420
-
421
- // Realtime needs the (possibly persisted) clientId for the
422
- // `{clientId}` URL placeholder — start the client first, then attach
423
- // the connector through a mutable slot read at connect time.
424
- let realtimeConnector: RealtimeConnector | undefined;
425
- offline = config.transportEnabled === false;
426
- const started = new SyncClient({
427
- transportEnabled: !offline,
428
- database,
429
- schema: config.schema,
430
- transport,
431
- ...(segments !== undefined ? { segments } : {}),
432
- ...(blobs !== undefined ? { blobs } : {}),
433
- realtime: (handlers) => {
434
- // §12.2: a local authority has no realtime channel.
435
- if (realtimeConnector === undefined || localActive()) {
436
- throw new ClientSyncError(
437
- 'sync.invalid_request',
438
- 'no realtime connector configured',
439
- );
440
- }
441
- return realtimeConnector(handlers);
442
- },
443
- ...(config.clientId !== undefined ? { clientId: config.clientId } : {}),
444
- ...(config.limits !== undefined ? { limits: config.limits } : {}),
445
- ...(config.retainFailedCommits !== undefined
446
- ? { retainFailedCommits: config.retainFailedCommits }
447
- : {}),
448
- ...(config.realtimePolicy !== undefined
449
- ? { realtimePolicy: config.realtimePolicy }
450
- : {}),
451
- ...(config.encryption !== undefined
452
- ? { encryption: encryptionConfigFromKeyring(config.encryption) }
453
- : {}),
454
- ...(config.authorityReads !== undefined
455
- ? {
456
- authorityReads: overrides.createAuthorityReads!(
457
- config.authorityReads,
458
- ),
459
- }
460
- : {}),
461
- ...(config.securityPreflight !== undefined
462
- ? { securityPreflight: config.securityPreflight }
463
- : {}),
464
- ...(config.previousVersionContext !== undefined
465
- ? { previousVersionContext: config.previousVersionContext }
466
- : {}),
467
- onSyncNeeded: (reason) => {
468
- post({ t: 'event', event: { kind: 'sync-needed', reason } });
469
- consumeSyncIntent({ kind: 'interactive' });
470
- },
471
- onSyncIntent: consumeSyncIntent,
472
- onConflict: (conflict) => {
473
- post({ t: 'event', event: { kind: 'conflict', conflict } });
474
- },
475
- onUpgrading: (upgrading) => {
476
- // §7.4.5: surface the schema-bump reset + completion to the UI thread.
477
- post({ t: 'event', event: { kind: 'upgrading', upgrading } });
478
- },
479
- onPresence: (scopeKey) => {
480
- // §8.6: surface a presence change to the UI thread.
481
- post({ t: 'event', event: { kind: 'presence', scopeKey } });
482
- },
483
- });
484
- started.onProgress((progress) => {
485
- if (!closed) post({ t: 'event', event: { kind: 'progress', progress } });
486
- });
487
- started.onChange((batch) => {
488
- if (!closed) post({ t: 'event', event: { kind: 'change', batch } });
489
- });
490
- const postDiagnostics = (snapshot: ClientDiagnosticsSnapshot): void => {
491
- if (closed) return;
492
- post({
493
- t: 'event',
494
- event: {
495
- kind: 'diagnostics',
496
- snapshot: withClientDiagnosticsHost(snapshot, {
497
- kind: 'worker',
498
- role: 'leader',
499
- connectivity: offline ? 'offline' : snapshot.host.connectivity,
500
- realtime: snapshot.host.realtime,
501
- }),
502
- },
503
- });
504
- };
505
- started.onDiagnostics(postDiagnostics);
506
- await started.start();
507
- realtimeConnector =
508
- overrides.createRealtime !== undefined
509
- ? overrides.createRealtime(config, started.clientId, http.headers)
510
- : config.endpoints.realtimeUrl !== undefined
511
- ? webSocketRealtimeConnector(
512
- config.endpoints.realtimeUrl.replace(
513
- '{clientId}',
514
- encodeURIComponent(started.clientId),
515
- ),
516
- )
517
- : undefined;
518
- client = started;
519
- // `start()` may discover persisted subscriptions/outbox work. Its callback
520
- // fires before this worker publishes the initialized client, so consume
521
- // the durable state once here as well; coalescing makes this a single task.
522
- if (started.statusSnapshot().syncNeeded)
523
- consumeSyncIntent({ kind: 'interactive' });
524
- return { clientId: started.clientId };
525
- }
526
-
527
- // Forward every argument to core methods, including authority's zero-argument check.
528
- const coreMethods = [
529
- 'securityLifecycle',
530
- 'subscribe',
531
- 'subscribeMany',
532
- 'unsubscribe',
533
- 'windowState',
534
- 'purgeLocalData',
535
- 'rebootstrapLocalData',
536
- 'previousVersionSnapshot',
537
- 'previousVersionAudit',
538
- 'previousVersionDiscard',
539
- 'query',
540
- 'authoritySnapshot',
541
- 'querySnapshot',
542
- 'snapshotRead',
543
- 'statusSnapshot',
544
- 'conflicts',
545
- 'rejections',
546
- 'commitOutcome',
547
- 'commitOutcomes',
548
- 'resolveCommitOutcome',
549
- 'pendingCommits',
550
- 'subscriptions',
551
- 'subscription',
552
- 'connectRealtime',
553
- 'disconnectRealtime',
554
- 'setPresence',
555
- 'presence',
556
- 'uploadBlob',
557
- 'fetchBlob',
558
- ] as const satisfies readonly (keyof WorkerApi & keyof SyncClient)[];
559
- const api: WorkerApi = {
560
- ...(Object.fromEntries(
561
- coreMethods.map((method) => [
562
- method,
563
- (...args: unknown[]) => {
564
- const running = requireClient();
565
- return Reflect.apply(running[method], running, args);
566
- },
567
- ]),
568
- ) as Pick<WorkerApi, (typeof coreMethods)[number]>),
569
- beginSecurityPreflight: async () => {
570
- if (backgroundTimer !== undefined) clearTimeout(backgroundTimer);
571
- backgroundTimer = undefined;
572
- backgroundDue = Number.POSITIVE_INFINITY;
573
- autoSyncScheduled = false;
574
- await requireClient().beginSecurityPreflight();
575
- },
576
- activateSecurity: async (options = {}) => {
577
- await requireClient().activateSecurity({
578
- ...(options.encryption !== undefined
579
- ? { encryption: encryptionConfigFromKeyring(options.encryption) }
580
- : {}),
581
- });
582
- },
583
- setWindow: async (base, units) => {
584
- await requireClient().setWindowCommand(base, units);
585
- },
586
- mutate: (mutations) => {
587
- return requireClient().mutateCommand(mutations).value;
588
- },
589
- patch: (table, rowId, partial, options) => {
590
- // Same §8.4 rule as `mutate`: a local write must push without the app
591
- // orchestrating sync, so consume the core's immediate intent.
592
- const result = requireClient().patchCommand(
593
- table,
594
- rowId,
595
- partial,
596
- options,
597
- );
598
- return result.value;
599
- },
600
- sync: () => {
601
- const running = requireClient();
602
- return serializedSync(() => running.sync());
603
- },
604
- syncUntilIdle: (options) => {
605
- const running = requireClient();
606
- return serializedSync(() => running.syncUntilIdle(options));
607
- },
608
- proveCurrentBase: (outcome, options) => {
609
- const running = requireClient();
610
- return serializedSync(() => running.proveCurrentBase(outcome, options));
611
- },
612
- resetClientId: () => requireClient().resetClientId(),
613
- localRevision: () => requireClient().localRevision,
614
- diagnosticsSnapshot: (request) => {
615
- const snapshot = requireClient().diagnosticsSnapshot(request);
616
- return withClientDiagnosticsHost(snapshot, {
617
- kind: 'worker',
618
- role: 'leader',
619
- connectivity: offline ? 'offline' : snapshot.host.connectivity,
620
- realtime: snapshot.host.realtime,
621
- });
622
- },
623
- setHeaders: (next) => {
624
- headers = next;
625
- },
626
- setOffline: (value) => {
627
- offline = value;
628
- requireClient().setTransportEnabled(!value);
629
- if (offline) {
630
- if (backgroundTimer !== undefined) clearTimeout(backgroundTimer);
631
- backgroundTimer = undefined;
632
- backgroundDue = Number.POSITIVE_INFINITY;
633
- } else if (client !== undefined) {
634
- const snapshot = client.diagnosticsSnapshot();
635
- post({
636
- t: 'event',
637
- event: {
638
- kind: 'diagnostics',
639
- snapshot: withClientDiagnosticsHost(snapshot, {
640
- kind: 'worker',
641
- role: 'leader',
642
- connectivity: snapshot.host.connectivity,
643
- realtime: snapshot.host.realtime,
644
- }),
645
- },
646
- });
647
- }
648
- },
649
- localAuthorityState: () => authority?.state,
650
- attachLocalAuthority: () =>
651
- driveLocalAuthority((local, remote) => local.attach(remote)),
652
- abandonLocalAuthorityAttach: () =>
653
- driveLocalAuthority((local, remote) => local.abandonAttach(remote)),
654
- close: async () => {
655
- closed = true;
656
- if (backgroundTimer !== undefined) clearTimeout(backgroundTimer);
657
- await client?.close();
658
- authoritySibling?.close();
659
- database?.close();
660
- client = undefined;
661
- database = undefined;
662
- },
663
- };
664
-
665
- // Local purge/rebootstrap/discard rewrite the same durable state an
666
- // in-flight sync round captured at send time, and the RFC 0005 reads observe
667
- // (and can themselves trigger the discard of) that same previous-version
668
- // container. Running all of them on the sync chain orders them against RPC-
669
- // and auto-driven rounds (the client core's reset fence covers hosts that
670
- // call the core directly).
671
- const syncChainMethods: ReadonlySet<WorkerMethod> = new Set<WorkerMethod>([
672
- 'purgeLocalData',
673
- 'rebootstrapLocalData',
674
- 'previousVersionSnapshot',
675
- 'previousVersionAudit',
676
- 'previousVersionDiscard',
677
- ]);
678
-
679
- async function dispatch(message: WorkerCallMessage): Promise<unknown> {
680
- const method = api[message.method] as (...args: unknown[]) => unknown;
681
- const invoke = () => method.apply(api, message.args as unknown[]);
682
- if (syncChainMethods.has(message.method)) {
683
- return await serializedSync(async () => invoke());
684
- }
685
- return await invoke();
686
- }
687
-
688
- function run(
689
- id: number,
690
- fn: () => Promise<unknown>,
691
- transfer?: (value: unknown) => Transferable[],
692
- ): void {
693
- void (async () => {
694
- try {
695
- const value = await fn();
696
- post(
697
- { t: 'result', id, value },
698
- transfer !== undefined ? transfer(value) : undefined,
699
- );
700
- } catch (error) {
701
- post({ t: 'error', id, error: toErrorShape(error) });
702
- }
703
- })();
704
- }
705
-
706
- scope.addEventListener('message', (event) => {
707
- const message = event.data as MainToWorkerMessage;
708
- if (message.t === 'init') {
709
- run(message.id, () => init(message.config));
710
- return;
711
- }
712
- if (message.t === 'call') {
713
- run(
714
- message.id,
715
- () => dispatch(message),
716
- message.method === 'query'
717
- ? (value) => queryTransferables(value as readonly unknown[])
718
- : message.method === 'querySnapshot'
719
- ? (value) =>
720
- queryTransferables(
721
- (value as { readonly rows: readonly unknown[] }).rows,
722
- )
723
- : undefined,
724
- );
725
- }
726
- });
727
-
728
- post({ t: 'ready' });
729
- }
730
-
731
- async function defaultOpenDatabase(
732
- config: WorkerInitConfig,
733
- ): Promise<ClientDatabase> {
734
- if (config.database.mode !== 'persistent') {
735
- throw new ClientSyncError(
736
- WORKER_FAILED_CODE,
737
- "database mode 'custom' requires an openDatabase override in the " +
738
- 'worker bootstrap (startSyncWorker({ openDatabase }))',
739
- );
740
- }
741
- return openPersistentWasmDatabase(config.database.name, {
742
- ...(config.database.directory !== undefined
743
- ? { directory: config.database.directory }
744
- : {}),
745
- ...(config.takeOverStorage === true ? { takeOver: true } : {}),
746
- });
8
+ startSyncWorkerCore(overrides, createWorkerLocalAuthority);
747
9
  }