@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,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
+ }