@syncular/client 0.53.0 → 0.54.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.
@@ -49,7 +49,7 @@ import type {
49
49
  WindowState,
50
50
  } from './client';
51
51
  import type { SqlRow, SqlValue } from './database';
52
- import { registerDevtools } from './devtools';
52
+ import type { DevtoolsRegistrar } from './devtools';
53
53
  import {
54
54
  ClientDiagnosticsEmitter,
55
55
  type ClientDiagnosticsListener,
@@ -178,6 +178,8 @@ export function isolatedReplicaNames(options: {
178
178
  }
179
179
 
180
180
  export interface SyncClientHandleConfig {
181
+ /** Explicit development console registry for this page's handle. */
182
+ readonly devtools?: DevtoolsRegistrar;
181
183
  /**
182
184
  * Spawns the worker running `startSyncWorker()` (a factory so bundlers
183
185
  * see `new Worker(new URL(...))` at the call site, and so no worker is
@@ -337,7 +339,7 @@ export class SyncClientHandle implements PromiseMethods<WorkerApi> {
337
339
  readonly #roleListeners: Set<(role: HandleRole) => void>;
338
340
  readonly #leadershipListeners: Set<(state: LeadershipState) => void>;
339
341
  readonly #unreachableListeners: Set<(event: LeaderUnreachableEvent) => void>;
340
- readonly #devtoolsUnregister: () => void;
342
+ readonly #devtoolsUnregister: (() => void) | undefined;
341
343
  #closed = false;
342
344
 
343
345
  /** @internal — use {@link createSyncClientHandle}. */
@@ -355,6 +357,7 @@ export class SyncClientHandle implements PromiseMethods<WorkerApi> {
355
357
  leadershipListeners?: Set<(state: LeadershipState) => void>;
356
358
  unreachableListeners?: Set<(event: LeaderUnreachableEvent) => void>;
357
359
  leadership?: LeadershipState;
360
+ devtools?: DevtoolsRegistrar;
358
361
  }) {
359
362
  this.#role = internals.role;
360
363
  this.#clientId = internals.clientId;
@@ -380,7 +383,7 @@ export class SyncClientHandle implements PromiseMethods<WorkerApi> {
380
383
  this.#leadershipListeners = internals.leadershipListeners ?? new Set();
381
384
  this.#unreachableListeners = internals.unreachableListeners ?? new Set();
382
385
  // Console introspection is a no-op outside a dev page.
383
- this.#devtoolsUnregister = registerDevtools({
386
+ this.#devtoolsUnregister = internals.devtools?.({
384
387
  kind: 'handle',
385
388
  ref: this,
386
389
  clientId: () => this.#clientId,
@@ -832,7 +835,7 @@ export class SyncClientHandle implements PromiseMethods<WorkerApi> {
832
835
  if (this.#closed) return;
833
836
  this.#closed = true;
834
837
  this.#progress.clear();
835
- this.#devtoolsUnregister();
838
+ this.#devtoolsUnregister?.();
836
839
  await this.#tabs?.close();
837
840
  if (this.#core !== undefined) {
838
841
  const core = this.#core;
@@ -1076,6 +1079,9 @@ export async function createSyncClientHandle(
1076
1079
  roleListeners,
1077
1080
  leadershipListeners,
1078
1081
  unreachableListeners,
1082
+ ...(resolvedConfig.devtools !== undefined
1083
+ ? { devtools: resolvedConfig.devtools }
1084
+ : {}),
1079
1085
  };
1080
1086
 
1081
1087
  if (resolvedConfig.multiTab === false) {
@@ -0,0 +1,106 @@
1
+ import type { SiblingDatabase } from './database';
2
+ import { ClientSyncError } from './errors';
3
+ import type { SyncTransport } from './transport';
4
+ import type {
5
+ WorkerLocalAuthority,
6
+ WorkerLocalAuthorityFactory,
7
+ } from './worker-core';
8
+ import { LOCAL_AUTHORITY_UNCONFIGURED_CODE } from './worker-protocol';
9
+
10
+ /** §12 behavior loaded by the authority-enabled worker entry. */
11
+ export const createWorkerLocalAuthority: WorkerLocalAuthorityFactory = (
12
+ context,
13
+ ) => {
14
+ let authority: WorkerLocalAuthority | undefined;
15
+ let sibling: SiblingDatabase | undefined;
16
+ let remoteTransport: SyncTransport | undefined;
17
+ const active = () =>
18
+ authority !== undefined && authority.state !== 'attached';
19
+
20
+ async function drive(
21
+ run: (local: WorkerLocalAuthority, remote: SyncTransport) => Promise<void>,
22
+ ): Promise<void> {
23
+ const running = context.requireClient();
24
+ const local = authority;
25
+ const remote = remoteTransport;
26
+ if (local === undefined || remote === undefined) {
27
+ throw new ClientSyncError(
28
+ LOCAL_AUTHORITY_UNCONFIGURED_CODE,
29
+ 'init did not enable the local authority',
30
+ );
31
+ }
32
+ running.setTransportEnabled(false);
33
+ try {
34
+ await context.serializedSync(() => run(local, remote));
35
+ } catch (error) {
36
+ if (
37
+ error instanceof Error &&
38
+ !(error instanceof ClientSyncError) &&
39
+ 'code' in error &&
40
+ typeof error.code === 'string'
41
+ ) {
42
+ throw new ClientSyncError(
43
+ error.code,
44
+ error.message,
45
+ false,
46
+ 'details' in error &&
47
+ typeof error.details === 'object' &&
48
+ error.details !== null
49
+ ? { ...error.details }
50
+ : undefined,
51
+ );
52
+ }
53
+ throw error;
54
+ } finally {
55
+ running.setTransportEnabled(!context.isOffline());
56
+ }
57
+ context.wake();
58
+ }
59
+
60
+ return {
61
+ methods: {
62
+ localAuthorityState: () => authority?.state,
63
+ attachLocalAuthority: () =>
64
+ drive((local, remote) => local.attach(remote)),
65
+ abandonLocalAuthorityAttach: () =>
66
+ drive((local, remote) => local.abandonAttach(remote)),
67
+ },
68
+ async open(config, database, remote, segments) {
69
+ remoteTransport = remote;
70
+ if (context.overrides.openLocalAuthority === undefined) {
71
+ throw new ClientSyncError(
72
+ LOCAL_AUTHORITY_UNCONFIGURED_CODE,
73
+ 'localAuthority requires startSyncWorker({ openLocalAuthority })',
74
+ );
75
+ }
76
+ sibling = database.openSibling?.('local-authority');
77
+ if (sibling === undefined) {
78
+ throw new ClientSyncError(
79
+ LOCAL_AUTHORITY_UNCONFIGURED_CODE,
80
+ 'localAuthority requires a database with sibling files',
81
+ );
82
+ }
83
+ const local = await context.overrides.openLocalAuthority(
84
+ sibling.database,
85
+ config.schema,
86
+ );
87
+ authority = local;
88
+ return {
89
+ transport: (request) =>
90
+ active() ? local.sync(request) : remote(request),
91
+ segments: (request) => {
92
+ if (active()) return local.downloadSegment(request);
93
+ if (segments === undefined) {
94
+ throw new ClientSyncError(
95
+ 'sync.invalid_request',
96
+ 'no segment endpoint configured',
97
+ );
98
+ }
99
+ return segments(request);
100
+ },
101
+ };
102
+ },
103
+ active,
104
+ close: () => sibling?.close(),
105
+ };
106
+ };
@@ -66,6 +66,9 @@ export const NOT_LEADER_CODE = 'client.not_leader';
66
66
  export const WORKER_FAILED_CODE = 'client.worker_failed';
67
67
  /** A page/worker bundle identity changed and a full host reload is required. */
68
68
  export const WORKER_RESTART_REQUIRED_CODE = 'client.worker_restart_required';
69
+ /** The selected worker entry does not provide the requested method. */
70
+ export const WORKER_METHOD_UNAVAILABLE_CODE =
71
+ 'client.worker_method_unavailable';
69
72
  /** SPEC §12: local-authority use without `startSyncWorker({ openLocalAuthority })`. */
70
73
  export const LOCAL_AUTHORITY_UNCONFIGURED_CODE =
71
74
  'client.local_authority_unconfigured';
@@ -0,0 +1,14 @@
1
+ import { startSyncWorkerCore, type SyncWorkerOverrides } from './worker-core';
2
+
3
+ /** Worker overrides for a host that uses only a remote sync authority. */
4
+ export type RemoteSyncWorkerOverrides = Omit<
5
+ SyncWorkerOverrides,
6
+ 'openLocalAuthority'
7
+ >;
8
+
9
+ /** Start a worker without the optional local-authority lifecycle (§12). */
10
+ export function startSyncWorker(
11
+ overrides: RemoteSyncWorkerOverrides = {},
12
+ ): void {
13
+ startSyncWorkerCore(overrides);
14
+ }