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,304 +1,301 @@
1
- import { expect, test, describe } from "vitest";
1
+ import { describe, expect, test } from "vitest";
2
2
  import { expectStream } from "../coValue.js";
3
- import { RawBinaryCoStream } from "../coValues/coStream.js";
4
3
  import { MAX_RECOMMENDED_TX_SIZE } from "../coValueCore.js";
4
+ import { RawBinaryCoStream } from "../coValues/coStream.js";
5
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
5
6
  import { LocalNode } from "../localNode.js";
6
7
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
7
- import { WasmCrypto } from "../crypto/WasmCrypto.js";
8
8
 
9
9
  const Crypto = await WasmCrypto.create();
10
10
 
11
11
  test("Empty CoStream works", () => {
12
- const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
12
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
13
13
 
14
- const coValue = node.createCoValue({
15
- type: "costream",
16
- ruleset: { type: "unsafeAllowAll" },
17
- meta: null,
18
- ...Crypto.createdNowUnique(),
19
- });
14
+ const coValue = node.createCoValue({
15
+ type: "costream",
16
+ ruleset: { type: "unsafeAllowAll" },
17
+ meta: null,
18
+ ...Crypto.createdNowUnique(),
19
+ });
20
20
 
21
- const content = expectStream(coValue.getCurrentContent());
21
+ const content = expectStream(coValue.getCurrentContent());
22
22
 
23
- expect(content.type).toEqual("costream");
24
- expect(content.toJSON()).toEqual({});
25
- expect(content.getSingleStream()).toEqual(undefined);
23
+ expect(content.type).toEqual("costream");
24
+ expect(content.toJSON()).toEqual({});
25
+ expect(content.getSingleStream()).toEqual(undefined);
26
26
  });
27
27
 
28
28
  test("Can push into CoStream", () => {
29
- const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
30
-
31
- const coValue = node.createCoValue({
32
- type: "costream",
33
- ruleset: { type: "unsafeAllowAll" },
34
- meta: null,
35
- ...Crypto.createdNowUnique(),
36
- });
37
-
38
- const content = expectStream(coValue.getCurrentContent());
39
-
40
- content.push({ hello: "world" }, "trusting");
41
- expect(content.toJSON()).toEqual({
42
- [node.currentSessionID]: [{ hello: "world" }],
43
- });
44
- content.push({ foo: "bar" }, "trusting");
45
- expect(content.toJSON()).toEqual({
46
- [node.currentSessionID]: [{ hello: "world" }, { foo: "bar" }],
47
- });
48
- expect(content.getSingleStream()).toEqual([
49
- { hello: "world" },
50
- { foo: "bar" },
51
- ]);
29
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
30
+
31
+ const coValue = node.createCoValue({
32
+ type: "costream",
33
+ ruleset: { type: "unsafeAllowAll" },
34
+ meta: null,
35
+ ...Crypto.createdNowUnique(),
36
+ });
37
+
38
+ const content = expectStream(coValue.getCurrentContent());
39
+
40
+ content.push({ hello: "world" }, "trusting");
41
+ expect(content.toJSON()).toEqual({
42
+ [node.currentSessionID]: [{ hello: "world" }],
43
+ });
44
+ content.push({ foo: "bar" }, "trusting");
45
+ expect(content.toJSON()).toEqual({
46
+ [node.currentSessionID]: [{ hello: "world" }, { foo: "bar" }],
47
+ });
48
+ expect(content.getSingleStream()).toEqual([
49
+ { hello: "world" },
50
+ { foo: "bar" },
51
+ ]);
52
52
  });
53
53
 
54
54
  test("Empty RawBinaryCoStream works", () => {
55
- const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
56
-
57
- const coValue = node.createCoValue({
58
- type: "costream",
59
- ruleset: { type: "unsafeAllowAll" },
60
- meta: { type: "binary" },
61
- ...Crypto.createdNowUnique(),
62
- });
63
-
64
- const content = coValue.getCurrentContent();
65
-
66
- if (
67
- content.type !== "costream" ||
68
- content.headerMeta?.type !== "binary" ||
69
- !(content instanceof RawBinaryCoStream)
70
- ) {
71
- throw new Error("Expected binary stream");
72
- }
73
-
74
- expect(content.type).toEqual("costream");
75
- expect(content.headerMeta.type).toEqual("binary");
76
- expect(content.toJSON()).toEqual({});
77
- expect(content.getBinaryChunks()).toEqual(undefined);
55
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
56
+
57
+ const coValue = node.createCoValue({
58
+ type: "costream",
59
+ ruleset: { type: "unsafeAllowAll" },
60
+ meta: { type: "binary" },
61
+ ...Crypto.createdNowUnique(),
62
+ });
63
+
64
+ const content = coValue.getCurrentContent();
65
+
66
+ if (
67
+ content.type !== "costream" ||
68
+ content.headerMeta?.type !== "binary" ||
69
+ !(content instanceof RawBinaryCoStream)
70
+ ) {
71
+ throw new Error("Expected binary stream");
72
+ }
73
+
74
+ expect(content.type).toEqual("costream");
75
+ expect(content.headerMeta.type).toEqual("binary");
76
+ expect(content.toJSON()).toEqual({});
77
+ expect(content.getBinaryChunks()).toEqual(undefined);
78
78
  });
