cojson 0.8.12 → 0.8.17

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.
Files changed (164) hide show
  1. package/CHANGELOG.md +95 -83
  2. package/dist/native/PeerKnownStates.js +6 -1
  3. package/dist/native/PeerKnownStates.js.map +1 -1
  4. package/dist/native/PeerState.js +4 -3
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/PriorityBasedMessageQueue.js +1 -10
  7. package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
  8. package/dist/native/SyncStateSubscriptionManager.js +70 -0
  9. package/dist/native/SyncStateSubscriptionManager.js.map +1 -0
  10. package/dist/native/base64url.js.map +1 -1
  11. package/dist/native/base64url.test.js +1 -1
  12. package/dist/native/base64url.test.js.map +1 -1
  13. package/dist/native/coValue.js.map +1 -1
  14. package/dist/native/coValueCore.js +141 -149
  15. package/dist/native/coValueCore.js.map +1 -1
  16. package/dist/native/coValueState.js.map +1 -1
  17. package/dist/native/coValues/account.js +6 -6
  18. package/dist/native/coValues/account.js.map +1 -1
  19. package/dist/native/coValues/coList.js +2 -3
  20. package/dist/native/coValues/coList.js.map +1 -1
  21. package/dist/native/coValues/coMap.js +1 -1
  22. package/dist/native/coValues/coMap.js.map +1 -1
  23. package/dist/native/coValues/coStream.js +3 -5
  24. package/dist/native/coValues/coStream.js.map +1 -1
  25. package/dist/native/coValues/group.js +11 -11
  26. package/dist/native/coValues/group.js.map +1 -1
  27. package/dist/native/coreToCoValue.js +2 -2
  28. package/dist/native/coreToCoValue.js.map +1 -1
  29. package/dist/native/crypto/PureJSCrypto.js +4 -4
  30. package/dist/native/crypto/PureJSCrypto.js.map +1 -1
  31. package/dist/native/crypto/crypto.js.map +1 -1
  32. package/dist/native/exports.js +12 -12
  33. package/dist/native/exports.js.map +1 -1
  34. package/dist/native/ids.js.map +1 -1
  35. package/dist/native/jsonStringify.js.map +1 -1
  36. package/dist/native/localNode.js +5 -7
  37. package/dist/native/localNode.js.map +1 -1
  38. package/dist/native/permissions.js +4 -7
  39. package/dist/native/permissions.js.map +1 -1
  40. package/dist/native/priority.js.map +1 -1
  41. package/dist/native/storage/FileSystem.js.map +1 -1
  42. package/dist/native/storage/chunksAndKnownStates.js +2 -4
  43. package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
  44. package/dist/native/storage/index.js +6 -15
  45. package/dist/native/storage/index.js.map +1 -1
  46. package/dist/native/streamUtils.js.map +1 -1
  47. package/dist/native/sync.js +57 -7
  48. package/dist/native/sync.js.map +1 -1
  49. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  50. package/dist/native/typeUtils/expectGroup.js.map +1 -1
  51. package/dist/native/typeUtils/isAccountID.js.map +1 -1
  52. package/dist/native/typeUtils/isCoValue.js +1 -1
  53. package/dist/native/typeUtils/isCoValue.js.map +1 -1
  54. package/dist/web/PeerKnownStates.js +6 -1
  55. package/dist/web/PeerKnownStates.js.map +1 -1
  56. package/dist/web/PeerState.js +4 -3
  57. package/dist/web/PeerState.js.map +1 -1
  58. package/dist/web/PriorityBasedMessageQueue.js +1 -10
  59. package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
  60. package/dist/web/SyncStateSubscriptionManager.js +70 -0
  61. package/dist/web/SyncStateSubscriptionManager.js.map +1 -0
  62. package/dist/web/base64url.js.map +1 -1
  63. package/dist/web/base64url.test.js +1 -1
  64. package/dist/web/base64url.test.js.map +1 -1
  65. package/dist/web/coValue.js.map +1 -1
  66. package/dist/web/coValueCore.js +141 -149
  67. package/dist/web/coValueCore.js.map +1 -1
  68. package/dist/web/coValueState.js.map +1 -1
  69. package/dist/web/coValues/account.js +6 -6
  70. package/dist/web/coValues/account.js.map +1 -1
  71. package/dist/web/coValues/coList.js +2 -3
  72. package/dist/web/coValues/coList.js.map +1 -1
  73. package/dist/web/coValues/coMap.js +1 -1
  74. package/dist/web/coValues/coMap.js.map +1 -1
  75. package/dist/web/coValues/coStream.js +3 -5
  76. package/dist/web/coValues/coStream.js.map +1 -1
  77. package/dist/web/coValues/group.js +11 -11
  78. package/dist/web/coValues/group.js.map +1 -1
  79. package/dist/web/coreToCoValue.js +2 -2
  80. package/dist/web/coreToCoValue.js.map +1 -1
  81. package/dist/web/crypto/PureJSCrypto.js +4 -4
  82. package/dist/web/crypto/PureJSCrypto.js.map +1 -1
  83. package/dist/web/crypto/WasmCrypto.js +5 -5
  84. package/dist/web/crypto/WasmCrypto.js.map +1 -1
  85. package/dist/web/crypto/crypto.js.map +1 -1
  86. package/dist/web/exports.js +12 -12
  87. package/dist/web/exports.js.map +1 -1
  88. package/dist/web/ids.js.map +1 -1
  89. package/dist/web/jsonStringify.js.map +1 -1
  90. package/dist/web/localNode.js +5 -7
  91. package/dist/web/localNode.js.map +1 -1
  92. package/dist/web/permissions.js +4 -7
  93. package/dist/web/permissions.js.map +1 -1
  94. package/dist/web/priority.js.map +1 -1
  95. package/dist/web/storage/FileSystem.js.map +1 -1
  96. package/dist/web/storage/chunksAndKnownStates.js +2 -4
  97. package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
  98. package/dist/web/storage/index.js +6 -15
  99. package/dist/web/storage/index.js.map +1 -1
  100. package/dist/web/streamUtils.js.map +1 -1
  101. package/dist/web/sync.js +57 -7
  102. package/dist/web/sync.js.map +1 -1
  103. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  104. package/dist/web/typeUtils/expectGroup.js.map +1 -1
  105. package/dist/web/typeUtils/isAccountID.js.map +1 -1
  106. package/dist/web/typeUtils/isCoValue.js +1 -1
  107. package/dist/web/typeUtils/isCoValue.js.map +1 -1
  108. package/package.json +4 -14
  109. package/src/PeerKnownStates.ts +98 -90
  110. package/src/PeerState.ts +92 -73
  111. package/src/PriorityBasedMessageQueue.ts +42 -49
  112. package/src/SyncStateSubscriptionManager.ts +124 -0
  113. package/src/base64url.test.ts +24 -24
  114. package/src/base64url.ts +44 -45
  115. package/src/coValue.ts +45 -45
  116. package/src/coValueCore.ts +746 -785
  117. package/src/coValueState.ts +82 -72
  118. package/src/coValues/account.ts +143 -150
  119. package/src/coValues/coList.ts +520 -522
  120. package/src/coValues/coMap.ts +283 -285
  121. package/src/coValues/coStream.ts +320 -324
  122. package/src/coValues/group.ts +306 -305
  123. package/src/coreToCoValue.ts +28 -31
  124. package/src/crypto/PureJSCrypto.ts +188 -194
  125. package/src/crypto/WasmCrypto.ts +236 -254
  126. package/src/crypto/crypto.ts +302 -309
  127. package/src/exports.ts +116 -116
  128. package/src/ids.ts +9 -9
  129. package/src/jsonStringify.ts +46 -46
  130. package/src/jsonValue.ts +24 -10
  131. package/src/localNode.ts +635 -660
  132. package/src/media.ts +3 -3
  133. package/src/permissions.ts +272 -278
  134. package/src/priority.ts +21 -19
  135. package/src/storage/FileSystem.ts +91 -99
  136. package/src/storage/chunksAndKnownStates.ts +110 -115
  137. package/src/storage/index.ts +466 -497
  138. package/src/streamUtils.ts +60 -60
  139. package/src/sync.ts +656 -608
  140. package/src/tests/PeerKnownStates.test.ts +38 -34
  141. package/src/tests/PeerState.test.ts +101 -64
  142. package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
  143. package/src/tests/SyncStateSubscriptionManager.test.ts +232 -0
  144. package/src/tests/account.test.ts +59 -59
  145. package/src/tests/coList.test.ts +65 -65
  146. package/src/tests/coMap.test.ts +137 -137
  147. package/src/tests/coStream.test.ts +254 -257
  148. package/src/tests/coValueCore.test.ts +153 -156
  149. package/src/tests/crypto.test.ts +136 -144
  150. package/src/tests/cryptoImpl.test.ts +205 -197
  151. package/src/tests/group.test.ts +24 -24
  152. package/src/tests/permissions.test.ts +1306 -1371
  153. package/src/tests/priority.test.ts +65 -82
  154. package/src/tests/sync.test.ts +1573 -1263
  155. package/src/tests/testUtils.ts +85 -53
  156. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
  157. package/src/typeUtils/expectGroup.ts +9 -9
  158. package/src/typeUtils/isAccountID.ts +1 -1
  159. package/src/typeUtils/isCoValue.ts +9 -9
  160. package/tsconfig.json +4 -6
  161. package/tsconfig.native.json +9 -11
  162. package/tsconfig.web.json +4 -10
  163. package/.eslintrc.cjs +0 -25
  164. package/.prettierrc.js +0 -9
