cojson 0.19.21 → 0.20.0

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 (254) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +67 -0
  3. package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts +42 -0
  4. package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts.map +1 -0
  5. package/dist/CojsonMessageChannel/CojsonMessageChannel.js +261 -0
  6. package/dist/CojsonMessageChannel/CojsonMessageChannel.js.map +1 -0
  7. package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts +18 -0
  8. package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts.map +1 -0
  9. package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js +37 -0
  10. package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js.map +1 -0
  11. package/dist/CojsonMessageChannel/index.d.ts +3 -0
  12. package/dist/CojsonMessageChannel/index.d.ts.map +1 -0
  13. package/dist/CojsonMessageChannel/index.js +2 -0
  14. package/dist/CojsonMessageChannel/index.js.map +1 -0
  15. package/dist/CojsonMessageChannel/types.d.ts +149 -0
  16. package/dist/CojsonMessageChannel/types.d.ts.map +1 -0
  17. package/dist/CojsonMessageChannel/types.js +36 -0
  18. package/dist/CojsonMessageChannel/types.js.map +1 -0
  19. package/dist/GarbageCollector.d.ts +4 -2
  20. package/dist/GarbageCollector.d.ts.map +1 -1
  21. package/dist/GarbageCollector.js +5 -3
  22. package/dist/GarbageCollector.js.map +1 -1
  23. package/dist/SyncStateManager.d.ts +3 -3
  24. package/dist/SyncStateManager.d.ts.map +1 -1
  25. package/dist/SyncStateManager.js +4 -4
  26. package/dist/SyncStateManager.js.map +1 -1
  27. package/dist/coValueContentMessage.d.ts +0 -2
  28. package/dist/coValueContentMessage.d.ts.map +1 -1
  29. package/dist/coValueContentMessage.js +0 -8
  30. package/dist/coValueContentMessage.js.map +1 -1
  31. package/dist/coValueCore/SessionMap.d.ts +4 -2
  32. package/dist/coValueCore/SessionMap.d.ts.map +1 -1
  33. package/dist/coValueCore/SessionMap.js +30 -0
  34. package/dist/coValueCore/SessionMap.js.map +1 -1
  35. package/dist/coValueCore/coValueCore.d.ts +86 -4
  36. package/dist/coValueCore/coValueCore.d.ts.map +1 -1
  37. package/dist/coValueCore/coValueCore.js +318 -17
  38. package/dist/coValueCore/coValueCore.js.map +1 -1
  39. package/dist/coValueCore/verifiedState.d.ts +6 -1
  40. package/dist/coValueCore/verifiedState.d.ts.map +1 -1
  41. package/dist/coValueCore/verifiedState.js +9 -0
  42. package/dist/coValueCore/verifiedState.js.map +1 -1
  43. package/dist/coValues/coList.d.ts +3 -2
  44. package/dist/coValues/coList.d.ts.map +1 -1
  45. package/dist/coValues/coList.js.map +1 -1
  46. package/dist/coValues/group.d.ts.map +1 -1
  47. package/dist/coValues/group.js +3 -6
  48. package/dist/coValues/group.js.map +1 -1
  49. package/dist/config.d.ts +0 -6
  50. package/dist/config.d.ts.map +1 -1
  51. package/dist/config.js +0 -8
  52. package/dist/config.js.map +1 -1
  53. package/dist/crypto/NapiCrypto.d.ts +1 -2
  54. package/dist/crypto/NapiCrypto.d.ts.map +1 -1
  55. package/dist/crypto/NapiCrypto.js +19 -4
  56. package/dist/crypto/NapiCrypto.js.map +1 -1
  57. package/dist/crypto/RNCrypto.d.ts.map +1 -1
  58. package/dist/crypto/RNCrypto.js +19 -4
  59. package/dist/crypto/RNCrypto.js.map +1 -1
  60. package/dist/crypto/WasmCrypto.d.ts +11 -4
  61. package/dist/crypto/WasmCrypto.d.ts.map +1 -1
  62. package/dist/crypto/WasmCrypto.js +52 -10
  63. package/dist/crypto/WasmCrypto.js.map +1 -1
  64. package/dist/crypto/WasmCryptoEdge.d.ts +1 -0
  65. package/dist/crypto/WasmCryptoEdge.d.ts.map +1 -1
  66. package/dist/crypto/WasmCryptoEdge.js +4 -1
  67. package/dist/crypto/WasmCryptoEdge.js.map +1 -1
  68. package/dist/crypto/crypto.d.ts +3 -3
  69. package/dist/crypto/crypto.d.ts.map +1 -1
  70. package/dist/crypto/crypto.js +6 -1
  71. package/dist/crypto/crypto.js.map +1 -1
  72. package/dist/exports.d.ts +3 -2
  73. package/dist/exports.d.ts.map +1 -1
  74. package/dist/exports.js +3 -1
  75. package/dist/exports.js.map +1 -1
  76. package/dist/ids.d.ts +4 -1
  77. package/dist/ids.d.ts.map +1 -1
  78. package/dist/ids.js +4 -0
  79. package/dist/ids.js.map +1 -1
  80. package/dist/knownState.d.ts +2 -0
  81. package/dist/knownState.d.ts.map +1 -1
  82. package/dist/localNode.d.ts +13 -3
  83. package/dist/localNode.d.ts.map +1 -1
  84. package/dist/localNode.js +17 -2
  85. package/dist/localNode.js.map +1 -1
  86. package/dist/platformUtils.d.ts +3 -0
  87. package/dist/platformUtils.d.ts.map +1 -0
  88. package/dist/platformUtils.js +24 -0
  89. package/dist/platformUtils.js.map +1 -0
  90. package/dist/storage/DeletedCoValuesEraserScheduler.d.ts +30 -0
  91. package/dist/storage/DeletedCoValuesEraserScheduler.d.ts.map +1 -0
  92. package/dist/storage/DeletedCoValuesEraserScheduler.js +84 -0
  93. package/dist/storage/DeletedCoValuesEraserScheduler.js.map +1 -0
  94. package/dist/storage/sqlite/client.d.ts +3 -0
  95. package/dist/storage/sqlite/client.d.ts.map +1 -1
  96. package/dist/storage/sqlite/client.js +44 -0
  97. package/dist/storage/sqlite/client.js.map +1 -1
  98. package/dist/storage/sqlite/sqliteMigrations.d.ts.map +1 -1
  99. package/dist/storage/sqlite/sqliteMigrations.js +7 -0
  100. package/dist/storage/sqlite/sqliteMigrations.js.map +1 -1
  101. package/dist/storage/sqliteAsync/client.d.ts +3 -0
  102. package/dist/storage/sqliteAsync/client.d.ts.map +1 -1
  103. package/dist/storage/sqliteAsync/client.js +42 -0
  104. package/dist/storage/sqliteAsync/client.js.map +1 -1
  105. package/dist/storage/storageAsync.d.ts +15 -3
  106. package/dist/storage/storageAsync.d.ts.map +1 -1
  107. package/dist/storage/storageAsync.js +60 -3
  108. package/dist/storage/storageAsync.js.map +1 -1
  109. package/dist/storage/storageSync.d.ts +14 -3
  110. package/dist/storage/storageSync.d.ts.map +1 -1
  111. package/dist/storage/storageSync.js +54 -3
  112. package/dist/storage/storageSync.js.map +1 -1
  113. package/dist/storage/types.d.ts +64 -0
  114. package/dist/storage/types.d.ts.map +1 -1
  115. package/dist/storage/types.js +12 -1
  116. package/dist/storage/types.js.map +1 -1
  117. package/dist/sync.d.ts +6 -0
  118. package/dist/sync.d.ts.map +1 -1
  119. package/dist/sync.js +69 -15
  120. package/dist/sync.js.map +1 -1
  121. package/dist/tests/CojsonMessageChannel.test.d.ts +2 -0
  122. package/dist/tests/CojsonMessageChannel.test.d.ts.map +1 -0
  123. package/dist/tests/CojsonMessageChannel.test.js +236 -0
  124. package/dist/tests/CojsonMessageChannel.test.js.map +1 -0
  125. package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts +2 -0
  126. package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts.map +1 -0
  127. package/dist/tests/DeletedCoValuesEraserScheduler.test.js +149 -0
  128. package/dist/tests/DeletedCoValuesEraserScheduler.test.js.map +1 -0
  129. package/dist/tests/GarbageCollector.test.js +91 -18
  130. package/dist/tests/GarbageCollector.test.js.map +1 -1
  131. package/dist/tests/StorageApiAsync.test.js +510 -146
  132. package/dist/tests/StorageApiAsync.test.js.map +1 -1
  133. package/dist/tests/StorageApiSync.test.js +531 -130
  134. package/dist/tests/StorageApiSync.test.js.map +1 -1
  135. package/dist/tests/SyncManager.processQueues.test.js +1 -1
  136. package/dist/tests/SyncManager.processQueues.test.js.map +1 -1
  137. package/dist/tests/SyncStateManager.test.js +1 -1
  138. package/dist/tests/SyncStateManager.test.js.map +1 -1
  139. package/dist/tests/WasmCrypto.test.js +6 -3
  140. package/dist/tests/WasmCrypto.test.js.map +1 -1
  141. package/dist/tests/coPlainText.test.js +1 -1
  142. package/dist/tests/coPlainText.test.js.map +1 -1
  143. package/dist/tests/coValueCore.loadFromStorage.test.js +4 -0
  144. package/dist/tests/coValueCore.loadFromStorage.test.js.map +1 -1
  145. package/dist/tests/coValueCore.test.js +34 -13
  146. package/dist/tests/coValueCore.test.js.map +1 -1
  147. package/dist/tests/coreWasm.test.js +127 -4
  148. package/dist/tests/coreWasm.test.js.map +1 -1
  149. package/dist/tests/crypto.test.js +89 -93
  150. package/dist/tests/crypto.test.js.map +1 -1
  151. package/dist/tests/deleteCoValue.test.d.ts +2 -0
  152. package/dist/tests/deleteCoValue.test.d.ts.map +1 -0
  153. package/dist/tests/deleteCoValue.test.js +313 -0
  154. package/dist/tests/deleteCoValue.test.js.map +1 -0
  155. package/dist/tests/group.removeMember.test.js +18 -30
  156. package/dist/tests/group.removeMember.test.js.map +1 -1
  157. package/dist/tests/knownState.lazyLoading.test.js +4 -0
  158. package/dist/tests/knownState.lazyLoading.test.js.map +1 -1
  159. package/dist/tests/sync.deleted.test.d.ts +2 -0
  160. package/dist/tests/sync.deleted.test.d.ts.map +1 -0
  161. package/dist/tests/sync.deleted.test.js +214 -0
  162. package/dist/tests/sync.deleted.test.js.map +1 -0
  163. package/dist/tests/sync.garbageCollection.test.js +56 -32
  164. package/dist/tests/sync.garbageCollection.test.js.map +1 -1
  165. package/dist/tests/sync.load.test.js +3 -5
  166. package/dist/tests/sync.load.test.js.map +1 -1
  167. package/dist/tests/sync.mesh.test.js +4 -3
  168. package/dist/tests/sync.mesh.test.js.map +1 -1
  169. package/dist/tests/sync.peerReconciliation.test.js +3 -3
  170. package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
  171. package/dist/tests/sync.storage.test.js +12 -11
  172. package/dist/tests/sync.storage.test.js.map +1 -1
  173. package/dist/tests/sync.storageAsync.test.js +7 -7
  174. package/dist/tests/sync.storageAsync.test.js.map +1 -1
  175. package/dist/tests/sync.test.js +3 -2
  176. package/dist/tests/sync.test.js.map +1 -1
  177. package/dist/tests/sync.tracking.test.js +35 -4
  178. package/dist/tests/sync.tracking.test.js.map +1 -1
  179. package/dist/tests/testStorage.d.ts +3 -0
  180. package/dist/tests/testStorage.d.ts.map +1 -1
  181. package/dist/tests/testStorage.js +16 -2
  182. package/dist/tests/testStorage.js.map +1 -1
  183. package/dist/tests/testUtils.d.ts +29 -4
  184. package/dist/tests/testUtils.d.ts.map +1 -1
  185. package/dist/tests/testUtils.js +84 -9
  186. package/dist/tests/testUtils.js.map +1 -1
  187. package/package.json +6 -16
  188. package/src/CojsonMessageChannel/CojsonMessageChannel.ts +332 -0
  189. package/src/CojsonMessageChannel/MessagePortOutgoingChannel.ts +52 -0
  190. package/src/CojsonMessageChannel/index.ts +9 -0
  191. package/src/CojsonMessageChannel/types.ts +200 -0
  192. package/src/GarbageCollector.ts +5 -5
  193. package/src/SyncStateManager.ts +6 -6
  194. package/src/coValueContentMessage.ts +0 -14
  195. package/src/coValueCore/SessionMap.ts +43 -1
  196. package/src/coValueCore/coValueCore.ts +430 -15
  197. package/src/coValueCore/verifiedState.ts +26 -3
  198. package/src/coValues/coList.ts +5 -3
  199. package/src/coValues/group.ts +5 -6
  200. package/src/config.ts +0 -9
  201. package/src/crypto/NapiCrypto.ts +29 -13
  202. package/src/crypto/RNCrypto.ts +29 -11
  203. package/src/crypto/WasmCrypto.ts +67 -20
  204. package/src/crypto/WasmCryptoEdge.ts +5 -1
  205. package/src/crypto/crypto.ts +16 -4
  206. package/src/exports.ts +3 -0
  207. package/src/ids.ts +11 -1
  208. package/src/localNode.ts +18 -5
  209. package/src/platformUtils.ts +26 -0
  210. package/src/storage/DeletedCoValuesEraserScheduler.ts +124 -0
  211. package/src/storage/sqlite/client.ts +77 -0
  212. package/src/storage/sqlite/sqliteMigrations.ts +7 -0
  213. package/src/storage/sqliteAsync/client.ts +75 -0
  214. package/src/storage/storageAsync.ts +77 -4
  215. package/src/storage/storageSync.ts +73 -4
  216. package/src/storage/types.ts +75 -0
  217. package/src/sync.ts +84 -15
  218. package/src/tests/CojsonMessageChannel.test.ts +306 -0
  219. package/src/tests/DeletedCoValuesEraserScheduler.test.ts +185 -0
  220. package/src/tests/GarbageCollector.test.ts +119 -22
  221. package/src/tests/StorageApiAsync.test.ts +615 -156
  222. package/src/tests/StorageApiSync.test.ts +623 -137
  223. package/src/tests/SyncManager.processQueues.test.ts +1 -1
  224. package/src/tests/SyncStateManager.test.ts +1 -1
  225. package/src/tests/WasmCrypto.test.ts +8 -3
  226. package/src/tests/coPlainText.test.ts +1 -1
  227. package/src/tests/coValueCore.loadFromStorage.test.ts +8 -0
  228. package/src/tests/coValueCore.test.ts +49 -14
  229. package/src/tests/coreWasm.test.ts +319 -10
  230. package/src/tests/crypto.test.ts +141 -150
  231. package/src/tests/deleteCoValue.test.ts +528 -0
  232. package/src/tests/group.removeMember.test.ts +35 -35
  233. package/src/tests/knownState.lazyLoading.test.ts +8 -0
  234. package/src/tests/sync.deleted.test.ts +294 -0
  235. package/src/tests/sync.garbageCollection.test.ts +69 -36
  236. package/src/tests/sync.load.test.ts +3 -5
  237. package/src/tests/sync.mesh.test.ts +6 -3
  238. package/src/tests/sync.peerReconciliation.test.ts +3 -3
  239. package/src/tests/sync.storage.test.ts +14 -11
  240. package/src/tests/sync.storageAsync.test.ts +7 -7
  241. package/src/tests/sync.test.ts +5 -2
  242. package/src/tests/sync.tracking.test.ts +54 -4
  243. package/src/tests/testStorage.ts +30 -3
  244. package/src/tests/testUtils.ts +113 -15
  245. package/dist/crypto/PureJSCrypto.d.ts +0 -77
  246. package/dist/crypto/PureJSCrypto.d.ts.map +0 -1
  247. package/dist/crypto/PureJSCrypto.js +0 -236
  248. package/dist/crypto/PureJSCrypto.js.map +0 -1
  249. package/dist/tests/PureJSCrypto.test.d.ts +0 -2
  250. package/dist/tests/PureJSCrypto.test.d.ts.map +0 -1
  251. package/dist/tests/PureJSCrypto.test.js +0 -145
  252. package/dist/tests/PureJSCrypto.test.js.map +0 -1
  253. package/src/crypto/PureJSCrypto.ts +0 -429
  254. package/src/tests/PureJSCrypto.test.ts +0 -217
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Type definitions for JazzMessageChannel
3
+ *
4
+ * These types support cross-context communication via MessageChannel API
5
+ * and compatible implementations (e.g., Electron's MessageChannelMain).
6
+ */
7
+
8
+ /**
9
+ * Duck-typed interface for any object that can receive messages via postMessage.
10
+ *
11
+ * Covers:
12
+ * - Worker
13
+ * - Window (including iframes via contentWindow)
14
+ * - MessagePort
15
+ * - ServiceWorker
16
+ * - Client (Service Worker clients)
17
+ * - Electron's WebContents (renderer windows)
18
+ */
19
+ export interface PostMessageTarget {
20
+ postMessage(message: unknown, transfer?: MessagePortLike[]): void;
21
+ postMessage(
22
+ message: unknown,
23
+ targetOrigin: string,
24
+ transfer?: MessagePortLike[],
25
+ ): void;
26
+ }
27
+
28
+ /**
29
+ * MessagePort-like interface that covers browser MessagePort and Electron MessagePortMain.
30
+ *
31
+ * Note: Electron's MessagePortMain does not have a start() method,
32
+ * so it's optional here.
33
+ */
34
+ export interface MessagePortLike {
35
+ postMessage(message: unknown): void;
36
+ addEventListener(
37
+ type: "message" | "messageerror",
38
+ listener: (event: unknown) => void,
39
+ ): void;
40
+ removeEventListener(
41
+ type: "message" | "messageerror",
42
+ listener: (event: unknown) => void,
43
+ ): void;
44
+ addEventListener(event: "close", listener: () => void): void;
45
+ removeEventListener(type: "close", listener: () => void): void;
46
+ /** Optional - not present on Electron's MessagePortMain */
47
+ start?(): void;
48
+ close(): void;
49
+ }
50
+
51
+ /**
52
+ * MessageChannel-like interface that covers browser MessageChannel
53
+ * and Electron MessageChannelMain.
54
+ */
55
+ export interface MessageChannelLike {
56
+ port1: MessagePortLike;
57
+ port2: MessagePortLike;
58
+ }
59
+
60
+ /**
61
+ * Options for JazzMessageChannel.expose()
62
+ */
63
+ export interface ExposeOptions {
64
+ /**
65
+ * Unique identifier for the peer connection, sent to the guest during handshake.
66
+ * Both sides will use this same ID for the Peer object.
67
+ * If not provided, a unique ID will be generated using `channel_${Math.random()}`.
68
+ */
69
+ id?: string;
70
+
71
+ /** Role of the peer in the sync topology */
72
+ role?: "client" | "server";
73
+
74
+ /** Target origin for Window targets (default: "*") */
75
+ targetOrigin?: string;
76
+
77
+ /**
78
+ * A pre-created MessageChannel to use instead of creating a new one.
79
+ * Use this for environments where the global MessageChannel is not available,
80
+ * e.g., Electron main process with MessageChannelMain.
81
+ * If not provided, a new MessageChannel will be created.
82
+ */
83
+ messageChannel?: MessageChannelLike;
84
+
85
+ /** Callback when the connection closes */
86
+ onClose?: () => void;
87
+ }
88
+
89
+ /**
90
+ * Options for CojsonMessageChannel.waitForConnection()
91
+ */
92
+ export interface WaitForConnectionOptions {
93
+ /**
94
+ * Expected peer ID to accept.
95
+ * If provided, only handshakes with matching id will be accepted; others are ignored.
96
+ * If not provided, any connection will be accepted.
97
+ */
98
+ id?: string;
99
+
100
+ /** Role of the peer in the sync topology */
101
+ role?: "client" | "server";
102
+
103
+ /** Allowed origins for Window contexts (default: ["*"]) */
104
+ allowedOrigins?: string[];
105
+
106
+ /** Callback when the connection closes */
107
+ onClose?: () => void;
108
+ }
109
+
110
+ /**
111
+ * Options for CojsonMessageChannel.acceptFromPort()
112
+ * Note: No timeout option - acceptFromPort waits indefinitely.
113
+ */
114
+ export interface AcceptFromPortOptions {
115
+ /**
116
+ * Expected peer ID to accept.
117
+ * If provided, only handshakes with matching id will be accepted; others are ignored.
118
+ * If not provided, any connection will be accepted.
119
+ */
120
+ id?: string;
121
+
122
+ /** Role of the peer in the sync topology */
123
+ role?: "client" | "server";
124
+
125
+ /** Callback when the connection closes */
126
+ onClose?: () => void;
127
+ }
128
+
129
+ /**
130
+ * Port transfer message sent via target.postMessage.
131
+ * The actual port is transferred via Transferable.
132
+ */
133
+ export interface PortTransferMessage {
134
+ type: "jazz:port";
135
+ /** The peer ID for this connection */
136
+ id: string;
137
+ }
138
+
139
+ /**
140
+ * Ready signal sent from host to guest via MessagePort.
141
+ * Contains the peer ID that both sides will use.
142
+ */
143
+ export interface ReadyMessage {
144
+ type: "jazz:ready";
145
+ /** The peer ID to use on both sides */
146
+ id: string;
147
+ }
148
+
149
+ /**
150
+ * Acknowledgment sent from guest to host via MessagePort.
151
+ * No id needed - guest uses the id from the host's ReadyMessage.
152
+ */
153
+ export interface ReadyAckMessage {
154
+ type: "jazz:ready";
155
+ }
156
+
157
+ /**
158
+ * Union of all control messages used in the handshake protocol.
159
+ */
160
+ export type ControlMessage =
161
+ | PortTransferMessage
162
+ | ReadyMessage
163
+ | ReadyAckMessage;
164
+
165
+ /**
166
+ * Type guard to check if a message is a Jazz control message.
167
+ * Control messages are identified by having a "type" property starting with "jazz:".
168
+ */
169
+ export function isControlMessage(msg: unknown): msg is ControlMessage {
170
+ return (
171
+ typeof msg === "object" &&
172
+ msg !== null &&
173
+ "type" in msg &&
174
+ typeof msg.type === "string" &&
175
+ msg.type.startsWith("jazz:")
176
+ );
177
+ }
178
+
179
+ /**
180
+ * Type guard to check if a message is a PortTransferMessage.
181
+ */
182
+ export function isPortTransferMessage(
183
+ msg: unknown,
184
+ ): msg is PortTransferMessage {
185
+ return isControlMessage(msg) && msg.type === "jazz:port";
186
+ }
187
+
188
+ /**
189
+ * Type guard to check if a message is a ReadyMessage (with id).
190
+ */
191
+ export function isReadyMessage(msg: unknown): msg is ReadyMessage {
192
+ return isControlMessage(msg) && msg.type === "jazz:ready" && "id" in msg;
193
+ }
194
+
195
+ /**
196
+ * Type guard to check if a message is a ReadyAckMessage (without id).
197
+ */
198
+ export function isReadyAckMessage(msg: unknown): msg is ReadyAckMessage {
199
+ return isControlMessage(msg) && msg.type === "jazz:ready" && !("id" in msg);
200
+ }
@@ -2,13 +2,13 @@ import { CoValueCore } from "./coValueCore/coValueCore.js";
2
2
  import { GARBAGE_COLLECTOR_CONFIG } from "./config.js";
3
3
  import { RawCoID } from "./ids.js";
4
4
 
5
+ /**
6
+ * TTL-based garbage collector for removing unused CoValues from memory.
7
+ */
5
8
  export class GarbageCollector {
6
9
  private readonly interval: ReturnType<typeof setInterval>;
7
10
 
8
- constructor(
9
- private readonly coValues: Map<RawCoID, CoValueCore>,
10
- private readonly garbageCollectGroups: boolean,
11
- ) {
11
+ constructor(private readonly coValues: Map<RawCoID, CoValueCore>) {
12
12
  this.interval = setInterval(() => {
13
13
  this.collect();
14
14
  }, GARBAGE_COLLECTOR_CONFIG.INTERVAL);
@@ -36,7 +36,7 @@ export class GarbageCollector {
36
36
  const timeSinceLastAccessed = currentTime - verified.lastAccessed;
37
37
 
38
38
  if (timeSinceLastAccessed > GARBAGE_COLLECTOR_CONFIG.MAX_AGE) {
39
- coValue.unmount(this.garbageCollectGroups);
39
+ coValue.unmount();
40
40
  }
41
41
  }
42
42
  }
@@ -4,14 +4,14 @@ import {
4
4
  areCurrentSessionsInSyncWith,
5
5
  } from "./knownState.js";
6
6
  import { PeerState } from "./PeerState.js";
7
- import { PeerID, SyncManager } from "./sync.js";
7
+ import { Peer, PeerID, SyncManager } from "./sync.js";
8
8
 
9
9
  export type SyncState = {
10
10
  uploaded: boolean;
11
11
  };
12
12
 
13
13
  export type GlobalSyncStateListenerCallback = (
14
- peerId: PeerID,
14
+ peer: Peer,
15
15
  knownState: CoValueKnownState,
16
16
  sync: SyncState,
17
17
  ) => void;
@@ -93,10 +93,10 @@ export class SyncStateManager {
93
93
  };
94
94
  }
95
95
 
96
- triggerUpdate(peerId: PeerID, id: RawCoID, knownState: CoValueKnownState) {
96
+ triggerUpdate(peer: Peer, id: RawCoID, knownState: CoValueKnownState) {
97
97
  const globalListeners = this.listeners;
98
98
  const coValueListeners = this.listenersByCoValues.get(id);
99
- const peerMap = this.listenersByPeersAndCoValues.get(peerId);
99
+ const peerMap = this.listenersByPeersAndCoValues.get(peer.id);
100
100
  const coValueAndPeerListeners = peerMap?.get(id);
101
101
 
102
102
  if (
@@ -113,12 +113,12 @@ export class SyncStateManager {
113
113
  };
114
114
 
115
115
  for (const listener of this.listeners) {
116
- listener(peerId, knownState, syncState);
116
+ listener(peer, knownState, syncState);
117
117
  }
118
118
 
119
119
  if (coValueListeners) {
120
120
  for (const listener of coValueListeners) {
121
- listener(peerId, knownState, syncState);
121
+ listener(peer, knownState, syncState);
122
122
  }
123
123
  }
124
124
 
@@ -61,20 +61,6 @@ export function exceedsRecommendedSize(
61
61
  );
62
62
  }
63
63
 
64
- const cachedTextEncoder = new TextEncoder();
65
-
66
- export function validateTxSizeLimitInBytes(changes: JsonValue[]): void {
67
- const serializedSize = cachedTextEncoder.encode(
68
- JSON.stringify(changes),
69
- ).length;
70
- if (serializedSize > TRANSACTION_CONFIG.MAX_TX_SIZE_BYTES) {
71
- throw new Error(
72
- `Transaction is too large to be synced: ${serializedSize} > ${TRANSACTION_CONFIG.MAX_TX_SIZE_BYTES} bytes. ` +
73
- `Consider breaking your transaction into smaller chunks.`,
74
- );
75
- }
76
- }
77
-
78
64
  export function knownStateFromContent(content: NewContentMessage) {
79
65
  const knownState = emptyKnownState(content.id);
80
66
  knownState.header = Boolean(content.header);
@@ -7,7 +7,7 @@ import type {
7
7
  Signature,
8
8
  SignerID,
9
9
  } from "../crypto/crypto.js";
10
- import { RawCoID, SessionID } from "../ids.js";
10
+ import { isDeleteSessionID, RawCoID, SessionID } from "../ids.js";
11
11
  import { parseJSON, Stringified } from "../jsonStringify.js";
12
12
  import { JsonObject, JsonValue } from "../jsonValue.js";
13
13
  import { TryAddTransactionsError } from "./coValueCore.js";
@@ -34,6 +34,7 @@ export type SessionLog = {
34
34
  };
35
35
 
36
36
  export class SessionMap {
37
+ private isDeleted: boolean = false;
37
38
  sessions: Map<SessionID, SessionLog> = new Map();
38
39
 
39
40
  // Known state related properies, mutated when adding transactions to the session map
@@ -60,7 +61,32 @@ export class SessionMap {
60
61
  }
61
62
  }
62
63
 
64
+ markAsDeleted() {
65
+ this.isDeleted = true;
66
+
67
+ // We reset the known state to report only the deleted session/transaction
68
+ this.knownState = { id: this.id, header: true, sessions: {} };
69
+
70
+ // We remove the streaming statuses, because once deleted we don't need
71
+ // to wait for any streaming to be completed
72
+ this.knownStateWithStreaming = undefined;
73
+ this.streamingKnownState = undefined;
74
+ this.invalidateKnownStateCache();
75
+
76
+ for (const [sessionID, sessionLog] of this.sessions.entries()) {
77
+ if (!isDeleteSessionID(sessionID)) {
78
+ continue;
79
+ }
80
+
81
+ this.knownState.sessions[sessionID] = sessionLog.transactions.length;
82
+ }
83
+ }
84
+
63
85
  setStreamingKnownState(streamingKnownState: KnownStateSessions) {
86
+ if (this.isDeleted) {
87
+ return;
88
+ }
89
+
64
90
  // if the streaming known state is a subset of the current known state, we can skip the update
65
91
  if (isKnownStateSubsetOf(streamingKnownState, this.knownState.sessions)) {
66
92
  return;
@@ -148,6 +174,10 @@ export class SessionMap {
148
174
  newSignature: Signature,
149
175
  skipVerify: boolean = false,
150
176
  ) {
177
+ if (this.isDeleted && !isDeleteSessionID(sessionID)) {
178
+ throw new Error("Cannot add transactions to a deleted coValue");
179
+ }
180
+
151
181
  const sessionLog = this.getOrCreateSessionLog(sessionID, signerID);
152
182
 
153
183
  sessionLog.impl.tryAdd(newTransactions, newSignature, skipVerify);
@@ -164,6 +194,12 @@ export class SessionMap {
164
194
  meta: JsonObject | undefined,
165
195
  madeAt: number,
166
196
  ): { signature: Signature; transaction: Transaction } {
197
+ if (this.isDeleted) {
198
+ throw new Error(
199
+ "Cannot make new private transaction on a deleted coValue",
200
+ );
201
+ }
202
+
167
203
  const sessionLog = this.getOrCreateSessionLog(
168
204
  sessionID,
169
205
  signerAgent.currentSignerID(),
@@ -194,6 +230,12 @@ export class SessionMap {
194
230
  meta: JsonObject | undefined,
195
231
  madeAt: number,
196
232
  ): { signature: Signature; transaction: Transaction } {
233
+ if (this.isDeleted) {
234
+ throw new Error(
235
+ "Cannot make new trusting transaction on a deleted coValue",
236
+ );
237
+ }
238
+
197
239
  const sessionLog = this.getOrCreateSessionLog(
198
240
  sessionID,
199
241
  signerAgent.currentSignerID(),