cojson 0.18.26 → 0.18.28
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +17 -0
- package/dist/PeerKnownStates.d.ts +4 -3
- package/dist/PeerKnownStates.d.ts.map +1 -1
- package/dist/PeerKnownStates.js +27 -18
- package/dist/PeerKnownStates.js.map +1 -1
- package/dist/PeerState.d.ts +3 -2
- package/dist/PeerState.d.ts.map +1 -1
- package/dist/PeerState.js.map +1 -1
- package/dist/SyncStateManager.d.ts +2 -2
- package/dist/SyncStateManager.d.ts.map +1 -1
- package/dist/SyncStateManager.js +2 -10
- package/dist/SyncStateManager.js.map +1 -1
- package/dist/coValueContentMessage.d.ts +1 -1
- package/dist/coValueContentMessage.d.ts.map +1 -1
- package/dist/coValueContentMessage.js +1 -1
- package/dist/coValueContentMessage.js.map +1 -1
- package/dist/coValueCore/SessionMap.d.ts +6 -3
- package/dist/coValueCore/SessionMap.d.ts.map +1 -1
- package/dist/coValueCore/SessionMap.js +41 -8
- package/dist/coValueCore/SessionMap.js.map +1 -1
- package/dist/coValueCore/branching.d.ts +5 -4
- package/dist/coValueCore/branching.d.ts.map +1 -1
- package/dist/coValueCore/branching.js +22 -4
- package/dist/coValueCore/branching.js.map +1 -1
- package/dist/coValueCore/coValueCore.d.ts +29 -25
- package/dist/coValueCore/coValueCore.d.ts.map +1 -1
- package/dist/coValueCore/coValueCore.js +163 -126
- package/dist/coValueCore/coValueCore.js.map +1 -1
- package/dist/coValueCore/decryptTransactionChangesAndMeta.d.ts +3 -0
- package/dist/coValueCore/decryptTransactionChangesAndMeta.d.ts.map +1 -0
- package/dist/coValueCore/decryptTransactionChangesAndMeta.js +34 -0
- package/dist/coValueCore/decryptTransactionChangesAndMeta.js.map +1 -0
- package/dist/coValueCore/verifiedState.d.ts +12 -6
- package/dist/coValueCore/verifiedState.d.ts.map +1 -1
- package/dist/coValueCore/verifiedState.js +28 -56
- package/dist/coValueCore/verifiedState.js.map +1 -1
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.d.ts.map +1 -1
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/exports.d.ts +3 -2
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +2 -1
- package/dist/exports.js.map +1 -1
- package/dist/knownState.d.ts +58 -2
- package/dist/knownState.d.ts.map +1 -1
- package/dist/knownState.js +79 -5
- package/dist/knownState.js.map +1 -1
- package/dist/localNode.js +1 -1
- package/dist/localNode.js.map +1 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +18 -20
- package/dist/permissions.js.map +1 -1
- package/dist/storage/knownState.d.ts +1 -1
- package/dist/storage/knownState.d.ts.map +1 -1
- package/dist/storage/knownState.js +2 -3
- package/dist/storage/knownState.js.map +1 -1
- package/dist/storage/storageAsync.d.ts +2 -1
- package/dist/storage/storageAsync.d.ts.map +1 -1
- package/dist/storage/storageAsync.js +5 -6
- package/dist/storage/storageAsync.js.map +1 -1
- package/dist/storage/storageSync.d.ts +2 -1
- package/dist/storage/storageSync.d.ts.map +1 -1
- package/dist/storage/storageSync.js +5 -5
- package/dist/storage/storageSync.js.map +1 -1
- package/dist/storage/types.d.ts +2 -1
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync.d.ts +2 -12
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +9 -38
- package/dist/sync.js.map +1 -1
- package/dist/tests/PeerKnownStates.test.js +1 -1
- package/dist/tests/PeerKnownStates.test.js.map +1 -1
- package/dist/tests/PeerState.test.js +19 -0
- package/dist/tests/PeerState.test.js.map +1 -1
- package/dist/tests/PureJSCrypto.test.js.map +1 -1
- package/dist/tests/StorageApiAsync.test.js +1 -1
- package/dist/tests/StorageApiAsync.test.js.map +1 -1
- package/dist/tests/StorageApiSync.test.js +1 -2
- package/dist/tests/StorageApiSync.test.js.map +1 -1
- package/dist/tests/StoreQueue.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +1 -1
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/branching.test.js +237 -28
- package/dist/tests/branching.test.js.map +1 -1
- package/dist/tests/coValueContentMessage.test.js +1 -1
- package/dist/tests/coValueContentMessage.test.js.map +1 -1
- package/dist/tests/coValueCore.loadFromStorage.test.d.ts +2 -0
- package/dist/tests/coValueCore.loadFromStorage.test.d.ts.map +1 -0
- package/dist/tests/coValueCore.loadFromStorage.test.js +395 -0
- package/dist/tests/coValueCore.loadFromStorage.test.js.map +1 -0
- package/dist/tests/coValueCore.loadingState.test.d.ts +2 -0
- package/dist/tests/coValueCore.loadingState.test.d.ts.map +1 -0
- package/dist/tests/{coValueCoreLoadingState.test.js → coValueCore.loadingState.test.js} +4 -12
- package/dist/tests/coValueCore.loadingState.test.js.map +1 -0
- package/dist/tests/coValueCore.test.js +30 -5
- package/dist/tests/coValueCore.test.js.map +1 -1
- package/dist/tests/knownState.test.d.ts +2 -0
- package/dist/tests/knownState.test.d.ts.map +1 -0
- package/dist/tests/knownState.test.js +510 -0
- package/dist/tests/knownState.test.js.map +1 -0
- package/dist/tests/messagesTestUtils.d.ts.map +1 -1
- package/dist/tests/messagesTestUtils.js.map +1 -1
- package/dist/tests/priority.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +4 -34
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.upload.test.js.map +1 -1
- package/dist/tests/testUtils.d.ts +7 -1
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +12 -0
- package/dist/tests/testUtils.js.map +1 -1
- package/package.json +3 -3
- package/src/PeerKnownStates.ts +36 -22
- package/src/PeerState.ts +2 -1
- package/src/SyncStateManager.ts +4 -17
- package/src/coValueContentMessage.ts +2 -1
- package/src/coValueCore/SessionMap.ts +66 -11
- package/src/coValueCore/branching.ts +37 -13
- package/src/coValueCore/coValueCore.ts +224 -177
- package/src/coValueCore/decryptTransactionChangesAndMeta.ts +56 -0
- package/src/coValueCore/verifiedState.ts +32 -64
- package/src/coValues/coMap.ts +1 -5
- package/src/coValues/coStream.ts +1 -5
- package/src/exports.ts +2 -2
- package/src/knownState.ts +118 -12
- package/src/localNode.ts +1 -1
- package/src/permissions.ts +21 -22
- package/src/storage/knownState.ts +9 -3
- package/src/storage/storageAsync.ts +15 -7
- package/src/storage/storageSync.ts +16 -8
- package/src/storage/types.ts +2 -1
- package/src/sync.ts +14 -60
- package/src/tests/PeerKnownStates.test.ts +1 -1
- package/src/tests/PeerState.test.ts +29 -3
- package/src/tests/PureJSCrypto.test.ts +0 -1
- package/src/tests/StorageApiAsync.test.ts +3 -6
- package/src/tests/StorageApiSync.test.ts +2 -6
- package/src/tests/StoreQueue.test.ts +3 -2
- package/src/tests/SyncStateManager.test.ts +1 -1
- package/src/tests/branching.test.ts +392 -45
- package/src/tests/coValueContentMessage.test.ts +2 -2
- package/src/tests/coValueCore.loadFromStorage.test.ts +540 -0
- package/src/tests/{coValueCoreLoadingState.test.ts → coValueCore.loadingState.test.ts} +3 -16
- package/src/tests/coValueCore.test.ts +40 -5
- package/src/tests/knownState.test.ts +665 -0
- package/src/tests/messagesTestUtils.ts +2 -1
- package/src/tests/priority.test.ts +0 -2
- package/src/tests/sync.mesh.test.ts +11 -38
- package/src/tests/sync.storage.test.ts +0 -1
- package/src/tests/sync.upload.test.ts +0 -1
- package/src/tests/testUtils.ts +22 -2
- package/dist/coValueCore/decodeTransactionChangesAndMeta.d.ts +0 -3
- package/dist/coValueCore/decodeTransactionChangesAndMeta.d.ts.map +0 -1
- package/dist/coValueCore/decodeTransactionChangesAndMeta.js +0 -59
- package/dist/coValueCore/decodeTransactionChangesAndMeta.js.map +0 -1
- package/dist/tests/coValueCoreLoadingState.test.d.ts +0 -2
- package/dist/tests/coValueCoreLoadingState.test.d.ts.map +0 -1
- package/dist/tests/coValueCoreLoadingState.test.js.map +0 -1
- package/src/coValueCore/decodeTransactionChangesAndMeta.ts +0 -81
package/src/sync.ts
CHANGED
|
@@ -16,20 +16,11 @@ import { logger } from "./logger.js";
|
|
|
16
16
|
import { CoValuePriority } from "./priority.js";
|
|
17
17
|
import { IncomingMessagesQueue } from "./queue/IncomingMessagesQueue.js";
|
|
18
18
|
import { LocalTransactionsSyncQueue } from "./queue/LocalTransactionsSyncQueue.js";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export function emptyKnownState(id: RawCoID): CoValueKnownState {
|
|
27
|
-
return {
|
|
28
|
-
id,
|
|
29
|
-
header: false,
|
|
30
|
-
sessions: {},
|
|
31
|
-
};
|
|
32
|
-
}
|
|
19
|
+
import {
|
|
20
|
+
CoValueKnownState,
|
|
21
|
+
knownStateFrom,
|
|
22
|
+
KnownStateSessions,
|
|
23
|
+
} from "./knownState.js";
|
|
33
24
|
|
|
34
25
|
export type SyncMessage =
|
|
35
26
|
| LoadMessage
|
|
@@ -55,9 +46,7 @@ export type NewContentMessage = {
|
|
|
55
46
|
new: {
|
|
56
47
|
[sessionID: SessionID]: SessionNewContent;
|
|
57
48
|
};
|
|
58
|
-
expectContentUntil?:
|
|
59
|
-
[sessionID: SessionID]: number;
|
|
60
|
-
};
|
|
49
|
+
expectContentUntil?: KnownStateSessions;
|
|
61
50
|
};
|
|
62
51
|
|
|
63
52
|
export type SessionNewContent = {
|
|
@@ -97,31 +86,6 @@ export interface Peer {
|
|
|
97
86
|
persistent?: boolean;
|
|
98
87
|
}
|
|
99
88
|
|
|
100
|
-
export function combinedKnownStates(
|
|
101
|
-
stateA: CoValueKnownState,
|
|
102
|
-
stateB: CoValueKnownState,
|
|
103
|
-
): CoValueKnownState {
|
|
104
|
-
const sessionStates: CoValueKnownState["sessions"] = {};
|
|
105
|
-
|
|
106
|
-
const allSessions = new Set([
|
|
107
|
-
...Object.keys(stateA.sessions),
|
|
108
|
-
...Object.keys(stateB.sessions),
|
|
109
|
-
] as SessionID[]);
|
|
110
|
-
|
|
111
|
-
for (const sessionID of allSessions) {
|
|
112
|
-
const stateAValue = stateA.sessions[sessionID];
|
|
113
|
-
const stateBValue = stateB.sessions[sessionID];
|
|
114
|
-
|
|
115
|
-
sessionStates[sessionID] = Math.max(stateAValue || 0, stateBValue || 0);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
id: stateA.id,
|
|
120
|
-
header: stateA.header || stateB.header,
|
|
121
|
-
sessions: sessionStates,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
89
|
export type ServerPeerSelector = (
|
|
126
90
|
id: RawCoID,
|
|
127
91
|
serverPeers: PeerState[],
|
|
@@ -446,7 +410,7 @@ export class SyncManager {
|
|
|
446
410
|
* This way we can track part of the data loss that may occur when the other peer is restarted
|
|
447
411
|
*
|
|
448
412
|
*/
|
|
449
|
-
peer.setKnownState(msg.id,
|
|
413
|
+
peer.setKnownState(msg.id, knownStateFrom(msg));
|
|
450
414
|
const coValue = this.local.getCoValue(msg.id);
|
|
451
415
|
|
|
452
416
|
if (coValue.isAvailable()) {
|
|
@@ -482,7 +446,7 @@ export class SyncManager {
|
|
|
482
446
|
handleKnownState(msg: KnownStateMessage, peer: PeerState) {
|
|
483
447
|
const coValue = this.local.getCoValue(msg.id);
|
|
484
448
|
|
|
485
|
-
peer.combineWith(msg.id,
|
|
449
|
+
peer.combineWith(msg.id, knownStateFrom(msg));
|
|
486
450
|
|
|
487
451
|
// The header is a boolean value that tells us if the other peer do have information about the header.
|
|
488
452
|
// If it's false in this point it means that the coValue is unavailable on the other peer.
|
|
@@ -706,12 +670,10 @@ export class SyncManager {
|
|
|
706
670
|
contentToStore.new[sessionID] = newContentForSession;
|
|
707
671
|
}
|
|
708
672
|
|
|
709
|
-
|
|
710
|
-
msg.id,
|
|
711
|
-
sessionID,
|
|
673
|
+
const transactionsCount =
|
|
712
674
|
newContentForSession.after +
|
|
713
|
-
|
|
714
|
-
);
|
|
675
|
+
newContentForSession.newTransactions.length;
|
|
676
|
+
peer?.updateSessionCounter(msg.id, sessionID, transactionsCount);
|
|
715
677
|
}
|
|
716
678
|
|
|
717
679
|
/**
|
|
@@ -778,11 +740,11 @@ export class SyncManager {
|
|
|
778
740
|
peer.role === "server" &&
|
|
779
741
|
!peer.loadRequestSent.has(coValue.id)
|
|
780
742
|
) {
|
|
781
|
-
const state = coValue.
|
|
743
|
+
const state = coValue.getLoadingStateForPeer(peer.id);
|
|
782
744
|
|
|
783
745
|
// Check if there is a inflight load operation and we
|
|
784
746
|
// are waiting for other peers to send the load request
|
|
785
|
-
if (state === "unknown"
|
|
747
|
+
if (state === "unknown") {
|
|
786
748
|
// Sending a load message to the peer to get to know how much content is missing
|
|
787
749
|
// before sending the new content
|
|
788
750
|
this.trySendToPeer(peer, {
|
|
@@ -804,7 +766,7 @@ export class SyncManager {
|
|
|
804
766
|
}
|
|
805
767
|
|
|
806
768
|
handleCorrection(msg: KnownStateMessage, peer: PeerState) {
|
|
807
|
-
peer.setKnownState(msg.id,
|
|
769
|
+
peer.setKnownState(msg.id, knownStateFrom(msg));
|
|
808
770
|
|
|
809
771
|
return this.sendNewContent(msg.id, peer);
|
|
810
772
|
}
|
|
@@ -952,14 +914,6 @@ export class SyncManager {
|
|
|
952
914
|
}
|
|
953
915
|
}
|
|
954
916
|
|
|
955
|
-
function knownStateIn(msg: LoadMessage | KnownStateMessage) {
|
|
956
|
-
return {
|
|
957
|
-
id: msg.id,
|
|
958
|
-
header: msg.header,
|
|
959
|
-
sessions: msg.sessions,
|
|
960
|
-
};
|
|
961
|
-
}
|
|
962
|
-
|
|
963
917
|
/**
|
|
964
918
|
* Returns a ServerPeerSelector that implements the Highest Weighted Random (HWR) algorithm.
|
|
965
919
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { PeerKnownStates } from "../PeerKnownStates.js";
|
|
3
3
|
import { RawCoID, SessionID } from "../ids.js";
|
|
4
|
-
import { CoValueKnownState, emptyKnownState } from "../
|
|
4
|
+
import { CoValueKnownState, emptyKnownState } from "../knownState.js";
|
|
5
5
|
|
|
6
6
|
describe("PeerKnownStates", () => {
|
|
7
7
|
test("should set and get a known state", () => {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { PeerState } from "../PeerState.js";
|
|
3
|
-
import { CO_VALUE_PRIORITY } from "../priority.js";
|
|
4
3
|
import { ConnectedPeerChannel } from "../streamUtils.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { Peer, SyncMessage } from "../sync.js";
|
|
5
|
+
import { CoValueKnownState, KnownStateSessions } from "../knownState.js";
|
|
7
6
|
|
|
8
7
|
function setup() {
|
|
9
8
|
const mockPeer: Peer = {
|
|
@@ -80,6 +79,33 @@ describe("PeerState", () => {
|
|
|
80
79
|
expect(optimisticKnownStatesSpy).toHaveBeenCalledWith("co_z1", state);
|
|
81
80
|
});
|
|
82
81
|
|
|
82
|
+
test("dispatching an optimistic update should not affect the known states", () => {
|
|
83
|
+
const { peerState } = setup();
|
|
84
|
+
|
|
85
|
+
const state: CoValueKnownState = {
|
|
86
|
+
id: "co_z1",
|
|
87
|
+
header: false,
|
|
88
|
+
sessions: {},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
peerState.setKnownState("co_z1", state);
|
|
92
|
+
|
|
93
|
+
const optimisticState: CoValueKnownState = {
|
|
94
|
+
id: "co_z1",
|
|
95
|
+
header: false,
|
|
96
|
+
sessions: {
|
|
97
|
+
"session-1": 1,
|
|
98
|
+
} as KnownStateSessions,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
peerState.combineOptimisticWith("co_z1", optimisticState);
|
|
102
|
+
|
|
103
|
+
expect(peerState.knownStates.get("co_z1")).not.toEqual(optimisticState);
|
|
104
|
+
expect(peerState.optimisticKnownStates.get("co_z1")).toEqual(
|
|
105
|
+
optimisticState,
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
83
109
|
test("should use separate references for knownStates and optimisticKnownStates for non-storage peers", () => {
|
|
84
110
|
const { peerState } = setup(); // Uses a regular peer
|
|
85
111
|
|
|
@@ -4,13 +4,9 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { afterEach, describe, expect, onTestFinished, test, vi } from "vitest";
|
|
6
6
|
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
7
|
-
import { CoID, LocalNode,
|
|
7
|
+
import { CoID, LocalNode, RawCoMap, logger } from "../exports.js";
|
|
8
8
|
import { CoValueCore } from "../exports.js";
|
|
9
|
-
import {
|
|
10
|
-
CoValueKnownState,
|
|
11
|
-
NewContentMessage,
|
|
12
|
-
emptyKnownState,
|
|
13
|
-
} from "../sync.js";
|
|
9
|
+
import { NewContentMessage } from "../sync.js";
|
|
14
10
|
import { createAsyncStorage } from "./testStorage.js";
|
|
15
11
|
import {
|
|
16
12
|
SyncMessagesLog,
|
|
@@ -18,6 +14,7 @@ import {
|
|
|
18
14
|
randomAgentAndSessionID,
|
|
19
15
|
waitFor,
|
|
20
16
|
} from "./testUtils.js";
|
|
17
|
+
import { CoValueKnownState, emptyKnownState } from "../knownState.js";
|
|
21
18
|
|
|
22
19
|
const crypto = await WasmCrypto.create();
|
|
23
20
|
|
|
@@ -6,11 +6,8 @@ import { describe, expect, onTestFinished, test, vi } from "vitest";
|
|
|
6
6
|
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
7
7
|
import { CoID, LocalNode, RawCoID, RawCoMap, logger } from "../exports.js";
|
|
8
8
|
import { CoValueCore } from "../exports.js";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
NewContentMessage,
|
|
12
|
-
emptyKnownState,
|
|
13
|
-
} from "../sync.js";
|
|
9
|
+
import { NewContentMessage } from "../sync.js";
|
|
10
|
+
import { CoValueKnownState, emptyKnownState } from "../knownState.js";
|
|
14
11
|
import { createSyncStorage } from "./testStorage.js";
|
|
15
12
|
import { loadCoValueOrFail, randomAgentAndSessionID } from "./testUtils.js";
|
|
16
13
|
|
|
@@ -240,7 +237,6 @@ describe("StorageApiSync", () => {
|
|
|
240
237
|
|
|
241
238
|
// Create a real group and add a member to create transactions
|
|
242
239
|
const group = fixturesNode.createGroup();
|
|
243
|
-
const knownState = group.core.verified.knownState();
|
|
244
240
|
|
|
245
241
|
group.addMember("everyone", "reader");
|
|
246
242
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { StoreQueue } from "../queue/StoreQueue.js";
|
|
3
|
-
import
|
|
3
|
+
import { NewContentMessage } from "../sync.js";
|
|
4
|
+
import { CoValueKnownState } from "../knownState.js";
|
|
4
5
|
|
|
5
6
|
function createMockNewContentMessage(id: string): NewContentMessage {
|
|
6
7
|
return {
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
PeerSyncStateListenerCallback,
|
|
5
5
|
} from "../SyncStateManager.js";
|
|
6
6
|
import { connectedPeers } from "../streamUtils.js";
|
|
7
|
-
import { emptyKnownState } from "../
|
|
7
|
+
import { emptyKnownState } from "../exports.js";
|
|
8
8
|
import {
|
|
9
9
|
SyncMessagesLog,
|
|
10
10
|
loadCoValueOrFail,
|