79
79
 
80
80
  test("Can push into RawBinaryCoStream", () => {
81
- const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
82
-
83
- const coValue = node.createCoValue({
84
- type: "costream",
85
- ruleset: { type: "unsafeAllowAll" },
86
- meta: { type: "binary" },
87
- ...Crypto.createdNowUnique(),
88
- });
89
-
90
- const content = coValue.getCurrentContent();
91
-
92
- if (
93
- content.type !== "costream" ||
94
- content.headerMeta?.type !== "binary" ||
95
- !(content instanceof RawBinaryCoStream)
96
- ) {
97
- throw new Error("Expected binary stream");
98
- }
99
-
100
- content.startBinaryStream(
101
- { mimeType: "text/plain", fileName: "test.txt" },
102
- "trusting",
103
- );
104
- content.pushBinaryStreamChunk(new Uint8Array([1, 2, 3]), "trusting");
105
- content.pushBinaryStreamChunk(new Uint8Array([4, 5, 6]), "trusting");
106
-
107
- content.endBinaryStream("trusting");
108
- expect(content.getBinaryChunks()).toEqual({
109
- mimeType: "text/plain",
110
- fileName: "test.txt",
111
- chunks: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
112
- finished: true,
113
- });
81
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
82
+
83
+ const coValue = node.createCoValue({
84
+ type: "costream",
85
+ ruleset: { type: "unsafeAllowAll" },
86
+ meta: { type: "binary" },
87
+ ...Crypto.createdNowUnique(),
88
+ });
89
+
90
+ const content = coValue.getCurrentContent();
91
+
92
+ if (
93
+ content.type !== "costream" ||
94
+ content.headerMeta?.type !== "binary" ||
95
+ !(content instanceof RawBinaryCoStream)
96
+ ) {
97
+ throw new Error("Expected binary stream");
98
+ }
99
+
100
+ content.startBinaryStream(
101
+ { mimeType: "text/plain", fileName: "test.txt" },
102
+ "trusting",
103
+ );
104
+ content.pushBinaryStreamChunk(new Uint8Array([1, 2, 3]), "trusting");
105
+ content.pushBinaryStreamChunk(new Uint8Array([4, 5, 6]), "trusting");
106
+
107
+ content.endBinaryStream("trusting");
108
+ expect(content.getBinaryChunks()).toEqual({
109
+ mimeType: "text/plain",
110
+ fileName: "test.txt",
111
+ chunks: [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])],
112
+ finished: true,
113
+ });
114
114
  });
115
115
 
