@syncular/client 0.48.0 → 0.49.2
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 +50 -1
- package/dist/availability.d.ts +1 -1
- package/dist/availability.js +1 -1
- package/dist/client.js +18 -3
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +5 -0
- package/dist/leader-lock.d.ts +18 -7
- package/dist/leader-lock.js +40 -22
- package/dist/multi-tab.d.ts +158 -22
- package/dist/multi-tab.js +361 -10
- package/dist/outbox.d.ts +5 -0
- package/dist/outbox.js +11 -3
- package/dist/outcomes.js +6 -2
- package/dist/previous-version.d.ts +3 -1
- package/dist/previous-version.js +24 -5
- package/dist/reactive-store.js +2 -1
- package/dist/wasm-database.d.ts +6 -0
- package/dist/wasm-database.js +36 -6
- package/dist/worker-entry.d.ts +17 -1
- package/dist/worker-entry.js +74 -4
- package/dist/worker-host.d.ts +45 -26
- package/dist/worker-host.js +172 -310
- package/dist/worker-protocol.d.ts +22 -0
- package/dist/worker-protocol.js +2 -0
- package/package.json +3 -3
- package/src/availability.ts +3 -2
- package/src/client.ts +26 -3
- package/src/errors.ts +6 -0
- package/src/leader-lock.ts +64 -34
- package/src/multi-tab.ts +576 -34
- package/src/outbox.ts +16 -2
- package/src/outcomes.ts +6 -2
- package/src/previous-version.ts +32 -5
- package/src/reactive-store.ts +2 -1
- package/src/wasm-database.ts +64 -6
- package/src/worker-entry.ts +124 -4
- package/src/worker-host.ts +219 -379
- package/src/worker-protocol.ts +24 -0
package/src/outbox.ts
CHANGED
|
@@ -40,6 +40,11 @@ export interface OutboxOperation {
|
|
|
40
40
|
* presence set — `mutate` marks every column, `patch` the supplied ones).
|
|
41
41
|
*/
|
|
42
42
|
readonly values?: Readonly<Record<string, JsonRowValue>>;
|
|
43
|
+
/**
|
|
44
|
+
* §7.1 insert intent: a `mutate` of a row absent from the replica. Replay
|
|
45
|
+
* and encoding treat the operation as a full row; a missing column is NULL.
|
|
46
|
+
*/
|
|
47
|
+
readonly insert?: true;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
export interface OutboxCommit {
|
|
@@ -205,6 +210,7 @@ export class OutboxEncodeError extends ClientSyncError {
|
|
|
205
210
|
function sparseValues(
|
|
206
211
|
table: CompiledClientTable,
|
|
207
212
|
values: Readonly<Record<string, JsonRowValue>>,
|
|
213
|
+
insert: boolean,
|
|
208
214
|
): SparseRowValue[] {
|
|
209
215
|
// Any persisted key that is not a column of the CURRENT schema means the
|
|
210
216
|
// bump removed (or renamed) it — the commit cannot be expressed now.
|
|
@@ -217,7 +223,15 @@ function sparseValues(
|
|
|
217
223
|
}
|
|
218
224
|
return table.columns.map((column) => {
|
|
219
225
|
const value = values[column.name];
|
|
220
|
-
|
|
226
|
+
if (value !== undefined) return jsonToRowValue(value);
|
|
227
|
+
if (!insert) return undefined;
|
|
228
|
+
// §7.1: an insert-intent operation presents every current column.
|
|
229
|
+
if (!column.nullable) {
|
|
230
|
+
throw new OutboxEncodeError(
|
|
231
|
+
'an outbox insert lacks a non-nullable column (§7.4.4)',
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
return null;
|
|
221
235
|
});
|
|
222
236
|
}
|
|
223
237
|
|
|
@@ -264,7 +278,7 @@ export async function encodeOutboxCommit(
|
|
|
264
278
|
`outbox upsert on ${op.table}/${op.rowId} has no values`,
|
|
265
279
|
);
|
|
266
280
|
}
|
|
267
|
-
let values = sparseValues(table, op.values);
|
|
281
|
+
let values = sparseValues(table, op.values, op.insert === true);
|
|
268
282
|
if (encryption !== undefined && table.hasEncryptedColumns) {
|
|
269
283
|
// Lazy: opt-in E2EE never enters an encryption-free app's bundle.
|
|
270
284
|
const { encryptRowValues } = await import('./encryption');
|
package/src/outcomes.ts
CHANGED
|
@@ -186,8 +186,12 @@ export function isStoredOperation(value: unknown): value is OutboxOperation {
|
|
|
186
186
|
typeof value.rowId === 'string' &&
|
|
187
187
|
(value.baseVersion === undefined ||
|
|
188
188
|
Number.isSafeInteger(value.baseVersion)) &&
|
|
189
|
-
((value.op === 'upsert' &&
|
|
190
|
-
(value.
|
|
189
|
+
((value.op === 'upsert' &&
|
|
190
|
+
isStoredRow(value.values) &&
|
|
191
|
+
(value.insert === undefined || value.insert === true)) ||
|
|
192
|
+
(value.op === 'delete' &&
|
|
193
|
+
value.values === undefined &&
|
|
194
|
+
value.insert === undefined))
|
|
191
195
|
);
|
|
192
196
|
}
|
|
193
197
|
|
package/src/previous-version.ts
CHANGED
|
@@ -167,7 +167,10 @@ type PreviousVersionCaptureOutcome =
|
|
|
167
167
|
interface PreviousVersionAuditEntry {
|
|
168
168
|
readonly commitId: string;
|
|
169
169
|
readonly table: string;
|
|
170
|
-
readonly reason:
|
|
170
|
+
readonly reason:
|
|
171
|
+
| 'unknown-table'
|
|
172
|
+
| 'unknown-column'
|
|
173
|
+
| 'missing-required-column';
|
|
171
174
|
/** The offending column; present for every reason except `unknown-table`. */
|
|
172
175
|
readonly column?: string;
|
|
173
176
|
}
|
|
@@ -761,7 +764,9 @@ interface PendingCommitLike {
|
|
|
761
764
|
readonly clientCommitId: string;
|
|
762
765
|
readonly operations: readonly {
|
|
763
766
|
readonly table: string;
|
|
767
|
+
readonly op: 'upsert' | 'delete';
|
|
764
768
|
readonly values?: Readonly<Record<string, JsonRowValue>>;
|
|
769
|
+
readonly insert?: true;
|
|
765
770
|
}[];
|
|
766
771
|
}
|
|
767
772
|
|
|
@@ -807,6 +812,9 @@ function firstIncompatibility(
|
|
|
807
812
|
commit: PendingCommitLike,
|
|
808
813
|
): PreviousVersionAuditEntry | undefined {
|
|
809
814
|
for (const operation of commit.operations) {
|
|
815
|
+
// §7.4.4: a value-free delete stays encodable and takes ordinary server
|
|
816
|
+
// validation, even against a removed table.
|
|
817
|
+
if (operation.op === 'delete') continue;
|
|
810
818
|
const table = schema.tables.get(operation.table);
|
|
811
819
|
if (table === undefined) {
|
|
812
820
|
return {
|
|
@@ -815,8 +823,7 @@ function firstIncompatibility(
|
|
|
815
823
|
reason: 'unknown-table',
|
|
816
824
|
};
|
|
817
825
|
}
|
|
818
|
-
|
|
819
|
-
for (const key of Object.keys(operation.values)) {
|
|
826
|
+
for (const key of Object.keys(operation.values ?? {})) {
|
|
820
827
|
if (!table.columnIndex.has(key)) {
|
|
821
828
|
return {
|
|
822
829
|
commitId: commit.clientCommitId,
|
|
@@ -826,6 +833,22 @@ function firstIncompatibility(
|
|
|
826
833
|
};
|
|
827
834
|
}
|
|
828
835
|
}
|
|
836
|
+
// §7.1: an insert-intent operation presents every current column, so a
|
|
837
|
+
// non-nullable column it lacks leaves the commit unencodable.
|
|
838
|
+
if (operation.insert === true) {
|
|
839
|
+
const missing = table.columns.find(
|
|
840
|
+
(column) =>
|
|
841
|
+
!column.nullable && operation.values?.[column.name] === undefined,
|
|
842
|
+
);
|
|
843
|
+
if (missing !== undefined) {
|
|
844
|
+
return {
|
|
845
|
+
commitId: commit.clientCommitId,
|
|
846
|
+
table: operation.table,
|
|
847
|
+
reason: 'missing-required-column',
|
|
848
|
+
column: missing.name,
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
}
|
|
829
852
|
}
|
|
830
853
|
return undefined;
|
|
831
854
|
}
|
|
@@ -859,7 +882,9 @@ export function storedPreviousVersionAudit(
|
|
|
859
882
|
if (
|
|
860
883
|
typeof item.commitId !== 'string' ||
|
|
861
884
|
typeof item.table !== 'string' ||
|
|
862
|
-
(item.reason !== 'unknown-table' &&
|
|
885
|
+
(item.reason !== 'unknown-table' &&
|
|
886
|
+
item.reason !== 'unknown-column' &&
|
|
887
|
+
item.reason !== 'missing-required-column') ||
|
|
863
888
|
(item.column !== undefined && typeof item.column !== 'string')
|
|
864
889
|
) {
|
|
865
890
|
return undefined;
|
|
@@ -1009,7 +1034,9 @@ export function decodePreviousVersionAuditResult(
|
|
|
1009
1034
|
) ||
|
|
1010
1035
|
typeof item.commitId !== 'string' ||
|
|
1011
1036
|
typeof item.table !== 'string' ||
|
|
1012
|
-
(item.reason !== 'unknown-table' &&
|
|
1037
|
+
(item.reason !== 'unknown-table' &&
|
|
1038
|
+
item.reason !== 'unknown-column' &&
|
|
1039
|
+
item.reason !== 'missing-required-column') ||
|
|
1013
1040
|
(item.column !== undefined && typeof item.column !== 'string')
|
|
1014
1041
|
) {
|
|
1015
1042
|
return invalidHostResponse(method);
|
package/src/reactive-store.ts
CHANGED
|
@@ -377,7 +377,8 @@ function localReadsBlocked(availability: SyncAvailability): boolean {
|
|
|
377
377
|
return (
|
|
378
378
|
availability.state === 'blocked' &&
|
|
379
379
|
(availability.reason === 'leader-unreachable' ||
|
|
380
|
-
availability.reason === 'leader-incompatible'
|
|
380
|
+
availability.reason === 'leader-incompatible' ||
|
|
381
|
+
availability.reason === 'leadership-taken')
|
|
381
382
|
);
|
|
382
383
|
}
|
|
383
384
|
|
package/src/wasm-database.ts
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
ClientSyncError,
|
|
37
37
|
invalidRequest,
|
|
38
38
|
STORAGE_BUSY_CODE,
|
|
39
|
+
STORAGE_FENCED_CODE,
|
|
39
40
|
STORAGE_UNAVAILABLE_CODE,
|
|
40
41
|
} from './errors';
|
|
41
42
|
|
|
@@ -121,6 +122,15 @@ function coerceParams(params: readonly SqlValue[]): unknown[] {
|
|
|
121
122
|
});
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Set when another worker took the pool's storage lock. Shared by a database
|
|
127
|
+
* and its siblings: every one of them refuses statements from then on.
|
|
128
|
+
*/
|
|
129
|
+
interface StorageFence {
|
|
130
|
+
fenced: boolean;
|
|
131
|
+
readonly siblings: Set<Oo1Database>;
|
|
132
|
+
}
|
|
133
|
+
|
|
124
134
|
class WasmClientDatabase implements ClientDatabase {
|
|
125
135
|
readonly #db: Oo1Database;
|
|
126
136
|
readonly #sqlite3: Sqlite3Static;
|
|
@@ -128,6 +138,7 @@ class WasmClientDatabase implements ClientDatabase {
|
|
|
128
138
|
readonly #sah:
|
|
129
139
|
| { readonly util: SahPoolUtil; readonly lease: LeaderLease }
|
|
130
140
|
| undefined;
|
|
141
|
+
readonly #fence: StorageFence | undefined;
|
|
131
142
|
#closed = false;
|
|
132
143
|
#tx = { depth: 0 };
|
|
133
144
|
|
|
@@ -135,13 +146,35 @@ class WasmClientDatabase implements ClientDatabase {
|
|
|
135
146
|
db: Oo1Database,
|
|
136
147
|
sqlite3: Sqlite3Static,
|
|
137
148
|
sah?: { readonly util: SahPoolUtil; readonly lease: LeaderLease },
|
|
149
|
+
fence?: StorageFence,
|
|
138
150
|
) {
|
|
139
151
|
this.#db = db;
|
|
140
152
|
this.#sqlite3 = sqlite3;
|
|
141
153
|
this.#sah = sah;
|
|
154
|
+
this.#fence = fence;
|
|
155
|
+
// A takeover stole the storage lock. The new owner cannot open the pool
|
|
156
|
+
// while these SAHs are held, so close them before any further statement:
|
|
157
|
+
// the lost lease runs as its own task, never inside a transaction.
|
|
158
|
+
void sah?.lease.lost?.then(() => {
|
|
159
|
+
if (fence === undefined || this.#closed) return;
|
|
160
|
+
fence.fenced = true;
|
|
161
|
+
for (const sibling of fence.siblings) sibling.close();
|
|
162
|
+
fence.siblings.clear();
|
|
163
|
+
this.close();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#assertOwned(): void {
|
|
168
|
+
if (this.#fence?.fenced === true) {
|
|
169
|
+
throw new ClientSyncError(
|
|
170
|
+
STORAGE_FENCED_CODE,
|
|
171
|
+
'another tab took over this persistent database',
|
|
172
|
+
);
|
|
173
|
+
}
|
|
142
174
|
}
|
|
143
175
|
|
|
144
176
|
exec(sql: string, params: readonly SqlValue[] = []): void {
|
|
177
|
+
this.#assertOwned();
|
|
145
178
|
this.#db.exec({
|
|
146
179
|
sql,
|
|
147
180
|
...(params.length > 0 ? { bind: coerceParams(params) } : {}),
|
|
@@ -151,6 +184,7 @@ class WasmClientDatabase implements ClientDatabase {
|
|
|
151
184
|
}
|
|
152
185
|
|
|
153
186
|
query(sql: string, params: readonly SqlValue[] = []): SqlRow[] {
|
|
187
|
+
this.#assertOwned();
|
|
154
188
|
const resultRows: unknown[] = [];
|
|
155
189
|
this.#db.exec({
|
|
156
190
|
sql,
|
|
@@ -162,6 +196,7 @@ class WasmClientDatabase implements ClientDatabase {
|
|
|
162
196
|
}
|
|
163
197
|
|
|
164
198
|
transaction<T>(fn: () => T): T {
|
|
199
|
+
this.#assertOwned();
|
|
165
200
|
return runTransaction(this.#tx, (sql) => this.exec(sql), fn);
|
|
166
201
|
}
|
|
167
202
|
|
|
@@ -183,9 +218,18 @@ class WasmClientDatabase implements ClientDatabase {
|
|
|
183
218
|
db.close();
|
|
184
219
|
throw error;
|
|
185
220
|
}
|
|
221
|
+
this.#fence?.siblings.add(db);
|
|
186
222
|
return {
|
|
187
|
-
database: new WasmClientDatabase(
|
|
188
|
-
|
|
223
|
+
database: new WasmClientDatabase(
|
|
224
|
+
db,
|
|
225
|
+
this.#sqlite3,
|
|
226
|
+
undefined,
|
|
227
|
+
this.#fence,
|
|
228
|
+
),
|
|
229
|
+
close: () => {
|
|
230
|
+
this.#fence?.siblings.delete(db);
|
|
231
|
+
db.close();
|
|
232
|
+
},
|
|
189
233
|
removeFile: () => {
|
|
190
234
|
sah.util.unlink(fileName);
|
|
191
235
|
},
|
|
@@ -289,6 +333,12 @@ export interface PersistentWasmDatabaseOptions {
|
|
|
289
333
|
readonly directory?: string;
|
|
290
334
|
/** Initial SAH pool capacity (sqlite-wasm default: 6). */
|
|
291
335
|
readonly initialCapacity?: number;
|
|
336
|
+
/**
|
|
337
|
+
* Take the storage lock from the worker that holds it, which fences that
|
|
338
|
+
* worker's database. Its SAHs stay held until it runs again, so the open
|
|
339
|
+
* fails with a retryable `client.storage_busy` until then.
|
|
340
|
+
*/
|
|
341
|
+
readonly takeOver?: boolean;
|
|
292
342
|
}
|
|
293
343
|
|
|
294
344
|
function inWorkerContext(): boolean {
|
|
@@ -444,9 +494,12 @@ export async function openPersistentWasmDatabase(
|
|
|
444
494
|
);
|
|
445
495
|
// The document's leader lease can end before its worker's SAHs close. This
|
|
446
496
|
// worker-owned lock has exactly the same lifetime as the physical pool.
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
497
|
+
const storageLock = webLocksLeaderLock();
|
|
498
|
+
const lockName = `syncular-opfs/${directory}`;
|
|
499
|
+
const lease =
|
|
500
|
+
options?.takeOver === true
|
|
501
|
+
? await storageLock.steal(lockName)
|
|
502
|
+
: await storageLock.acquire(lockName);
|
|
450
503
|
let util: SahPoolUtil | undefined;
|
|
451
504
|
let db: Oo1Database | undefined;
|
|
452
505
|
try {
|
|
@@ -473,7 +526,12 @@ export async function openPersistentWasmDatabase(
|
|
|
473
526
|
await util.unpauseVfs();
|
|
474
527
|
db = new util.OpfsSAHPoolDb(`/${name}.db`);
|
|
475
528
|
configureSahCrashRecovery(db, sqlite3);
|
|
476
|
-
return new WasmClientDatabase(
|
|
529
|
+
return new WasmClientDatabase(
|
|
530
|
+
db,
|
|
531
|
+
sqlite3,
|
|
532
|
+
{ util, lease },
|
|
533
|
+
{ fenced: false, siblings: new Set() },
|
|
534
|
+
);
|
|
477
535
|
} catch (error) {
|
|
478
536
|
db?.close();
|
|
479
537
|
util?.pauseVfs();
|
package/src/worker-entry.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import type { defineAuthorityReads } from './authority';
|
|
17
17
|
import { SyncClient } from './client';
|
|
18
|
-
import type { ClientDatabase } from './database';
|
|
18
|
+
import type { ClientDatabase, SiblingDatabase } from './database';
|
|
19
19
|
import {
|
|
20
20
|
type ClientDiagnosticsSnapshot,
|
|
21
21
|
withClientDiagnosticsHost,
|
|
@@ -38,8 +38,11 @@ import type {
|
|
|
38
38
|
SegmentDownloader,
|
|
39
39
|
SyncTransport,
|
|
40
40
|
} from './transport';
|
|
41
|
+
import type { ClientSchema } from './schema';
|
|
41
42
|
import { openPersistentWasmDatabase } from './wasm-database';
|
|
42
43
|
import {
|
|
44
|
+
LOCAL_AUTHORITY_UNCONFIGURED_CODE,
|
|
45
|
+
type LocalAuthorityState,
|
|
43
46
|
type MainToWorkerMessage,
|
|
44
47
|
WORKER_FAILED_CODE,
|
|
45
48
|
type WorkerApi,
|
|
@@ -73,8 +76,30 @@ export interface SyncWorkerOverrides {
|
|
|
73
76
|
clientId: string,
|
|
74
77
|
headers: () => Readonly<Record<string, string>>,
|
|
75
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>;
|
|
76
98
|
}
|
|
77
99
|
|
|
100
|
+
/** §12: the worker's sibling file for the local authority. */
|
|
101
|
+
const LOCAL_AUTHORITY_SIBLING = 'local-authority';
|
|
102
|
+
|
|
78
103
|
/** Minimal structural view of the dedicated-worker global scope. */
|
|
79
104
|
interface WorkerScope {
|
|
80
105
|
postMessage(message: unknown, transfer?: Transferable[]): void;
|
|
@@ -142,6 +167,9 @@ export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
|
|
|
142
167
|
|
|
143
168
|
let client: SyncClient | undefined;
|
|
144
169
|
let database: ClientDatabase | undefined;
|
|
170
|
+
let authority: WorkerLocalAuthority | undefined;
|
|
171
|
+
let authoritySibling: SiblingDatabase | undefined;
|
|
172
|
+
let remoteTransport: SyncTransport | undefined;
|
|
145
173
|
let offline = false;
|
|
146
174
|
let headers: Readonly<Record<string, string>> = {};
|
|
147
175
|
let closed = false;
|
|
@@ -237,6 +265,51 @@ export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
|
|
|
237
265
|
queueMicrotask(runAutoSync);
|
|
238
266
|
}
|
|
239
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
|
+
|
|
240
313
|
function requireClient(): SyncClient {
|
|
241
314
|
if (client === undefined) {
|
|
242
315
|
throw new ClientSyncError(
|
|
@@ -290,16 +363,55 @@ export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
|
|
|
290
363
|
}
|
|
291
364
|
}
|
|
292
365
|
}
|
|
293
|
-
const
|
|
366
|
+
const remote =
|
|
294
367
|
overrides.createTransport !== undefined
|
|
295
368
|
? overrides.createTransport(config)
|
|
296
369
|
: httpSyncTransport(config.endpoints.syncUrl, http);
|
|
297
|
-
|
|
370
|
+
remoteTransport = remote;
|
|
371
|
+
const remoteSegments =
|
|
298
372
|
overrides.createSegments !== undefined
|
|
299
373
|
? overrides.createSegments(config)
|
|
300
374
|
: config.endpoints.segmentsUrl !== undefined
|
|
301
375
|
? httpSegmentDownloader(config.endpoints.segmentsUrl, http)
|
|
302
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
|
+
};
|
|
303
415
|
// §5.9 blob transport: only when a blobs URL is configured.
|
|
304
416
|
const blobs =
|
|
305
417
|
config.endpoints.blobsUrl !== undefined
|
|
@@ -319,7 +431,8 @@ export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
|
|
|
319
431
|
...(segments !== undefined ? { segments } : {}),
|
|
320
432
|
...(blobs !== undefined ? { blobs } : {}),
|
|
321
433
|
realtime: (handlers) => {
|
|
322
|
-
|
|
434
|
+
// §12.2: a local authority has no realtime channel.
|
|
435
|
+
if (realtimeConnector === undefined || localActive()) {
|
|
323
436
|
throw new ClientSyncError(
|
|
324
437
|
'sync.invalid_request',
|
|
325
438
|
'no realtime connector configured',
|
|
@@ -529,10 +642,16 @@ export function startSyncWorker(overrides: SyncWorkerOverrides = {}): void {
|
|
|
529
642
|
});
|
|
530
643
|
}
|
|
531
644
|
},
|
|
645
|
+
localAuthorityState: () => authority?.state,
|
|
646
|
+
attachLocalAuthority: () =>
|
|
647
|
+
driveLocalAuthority((local, remote) => local.attach(remote)),
|
|
648
|
+
abandonLocalAuthorityAttach: () =>
|
|
649
|
+
driveLocalAuthority((local, remote) => local.abandonAttach(remote)),
|
|
532
650
|
close: async () => {
|
|
533
651
|
closed = true;
|
|
534
652
|
if (backgroundTimer !== undefined) clearTimeout(backgroundTimer);
|
|
535
653
|
await client?.close();
|
|
654
|
+
authoritySibling?.close();
|
|
536
655
|
database?.close();
|
|
537
656
|
client = undefined;
|
|
538
657
|
database = undefined;
|
|
@@ -619,5 +738,6 @@ async function defaultOpenDatabase(
|
|
|
619
738
|
...(config.database.directory !== undefined
|
|
620
739
|
? { directory: config.database.directory }
|
|
621
740
|
: {}),
|
|
741
|
+
...(config.takeOverStorage === true ? { takeOver: true } : {}),
|
|
622
742
|
});
|
|
623
743
|
}
|