@@ -1,196 +1,193 @@
1
1
  import { expect, test, vi } from "vitest";
2
2
  import { Transaction } from "../coValueCore.js";
3
- import { LocalNode } from "../localNode.js";
4
- import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
5
3
  import { MapOpPayload } from "../coValues/coMap.js";
6
- import { Role } from "../permissions.js";
7
- import { stableStringify } from "../jsonStringify.js";
8
4
  import { WasmCrypto } from "../crypto/WasmCrypto.js";
5
+ import { stableStringify } from "../jsonStringify.js";
6
+ import { LocalNode } from "../localNode.js";
7
+ import { Role } from "../permissions.js";
8
+ import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
9
9
 
10
10
  const Crypto = await WasmCrypto.create();
11
11
 
12
12
  test("Can create coValue with new agent credentials and add transaction to it", () => {
13
- const [account, sessionID] = randomAnonymousAccountAndSessionID();
14
- const node = new LocalNode(account, sessionID, Crypto);
15
-
16
- const coValue = node.createCoValue({
17
- type: "costream",
18
- ruleset: { type: "unsafeAllowAll" },
19
- meta: null,
20
- ...Crypto.createdNowUnique(),
21
- });
22
-
23
- const transaction: Transaction = {
24
- privacy: "trusting",
25
- madeAt: Date.now(),
26
- changes: stableStringify([
27
- {
28
- hello: "world",
29
- },
30
- ]),
31
- };
32
-
33
- const { expectedNewHash } = coValue.expectedNewHashAfter(
13
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
14
+ const node = new LocalNode(account, sessionID, Crypto);
15
+
16
+ const coValue = node.createCoValue({
17
+ type: "costream",
18
+ ruleset: { type: "unsafeAllowAll" },
19
+ meta: null,
20
+ ...Crypto.createdNowUnique(),
21
+ });
22
+
23
+ const transaction: Transaction = {
24
+ privacy: "trusting",
25
+ madeAt: Date.now(),
26
+ changes: stableStringify([
27
+ {
28
+ hello: "world",
29
+ },
30
+ ]),
31
+ };
32
+
33
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
34
+ node.currentSessionID,
35
+ [transaction],
36
+ );
37
+
38
+ expect(
39
+ coValue
40
+ .tryAddTransactions(
34
41
  node.currentSessionID,
35
42
  [transaction],
36
- );
37
-
38
- expect(
39
- coValue
40
- .tryAddTransactions(
41
- node.currentSessionID,
42
- [transaction],
43
- expectedNewHash,
44
- Crypto.sign(account.currentSignerSecret(), expectedNewHash),
45
- )
46
- ._unsafeUnwrap(),
47
- ).toBe(true);
43
+ expectedNewHash,
44
+ Crypto.sign(account.currentSignerSecret(), expectedNewHash),
45
+ )
46
+ ._unsafeUnwrap(),
47
+ ).toBe(true);
48
48
  });