116
116
  test("When adding large transactions (small fraction of MAX_RECOMMENDED_TX_SIZE), we store an inbetween signature every time we reach MAX_RECOMMENDED_TX_SIZE and split up newContentSince accordingly", () => {
117
- const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
118
-
119
- const coValue = node.createCoValue({
120
- type: "costream",
121
- ruleset: { type: "unsafeAllowAll" },
122
- meta: { type: "binary" },
123
- ...Crypto.createdNowUnique(),
124
- });
125
-
126
- const content = coValue.getCurrentContent();
127
-
128
- if (
129
- content.type !== "costream" ||
130
- content.headerMeta?.type !== "binary" ||
131
- !(content instanceof RawBinaryCoStream)
132
- ) {
133
- throw new Error("Expected binary stream");
134
- }
135
-
136
- content.startBinaryStream(
137
- { mimeType: "text/plain", fileName: "test.txt" },
138
- "trusting",
139
- );
140
-
141
- for (let i = 0; i < 10; i++) {
142
- const chunk = new Uint8Array(MAX_RECOMMENDED_TX_SIZE / 3 + 100);
143
-
144
- content.pushBinaryStreamChunk(chunk, "trusting");
145
- }
146
-
147
- content.endBinaryStream("trusting");
148
-
149
- const sessionEntry = coValue.sessionLogs.get(node.currentSessionID)!;
150
- expect(sessionEntry.transactions.length).toEqual(12);
151
- expect(sessionEntry.signatureAfter[0]).not.toBeDefined();
152
- expect(sessionEntry.signatureAfter[1]).not.toBeDefined();
153
- expect(sessionEntry.signatureAfter[2]).not.toBeDefined();
154
- expect(sessionEntry.signatureAfter[3]).toBeDefined();
155
- expect(sessionEntry.signatureAfter[4]).not.toBeDefined();
156
- expect(sessionEntry.signatureAfter[5]).not.toBeDefined();
157
- expect(sessionEntry.signatureAfter[6]).toBeDefined();
158
- expect(sessionEntry.signatureAfter[7]).not.toBeDefined();
159
- expect(sessionEntry.signatureAfter[8]).not.toBeDefined();
160
- expect(sessionEntry.signatureAfter[9]).toBeDefined();
161
- expect(sessionEntry.signatureAfter[10]).not.toBeDefined();
162
- expect(sessionEntry.signatureAfter[11]).not.toBeDefined();
163
-
164
- const newContent = coValue.newContentSince({
165
- id: coValue.id,
166
- header: false,
167
- sessions: {},
168
- })!;
169
-
170
- expect(newContent.length).toEqual(5);
171
- expect(newContent[0]!.header).toBeDefined();
172
- expect(newContent[1]!.new[node.currentSessionID]!.lastSignature).toEqual(
173
- sessionEntry.signatureAfter[3],
174
- );
175
- expect(newContent[2]!.new[node.currentSessionID]!.lastSignature).toEqual(
176
- sessionEntry.signatureAfter[6],
177
- );
178
- expect(newContent[3]!.new[node.currentSessionID]!.lastSignature).toEqual(
179
- sessionEntry.signatureAfter[9],
180
- );
181
- expect(newContent[4]!.new[node.currentSessionID]!.lastSignature).toEqual(
182
- sessionEntry.lastSignature,
183
- );
117
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
118
+
119
+ const coValue = node.createCoValue({
120
+ type: "costream",
121
+ ruleset: { type: "unsafeAllowAll" },
122
+ meta: { type: "binary" },
123
+ ...Crypto.createdNowUnique(),
124
+ });
125
+
126
+ const content = coValue.getCurrentContent();
127
+
128
+ if (
129
+ content.type !== "costream" ||
130
+ content.headerMeta?.type !== "binary" ||
131
+ !(content instanceof RawBinaryCoStream)
132
+ ) {
133
+ throw new Error("Expected binary stream");
134
+ }
135
+
136
+ content.startBinaryStream(
137
+ { mimeType: "text/plain", fileName: "test.txt" },
138
+ "trusting",
139
+ );
140
+
141
+ for (let i = 0; i < 10; i++) {
142
+ const chunk = new Uint8Array(MAX_RECOMMENDED_TX_SIZE / 3 + 100);
143
+
144
+ content.pushBinaryStreamChunk(chunk, "trusting");
145
+ }
146
+
147
+ content.endBinaryStream("trusting");
148
+
149
+ const sessionEntry = coValue.sessionLogs.get(node.currentSessionID)!;
150
+ expect(sessionEntry.transactions.length).toEqual(12);
151
+ expect(sessionEntry.signatureAfter[0]).not.toBeDefined();
152
+ expect(sessionEntry.signatureAfter[1]).not.toBeDefined();
153
+ expect(sessionEntry.signatureAfter[2]).not.toBeDefined();
154
+ expect(sessionEntry.signatureAfter[3]).toBeDefined();
155
+ expect(sessionEntry.signatureAfter[4]).not.toBeDefined();
156
+ expect(sessionEntry.signatureAfter[5]).not.toBeDefined();
157
+ expect(sessionEntry.signatureAfter[6]).toBeDefined();
158
+ expect(sessionEntry.signatureAfter[7]).not.toBeDefined();
159
+ expect(sessionEntry.signatureAfter[8]).not.toBeDefined();
160
+ expect(sessionEntry.signatureAfter[9]).toBeDefined();
161
+ expect(sessionEntry.signatureAfter[10]).not.toBeDefined();
162
+ expect(sessionEntry.signatureAfter[11]).not.toBeDefined();
163
+
164
+ const newContent = coValue.newContentSince({
165
+ id: coValue.id,
166
+ header: false,
167
+ sessions: {},
168
+ })!;
169
+
170
+ expect(newContent.length).toEqual(5);
171
+ expect(newContent[0]!.header).toBeDefined();
172
+ expect(newContent[1]!.new[node.currentSessionID]!.lastSignature).toEqual(
173
+ sessionEntry.signatureAfter[3],
174
+ );
175
+ expect(newContent[2]!.new[node.currentSessionID]!.lastSignature).toEqual(
176
+ sessionEntry.signatureAfter[6],
177
+ );
178
+ expect(newContent[3]!.new[node.currentSessionID]!.lastSignature).toEqual(
179
+ sessionEntry.signatureAfter[9],
180
+ );
181
+ expect(newContent[4]!.new[node.currentSessionID]!.lastSignature).toEqual(
182
+ sessionEntry.lastSignature,
183
+ );
184
184
  });
