cojson 0.8.12 → 0.8.16
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/CHANGELOG.md +89 -83
- package/dist/native/PeerKnownStates.js +1 -1
- package/dist/native/PeerKnownStates.js.map +1 -1
- package/dist/native/PeerState.js +1 -1
- package/dist/native/PeerState.js.map +1 -1
- package/dist/native/PriorityBasedMessageQueue.js +1 -10
- package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/native/base64url.js.map +1 -1
- package/dist/native/base64url.test.js +1 -1
- package/dist/native/base64url.test.js.map +1 -1
- package/dist/native/coValue.js.map +1 -1
- package/dist/native/coValueCore.js +141 -149
- package/dist/native/coValueCore.js.map +1 -1
- package/dist/native/coValueState.js.map +1 -1
- package/dist/native/coValues/account.js +6 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +2 -3
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +1 -1
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +3 -5
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/group.js +11 -11
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coreToCoValue.js +2 -2
- package/dist/native/coreToCoValue.js.map +1 -1
- package/dist/native/crypto/PureJSCrypto.js +4 -4
- package/dist/native/crypto/PureJSCrypto.js.map +1 -1
- package/dist/native/crypto/crypto.js.map +1 -1
- package/dist/native/exports.js +12 -12
- package/dist/native/exports.js.map +1 -1
- package/dist/native/ids.js.map +1 -1
- package/dist/native/jsonStringify.js.map +1 -1
- package/dist/native/localNode.js +5 -7
- package/dist/native/localNode.js.map +1 -1
- package/dist/native/permissions.js +4 -7
- package/dist/native/permissions.js.map +1 -1
- package/dist/native/priority.js.map +1 -1
- package/dist/native/storage/FileSystem.js.map +1 -1
- package/dist/native/storage/chunksAndKnownStates.js +2 -4
- package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/native/storage/index.js +6 -15
- package/dist/native/storage/index.js.map +1 -1
- package/dist/native/streamUtils.js.map +1 -1
- package/dist/native/sync.js +2 -4
- package/dist/native/sync.js.map +1 -1
- package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/native/typeUtils/expectGroup.js.map +1 -1
- package/dist/native/typeUtils/isAccountID.js.map +1 -1
- package/dist/native/typeUtils/isCoValue.js +1 -1
- package/dist/native/typeUtils/isCoValue.js.map +1 -1
- package/dist/web/PeerKnownStates.js +1 -1
- package/dist/web/PeerKnownStates.js.map +1 -1
- package/dist/web/PeerState.js +1 -1
- package/dist/web/PeerState.js.map +1 -1
- package/dist/web/PriorityBasedMessageQueue.js +1 -10
- package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/web/base64url.js.map +1 -1
- package/dist/web/base64url.test.js +1 -1
- package/dist/web/base64url.test.js.map +1 -1
- package/dist/web/coValue.js.map +1 -1
- package/dist/web/coValueCore.js +141 -149
- package/dist/web/coValueCore.js.map +1 -1
- package/dist/web/coValueState.js.map +1 -1
- package/dist/web/coValues/account.js +6 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +2 -3
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +1 -1
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +3 -5
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/group.js +11 -11
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coreToCoValue.js +2 -2
- package/dist/web/coreToCoValue.js.map +1 -1
- package/dist/web/crypto/PureJSCrypto.js +4 -4
- package/dist/web/crypto/PureJSCrypto.js.map +1 -1
- package/dist/web/crypto/WasmCrypto.js +5 -5
- package/dist/web/crypto/WasmCrypto.js.map +1 -1
- package/dist/web/crypto/crypto.js.map +1 -1
- package/dist/web/exports.js +12 -12
- package/dist/web/exports.js.map +1 -1
- package/dist/web/ids.js.map +1 -1
- package/dist/web/jsonStringify.js.map +1 -1
- package/dist/web/localNode.js +5 -7
- package/dist/web/localNode.js.map +1 -1
- package/dist/web/permissions.js +4 -7
- package/dist/web/permissions.js.map +1 -1
- package/dist/web/priority.js.map +1 -1
- package/dist/web/storage/FileSystem.js.map +1 -1
- package/dist/web/storage/chunksAndKnownStates.js +2 -4
- package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/web/storage/index.js +6 -15
- package/dist/web/storage/index.js.map +1 -1
- package/dist/web/streamUtils.js.map +1 -1
- package/dist/web/sync.js +2 -4
- package/dist/web/sync.js.map +1 -1
- package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/web/typeUtils/expectGroup.js.map +1 -1
- package/dist/web/typeUtils/isAccountID.js.map +1 -1
- package/dist/web/typeUtils/isCoValue.js +1 -1
- package/dist/web/typeUtils/isCoValue.js.map +1 -1
- package/package.json +4 -14
- package/src/PeerKnownStates.ts +91 -89
- package/src/PeerState.ts +72 -73
- package/src/PriorityBasedMessageQueue.ts +42 -49
- package/src/base64url.test.ts +24 -24
- package/src/base64url.ts +44 -45
- package/src/coValue.ts +45 -45
- package/src/coValueCore.ts +746 -785
- package/src/coValueState.ts +82 -72
- package/src/coValues/account.ts +143 -150
- package/src/coValues/coList.ts +520 -522
- package/src/coValues/coMap.ts +283 -285
- package/src/coValues/coStream.ts +320 -324
- package/src/coValues/group.ts +306 -305
- package/src/coreToCoValue.ts +28 -31
- package/src/crypto/PureJSCrypto.ts +188 -194
- package/src/crypto/WasmCrypto.ts +236 -254
- package/src/crypto/crypto.ts +302 -309
- package/src/exports.ts +116 -116
- package/src/ids.ts +9 -9
- package/src/jsonStringify.ts +46 -46
- package/src/jsonValue.ts +24 -10
- package/src/localNode.ts +635 -660
- package/src/media.ts +3 -3
- package/src/permissions.ts +272 -278
- package/src/priority.ts +21 -19
- package/src/storage/FileSystem.ts +91 -99
- package/src/storage/chunksAndKnownStates.ts +110 -115
- package/src/storage/index.ts +466 -497
- package/src/streamUtils.ts +60 -60
- package/src/sync.ts +593 -615
- package/src/tests/PeerKnownStates.test.ts +38 -34
- package/src/tests/PeerState.test.ts +101 -64
- package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
- package/src/tests/account.test.ts +59 -59
- package/src/tests/coList.test.ts +65 -65
- package/src/tests/coMap.test.ts +137 -137
- package/src/tests/coStream.test.ts +254 -257
- package/src/tests/coValueCore.test.ts +153 -156
- package/src/tests/crypto.test.ts +136 -144
- package/src/tests/cryptoImpl.test.ts +205 -197
- package/src/tests/group.test.ts +24 -24
- package/src/tests/permissions.test.ts +1306 -1371
- package/src/tests/priority.test.ts +65 -82
- package/src/tests/sync.test.ts +1300 -1291
- package/src/tests/testUtils.ts +52 -53
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
- package/src/typeUtils/expectGroup.ts +9 -9
- package/src/typeUtils/isAccountID.ts +1 -1
- package/src/typeUtils/isCoValue.ts +9 -9
- package/tsconfig.json +4 -6
- package/tsconfig.native.json +9 -11
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -25
- package/.prettierrc.js +0 -9
|
@@ -1,47 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { RawCoID, SessionID } from
|
|
4
|
-
import {
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
import { PeerKnownStates } from "../PeerKnownStates.js";
|
|
3
|
+
import { RawCoID, SessionID } from "../ids.js";
|
|
4
|
+
import { CoValueKnownState, emptyKnownState } from "../sync.js";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
test('should set and get a known state', () => {
|
|
6
|
+
describe("PeerKnownStates", () => {
|
|
7
|
+
test("should set and get a known state", () => {
|
|
9
8
|
const peerKnownStates = new PeerKnownStates();
|
|
10
|
-
const id =
|
|
9
|
+
const id = "test-id" as RawCoID;
|
|
11
10
|
const knownState: CoValueKnownState = emptyKnownState(id);
|
|
12
11
|
|
|
13
|
-
peerKnownStates.dispatch({ type:
|
|
12
|
+
peerKnownStates.dispatch({ type: "SET", id, value: knownState });
|
|
14
13
|
|
|
15
14
|
expect(peerKnownStates.get(id)).toEqual(knownState);
|
|
16
15
|
expect(peerKnownStates.has(id)).toBe(true);
|
|
17
16
|
});
|
|
18
17
|
|
|
19
|
-
test(
|
|
18
|
+
test("should update header", () => {
|
|
20
19
|
const peerKnownStates = new PeerKnownStates();
|
|
21
|
-
const id =
|
|
20
|
+
const id = "test-id" as RawCoID;
|
|
22
21
|
|
|
23
|
-
peerKnownStates.dispatch({ type:
|
|
22
|
+
peerKnownStates.dispatch({ type: "UPDATE_HEADER", id, header: true });
|
|
24
23
|
|
|
25
24
|
const result = peerKnownStates.get(id);
|
|
26
25
|
expect(result?.header).toBe(true);
|
|
27
26
|
});
|
|
28
27
|
|
|
29
|
-
test(
|
|
28
|
+
test("should update session counter", () => {
|
|
30
29
|
const peerKnownStates = new PeerKnownStates();
|
|
31
|
-
const id =
|
|
32
|
-
const sessionId =
|
|
30
|
+
const id = "test-id" as RawCoID;
|
|
31
|
+
const sessionId = "session-1" as SessionID;
|
|
33
32
|
|
|
34
|
-
peerKnownStates.dispatch({
|
|
33
|
+
peerKnownStates.dispatch({
|
|
34
|
+
type: "UPDATE_SESSION_COUNTER",
|
|
35
|
+
id,
|
|
36
|
+
sessionId,
|
|
37
|
+
value: 5,
|
|
38
|
+
});
|
|
35
39
|
|
|
36
40
|
const result = peerKnownStates.get(id);
|
|
37
41
|
expect(result?.sessions[sessionId]).toBe(5);
|
|
38
42
|
});
|
|
39
43
|
|
|
40
|
-
test(
|
|
44
|
+
test("should combine with existing state", () => {
|
|
41
45
|
const peerKnownStates = new PeerKnownStates();
|
|
42
|
-
const id =
|
|
43
|
-
const session1 =
|
|
44
|
-
const session2 =
|
|
46
|
+
const id = "test-id" as RawCoID;
|
|
47
|
+
const session1 = "session-1" as SessionID;
|
|
48
|
+
const session2 = "session-2" as SessionID;
|
|
45
49
|
const initialState: CoValueKnownState = {
|
|
46
50
|
...emptyKnownState(id),
|
|
47
51
|
sessions: { [session1]: 5 },
|
|
@@ -51,50 +55,50 @@ describe('PeerKnownStates', () => {
|
|
|
51
55
|
sessions: { [session2]: 10 },
|
|
52
56
|
};
|
|
53
57
|
|
|
54
|
-
peerKnownStates.dispatch({ type:
|
|
55
|
-
peerKnownStates.dispatch({ type:
|
|
58
|
+
peerKnownStates.dispatch({ type: "SET", id, value: initialState });
|
|
59
|
+
peerKnownStates.dispatch({ type: "COMBINE_WITH", id, value: combineState });
|
|
56
60
|
|
|
57
61
|
const result = peerKnownStates.get(id);
|
|
58
62
|
expect(result?.sessions).toEqual({ [session1]: 5, [session2]: 10 });
|
|
59
63
|
});
|
|
60
64
|
|
|
61
|
-
test(
|
|
65
|
+
test("should set as empty", () => {
|
|
62
66
|
const peerKnownStates = new PeerKnownStates();
|
|
63
|
-
const id =
|
|
64
|
-
const sessionId =
|
|
67
|
+
const id = "test-id" as RawCoID;
|
|
68
|
+
const sessionId = "session-1" as SessionID;
|
|
65
69
|
const initialState: CoValueKnownState = {
|
|
66
70
|
...emptyKnownState(id),
|
|
67
71
|
sessions: { [sessionId]: 5 },
|
|
68
72
|
};
|
|
69
73
|
|
|
70
|
-
peerKnownStates.dispatch({ type:
|
|
71
|
-
peerKnownStates.dispatch({ type:
|
|
74
|
+
peerKnownStates.dispatch({ type: "SET", id, value: initialState });
|
|
75
|
+
peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
|
|
72
76
|
|
|
73
77
|
const result = peerKnownStates.get(id);
|
|
74
78
|
expect(result).toEqual(emptyKnownState(id));
|
|
75
79
|
});
|
|
76
80
|
|
|
77
|
-
test(
|
|
81
|
+
test("should trigger listeners on dispatch", () => {
|
|
78
82
|
const peerKnownStates = new PeerKnownStates();
|
|
79
|
-
const id =
|
|
83
|
+
const id = "test-id" as RawCoID;
|
|
80
84
|
const listener = vi.fn();
|
|
81
85
|
|
|
82
86
|
peerKnownStates.subscribe(listener);
|
|
83
|
-
peerKnownStates.dispatch({ type:
|
|
87
|
+
peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
|
|
84
88
|
|
|
85
89
|
expect(listener).toHaveBeenCalledWith(id, emptyKnownState(id));
|
|
86
90
|
});
|
|
87
91
|
|
|
88
|
-
test(
|
|
92
|
+
test("should unsubscribe listener", () => {
|
|
89
93
|
const peerKnownStates = new PeerKnownStates();
|
|
90
|
-
const id =
|
|
94
|
+
const id = "test-id" as RawCoID;
|
|
91
95
|
const listener = vi.fn();
|
|
92
96
|
|
|
93
97
|
const unsubscribe = peerKnownStates.subscribe(listener);
|
|
94
98
|
unsubscribe();
|
|
95
99
|
|
|
96
|
-
peerKnownStates.dispatch({ type:
|
|
100
|
+
peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
|
|
97
101
|
|
|
98
102
|
expect(listener).not.toHaveBeenCalled();
|
|
99
103
|
});
|
|
100
|
-
});
|
|
104
|
+
});
|
|
@@ -1,81 +1,118 @@
|
|
|
1
|
-
import { describe,
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
2
|
import { PeerState } from "../PeerState.js";
|
|
3
|
-
import { Peer, SyncMessage } from "../sync.js";
|
|
4
3
|
import { CO_VALUE_PRIORITY } from "../priority.js";
|
|
4
|
+
import { Peer, SyncMessage } from "../sync.js";
|
|
5
5
|
|
|
6
6
|
function setup() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
const mockPeer: Peer = {
|
|
8
|
+
id: "test-peer",
|
|
9
|
+
role: "peer",
|
|
10
|
+
priority: 1,
|
|
11
|
+
crashOnClose: false,
|
|
12
|
+
incoming: (async function* () {})(),
|
|
13
|
+
outgoing: {
|
|
14
|
+
push: vi.fn().mockResolvedValue(undefined),
|
|
15
|
+
close: vi.fn(),
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
const peerState = new PeerState(mockPeer);
|
|
19
|
+
return { mockPeer, peerState };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
describe("PeerState", () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
test("should push outgoing message to peer", async () => {
|
|
24
|
+
const { mockPeer, peerState } = setup();
|
|
25
|
+
const message: SyncMessage = {
|
|
26
|
+
action: "load",
|
|
27
|
+
id: "co_ztest-id",
|
|
28
|
+
header: false,
|
|
29
|
+
sessions: {},
|
|
30
|
+
};
|
|
31
|
+
await peerState.pushOutgoingMessage(message);
|
|
32
|
+
expect(mockPeer.outgoing.push).toHaveBeenCalledWith(message);
|
|
33
|
+
});
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
test("should return peer's incoming when not closed", () => {
|
|
36
|
+
const { mockPeer, peerState } = setup();
|
|
37
|
+
expect(peerState.incoming).toBe(mockPeer.incoming);
|
|
38
|
+
});
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
test("should return Disconnected when closed", async () => {
|
|
41
|
+
const { peerState } = setup();
|
|
42
|
+
peerState.gracefulShutdown();
|
|
43
|
+
const incomingIterator = peerState.incoming[Symbol.asyncIterator]();
|
|
44
|
+
const { value, done } = await incomingIterator.next();
|
|
45
|
+
expect(value).toBe("Disconnected");
|
|
46
|
+
expect(done).toBe(false);
|
|
47
|
+
});
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
test("should perform graceful shutdown", () => {
|
|
50
|
+
const { mockPeer, peerState } = setup();
|
|
51
|
+
const consoleSpy = vi.spyOn(console, "debug").mockImplementation(() => {});
|
|
52
|
+
peerState.gracefulShutdown();
|
|
53
|
+
expect(mockPeer.outgoing.close).toHaveBeenCalled();
|
|
54
|
+
expect(peerState.closed).toBe(true);
|
|
55
|
+
expect(consoleSpy).toHaveBeenCalledWith("Gracefully closing", "test-peer");
|
|
56
|
+
consoleSpy.mockRestore();
|
|
57
|
+
});
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
test("should schedule outgoing messages based on their priority", async () => {
|
|
60
|
+
const { peerState } = setup();
|
|
56
61
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
const loadMessage: SyncMessage = {
|
|
63
|
+
action: "load",
|
|
64
|
+
id: "co_zhigh",
|
|
65
|
+
header: false,
|
|
66
|
+
sessions: {},
|
|
67
|
+
};
|
|
68
|
+
const contentMessageHigh: SyncMessage = {
|
|
69
|
+
action: "content",
|
|
70
|
+
id: "co_zhigh",
|
|
71
|
+
new: {},
|
|
72
|
+
priority: CO_VALUE_PRIORITY.HIGH,
|
|
73
|
+
};
|
|
74
|
+
const contentMessageMid: SyncMessage = {
|
|
75
|
+
action: "content",
|
|
76
|
+
id: "co_zmid",
|
|
77
|
+
new: {},
|
|
78
|
+
priority: CO_VALUE_PRIORITY.MEDIUM,
|
|
79
|
+
};
|
|
80
|
+
const contentMessageLow: SyncMessage = {
|
|
81
|
+
action: "content",
|
|
82
|
+
id: "co_zlow",
|
|
83
|
+
new: {},
|
|
84
|
+
priority: CO_VALUE_PRIORITY.LOW,
|
|
85
|
+
};
|
|
61
86
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
const promises = [
|
|
88
|
+
peerState.pushOutgoingMessage(contentMessageLow),
|
|
89
|
+
peerState.pushOutgoingMessage(contentMessageMid),
|
|
90
|
+
peerState.pushOutgoingMessage(contentMessageHigh),
|
|
91
|
+
peerState.pushOutgoingMessage(loadMessage),
|
|
92
|
+
];
|
|
68
93
|
|
|
69
|
-
|
|
94
|
+
await Promise.all(promises);
|
|
70
95
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
96
|
+
// The first message is pushed directly, the other three are queued because are waiting
|
|
97
|
+
// for the first push to be completed.
|
|
98
|
+
expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
|
|
99
|
+
1,
|
|
100
|
+
contentMessageLow,
|
|
101
|
+
);
|
|
74
102
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
103
|
+
// Load message are managed as high priority messages and having the same priority as the content message
|
|
104
|
+
// they follow the push order.
|
|
105
|
+
expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
|
|
106
|
+
2,
|
|
107
|
+
contentMessageHigh,
|
|
108
|
+
);
|
|
109
|
+
expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
|
|
110
|
+
3,
|
|
111
|
+
loadMessage,
|
|
112
|
+
);
|
|
113
|
+
expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
|
|
114
|
+
4,
|
|
115
|
+
contentMessageMid,
|
|
116
|
+
);
|
|
117
|
+
});
|
|
81
118
|
});
|
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
import { describe,
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
2
|
import { PriorityBasedMessageQueue } from "../PriorityBasedMessageQueue.js";
|
|
3
|
-
import { SyncMessage } from "../sync.js";
|
|
4
3
|
import { CO_VALUE_PRIORITY } from "../priority.js";
|
|
4
|
+
import { SyncMessage } from "../sync.js";
|
|
5
5
|
|
|
6
6
|
function setup() {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const queue = new PriorityBasedMessageQueue(CO_VALUE_PRIORITY.MEDIUM);
|
|
8
|
+
return { queue };
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
describe("PriorityBasedMessageQueue", () => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
test("should initialize with correct properties", () => {
|
|
13
|
+
const { queue } = setup();
|
|
14
|
+
expect(queue["defaultPriority"]).toBe(CO_VALUE_PRIORITY.MEDIUM);
|
|
15
|
+
expect(queue["queues"].length).toBe(8);
|
|
16
|
+
expect(queue["queues"].every((q) => q.length === 0)).toBe(true);
|
|
17
|
+
});
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
test("should push message with default priority", async () => {
|
|
20
|
+
const { queue } = setup();
|
|
21
|
+
const message: SyncMessage = {
|
|
22
|
+
action: "load",
|
|
23
|
+
id: "co_ztest-id",
|
|
24
|
+
header: false,
|
|
25
|
+
sessions: {},
|
|
26
|
+
};
|
|
27
|
+
void queue.push(message);
|
|
28
|
+
const pulledEntry = queue.pull();
|
|
29
|
+
expect(pulledEntry?.msg).toEqual(message);
|
|
30
|
+
});
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
test("should push message with specified priority", async () => {
|
|
33
|
+
const { queue } = setup();
|
|
34
|
+
const message: SyncMessage = {
|
|
35
|
+
action: "content",
|
|
36
|
+
id: "co_zhigh",
|
|
37
|
+
new: {},
|
|
38
|
+
priority: CO_VALUE_PRIORITY.HIGH,
|
|
39
|
+
};
|
|
40
|
+
void queue.push(message);
|
|
41
|
+
const pulledEntry = queue.pull();
|
|
42
|
+
expect(pulledEntry?.msg).toEqual(message);
|
|
43
|
+
});
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
45
|
+
test("should pull messages in priority order", async () => {
|
|
46
|
+
const { queue } = setup();
|
|
47
|
+
const lowPriorityMsg: SyncMessage = {
|
|
48
|
+
action: "content",
|
|
49
|
+
id: "co_zlow",
|
|
50
|
+
new: {},
|
|
51
|
+
priority: CO_VALUE_PRIORITY.LOW,
|
|
52
|
+
};
|
|
53
|
+
const mediumPriorityMsg: SyncMessage = {
|
|
54
|
+
action: "content",
|
|
55
|
+
id: "co_zmedium",
|
|
56
|
+
new: {},
|
|
57
|
+
priority: CO_VALUE_PRIORITY.MEDIUM,
|
|
58
|
+
};
|
|
59
|
+
const highPriorityMsg: SyncMessage = {
|
|
60
|
+
action: "content",
|
|
61
|
+
id: "co_zhigh",
|
|
62
|
+
new: {},
|
|
63
|
+
priority: CO_VALUE_PRIORITY.HIGH,
|
|
64
|
+
};
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
void queue.push(lowPriorityMsg);
|
|
67
|
+
void queue.push(mediumPriorityMsg);
|
|
68
|
+
void queue.push(highPriorityMsg);
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
expect(queue.pull()?.msg).toEqual(highPriorityMsg);
|
|
71
|
+
expect(queue.pull()?.msg).toEqual(mediumPriorityMsg);
|
|
72
|
+
expect(queue.pull()?.msg).toEqual(lowPriorityMsg);
|
|
73
|
+
});
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
test("should return undefined when pulling from empty queue", () => {
|
|
76
|
+
const { queue } = setup();
|
|
77
|
+
expect(queue.pull()).toBeUndefined();
|
|
78
|
+
});
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
test("should resolve promise when message is pulled", async () => {
|
|
81
|
+
const { queue } = setup();
|
|
82
|
+
const message: SyncMessage = {
|
|
83
|
+
action: "load",
|
|
84
|
+
id: "co_ztest-id",
|
|
85
|
+
header: false,
|
|
86
|
+
sessions: {},
|
|
87
|
+
};
|
|
88
|
+
const pushPromise = queue.push(message);
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
const pulledEntry = queue.pull();
|
|
91
|
+
pulledEntry?.resolve();
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
await expect(pushPromise).resolves.toBeUndefined();
|
|
94
|
+
});
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
test("should reject promise when message is rejected", async () => {
|
|
97
|
+
const { queue } = setup();
|
|
98
|
+
const message: SyncMessage = {
|
|
99
|
+
action: "load",
|
|
100
|
+
id: "co_ztest-id",
|
|
101
|
+
header: false,
|
|
102
|
+
sessions: {},
|
|
103
|
+
};
|
|
104
|
+
const pushPromise = queue.push(message);
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
const pulledEntry = queue.pull();
|
|
107
|
+
pulledEntry?.reject(new Error("Test error"));
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
await expect(pushPromise).rejects.toThrow("Test error");
|
|
110
|
+
});
|
|
111
111
|
});
|
|
@@ -1,76 +1,76 @@
|
|
|
1
1
|
import { expect, test } from "vitest";
|
|
2
|
+
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
2
3
|
import { LocalNode } from "../localNode.js";
|
|
3
4
|
import { connectedPeers } from "../streamUtils.js";
|
|
4
|
-
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
5
5
|
|
|
6
6
|
const Crypto = await WasmCrypto.create();
|
|
7
7
|
|
|
8
8
|
test("Can create a node while creating a new account with profile", async () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
expect(node).not.toBeNull();
|
|
16
|
-
expect(accountID).not.toBeNull();
|
|
17
|
-
expect(accountSecret).not.toBeNull();
|
|
18
|
-
expect(sessionID).not.toBeNull();
|
|
19
|
-
|
|
20
|
-
expect(node.expectProfileLoaded(accountID).get("name")).toEqual(
|
|
21
|
-
"Hermes Puggington",
|
|
22
|
-
);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("A node with an account can create groups and and objects within them", async () => {
|
|
26
|
-
const { node, accountID } = await LocalNode.withNewlyCreatedAccount({
|
|
27
|
-
creationProps: { name: "Hermes Puggington" },
|
|
28
|
-
crypto: Crypto,
|
|
9
|
+
const { node, accountID, accountSecret, sessionID } =
|
|
10
|
+
await LocalNode.withNewlyCreatedAccount({
|
|
11
|
+
creationProps: { name: "Hermes Puggington" },
|
|
12
|
+
crypto: Crypto,
|
|
29
13
|
});
|
|
30
14
|
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
expect(node).not.toBeNull();
|
|
16
|
+
expect(accountID).not.toBeNull();
|
|
17
|
+
expect(accountSecret).not.toBeNull();
|
|
18
|
+
expect(sessionID).not.toBeNull();
|
|
19
|
+
|
|
20
|
+
expect(node.expectProfileLoaded(accountID).get("name")).toEqual(
|
|
21
|
+
"Hermes Puggington",
|
|
22
|
+
);
|
|
23
|
+
});
|
|
33
24
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
test("A node with an account can create groups and and objects within them", async () => {
|
|
26
|
+
const { node, accountID } = await LocalNode.withNewlyCreatedAccount({
|
|
27
|
+
creationProps: { name: "Hermes Puggington" },
|
|
28
|
+
crypto: Crypto,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const group = await node.createGroup();
|
|
32
|
+
expect(group).not.toBeNull();
|
|
33
|
+
|
|
34
|
+
const map = group.createMap();
|
|
35
|
+
map.set("foo", "bar", "private");
|
|
36
|
+
expect(map.get("foo")).toEqual("bar");
|
|
37
|
+
expect(map.lastEditAt("foo")?.by).toEqual(accountID);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
test("Can create account with one node, and then load it on another", async () => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const group = await node.createGroup();
|
|
48
|
-
expect(group).not.toBeNull();
|
|
49
|
-
|
|
50
|
-
const map = group.createMap();
|
|
51
|
-
map.set("foo", "bar", "private");
|
|
52
|
-
expect(map.get("foo")).toEqual("bar");
|
|
53
|
-
|
|
54
|
-
const [node1asPeer, node2asPeer] = connectedPeers("node1", "node2", {
|
|
55
|
-
trace: true,
|
|
56
|
-
peer1role: "server",
|
|
57
|
-
peer2role: "client",
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
console.log("After connected peers");
|
|
61
|
-
|
|
62
|
-
node.syncManager.addPeer(node2asPeer);
|
|
63
|
-
|
|
64
|
-
const node2 = await LocalNode.withLoadedAccount({
|
|
65
|
-
accountID,
|
|
66
|
-
accountSecret,
|
|
67
|
-
sessionID: Crypto.newRandomSessionID(accountID),
|
|
68
|
-
peersToLoadFrom: [node1asPeer],
|
|
69
|
-
crypto: Crypto,
|
|
41
|
+
const { node, accountID, accountSecret } =
|
|
42
|
+
await LocalNode.withNewlyCreatedAccount({
|
|
43
|
+
creationProps: { name: "Hermes Puggington" },
|
|
44
|
+
crypto: Crypto,
|
|
70
45
|
});
|
|
71
46
|
|
|
72
|
-
|
|
73
|
-
|
|
47
|
+
const group = await node.createGroup();
|
|
48
|
+
expect(group).not.toBeNull();
|
|
49
|
+
|
|
50
|
+
const map = group.createMap();
|
|
51
|
+
map.set("foo", "bar", "private");
|
|
52
|
+
expect(map.get("foo")).toEqual("bar");
|
|
53
|
+
|
|
54
|
+
const [node1asPeer, node2asPeer] = connectedPeers("node1", "node2", {
|
|
55
|
+
trace: true,
|
|
56
|
+
peer1role: "server",
|
|
57
|
+
peer2role: "client",
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.log("After connected peers");
|
|
61
|
+
|
|
62
|
+
node.syncManager.addPeer(node2asPeer);
|
|
63
|
+
|
|
64
|
+
const node2 = await LocalNode.withLoadedAccount({
|
|
65
|
+
accountID,
|
|
66
|
+
accountSecret,
|
|
67
|
+
sessionID: Crypto.newRandomSessionID(accountID),
|
|
68
|
+
peersToLoadFrom: [node1asPeer],
|
|
69
|
+
crypto: Crypto,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const map2 = await node2.load(map.id);
|
|
73
|
+
if (map2 === "unavailable") throw new Error("Map unavailable");
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
expect(map2.get("foo")).toEqual("bar");
|
|
76
76
|
});
|