cojson 0.0.7 → 0.0.8

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 (92) hide show
  1. package/.eslintrc.cjs +11 -8
  2. package/dist/coValue.d.ts +97 -0
  3. package/dist/coValue.js +381 -0
  4. package/dist/coValue.js.map +1 -0
  5. package/dist/coValue.test.d.ts +1 -0
  6. package/dist/coValue.test.js +78 -0
  7. package/dist/coValue.test.js.map +1 -0
  8. package/dist/contentType.d.ts +15 -0
  9. package/dist/contentType.js +7 -0
  10. package/dist/contentType.js.map +1 -0
  11. package/dist/contentType.test.d.ts +1 -0
  12. package/dist/contentType.test.js +149 -0
  13. package/dist/contentType.test.js.map +1 -0
  14. package/dist/contentTypes/coList.d.ts +11 -0
  15. package/dist/contentTypes/coList.js +16 -0
  16. package/dist/contentTypes/coList.js.map +1 -0
  17. package/dist/contentTypes/coMap.d.ts +56 -0
  18. package/dist/contentTypes/coMap.js +126 -0
  19. package/dist/contentTypes/coMap.js.map +1 -0
  20. package/dist/contentTypes/coStream.d.ts +11 -0
  21. package/dist/contentTypes/coStream.js +16 -0
  22. package/dist/contentTypes/coStream.js.map +1 -0
  23. package/dist/contentTypes/static.d.ts +11 -0
  24. package/dist/contentTypes/static.js +14 -0
  25. package/dist/contentTypes/static.js.map +1 -0
  26. package/dist/crypto.d.ts +97 -0
  27. package/dist/crypto.js +156 -0
  28. package/dist/crypto.js.map +1 -0
  29. package/dist/crypto.test.d.ts +1 -0
  30. package/dist/crypto.test.js +115 -0
  31. package/dist/crypto.test.js.map +1 -0
  32. package/dist/ids.d.ts +7 -0
  33. package/dist/ids.js +2 -0
  34. package/dist/ids.js.map +1 -0
  35. package/dist/index.d.ts +19 -0
  36. package/dist/index.js +13 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/jsonValue.d.ts +7 -0
  39. package/dist/jsonValue.js +2 -0
  40. package/dist/jsonValue.js.map +1 -0
  41. package/dist/node.d.ts +33 -0
  42. package/dist/node.js +113 -0
  43. package/dist/node.js.map +1 -0
  44. package/dist/permissions.d.ts +54 -0
  45. package/dist/permissions.js +218 -0
  46. package/dist/permissions.js.map +1 -0
  47. package/dist/permissions.test.d.ts +1 -0
  48. package/dist/permissions.test.js +794 -0
  49. package/dist/permissions.test.js.map +1 -0
  50. package/dist/sync.d.ts +80 -0
  51. package/dist/sync.js +271 -0
  52. package/dist/sync.js.map +1 -0
  53. package/dist/sync.test.d.ts +1 -0
  54. package/dist/sync.test.js +826 -0
  55. package/dist/sync.test.js.map +1 -0
  56. package/package.json +5 -4
  57. package/src/coValue.test.ts +3 -4
  58. package/src/coValue.ts +11 -11
  59. package/src/contentType.test.ts +3 -3
  60. package/src/contentType.ts +6 -6
  61. package/src/contentTypes/coList.ts +4 -4
  62. package/src/contentTypes/coMap.ts +6 -6
  63. package/src/contentTypes/coStream.ts +4 -4
  64. package/src/contentTypes/static.ts +5 -5
  65. package/src/crypto.test.ts +1 -1
  66. package/src/crypto.ts +2 -2
  67. package/src/index.ts +8 -8
  68. package/src/jsonValue.ts +1 -1
  69. package/src/node.ts +6 -7
  70. package/src/permissions.test.ts +5 -5
  71. package/src/permissions.ts +7 -7
  72. package/src/sync.test.ts +7 -7
  73. package/src/sync.ts +6 -6
  74. package/tsconfig.json +1 -7
  75. package/dist/coValue.mjs +0 -437
  76. package/dist/coValue.test.mjs +0 -122
  77. package/dist/contentType.mjs +0 -7
  78. package/dist/contentType.test.mjs +0 -179
  79. package/dist/contentTypes/coList.mjs +0 -18
  80. package/dist/contentTypes/coMap.mjs +0 -126
  81. package/dist/contentTypes/coStream.mjs +0 -18
  82. package/dist/contentTypes/static.mjs +0 -16
  83. package/dist/crypto.mjs +0 -207
  84. package/dist/crypto.test.mjs +0 -155
  85. package/dist/ids.mjs +0 -1
  86. package/dist/index.mjs +0 -21
  87. package/dist/jsonValue.mjs +0 -1
  88. package/dist/node.mjs +0 -144
  89. package/dist/permissions.mjs +0 -244
  90. package/dist/permissions.test.mjs +0 -985
  91. package/dist/sync.mjs +0 -318
  92. package/dist/sync.test.mjs +0 -861
