@syncular/client 0.70.2 → 0.71.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/dist/client.js +5 -1
- package/dist/native-bridge.d.ts +1 -0
- package/dist/native-bridge.js +4 -0
- package/dist/outcomes.d.ts +6 -0
- package/dist/outcomes.js +64 -0
- package/dist/realtime-supervisor.d.ts +9 -5
- package/dist/realtime-supervisor.js +14 -7
- package/dist/worker-host.d.ts +1 -0
- package/dist/worker-host.js +3 -0
- package/package.json +3 -3
- package/src/client.ts +12 -0
- package/src/native-bridge.ts +3 -0
- package/src/outcomes.ts +83 -0
- package/src/realtime-supervisor.ts +20 -10
- package/src/worker-host.ts +2 -0
package/dist/client.js
CHANGED
|
@@ -27,7 +27,7 @@ import { compileLocalDataPurge, localDataPurgeMetaKey, localDataPurgeTargetMatch
|
|
|
27
27
|
import { compileLocalDataRebootstrap, localDataRebootstrapMetaKey, } from './local-rebootstrap.js';
|
|
28
28
|
import { decodeLocalDataRebootstrapReceipt, encodeLocalDataRebootstrapReceipt, } from './local-rebootstrap-receipt.js';
|
|
29
29
|
import { appendOutboxCommit, chainsOn, deleteOutboxCommit, encodeOutboxCommit, listOutbox, iterateOutbox, countOutbox, listOutboxBeforeImages, OutboxEncodeError, replaceOutboxBeforeImages, } from './outbox.js';
|
|
30
|
-
import { activeFailureRecords, isStoredOperation, listCommitOutcomes, persistCommitOutcomeResolution, persistedCommitOutcome, pruneCommitOutcomes, commitOutcome as readCommitOutcome, recordCommitOutcome, } from './outcomes.js';
|
|
30
|
+
import { activeFailureRecords, isStoredOperation, listCommitOutcomes, persistCommitOutcomeResolution, persistedCommitOutcome, pruneCommitOutcomes, commitOutcome as readCommitOutcome, recordCommitOutcome, scrubPurgedOutcomes, } from './outcomes.js';
|
|
31
31
|
import { buildPreviousVersionAudit, capturePreviousVersion, clearPreviousVersionAudit, clearPreviousVersionRefusal, dropPreviousVersionContainer, loadLocalSchemaDescriptor, PREVIOUS_VERSION_CONTAINER_NAME, PREVIOUS_VERSION_MAX_LIMIT, readPreviousVersionContainer, readPreviousVersionRows, setLocalSchemaDescriptor, storedPreviousVersionAudit, storedPreviousVersionRefusal, withPreviousVersionContainer, writePreviousVersionAudit, writePreviousVersionRefusal, } from './previous-version.js';
|
|
32
32
|
import { bytesToHex, compileClientSchema, hexToBytes, matchesLocalScopes, dropAndRecreateSyncedTables, ensureLocalBookkeepingSchema, ensureLocalSyncedSchema, fromSqlValue, coerceSqlRepresentation, localColumnType, jsonToRowValue, LOCAL_SCHEMA_VERSION_KEY, normalizeRecordKeys, OPTIMISTIC_VERSION, quoteIdent, recordToRowValues, rowValueToJson, SYNC_VERSION_COLUMN, stripSyncColumns, } from './schema.js';
|
|
33
33
|
import { bootstrapping, bumpLocalRevision, covers, deleteUnit, ECHO_DIGEST_KEY, getLocalRevision, getMeta, getUnit, loadUnits, META_TABLE, owning, pairScopes, pruneUnknownUnits, REGISTERED_DIGEST_KEY, REPLICA_CURSOR_KEY, replicaCursor, resetUnits, saveUnit, saveUnitScopeSchema, setMeta, unitOwners, } from './state.js';
|
|
@@ -2508,6 +2508,10 @@ export class SyncClient {
|
|
|
2508
2508
|
this.#db.exec(`DELETE FROM ${quoteIdent(tableName)} WHERE ${quoteIdent(table.primaryKey)} IN(${chunk.map(() => '?').join(', ')})`, chunk);
|
|
2509
2509
|
}
|
|
2510
2510
|
}
|
|
2511
|
+
if (scrubPurgedOutcomes(this.#db, (table, rowId, values) => (targetsByTable.get(table) ?? []).some((target) => rowIds.get(table)?.has(rowId) === true ||
|
|
2512
|
+
(values !== undefined &&
|
|
2513
|
+
localDataPurgeTargetMatches(target, values)))))
|
|
2514
|
+
batch.outcomes();
|
|
2511
2515
|
if (doomed.length > 0) {
|
|
2512
2516
|
// §5.11 purge erasure: the journal keeps a dropped commit's identity and codes,
|
|
2513
2517
|
// never its operations, so no purged value survives in it.
|
package/dist/native-bridge.d.ts
CHANGED
|
@@ -225,6 +225,7 @@ export declare abstract class NativeBridgeClient implements PromiseMethods<Clien
|
|
|
225
225
|
presence(scopeKey: string): Promise<readonly PresencePeer[]>;
|
|
226
226
|
setPresence(scopeKey: string, doc: Record<string, unknown> | null): Promise<void>;
|
|
227
227
|
connectRealtime(): Promise<void>;
|
|
228
|
+
/** A closed client holds no socket, so its disconnect succeeds. */
|
|
228
229
|
disconnectRealtime(): Promise<void>;
|
|
229
230
|
/**
|
|
230
231
|
* Shut down the native core, then detach listeners. Every call returns the
|
package/dist/native-bridge.js
CHANGED
|
@@ -243,6 +243,7 @@ export class NativeBridgeClient {
|
|
|
243
243
|
'beginSecurityPreflight',
|
|
244
244
|
'activateSecurity',
|
|
245
245
|
'setTransportEnabled',
|
|
246
|
+
'disconnectRealtime',
|
|
246
247
|
'purgeLocalData',
|
|
247
248
|
'previousVersionDiscard',
|
|
248
249
|
'localRevision',
|
|
@@ -769,7 +770,10 @@ export class NativeBridgeClient {
|
|
|
769
770
|
async connectRealtime() {
|
|
770
771
|
await this.command('connectRealtime', {});
|
|
771
772
|
}
|
|
773
|
+
/** A closed client holds no socket, so its disconnect succeeds. */
|
|
772
774
|
async disconnectRealtime() {
|
|
775
|
+
if (this.#closed)
|
|
776
|
+
return;
|
|
773
777
|
await this.command('disconnectRealtime', {});
|
|
774
778
|
}
|
|
775
779
|
/**
|
package/dist/outcomes.d.ts
CHANGED
|
@@ -118,3 +118,9 @@ export declare function activeFailureRecords(outcomes: readonly CommitOutcome[])
|
|
|
118
118
|
readonly conflicts: ConflictRecord[];
|
|
119
119
|
readonly rejections: RejectionRecord[];
|
|
120
120
|
};
|
|
121
|
+
/**
|
|
122
|
+
* §5.11 purge erasure: every journal entry with an operation, or a conflict
|
|
123
|
+
* row, that `touches` accepts keeps its identity, status, ids and codes only.
|
|
124
|
+
* Returns whether an entry changed.
|
|
125
|
+
*/
|
|
126
|
+
export declare function scrubPurgedOutcomes(db: ClientDatabase, touches: (table: string, rowId: string, values: Readonly<Record<string, unknown>> | undefined) => boolean): boolean;
|
package/dist/outcomes.js
CHANGED
|
@@ -306,3 +306,67 @@ export function activeFailureRecords(outcomes) {
|
|
|
306
306
|
}
|
|
307
307
|
return { conflicts, rejections };
|
|
308
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* §5.11 purge erasure: every journal entry with an operation, or a conflict
|
|
311
|
+
* row, that `touches` accepts keeps its identity, status, ids and codes only.
|
|
312
|
+
* Returns whether an entry changed.
|
|
313
|
+
*/
|
|
314
|
+
export function scrubPurgedOutcomes(db, touches) {
|
|
315
|
+
let changed = false;
|
|
316
|
+
for (const row of db.query(`SELECT client_commit_id,results,operations FROM ${OUTCOMES_TABLE}`)) {
|
|
317
|
+
const results = JSON.parse(String(row.results));
|
|
318
|
+
const operations = typeof row.operations === 'string'
|
|
319
|
+
? JSON.parse(row.operations)
|
|
320
|
+
: [];
|
|
321
|
+
const named = [
|
|
322
|
+
...operations,
|
|
323
|
+
...results.flatMap((result) => result.status === 'conflict'
|
|
324
|
+
? [
|
|
325
|
+
{
|
|
326
|
+
table: result.conflict.table,
|
|
327
|
+
rowId: result.conflict.rowId,
|
|
328
|
+
values: result.conflict.serverRow,
|
|
329
|
+
},
|
|
330
|
+
...(result.conflict.operation === undefined
|
|
331
|
+
? []
|
|
332
|
+
: [result.conflict.operation]),
|
|
333
|
+
]
|
|
334
|
+
: result.status === 'error' &&
|
|
335
|
+
result.rejection.operation !== undefined
|
|
336
|
+
? [result.rejection.operation]
|
|
337
|
+
: []),
|
|
338
|
+
];
|
|
339
|
+
if (!named.some((op) => touches(op.table, op.rowId, op.values)))
|
|
340
|
+
continue;
|
|
341
|
+
const scrubbed = results.map((result) => {
|
|
342
|
+
if (result.status === 'conflict') {
|
|
343
|
+
const { clientCommitId, opIndex, table, rowId, code, message } = result.conflict;
|
|
344
|
+
return {
|
|
345
|
+
status: 'conflict',
|
|
346
|
+
conflict: {
|
|
347
|
+
clientCommitId,
|
|
348
|
+
opIndex,
|
|
349
|
+
table,
|
|
350
|
+
rowId,
|
|
351
|
+
code,
|
|
352
|
+
message,
|
|
353
|
+
serverVersion: result.conflict.serverVersion,
|
|
354
|
+
serverRow: {},
|
|
355
|
+
conflictColumns: [],
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
if (result.status === 'error') {
|
|
360
|
+
const { clientCommitId, opIndex, code, message, retryable } = result.rejection;
|
|
361
|
+
return {
|
|
362
|
+
status: 'error',
|
|
363
|
+
rejection: { clientCommitId, opIndex, code, message, retryable },
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
});
|
|
368
|
+
db.exec(`UPDATE ${OUTCOMES_TABLE} SET results=?,operations=NULL WHERE client_commit_id=?`, [JSON.stringify(scrubbed), String(row.client_commit_id)]);
|
|
369
|
+
changed = true;
|
|
370
|
+
}
|
|
371
|
+
return changed;
|
|
372
|
+
}
|
|
@@ -26,9 +26,14 @@ export interface RealtimeSupervisorSnapshot {
|
|
|
26
26
|
readonly retryDelayMs?: number;
|
|
27
27
|
/** The latched failure of phase `unsupported` (or `stopped`). */
|
|
28
28
|
readonly failureCode?: RealtimeSupervisorFailureCode;
|
|
29
|
+
/**
|
|
30
|
+
* The Syncular code (or `unknown`) the latest refused
|
|
31
|
+
* `disconnectRealtime()` rejected with, kept until the next `connected`.
|
|
32
|
+
*/
|
|
33
|
+
readonly disconnectCauseCode?: string;
|
|
29
34
|
}
|
|
30
35
|
/** SPEC §10.3: client-local supervisor failures, never on the wire. */
|
|
31
|
-
export type RealtimeSupervisorFailureCode = 'realtime.diagnostics_unavailable' | 'realtime.
|
|
36
|
+
export type RealtimeSupervisorFailureCode = 'realtime.diagnostics_unavailable' | 'realtime.cleanup_failed';
|
|
32
37
|
export interface RealtimeSupervisorOptions {
|
|
33
38
|
/**
|
|
34
39
|
* Host online/offline evidence, the only connectivity the supervisor
|
|
@@ -89,8 +94,6 @@ export declare class RealtimeSupervisorError extends ClientSyncError {
|
|
|
89
94
|
readonly code: RealtimeSupervisorFailureCode;
|
|
90
95
|
readonly cleanupFailureCode?: 'realtime.cleanup_failed';
|
|
91
96
|
readonly diagnosticsFailureCode?: 'realtime.diagnostics_unavailable';
|
|
92
|
-
/** The code the client's `disconnectRealtime` rejected with. */
|
|
93
|
-
readonly disconnectCauseCode?: string;
|
|
94
97
|
/** Set by the installed `close()` when the client close failed as well. */
|
|
95
98
|
readonly stopFailureCode?: RealtimeSupervisorFailureCode;
|
|
96
99
|
readonly closeFailureCode?: string;
|
|
@@ -104,8 +107,9 @@ export declare class RealtimeSupervisorError extends ClientSyncError {
|
|
|
104
107
|
* exponential jitter, and the backoff survives offline, background and
|
|
105
108
|
* protected suspensions. While no socket is connected, each failed connect,
|
|
106
109
|
* each host `online` report and each foreground return drives one HTTP round
|
|
107
|
-
* (§1.8). A disconnect
|
|
108
|
-
*
|
|
110
|
+
* (§1.8). A refused disconnect counts one retry and names its cause in the
|
|
111
|
+
* snapshot. A diagnostics or cleanup failure latches phase `unsupported` with
|
|
112
|
+
* its `failureCode`, and `stop()` rejects with it.
|
|
109
113
|
*/
|
|
110
114
|
export declare class RealtimeSupervisor {
|
|
111
115
|
#private;
|
|
@@ -150,8 +150,9 @@ export class RealtimeSupervisorError extends ClientSyncError {
|
|
|
150
150
|
* exponential jitter, and the backoff survives offline, background and
|
|
151
151
|
* protected suspensions. While no socket is connected, each failed connect,
|
|
152
152
|
* each host `online` report and each foreground return drives one HTTP round
|
|
153
|
-
* (§1.8). A disconnect
|
|
154
|
-
*
|
|
153
|
+
* (§1.8). A refused disconnect counts one retry and names its cause in the
|
|
154
|
+
* snapshot. A diagnostics or cleanup failure latches phase `unsupported` with
|
|
155
|
+
* its `failureCode`, and `stop()` rejects with it.
|
|
155
156
|
*/
|
|
156
157
|
export class RealtimeSupervisor {
|
|
157
158
|
#client;
|
|
@@ -192,6 +193,7 @@ export class RealtimeSupervisor {
|
|
|
192
193
|
#failure;
|
|
193
194
|
/** Every failure of the lifetime, named for the stop rejection. */
|
|
194
195
|
#failures = {};
|
|
196
|
+
#disconnectCause;
|
|
195
197
|
#snapshot = { phase: 'idle', attempt: 0 };
|
|
196
198
|
constructor(client, options = {}) {
|
|
197
199
|
this.#client = client;
|
|
@@ -334,7 +336,10 @@ export class RealtimeSupervisor {
|
|
|
334
336
|
this.#drive();
|
|
335
337
|
});
|
|
336
338
|
}
|
|
337
|
-
#publish(
|
|
339
|
+
#publish(next) {
|
|
340
|
+
const snapshot = this.#disconnectCause === undefined
|
|
341
|
+
? next
|
|
342
|
+
: { ...next, disconnectCauseCode: this.#disconnectCause };
|
|
338
343
|
if (JSON.stringify(this.#snapshot) === JSON.stringify(snapshot))
|
|
339
344
|
return;
|
|
340
345
|
this.#snapshot = Object.freeze(snapshot);
|
|
@@ -484,6 +489,7 @@ export class RealtimeSupervisor {
|
|
|
484
489
|
this.#connected = true;
|
|
485
490
|
this.#attempt = 0;
|
|
486
491
|
this.#delayMs = 0;
|
|
492
|
+
this.#disconnectCause = undefined;
|
|
487
493
|
this.#publish({ phase: 'connected', attempt: 0 });
|
|
488
494
|
return;
|
|
489
495
|
}
|
|
@@ -517,10 +523,11 @@ export class RealtimeSupervisor {
|
|
|
517
523
|
this.#disconnecting = Promise.resolve();
|
|
518
524
|
this.#disconnecting = new Promise((resolve) => resolve(this.#client.disconnectRealtime()))
|
|
519
525
|
.catch((error) => {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
this.#
|
|
523
|
-
this.#
|
|
526
|
+
// The socket counts as released: one the client kept open reports
|
|
527
|
+
// `connected` in its diagnostics and is adopted after a catch-up.
|
|
528
|
+
this.#disconnectCause = causeCode(error);
|
|
529
|
+
this.#retry();
|
|
530
|
+
this.#publish(this.#snapshot);
|
|
524
531
|
})
|
|
525
532
|
.finally(() => {
|
|
526
533
|
this.#disconnecting = undefined;
|
package/dist/worker-host.d.ts
CHANGED
|
@@ -305,6 +305,7 @@ export declare class SyncClientHandle implements PromiseMethods<WorkerApi> {
|
|
|
305
305
|
units(): Promise<UnitRecord[]>;
|
|
306
306
|
unit(id: string): Promise<UnitRecord | undefined>;
|
|
307
307
|
connectRealtime(): Promise<void>;
|
|
308
|
+
/** A closed handle holds no socket, so its disconnect succeeds. */
|
|
308
309
|
disconnectRealtime(): Promise<void>;
|
|
309
310
|
/** §8.6: publish/clear a scope-keyed presence document. */
|
|
310
311
|
setPresence(scopeKey: string, doc: Record<string, unknown> | null): Promise<void>;
|
package/dist/worker-host.js
CHANGED
|
@@ -451,7 +451,10 @@ export class SyncClientHandle {
|
|
|
451
451
|
connectRealtime() {
|
|
452
452
|
return this.#call('connectRealtime', []);
|
|
453
453
|
}
|
|
454
|
+
/** A closed handle holds no socket, so its disconnect succeeds. */
|
|
454
455
|
disconnectRealtime() {
|
|
456
|
+
if (this.#closed)
|
|
457
|
+
return Promise.resolve();
|
|
455
458
|
return this.#call('disconnectRealtime', []);
|
|
456
459
|
}
|
|
457
460
|
/** §8.6: publish/clear a scope-keyed presence document. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.71.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.71.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.71.0",
|
|
129
129
|
"playwright": "1.63.0",
|
|
130
130
|
"vite": "8.3.4"
|
|
131
131
|
}
|
package/src/client.ts
CHANGED
|
@@ -191,6 +191,7 @@ import {
|
|
|
191
191
|
type ResolveCommitOutcomeInput,
|
|
192
192
|
commitOutcome as readCommitOutcome,
|
|
193
193
|
recordCommitOutcome,
|
|
194
|
+
scrubPurgedOutcomes,
|
|
194
195
|
} from './outcomes';
|
|
195
196
|
import {
|
|
196
197
|
buildPreviousVersionAudit,
|
|
@@ -3781,6 +3782,17 @@ export class SyncClient {
|
|
|
3781
3782
|
);
|
|
3782
3783
|
}
|
|
3783
3784
|
}
|
|
3785
|
+
if (
|
|
3786
|
+
scrubPurgedOutcomes(this.#db, (table, rowId, values) =>
|
|
3787
|
+
(targetsByTable.get(table) ?? []).some(
|
|
3788
|
+
(target) =>
|
|
3789
|
+
rowIds.get(table)?.has(rowId) === true ||
|
|
3790
|
+
(values !== undefined &&
|
|
3791
|
+
localDataPurgeTargetMatches(target, values)),
|
|
3792
|
+
),
|
|
3793
|
+
)
|
|
3794
|
+
)
|
|
3795
|
+
batch.outcomes();
|
|
3784
3796
|
|
|
3785
3797
|
if (doomed.length > 0) {
|
|
3786
3798
|
// §5.11 purge erasure: the journal keeps a dropped commit's identity and codes,
|
package/src/native-bridge.ts
CHANGED
|
@@ -417,6 +417,7 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
417
417
|
'beginSecurityPreflight',
|
|
418
418
|
'activateSecurity',
|
|
419
419
|
'setTransportEnabled',
|
|
420
|
+
'disconnectRealtime',
|
|
420
421
|
'purgeLocalData',
|
|
421
422
|
'previousVersionDiscard',
|
|
422
423
|
'localRevision',
|
|
@@ -1177,7 +1178,9 @@ export abstract class NativeBridgeClient implements PromiseMethods<
|
|
|
1177
1178
|
await this.command('connectRealtime', {});
|
|
1178
1179
|
}
|
|
1179
1180
|
|
|
1181
|
+
/** A closed client holds no socket, so its disconnect succeeds. */
|
|
1180
1182
|
async disconnectRealtime(): Promise<void> {
|
|
1183
|
+
if (this.#closed) return;
|
|
1181
1184
|
await this.command('disconnectRealtime', {});
|
|
1182
1185
|
}
|
|
1183
1186
|
|
package/src/outcomes.ts
CHANGED
|
@@ -530,3 +530,86 @@ export function activeFailureRecords(outcomes: readonly CommitOutcome[]): {
|
|
|
530
530
|
}
|
|
531
531
|
return { conflicts, rejections };
|
|
532
532
|
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* §5.11 purge erasure: every journal entry with an operation, or a conflict
|
|
536
|
+
* row, that `touches` accepts keeps its identity, status, ids and codes only.
|
|
537
|
+
* Returns whether an entry changed.
|
|
538
|
+
*/
|
|
539
|
+
export function scrubPurgedOutcomes(
|
|
540
|
+
db: ClientDatabase,
|
|
541
|
+
touches: (
|
|
542
|
+
table: string,
|
|
543
|
+
rowId: string,
|
|
544
|
+
values: Readonly<Record<string, unknown>> | undefined,
|
|
545
|
+
) => boolean,
|
|
546
|
+
): boolean {
|
|
547
|
+
let changed = false;
|
|
548
|
+
for (const row of db.query(
|
|
549
|
+
`SELECT client_commit_id,results,operations FROM ${OUTCOMES_TABLE}`,
|
|
550
|
+
)) {
|
|
551
|
+
const results = JSON.parse(
|
|
552
|
+
String(row.results),
|
|
553
|
+
) as StoredCommitOperationOutcome[];
|
|
554
|
+
const operations =
|
|
555
|
+
typeof row.operations === 'string'
|
|
556
|
+
? (JSON.parse(row.operations) as OutboxOperation[])
|
|
557
|
+
: [];
|
|
558
|
+
const named = [
|
|
559
|
+
...operations,
|
|
560
|
+
...results.flatMap((result) =>
|
|
561
|
+
result.status === 'conflict'
|
|
562
|
+
? [
|
|
563
|
+
{
|
|
564
|
+
table: result.conflict.table,
|
|
565
|
+
rowId: result.conflict.rowId,
|
|
566
|
+
values: result.conflict.serverRow,
|
|
567
|
+
},
|
|
568
|
+
...(result.conflict.operation === undefined
|
|
569
|
+
? []
|
|
570
|
+
: [result.conflict.operation]),
|
|
571
|
+
]
|
|
572
|
+
: result.status === 'error' &&
|
|
573
|
+
result.rejection.operation !== undefined
|
|
574
|
+
? [result.rejection.operation]
|
|
575
|
+
: [],
|
|
576
|
+
),
|
|
577
|
+
];
|
|
578
|
+
if (!named.some((op) => touches(op.table, op.rowId, op.values))) continue;
|
|
579
|
+
const scrubbed: StoredCommitOperationOutcome[] = results.map((result) => {
|
|
580
|
+
if (result.status === 'conflict') {
|
|
581
|
+
const { clientCommitId, opIndex, table, rowId, code, message } =
|
|
582
|
+
result.conflict;
|
|
583
|
+
return {
|
|
584
|
+
status: 'conflict',
|
|
585
|
+
conflict: {
|
|
586
|
+
clientCommitId,
|
|
587
|
+
opIndex,
|
|
588
|
+
table,
|
|
589
|
+
rowId,
|
|
590
|
+
code,
|
|
591
|
+
message,
|
|
592
|
+
serverVersion: result.conflict.serverVersion,
|
|
593
|
+
serverRow: {},
|
|
594
|
+
conflictColumns: [],
|
|
595
|
+
},
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
if (result.status === 'error') {
|
|
599
|
+
const { clientCommitId, opIndex, code, message, retryable } =
|
|
600
|
+
result.rejection;
|
|
601
|
+
return {
|
|
602
|
+
status: 'error',
|
|
603
|
+
rejection: { clientCommitId, opIndex, code, message, retryable },
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
return result;
|
|
607
|
+
});
|
|
608
|
+
db.exec(
|
|
609
|
+
`UPDATE ${OUTCOMES_TABLE} SET results=?,operations=NULL WHERE client_commit_id=?`,
|
|
610
|
+
[JSON.stringify(scrubbed), String(row.client_commit_id)],
|
|
611
|
+
);
|
|
612
|
+
changed = true;
|
|
613
|
+
}
|
|
614
|
+
return changed;
|
|
615
|
+
}
|
|
@@ -48,12 +48,16 @@ export interface RealtimeSupervisorSnapshot {
|
|
|
48
48
|
readonly retryDelayMs?: number;
|
|
49
49
|
/** The latched failure of phase `unsupported` (or `stopped`). */
|
|
50
50
|
readonly failureCode?: RealtimeSupervisorFailureCode;
|
|
51
|
+
/**
|
|
52
|
+
* The Syncular code (or `unknown`) the latest refused
|
|
53
|
+
* `disconnectRealtime()` rejected with, kept until the next `connected`.
|
|
54
|
+
*/
|
|
55
|
+
readonly disconnectCauseCode?: string;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
/** SPEC §10.3: client-local supervisor failures, never on the wire. */
|
|
54
59
|
export type RealtimeSupervisorFailureCode =
|
|
55
60
|
| 'realtime.diagnostics_unavailable'
|
|
56
|
-
| 'realtime.disconnect_failed'
|
|
57
61
|
| 'realtime.cleanup_failed';
|
|
58
62
|
|
|
59
63
|
export interface RealtimeSupervisorOptions {
|
|
@@ -262,8 +266,6 @@ export class RealtimeSupervisorError extends ClientSyncError {
|
|
|
262
266
|
declare readonly code: RealtimeSupervisorFailureCode;
|
|
263
267
|
declare readonly cleanupFailureCode?: 'realtime.cleanup_failed';
|
|
264
268
|
declare readonly diagnosticsFailureCode?: 'realtime.diagnostics_unavailable';
|
|
265
|
-
/** The code the client's `disconnectRealtime` rejected with. */
|
|
266
|
-
declare readonly disconnectCauseCode?: string;
|
|
267
269
|
/** Set by the installed `close()` when the client close failed as well. */
|
|
268
270
|
declare readonly stopFailureCode?: RealtimeSupervisorFailureCode;
|
|
269
271
|
declare readonly closeFailureCode?: string;
|
|
@@ -285,8 +287,9 @@ export class RealtimeSupervisorError extends ClientSyncError {
|
|
|
285
287
|
* exponential jitter, and the backoff survives offline, background and
|
|
286
288
|
* protected suspensions. While no socket is connected, each failed connect,
|
|
287
289
|
* each host `online` report and each foreground return drives one HTTP round
|
|
288
|
-
* (§1.8). A disconnect
|
|
289
|
-
*
|
|
290
|
+
* (§1.8). A refused disconnect counts one retry and names its cause in the
|
|
291
|
+
* snapshot. A diagnostics or cleanup failure latches phase `unsupported` with
|
|
292
|
+
* its `failureCode`, and `stop()` rejects with it.
|
|
290
293
|
*/
|
|
291
294
|
export class RealtimeSupervisor {
|
|
292
295
|
readonly #client: RealtimeSupervisorClient;
|
|
@@ -330,8 +333,8 @@ export class RealtimeSupervisor {
|
|
|
330
333
|
readonly #failures: {
|
|
331
334
|
cleanupFailureCode?: 'realtime.cleanup_failed';
|
|
332
335
|
diagnosticsFailureCode?: 'realtime.diagnostics_unavailable';
|
|
333
|
-
disconnectCauseCode?: string;
|
|
334
336
|
} = {};
|
|
337
|
+
#disconnectCause: string | undefined;
|
|
335
338
|
#snapshot: RealtimeSupervisorSnapshot = { phase: 'idle', attempt: 0 };
|
|
336
339
|
|
|
337
340
|
constructor(
|
|
@@ -487,7 +490,11 @@ export class RealtimeSupervisor {
|
|
|
487
490
|
});
|
|
488
491
|
}
|
|
489
492
|
|
|
490
|
-
#publish(
|
|
493
|
+
#publish(next: RealtimeSupervisorSnapshot): void {
|
|
494
|
+
const snapshot =
|
|
495
|
+
this.#disconnectCause === undefined
|
|
496
|
+
? next
|
|
497
|
+
: { ...next, disconnectCauseCode: this.#disconnectCause };
|
|
491
498
|
if (JSON.stringify(this.#snapshot) === JSON.stringify(snapshot)) return;
|
|
492
499
|
this.#snapshot = Object.freeze(snapshot);
|
|
493
500
|
for (const listener of this.#listeners) {
|
|
@@ -639,6 +646,7 @@ export class RealtimeSupervisor {
|
|
|
639
646
|
this.#connected = true;
|
|
640
647
|
this.#attempt = 0;
|
|
641
648
|
this.#delayMs = 0;
|
|
649
|
+
this.#disconnectCause = undefined;
|
|
642
650
|
this.#publish({ phase: 'connected', attempt: 0 });
|
|
643
651
|
return;
|
|
644
652
|
}
|
|
@@ -674,9 +682,11 @@ export class RealtimeSupervisor {
|
|
|
674
682
|
resolve(this.#client.disconnectRealtime()),
|
|
675
683
|
)
|
|
676
684
|
.catch((error: unknown) => {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
this.#
|
|
685
|
+
// The socket counts as released: one the client kept open reports
|
|
686
|
+
// `connected` in its diagnostics and is adopted after a catch-up.
|
|
687
|
+
this.#disconnectCause = causeCode(error);
|
|
688
|
+
this.#retry();
|
|
689
|
+
this.#publish(this.#snapshot);
|
|
680
690
|
})
|
|
681
691
|
.finally(() => {
|
|
682
692
|
this.#disconnecting = undefined;
|
package/src/worker-host.ts
CHANGED
|
@@ -828,7 +828,9 @@ export class SyncClientHandle implements PromiseMethods<WorkerApi> {
|
|
|
828
828
|
return this.#call('connectRealtime', []);
|
|
829
829
|
}
|
|
830
830
|
|
|
831
|
+
/** A closed handle holds no socket, so its disconnect succeeds. */
|
|
831
832
|
disconnectRealtime(): Promise<void> {
|
|
833
|
+
if (this.#closed) return Promise.resolve();
|
|
832
834
|
return this.#call('disconnectRealtime', []);
|
|
833
835
|
}
|
|
834
836
|
|