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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +67 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts +42 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.js +261 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.js.map +1 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts +18 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js +37 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js.map +1 -0
- package/dist/CojsonMessageChannel/index.d.ts +3 -0
- package/dist/CojsonMessageChannel/index.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/index.js +2 -0
- package/dist/CojsonMessageChannel/index.js.map +1 -0
- package/dist/CojsonMessageChannel/types.d.ts +149 -0
- package/dist/CojsonMessageChannel/types.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/types.js +36 -0
- package/dist/CojsonMessageChannel/types.js.map +1 -0
- package/dist/GarbageCollector.d.ts +4 -2
- package/dist/GarbageCollector.d.ts.map +1 -1
- package/dist/GarbageCollector.js +5 -3
- package/dist/GarbageCollector.js.map +1 -1
- package/dist/SyncStateManager.d.ts +3 -3
- package/dist/SyncStateManager.d.ts.map +1 -1
- package/dist/SyncStateManager.js +4 -4
- package/dist/SyncStateManager.js.map +1 -1
- package/dist/coValueContentMessage.d.ts +0 -2
- package/dist/coValueContentMessage.d.ts.map +1 -1
- package/dist/coValueContentMessage.js +0 -8
- package/dist/coValueContentMessage.js.map +1 -1
- package/dist/coValueCore/SessionMap.d.ts +4 -2
- package/dist/coValueCore/SessionMap.d.ts.map +1 -1
- package/dist/coValueCore/SessionMap.js +30 -0
- package/dist/coValueCore/SessionMap.js.map +1 -1
- package/dist/coValueCore/coValueCore.d.ts +86 -4
- package/dist/coValueCore/coValueCore.d.ts.map +1 -1
- package/dist/coValueCore/coValueCore.js +318 -17
- package/dist/coValueCore/coValueCore.js.map +1 -1
- package/dist/coValueCore/verifiedState.d.ts +6 -1
- package/dist/coValueCore/verifiedState.d.ts.map +1 -1
- package/dist/coValueCore/verifiedState.js +9 -0
- package/dist/coValueCore/verifiedState.js.map +1 -1
- package/dist/coValues/coList.d.ts +3 -2
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/group.js +3 -6
- package/dist/coValues/group.js.map +1 -1
- package/dist/config.d.ts +0 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +0 -8
- package/dist/config.js.map +1 -1
- package/dist/crypto/NapiCrypto.d.ts +1 -2
- package/dist/crypto/NapiCrypto.d.ts.map +1 -1
- package/dist/crypto/NapiCrypto.js +19 -4
- package/dist/crypto/NapiCrypto.js.map +1 -1
- package/dist/crypto/RNCrypto.d.ts.map +1 -1
- package/dist/crypto/RNCrypto.js +19 -4
- package/dist/crypto/RNCrypto.js.map +1 -1
- package/dist/crypto/WasmCrypto.d.ts +11 -4
- package/dist/crypto/WasmCrypto.d.ts.map +1 -1
- package/dist/crypto/WasmCrypto.js +52 -10
- package/dist/crypto/WasmCrypto.js.map +1 -1
- package/dist/crypto/WasmCryptoEdge.d.ts +1 -0
- package/dist/crypto/WasmCryptoEdge.d.ts.map +1 -1
- package/dist/crypto/WasmCryptoEdge.js +4 -1
- package/dist/crypto/WasmCryptoEdge.js.map +1 -1
- package/dist/crypto/crypto.d.ts +3 -3
- package/dist/crypto/crypto.d.ts.map +1 -1
- package/dist/crypto/crypto.js +6 -1
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/exports.d.ts +3 -2
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +3 -1
- package/dist/exports.js.map +1 -1
- package/dist/ids.d.ts +4 -1
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +4 -0
- package/dist/ids.js.map +1 -1
- package/dist/knownState.d.ts +2 -0
- package/dist/knownState.d.ts.map +1 -1
- package/dist/localNode.d.ts +13 -3
- package/dist/localNode.d.ts.map +1 -1
- package/dist/localNode.js +17 -2
- package/dist/localNode.js.map +1 -1
- package/dist/platformUtils.d.ts +3 -0
- package/dist/platformUtils.d.ts.map +1 -0
- package/dist/platformUtils.js +24 -0
- package/dist/platformUtils.js.map +1 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.d.ts +30 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.d.ts.map +1 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.js +84 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.js.map +1 -0
- package/dist/storage/sqlite/client.d.ts +3 -0
- package/dist/storage/sqlite/client.d.ts.map +1 -1
- package/dist/storage/sqlite/client.js +44 -0
- package/dist/storage/sqlite/client.js.map +1 -1
- package/dist/storage/sqlite/sqliteMigrations.d.ts.map +1 -1
- package/dist/storage/sqlite/sqliteMigrations.js +7 -0
- package/dist/storage/sqlite/sqliteMigrations.js.map +1 -1
- package/dist/storage/sqliteAsync/client.d.ts +3 -0
- package/dist/storage/sqliteAsync/client.d.ts.map +1 -1
- package/dist/storage/sqliteAsync/client.js +42 -0
- package/dist/storage/sqliteAsync/client.js.map +1 -1
- package/dist/storage/storageAsync.d.ts +15 -3
- package/dist/storage/storageAsync.d.ts.map +1 -1
- package/dist/storage/storageAsync.js +60 -3
- package/dist/storage/storageAsync.js.map +1 -1
- package/dist/storage/storageSync.d.ts +14 -3
- package/dist/storage/storageSync.d.ts.map +1 -1
- package/dist/storage/storageSync.js +54 -3
- package/dist/storage/storageSync.js.map +1 -1
- package/dist/storage/types.d.ts +64 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/types.js +12 -1
- package/dist/storage/types.js.map +1 -1
- package/dist/sync.d.ts +6 -0
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +69 -15
- package/dist/sync.js.map +1 -1
- package/dist/tests/CojsonMessageChannel.test.d.ts +2 -0
- package/dist/tests/CojsonMessageChannel.test.d.ts.map +1 -0
- package/dist/tests/CojsonMessageChannel.test.js +236 -0
- package/dist/tests/CojsonMessageChannel.test.js.map +1 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts +2 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts.map +1 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.js +149 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.js.map +1 -0
- package/dist/tests/GarbageCollector.test.js +91 -18
- package/dist/tests/GarbageCollector.test.js.map +1 -1
- package/dist/tests/StorageApiAsync.test.js +510 -146
- package/dist/tests/StorageApiAsync.test.js.map +1 -1
- package/dist/tests/StorageApiSync.test.js +531 -130
- package/dist/tests/StorageApiSync.test.js.map +1 -1
- package/dist/tests/SyncManager.processQueues.test.js +1 -1
- package/dist/tests/SyncManager.processQueues.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +1 -1
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/WasmCrypto.test.js +6 -3
- package/dist/tests/WasmCrypto.test.js.map +1 -1
- package/dist/tests/coPlainText.test.js +1 -1
- package/dist/tests/coPlainText.test.js.map +1 -1
- package/dist/tests/coValueCore.loadFromStorage.test.js +4 -0
- package/dist/tests/coValueCore.loadFromStorage.test.js.map +1 -1
- package/dist/tests/coValueCore.test.js +34 -13
- package/dist/tests/coValueCore.test.js.map +1 -1
- package/dist/tests/coreWasm.test.js +127 -4
- package/dist/tests/coreWasm.test.js.map +1 -1
- package/dist/tests/crypto.test.js +89 -93
- package/dist/tests/crypto.test.js.map +1 -1
- package/dist/tests/deleteCoValue.test.d.ts +2 -0
- package/dist/tests/deleteCoValue.test.d.ts.map +1 -0
- package/dist/tests/deleteCoValue.test.js +313 -0
- package/dist/tests/deleteCoValue.test.js.map +1 -0
- package/dist/tests/group.removeMember.test.js +18 -30
- package/dist/tests/group.removeMember.test.js.map +1 -1
- package/dist/tests/knownState.lazyLoading.test.js +4 -0
- package/dist/tests/knownState.lazyLoading.test.js.map +1 -1
- package/dist/tests/sync.deleted.test.d.ts +2 -0
- package/dist/tests/sync.deleted.test.d.ts.map +1 -0
- package/dist/tests/sync.deleted.test.js +214 -0
- package/dist/tests/sync.deleted.test.js.map +1 -0
- package/dist/tests/sync.garbageCollection.test.js +56 -32
- package/dist/tests/sync.garbageCollection.test.js.map +1 -1
- package/dist/tests/sync.load.test.js +3 -5
- package/dist/tests/sync.load.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +4 -3
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.peerReconciliation.test.js +3 -3
- package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js +12 -11
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.storageAsync.test.js +7 -7
- package/dist/tests/sync.storageAsync.test.js.map +1 -1
- package/dist/tests/sync.test.js +3 -2
- package/dist/tests/sync.test.js.map +1 -1
- package/dist/tests/sync.tracking.test.js +35 -4
- package/dist/tests/sync.tracking.test.js.map +1 -1
- package/dist/tests/testStorage.d.ts +3 -0
- package/dist/tests/testStorage.d.ts.map +1 -1
- package/dist/tests/testStorage.js +16 -2
- package/dist/tests/testStorage.js.map +1 -1
- package/dist/tests/testUtils.d.ts +29 -4
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +84 -9
- package/dist/tests/testUtils.js.map +1 -1
- package/package.json +6 -16
- package/src/CojsonMessageChannel/CojsonMessageChannel.ts +332 -0
- package/src/CojsonMessageChannel/MessagePortOutgoingChannel.ts +52 -0
- package/src/CojsonMessageChannel/index.ts +9 -0
- package/src/CojsonMessageChannel/types.ts +200 -0
- package/src/GarbageCollector.ts +5 -5
- package/src/SyncStateManager.ts +6 -6
- package/src/coValueContentMessage.ts +0 -14
- package/src/coValueCore/SessionMap.ts +43 -1
- package/src/coValueCore/coValueCore.ts +430 -15
- package/src/coValueCore/verifiedState.ts +26 -3
- package/src/coValues/coList.ts +5 -3
- package/src/coValues/group.ts +5 -6
- package/src/config.ts +0 -9
- package/src/crypto/NapiCrypto.ts +29 -13
- package/src/crypto/RNCrypto.ts +29 -11
- package/src/crypto/WasmCrypto.ts +67 -20
- package/src/crypto/WasmCryptoEdge.ts +5 -1
- package/src/crypto/crypto.ts +16 -4
- package/src/exports.ts +3 -0
- package/src/ids.ts +11 -1
- package/src/localNode.ts +18 -5
- package/src/platformUtils.ts +26 -0
- package/src/storage/DeletedCoValuesEraserScheduler.ts +124 -0
- package/src/storage/sqlite/client.ts +77 -0
- package/src/storage/sqlite/sqliteMigrations.ts +7 -0
- package/src/storage/sqliteAsync/client.ts +75 -0
- package/src/storage/storageAsync.ts +77 -4
- package/src/storage/storageSync.ts +73 -4
- package/src/storage/types.ts +75 -0
- package/src/sync.ts +84 -15
- package/src/tests/CojsonMessageChannel.test.ts +306 -0
- package/src/tests/DeletedCoValuesEraserScheduler.test.ts +185 -0
- package/src/tests/GarbageCollector.test.ts +119 -22
- package/src/tests/StorageApiAsync.test.ts +615 -156
- package/src/tests/StorageApiSync.test.ts +623 -137
- package/src/tests/SyncManager.processQueues.test.ts +1 -1
- package/src/tests/SyncStateManager.test.ts +1 -1
- package/src/tests/WasmCrypto.test.ts +8 -3
- package/src/tests/coPlainText.test.ts +1 -1
- package/src/tests/coValueCore.loadFromStorage.test.ts +8 -0
- package/src/tests/coValueCore.test.ts +49 -14
- package/src/tests/coreWasm.test.ts +319 -10
- package/src/tests/crypto.test.ts +141 -150
- package/src/tests/deleteCoValue.test.ts +528 -0
- package/src/tests/group.removeMember.test.ts +35 -35
- package/src/tests/knownState.lazyLoading.test.ts +8 -0
- package/src/tests/sync.deleted.test.ts +294 -0
- package/src/tests/sync.garbageCollection.test.ts +69 -36
- package/src/tests/sync.load.test.ts +3 -5
- package/src/tests/sync.mesh.test.ts +6 -3
- package/src/tests/sync.peerReconciliation.test.ts +3 -3
- package/src/tests/sync.storage.test.ts +14 -11
- package/src/tests/sync.storageAsync.test.ts +7 -7
- package/src/tests/sync.test.ts +5 -2
- package/src/tests/sync.tracking.test.ts +54 -4
- package/src/tests/testStorage.ts +30 -3
- package/src/tests/testUtils.ts +113 -15
- package/dist/crypto/PureJSCrypto.d.ts +0 -77
- package/dist/crypto/PureJSCrypto.d.ts.map +0 -1
- package/dist/crypto/PureJSCrypto.js +0 -236
- package/dist/crypto/PureJSCrypto.js.map +0 -1
- package/dist/tests/PureJSCrypto.test.d.ts +0 -2
- package/dist/tests/PureJSCrypto.test.d.ts.map +0 -1
- package/dist/tests/PureJSCrypto.test.js +0 -145
- package/dist/tests/PureJSCrypto.test.js.map +0 -1
- package/src/crypto/PureJSCrypto.ts +0 -429
- 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
|
+
}
|
package/src/GarbageCollector.ts
CHANGED
|
@@ -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(
|
|
39
|
+
coValue.unmount();
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/SyncStateManager.ts
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
116
|
+
listener(peer, knownState, syncState);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
if (coValueListeners) {
|
|
120
120
|
for (const listener of coValueListeners) {
|
|
121
|
-
listener(
|
|
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(),
|