49
49
 
50
50
  test("transactions with wrong signature are rejected", () => {
51
- const wrongAgent = Crypto.newRandomAgentSecret();
52
- const [agentSecret, sessionID] = randomAnonymousAccountAndSessionID();
53
- const node = new LocalNode(agentSecret, sessionID, Crypto);
54
-
55
- const coValue = node.createCoValue({
56
- type: "costream",
57
- ruleset: { type: "unsafeAllowAll" },
58
- meta: null,
59
- ...Crypto.createdNowUnique(),
60
- });
61
-
62
- const transaction: Transaction = {
63
- privacy: "trusting",
64
- madeAt: Date.now(),
65
- changes: stableStringify([
66
- {
67
- hello: "world",
68
- },
69
- ]),
70
- };
71
-
72
- const { expectedNewHash } = coValue.expectedNewHashAfter(
73
- node.currentSessionID,
74
- [transaction],
75
- );
76
-
77
- // eslint-disable-next-line neverthrow/must-use-result
78
- coValue
79
- .tryAddTransactions(
80
- node.currentSessionID,
81
- [transaction],
82
- expectedNewHash,
83
- Crypto.sign(
84
- Crypto.getAgentSignerSecret(wrongAgent),
85
- expectedNewHash,
86
- ),
87
- )
88
- ._unsafeUnwrapErr({ withStackTrace: true });
51
+ const wrongAgent = Crypto.newRandomAgentSecret();
52
+ const [agentSecret, sessionID] = randomAnonymousAccountAndSessionID();
53
+ const node = new LocalNode(agentSecret, sessionID, Crypto);
54
+
55
+ const coValue = node.createCoValue({
56
+ type: "costream",
57
+ ruleset: { type: "unsafeAllowAll" },
58
+ meta: null,
59
+ ...Crypto.createdNowUnique(),
60
+ });
61
+
62
+ const transaction: Transaction = {
63
+ privacy: "trusting",
64
+ madeAt: Date.now(),
65
+ changes: stableStringify([
66
+ {
67
+ hello: "world",
68
+ },
69
+ ]),
70
+ };
71
+
72
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
73
+ node.currentSessionID,
74
+ [transaction],
75
+ );
76
+
77
+ // eslint-disable-next-line neverthrow/must-use-result
78
+ coValue
79
+ .tryAddTransactions(
80
+ node.currentSessionID,
81
+ [transaction],
82
+ expectedNewHash,
83
+ Crypto.sign(Crypto.getAgentSignerSecret(wrongAgent), expectedNewHash),
84
+ )
85
+ ._unsafeUnwrapErr({ withStackTrace: true });
89
86
  });
