@syncular/client 0.86.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 +3 -1
- package/dist/blob.d.ts +0 -4
- package/dist/blob.js +7 -22
- package/dist/client.d.ts +12 -1
- package/dist/client.js +441 -422
- package/dist/diagnostics.d.ts +2 -4
- package/dist/diagnostics.js +3 -17
- package/dist/encryption.js +2 -21
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +4 -0
- package/dist/failed-overlay.js +2 -2
- 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 +6 -2
- package/dist/native-bridge.js +24 -11
- package/dist/outcomes.d.ts +11 -0
- package/dist/outcomes.js +19 -16
- package/dist/previous-version.js +23 -35
- package/dist/schema.js +14 -13
- package/dist/state.d.ts +16 -4
- package/dist/state.js +83 -44
- package/dist/worker-core.js +7 -8
- package/dist/worker-host.d.ts +5 -2
- package/dist/worker-host.js +9 -0
- package/dist/worker-protocol.d.ts +2 -1
- package/package.json +3 -3
- package/src/blob.ts +7 -24
- package/src/client.ts +615 -501
- package/src/diagnostics.ts +3 -19
- package/src/encryption.ts +7 -25
- package/src/errors.ts +5 -0
- package/src/failed-overlay.ts +2 -6
- 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 +35 -13
- package/src/outcomes.ts +34 -25
- package/src/previous-version.ts +31 -40
- package/src/schema.ts +13 -13
- package/src/state.ts +107 -64
- package/src/worker-core.ts +9 -16
- package/src/worker-host.ts +17 -1
- package/src/worker-protocol.ts +2 -0
package/src/diagnostics.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* diagnostic prose.
|
|
7
7
|
*/
|
|
8
8
|
import type { SecurityLifecycle } from './client';
|
|
9
|
+
import { ChangeEmitter } from './invalidation';
|
|
9
10
|
|
|
10
11
|
export const CLIENT_DIAGNOSTICS_VERSION = 2 as const;
|
|
11
12
|
export const MAX_DIAGNOSTIC_DOMAINS = 256;
|
|
@@ -198,26 +199,9 @@ export type ClientDiagnosticsListener = (
|
|
|
198
199
|
snapshot: ClientDiagnosticsSnapshot,
|
|
199
200
|
) => void;
|
|
200
201
|
|
|
201
|
-
export class ClientDiagnosticsEmitter {
|
|
202
|
-
readonly #listeners = new Set<ClientDiagnosticsListener>();
|
|
203
|
-
|
|
202
|
+
export class ClientDiagnosticsEmitter extends ChangeEmitter<ClientDiagnosticsSnapshot> {
|
|
204
203
|
get observed(): boolean {
|
|
205
|
-
return this
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
on(listener: ClientDiagnosticsListener): () => void {
|
|
209
|
-
this.#listeners.add(listener);
|
|
210
|
-
return () => this.#listeners.delete(listener);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
emit(snapshot: ClientDiagnosticsSnapshot): void {
|
|
214
|
-
for (const listener of this.#listeners) {
|
|
215
|
-
try {
|
|
216
|
-
listener(snapshot);
|
|
217
|
-
} catch {
|
|
218
|
-
// Diagnostics observers cannot alter sync correctness.
|
|
219
|
-
}
|
|
220
|
-
}
|
|
204
|
+
return this.size > 0;
|
|
221
205
|
}
|
|
222
206
|
}
|
|
223
207
|
|
package/src/encryption.ts
CHANGED
|
@@ -151,20 +151,11 @@ export async function encryptRowValues<T extends RowValue | undefined>(
|
|
|
151
151
|
values: readonly T[],
|
|
152
152
|
storedKeyId?: string,
|
|
153
153
|
): Promise<T[]> {
|
|
154
|
-
if (!table.hasEncryptedColumns) return values.slice();
|
|
155
154
|
const out = values.slice();
|
|
156
155
|
// Sparse semantics: absent (undefined) and NULL columns are never
|
|
157
156
|
// re-encrypted, so a patch that presents no encrypted value needs no key.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (table.columns[i]?.encrypted !== true) continue;
|
|
161
|
-
const value = out[i];
|
|
162
|
-
if (value !== undefined && value !== null) {
|
|
163
|
-
hasPresentEncrypted = true;
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (!hasPresentEncrypted) return out;
|
|
157
|
+
if (!table.columns.some((column, i) => column.encrypted && out[i] != null))
|
|
158
|
+
return out;
|
|
168
159
|
if (config === undefined) {
|
|
169
160
|
throw new EncryptError('encrypted columns need an encryption config');
|
|
170
161
|
}
|
|
@@ -272,7 +263,6 @@ export async function decryptRowValues(
|
|
|
272
263
|
table: CompiledClientTable,
|
|
273
264
|
values: readonly RowValue[],
|
|
274
265
|
): Promise<RowValue[]> {
|
|
275
|
-
if (!table.hasEncryptedColumns) return values.slice();
|
|
276
266
|
const out = values.slice();
|
|
277
267
|
for (let i = 0; i < table.columns.length; i++) {
|
|
278
268
|
const column = table.columns[i];
|
|
@@ -287,20 +277,12 @@ export async function decryptRowValues(
|
|
|
287
277
|
if (config === undefined) {
|
|
288
278
|
throw new DecryptError('encrypted columns need an encryption config');
|
|
289
279
|
}
|
|
290
|
-
out[i] =
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
table.name,
|
|
296
|
-
),
|
|
280
|
+
out[i] = await decryptValue(
|
|
281
|
+
declaredTypeOf(column),
|
|
282
|
+
value,
|
|
283
|
+
config.keyProvider,
|
|
284
|
+
table.name,
|
|
297
285
|
);
|
|
298
286
|
}
|
|
299
287
|
return out;
|
|
300
288
|
}
|
|
301
|
-
|
|
302
|
-
// decryptValue already returns a PlainValue; RowValue is a superset, so this
|
|
303
|
-
// is just the type widening (kept explicit for clarity at the seam).
|
|
304
|
-
function deserializeGuard(value: PlainValue): RowValue {
|
|
305
|
-
return value;
|
|
306
|
-
}
|
package/src/errors.ts
CHANGED
|
@@ -57,6 +57,11 @@ export class ClientSyncError extends Error {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/** `sync.local_corrupt`: persisted local state this client cannot read. */
|
|
61
|
+
export function localStateCorrupt(message: string): ClientSyncError {
|
|
62
|
+
return new ClientSyncError('sync.local_corrupt', message);
|
|
63
|
+
}
|
|
64
|
+
|
|
60
65
|
/** Shared invalid-request construction for client validators and transport guards. */
|
|
61
66
|
export function invalidRequest(
|
|
62
67
|
message: string,
|
package/src/failed-overlay.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { hasUniqueIndex } from './schema';
|
|
|
2
2
|
import { OUTCOMES_TABLE } from './outcomes';
|
|
3
3
|
import { OUTBOX_IMAGES } from './outbox';
|
|
4
4
|
export const RETAINED_ROWS = '_syncular_failed_rows';
|
|
5
|
-
import {
|
|
5
|
+
import { localStateCorrupt } from './errors';
|
|
6
6
|
import type { ClientDatabase } from './database';
|
|
7
7
|
import type {
|
|
8
8
|
CompiledClientSchema,
|
|
@@ -130,11 +130,7 @@ export function retainFailedRows(
|
|
|
130
130
|
operation.op === 'upsert'
|
|
131
131
|
? { ...image?.values, ...operation.values }
|
|
132
132
|
: undefined;
|
|
133
|
-
if (!image)
|
|
134
|
-
throw new ClientSyncError(
|
|
135
|
-
'sync.local_corrupt',
|
|
136
|
-
'missing intent before-image',
|
|
137
|
-
);
|
|
133
|
+
if (!image) throw localStateCorrupt('missing intent before-image');
|
|
138
134
|
db.exec(
|
|
139
135
|
`INSERT INTO ${RETAINED_ROWS}(commit_id,idx,tbl,id,at,op,intent,base,version,commit_seq)VALUES(?,?,?,?,?,?,?,?,?,?)`,
|
|
140
136
|
[
|
package/src/local-purge.ts
CHANGED
|
@@ -15,7 +15,7 @@ const MAX_TARGETS = 64;
|
|
|
15
15
|
const MAX_SELECTORS_PER_TARGET = 8;
|
|
16
16
|
const MAX_VALUES_PER_SELECTOR = 128;
|
|
17
17
|
const MAX_ROUTING_VALUE_LENGTH = 256;
|
|
18
|
-
const CODE_LIKE_VALUE = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
18
|
+
export const CODE_LIKE_VALUE = /^[A-Za-z0-9][A-Za-z0-9._:-]*$/;
|
|
19
19
|
|
|
20
20
|
function compareCodeLike(left: string, right: string): number {
|
|
21
21
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { localStateCorrupt } from './errors';
|
|
2
2
|
|
|
3
3
|
const LEGACY_MARKER = 'v1';
|
|
4
4
|
const RECEIPT_VERSION = 2;
|
|
@@ -14,10 +14,7 @@ export interface LocalDataRebootstrapReceipt {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
function invalidReceipt(): never {
|
|
17
|
-
throw
|
|
18
|
-
'sync.local_corrupt',
|
|
19
|
-
'persisted local rebootstrap receipt is invalid',
|
|
20
|
-
);
|
|
17
|
+
throw localStateCorrupt('persisted local rebootstrap receipt is invalid');
|
|
21
18
|
}
|
|
22
19
|
|
|
23
20
|
function isCount(value: unknown): value is number {
|
package/src/local-rebootstrap.ts
CHANGED
package/src/native-bridge.ts
CHANGED
|
@@ -13,8 +13,10 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import {
|
|
15
15
|
type ClientSnapshotMethods,
|
|
16
|
+
type CommitDelivery,
|
|
16
17
|
type CurrentBaseProof,
|
|
17
18
|
type MutationInput,
|
|
19
|
+
type ReplaceCommitOutcomeInput,
|
|
18
20
|
type PresencePeer,
|
|
19
21
|
type PromiseMethods,
|
|
20
22
|
type QueryReadSpec,
|
|
@@ -73,6 +75,7 @@ import {
|
|
|
73
75
|
type SyncProgressListener,
|
|
74
76
|
} from './progress';
|
|
75
77
|
import type { UnitRecord } from './state';
|
|
78
|
+
import type { OutboxCommit } from './outbox';
|
|
76
79
|
import type { WindowBase } from './window';
|
|
77
80
|
|
|
78
81
|
/** A driver-protocol reply: `{result}` on success or `{error}` on failure. */
|
|
@@ -740,7 +743,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
740
743
|
sql: spec.sql,
|
|
741
744
|
params: encodeValue(spec.params ?? []),
|
|
742
745
|
coverage: spec.coverage ?? [],
|
|
743
|
-
|
|
746
|
+
owner: spec.owner,
|
|
744
747
|
}),
|
|
745
748
|
)) as {
|
|
746
749
|
revision: string;
|
|
@@ -767,7 +770,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
767
770
|
coverage: request.coverage ?? [],
|
|
768
771
|
units: request.units ?? [],
|
|
769
772
|
commitIds: request.commitIds ?? [],
|
|
770
|
-
|
|
773
|
+
owner: request.owner,
|
|
771
774
|
})) as {
|
|
772
775
|
revision: string;
|
|
773
776
|
queries: readonly (readonly Record<string, unknown>[])[];
|
|
@@ -996,10 +999,8 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
996
999
|
): Promise<string> {
|
|
997
1000
|
const result = (await this.command('uploadBlob', {
|
|
998
1001
|
bytes: encodeValue(bytes),
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
: {}),
|
|
1002
|
-
...(options?.name !== undefined ? { name: options.name } : {}),
|
|
1002
|
+
mediaType: options?.mediaType,
|
|
1003
|
+
name: options?.name,
|
|
1003
1004
|
})) as { ref: string };
|
|
1004
1005
|
return result.ref;
|
|
1005
1006
|
}
|
|
@@ -1072,6 +1073,12 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1072
1073
|
return (await this.snapshotRead({ statements: [], units: [id] })).units[0]!;
|
|
1073
1074
|
}
|
|
1074
1075
|
|
|
1076
|
+
/** §7.5: one client commit's delivery status, from the same snapshot read. */
|
|
1077
|
+
async commitDelivery(id: string): Promise<CommitDelivery> {
|
|
1078
|
+
return (await this.snapshotRead({ statements: [], commitIds: [id] }))
|
|
1079
|
+
.deliveries[0]!;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1075
1082
|
async setWindow(base: WindowBase, units: readonly string[]): Promise<void> {
|
|
1076
1083
|
await this.command('setWindow', { base, units });
|
|
1077
1084
|
}
|
|
@@ -1096,8 +1103,8 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1096
1103
|
const maxRounds = typeof options === 'object' ? options.maxRounds : options;
|
|
1097
1104
|
if (typeof options === 'object') timeoutMs = options.timeoutMs;
|
|
1098
1105
|
return this.command('syncUntilIdle', {
|
|
1099
|
-
|
|
1100
|
-
|
|
1106
|
+
maxRounds,
|
|
1107
|
+
timeoutMs,
|
|
1101
1108
|
});
|
|
1102
1109
|
}
|
|
1103
1110
|
|
|
@@ -1118,7 +1125,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1118
1125
|
clientCommitId: outcome.clientCommitId,
|
|
1119
1126
|
sequence: outcome.sequence,
|
|
1120
1127
|
},
|
|
1121
|
-
|
|
1128
|
+
maxRounds,
|
|
1122
1129
|
})) as CurrentBaseProof;
|
|
1123
1130
|
return proof.proven
|
|
1124
1131
|
? { proven: true, rows: this.#retainedRows(proof.rows) }
|
|
@@ -1168,11 +1175,26 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1168
1175
|
return this.#commitOutcome(result.outcome);
|
|
1169
1176
|
}
|
|
1170
1177
|
|
|
1171
|
-
async
|
|
1172
|
-
|
|
1173
|
-
|
|
1178
|
+
async replaceCommitOutcome({
|
|
1179
|
+
clientCommitId,
|
|
1180
|
+
sequence,
|
|
1181
|
+
replacement,
|
|
1182
|
+
}: ReplaceCommitOutcomeInput): Promise<string> {
|
|
1183
|
+
const result = (await this.command('replaceCommitOutcome', {
|
|
1184
|
+
clientCommitId,
|
|
1185
|
+
sequence,
|
|
1186
|
+
...(replacement === undefined
|
|
1187
|
+
? {}
|
|
1188
|
+
: { replacement: encodeValue(replacement) }),
|
|
1189
|
+
})) as { clientCommitId: string };
|
|
1190
|
+
return result.clientCommitId;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
async pendingCommits(): Promise<OutboxCommit[]> {
|
|
1194
|
+
const result = (await this.command('pendingCommits', {})) as {
|
|
1195
|
+
commits: OutboxCommit[];
|
|
1174
1196
|
};
|
|
1175
|
-
return result.
|
|
1197
|
+
return result.commits;
|
|
1176
1198
|
}
|
|
1177
1199
|
|
|
1178
1200
|
async presence(scopeKey: string): Promise<readonly PresencePeer[]> {
|
package/src/outcomes.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
type RowValue,
|
|
16
16
|
} from '@syncular/core';
|
|
17
17
|
import type { ClientDatabase } from './database';
|
|
18
|
-
import {
|
|
18
|
+
import { invalidRequest, localStateCorrupt } from './errors';
|
|
19
19
|
import type { OutboxOperation } from './outbox';
|
|
20
20
|
import {
|
|
21
21
|
isBytesEnvelope,
|
|
@@ -116,9 +116,32 @@ export interface CommitOutcome {
|
|
|
116
116
|
readonly retainedRows?: readonly RetainedCommitRow[];
|
|
117
117
|
readonly resolution: CommitOutcomeResolution;
|
|
118
118
|
readonly resolvedAtMs?: number;
|
|
119
|
+
/**
|
|
120
|
+
* The replacement commit of a `superseded` outcome; on an `active`
|
|
121
|
+
* outcome, the replacement of a pending decision (§7.2.2).
|
|
122
|
+
*/
|
|
119
123
|
readonly replacementClientCommitId?: string;
|
|
120
124
|
}
|
|
121
125
|
|
|
126
|
+
export interface PendingDecision {
|
|
127
|
+
readonly clientCommitId: string;
|
|
128
|
+
readonly sequence: number;
|
|
129
|
+
readonly replacementClientCommitId: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** §7.2.2: active outcomes whose replacement waits for a proven base. */
|
|
133
|
+
export function pendingDecisions(db: ClientDatabase): PendingDecision[] {
|
|
134
|
+
return db
|
|
135
|
+
.query(
|
|
136
|
+
`SELECT seq,client_commit_id,replacement_client_commit_id FROM ${OUTCOMES_TABLE} WHERE resolution='active' AND replacement_client_commit_id IS NOT NULL`,
|
|
137
|
+
)
|
|
138
|
+
.map((row) => ({
|
|
139
|
+
clientCommitId: row.client_commit_id as string,
|
|
140
|
+
sequence: row.seq as number,
|
|
141
|
+
replacementClientCommitId: row.replacement_client_commit_id as string,
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
|
|
122
145
|
export interface CommitOutcomeQuery {
|
|
123
146
|
/** Newest-first result cap. Defaults to all retained entries. */
|
|
124
147
|
readonly limit?: number;
|
|
@@ -195,24 +218,20 @@ export function isStoredOperation(value: unknown): value is OutboxOperation {
|
|
|
195
218
|
);
|
|
196
219
|
}
|
|
197
220
|
|
|
221
|
+
function corrupt(): never {
|
|
222
|
+
throw localStateCorrupt('persisted commit outcome is invalid');
|
|
223
|
+
}
|
|
224
|
+
|
|
198
225
|
function decodeResults(raw: string): CommitOperationOutcome[] {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
'persisted commit outcome is invalid',
|
|
203
|
-
);
|
|
204
|
-
}
|
|
226
|
+
// §6.1 op indexes are i32.
|
|
227
|
+
const badIndex = (index: number) =>
|
|
228
|
+
!Number.isInteger(index) || index < -2147483648 || index > 2147483647;
|
|
205
229
|
const stored = JSON.parse(raw) as StoredCommitOperationOutcome[];
|
|
206
230
|
if (!Array.isArray(stored)) corrupt();
|
|
207
231
|
return stored.map((result) => {
|
|
208
232
|
if (!isRecord(result)) corrupt();
|
|
209
233
|
if (result.status === 'applied') {
|
|
210
|
-
if (
|
|
211
|
-
!Number.isInteger(result.opIndex) ||
|
|
212
|
-
result.opIndex < -2147483648 ||
|
|
213
|
-
result.opIndex > 2147483647
|
|
214
|
-
)
|
|
215
|
-
corrupt();
|
|
234
|
+
if (badIndex(result.opIndex)) corrupt();
|
|
216
235
|
return result;
|
|
217
236
|
}
|
|
218
237
|
if (result.status === 'conflict') {
|
|
@@ -221,9 +240,7 @@ function decodeResults(raw: string): CommitOperationOutcome[] {
|
|
|
221
240
|
!isRecord(conflict) ||
|
|
222
241
|
!isStoredRow(conflict.serverRow) ||
|
|
223
242
|
typeof conflict.clientCommitId !== 'string' ||
|
|
224
|
-
|
|
225
|
-
conflict.opIndex < -2147483648 ||
|
|
226
|
-
conflict.opIndex > 2147483647 ||
|
|
243
|
+
badIndex(conflict.opIndex) ||
|
|
227
244
|
typeof conflict.table !== 'string' ||
|
|
228
245
|
typeof conflict.rowId !== 'string' ||
|
|
229
246
|
typeof conflict.code !== 'string' ||
|
|
@@ -254,9 +271,7 @@ function decodeResults(raw: string): CommitOperationOutcome[] {
|
|
|
254
271
|
if (
|
|
255
272
|
!isRecord(rejection) ||
|
|
256
273
|
typeof rejection.clientCommitId !== 'string' ||
|
|
257
|
-
|
|
258
|
-
rejection.opIndex < -2147483648 ||
|
|
259
|
-
rejection.opIndex > 2147483647 ||
|
|
274
|
+
badIndex(rejection.opIndex) ||
|
|
260
275
|
typeof rejection.code !== 'string' ||
|
|
261
276
|
typeof rejection.message !== 'string' ||
|
|
262
277
|
typeof rejection.retryable !== 'boolean' ||
|
|
@@ -391,12 +406,6 @@ export function persistedCommitOutcome(
|
|
|
391
406
|
): CommitOutcome | undefined {
|
|
392
407
|
const row = outcomeRow(db, clientCommitId);
|
|
393
408
|
if (row === undefined) return undefined;
|
|
394
|
-
function corrupt(): never {
|
|
395
|
-
throw new ClientSyncError(
|
|
396
|
-
'sync.local_corrupt',
|
|
397
|
-
'persisted commit outcome is invalid',
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
409
|
const status = row.status;
|
|
401
410
|
const resolution = row.resolution;
|
|
402
411
|
if (
|
package/src/previous-version.ts
CHANGED
|
@@ -31,9 +31,10 @@
|
|
|
31
31
|
*/
|
|
32
32
|
import type { RowColumn, RowValue } from '@syncular/core';
|
|
33
33
|
import type { ClientDatabase, SqlRow, SqlValue } from './database';
|
|
34
|
-
import { ClientSyncError } from './errors';
|
|
34
|
+
import { ClientSyncError, localStateCorrupt } from './errors';
|
|
35
35
|
import { INVALID_HOST_RESPONSE_CODE } from './local-rebootstrap';
|
|
36
36
|
import {
|
|
37
|
+
bytesToHex,
|
|
37
38
|
type CompiledClientSchema,
|
|
38
39
|
fromSqlValue,
|
|
39
40
|
type JsonRowValue,
|
|
@@ -285,10 +286,7 @@ function hasExactKeys(
|
|
|
285
286
|
}
|
|
286
287
|
|
|
287
288
|
function localCorrupt(what: string): never {
|
|
288
|
-
throw
|
|
289
|
-
'sync.local_corrupt',
|
|
290
|
-
`persisted ${what} is invalid`,
|
|
291
|
-
);
|
|
289
|
+
throw localStateCorrupt(`persisted ${what} is invalid`);
|
|
292
290
|
}
|
|
293
291
|
|
|
294
292
|
// ---------------------------------------------------------------------------
|
|
@@ -649,12 +647,7 @@ export function capturePreviousVersion(
|
|
|
649
647
|
function rowIdText(table: LocalSchemaDescriptorTable, row: SqlRow): string {
|
|
650
648
|
const value = row[table.primaryKey] ?? null;
|
|
651
649
|
if (value === null) return '';
|
|
652
|
-
|
|
653
|
-
return [...value]
|
|
654
|
-
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
655
|
-
.join('');
|
|
656
|
-
}
|
|
657
|
-
return String(value);
|
|
650
|
+
return value instanceof Uint8Array ? bytesToHex(value) : String(value);
|
|
658
651
|
}
|
|
659
652
|
|
|
660
653
|
function decodePreviousVersionPayload(
|
|
@@ -807,39 +800,37 @@ function firstIncompatibility(
|
|
|
807
800
|
// validation, even against a removed table.
|
|
808
801
|
if (operation.op === 'delete') continue;
|
|
809
802
|
const table = schema.tables.get(operation.table);
|
|
810
|
-
|
|
803
|
+
const column =
|
|
804
|
+
table &&
|
|
805
|
+
Object.keys(operation.values ?? {}).find(
|
|
806
|
+
(key) => !table.columnIndex.has(key),
|
|
807
|
+
);
|
|
808
|
+
// §7.1: an insert-intent operation presents every current column, so a
|
|
809
|
+
// non-nullable column it lacks leaves the commit unencodable.
|
|
810
|
+
const missing =
|
|
811
|
+
operation.insert === true
|
|
812
|
+
? table?.columns.find(
|
|
813
|
+
(entry) =>
|
|
814
|
+
!entry.nullable && operation.values?.[entry.name] === undefined,
|
|
815
|
+
)
|
|
816
|
+
: undefined;
|
|
817
|
+
const refusal =
|
|
818
|
+
table === undefined
|
|
819
|
+
? { reason: 'unknown-table' as const }
|
|
820
|
+
: column !== undefined
|
|
821
|
+
? { reason: 'unknown-column' as const, column }
|
|
822
|
+
: missing !== undefined
|
|
823
|
+
? {
|
|
824
|
+
reason: 'missing-required-column' as const,
|
|
825
|
+
column: missing.name,
|
|
826
|
+
}
|
|
827
|
+
: undefined;
|
|
828
|
+
if (refusal !== undefined)
|
|
811
829
|
return {
|
|
812
830
|
commitId: commit.clientCommitId,
|
|
813
831
|
table: operation.table,
|
|
814
|
-
|
|
832
|
+
...refusal,
|
|
815
833
|
};
|
|
816
|
-
}
|
|
817
|
-
for (const key of Object.keys(operation.values ?? {})) {
|
|
818
|
-
if (!table.columnIndex.has(key)) {
|
|
819
|
-
return {
|
|
820
|
-
commitId: commit.clientCommitId,
|
|
821
|
-
table: operation.table,
|
|
822
|
-
reason: 'unknown-column',
|
|
823
|
-
column: key,
|
|
824
|
-
};
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
// §7.1: an insert-intent operation presents every current column, so a
|
|
828
|
-
// non-nullable column it lacks leaves the commit unencodable.
|
|
829
|
-
if (operation.insert === true) {
|
|
830
|
-
const missing = table.columns.find(
|
|
831
|
-
(column) =>
|
|
832
|
-
!column.nullable && operation.values?.[column.name] === undefined,
|
|
833
|
-
);
|
|
834
|
-
if (missing !== undefined) {
|
|
835
|
-
return {
|
|
836
|
-
commitId: commit.clientCommitId,
|
|
837
|
-
table: operation.table,
|
|
838
|
-
reason: 'missing-required-column',
|
|
839
|
-
column: missing.name,
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
834
|
}
|
|
844
835
|
return undefined;
|
|
845
836
|
}
|
package/src/schema.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { ensureFailedOverlaySchema, RETAINED_ROWS } from './failed-overlay';
|
|
|
18
18
|
*/
|
|
19
19
|
import type { RowColumn, RowValue } from '@syncular/core';
|
|
20
20
|
import type { ClientDatabase, SqlRow, SqlValue } from './database';
|
|
21
|
-
import {
|
|
21
|
+
import { invalidRequest, localStateCorrupt } from './errors';
|
|
22
22
|
import { snakeToCamel } from './naming';
|
|
23
23
|
|
|
24
24
|
/** `'prefix:{variable}'` shorthand (column name = variable) or explicit. */
|
|
@@ -383,11 +383,10 @@ function sqlType(column: RowColumn): string {
|
|
|
383
383
|
case 'blob_ref':
|
|
384
384
|
return 'TEXT';
|
|
385
385
|
case 'integer':
|
|
386
|
+
case 'boolean':
|
|
386
387
|
return 'INTEGER';
|
|
387
388
|
case 'float':
|
|
388
389
|
return 'REAL';
|
|
389
|
-
case 'boolean':
|
|
390
|
-
return 'INTEGER';
|
|
391
390
|
case 'bytes':
|
|
392
391
|
case 'crdt':
|
|
393
392
|
// §5.10: a crdt column stores its opaque bytes locally, exactly like
|
|
@@ -712,12 +711,14 @@ export function ensureLocalBookkeepingSchema(db: ClientDatabase): void {
|
|
|
712
711
|
UNITS_TABLE,
|
|
713
712
|
"id TEXT PRIMARY KEY,scopes TEXT NOT NULL,explicit INTEGER NOT NULL DEFAULT 0,status TEXT NOT NULL DEFAULT 'active',reason_code TEXT,cursor INTEGER DEFAULT -1,bootstrap_state TEXT,seeded TEXT NOT NULL DEFAULT '[]',echo TEXT",
|
|
714
713
|
);
|
|
715
|
-
// §5.6 applied-segment record, added after the
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
714
|
+
// §5.6 applied-segment record and §7.4.3 restriction, added after the
|
|
715
|
+
// table shipped.
|
|
716
|
+
for (const column of ['applied', 'restriction'])
|
|
717
|
+
try {
|
|
718
|
+
db.exec(`ALTER TABLE ${UNITS_TABLE} ADD COLUMN ${column} TEXT`);
|
|
719
|
+
} catch {
|
|
720
|
+
// column already exists
|
|
721
|
+
}
|
|
721
722
|
// §4.8 window registry: base + value → the unit it holds, the
|
|
722
723
|
// completeness oracle and one of a unit's references (§4.1).
|
|
723
724
|
createTable(
|
|
@@ -743,8 +744,7 @@ export function ensureLocalBookkeepingSchema(db: ClientDatabase): void {
|
|
|
743
744
|
`SELECT 1 FROM ${legacyEvictions} WHERE NOT json_valid(effective_scopes) OR json_type(effective_scopes)<>'object' OR EXISTS(SELECT 1 FROM json_each(effective_scopes) WHERE json_type(value)<>'array' OR EXISTS(SELECT 1 FROM json_each(value) v WHERE v.type<>'text'))`,
|
|
744
745
|
).length > 0
|
|
745
746
|
)
|
|
746
|
-
throw
|
|
747
|
-
'sync.local_corrupt',
|
|
747
|
+
throw localStateCorrupt(
|
|
748
748
|
'a version 3 pending eviction cannot be upgraded',
|
|
749
749
|
);
|
|
750
750
|
db.exec(
|
|
@@ -790,7 +790,7 @@ export function resetLocalReplica(
|
|
|
790
790
|
): string[] {
|
|
791
791
|
const kept = bump
|
|
792
792
|
? pruneUnknownUnits(db, schema, true)
|
|
793
|
-
: { tables: new Set<string>(), units: [] };
|
|
793
|
+
: { tables: new Set<string>(), units: [], restricts: undefined };
|
|
794
794
|
const keptNames = [...kept.tables];
|
|
795
795
|
db.exec(
|
|
796
796
|
`DELETE FROM ${RETAINED_ROWS} WHERE commit_seq IS NOT NULL AND tbl NOT IN(${keptNames.map(() => '?')})`,
|
|
@@ -815,7 +815,7 @@ export function resetLocalReplica(
|
|
|
815
815
|
for (const table of schema.tables.values()) createSyncedTable(db, table);
|
|
816
816
|
for (const table of schema.tables.values())
|
|
817
817
|
for (const index of table.ftsIndexes) createFtsProjection(db, table, index);
|
|
818
|
-
resetUnits(db, kept.units);
|
|
818
|
+
resetUnits(db, kept.units, kept.restricts);
|
|
819
819
|
return [...schema.tables.keys()].filter((name) => !kept.tables.has(name));
|
|
820
820
|
}
|
|
821
821
|
|