@terreno/syncdb 57.3.0 → 57.6.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 -3
- package/dist/auth/betterAuthAdapter.js +1 -1
- package/dist/auth/betterAuthAdapter.js.map +1 -1
- package/dist/auth/betterAuthReactClient.d.ts +17 -0
- package/dist/auth/betterAuthReactClient.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.js +11 -0
- package/dist/auth/betterAuthReactClient.js.map +1 -0
- package/dist/auth/betterAuthReactClient.test.d.ts +2 -0
- package/dist/auth/betterAuthReactClient.test.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.test.js +43 -0
- package/dist/auth/betterAuthReactClient.test.js.map +1 -0
- package/dist/client.adminWindowMutation.test.d.ts +2 -0
- package/dist/client.adminWindowMutation.test.d.ts.map +1 -0
- package/dist/client.adminWindowMutation.test.js +65 -0
- package/dist/client.adminWindowMutation.test.js.map +1 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +89 -12
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +308 -12
- package/dist/client.test.js.map +1 -1
- package/dist/codegen/emitSdk.js +3 -3
- package/dist/codegen/emitSdk.js.map +1 -1
- package/dist/codegen/generate.test.js +32 -2
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/safeIdentifiers.d.ts +5 -0
- package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
- package/dist/codegen/safeIdentifiers.js +8 -0
- package/dist/codegen/safeIdentifiers.js.map +1 -1
- package/dist/codegen/safeIdentifiers.test.js +20 -8
- package/dist/codegen/safeIdentifiers.test.js.map +1 -1
- package/dist/codegen/types.d.ts +4 -3
- package/dist/codegen/types.d.ts.map +1 -1
- package/dist/crypto/keyProviders.js +1 -1
- package/dist/crypto/keyProviders.js.map +1 -1
- package/dist/debug/debugChannel.test.js +5 -0
- package/dist/debug/debugChannel.test.js.map +1 -1
- package/dist/debug/debugLog.d.ts +1 -0
- package/dist/debug/debugLog.d.ts.map +1 -1
- package/dist/debug/debugLog.js +1 -0
- package/dist/debug/debugLog.js.map +1 -1
- package/dist/debug/devtools.d.ts +6 -0
- package/dist/debug/devtools.d.ts.map +1 -0
- package/dist/debug/devtools.js +111 -0
- package/dist/debug/devtools.js.map +1 -0
- package/dist/debug/devtools.test.d.ts +2 -0
- package/dist/debug/devtools.test.d.ts.map +1 -0
- package/dist/debug/devtools.test.js +168 -0
- package/dist/debug/devtools.test.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js +19 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js.map +1 -0
- package/dist/mutations/outbox.d.ts +7 -3
- package/dist/mutations/outbox.d.ts.map +1 -1
- package/dist/mutations/outbox.js +6 -0
- package/dist/mutations/outbox.js.map +1 -1
- package/dist/mutations/outbox.test.js +17 -0
- package/dist/mutations/outbox.test.js.map +1 -1
- package/dist/react/collectionHooks.js +2 -2
- package/dist/react/collectionHooks.js.map +1 -1
- package/dist/storage/idb.d.ts +1 -0
- package/dist/storage/idb.d.ts.map +1 -1
- package/dist/storage/idb.js +1 -0
- package/dist/storage/idb.js.map +1 -1
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +1 -0
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/store.d.ts +3 -2
- package/dist/storage/store.d.ts.map +1 -1
- package/dist/storage/store.test.js +6 -6
- package/dist/storage/store.test.js.map +1 -1
- package/dist/storage/types.d.ts +1 -7
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync/deltaApplier.d.ts +2 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -1
- package/dist/sync/deltaApplier.js +9 -0
- package/dist/sync/deltaApplier.js.map +1 -1
- package/dist/sync/deltaApplier.test.js +63 -0
- package/dist/sync/deltaApplier.test.js.map +1 -1
- package/dist/sync/entityRepair.d.ts +1 -0
- package/dist/sync/entityRepair.d.ts.map +1 -1
- package/dist/sync/entityRepair.js +1 -0
- package/dist/sync/entityRepair.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +3 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +3 -1
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +68 -0
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/sync/replayCoordinator.d.ts +6 -12
- package/dist/sync/replayCoordinator.d.ts.map +1 -1
- package/dist/sync/replayCoordinator.js +44 -16
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/replayCoordinator.test.js +29 -10
- package/dist/sync/replayCoordinator.test.js.map +1 -1
- package/dist/sync/socketTransport.d.ts +3 -45
- package/dist/sync/socketTransport.d.ts.map +1 -1
- package/dist/sync/socketTransport.js +73 -9
- package/dist/sync/socketTransport.js.map +1 -1
- package/dist/sync/socketTransport.test.js +41 -1
- package/dist/sync/socketTransport.test.js.map +1 -1
- package/dist/sync/transport.d.ts +9 -1
- package/dist/sync/transport.d.ts.map +1 -1
- package/dist/sync/transport.js +1 -1
- package/dist/sync/transport.js.map +1 -1
- package/dist/sync/windowHydrate.d.ts +25 -0
- package/dist/sync/windowHydrate.d.ts.map +1 -0
- package/dist/sync/windowHydrate.js +67 -0
- package/dist/sync/windowHydrate.js.map +1 -0
- package/dist/sync/windowHydrate.test.d.ts +2 -0
- package/dist/sync/windowHydrate.test.d.ts.map +1 -0
- package/dist/sync/windowHydrate.test.js +164 -0
- package/dist/sync/windowHydrate.test.js.map +1 -0
- package/dist/testing/fakeTransport.js +1 -1
- package/dist/testing/fakeTransport.js.map +1 -1
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -4
- package/src/auth/betterAuthReactClient.test.ts +50 -0
- package/src/auth/betterAuthReactClient.ts +33 -0
- package/src/client.adminWindowMutation.test.ts +69 -0
- package/src/client.test.ts +321 -3
- package/src/client.ts +125 -8
- package/src/codegen/emitSdk.ts +3 -3
- package/src/codegen/generate.test.ts +35 -2
- package/src/codegen/safeIdentifiers.test.ts +37 -10
- package/src/codegen/safeIdentifiers.ts +17 -0
- package/src/codegen/types.ts +3 -3
- package/src/debug/debugChannel.test.ts +6 -0
- package/src/debug/debugLog.ts +1 -0
- package/src/debug/devtools.test.ts +234 -0
- package/src/debug/devtools.ts +196 -0
- package/src/index.ts +4 -0
- package/src/isolated/defaultPersisterFactoryNativeErrors.isolated.ts +23 -0
- package/src/mutations/outbox.test.ts +20 -0
- package/src/mutations/outbox.ts +11 -3
- package/src/react/collectionHooks.ts +2 -2
- package/src/storage/idb.ts +1 -0
- package/src/storage/schema.ts +1 -0
- package/src/storage/store.ts +2 -2
- package/src/storage/types.ts +1 -8
- package/src/sync/deltaApplier.test.ts +70 -0
- package/src/sync/deltaApplier.ts +10 -0
- package/src/sync/entityRepair.ts +1 -0
- package/src/sync/httpChannel.test.ts +82 -0
- package/src/sync/httpChannel.ts +5 -0
- package/src/sync/replayCoordinator.test.ts +28 -0
- package/src/sync/replayCoordinator.ts +53 -14
- package/src/sync/socketTransport.test.ts +45 -2
- package/src/sync/socketTransport.ts +106 -20
- package/src/sync/transport.ts +7 -1
- package/src/sync/windowHydrate.test.ts +179 -0
- package/src/sync/windowHydrate.ts +97 -0
- package/src/testing/fakeTransport.ts +1 -1
- package/src/types.ts +16 -0
|
@@ -266,6 +266,26 @@ describe("state machine transitions", () => {
|
|
|
266
266
|
expect(outbox.getMutation({mutationId: retry.mutationId})?.baseVersion).toBe(3);
|
|
267
267
|
});
|
|
268
268
|
|
|
269
|
+
it("requeue preserves an admin-window mutation mode", () => {
|
|
270
|
+
const outbox = makeOutbox();
|
|
271
|
+
outbox.enqueue({
|
|
272
|
+
args: {title: "Admin retry"},
|
|
273
|
+
collection: "todos",
|
|
274
|
+
entityId: "t1",
|
|
275
|
+
mutationId: "m1",
|
|
276
|
+
mutationMode: "adminWindow",
|
|
277
|
+
operation: "update",
|
|
278
|
+
userId: "user-1",
|
|
279
|
+
});
|
|
280
|
+
outbox.markInFlight({mutationId: "m1"});
|
|
281
|
+
outbox.markConflicted({mutationId: "m1"});
|
|
282
|
+
|
|
283
|
+
const retry = outbox.requeue({baseVersion: 9, mutationId: "m1"});
|
|
284
|
+
|
|
285
|
+
expect(retry.mutationMode).toBe("adminWindow");
|
|
286
|
+
expect(outbox.getMutation({mutationId: retry.mutationId})?.mutationMode).toBe("adminWindow");
|
|
287
|
+
});
|
|
288
|
+
|
|
269
289
|
it("requeue copies maxAttempts onto the cloned row", () => {
|
|
270
290
|
const outbox = makeOutbox();
|
|
271
291
|
outbox.enqueue({
|
package/src/mutations/outbox.ts
CHANGED
|
@@ -3,11 +3,12 @@ import type {Row} from "tinybase";
|
|
|
3
3
|
|
|
4
4
|
import type {SyncStore} from "../storage/store";
|
|
5
5
|
import {CONFLICTS_TABLE, OUTBOX_TABLE, type OutboxRow} from "../storage/types";
|
|
6
|
-
import type {OutboxMutation, OutboxStatus, SyncMutationOperation} from "../types";
|
|
6
|
+
import type {OutboxMutation, OutboxStatus, SyncMutationMode, SyncMutationOperation} from "../types";
|
|
7
7
|
|
|
8
8
|
const defaultNow = (): string => DateTime.now().toISO();
|
|
9
9
|
|
|
10
10
|
/** Default number of failed rows retained by `prune()` for debugging/UI. */
|
|
11
|
+
/** @internal */
|
|
11
12
|
export const DEFAULT_KEEP_FAILED = 50;
|
|
12
13
|
|
|
13
14
|
/** Generate a stable client mutation id (idempotency key). */
|
|
@@ -46,12 +47,13 @@ const rowToMutation = (mutationId: string, row: Partial<OutboxRow>): OutboxMutat
|
|
|
46
47
|
errorNackCount: row.errorNackCount ?? 0,
|
|
47
48
|
maxAttempts: typeof row.maxAttempts === "number" ? row.maxAttempts : undefined,
|
|
48
49
|
mutationId,
|
|
50
|
+
mutationMode: row.mutationMode,
|
|
49
51
|
operation: (row.operation ?? "update") as SyncMutationOperation,
|
|
50
52
|
status: (row.status ?? "queued") as OutboxStatus,
|
|
51
53
|
userId: row.userId ?? "",
|
|
52
54
|
});
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
interface EnqueueArgs {
|
|
55
57
|
collection: string;
|
|
56
58
|
operation: SyncMutationOperation;
|
|
57
59
|
entityId: string;
|
|
@@ -68,9 +70,11 @@ export interface EnqueueArgs {
|
|
|
68
70
|
userId: string;
|
|
69
71
|
/** Optional explicit id (defaults to a generated UUID; useful in tests). */
|
|
70
72
|
mutationId?: string;
|
|
73
|
+
/** Admin-window marker persisted for replay after restart. */
|
|
74
|
+
mutationMode?: SyncMutationMode;
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
|
|
77
|
+
interface RecoverStartupStateResult {
|
|
74
78
|
/** mutationIds that were stranded `inFlight` and moved back to `queued`. */
|
|
75
79
|
recoveredInFlight: string[];
|
|
76
80
|
/** entityIds whose stale `pendingMutationId` was cleared (acked-with-pending). */
|
|
@@ -255,6 +259,9 @@ export const createOutbox = ({
|
|
|
255
259
|
if (args.maxAttempts !== undefined) {
|
|
256
260
|
row.maxAttempts = args.maxAttempts;
|
|
257
261
|
}
|
|
262
|
+
if (args.mutationMode !== undefined) {
|
|
263
|
+
row.mutationMode = args.mutationMode;
|
|
264
|
+
}
|
|
258
265
|
store.raw.setRow(OUTBOX_TABLE, mutationId, row as unknown as Row);
|
|
259
266
|
return rowToMutation(mutationId, row);
|
|
260
267
|
};
|
|
@@ -353,6 +360,7 @@ export const createOutbox = ({
|
|
|
353
360
|
enqueueOrder: row.enqueueOrder ?? 0,
|
|
354
361
|
entityId: row.entityId ?? "",
|
|
355
362
|
errorNackCount: 0,
|
|
363
|
+
...(row.mutationMode !== undefined ? {mutationMode: row.mutationMode} : {}),
|
|
356
364
|
operation: row.operation ?? "update",
|
|
357
365
|
status: "queued",
|
|
358
366
|
userId: row.userId ?? "",
|
|
@@ -75,7 +75,7 @@ export const createCollectionHooks = <
|
|
|
75
75
|
id: args.id,
|
|
76
76
|
...(maxAttempts !== undefined ? {maxAttempts} : {}),
|
|
77
77
|
}),
|
|
78
|
-
[
|
|
78
|
+
[update]
|
|
79
79
|
);
|
|
80
80
|
return useMemo(() => [trigger], [trigger]);
|
|
81
81
|
};
|
|
@@ -88,7 +88,7 @@ export const createCollectionHooks = <
|
|
|
88
88
|
id: args.id,
|
|
89
89
|
...(maxAttempts !== undefined ? {maxAttempts} : {}),
|
|
90
90
|
}),
|
|
91
|
-
[
|
|
91
|
+
[remove]
|
|
92
92
|
);
|
|
93
93
|
return useMemo(() => [trigger], [trigger]);
|
|
94
94
|
};
|
package/src/storage/idb.ts
CHANGED
package/src/storage/schema.ts
CHANGED
|
@@ -84,6 +84,7 @@ export const buildTablesSchema = ({collections}: {collections: string[]}): Table
|
|
|
84
84
|
entityId: {type: "string"},
|
|
85
85
|
errorNackCount: {type: "number"},
|
|
86
86
|
maxAttempts: {type: "number"},
|
|
87
|
+
mutationMode: {type: "string"},
|
|
87
88
|
operation: {type: "string"},
|
|
88
89
|
status: {type: "string"},
|
|
89
90
|
userId: {type: "string"},
|
package/src/storage/store.ts
CHANGED
|
@@ -39,7 +39,7 @@ const rowToEntity = <TData>(id: string, row: Partial<EntityRow>): SyncEntity<TDa
|
|
|
39
39
|
stream: row.stream ? row.stream : undefined,
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
interface UpsertEntityArgs {
|
|
43
43
|
collection: string;
|
|
44
44
|
id: string;
|
|
45
45
|
data: unknown;
|
|
@@ -53,7 +53,7 @@ export interface UpsertEntityArgs {
|
|
|
53
53
|
stream?: string;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
interface CompactTombstonesResult {
|
|
57
57
|
/** Number of tombstone rows removed (across all configured collections). */
|
|
58
58
|
removed: number;
|
|
59
59
|
}
|
package/src/storage/types.ts
CHANGED
|
@@ -66,14 +66,6 @@ export interface EntityRow {
|
|
|
66
66
|
stream: string;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/** Primitive row shape for the `_knownStreams` table; rowId = stream key. */
|
|
70
|
-
export interface KnownStreamRow {
|
|
71
|
-
/** The collection tag the stream belongs to. */
|
|
72
|
-
collection: string;
|
|
73
|
-
/** When the stream was first bootstrapped. */
|
|
74
|
-
addedAt: string;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
69
|
/** Primitive row shape for the `_outbox` table; rowId = mutationId. */
|
|
78
70
|
export interface OutboxRow {
|
|
79
71
|
/** JSON-encoded mutation args. */
|
|
@@ -98,6 +90,7 @@ export interface OutboxRow {
|
|
|
98
90
|
operation: SyncMutationOperation;
|
|
99
91
|
status: OutboxStatus;
|
|
100
92
|
userId: string;
|
|
93
|
+
mutationMode?: "adminWindow";
|
|
101
94
|
}
|
|
102
95
|
|
|
103
96
|
/** Primitive row shape for the `_cursors` table; rowId = stream key. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
|
|
3
4
|
import {createSyncStore, type SyncStore} from "../storage/store";
|
|
4
5
|
import {NEEDS_REPAIR_TABLE} from "../storage/types";
|
|
@@ -235,3 +236,72 @@ describe("applyDelta", () => {
|
|
|
235
236
|
);
|
|
236
237
|
});
|
|
237
238
|
});
|
|
239
|
+
|
|
240
|
+
describe("applyDelta window |admin (Task 3.5)", () => {
|
|
241
|
+
const ADMIN_STREAM = "todos|admin";
|
|
242
|
+
|
|
243
|
+
it("is a no-op for an unknown id (no surprise insert) and still advances the cursor", () => {
|
|
244
|
+
const store = makeStore();
|
|
245
|
+
const result = applyDelta({
|
|
246
|
+
delta: makeDelta({
|
|
247
|
+
data: {title: "surprise"},
|
|
248
|
+
id: "unknown",
|
|
249
|
+
method: "create",
|
|
250
|
+
seq: 4,
|
|
251
|
+
stream: ADMIN_STREAM,
|
|
252
|
+
}),
|
|
253
|
+
store,
|
|
254
|
+
});
|
|
255
|
+
assert.isFalse(result.applied);
|
|
256
|
+
assert.isUndefined(store.getEntity({collection: "todos", id: "unknown"}));
|
|
257
|
+
assert.equal(getCursor({store, stream: ADMIN_STREAM}), 4);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("updates a known id on the admin stream", () => {
|
|
261
|
+
const store = makeStore();
|
|
262
|
+
store.upsertEntity({
|
|
263
|
+
collection: "todos",
|
|
264
|
+
data: {title: "old"},
|
|
265
|
+
id: "t1",
|
|
266
|
+
seq: 1,
|
|
267
|
+
stream: ADMIN_STREAM,
|
|
268
|
+
});
|
|
269
|
+
const result = applyDelta({
|
|
270
|
+
delta: makeDelta({
|
|
271
|
+
data: {title: "new"},
|
|
272
|
+
method: "update",
|
|
273
|
+
seq: 2,
|
|
274
|
+
stream: ADMIN_STREAM,
|
|
275
|
+
}),
|
|
276
|
+
store,
|
|
277
|
+
});
|
|
278
|
+
assert.isTrue(result.applied);
|
|
279
|
+
assert.equal(
|
|
280
|
+
store.getEntity<{title: string}>({collection: "todos", id: "t1"})?.data.title,
|
|
281
|
+
"new"
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("delete delta removes a known id and ignores an unknown id", () => {
|
|
286
|
+
const store = makeStore();
|
|
287
|
+
store.upsertEntity({
|
|
288
|
+
collection: "todos",
|
|
289
|
+
data: {title: "gone"},
|
|
290
|
+
id: "t1",
|
|
291
|
+
seq: 1,
|
|
292
|
+
stream: ADMIN_STREAM,
|
|
293
|
+
});
|
|
294
|
+
const known = applyDelta({
|
|
295
|
+
delta: makeDelta({deleted: true, id: "t1", method: "delete", seq: 2, stream: ADMIN_STREAM}),
|
|
296
|
+
store,
|
|
297
|
+
});
|
|
298
|
+
const unknown = applyDelta({
|
|
299
|
+
delta: makeDelta({deleted: true, id: "nope", method: "delete", seq: 3, stream: ADMIN_STREAM}),
|
|
300
|
+
store,
|
|
301
|
+
});
|
|
302
|
+
assert.isTrue(known.applied);
|
|
303
|
+
assert.isTrue(store.getEntity({collection: "todos", id: "t1"})?.deleted);
|
|
304
|
+
assert.isFalse(unknown.applied);
|
|
305
|
+
assert.isUndefined(store.getEntity({collection: "todos", id: "nope"}));
|
|
306
|
+
});
|
|
307
|
+
});
|
package/src/sync/deltaApplier.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface ApplyDeltaResult {
|
|
|
22
22
|
* optimistic state is protected until conflict resolution decides;
|
|
23
23
|
* - create/update writes data + seq; tombstones set `deleted: true` (keeping
|
|
24
24
|
* the last known data for conflict UIs);
|
|
25
|
+
* - `{collection}|admin` window streams skip inserts for unknown ids (no surprise
|
|
26
|
+
* rows); Refresh is a REST re-query plus `hydrateWindow`, not a snapshot;
|
|
25
27
|
* - the cursor for `delta.stream` advances in every case (including skips), so
|
|
26
28
|
* a skipped delta is never re-fetched on catch-up.
|
|
27
29
|
*/
|
|
@@ -66,6 +68,14 @@ export const applyDelta = ({
|
|
|
66
68
|
return {applied: false, seqJump};
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
// Admin window (`{collection}|admin`): apply update/delete only for ids already
|
|
72
|
+
// in the local window. Unknown ids wait for REST membership + hydrateWindow
|
|
73
|
+
// (Refresh / load more). Cursor still advances so live seq is not replayed.
|
|
74
|
+
if (!existing && delta.stream.endsWith("|admin")) {
|
|
75
|
+
advanceCursor();
|
|
76
|
+
return {applied: false, seqJump};
|
|
77
|
+
}
|
|
78
|
+
|
|
69
79
|
const deleted = delta.deleted === true || delta.method === "delete";
|
|
70
80
|
store.upsertEntity({
|
|
71
81
|
collection: delta.collection,
|
package/src/sync/entityRepair.ts
CHANGED
|
@@ -12,6 +12,7 @@ export const MAX_REPAIR_FETCH_IDS = 100;
|
|
|
12
12
|
* needs-repair mark) when the entity is still pending or conflicted so a
|
|
13
13
|
* reconcile-time repair pass cannot clobber optimistic local state.
|
|
14
14
|
*/
|
|
15
|
+
/** @internal */
|
|
15
16
|
export const applyRepairedEntity = ({
|
|
16
17
|
store,
|
|
17
18
|
collection,
|
|
@@ -57,6 +57,27 @@ describe("createHttpChannel", () => {
|
|
|
57
57
|
expect(headersOf(requests[0]).Authorization).toBe("Bearer token-123");
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
+
it("attaches X-Organization-Id when an organizationIdProvider returns an id", async () => {
|
|
61
|
+
const {fetchImpl, requests} = makeFetch(() =>
|
|
62
|
+
json({
|
|
63
|
+
cursor: 0,
|
|
64
|
+
entities: [],
|
|
65
|
+
frontierSeq: 0,
|
|
66
|
+
hasMore: false,
|
|
67
|
+
oldestRetainedSeq: 0,
|
|
68
|
+
stream: "todos|owner:u1",
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
const channel = createHttpChannel({
|
|
72
|
+
authProvider,
|
|
73
|
+
baseUrl: "http://api",
|
|
74
|
+
fetchImpl,
|
|
75
|
+
organizationIdProvider: () => "org-selected",
|
|
76
|
+
});
|
|
77
|
+
await channel.fetchSnapshotPage({cursor: 0, stream: "todos|owner:u1"});
|
|
78
|
+
expect(headersOf(requests[0])["X-Organization-Id"]).toBe("org-selected");
|
|
79
|
+
});
|
|
80
|
+
|
|
60
81
|
it("forwards the legacyCursor token (C3) when provided", async () => {
|
|
61
82
|
const page = {
|
|
62
83
|
cursor: 0,
|
|
@@ -220,6 +241,67 @@ describe("createHttpChannel", () => {
|
|
|
220
241
|
});
|
|
221
242
|
});
|
|
222
243
|
|
|
244
|
+
describe("fetchEntities", () => {
|
|
245
|
+
it("GETs /sync/entities with collection and ids", async () => {
|
|
246
|
+
const body = {
|
|
247
|
+
entities: [{data: {title: "a"}, deleted: false, id: "a", seq: 1}],
|
|
248
|
+
};
|
|
249
|
+
const {fetchImpl, requests} = makeFetch(() => json(body));
|
|
250
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
251
|
+
await expect(channel.fetchEntities({collection: "todos", ids: ["a", "b"]})).resolves.toEqual(
|
|
252
|
+
body
|
|
253
|
+
);
|
|
254
|
+
expect(requests[0]?.input).toBe("http://api/sync/entities?collection=todos&ids=a%2Cb");
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("rejects on non-ok statuses", async () => {
|
|
258
|
+
const {fetchImpl} = makeFetch(() => json({}, 500));
|
|
259
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
260
|
+
await expect(channel.fetchEntities({collection: "todos", ids: ["a"]})).rejects.toThrow(
|
|
261
|
+
"status 500"
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe("sendMutationBatch", () => {
|
|
267
|
+
const batch = {
|
|
268
|
+
mutations: [
|
|
269
|
+
{
|
|
270
|
+
collection: "todos",
|
|
271
|
+
data: {title: "hi"},
|
|
272
|
+
id: "t1",
|
|
273
|
+
mutationId: "m1",
|
|
274
|
+
operation: "create" as const,
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
it("maps 200 results to a results payload", async () => {
|
|
280
|
+
const results = [{ack: {id: "t1", mutationId: "m1", seq: 1}, type: "ack" as const}];
|
|
281
|
+
const {fetchImpl} = makeFetch(() => json({results}));
|
|
282
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
283
|
+
await expect(channel.sendMutationBatch?.(batch)).resolves.toEqual({results, type: "results"});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("maps 404 to unsupported", async () => {
|
|
287
|
+
const {fetchImpl} = makeFetch(() => json({}, 404));
|
|
288
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
289
|
+
await expect(channel.sendMutationBatch?.(batch)).resolves.toEqual({type: "unsupported"});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("rejects on non-JSON responses", async () => {
|
|
293
|
+
const {fetchImpl} = makeFetch(() => new Response("nope", {status: 500}));
|
|
294
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
295
|
+
await expect(channel.sendMutationBatch?.(batch)).rejects.toThrow("non-JSON");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("rejects when results is missing", async () => {
|
|
299
|
+
const {fetchImpl} = makeFetch(() => json({}));
|
|
300
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
301
|
+
await expect(channel.sendMutationBatch?.(batch)).rejects.toThrow("status 200");
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
223
305
|
it("uses global fetch when no fetchImpl is provided (network error path)", async () => {
|
|
224
306
|
const channel = createHttpChannel({
|
|
225
307
|
authProvider,
|
package/src/sync/httpChannel.ts
CHANGED
|
@@ -82,6 +82,8 @@ export interface HttpChannelConfig {
|
|
|
82
82
|
authProvider: Pick<AuthProvider, "getToken">;
|
|
83
83
|
/** Fetch implementation override for tests/SSR (defaults to global fetch). */
|
|
84
84
|
fetchImpl?: FetchLike;
|
|
85
|
+
/** Selected organization id attached as `X-Organization-Id` when present. */
|
|
86
|
+
organizationIdProvider?: () => string | undefined;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
/** Create an {@link HttpChannel} speaking @terreno/api's `/sync/*` routes. */
|
|
@@ -89,15 +91,18 @@ export const createHttpChannel = ({
|
|
|
89
91
|
baseUrl,
|
|
90
92
|
authProvider,
|
|
91
93
|
fetchImpl,
|
|
94
|
+
organizationIdProvider,
|
|
92
95
|
}: HttpChannelConfig): HttpChannel => {
|
|
93
96
|
const fetcher: FetchLike = fetchImpl ?? ((input, init) => globalThis.fetch(input, init));
|
|
94
97
|
|
|
95
98
|
const request = async (path: string, init?: RequestInit): Promise<Response> => {
|
|
96
99
|
const token = await authProvider.getToken();
|
|
100
|
+
const organizationId = organizationIdProvider?.()?.trim();
|
|
97
101
|
const headers: Record<string, string> = {
|
|
98
102
|
Accept: "application/json",
|
|
99
103
|
...(init?.body !== undefined ? {"Content-Type": "application/json"} : {}),
|
|
100
104
|
...(token ? {Authorization: `Bearer ${token}`} : {}),
|
|
105
|
+
...(organizationId ? {"X-Organization-Id": organizationId} : {}),
|
|
101
106
|
};
|
|
102
107
|
const response = await fetcher(`${baseUrl}${path}`, {...init, headers});
|
|
103
108
|
if (response.status === 401) {
|
|
@@ -167,6 +167,34 @@ describe("createReplayCoordinator", () => {
|
|
|
167
167
|
expect(entity?.data).toEqual({title: "t1"});
|
|
168
168
|
});
|
|
169
169
|
|
|
170
|
+
it("ignores an ack that resolves after its replay lifecycle is reset", async () => {
|
|
171
|
+
const harness = makeHarness();
|
|
172
|
+
enqueue(harness, {entityId: "t1", mutationId: "m1"});
|
|
173
|
+
let resolveResponse:
|
|
174
|
+
| ((result: {ack: {id: string; mutationId: string; seq: number}; type: "ack"}) => void)
|
|
175
|
+
| undefined;
|
|
176
|
+
harness.transport.respondWith(
|
|
177
|
+
(request) =>
|
|
178
|
+
new Promise((resolve) => {
|
|
179
|
+
resolveResponse = resolve;
|
|
180
|
+
expect(request.mutationId).toBe("m1");
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const replay = harness.coordinator.replay({userId: USER});
|
|
185
|
+
await Promise.resolve();
|
|
186
|
+
expect(resolveResponse).toBeDefined();
|
|
187
|
+
|
|
188
|
+
// A lifecycle boundary can reset replay state while an HTTP request is still
|
|
189
|
+
// pending; the mutation can subsequently be restored to queued for a retry.
|
|
190
|
+
harness.coordinator.reset();
|
|
191
|
+
harness.outbox.markQueued({mutationId: "m1"});
|
|
192
|
+
resolveResponse?.({ack: {id: "t1", mutationId: "m1", seq: 42}, type: "ack"});
|
|
193
|
+
|
|
194
|
+
await replay;
|
|
195
|
+
expect(harness.outbox.getMutation({mutationId: "m1"})?.status).toBe("queued");
|
|
196
|
+
});
|
|
197
|
+
|
|
170
198
|
it("does not clear a pendingMutationId owned by a newer mutation", async () => {
|
|
171
199
|
const harness = makeHarness();
|
|
172
200
|
enqueue(harness, {entityId: "t1", mutationId: "m1"});
|
|
@@ -32,15 +32,18 @@ const debugBody = (mutation: OutboxMutation): Record<string, unknown> | string =
|
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
/** Error-nack retries beyond this attempt count become terminal failures. */
|
|
35
|
+
/** @internal */
|
|
35
36
|
export const MAX_ERROR_NACK_ATTEMPTS = 5;
|
|
36
37
|
|
|
37
38
|
/** Base delay for the error-nack exponential backoff (doubles per attempt). */
|
|
39
|
+
/** @internal */
|
|
38
40
|
export const ERROR_NACK_BASE_BACKOFF_MS = 1_000;
|
|
39
41
|
|
|
40
42
|
/** Base delay for transport-failure backoff (unlimited retries, same cap/jitter shape). */
|
|
41
|
-
|
|
43
|
+
const TRANSPORT_FAILURE_BASE_BACKOFF_MS = 1_000;
|
|
42
44
|
|
|
43
45
|
/** Cap applied to every jittered backoff (error-nack and transport-failure alike). */
|
|
46
|
+
/** @internal */
|
|
44
47
|
export const MAX_BACKOFF_MS = 30_000;
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -51,7 +54,7 @@ export const MAX_BACKOFF_MS = 30_000;
|
|
|
51
54
|
* produce one stray `unsupported`) must never permanently downgrade the
|
|
52
55
|
* session to single-sends.
|
|
53
56
|
*/
|
|
54
|
-
|
|
57
|
+
const BATCH_UNSUPPORTED_LATCH_THRESHOLD = 2;
|
|
55
58
|
|
|
56
59
|
/**
|
|
57
60
|
* FIX 5: while latched `batchUnsupported`, re-probe batch support on this
|
|
@@ -59,9 +62,10 @@ export const BATCH_UNSUPPORTED_LATCH_THRESHOLD = 2;
|
|
|
59
62
|
* sends to an upgraded instance, or the original instance could itself be
|
|
60
63
|
* upgraded, without the connection ever dropping.
|
|
61
64
|
*/
|
|
65
|
+
/** @internal */
|
|
62
66
|
export const BATCH_UNSUPPORTED_REPROBE_INTERVAL_MS = 60_000;
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
interface ReplayResult {
|
|
65
69
|
/** Set when replay stopped early because the server rejected our auth. */
|
|
66
70
|
paused?: "auth";
|
|
67
71
|
}
|
|
@@ -252,6 +256,10 @@ export const createReplayCoordinator = ({
|
|
|
252
256
|
shouldDrain = () => true,
|
|
253
257
|
}: CreateReplayCoordinatorArgs): ReplayCoordinator => {
|
|
254
258
|
const inFlightReplays = new Map<string, Promise<ReplayResult>>();
|
|
259
|
+
// Every reset is a lifecycle boundary (stop, sign-out, or user switch). A
|
|
260
|
+
// send that settles after one must never apply its old outcome to the
|
|
261
|
+
// current store/outbox, even if its mutation row was restored to queued.
|
|
262
|
+
let lifecycleEpoch = 0;
|
|
255
263
|
/**
|
|
256
264
|
* Set when a `replay()` call coalesces into an already-running drain whose
|
|
257
265
|
* final pass may have already observed the queue as empty (a narrow race:
|
|
@@ -384,6 +392,9 @@ export const createReplayCoordinator = ({
|
|
|
384
392
|
request.data = {};
|
|
385
393
|
}
|
|
386
394
|
}
|
|
395
|
+
if (mutation.mutationMode !== undefined) {
|
|
396
|
+
request.mutationMode = mutation.mutationMode;
|
|
397
|
+
}
|
|
387
398
|
return request;
|
|
388
399
|
};
|
|
389
400
|
|
|
@@ -846,7 +857,11 @@ export const createReplayCoordinator = ({
|
|
|
846
857
|
* batching is marked unsupported for this connection, or a chunk's only
|
|
847
858
|
* eligible mutation count is 1).
|
|
848
859
|
*/
|
|
849
|
-
const sendSingle = async (
|
|
860
|
+
const sendSingle = async (
|
|
861
|
+
userId: string,
|
|
862
|
+
mutation: OutboxMutation,
|
|
863
|
+
replayEpoch: number
|
|
864
|
+
): Promise<OutcomeDecision> => {
|
|
850
865
|
outbox.markInFlight({mutationId: mutation.mutationId});
|
|
851
866
|
debug?.record({
|
|
852
867
|
collection: mutation.collection,
|
|
@@ -862,10 +877,16 @@ export const createReplayCoordinator = ({
|
|
|
862
877
|
try {
|
|
863
878
|
result = await sendMutation(buildRequest(mutation));
|
|
864
879
|
} catch (error) {
|
|
880
|
+
if (replayEpoch !== lifecycleEpoch) {
|
|
881
|
+
return {authPaused: false, haltDrain: true, progressed: false};
|
|
882
|
+
}
|
|
865
883
|
const decision = applyTransportError(mutation, error);
|
|
866
884
|
reportProgress(userId, 1);
|
|
867
885
|
return decision;
|
|
868
886
|
}
|
|
887
|
+
if (replayEpoch !== lifecycleEpoch) {
|
|
888
|
+
return {authPaused: false, haltDrain: true, progressed: false};
|
|
889
|
+
}
|
|
869
890
|
const decision = applyResult(mutation, result);
|
|
870
891
|
reportProgress(userId, 1);
|
|
871
892
|
return decision;
|
|
@@ -945,13 +966,16 @@ export const createReplayCoordinator = ({
|
|
|
945
966
|
/** Send one chunk as a batch; returns the per-mutation outcome decisions and any halt reached. */
|
|
946
967
|
const sendChunk = async (
|
|
947
968
|
userId: string,
|
|
948
|
-
chunk: OutboxMutation[]
|
|
969
|
+
chunk: OutboxMutation[],
|
|
970
|
+
replayEpoch: number
|
|
949
971
|
): Promise<{
|
|
950
972
|
decisions: Map<string, OutcomeDecision>;
|
|
951
973
|
unsupported: boolean;
|
|
952
974
|
haltedAt?: number;
|
|
953
975
|
/** True when the server returned fewer results than mutations sent. */
|
|
954
976
|
shortResponse: boolean;
|
|
977
|
+
/** True when a reset invalidated this request while it was awaiting a response. */
|
|
978
|
+
superseded: boolean;
|
|
955
979
|
}> => {
|
|
956
980
|
const decisions = new Map<string, OutcomeDecision>();
|
|
957
981
|
for (const mutation of chunk) {
|
|
@@ -973,6 +997,9 @@ export const createReplayCoordinator = ({
|
|
|
973
997
|
// biome-ignore lint/style/noNonNullAssertion: caller only invokes sendChunk when sendMutationBatch is defined and batching is not marked unsupported.
|
|
974
998
|
response = await sendMutationBatch!(request);
|
|
975
999
|
} catch (error) {
|
|
1000
|
+
if (replayEpoch !== lifecycleEpoch) {
|
|
1001
|
+
return {decisions, shortResponse: false, superseded: true, unsupported: false};
|
|
1002
|
+
}
|
|
976
1003
|
// Transport failure for the WHOLE chunk: every mutation in it goes back
|
|
977
1004
|
// to queued via the same transport-error handling as a single send, and
|
|
978
1005
|
// the drain halts (INV-3 — resending the whole chunk is safe).
|
|
@@ -986,7 +1013,10 @@ export const createReplayCoordinator = ({
|
|
|
986
1013
|
}
|
|
987
1014
|
}
|
|
988
1015
|
reportProgress(userId, chunk.length);
|
|
989
|
-
return {decisions, haltedAt, shortResponse: false, unsupported: false};
|
|
1016
|
+
return {decisions, haltedAt, shortResponse: false, superseded: false, unsupported: false};
|
|
1017
|
+
}
|
|
1018
|
+
if (replayEpoch !== lifecycleEpoch) {
|
|
1019
|
+
return {decisions, shortResponse: false, superseded: true, unsupported: false};
|
|
990
1020
|
}
|
|
991
1021
|
if (response.type === "unsupported") {
|
|
992
1022
|
// The chunk was marked inFlight before sending (for diagnostics/debug
|
|
@@ -996,7 +1026,7 @@ export const createReplayCoordinator = ({
|
|
|
996
1026
|
for (const mutation of chunk) {
|
|
997
1027
|
outbox.markQueued({mutationId: mutation.mutationId});
|
|
998
1028
|
}
|
|
999
|
-
return {decisions, shortResponse: false, unsupported: true};
|
|
1029
|
+
return {decisions, shortResponse: false, superseded: false, unsupported: true};
|
|
1000
1030
|
}
|
|
1001
1031
|
// Walk results in order (INV-1), applying the same per-outcome handlers as
|
|
1002
1032
|
// the single-send path, then the B4 stop-the-line policy. Per B2, the
|
|
@@ -1039,10 +1069,10 @@ export const createReplayCoordinator = ({
|
|
|
1039
1069
|
}
|
|
1040
1070
|
}
|
|
1041
1071
|
reportProgress(userId, processed);
|
|
1042
|
-
return {decisions, haltedAt, shortResponse, unsupported: false};
|
|
1072
|
+
return {decisions, haltedAt, shortResponse, superseded: false, unsupported: false};
|
|
1043
1073
|
};
|
|
1044
1074
|
|
|
1045
|
-
const drainOnce = async (userId: string): Promise<DrainPassResult> => {
|
|
1075
|
+
const drainOnce = async (userId: string, replayEpoch: number): Promise<DrainPassResult> => {
|
|
1046
1076
|
let progressed = false;
|
|
1047
1077
|
let nextWakeAt: number | undefined;
|
|
1048
1078
|
|
|
@@ -1076,7 +1106,7 @@ export const createReplayCoordinator = ({
|
|
|
1076
1106
|
if (blockedKeys.has(entityKey(mutation.collection, mutation.entityId))) {
|
|
1077
1107
|
continue;
|
|
1078
1108
|
}
|
|
1079
|
-
const decision = await sendSingle(userId, mutation);
|
|
1109
|
+
const decision = await sendSingle(userId, mutation, replayEpoch);
|
|
1080
1110
|
sentAny = true;
|
|
1081
1111
|
progressed = progressed || decision.progressed;
|
|
1082
1112
|
if (decision.authPaused) {
|
|
@@ -1135,7 +1165,7 @@ export const createReplayCoordinator = ({
|
|
|
1135
1165
|
// A lone eligible mutation: sending it as a "batch of one" adds no
|
|
1136
1166
|
// value and costs an extra round-trip on unsupported servers — reuse
|
|
1137
1167
|
// the single-send path directly (still counts as this pass's send).
|
|
1138
|
-
const decision = await sendSingle(userId, chunk[0]);
|
|
1168
|
+
const decision = await sendSingle(userId, chunk[0], replayEpoch);
|
|
1139
1169
|
progressed = progressed || decision.progressed;
|
|
1140
1170
|
if (decision.authPaused) {
|
|
1141
1171
|
return {authPaused: true, progressed};
|
|
@@ -1157,7 +1187,14 @@ export const createReplayCoordinator = ({
|
|
|
1157
1187
|
continue;
|
|
1158
1188
|
}
|
|
1159
1189
|
|
|
1160
|
-
const {decisions, unsupported, haltedAt, shortResponse} = await sendChunk(
|
|
1190
|
+
const {decisions, unsupported, haltedAt, shortResponse, superseded} = await sendChunk(
|
|
1191
|
+
userId,
|
|
1192
|
+
chunk,
|
|
1193
|
+
replayEpoch
|
|
1194
|
+
);
|
|
1195
|
+
if (superseded) {
|
|
1196
|
+
return {authPaused: false, progressed, stopReplayCall: true};
|
|
1197
|
+
}
|
|
1161
1198
|
if (unsupported) {
|
|
1162
1199
|
// FIX 5: only latch after BATCH_UNSUPPORTED_LATCH_THRESHOLD
|
|
1163
1200
|
// consecutive unsupported results — a single slow-but-supported
|
|
@@ -1229,6 +1266,7 @@ export const createReplayCoordinator = ({
|
|
|
1229
1266
|
// A fresh replay call supersedes any pending timed wake-up — it is about
|
|
1230
1267
|
// to drain right now.
|
|
1231
1268
|
clearWakeTimer(userId);
|
|
1269
|
+
const replayEpoch = lifecycleEpoch;
|
|
1232
1270
|
// B5: (re)start this call's progress counters. `total` is a lower bound —
|
|
1233
1271
|
// snapshotted once at entry — since more may enqueue mid-drain; onProgress
|
|
1234
1272
|
// still reports every attempt made.
|
|
@@ -1243,10 +1281,10 @@ export const createReplayCoordinator = ({
|
|
|
1243
1281
|
// the host may have gone offline or paused — an internally-scheduled
|
|
1244
1282
|
// continuation must never race past that.
|
|
1245
1283
|
for (;;) {
|
|
1246
|
-
if (!shouldDrain()) {
|
|
1284
|
+
if (replayEpoch !== lifecycleEpoch || !shouldDrain()) {
|
|
1247
1285
|
break;
|
|
1248
1286
|
}
|
|
1249
|
-
const pass = await drainOnce(userId);
|
|
1287
|
+
const pass = await drainOnce(userId, replayEpoch);
|
|
1250
1288
|
if (pass.progressed) {
|
|
1251
1289
|
onDrainPass?.({userId});
|
|
1252
1290
|
}
|
|
@@ -1299,6 +1337,7 @@ export const createReplayCoordinator = ({
|
|
|
1299
1337
|
|
|
1300
1338
|
/** FIX 3: full lifecycle reset — see the `ReplayCoordinator.reset` doc comment. */
|
|
1301
1339
|
const reset = (): void => {
|
|
1340
|
+
lifecycleEpoch += 1;
|
|
1302
1341
|
for (const userId of [...wakeTimers.keys()]) {
|
|
1303
1342
|
clearWakeTimer(userId);
|
|
1304
1343
|
}
|