90
87
 
91
88
  test("transactions with correctly signed, but wrong hash are rejected", () => {
92
- const [account, sessionID] = randomAnonymousAccountAndSessionID();
93
- const node = new LocalNode(account, sessionID, Crypto);
94
-
95
- const coValue = node.createCoValue({
96
- type: "costream",
97
- ruleset: { type: "unsafeAllowAll" },
98
- meta: null,
99
- ...Crypto.createdNowUnique(),
100
- });
101
-
102
- const transaction: Transaction = {
89
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
90
+ const node = new LocalNode(account, sessionID, Crypto);
91
+
92
+ const coValue = node.createCoValue({
93
+ type: "costream",
94
+ ruleset: { type: "unsafeAllowAll" },
95
+ meta: null,
96
+ ...Crypto.createdNowUnique(),
97
+ });
98
+
99
+ const transaction: Transaction = {
100
+ privacy: "trusting",
101
+ madeAt: Date.now(),
102
+ changes: stableStringify([
103
+ {
104
+ hello: "world",
105
+ },
106
+ ]),
107
+ };
108
+
109
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
110
+ node.currentSessionID,
111
+ [
112
+ {
103
113
  privacy: "trusting",
104
114
  madeAt: Date.now(),
105
115
  changes: stableStringify([
106
- {
107
- hello: "world",
108
- },
116
+ {
117
+ hello: "wrong",
118
+ },
109
119
  ]),
110
- };
111
-
112
- const { expectedNewHash } = coValue.expectedNewHashAfter(
113
- node.currentSessionID,
114
- [
115
- {
116
- privacy: "trusting",
117
- madeAt: Date.now(),
118
- changes: stableStringify([
119
- {
120
- hello: "wrong",
121
- },
122
- ]),
123
- },
124
- ],
125
- );
126
-
127
- // eslint-disable-next-line neverthrow/must-use-result
128
- coValue
129
- .tryAddTransactions(
130
- node.currentSessionID,
131
- [transaction],
132
- expectedNewHash,
133
- Crypto.sign(account.currentSignerSecret(), expectedNewHash),
134
- )
135
- ._unsafeUnwrapErr({ withStackTrace: true });
120
+ },
121
+ ],
122
+ );
123
+
124
+ // eslint-disable-next-line neverthrow/must-use-result
125
+ coValue
126
+ .tryAddTransactions(
127
+ node.currentSessionID,
128
+ [transaction],
129
+ expectedNewHash,
130
+ Crypto.sign(account.currentSignerSecret(), expectedNewHash),
131
+ )
132
+ ._unsafeUnwrapErr({ withStackTrace: true });
136
133
  });
