cojson 0.1.8 → 0.1.10

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 (76) hide show
  1. package/dist/account.d.ts +6 -3
  2. package/dist/account.js +4 -2
  3. package/dist/account.js.map +1 -1
  4. package/dist/coValue.d.ts +44 -80
  5. package/dist/coValue.js +4 -348
  6. package/dist/coValue.js.map +1 -1
  7. package/dist/coValueCore.d.ts +84 -0
  8. package/dist/coValueCore.js +356 -0
  9. package/dist/coValueCore.js.map +1 -0
  10. package/dist/coValues/coList.d.ts +114 -0
  11. package/dist/{contentTypes → coValues}/coList.js +59 -19
  12. package/dist/coValues/coList.js.map +1 -0
  13. package/dist/{contentTypes → coValues}/coMap.d.ts +25 -7
  14. package/dist/{contentTypes → coValues}/coMap.js +34 -15
  15. package/dist/coValues/coMap.js.map +1 -0
  16. package/dist/coValues/coStream.d.ts +69 -0
  17. package/dist/coValues/coStream.js +131 -0
  18. package/dist/coValues/coStream.js.map +1 -0
  19. package/dist/coValues/static.d.ts +14 -0
  20. package/dist/coValues/static.js +20 -0
  21. package/dist/coValues/static.js.map +1 -0
  22. package/dist/group.d.ts +57 -9
  23. package/dist/group.js +94 -28
  24. package/dist/group.js.map +1 -1
  25. package/dist/index.d.ts +19 -10
  26. package/dist/index.js +7 -5
  27. package/dist/index.js.map +1 -1
  28. package/dist/node.d.ts +59 -5
  29. package/dist/node.js +36 -15
  30. package/dist/node.js.map +1 -1
  31. package/dist/permissions.d.ts +2 -2
  32. package/dist/permissions.js +1 -1
  33. package/dist/permissions.js.map +1 -1
  34. package/dist/sync.d.ts +3 -3
  35. package/dist/sync.js +2 -2
  36. package/dist/sync.js.map +1 -1
  37. package/dist/testUtils.d.ts +2 -2
  38. package/dist/testUtils.js +1 -1
  39. package/dist/testUtils.js.map +1 -1
  40. package/package.json +2 -2
  41. package/src/account.test.ts +1 -1
  42. package/src/account.ts +8 -5
  43. package/src/coValue.test.ts +335 -129
  44. package/src/coValue.ts +52 -576
  45. package/src/coValueCore.test.ts +180 -0
  46. package/src/coValueCore.ts +592 -0
  47. package/src/{contentTypes → coValues}/coList.ts +91 -42
  48. package/src/{contentTypes → coValues}/coMap.ts +40 -20
  49. package/src/coValues/coStream.ts +249 -0
  50. package/src/coValues/static.ts +31 -0
  51. package/src/group.test.ts +47 -0
  52. package/src/group.ts +120 -50
  53. package/src/index.ts +43 -28
  54. package/src/node.ts +48 -27
  55. package/src/permissions.test.ts +32 -32
  56. package/src/permissions.ts +5 -5
  57. package/src/sync.test.ts +77 -77
  58. package/src/sync.ts +5 -5
  59. package/src/testUtils.ts +1 -1
  60. package/tsconfig.json +1 -2
  61. package/dist/contentType.d.ts +0 -15
  62. package/dist/contentType.js +0 -7
  63. package/dist/contentType.js.map +0 -1
  64. package/dist/contentTypes/coList.d.ts +0 -77
  65. package/dist/contentTypes/coList.js.map +0 -1
  66. package/dist/contentTypes/coMap.js.map +0 -1
  67. package/dist/contentTypes/coStream.d.ts +0 -11
  68. package/dist/contentTypes/coStream.js +0 -16
  69. package/dist/contentTypes/coStream.js.map +0 -1
  70. package/dist/contentTypes/static.d.ts +0 -11
  71. package/dist/contentTypes/static.js +0 -14
  72. package/dist/contentTypes/static.js.map +0 -1
  73. package/src/contentType.test.ts +0 -284
  74. package/src/contentType.ts +0 -26
  75. package/src/contentTypes/coStream.ts +0 -24
  76. package/src/contentTypes/static.ts +0 -22
