@terreno/syncdb 56.0.0-beta.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 +419 -0
- package/dist/auth/betterAuthAdapter.d.ts +22 -0
- package/dist/auth/betterAuthAdapter.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.js +181 -0
- package/dist/auth/betterAuthAdapter.js.map +1 -0
- package/dist/auth/betterAuthAdapter.test.d.ts +2 -0
- package/dist/auth/betterAuthAdapter.test.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.test.js +350 -0
- package/dist/auth/betterAuthAdapter.test.js.map +1 -0
- package/dist/auth/types.d.ts +42 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +9 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/client.d.ts +246 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1415 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +2310 -0
- package/dist/client.test.js.map +1 -0
- package/dist/crypto/aesGcmCodec.d.ts +33 -0
- package/dist/crypto/aesGcmCodec.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.js +80 -0
- package/dist/crypto/aesGcmCodec.js.map +1 -0
- package/dist/crypto/aesGcmCodec.test.d.ts +2 -0
- package/dist/crypto/aesGcmCodec.test.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.test.js +83 -0
- package/dist/crypto/aesGcmCodec.test.js.map +1 -0
- package/dist/crypto/identityCodec.d.ts +7 -0
- package/dist/crypto/identityCodec.d.ts.map +1 -0
- package/dist/crypto/identityCodec.js +9 -0
- package/dist/crypto/identityCodec.js.map +1 -0
- package/dist/crypto/keyProviders.d.ts +34 -0
- package/dist/crypto/keyProviders.d.ts.map +1 -0
- package/dist/crypto/keyProviders.js +102 -0
- package/dist/crypto/keyProviders.js.map +1 -0
- package/dist/crypto/keyProviders.test.d.ts +2 -0
- package/dist/crypto/keyProviders.test.d.ts.map +1 -0
- package/dist/crypto/keyProviders.test.js +189 -0
- package/dist/crypto/keyProviders.test.js.map +1 -0
- package/dist/crypto/types.d.ts +24 -0
- package/dist/crypto/types.d.ts.map +1 -0
- package/dist/crypto/types.js +8 -0
- package/dist/crypto/types.js.map +1 -0
- package/dist/debug/debugChannel.d.ts +52 -0
- package/dist/debug/debugChannel.d.ts.map +1 -0
- package/dist/debug/debugChannel.js +73 -0
- package/dist/debug/debugChannel.js.map +1 -0
- package/dist/debug/debugChannel.test.d.ts +2 -0
- package/dist/debug/debugChannel.test.d.ts.map +1 -0
- package/dist/debug/debugChannel.test.js +105 -0
- package/dist/debug/debugChannel.test.js.map +1 -0
- package/dist/debug/debugLog.d.ts +111 -0
- package/dist/debug/debugLog.d.ts.map +1 -0
- package/dist/debug/debugLog.js +150 -0
- package/dist/debug/debugLog.js.map +1 -0
- package/dist/debug/debugLog.test.d.ts +2 -0
- package/dist/debug/debugLog.test.d.ts.map +1 -0
- package/dist/debug/debugLog.test.js +121 -0
- package/dist/debug/debugLog.test.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js +38 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js.map +1 -0
- package/dist/mutations/conflicts.d.ts +31 -0
- package/dist/mutations/conflicts.d.ts.map +1 -0
- package/dist/mutations/conflicts.js +73 -0
- package/dist/mutations/conflicts.js.map +1 -0
- package/dist/mutations/outbox.d.ts +174 -0
- package/dist/mutations/outbox.d.ts.map +1 -0
- package/dist/mutations/outbox.js +385 -0
- package/dist/mutations/outbox.js.map +1 -0
- package/dist/mutations/outbox.test.d.ts +2 -0
- package/dist/mutations/outbox.test.d.ts.map +1 -0
- package/dist/mutations/outbox.test.js +643 -0
- package/dist/mutations/outbox.test.js.map +1 -0
- package/dist/mutations/resolveConflict.d.ts +32 -0
- package/dist/mutations/resolveConflict.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.js +101 -0
- package/dist/mutations/resolveConflict.js.map +1 -0
- package/dist/mutations/resolveConflict.test.d.ts +2 -0
- package/dist/mutations/resolveConflict.test.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.test.js +282 -0
- package/dist/mutations/resolveConflict.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.d.ts +9 -0
- package/dist/persisters/defaultPersisterFactory.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.js +9 -0
- package/dist/persisters/defaultPersisterFactory.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts +10 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.js +44 -0
- package/dist/persisters/defaultPersisterFactory.native.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts +2 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.js +177 -0
- package/dist/persisters/defaultPersisterFactory.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts +17 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.js +65 -0
- package/dist/persisters/defaultPersisterFactory.web.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts +46 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.js +171 -0
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts +2 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js +352 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -0
- package/dist/persisters/memoryPersister.d.ts +19 -0
- package/dist/persisters/memoryPersister.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.js +31 -0
- package/dist/persisters/memoryPersister.js.map +1 -0
- package/dist/persisters/memoryPersister.test.d.ts +2 -0
- package/dist/persisters/memoryPersister.test.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.test.js +113 -0
- package/dist/persisters/memoryPersister.test.js.map +1 -0
- package/dist/persisters/types.d.ts +65 -0
- package/dist/persisters/types.d.ts.map +1 -0
- package/dist/persisters/types.js +2 -0
- package/dist/persisters/types.js.map +1 -0
- package/dist/react/hooks.d.ts +132 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +269 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/hooks.test.d.ts +2 -0
- package/dist/react/hooks.test.d.ts.map +1 -0
- package/dist/react/hooks.test.js +645 -0
- package/dist/react/hooks.test.js.map +1 -0
- package/dist/react/index.d.ts +11 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +11 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +11 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +17 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/storage/idb.d.ts +27 -0
- package/dist/storage/idb.d.ts.map +1 -0
- package/dist/storage/idb.js +72 -0
- package/dist/storage/idb.js.map +1 -0
- package/dist/storage/idb.test.d.ts +2 -0
- package/dist/storage/idb.test.d.ts.map +1 -0
- package/dist/storage/idb.test.js +100 -0
- package/dist/storage/idb.test.js.map +1 -0
- package/dist/storage/purgeUnknownStream.test.d.ts +2 -0
- package/dist/storage/purgeUnknownStream.test.d.ts.map +1 -0
- package/dist/storage/purgeUnknownStream.test.js +64 -0
- package/dist/storage/purgeUnknownStream.test.js.map +1 -0
- package/dist/storage/schema.d.ts +33 -0
- package/dist/storage/schema.d.ts.map +1 -0
- package/dist/storage/schema.js +92 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/store.d.ts +136 -0
- package/dist/storage/store.d.ts.map +1 -0
- package/dist/storage/store.js +275 -0
- package/dist/storage/store.js.map +1 -0
- package/dist/storage/store.test.d.ts +2 -0
- package/dist/storage/store.test.d.ts.map +1 -0
- package/dist/storage/store.test.js +357 -0
- package/dist/storage/store.test.js.map +1 -0
- package/dist/storage/types.d.ts +146 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +32 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/storage/wipe.d.ts +22 -0
- package/dist/storage/wipe.d.ts.map +1 -0
- package/dist/storage/wipe.js +43 -0
- package/dist/storage/wipe.js.map +1 -0
- package/dist/storage/wipe.test.d.ts +2 -0
- package/dist/storage/wipe.test.d.ts.map +1 -0
- package/dist/storage/wipe.test.js +95 -0
- package/dist/storage/wipe.test.js.map +1 -0
- package/dist/sync/bootstrap.d.ts +64 -0
- package/dist/sync/bootstrap.d.ts.map +1 -0
- package/dist/sync/bootstrap.js +160 -0
- package/dist/sync/bootstrap.js.map +1 -0
- package/dist/sync/bootstrap.test.d.ts +2 -0
- package/dist/sync/bootstrap.test.d.ts.map +1 -0
- package/dist/sync/bootstrap.test.js +447 -0
- package/dist/sync/bootstrap.test.js.map +1 -0
- package/dist/sync/cursor.d.ts +67 -0
- package/dist/sync/cursor.d.ts.map +1 -0
- package/dist/sync/cursor.js +74 -0
- package/dist/sync/cursor.js.map +1 -0
- package/dist/sync/cursor.test.d.ts +2 -0
- package/dist/sync/cursor.test.d.ts.map +1 -0
- package/dist/sync/cursor.test.js +73 -0
- package/dist/sync/cursor.test.js.map +1 -0
- package/dist/sync/deltaApplier.d.ts +30 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -0
- package/dist/sync/deltaApplier.js +61 -0
- package/dist/sync/deltaApplier.js.map +1 -0
- package/dist/sync/deltaApplier.test.d.ts +2 -0
- package/dist/sync/deltaApplier.test.d.ts.map +1 -0
- package/dist/sync/deltaApplier.test.js +212 -0
- package/dist/sync/deltaApplier.test.js.map +1 -0
- package/dist/sync/entityRepair.d.ts +45 -0
- package/dist/sync/entityRepair.d.ts.map +1 -0
- package/dist/sync/entityRepair.js +91 -0
- package/dist/sync/entityRepair.js.map +1 -0
- package/dist/sync/entityRepair.test.d.ts +2 -0
- package/dist/sync/entityRepair.test.d.ts.map +1 -0
- package/dist/sync/entityRepair.test.js +146 -0
- package/dist/sync/entityRepair.test.js.map +1 -0
- package/dist/sync/httpChannel.d.ts +74 -0
- package/dist/sync/httpChannel.d.ts.map +1 -0
- package/dist/sync/httpChannel.js +123 -0
- package/dist/sync/httpChannel.js.map +1 -0
- package/dist/sync/httpChannel.test.d.ts +2 -0
- package/dist/sync/httpChannel.test.d.ts.map +1 -0
- package/dist/sync/httpChannel.test.js +215 -0
- package/dist/sync/httpChannel.test.js.map +1 -0
- package/dist/sync/replayCoordinator.d.ts +212 -0
- package/dist/sync/replayCoordinator.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.js +1110 -0
- package/dist/sync/replayCoordinator.js.map +1 -0
- package/dist/sync/replayCoordinator.test.d.ts +2 -0
- package/dist/sync/replayCoordinator.test.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.test.js +1496 -0
- package/dist/sync/replayCoordinator.test.js.map +1 -0
- package/dist/sync/socketTransport.d.ts +56 -0
- package/dist/sync/socketTransport.d.ts.map +1 -0
- package/dist/sync/socketTransport.js +292 -0
- package/dist/sync/socketTransport.js.map +1 -0
- package/dist/sync/socketTransport.test.d.ts +2 -0
- package/dist/sync/socketTransport.test.d.ts.map +1 -0
- package/dist/sync/socketTransport.test.js +346 -0
- package/dist/sync/socketTransport.test.js.map +1 -0
- package/dist/sync/transport.d.ts +80 -0
- package/dist/sync/transport.d.ts.map +1 -0
- package/dist/sync/transport.js +5 -0
- package/dist/sync/transport.js.map +1 -0
- package/dist/testing/fakeTransport.d.ts +55 -0
- package/dist/testing/fakeTransport.d.ts.map +1 -0
- package/dist/testing/fakeTransport.js +139 -0
- package/dist/testing/fakeTransport.js.map +1 -0
- package/dist/testing/fakeTransport.test.d.ts +2 -0
- package/dist/testing/fakeTransport.test.d.ts.map +1 -0
- package/dist/testing/fakeTransport.test.js +112 -0
- package/dist/testing/fakeTransport.test.js.map +1 -0
- package/dist/testing/index.d.ts +13 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +13 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types.d.ts +271 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +71 -0
- package/src/auth/betterAuthAdapter.test.ts +455 -0
- package/src/auth/betterAuthAdapter.ts +214 -0
- package/src/auth/types.ts +49 -0
- package/src/client.test.ts +2670 -0
- package/src/client.ts +1748 -0
- package/src/crypto/aesGcmCodec.test.ts +103 -0
- package/src/crypto/aesGcmCodec.ts +98 -0
- package/src/crypto/identityCodec.ts +10 -0
- package/src/crypto/keyProviders.test.ts +228 -0
- package/src/crypto/keyProviders.ts +153 -0
- package/src/crypto/types.ts +23 -0
- package/src/debug/debugChannel.test.ts +121 -0
- package/src/debug/debugChannel.ts +112 -0
- package/src/debug/debugLog.test.ts +139 -0
- package/src/debug/debugLog.ts +272 -0
- package/src/index.ts +157 -0
- package/src/isolated/defaultPersisterFactoryNative.isolated.ts +45 -0
- package/src/mutations/conflicts.ts +108 -0
- package/src/mutations/outbox.test.ts +754 -0
- package/src/mutations/outbox.ts +585 -0
- package/src/mutations/resolveConflict.test.ts +338 -0
- package/src/mutations/resolveConflict.ts +115 -0
- package/src/persisters/defaultPersisterFactory.native.ts +53 -0
- package/src/persisters/defaultPersisterFactory.test.ts +207 -0
- package/src/persisters/defaultPersisterFactory.ts +12 -0
- package/src/persisters/defaultPersisterFactory.web.ts +78 -0
- package/src/persisters/encryptedIndexedDbPersister.test.ts +381 -0
- package/src/persisters/encryptedIndexedDbPersister.ts +218 -0
- package/src/persisters/memoryPersister.test.ts +136 -0
- package/src/persisters/memoryPersister.ts +52 -0
- package/src/persisters/types.ts +71 -0
- package/src/react/hooks.test.tsx +761 -0
- package/src/react/hooks.ts +409 -0
- package/src/react/index.ts +11 -0
- package/src/react/provider.tsx +27 -0
- package/src/storage/idb.test.ts +116 -0
- package/src/storage/idb.ts +112 -0
- package/src/storage/purgeUnknownStream.test.ts +74 -0
- package/src/storage/schema.ts +103 -0
- package/src/storage/store.test.ts +424 -0
- package/src/storage/store.ts +461 -0
- package/src/storage/types.ts +159 -0
- package/src/storage/wipe.test.ts +111 -0
- package/src/storage/wipe.ts +58 -0
- package/src/sync/bootstrap.test.ts +486 -0
- package/src/sync/bootstrap.ts +217 -0
- package/src/sync/cursor.test.ts +84 -0
- package/src/sync/cursor.ts +113 -0
- package/src/sync/deltaApplier.test.ts +237 -0
- package/src/sync/deltaApplier.ts +84 -0
- package/src/sync/entityRepair.test.ts +161 -0
- package/src/sync/entityRepair.ts +124 -0
- package/src/sync/httpChannel.test.ts +265 -0
- package/src/sync/httpChannel.ts +220 -0
- package/src/sync/replayCoordinator.test.ts +1709 -0
- package/src/sync/replayCoordinator.ts +1370 -0
- package/src/sync/socketTransport.test.ts +413 -0
- package/src/sync/socketTransport.ts +379 -0
- package/src/sync/transport.ts +76 -0
- package/src/testing/fakeTransport.test.ts +125 -0
- package/src/testing/fakeTransport.ts +206 -0
- package/src/testing/index.ts +18 -0
- package/src/types.ts +284 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createSyncStore, type SyncStore} from "../storage/store";
|
|
4
|
+
import {
|
|
5
|
+
deleteConflict,
|
|
6
|
+
getConflict,
|
|
7
|
+
listConflicts,
|
|
8
|
+
pruneGhostConflicts,
|
|
9
|
+
writeConflict,
|
|
10
|
+
} from "./conflicts";
|
|
11
|
+
import {createOutbox, type Outbox} from "./outbox";
|
|
12
|
+
import {resolveConflict} from "./resolveConflict";
|
|
13
|
+
|
|
14
|
+
const USER = "user-1";
|
|
15
|
+
|
|
16
|
+
const makeHarness = (): {store: SyncStore; outbox: Outbox} => {
|
|
17
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
18
|
+
const outbox = createOutbox({store});
|
|
19
|
+
return {outbox, store};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Seed a conflicted mutation + optimistic entity + conflict row. */
|
|
23
|
+
const seedConflict = ({store, outbox}: {store: SyncStore; outbox: Outbox}): void => {
|
|
24
|
+
store.upsertEntity({
|
|
25
|
+
collection: "todos",
|
|
26
|
+
data: {title: "local"},
|
|
27
|
+
id: "t1",
|
|
28
|
+
pendingMutationId: "m1",
|
|
29
|
+
seq: 2,
|
|
30
|
+
});
|
|
31
|
+
outbox.enqueue({
|
|
32
|
+
args: {title: "local"},
|
|
33
|
+
baseVersion: 2,
|
|
34
|
+
collection: "todos",
|
|
35
|
+
entityId: "t1",
|
|
36
|
+
mutationId: "m1",
|
|
37
|
+
operation: "update",
|
|
38
|
+
userId: USER,
|
|
39
|
+
});
|
|
40
|
+
outbox.markInFlight({mutationId: "m1"});
|
|
41
|
+
outbox.markConflicted({mutationId: "m1"});
|
|
42
|
+
writeConflict({
|
|
43
|
+
conflict: {
|
|
44
|
+
collection: "todos",
|
|
45
|
+
dismissed: false,
|
|
46
|
+
entityId: "t1",
|
|
47
|
+
localData: JSON.stringify({title: "local"}),
|
|
48
|
+
mutationId: "m1",
|
|
49
|
+
serverData: JSON.stringify({title: "server"}),
|
|
50
|
+
serverSeq: 9,
|
|
51
|
+
},
|
|
52
|
+
store,
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
describe("resolveConflict", () => {
|
|
57
|
+
it("useServer applies the server data/seq, clears pending, and deletes the conflict", () => {
|
|
58
|
+
const harness = makeHarness();
|
|
59
|
+
seedConflict(harness);
|
|
60
|
+
|
|
61
|
+
resolveConflict({
|
|
62
|
+
mutationId: "m1",
|
|
63
|
+
outbox: harness.outbox,
|
|
64
|
+
store: harness.store,
|
|
65
|
+
strategy: "useServer",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
69
|
+
expect(entity?.data).toEqual({title: "server"});
|
|
70
|
+
expect(entity?.seq).toBe(9);
|
|
71
|
+
expect(entity?.pendingMutationId).toBeUndefined();
|
|
72
|
+
expect(getConflict({mutationId: "m1", store: harness.store})).toBeUndefined();
|
|
73
|
+
// The spent conflicted outbox row is discarded so startup recovery cannot
|
|
74
|
+
// resurrect a phantom conflict from it.
|
|
75
|
+
expect(harness.outbox.getMutation({mutationId: "m1"})).toBeUndefined();
|
|
76
|
+
expect(harness.outbox.listQueued({userId: USER})).toHaveLength(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("useServer tolerates corrupt serverData by applying null", () => {
|
|
80
|
+
const harness = makeHarness();
|
|
81
|
+
seedConflict(harness);
|
|
82
|
+
harness.store.raw.setCell("_conflicts", "m1", "serverData", "{corrupt");
|
|
83
|
+
|
|
84
|
+
resolveConflict({
|
|
85
|
+
mutationId: "m1",
|
|
86
|
+
outbox: harness.outbox,
|
|
87
|
+
store: harness.store,
|
|
88
|
+
strategy: "useServer",
|
|
89
|
+
});
|
|
90
|
+
expect(harness.store.getEntity({collection: "todos", id: "t1"})?.data).toBeNull();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("keepMine requeues under a fresh mutationId with baseVersion = serverSeq and keeps the local entity", () => {
|
|
94
|
+
const harness = makeHarness();
|
|
95
|
+
seedConflict(harness);
|
|
96
|
+
|
|
97
|
+
resolveConflict({
|
|
98
|
+
mutationId: "m1",
|
|
99
|
+
outbox: harness.outbox,
|
|
100
|
+
store: harness.store,
|
|
101
|
+
strategy: "keepMine",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// The retry carries a fresh mutationId: the original id is burned on the server's
|
|
105
|
+
// idempotency ledger (it would replay the recorded conflict nack forever).
|
|
106
|
+
expect(harness.outbox.getMutation({mutationId: "m1"})).toBeUndefined();
|
|
107
|
+
const queued = harness.outbox.listQueued({userId: USER});
|
|
108
|
+
expect(queued).toHaveLength(1);
|
|
109
|
+
const retry = queued[0];
|
|
110
|
+
expect(retry.mutationId).not.toBe("m1");
|
|
111
|
+
expect(retry.status).toBe("queued");
|
|
112
|
+
expect(retry.baseVersion).toBe(9);
|
|
113
|
+
expect(getConflict({mutationId: "m1", store: harness.store})).toBeUndefined();
|
|
114
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
115
|
+
expect(entity?.data).toEqual({title: "local"});
|
|
116
|
+
// The optimistic guard is re-pointed at the retry so its ack can release it.
|
|
117
|
+
expect(entity?.pendingMutationId).toBe(retry.mutationId);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("keepMine repairs a corrupt queued+conflict row instead of throwing queued→queued", () => {
|
|
121
|
+
const harness = makeHarness();
|
|
122
|
+
seedConflict(harness);
|
|
123
|
+
// Simulate the historical bug: markQueued was incorrectly legal from
|
|
124
|
+
// conflicted, leaving a `_conflicts` row pointing at a queued mutation.
|
|
125
|
+
harness.store.raw.setCell("_outbox", "m1", "status", "queued");
|
|
126
|
+
expect(harness.outbox.getMutation({mutationId: "m1"})?.status).toBe("queued");
|
|
127
|
+
|
|
128
|
+
resolveConflict({
|
|
129
|
+
mutationId: "m1",
|
|
130
|
+
outbox: harness.outbox,
|
|
131
|
+
store: harness.store,
|
|
132
|
+
strategy: "keepMine",
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(harness.outbox.getMutation({mutationId: "m1"})).toBeUndefined();
|
|
136
|
+
const queued = harness.outbox.listQueued({userId: USER});
|
|
137
|
+
expect(queued).toHaveLength(1);
|
|
138
|
+
expect(queued[0].mutationId).not.toBe("m1");
|
|
139
|
+
expect(queued[0].baseVersion).toBe(9);
|
|
140
|
+
expect(getConflict({mutationId: "m1", store: harness.store})).toBeUndefined();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Task 9.12: a conflict whose server state is "no document at a real seq"
|
|
144
|
+
// (deleted, hard-deleted, or moved out of the client's scope) must resolve to
|
|
145
|
+
// a TOMBSTONE. Upserting null data over a live row instead leaves a ghost that
|
|
146
|
+
// lists, renders, and syncs forever as an empty entity.
|
|
147
|
+
describe("server-deleted conflicts (9.12)", () => {
|
|
148
|
+
const seedServerDeleted = ({
|
|
149
|
+
store,
|
|
150
|
+
outbox,
|
|
151
|
+
serverSeq,
|
|
152
|
+
serverDeleted,
|
|
153
|
+
}: {
|
|
154
|
+
store: SyncStore;
|
|
155
|
+
outbox: Outbox;
|
|
156
|
+
serverSeq: number;
|
|
157
|
+
serverDeleted?: boolean;
|
|
158
|
+
}): void => {
|
|
159
|
+
seedConflict({outbox, store});
|
|
160
|
+
writeConflict({
|
|
161
|
+
conflict: {
|
|
162
|
+
collection: "todos",
|
|
163
|
+
dismissed: false,
|
|
164
|
+
entityId: "t1",
|
|
165
|
+
localData: JSON.stringify({title: "local"}),
|
|
166
|
+
mutationId: "m1",
|
|
167
|
+
serverData: JSON.stringify(null),
|
|
168
|
+
serverSeq,
|
|
169
|
+
...(serverDeleted === undefined ? {} : {serverDeleted}),
|
|
170
|
+
},
|
|
171
|
+
store,
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
it("useServer upserts a tombstone when the server has no document at a real seq", () => {
|
|
176
|
+
const harness = makeHarness();
|
|
177
|
+
seedServerDeleted({...harness, serverSeq: 9});
|
|
178
|
+
|
|
179
|
+
resolveConflict({
|
|
180
|
+
mutationId: "m1",
|
|
181
|
+
outbox: harness.outbox,
|
|
182
|
+
store: harness.store,
|
|
183
|
+
strategy: "useServer",
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
187
|
+
expect(entity?.deleted).toBe(true);
|
|
188
|
+
expect(entity?.seq).toBe(9);
|
|
189
|
+
expect(entity?.pendingMutationId).toBeUndefined();
|
|
190
|
+
// A tombstone is excluded from live reads — no ghost row in the UI.
|
|
191
|
+
expect(harness.store.listEntities({collection: "todos"})).toHaveLength(0);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("useServer honors an explicit serverDeleted flag even at seq 0", () => {
|
|
195
|
+
const harness = makeHarness();
|
|
196
|
+
seedServerDeleted({...harness, serverDeleted: true, serverSeq: 0});
|
|
197
|
+
|
|
198
|
+
resolveConflict({
|
|
199
|
+
mutationId: "m1",
|
|
200
|
+
outbox: harness.outbox,
|
|
201
|
+
store: harness.store,
|
|
202
|
+
strategy: "useServer",
|
|
203
|
+
});
|
|
204
|
+
expect(harness.store.getEntity({collection: "todos", id: "t1"})?.deleted).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// seq 0 with no flag is the startup-recovery husk shape (recoverStartupState
|
|
208
|
+
// writes serverData null / serverSeq 0 for a conflicted row it never saw a
|
|
209
|
+
// nack for). That is "server state unknown", NOT "server deleted" — deleting
|
|
210
|
+
// the user's local data on it would be silent data loss.
|
|
211
|
+
it("does not tombstone when serverData is null at seq 0 (server state unknown)", () => {
|
|
212
|
+
const harness = makeHarness();
|
|
213
|
+
seedServerDeleted({...harness, serverSeq: 0});
|
|
214
|
+
|
|
215
|
+
resolveConflict({
|
|
216
|
+
mutationId: "m1",
|
|
217
|
+
outbox: harness.outbox,
|
|
218
|
+
store: harness.store,
|
|
219
|
+
strategy: "useServer",
|
|
220
|
+
});
|
|
221
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
222
|
+
expect(entity?.deleted).toBe(false);
|
|
223
|
+
expect(entity?.data).toBeNull();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// The mirror image: a local DELETE that conflicts with a server edit. The
|
|
227
|
+
// local row is already a tombstone, so accepting the server side has to
|
|
228
|
+
// resurrect it — otherwise the live server document stays invisible locally
|
|
229
|
+
// (and keeps arriving as a delta the tombstone hides) until a re-bootstrap.
|
|
230
|
+
it("useServer resurrects a local tombstone when the server still has the document", () => {
|
|
231
|
+
const harness = makeHarness();
|
|
232
|
+
seedConflict(harness);
|
|
233
|
+
harness.store.softDeleteEntity({collection: "todos", id: "t1"});
|
|
234
|
+
expect(harness.store.getEntity({collection: "todos", id: "t1"})?.deleted).toBe(true);
|
|
235
|
+
|
|
236
|
+
resolveConflict({
|
|
237
|
+
mutationId: "m1",
|
|
238
|
+
outbox: harness.outbox,
|
|
239
|
+
store: harness.store,
|
|
240
|
+
strategy: "useServer",
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
244
|
+
expect(entity?.deleted).toBe(false);
|
|
245
|
+
expect(entity?.data).toEqual({title: "server"});
|
|
246
|
+
expect(harness.store.listEntities({collection: "todos"})).toHaveLength(1);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("keepMine still retries against the server seq for a server-deleted conflict", () => {
|
|
250
|
+
const harness = makeHarness();
|
|
251
|
+
seedServerDeleted({...harness, serverSeq: 9});
|
|
252
|
+
|
|
253
|
+
resolveConflict({
|
|
254
|
+
mutationId: "m1",
|
|
255
|
+
outbox: harness.outbox,
|
|
256
|
+
store: harness.store,
|
|
257
|
+
strategy: "keepMine",
|
|
258
|
+
});
|
|
259
|
+
const entity = harness.store.getEntity({collection: "todos", id: "t1"});
|
|
260
|
+
expect(entity?.deleted).toBe(false);
|
|
261
|
+
expect(entity?.data).toEqual({title: "local"});
|
|
262
|
+
expect(harness.outbox.listQueued({userId: USER})[0]?.baseVersion).toBe(9);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("throws for an unknown conflict", () => {
|
|
267
|
+
const harness = makeHarness();
|
|
268
|
+
expect(() =>
|
|
269
|
+
resolveConflict({
|
|
270
|
+
mutationId: "missing",
|
|
271
|
+
outbox: harness.outbox,
|
|
272
|
+
store: harness.store,
|
|
273
|
+
strategy: "useServer",
|
|
274
|
+
})
|
|
275
|
+
).toThrow("Conflict not found: missing");
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("conflict row helpers", () => {
|
|
280
|
+
it("write/get/delete round-trips a conflict row", () => {
|
|
281
|
+
const harness = makeHarness();
|
|
282
|
+
seedConflict(harness);
|
|
283
|
+
expect(getConflict({mutationId: "m1", store: harness.store})?.entityId).toBe("t1");
|
|
284
|
+
deleteConflict({mutationId: "m1", store: harness.store});
|
|
285
|
+
expect(getConflict({mutationId: "m1", store: harness.store})).toBeUndefined();
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("listConflicts excludes dismissed rows unless requested", () => {
|
|
289
|
+
const harness = makeHarness();
|
|
290
|
+
seedConflict(harness);
|
|
291
|
+
harness.store.raw.setCell("_conflicts", "m1", "dismissed", true);
|
|
292
|
+
expect(listConflicts({store: harness.store})).toHaveLength(0);
|
|
293
|
+
expect(listConflicts({includeDismissed: true, store: harness.store})).toHaveLength(1);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Husk rows are what a pre-fix build persisted when TinyBase re-materialized a
|
|
299
|
+
* deleted `_conflicts` row from its schema defaults: no `collection`, no
|
|
300
|
+
* `entityId`, nothing to resolve. They must never reach the UI, and resolving
|
|
301
|
+
* one must not reach `upsertEntity` (which threw `Unknown collection ""`).
|
|
302
|
+
*/
|
|
303
|
+
describe("husk conflict rows", () => {
|
|
304
|
+
const seedHusk = (store: SyncStore): void => {
|
|
305
|
+
store.raw.setRow("_conflicts", "husk", {dismissed: false, serverSeq: 0});
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
it("are hidden from getConflict and listConflicts", () => {
|
|
309
|
+
const harness = makeHarness();
|
|
310
|
+
seedConflict(harness);
|
|
311
|
+
seedHusk(harness.store);
|
|
312
|
+
expect(getConflict({mutationId: "husk", store: harness.store})).toBeUndefined();
|
|
313
|
+
expect(
|
|
314
|
+
listConflicts({includeDismissed: true, store: harness.store}).map((c) => c.mutationId)
|
|
315
|
+
).toEqual(["m1"]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("resolving one reports a missing conflict instead of writing an empty collection", () => {
|
|
319
|
+
const harness = makeHarness();
|
|
320
|
+
seedHusk(harness.store);
|
|
321
|
+
expect(() =>
|
|
322
|
+
resolveConflict({
|
|
323
|
+
mutationId: "husk",
|
|
324
|
+
outbox: harness.outbox,
|
|
325
|
+
store: harness.store,
|
|
326
|
+
strategy: "useServer",
|
|
327
|
+
})
|
|
328
|
+
).toThrow(/Conflict not found/);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("pruneGhostConflicts removes them and keeps real conflicts", () => {
|
|
332
|
+
const harness = makeHarness();
|
|
333
|
+
seedConflict(harness);
|
|
334
|
+
seedHusk(harness.store);
|
|
335
|
+
expect(pruneGhostConflicts({store: harness.store})).toEqual(["husk"]);
|
|
336
|
+
expect(harness.store.raw.getRowIds("_conflicts")).toEqual(["m1"]);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type {SyncStore} from "../storage/store";
|
|
2
|
+
import type {ConflictResolutionStrategy} from "../types";
|
|
3
|
+
import {deleteConflict, getConflict} from "./conflicts";
|
|
4
|
+
import type {Outbox} from "./outbox";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Apply the user's resolution choice to a recorded conflict:
|
|
8
|
+
*
|
|
9
|
+
* - `"useServer"`: overwrite the local entity with the canonical server data
|
|
10
|
+
* and seq, clear its `pendingMutationId`, delete the conflict row, and
|
|
11
|
+
* discard the spent conflicted outbox row (so startup recovery cannot
|
|
12
|
+
* resurrect a phantom conflict). The local row's deleted flag follows the
|
|
13
|
+
* server side: a tombstone when the server deleted the document (rather than a
|
|
14
|
+
* null-data ghost row), and a resurrection when the server still has it and
|
|
15
|
+
* the local row was a tombstone (Task 9.12).
|
|
16
|
+
* - `"keepMine"`: requeue the conflicted mutation under a FRESH mutationId
|
|
17
|
+
* (the original id is burned on the server's idempotency ledger, which would
|
|
18
|
+
* replay the recorded conflict nack forever) with `baseVersion` set to the
|
|
19
|
+
* server seq recorded on the conflict so the retry passes the LWW check, and
|
|
20
|
+
* delete the conflict row. The entity keeps its optimistic local data and
|
|
21
|
+
* its `pendingMutationId` is re-pointed at the retry so the retry's ack can
|
|
22
|
+
* release it.
|
|
23
|
+
*
|
|
24
|
+
* If a prior bug left the outbox row as `queued`/`inFlight` while the
|
|
25
|
+
* `_conflicts` row still existed, keepMine repairs that first so resolve does
|
|
26
|
+
* not throw `Illegal outbox transition "queued" → "queued"`.
|
|
27
|
+
*/
|
|
28
|
+
export const resolveConflict = ({
|
|
29
|
+
store,
|
|
30
|
+
outbox,
|
|
31
|
+
mutationId,
|
|
32
|
+
strategy,
|
|
33
|
+
}: {
|
|
34
|
+
store: SyncStore;
|
|
35
|
+
outbox: Outbox;
|
|
36
|
+
mutationId: string;
|
|
37
|
+
strategy: ConflictResolutionStrategy;
|
|
38
|
+
}): void => {
|
|
39
|
+
const conflict = getConflict({mutationId, store});
|
|
40
|
+
if (!conflict) {
|
|
41
|
+
throw new Error(`Conflict not found: ${mutationId}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (strategy === "useServer") {
|
|
45
|
+
let serverData: unknown = null;
|
|
46
|
+
try {
|
|
47
|
+
serverData = JSON.parse(conflict.serverData);
|
|
48
|
+
} catch {
|
|
49
|
+
serverData = null;
|
|
50
|
+
}
|
|
51
|
+
// Task 9.12: the server side of the conflict can be a tombstone (deleted,
|
|
52
|
+
// hard-deleted, or moved out of this client's scope). Writing its null data
|
|
53
|
+
// over a live row would leave a ghost entity that lists and renders forever
|
|
54
|
+
// — the resolution is a tombstone. `serverDeleted` is authoritative when the
|
|
55
|
+
// server sends it; otherwise null data AT A REAL SEQ means the same thing.
|
|
56
|
+
// Null data at seq 0 does NOT: that is the startup-recovery shape for a
|
|
57
|
+
// conflict whose nack was never seen, i.e. server state unknown, and
|
|
58
|
+
// deleting local data on it would be silent data loss.
|
|
59
|
+
const serverDeleted =
|
|
60
|
+
conflict.serverDeleted === true || (serverData === null && conflict.serverSeq > 0);
|
|
61
|
+
// The mirror image matters too: when the server DOES have the document, a
|
|
62
|
+
// local tombstone (a delete that lost the conflict) must be resurrected, or
|
|
63
|
+
// the live server row stays invisible locally. `undefined` leaves the local
|
|
64
|
+
// flag alone, which is the right answer only for the seq-0 "unknown" shape.
|
|
65
|
+
let deleted: boolean | undefined;
|
|
66
|
+
if (serverDeleted) {
|
|
67
|
+
deleted = true;
|
|
68
|
+
} else if (serverData !== null) {
|
|
69
|
+
deleted = false;
|
|
70
|
+
}
|
|
71
|
+
store.upsertEntity({
|
|
72
|
+
collection: conflict.collection,
|
|
73
|
+
data: serverData,
|
|
74
|
+
deleted,
|
|
75
|
+
id: conflict.entityId,
|
|
76
|
+
pendingMutationId: "",
|
|
77
|
+
seq: conflict.serverSeq,
|
|
78
|
+
});
|
|
79
|
+
store.clearNeedsRepair({collection: conflict.collection, entityId: conflict.entityId});
|
|
80
|
+
deleteConflict({mutationId, store});
|
|
81
|
+
// Drop the spent outbox row if it is still around. Leaving it as
|
|
82
|
+
// `conflicted` lets recoverStartupState rewrite a phantom `_conflicts`
|
|
83
|
+
// row on the next start(); a corrupt `queued` row for this burned id
|
|
84
|
+
// must not replay either.
|
|
85
|
+
if (outbox.getMutation({mutationId})) {
|
|
86
|
+
outbox.discard({mutationId});
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// keepMine: replay the local mutation against the latest server version,
|
|
92
|
+
// under a fresh mutationId (see requeue).
|
|
93
|
+
const existing = outbox.getMutation({mutationId});
|
|
94
|
+
if (!existing) {
|
|
95
|
+
// Conflict row without an outbox row — nothing to retry; just clear UI.
|
|
96
|
+
deleteConflict({mutationId, store});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (existing.status !== "conflicted") {
|
|
100
|
+
// Repair corrupt/racing state (e.g. markQueued-from-conflicted) so requeue
|
|
101
|
+
// can mint a fresh id for the burned mutationId.
|
|
102
|
+
outbox.restoreConflicted({mutationId});
|
|
103
|
+
}
|
|
104
|
+
const retry = outbox.requeue({baseVersion: conflict.serverSeq, mutationId});
|
|
105
|
+
const entity = store.getEntity({collection: conflict.collection, id: conflict.entityId});
|
|
106
|
+
if (entity?.pendingMutationId === mutationId) {
|
|
107
|
+
store.upsertEntity({
|
|
108
|
+
collection: conflict.collection,
|
|
109
|
+
data: entity.data,
|
|
110
|
+
id: conflict.entityId,
|
|
111
|
+
pendingMutationId: retry.mutationId,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
deleteConflict({mutationId, store});
|
|
115
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {DefaultPersisterFactoryConfig, PersisterFactory} from "./types";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_STORE_TABLE_NAME = "terreno_syncdb";
|
|
4
|
+
|
|
5
|
+
const MISSING_EXPO_SQLITE_MESSAGE =
|
|
6
|
+
"@terreno/syncdb needs the optional expo-sqlite peer for native persistence. Add it to your " +
|
|
7
|
+
"app (not just to a library that depends on @terreno/syncdb, or Expo autolinking will skip " +
|
|
8
|
+
"it) with `bunx expo install expo-sqlite`, then rebuild the native project so the ExpoSQLite " +
|
|
9
|
+
"module is linked in — reloading JS is not enough.";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Loads expo-sqlite, collapsing its two failure modes into one actionable error: the JS package
|
|
13
|
+
* missing entirely (`require` throws) and the package resolving while its native module is
|
|
14
|
+
* absent from the build, which otherwise surfaces as a bare "openDatabaseSync of undefined".
|
|
15
|
+
*/
|
|
16
|
+
const loadExpoSqlite = (): typeof import("expo-sqlite") => {
|
|
17
|
+
let expoSqlite: typeof import("expo-sqlite") | undefined;
|
|
18
|
+
try {
|
|
19
|
+
expoSqlite = require("expo-sqlite") as typeof import("expo-sqlite");
|
|
20
|
+
} catch (error: unknown) {
|
|
21
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
22
|
+
throw new Error(`${MISSING_EXPO_SQLITE_MESSAGE} (${detail})`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof expoSqlite?.openDatabaseSync !== "function") {
|
|
26
|
+
throw new Error(MISSING_EXPO_SQLITE_MESSAGE);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return expoSqlite;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Native default persister: the MergeableStore is stored in an expo-sqlite
|
|
34
|
+
* database using TinyBase's JSON serialization mode (a string table name —
|
|
35
|
+
* required, since tabular mode cannot carry MergeableStore CRDT metadata).
|
|
36
|
+
* SQLite files are already sandboxed per app on iOS/Android, so no additional
|
|
37
|
+
* encryption layer is applied here.
|
|
38
|
+
*/
|
|
39
|
+
export const createDefaultPersisterFactory = (
|
|
40
|
+
config: DefaultPersisterFactoryConfig = {}
|
|
41
|
+
): PersisterFactory => {
|
|
42
|
+
return ({store, databaseName}) => {
|
|
43
|
+
// Lazy requires: TinyBase's expo-sqlite persister module itself imports
|
|
44
|
+
// expo-sqlite at its top level, so deferring BOTH loads until a persister
|
|
45
|
+
// is actually created keeps @terreno/syncdb importable in apps (and test
|
|
46
|
+
// runtimes) that do not have the optional expo-sqlite peer installed.
|
|
47
|
+
const {openDatabaseSync} = loadExpoSqlite();
|
|
48
|
+
const {createExpoSqlitePersister} =
|
|
49
|
+
require("tinybase/persisters/persister-expo-sqlite") as typeof import("tinybase/persisters/persister-expo-sqlite");
|
|
50
|
+
const db = openDatabaseSync(databaseName);
|
|
51
|
+
return createExpoSqlitePersister(store, db, config.storeTableName ?? DEFAULT_STORE_TABLE_NAME);
|
|
52
|
+
};
|
|
53
|
+
};
|