137
134
 
138
135
  test("New transactions in a group correctly update owned values, including subscriptions", async () => {
139
- const [account, sessionID] = randomAnonymousAccountAndSessionID();
140
- const node = new LocalNode(account, sessionID, Crypto);
136
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
137
+ const node = new LocalNode(account, sessionID, Crypto);
141
138
 
142
- const group = node.createGroup();
139
+ const group = node.createGroup();
143
140
 
144
- const timeBeforeEdit = Date.now();
141
+ const timeBeforeEdit = Date.now();
145
142
 
146
- await new Promise((resolve) => setTimeout(resolve, 10));
143
+ await new Promise((resolve) => setTimeout(resolve, 10));
147
144
 
148
- const map = group.createMap();
145
+ const map = group.createMap();
149
146
 
150
- map.set("hello", "world");
147
+ map.set("hello", "world");
151
148
 
152
- const listener = vi.fn();
149
+ const listener = vi.fn();
153
150
 
154
- map.subscribe(listener);
151
+ map.subscribe(listener);
155
152
 
156
- expect(listener.mock.calls[0][0].get("hello")).toBe("world");
153
+ expect(listener.mock.calls[0][0].get("hello")).toBe("world");
157
154
 
158
- const resignationThatWeJustLearnedAbout = {
159
- privacy: "trusting",
160
- madeAt: timeBeforeEdit,
161
- changes: stableStringify([
162
- {
163
- op: "set",
164
- key: account.id,
165
- value: "revoked",
166
- } satisfies MapOpPayload<typeof account.id, Role>,
167
- ]),
168
- } satisfies Transaction;
155
+ const resignationThatWeJustLearnedAbout = {
156
+ privacy: "trusting",
157
+ madeAt: timeBeforeEdit,
158
+ changes: stableStringify([
159
+ {
160
+ op: "set",
161
+ key: account.id,
162
+ value: "revoked",
163
+ } satisfies MapOpPayload<typeof account.id, Role>,
164
+ ]),
165
+ } satisfies Transaction;
169
166
 
170
- const { expectedNewHash } = group.core.expectedNewHashAfter(sessionID, [
171
- resignationThatWeJustLearnedAbout,
172
- ]);
167
+ const { expectedNewHash } = group.core.expectedNewHashAfter(sessionID, [
168
+ resignationThatWeJustLearnedAbout,
169
+ ]);
173
170
 
174
- const signature = Crypto.sign(
175
- node.account.currentSignerSecret(),
176
- expectedNewHash,
177
- );
171
+ const signature = Crypto.sign(
172
+ node.account.currentSignerSecret(),
173
+ expectedNewHash,
174
+ );
178
175
 
179
- expect(map.core.getValidSortedTransactions().length).toBe(1);
176
+ expect(map.core.getValidSortedTransactions().length).toBe(1);
180
177
 
181
- const manuallyAdddedTxSuccess = group.core
182
- .tryAddTransactions(
183
- node.currentSessionID,
184
- [resignationThatWeJustLearnedAbout],
185
- expectedNewHash,
186
- signature,
187
- )
188
- ._unsafeUnwrap({ withStackTrace: true });
178
+ const manuallyAdddedTxSuccess = group.core
179
+ .tryAddTransactions(
180
+ node.currentSessionID,
181
+ [resignationThatWeJustLearnedAbout],
182
+ expectedNewHash,
183
+ signature,
184
+ )
185
+ ._unsafeUnwrap({ withStackTrace: true });
189
186
 
190
- expect(manuallyAdddedTxSuccess).toBe(true);
187
+ expect(manuallyAdddedTxSuccess).toBe(true);
191
188
 
192
- expect(listener.mock.calls.length).toBe(2);
193
- expect(listener.mock.calls[1][0].get("hello")).toBe(undefined);
189
+ expect(listener.mock.calls.length).toBe(2);
190
+ expect(listener.mock.calls[1][0].get("hello")).toBe(undefined);
194
191
 
195
- expect(map.core.getValidSortedTransactions().length).toBe(0);
192
+ expect(map.core.getValidSortedTransactions().length).toBe(0);
196
193
  });