@@ -0,0 +1,180 @@
1
+ import { Transaction } from "./coValueCore.js";
2
+ import { LocalNode } from "./node.js";
3
+ import { createdNowUnique, getAgentSignerSecret, newRandomAgentSecret, sign } from "./crypto.js";
4
+ import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
5
+ import { MapOpPayload } from "./coValues/coMap.js";
6
+ import { Role } from "./permissions.js";
7
+
8
+ test("Can create coValue with new agent credentials and add transaction to it", () => {
9
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
10
+ const node = new LocalNode(account, sessionID);
11
+
12
+ const coValue = node.createCoValue({
13
+ type: "costream",
14
+ ruleset: { type: "unsafeAllowAll" },
15
+ meta: null,
16
+ ...createdNowUnique(),
17
+ });
18
+
19
+ const transaction: Transaction = {
20
+ privacy: "trusting",
21
+ madeAt: Date.now(),
22
+ changes: [
23
+ {
24
+ hello: "world",
25
+ },
26
+ ],
27
+ };
28
+
29
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
30
+ node.currentSessionID,
31
+ [transaction]
32
+ );
33
+
34
+ expect(
35
+ coValue.tryAddTransactions(
36
+ node.currentSessionID,
37
+ [transaction],
38
+ expectedNewHash,
39
+ sign(account.currentSignerSecret(), expectedNewHash)
40
+ )
41
+ ).toBe(true);
42
+ });
43
+
44
+ test("transactions with wrong signature are rejected", () => {
45
+ const wrongAgent = newRandomAgentSecret();
46
+ const [agentSecret, sessionID] = randomAnonymousAccountAndSessionID();
47
+ const node = new LocalNode(agentSecret, sessionID);
48
+
49
+ const coValue = node.createCoValue({
50
+ type: "costream",
51
+ ruleset: { type: "unsafeAllowAll" },
52
+ meta: null,
53
+ ...createdNowUnique(),
54
+ });
55
+
56
+ const transaction: Transaction = {
57
+ privacy: "trusting",
58
+ madeAt: Date.now(),
59
+ changes: [
60
+ {
61
+ hello: "world",
62
+ },
63
+ ],
64
+ };
65
+
66
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
67
+ node.currentSessionID,
68
+ [transaction]
69
+ );
70
+
71
+ expect(
72
+ coValue.tryAddTransactions(
73
+ node.currentSessionID,
74
+ [transaction],
75
+ expectedNewHash,
76
+ sign(getAgentSignerSecret(wrongAgent), expectedNewHash)
77
+ )
78
+ ).toBe(false);
79
+ });
80
+
81
+ test("transactions with correctly signed, but wrong hash are rejected", () => {
82
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
83
+ const node = new LocalNode(account, sessionID);
84
+
85
+ const coValue = node.createCoValue({
86
+ type: "costream",
87
+ ruleset: { type: "unsafeAllowAll" },
88
+ meta: null,
89
+ ...createdNowUnique(),
90
+ });
91
+
92
+ const transaction: Transaction = {
93
+ privacy: "trusting",
94
+ madeAt: Date.now(),
95
+ changes: [
96
+ {
97
+ hello: "world",
98
+ },
99
+ ],
100
+ };
101
+
102
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
103
+ node.currentSessionID,
104
+ [
105
+ {
106
+ privacy: "trusting",
107
+ madeAt: Date.now(),
108
+ changes: [
109
+ {
110
+ hello: "wrong",
111
+ },
112
+ ],
113
+ },
114
+ ]
115
+ );
116
+
117
+ expect(
118
+ coValue.tryAddTransactions(
119
+ node.currentSessionID,
120
+ [transaction],
121
+ expectedNewHash,
122
+ sign(account.currentSignerSecret(), expectedNewHash)
123
+ )
124
+ ).toBe(false);
125
+ });
126
+
127
+ test("New transactions in a group correctly update owned values, including subscriptions", async () => {
128
+ const [account, sessionID] = randomAnonymousAccountAndSessionID();
129
+ const node = new LocalNode(account, sessionID);
130
+
131
+ const group = node.createGroup();
132
+
133
+ const timeBeforeEdit = Date.now();
134
+
135
+ await new Promise((resolve) => setTimeout(resolve, 10));
136
+
137
+ let map = group.createMap();
138
+
139
+ let mapAfterEdit = map.edit((map) => {
140
+ map.set("hello", "world");
141
+ });
142
+
143
+ const listener = jest.fn().mockImplementation();
144
+
145
+ map.subscribe(listener);
146
+
147
+ expect(listener.mock.calls[0][0].get("hello")).toBe("world");
148
+
149
+ const resignationThatWeJustLearnedAbout = {
150
+ privacy: "trusting",
151
+ madeAt: timeBeforeEdit,
152
+ changes: [
153
+ {
154
+ op: "set",
155
+ key: account.id,
156
+ value: "revoked"
157
+ } satisfies MapOpPayload<typeof account.id, Role>
158
+ ]
159
+ } satisfies Transaction;
160
+
161
+ const { expectedNewHash } = group.underlyingMap.core.expectedNewHashAfter(sessionID, [
162
+ resignationThatWeJustLearnedAbout,
163
+ ]);
164
+
165
+ const signature = sign(
166
+ node.account.currentSignerSecret(),
167
+ expectedNewHash
168
+ );
169
+
170
+ expect(map.core.getValidSortedTransactions().length).toBe(1);
171
+
172
+ const manuallyAdddedTxSuccess = group.underlyingMap.core.tryAddTransactions(node.currentSessionID, [resignationThatWeJustLearnedAbout], expectedNewHash, signature);
173
+
174
+ expect(manuallyAdddedTxSuccess).toBe(true);
175
+
176
+ expect(listener.mock.calls.length).toBe(2);
177
+ expect(listener.mock.calls[1][0].get("hello")).toBe(undefined);
178
+
179
+ expect(map.core.getValidSortedTransactions().length).toBe(0);
180
+ });