package/dist/sync.mjs DELETED
@@ -1,318 +0,0 @@
1
- "use strict";
2
- import { CoValue } from "./coValue";
3
- import { newLoadingState } from "./node";
4
- export function emptyKnownState(coValueID) {
5
- return {
6
- coValueID,
7
- header: false,
8
- sessions: {}
9
- };
10
- }
11
- export function combinedKnownStates(stateA, stateB) {
12
- const sessionStates = {};
13
- const allSessions = /* @__PURE__ */ new Set([
14
- ...Object.keys(stateA.sessions),
15
- ...Object.keys(stateB.sessions)
16
- ]);
17
- for (const sessionID of allSessions) {
18
- const stateAValue = stateA.sessions[sessionID];
19
- const stateBValue = stateB.sessions[sessionID];
20
- sessionStates[sessionID] = Math.max(stateAValue || 0, stateBValue || 0);
21
- }
22
- return {
23
- coValueID: stateA.coValueID,
24
- header: stateA.header || stateB.header,
25
- sessions: sessionStates
26
- };
27
- }
28
- export class SyncManager {
29
- peers = {};
30
- local;
31
- constructor(local) {
32
- this.local = local;
33
- }
34
- loadFromPeers(id) {
35
- for (const peer of Object.values(this.peers)) {
36
- peer.outgoing.write({
37
- action: "subscribe",
38
- coValueID: id,
39
- header: false,
40
- sessions: {}
41
- }).catch((e) => {
42
- console.error("Error writing to peer", e);
43
- });
44
- }
45
- }
46
- async handleSyncMessage(msg, peer) {
47
- switch (msg.action) {
48
- case "subscribe":
49
- return await this.handleSubscribe(msg, peer);
50
- case "tellKnownState":
51
- return await this.handleTellKnownState(msg, peer);
52
- case "newContent":
53
- return await this.handleNewContent(msg, peer);
54
- case "wrongAssumedKnownState":
55
- return await this.handleWrongAssumedKnownState(msg, peer);
56
- case "unsubscribe":
57
- return await this.handleUnsubscribe(msg);
58
- default:
59
- throw new Error(
60
- `Unknown message type ${msg.action}`
61
- );
62
- }
63
- }
64
- async subscribeToIncludingDependencies(coValueID, peer) {
65
- const coValue = this.local.expectCoValueLoaded(coValueID);
66
- for (const coValueID2 of coValue.getDependedOnCoValues()) {
67
- await this.subscribeToIncludingDependencies(coValueID2, peer);
68
- }
69
- if (!peer.toldKnownState.has(coValueID)) {
70
- peer.toldKnownState.add(coValueID);
71
- await peer.outgoing.write({
72
- action: "subscribe",
73
- ...coValue.knownState()
74
- });
75
- }
76
- }
77
- async tellUntoldKnownStateIncludingDependencies(coValueID, peer, asDependencyOf) {
78
- const coValue = this.local.expectCoValueLoaded(coValueID);
79
- for (const dependentCoValueID of coValue.getDependedOnCoValues()) {
80
- await this.tellUntoldKnownStateIncludingDependencies(
81
- dependentCoValueID,
82
- peer,
83
- asDependencyOf || coValueID
84
- );
85
- }
86
- if (!peer.toldKnownState.has(coValueID)) {
87
- await peer.outgoing.write({
88
- action: "tellKnownState",
89
- asDependencyOf,
90
- ...coValue.knownState()
91
- });
92
- peer.toldKnownState.add(coValueID);
93
- }
94
- }
95
- async sendNewContentIncludingDependencies(coValueID, peer) {
96
- const coValue = this.local.expectCoValueLoaded(coValueID);
97
- for (const coValueID2 of coValue.getDependedOnCoValues()) {
98
- await this.sendNewContentIncludingDependencies(coValueID2, peer);
99
- }
100
- const newContent = coValue.newContentSince(
101
- peer.optimisticKnownStates[coValueID]
102
- );
103
- if (newContent) {
104
- await peer.outgoing.write(newContent);
105
- peer.optimisticKnownStates[coValueID] = combinedKnownStates(
106
- peer.optimisticKnownStates[coValueID] || emptyKnownState(coValueID),
107
- coValue.knownState()
108
- );
109
- }
110
- }
111
- addPeer(peer) {
112
- const peerState = {
113
- id: peer.id,
114
- optimisticKnownStates: {},
115
- incoming: peer.incoming,
116
- outgoing: peer.outgoing.getWriter(),
117
- toldKnownState: /* @__PURE__ */ new Set(),
118
- role: peer.role
119
- };
120
- this.peers[peer.id] = peerState;
121
- if (peer.role === "server") {
122
- const initialSync = async () => {
123
- for (const entry of Object.values(this.local.coValues)) {
124
- if (entry.state === "loading") {
125
- continue;
126
- }
127
- await this.subscribeToIncludingDependencies(
128
- entry.coValue.id,
129
- peerState
130
- );
131
- peerState.optimisticKnownStates[entry.coValue.id] = {
132
- coValueID: entry.coValue.id,
133
- header: false,
134
- sessions: {}
135
- };
136
- }
137
- };
138
- void initialSync();
139
- }
140
- const readIncoming = async () => {
141
- for await (const msg of peerState.incoming) {
142
- try {
143
- await this.handleSyncMessage(msg, peerState);
144
- } catch (e) {
145
- console.error(
146
- `Error reading from peer ${peer.id}`,
147
- JSON.stringify(msg),
148
- e
149
- );
150
- }
151
- }
152
- };
153
- void readIncoming();
154
- }
155
- async handleSubscribe(msg, peer) {
156
- const entry = this.local.coValues[msg.coValueID];
157
- if (!entry || entry.state === "loading") {
158
- if (!entry) {
159
- this.local.coValues[msg.coValueID] = newLoadingState();
160
- }
161
- peer.optimisticKnownStates[msg.coValueID] = knownStateIn(msg);
162
- peer.toldKnownState.add(msg.coValueID);
163
- await peer.outgoing.write({
164
- action: "tellKnownState",
165
- coValueID: msg.coValueID,
166
- header: false,
167
- sessions: {}
168
- });
169
- return;
170
- }
171
- peer.optimisticKnownStates[msg.coValueID] = knownStateIn(msg);
172
- await this.tellUntoldKnownStateIncludingDependencies(
173
- msg.coValueID,
174
- peer
175
- );
176
- await this.sendNewContentIncludingDependencies(msg.coValueID, peer);
177
- }
178
- async handleTellKnownState(msg, peer) {
179
- let entry = this.local.coValues[msg.coValueID];
180
- peer.optimisticKnownStates[msg.coValueID] = combinedKnownStates(
181
- peer.optimisticKnownStates[msg.coValueID] || emptyKnownState(msg.coValueID),
182
- knownStateIn(msg)
183
- );
184
- if (!entry) {
185
- if (msg.asDependencyOf) {
186
- if (this.local.coValues[msg.asDependencyOf]) {
187
- entry = newLoadingState();
188
- this.local.coValues[msg.coValueID] = entry;
189
- } else {
190
- throw new Error(
191
- "Expected coValue dependency entry to be created, missing subscribe?"
192
- );
193
- }
194
- } else {
195
- throw new Error(
196
- "Expected coValue entry to be created, missing subscribe?"
197
- );
198
- }
199
- }
200
- if (entry.state === "loading") {
201
- return [];
202
- }
203
- await this.tellUntoldKnownStateIncludingDependencies(
204
- msg.coValueID,
205
- peer
206
- );
207
- await this.sendNewContentIncludingDependencies(msg.coValueID, peer);
208
- }
209
- async handleNewContent(msg, peer) {
210
- var _a;
211
- let entry = this.local.coValues[msg.coValueID];
212
- if (!entry) {
213
- throw new Error(
214
- "Expected coValue entry to be created, missing subscribe?"
215
- );
216
- }
217
- let resolveAfterDone;
218
- const peerOptimisticKnownState = peer.optimisticKnownStates[msg.coValueID];
219
- if (!peerOptimisticKnownState) {
220
- throw new Error(
221
- "Expected optimisticKnownState to be set for coValue we receive new content for"
222
- );
223
- }
224
- if (entry.state === "loading") {
225
- if (!msg.header) {
226
- throw new Error("Expected header to be sent in first message");
227
- }
228
- peerOptimisticKnownState.header = true;
229
- const coValue2 = new CoValue(msg.header, this.local);
230
- resolveAfterDone = entry.resolve;
231
- entry = {
232
- state: "loaded",
233
- coValue: coValue2
234
- };
235
- this.local.coValues[msg.coValueID] = entry;
236
- }
237
- const coValue = entry.coValue;
238
- let invalidStateAssumed = false;
239
- for (const [sessionID, newContentForSession] of Object.entries(
240
- msg.newContent
241
- )) {
242
- const ourKnownTxIdx = (_a = coValue.sessions[sessionID]) == null ? void 0 : _a.transactions.length;
243
- const theirFirstNewTxIdx = newContentForSession.after;
244
- if ((ourKnownTxIdx || 0) < theirFirstNewTxIdx) {
245
- invalidStateAssumed = true;
246
- continue;
247
- }
248
- const alreadyKnownOffset = ourKnownTxIdx ? ourKnownTxIdx - theirFirstNewTxIdx : 0;
249
- const newTransactions = newContentForSession.newTransactions.slice(alreadyKnownOffset);
250
- const success = coValue.tryAddTransactions(
251
- sessionID,
252
- newTransactions,
253
- newContentForSession.lastHash,
254
- newContentForSession.lastSignature
255
- );
256
- if (!success) {
257
- console.error("Failed to add transactions", newTransactions);
258
- continue;
259
- }
260
- peerOptimisticKnownState.sessions[sessionID] = newContentForSession.after + newContentForSession.newTransactions.length;
261
- }
262
- if (resolveAfterDone) {
263
- resolveAfterDone(coValue);
264
- }
265
- await this.syncCoValue(coValue);
266
- if (invalidStateAssumed) {
267
- await peer.outgoing.write({
268
- action: "wrongAssumedKnownState",
269
- ...coValue.knownState()
270
- });
271
- }
272
- }
273
- async handleWrongAssumedKnownState(msg, peer) {
274
- const coValue = this.local.expectCoValueLoaded(msg.coValueID);
275
- peer.optimisticKnownStates[msg.coValueID] = combinedKnownStates(
276
- msg,
277
- coValue.knownState()
278
- );
279
- const newContent = coValue.newContentSince(msg);
280
- if (newContent) {
281
- await peer.outgoing.write(newContent);
282
- }
283
- }
284
- handleUnsubscribe(_msg) {
285
- throw new Error("Method not implemented.");
286
- }
287
- async syncCoValue(coValue) {
288
- for (const peer of Object.values(this.peers)) {
289
- const optimisticKnownState = peer.optimisticKnownStates[coValue.id];
290
- if (optimisticKnownState) {
291
- await this.tellUntoldKnownStateIncludingDependencies(
292
- coValue.id,
293
- peer
294
- );
295
- await this.sendNewContentIncludingDependencies(
296
- coValue.id,
297
- peer
298
- );
299
- } else if (peer.role === "server") {
300
- await this.subscribeToIncludingDependencies(
301
- coValue.id,
302
- peer
303
- );
304
- await this.sendNewContentIncludingDependencies(
305
- coValue.id,
306
- peer
307
- );
308
- }
309
- }
310
- }
311
- }
312
- function knownStateIn(msg) {
313
- return {
314
- coValueID: msg.coValueID,
315
- header: msg.header,
316
- sessions: msg.sessions
317
- };
318
- }