@syncular/client 0.85.0 → 0.90.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.
- package/README.md +13 -4
- package/dist/apply.d.ts +1 -1
- package/dist/apply.js +25 -26
- package/dist/blob.d.ts +0 -4
- package/dist/blob.js +7 -22
- package/dist/client.d.ts +31 -10
- package/dist/client.js +501 -465
- package/dist/diagnostics.d.ts +2 -4
- package/dist/diagnostics.js +3 -17
- package/dist/encryption.js +5 -30
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +4 -0
- package/dist/failed-overlay.d.ts +8 -2
- package/dist/failed-overlay.js +33 -22
- package/dist/local-purge.d.ts +1 -0
- package/dist/local-purge.js +1 -1
- package/dist/local-rebootstrap-receipt.js +2 -2
- package/dist/local-rebootstrap.js +1 -1
- package/dist/native-bridge.d.ts +8 -3
- package/dist/native-bridge.js +29 -12
- package/dist/outcomes.d.ts +13 -1
- package/dist/outcomes.js +21 -20
- package/dist/previous-version.js +23 -35
- package/dist/progress.d.ts +4 -0
- package/dist/progress.js +14 -2
- package/dist/realtime-supervisor.d.ts +4 -1
- package/dist/realtime-supervisor.js +2 -2
- package/dist/schema.js +14 -13
- package/dist/state.d.ts +16 -4
- package/dist/state.js +83 -44
- package/dist/sync-scheduler.d.ts +8 -2
- package/dist/sync-scheduler.js +11 -3
- package/dist/worker-core.js +14 -10
- package/dist/worker-host.d.ts +10 -2
- package/dist/worker-host.js +15 -0
- package/dist/worker-protocol.d.ts +4 -1
- package/package.json +3 -3
- package/src/apply.ts +34 -47
- package/src/blob.ts +7 -24
- package/src/client.ts +699 -553
- package/src/diagnostics.ts +3 -19
- package/src/encryption.ts +15 -40
- package/src/errors.ts +5 -0
- package/src/failed-overlay.ts +39 -29
- package/src/local-purge.ts +1 -1
- package/src/local-rebootstrap-receipt.ts +2 -5
- package/src/local-rebootstrap.ts +1 -2
- package/src/native-bridge.ts +40 -14
- package/src/outcomes.ts +36 -31
- package/src/previous-version.ts +31 -40
- package/src/progress.ts +18 -2
- package/src/realtime-supervisor.ts +7 -3
- package/src/schema.ts +13 -13
- package/src/state.ts +107 -64
- package/src/sync-scheduler.ts +17 -5
- package/src/worker-core.ts +14 -18
- package/src/worker-host.ts +30 -1
- package/src/worker-protocol.ts +4 -0
package/dist/worker-core.js
CHANGED
|
@@ -73,6 +73,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
73
73
|
}
|
|
74
74
|
// -- SPEC §7.5 host loop: event-driven intents, no interactive polling --
|
|
75
75
|
let autoSync = true;
|
|
76
|
+
let coalesceMs = 0;
|
|
76
77
|
let autoSyncScheduled = false;
|
|
77
78
|
let cancelBackground;
|
|
78
79
|
let backgroundDue = Number.POSITIVE_INFINITY;
|
|
@@ -104,7 +105,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
104
105
|
// let every wake-up that arrives while this round waits behind the
|
|
105
106
|
// running one queue its own full drive (§8.4 coalescing).
|
|
106
107
|
autoSyncScheduled = false;
|
|
107
|
-
return running.syncUntilIdle();
|
|
108
|
+
return running.syncUntilIdle({ skipEmpty: true });
|
|
108
109
|
})
|
|
109
110
|
.then((summary) => {
|
|
110
111
|
retryDelayMs = 250;
|
|
@@ -169,7 +170,10 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
169
170
|
return;
|
|
170
171
|
}
|
|
171
172
|
autoSyncScheduled = true;
|
|
172
|
-
|
|
173
|
+
if (coalesceMs > 0)
|
|
174
|
+
scheduleBackground(runAutoSync, coalesceMs);
|
|
175
|
+
else
|
|
176
|
+
queueMicrotask(runAutoSync);
|
|
173
177
|
}
|
|
174
178
|
function requireClient() {
|
|
175
179
|
if (client === undefined) {
|
|
@@ -193,6 +197,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
193
197
|
throw new ClientSyncError(WORKER_FAILED_CODE, 'the worker is already initialized');
|
|
194
198
|
}
|
|
195
199
|
autoSync = config.autoSync ?? true;
|
|
200
|
+
coalesceMs = config.coalesceMs ?? 0;
|
|
196
201
|
headers = config.headers ?? {};
|
|
197
202
|
const http = { headers: () => headers };
|
|
198
203
|
for (let attempt = 0;; attempt++) {
|
|
@@ -347,6 +352,8 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
347
352
|
'commitOutcome',
|
|
348
353
|
'commitOutcomes',
|
|
349
354
|
'resolveCommitOutcome',
|
|
355
|
+
'commitDelivery',
|
|
356
|
+
'replaceCommitOutcome',
|
|
350
357
|
'pendingCommits',
|
|
351
358
|
'units',
|
|
352
359
|
'unit',
|
|
@@ -371,11 +378,9 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
371
378
|
await requireClient().beginSecurityPreflight();
|
|
372
379
|
},
|
|
373
380
|
activateSecurity: async (options = {}) => {
|
|
374
|
-
await requireClient().activateSecurity(
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
: {}),
|
|
378
|
-
});
|
|
381
|
+
await requireClient().activateSecurity(options.encryption === undefined
|
|
382
|
+
? {}
|
|
383
|
+
: { encryption: encryptionConfigFromKeyring(options.encryption) });
|
|
379
384
|
},
|
|
380
385
|
setWindow: async (base, units) => {
|
|
381
386
|
await requireClient().setWindowCommand(base, units);
|
|
@@ -386,8 +391,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
386
391
|
patch: (table, rowId, partial, options) => {
|
|
387
392
|
// Same §8.4 rule as `mutate`: a local write must push without the app
|
|
388
393
|
// orchestrating sync, so consume the core's immediate intent.
|
|
389
|
-
|
|
390
|
-
return result.value;
|
|
394
|
+
return requireClient().patchCommand(table, rowId, partial, options).value;
|
|
391
395
|
},
|
|
392
396
|
sync: () => {
|
|
393
397
|
const running = requireClient();
|
|
@@ -464,7 +468,7 @@ export function startSyncWorkerCore(overrides, createLocalAuthority) {
|
|
|
464
468
|
void (async () => {
|
|
465
469
|
try {
|
|
466
470
|
const value = await fn();
|
|
467
|
-
post({ t: 'result', id, value }, transfer
|
|
471
|
+
post({ t: 'result', id, value }, transfer?.(value));
|
|
468
472
|
}
|
|
469
473
|
catch (error) {
|
|
470
474
|
post({ t: 'error', id, error: toErrorShape(error) });
|
package/dist/worker-host.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type SyncProgress, type SyncProgressListener } from './progress.js';
|
|
2
|
-
import type { PromiseMethods } from './client.js';
|
|
2
|
+
import type { CommitDelivery, PromiseMethods } from './client.js';
|
|
3
3
|
/**
|
|
4
4
|
* Main-thread side of the worker mode and the
|
|
5
5
|
* multi-tab topology.
|
|
@@ -25,7 +25,7 @@ import type { PromiseMethods } from './client.js';
|
|
|
25
25
|
*/
|
|
26
26
|
import { type PresenceErrorCode, type WakeReason } from '@syncular/core';
|
|
27
27
|
import type { BlobRef, CachedBlob } from './blob.js';
|
|
28
|
-
import type { ConflictRecord, CurrentBaseProof, MutationInput, PresencePeer, QueryReadSpec, QuerySnapshot, RejectionRecord, SecurityLifecycle, SnapshotRead, SnapshotReadRequest, SyncClientConfig, SyncClientLimits, SyncSummary, SyncIdleOptions, SyncIdleResult, WindowState } from './client.js';
|
|
28
|
+
import type { ConflictRecord, CurrentBaseProof, MutationInput, ReplaceCommitOutcomeInput, PresencePeer, QueryReadSpec, QuerySnapshot, RejectionRecord, SecurityLifecycle, SnapshotRead, SnapshotReadRequest, SyncClientConfig, SyncClientLimits, SyncSummary, SyncIdleOptions, SyncIdleResult, WindowState } from './client.js';
|
|
29
29
|
import type { SqlRow, SqlValue } from './database.js';
|
|
30
30
|
import type { DevtoolsRegistrar } from './devtools.js';
|
|
31
31
|
import { ClientDiagnosticsEmitter, type ClientDiagnosticsListener, type ClientDiagnosticsSnapshot, type RealtimePolicy } from './diagnostics.js';
|
|
@@ -111,6 +111,11 @@ export interface SyncClientHandleConfig {
|
|
|
111
111
|
readonly previousVersionContext?: SyncClientConfig['previousVersionContext'];
|
|
112
112
|
/** Worker-side host loop (§8.4); default true. */
|
|
113
113
|
readonly autoSync?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* §8.4: milliseconds a queued `autoSync` drive waits before it starts, so
|
|
116
|
+
* the claims and writes of one burst ride in one round. Default 0.
|
|
117
|
+
*/
|
|
118
|
+
readonly coalesceMs?: number;
|
|
114
119
|
/**
|
|
115
120
|
* Default: Web Locks. A runtime without Web Locks refuses the handle with
|
|
116
121
|
* `client.web_locks_unavailable` unless the host injects a lock.
|
|
@@ -306,6 +311,9 @@ export declare class SyncClientHandle implements PromiseMethods<WorkerApi> {
|
|
|
306
311
|
commitOutcome(clientCommitId: string): Promise<CommitOutcome | undefined>;
|
|
307
312
|
commitOutcomes(query?: CommitOutcomeQuery): Promise<readonly CommitOutcome[]>;
|
|
308
313
|
resolveCommitOutcome(input: ResolveCommitOutcomeInput): Promise<CommitOutcome>;
|
|
314
|
+
commitDelivery(id: string): Promise<CommitDelivery>;
|
|
315
|
+
/** §7.2.2: record a decision as a replacement held for a proven base. */
|
|
316
|
+
replaceCommitOutcome({ clientCommitId, sequence, replacement, }: ReplaceCommitOutcomeInput): Promise<string>;
|
|
309
317
|
/** §7.4.5: true while a schema-bump reset + first re-bootstrap runs. */
|
|
310
318
|
pendingCommits(): Promise<OutboxCommit[]>;
|
|
311
319
|
units(): Promise<UnitRecord[]>;
|
package/dist/worker-host.js
CHANGED
|
@@ -450,6 +450,15 @@ export class SyncClientHandle {
|
|
|
450
450
|
resolveCommitOutcome(input) {
|
|
451
451
|
return this.#call('resolveCommitOutcome', [input]);
|
|
452
452
|
}
|
|
453
|
+
commitDelivery(id) {
|
|
454
|
+
return this.#call('commitDelivery', [id]);
|
|
455
|
+
}
|
|
456
|
+
/** §7.2.2: record a decision as a replacement held for a proven base. */
|
|
457
|
+
replaceCommitOutcome({ clientCommitId, sequence, replacement, }) {
|
|
458
|
+
return this.#call('replaceCommitOutcome', [
|
|
459
|
+
{ clientCommitId, sequence, replacement },
|
|
460
|
+
]);
|
|
461
|
+
}
|
|
453
462
|
/** §7.4.5: true while a schema-bump reset + first re-bootstrap runs. */
|
|
454
463
|
pendingCommits() {
|
|
455
464
|
return this.#call('pendingCommits', []);
|
|
@@ -705,6 +714,9 @@ function buildInitConfig(config, headers = config.headers) {
|
|
|
705
714
|
? { previousVersionContext: config.previousVersionContext }
|
|
706
715
|
: {}),
|
|
707
716
|
...(config.autoSync !== undefined ? { autoSync: config.autoSync } : {}),
|
|
717
|
+
...(config.coalesceMs !== undefined
|
|
718
|
+
? { coalesceMs: config.coalesceMs }
|
|
719
|
+
: {}),
|
|
708
720
|
};
|
|
709
721
|
}
|
|
710
722
|
function syncedResult(event) {
|
|
@@ -743,6 +755,9 @@ function fireConfigCallbacks(config, event) {
|
|
|
743
755
|
*/
|
|
744
756
|
export async function createSyncClientHandle(config) {
|
|
745
757
|
const resolvedConfig = resolveReplicaConfig(config);
|
|
758
|
+
if (config.coalesceMs !== undefined &&
|
|
759
|
+
!(Number.isFinite(config.coalesceMs) && config.coalesceMs >= 0))
|
|
760
|
+
throw invalidRequest('coalesceMs must be a non-negative finite number');
|
|
746
761
|
const lock = resolvedConfig.leaderLock ?? defaultLeaderLock();
|
|
747
762
|
const lockName = resolvedConfig.lockName ?? 'syncular-leader';
|
|
748
763
|
const invalidation = new InvalidationEmitter();
|
|
@@ -19,7 +19,7 @@ import type { SyncProgress } from './progress.js';
|
|
|
19
19
|
*/
|
|
20
20
|
import type { PresenceErrorCode, WakeReason } from '@syncular/core';
|
|
21
21
|
import type { BlobRef, CachedBlob } from './blob.js';
|
|
22
|
-
import type { ConflictRecord, CurrentBaseProof, ClientSnapshotMethods, MutationInput, PresencePeer, QueryReadSpec, QuerySnapshot, SecurityLifecycle, SyncClientConfig, SyncClientLimits, SyncSummary, SyncIdleOptions, SyncIdleResult, WindowState } from './client.js';
|
|
22
|
+
import type { ConflictRecord, CurrentBaseProof, ClientSnapshotMethods, ReplaceCommitOutcomeInput, MutationInput, PresencePeer, QueryReadSpec, QuerySnapshot, SecurityLifecycle, SyncClientConfig, SyncClientLimits, SyncSummary, SyncIdleOptions, SyncIdleResult, WindowState } from './client.js';
|
|
23
23
|
import type { SqlRow, SqlValue } from './database.js';
|
|
24
24
|
import type { ClientDiagnosticsSnapshot, RealtimePolicy } from './diagnostics.js';
|
|
25
25
|
import type { EncryptionKeyringConfig } from './encryption.js';
|
|
@@ -121,6 +121,8 @@ export interface WorkerInitConfig {
|
|
|
121
121
|
* `syncUntilIdle` rounds inside the worker. Default true.
|
|
122
122
|
*/
|
|
123
123
|
readonly autoSync?: boolean;
|
|
124
|
+
/** Delay of a queued `autoSync` drive (§8.4); validated by the handle. */
|
|
125
|
+
readonly coalesceMs?: number;
|
|
124
126
|
}
|
|
125
127
|
/** Successful init reply. */
|
|
126
128
|
export interface WorkerInitResult {
|
|
@@ -164,6 +166,7 @@ export interface WorkerApi extends Omit<ClientSnapshotMethods, 'querySnapshot'>
|
|
|
164
166
|
sync(): Promise<SyncSummary>;
|
|
165
167
|
syncUntilIdle(options?: number | SyncIdleOptions): Promise<SyncIdleResult>;
|
|
166
168
|
proveCurrentBase(outcome: Pick<CommitOutcome, 'clientCommitId' | 'sequence'>, maxRounds?: number): Promise<CurrentBaseProof>;
|
|
169
|
+
replaceCommitOutcome(input: ReplaceCommitOutcomeInput): string;
|
|
167
170
|
resetClientId(): string;
|
|
168
171
|
query(sql: string, params?: readonly SqlValue[]): SqlRow[];
|
|
169
172
|
authoritySnapshot(): import('./authority.js').AuthoritySnapshot;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.90.0",
|
|
4
4
|
"description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -121,11 +121,11 @@
|
|
|
121
121
|
},
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"@sqlite.org/sqlite-wasm": "^3.53.0-build1",
|
|
124
|
-
"@syncular/core": "0.
|
|
124
|
+
"@syncular/core": "0.90.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.90.0",
|
|
129
129
|
"playwright": "1.63.0",
|
|
130
130
|
"vite": "8.3.4"
|
|
131
131
|
}
|
package/src/apply.ts
CHANGED
|
@@ -36,7 +36,6 @@ import {
|
|
|
36
36
|
SEALED_KEY_COLUMN,
|
|
37
37
|
SYNC_VERSION_COLUMN,
|
|
38
38
|
toSqlValue,
|
|
39
|
-
rowValueToJson,
|
|
40
39
|
} from './schema';
|
|
41
40
|
|
|
42
41
|
/** Lets the client wrap the physical write in its revision transaction. */
|
|
@@ -63,7 +62,6 @@ export function upsertLocalRow(
|
|
|
63
62
|
table: CompiledClientTable,
|
|
64
63
|
values: readonly RowValue[],
|
|
65
64
|
syncVersion: number,
|
|
66
|
-
serverWrite = true,
|
|
67
65
|
sealed?: SealedRow,
|
|
68
66
|
): void {
|
|
69
67
|
db.exec(upsertSql(table), [
|
|
@@ -73,19 +71,6 @@ export function upsertLocalRow(
|
|
|
73
71
|
? [sealed?.keyId ?? null, sealed?.envelopes ?? null]
|
|
74
72
|
: []),
|
|
75
73
|
]);
|
|
76
|
-
if (serverWrite)
|
|
77
|
-
retainedBaseWrite(
|
|
78
|
-
db,
|
|
79
|
-
table,
|
|
80
|
-
String(values[table.primaryKeyIndex]),
|
|
81
|
-
Object.fromEntries(
|
|
82
|
-
table.columns.map((column, index) => [
|
|
83
|
-
column.name,
|
|
84
|
-
rowValueToJson(values[index] ?? null),
|
|
85
|
-
]),
|
|
86
|
-
),
|
|
87
|
-
syncVersion,
|
|
88
|
-
);
|
|
89
74
|
}
|
|
90
75
|
|
|
91
76
|
export function deleteLocalRow(
|
|
@@ -94,11 +79,11 @@ export function deleteLocalRow(
|
|
|
94
79
|
rowId: string,
|
|
95
80
|
serverWrite = true,
|
|
96
81
|
): void {
|
|
97
|
-
if (serverWrite) retainedBaseWrite(db, table, rowId);
|
|
98
82
|
db.exec(
|
|
99
83
|
`DELETE FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(table.primaryKey)}=?`,
|
|
100
84
|
[pkSqlValue(table, rowId)],
|
|
101
85
|
);
|
|
86
|
+
if (serverWrite) retainedBaseWrite(db, table, new Map([[rowId, undefined]]));
|
|
102
87
|
}
|
|
103
88
|
|
|
104
89
|
/**
|
|
@@ -238,15 +223,20 @@ export async function applyCommitFrame(
|
|
|
238
223
|
);
|
|
239
224
|
if (!land) continue;
|
|
240
225
|
if (change.op === 'delete') deleteLocalRow(db, change.table, rowId);
|
|
241
|
-
else
|
|
226
|
+
else {
|
|
242
227
|
upsertLocalRow(
|
|
243
228
|
db,
|
|
244
229
|
change.table,
|
|
245
230
|
change.values,
|
|
246
231
|
rowVersion,
|
|
247
|
-
true,
|
|
248
232
|
change.sealed,
|
|
249
233
|
);
|
|
234
|
+
retainedBaseWrite(
|
|
235
|
+
db,
|
|
236
|
+
change.table,
|
|
237
|
+
new Map([[rowId, [change.values, rowVersion]]]),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
250
240
|
}
|
|
251
241
|
return rebaseScope(scope, kept, skipped);
|
|
252
242
|
});
|
|
@@ -416,11 +406,13 @@ export function deleteScopedRows(
|
|
|
416
406
|
const scope = localScopePredicate(table, effective, owners);
|
|
417
407
|
if (!scope) return;
|
|
418
408
|
const { sql, params } = scope;
|
|
419
|
-
|
|
420
|
-
for (const row of scopedRows(db, table, scope)) {
|
|
421
|
-
retainedBaseWrite(db, table, String(row.id));
|
|
422
|
-
}
|
|
409
|
+
const retained = retainsRows(db, table) ? scopedRows(db, table, scope) : [];
|
|
423
410
|
db.exec(`DELETE FROM ${quoteIdent(table.name)} WHERE ${sql}`, params);
|
|
411
|
+
retainedBaseWrite(
|
|
412
|
+
db,
|
|
413
|
+
table,
|
|
414
|
+
new Map(retained.map((row) => [String(row.id), undefined])),
|
|
415
|
+
);
|
|
424
416
|
}
|
|
425
417
|
|
|
426
418
|
/**
|
|
@@ -668,7 +660,7 @@ export async function applySqliteSegment(
|
|
|
668
660
|
JSON.stringify([table.name, String(row[table.primaryKey])]),
|
|
669
661
|
),
|
|
670
662
|
];
|
|
671
|
-
|
|
663
|
+
restoreFailedBases(db, schema, scope);
|
|
672
664
|
if (first && options.clearFirst)
|
|
673
665
|
deleteScopedRows(db, table, options.effective, options.owners());
|
|
674
666
|
const kept = new Set<string>();
|
|
@@ -709,21 +701,19 @@ export async function applySqliteSegment(
|
|
|
709
701
|
Math.min(1024, size - chunk),
|
|
710
702
|
);
|
|
711
703
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
704
|
+
retainedBaseWrite(
|
|
705
|
+
db,
|
|
706
|
+
table,
|
|
707
|
+
new Map(
|
|
708
|
+
landing.map((row) => [
|
|
717
709
|
String(row[table.primaryKey]),
|
|
718
|
-
|
|
719
|
-
table.columns.map((column) => [
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
);
|
|
726
|
-
}
|
|
710
|
+
[
|
|
711
|
+
table.columns.map((column) => row[column.name] as RowValue),
|
|
712
|
+
Number(row._syncular_version),
|
|
713
|
+
],
|
|
714
|
+
]),
|
|
715
|
+
),
|
|
716
|
+
);
|
|
727
717
|
options.onChunk?.(applied);
|
|
728
718
|
return rebaseScope(scope, kept, skipped);
|
|
729
719
|
});
|
|
@@ -812,26 +802,23 @@ export async function applyRowsSegment(
|
|
|
812
802
|
]),
|
|
813
803
|
),
|
|
814
804
|
];
|
|
815
|
-
|
|
805
|
+
restoreFailedBases(db, schema, scope);
|
|
816
806
|
if (clearThisBlock) {
|
|
817
807
|
deleteScopedRows(db, table, options.effective, options.owners());
|
|
818
808
|
}
|
|
819
809
|
const kept = new Set<string>();
|
|
820
810
|
const skipped = new Set<string>();
|
|
811
|
+
const written = new Map<string, readonly [readonly RowValue[], number]>();
|
|
821
812
|
for (const row of rows) {
|
|
822
813
|
const rowId = String(row.values[table.primaryKeyIndex]);
|
|
823
|
-
if (lands(db, table, rowId, row.serverVersion, kept, skipped))
|
|
824
|
-
upsertLocalRow(
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
row.values,
|
|
828
|
-
row.serverVersion,
|
|
829
|
-
protectedRows,
|
|
830
|
-
row.sealed,
|
|
831
|
-
);
|
|
814
|
+
if (lands(db, table, rowId, row.serverVersion, kept, skipped)) {
|
|
815
|
+
upsertLocalRow(db, table, row.values, row.serverVersion, row.sealed);
|
|
816
|
+
written.set(rowId, [row.values, row.serverVersion]);
|
|
817
|
+
}
|
|
832
818
|
applied += 1;
|
|
833
819
|
if (applied % 1024 === 0) options.onProgress?.(applied);
|
|
834
820
|
}
|
|
821
|
+
retainedBaseWrite(db, table, written);
|
|
835
822
|
return rebaseScope(scope, kept, skipped);
|
|
836
823
|
});
|
|
837
824
|
options.onProgress?.(applied);
|
package/src/blob.ts
CHANGED
|
@@ -8,9 +8,9 @@ export const BLOB_COMMIT_REFS = '_syncular_blob_commit_refs';
|
|
|
8
8
|
*/
|
|
9
9
|
import { type BlobRef, parseBlobRef, serializeBlobRef } from '@syncular/core';
|
|
10
10
|
import type { ClientDatabase } from './database';
|
|
11
|
-
import { ClientSyncError } from './errors';
|
|
11
|
+
import { ClientSyncError, localStateCorrupt } from './errors';
|
|
12
12
|
import type { CompiledClientSchema } from './schema';
|
|
13
|
-
import { quoteIdent, tableExists } from './schema';
|
|
13
|
+
import { bytesToHex, quoteIdent, tableExists } from './schema';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* A blob download result (§5.9.5). The authorized endpoint either serves the
|
|
@@ -114,10 +114,7 @@ export async function computeBlobId(bytes: Uint8Array): Promise<string> {
|
|
|
114
114
|
'SHA-256',
|
|
115
115
|
input as Uint8Array<ArrayBuffer>,
|
|
116
116
|
);
|
|
117
|
-
|
|
118
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
119
|
-
.join('');
|
|
120
|
-
return `sha256:${hex}`;
|
|
117
|
+
return `sha256:${bytesToHex(new Uint8Array(digest))}`;
|
|
121
118
|
}
|
|
122
119
|
|
|
123
120
|
export function ensureBlobSchema(db: ClientDatabase): void {
|
|
@@ -126,16 +123,8 @@ export function ensureBlobSchema(db: ClientDatabase): void {
|
|
|
126
123
|
.query('PRAGMA table_info(_syncular_blobs)')
|
|
127
124
|
.map((row) => row.name)
|
|
128
125
|
.sort();
|
|
129
|
-
const expected = [
|
|
130
|
-
'blob_id',
|
|
131
|
-
'byte_length',
|
|
132
|
-
'bytes',
|
|
133
|
-
'created_at_ms',
|
|
134
|
-
'media_type',
|
|
135
|
-
].sort();
|
|
136
126
|
if (
|
|
137
|
-
columns.
|
|
138
|
-
columns.some((column, index) => column !== expected[index])
|
|
127
|
+
columns.join() !== 'blob_id,byte_length,bytes,created_at_ms,media_type'
|
|
139
128
|
) {
|
|
140
129
|
throw new ClientSyncError(
|
|
141
130
|
'sync.schema_mismatch',
|
|
@@ -228,10 +217,7 @@ export function listPendingUploads(
|
|
|
228
217
|
typeof row.blob_id !== 'string' ||
|
|
229
218
|
(row.media_type !== null && typeof row.media_type !== 'string')
|
|
230
219
|
) {
|
|
231
|
-
throw
|
|
232
|
-
'sync.local_corrupt',
|
|
233
|
-
'Pending blob upload metadata is invalid',
|
|
234
|
-
);
|
|
220
|
+
throw localStateCorrupt('Pending blob upload metadata is invalid');
|
|
235
221
|
}
|
|
236
222
|
return {
|
|
237
223
|
blobId: row.blob_id,
|
|
@@ -259,7 +245,7 @@ export function clearPendingUpload(db: ClientDatabase, blobId: string): void {
|
|
|
259
245
|
/**
|
|
260
246
|
* All `blob_ref` column names per table.
|
|
261
247
|
*/
|
|
262
|
-
|
|
248
|
+
function blobRefColumnsBySchema(
|
|
263
249
|
schema: CompiledClientSchema,
|
|
264
250
|
): Map<string, string[]> {
|
|
265
251
|
const out = new Map<string, string[]>();
|
|
@@ -273,10 +259,7 @@ export function blobRefColumnsBySchema(
|
|
|
273
259
|
}
|
|
274
260
|
|
|
275
261
|
export function schemaHasBlobs(schema: CompiledClientSchema): boolean {
|
|
276
|
-
|
|
277
|
-
if (table.columns.some((c) => c.type === 'blob_ref')) return true;
|
|
278
|
-
}
|
|
279
|
-
return false;
|
|
262
|
+
return blobRefColumnsBySchema(schema).size > 0;
|
|
280
263
|
}
|
|
281
264
|
|
|
282
265
|
/** The cached bodies no upload, pending commit, or visible row references. */
|