185
185
 
186
186
  test("When adding large transactions (bigger than MAX_RECOMMENDED_TX_SIZE), we store an inbetween signature after every large transaction and split up newContentSince accordingly", () => {
187
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
188
+
189
+ const coValue = node.createCoValue({
190
+ type: "costream",
191
+ ruleset: { type: "unsafeAllowAll" },
192
+ meta: { type: "binary" },
193
+ ...Crypto.createdNowUnique(),
194
+ });
195
+
196
+ const content = coValue.getCurrentContent();
197
+
198
+ if (
199
+ content.type !== "costream" ||
200
+ content.headerMeta?.type !== "binary" ||
201
+ !(content instanceof RawBinaryCoStream)
202
+ ) {
203
+ throw new Error("Expected binary stream");
204
+ }
205
+
206
+ content.startBinaryStream(
207
+ { mimeType: "text/plain", fileName: "test.txt" },
208
+ "trusting",
209
+ );
210
+
211
+ const chunk = new Uint8Array(MAX_RECOMMENDED_TX_SIZE + 100);
212
+
213
+ for (let i = 0; i < 3; i++) {
214
+ content.pushBinaryStreamChunk(chunk, "trusting");
215
+ }
216
+
217
+ content.endBinaryStream("trusting");
218
+
219
+ const sessionEntry = coValue.sessionLogs.get(node.currentSessionID)!;
220
+ expect(sessionEntry.transactions.length).toEqual(5);
221
+ expect(sessionEntry.signatureAfter[0]).not.toBeDefined();
222
+ expect(sessionEntry.signatureAfter[1]).toBeDefined();
223
+ expect(sessionEntry.signatureAfter[2]).toBeDefined();
224
+ expect(sessionEntry.signatureAfter[3]).toBeDefined();
225
+ expect(sessionEntry.signatureAfter[4]).not.toBeDefined();
226
+
227
+ const newContent = coValue.newContentSince({
228
+ id: coValue.id,
229
+ header: false,
230
+ sessions: {},
231
+ })!;
232
+
233
+ expect(newContent.length).toEqual(5);
234
+ expect(newContent[0]!.header).toBeDefined();
235
+ expect(newContent[1]!.new[node.currentSessionID]!.lastSignature).toEqual(
236
+ sessionEntry.signatureAfter[1],
237
+ );
238
+ expect(newContent[2]!.new[node.currentSessionID]!.lastSignature).toEqual(
239
+ sessionEntry.signatureAfter[2],
240
+ );
241
+ expect(newContent[3]!.new[node.currentSessionID]!.lastSignature).toEqual(
242
+ sessionEntry.signatureAfter[3],
243
+ );
244
+ expect(newContent[4]!.new[node.currentSessionID]!.lastSignature).toEqual(
245
+ sessionEntry.lastSignature,
246
+ );
247
+ });
248
+
249
+ describe("isBinaryStreamEnded", () => {
250
+ function setup() {
187
251
  const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
188
252
 
189
253
  const coValue = node.createCoValue({
190
- type: "costream",
191
- ruleset: { type: "unsafeAllowAll" },
192
- meta: { type: "binary" },
193
- ...Crypto.createdNowUnique(),
254
+ type: "costream",
255
+ ruleset: { type: "unsafeAllowAll" },
256
+ meta: { type: "binary" },
257
+ ...Crypto.createdNowUnique(),
194
258
  });
195
259
 
196
260
  const content = coValue.getCurrentContent();
197
261
 
198
262
  if (
199
- content.type !== "costream" ||
200
- content.headerMeta?.type !== "binary" ||
201
- !(content instanceof RawBinaryCoStream)
263
+ content.type !== "costream" ||
264
+ content.headerMeta?.type !== "binary" ||
265
+ !(content instanceof RawBinaryCoStream)
202
266
  ) {
203
- throw new Error("Expected binary stream");
267
+ throw new Error("Expected binary stream");
204
268
  }
205
269
 
206
- content.startBinaryStream(
207
- { mimeType: "text/plain", fileName: "test.txt" },
208
- "trusting",
209
- );
210
-
211
- const chunk = new Uint8Array(MAX_RECOMMENDED_TX_SIZE + 100);
270
+ return content;
271
+ }
212
272
 
213
- for (let i = 0; i < 3; i++) {
214
- content.pushBinaryStreamChunk(chunk, "trusting");
215
- }
273
+ test("returns true when the last item is end", () => {
274
+ const stream = setup();
216
275
 
217
- content.endBinaryStream("trusting");
218
-
219
- const sessionEntry = coValue.sessionLogs.get(node.currentSessionID)!;
220
- expect(sessionEntry.transactions.length).toEqual(5);
221
- expect(sessionEntry.signatureAfter[0]).not.toBeDefined();
222
- expect(sessionEntry.signatureAfter[1]).toBeDefined();
223
- expect(sessionEntry.signatureAfter[2]).toBeDefined();
224
- expect(sessionEntry.signatureAfter[3]).toBeDefined();
225
- expect(sessionEntry.signatureAfter[4]).not.toBeDefined();
226
-
227
- const newContent = coValue.newContentSince({
228
- id: coValue.id,
229
- header: false,
230
- sessions: {},
231
- })!;
232
-
233
- expect(newContent.length).toEqual(5);
234
- expect(newContent[0]!.header).toBeDefined();
235
- expect(newContent[1]!.new[node.currentSessionID]!.lastSignature).toEqual(
236
- sessionEntry.signatureAfter[1],
237
- );
238
- expect(newContent[2]!.new[node.currentSessionID]!.lastSignature).toEqual(
239
- sessionEntry.signatureAfter[2],
240
- );
241
- expect(newContent[3]!.new[node.currentSessionID]!.lastSignature).toEqual(
242
- sessionEntry.signatureAfter[3],
276
+ stream.startBinaryStream(
277
+ { mimeType: "text/plain", fileName: "test.txt" },
278
+ "trusting",
243
279
  );
244
- expect(newContent[4]!.new[node.currentSessionID]!.lastSignature).toEqual(
245
- sessionEntry.lastSignature,
246
- );
247
- });
280
+ stream.endBinaryStream("trusting");
248
281
 
249
- describe("isBinaryStreamEnded", () => {
250
- function setup() {
251
- const node = new LocalNode(
252
- ...randomAnonymousAccountAndSessionID(),
253
- Crypto,
254
- );
255
-
256
- const coValue = node.createCoValue({
257
- type: "costream",
258
- ruleset: { type: "unsafeAllowAll" },
259
- meta: { type: "binary" },
260
- ...Crypto.createdNowUnique(),
261
- });
262
-
263
- const content = coValue.getCurrentContent();
264
-
265
- if (
266
- content.type !== "costream" ||
267
- content.headerMeta?.type !== "binary" ||
268
- !(content instanceof RawBinaryCoStream)
269
- ) {
270
- throw new Error("Expected binary stream");
271
- }
272
-
273
- return content;
274
- }
275
-
276
- test("returns true when the last item is end", () => {
277
- const stream = setup();
282
+ expect(stream.isBinaryStreamEnded()).toBe(true);
283
+ });
278
284
 
279
- stream.startBinaryStream(
280
- { mimeType: "text/plain", fileName: "test.txt" },
281
- "trusting",
282
- );
283
- stream.endBinaryStream("trusting");
284
-
285
- expect(stream.isBinaryStreamEnded()).toBe(true);
286
- });
285
+ test("returns false if the stream isn't ended", () => {
286
+ const stream = setup();
287
287
 
288
- test("returns false if the stream isn't ended", () => {
289
- const stream = setup();
290
-
291
- stream.startBinaryStream(
292
- { mimeType: "text/plain", fileName: "test.txt" },
293
- "trusting",
294
- );
288
+ stream.startBinaryStream(
289
+ { mimeType: "text/plain", fileName: "test.txt" },
290
+ "trusting",
291
+ );
295
292
 
296
- expect(stream.isBinaryStreamEnded()).toBe(false);
297
- });
293
+ expect(stream.isBinaryStreamEnded()).toBe(false);
294
+ });
298
295
 
299
- test("returns false if the stream isn't started", () => {
300
- const stream = setup();
296
+ test("returns false if the stream isn't started", () => {
297
+ const stream = setup();
301
298
 
302
- expect(stream.isBinaryStreamEnded()).toBe(false);
303
- });
299
+ expect(stream.isBinaryStreamEnded()).toBe(false);
300
+ });
304
301
  });