cojson 0.16.3 → 0.16.5

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 (138) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +20 -0
  3. package/dist/coValue.d.ts +1 -1
  4. package/dist/coValue.d.ts.map +1 -1
  5. package/dist/coValue.js.map +1 -1
  6. package/dist/coValueContentMessage.d.ts +10 -0
  7. package/dist/coValueContentMessage.d.ts.map +1 -0
  8. package/dist/coValueContentMessage.js +46 -0
  9. package/dist/coValueContentMessage.js.map +1 -0
  10. package/dist/coValueCore/coValueCore.d.ts +6 -10
  11. package/dist/coValueCore/coValueCore.d.ts.map +1 -1
  12. package/dist/coValueCore/coValueCore.js +20 -125
  13. package/dist/coValueCore/coValueCore.js.map +1 -1
  14. package/dist/coValueCore/verifiedState.d.ts +1 -0
  15. package/dist/coValueCore/verifiedState.d.ts.map +1 -1
  16. package/dist/coValueCore/verifiedState.js +14 -27
  17. package/dist/coValueCore/verifiedState.js.map +1 -1
  18. package/dist/coValues/group.d.ts +18 -10
  19. package/dist/coValues/group.d.ts.map +1 -1
  20. package/dist/coValues/group.js +237 -67
  21. package/dist/coValues/group.js.map +1 -1
  22. package/dist/ids.d.ts +3 -3
  23. package/dist/ids.d.ts.map +1 -1
  24. package/dist/ids.js.map +1 -1
  25. package/dist/localNode.d.ts +11 -6
  26. package/dist/localNode.d.ts.map +1 -1
  27. package/dist/localNode.js +7 -2
  28. package/dist/localNode.js.map +1 -1
  29. package/dist/queue/LocalTransactionsSyncQueue.d.ts +24 -0
  30. package/dist/queue/LocalTransactionsSyncQueue.d.ts.map +1 -0
  31. package/dist/queue/LocalTransactionsSyncQueue.js +55 -0
  32. package/dist/queue/LocalTransactionsSyncQueue.js.map +1 -0
  33. package/dist/queue/StoreQueue.d.ts +9 -6
  34. package/dist/queue/StoreQueue.d.ts.map +1 -1
  35. package/dist/queue/StoreQueue.js +10 -2
  36. package/dist/queue/StoreQueue.js.map +1 -1
  37. package/dist/storage/storageAsync.d.ts +11 -3
  38. package/dist/storage/storageAsync.d.ts.map +1 -1
  39. package/dist/storage/storageAsync.js +59 -46
  40. package/dist/storage/storageAsync.js.map +1 -1
  41. package/dist/storage/storageSync.d.ts +9 -3
  42. package/dist/storage/storageSync.d.ts.map +1 -1
  43. package/dist/storage/storageSync.js +48 -35
  44. package/dist/storage/storageSync.js.map +1 -1
  45. package/dist/storage/syncUtils.d.ts +2 -1
  46. package/dist/storage/syncUtils.d.ts.map +1 -1
  47. package/dist/storage/syncUtils.js +4 -0
  48. package/dist/storage/syncUtils.js.map +1 -1
  49. package/dist/storage/types.d.ts +3 -2
  50. package/dist/storage/types.d.ts.map +1 -1
  51. package/dist/sync.d.ts +6 -6
  52. package/dist/sync.d.ts.map +1 -1
  53. package/dist/sync.js +33 -56
  54. package/dist/sync.js.map +1 -1
  55. package/dist/tests/StorageApiAsync.test.d.ts +2 -0
  56. package/dist/tests/StorageApiAsync.test.d.ts.map +1 -0
  57. package/dist/tests/StorageApiAsync.test.js +574 -0
  58. package/dist/tests/StorageApiAsync.test.js.map +1 -0
  59. package/dist/tests/StorageApiSync.test.d.ts +2 -0
  60. package/dist/tests/StorageApiSync.test.d.ts.map +1 -0
  61. package/dist/tests/StorageApiSync.test.js +426 -0
  62. package/dist/tests/StorageApiSync.test.js.map +1 -0
  63. package/dist/tests/StoreQueue.test.js +9 -21
  64. package/dist/tests/StoreQueue.test.js.map +1 -1
  65. package/dist/tests/SyncStateManager.test.js +18 -8
  66. package/dist/tests/SyncStateManager.test.js.map +1 -1
  67. package/dist/tests/group.inheritance.test.js +274 -2
  68. package/dist/tests/group.inheritance.test.js.map +1 -1
  69. package/dist/tests/group.removeMember.test.js +152 -1
  70. package/dist/tests/group.removeMember.test.js.map +1 -1
  71. package/dist/tests/group.roleOf.test.js +2 -2
  72. package/dist/tests/group.roleOf.test.js.map +1 -1
  73. package/dist/tests/group.test.js +81 -3
  74. package/dist/tests/group.test.js.map +1 -1
  75. package/dist/tests/sync.auth.test.js +22 -10
  76. package/dist/tests/sync.auth.test.js.map +1 -1
  77. package/dist/tests/sync.load.test.js +30 -25
  78. package/dist/tests/sync.load.test.js.map +1 -1
  79. package/dist/tests/sync.mesh.test.js +12 -6
  80. package/dist/tests/sync.mesh.test.js.map +1 -1
  81. package/dist/tests/sync.peerReconciliation.test.js +6 -4
  82. package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
  83. package/dist/tests/sync.storage.test.js +8 -14
  84. package/dist/tests/sync.storage.test.js.map +1 -1
  85. package/dist/tests/sync.storageAsync.test.js +31 -14
  86. package/dist/tests/sync.storageAsync.test.js.map +1 -1
  87. package/dist/tests/sync.test.js +5 -9
  88. package/dist/tests/sync.test.js.map +1 -1
  89. package/dist/tests/sync.upload.test.js +31 -1
  90. package/dist/tests/sync.upload.test.js.map +1 -1
  91. package/dist/tests/testStorage.d.ts +2 -3
  92. package/dist/tests/testStorage.d.ts.map +1 -1
  93. package/dist/tests/testStorage.js +16 -8
  94. package/dist/tests/testStorage.js.map +1 -1
  95. package/dist/tests/testUtils.d.ts +4 -0
  96. package/dist/tests/testUtils.d.ts.map +1 -1
  97. package/dist/tests/testUtils.js +22 -4
  98. package/dist/tests/testUtils.js.map +1 -1
  99. package/dist/typeUtils/accountOrAgentIDfromSessionID.d.ts +2 -2
  100. package/dist/typeUtils/accountOrAgentIDfromSessionID.d.ts.map +1 -1
  101. package/dist/typeUtils/expectGroup.d.ts.map +1 -1
  102. package/dist/typeUtils/expectGroup.js +6 -5
  103. package/dist/typeUtils/expectGroup.js.map +1 -1
  104. package/package.json +1 -1
  105. package/src/coValue.ts +1 -4
  106. package/src/coValueContentMessage.ts +73 -0
  107. package/src/coValueCore/coValueCore.ts +36 -192
  108. package/src/coValueCore/verifiedState.ts +28 -35
  109. package/src/coValues/group.ts +329 -99
  110. package/src/ids.ts +3 -3
  111. package/src/localNode.ts +15 -10
  112. package/src/queue/LocalTransactionsSyncQueue.ts +96 -0
  113. package/src/queue/StoreQueue.ts +22 -12
  114. package/src/storage/storageAsync.ts +78 -56
  115. package/src/storage/storageSync.ts +66 -45
  116. package/src/storage/syncUtils.ts +9 -1
  117. package/src/storage/types.ts +6 -5
  118. package/src/sync.ts +47 -67
  119. package/src/tests/StorageApiAsync.test.ts +829 -0
  120. package/src/tests/StorageApiSync.test.ts +628 -0
  121. package/src/tests/StoreQueue.test.ts +10 -24
  122. package/src/tests/SyncStateManager.test.ts +22 -21
  123. package/src/tests/group.inheritance.test.ts +415 -1
  124. package/src/tests/group.removeMember.test.ts +244 -1
  125. package/src/tests/group.roleOf.test.ts +2 -2
  126. package/src/tests/group.test.ts +105 -5
  127. package/src/tests/sync.auth.test.ts +22 -10
  128. package/src/tests/sync.load.test.ts +32 -26
  129. package/src/tests/sync.mesh.test.ts +12 -6
  130. package/src/tests/sync.peerReconciliation.test.ts +6 -4
  131. package/src/tests/sync.storage.test.ts +8 -14
  132. package/src/tests/sync.storageAsync.test.ts +39 -14
  133. package/src/tests/sync.test.ts +6 -14
  134. package/src/tests/sync.upload.test.ts +38 -1
  135. package/src/tests/testStorage.ts +19 -13
  136. package/src/tests/testUtils.ts +29 -5
  137. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +2 -2
  138. package/src/typeUtils/expectGroup.ts +8 -5
package/src/sync.ts CHANGED
@@ -1,15 +1,24 @@
1
1
  import { Histogram, ValueType, metrics } from "@opentelemetry/api";
2
2
  import { PeerState } from "./PeerState.js";
3
3
  import { SyncStateManager } from "./SyncStateManager.js";
4
+ import {
5
+ getTransactionSize,
6
+ knownStateFromContent,
7
+ } from "./coValueContentMessage.js";
4
8
  import { CoValueCore } from "./coValueCore/coValueCore.js";
5
9
  import { getDependedOnCoValuesFromRawData } from "./coValueCore/utils.js";
6
- import { CoValueHeader, Transaction } from "./coValueCore/verifiedState.js";
10
+ import {
11
+ CoValueHeader,
12
+ Transaction,
13
+ VerifiedState,
14
+ } from "./coValueCore/verifiedState.js";
7
15
  import { Signature } from "./crypto/crypto.js";
8
- import { RawCoID, SessionID } from "./ids.js";
16
+ import { RawCoID, SessionID, isRawCoID } from "./ids.js";
9
17
  import { LocalNode } from "./localNode.js";
10
18
  import { logger } from "./logger.js";
11
19
  import { CoValuePriority } from "./priority.js";
12
20
  import { IncomingMessagesQueue } from "./queue/IncomingMessagesQueue.js";
21
+ import { LocalTransactionsSyncQueue } from "./queue/LocalTransactionsSyncQueue.js";
13
22
  import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
14
23
  import { isAccountID } from "./typeUtils/isAccountID.js";
15
24
 
@@ -57,10 +66,12 @@ export type NewContentMessage = {
57
66
  };
58
67
 
59
68
  export type SessionNewContent = {
69
+ // The index where to start appending the new transactions. The index counting starts from 1.
60
70
  after: number;
61
71
  newTransactions: Transaction[];
62
72
  lastSignature: Signature;
63
73
  };
74
+
64
75
  export type DoneMessage = {
65
76
  action: "done";
66
77
  id: RawCoID;
@@ -162,13 +173,9 @@ export class SyncManager {
162
173
  }
163
174
 
164
175
  handleSyncMessage(msg: SyncMessage, peer: PeerState) {
165
- if (msg.id === undefined || msg.id === null) {
166
- logger.warn("Received sync message with undefined id", {
167
- msg,
168
- });
169
- return;
170
- } else if (!msg.id.startsWith("co_z")) {
171
- logger.warn("Received sync message with invalid id", {
176
+ if (!isRawCoID(msg.id)) {
177
+ const errorType = msg.id ? "invalid" : "undefined";
178
+ logger.warn(`Received sync message with ${errorType} id`, {
172
179
  msg,
173
180
  });
174
181
  return;
@@ -431,12 +438,9 @@ export class SyncManager {
431
438
 
432
439
  recordTransactionsSize(newTransactions: Transaction[], source: string) {
433
440
  for (const tx of newTransactions) {
434
- const txLength =
435
- tx.privacy === "private"
436
- ? tx.encryptedChanges.length
437
- : tx.changes.length;
441
+ const size = getTransactionSize(tx);
438
442
 
439
- this.transactionsSizeHistogram.record(txLength, {
443
+ this.transactionsSizeHistogram.record(size, {
440
444
  source,
441
445
  });
442
446
  }
@@ -674,7 +678,7 @@ export class SyncManager {
674
678
  const syncedPeers = [];
675
679
 
676
680
  if (from !== "storage") {
677
- this.storeCoValue(coValue, [msg]);
681
+ this.storeContent(msg);
678
682
  }
679
683
 
680
684
  for (const peer of this.peersInPriorityOrder()) {
@@ -736,60 +740,18 @@ export class SyncManager {
736
740
  };
737
741
  }
738
742
 
739
- requestedSyncs = new Set<RawCoID>();
740
- requestCoValueSync(coValue: CoValueCore) {
741
- if (this.requestedSyncs.has(coValue.id)) {
742
- return;
743
- }
744
-
745
- for (const trackingSet of this.dirtyCoValuesTrackingSets) {
746
- trackingSet.add(coValue.id);
747
- }
748
-
749
- queueMicrotask(() => {
750
- if (this.requestedSyncs.has(coValue.id)) {
751
- this.syncCoValue(coValue);
752
- }
753
- });
754
-
755
- this.requestedSyncs.add(coValue.id);
756
- }
757
-
758
- storeCoValue(coValue: CoValueCore, data: NewContentMessage[] | undefined) {
759
- const storage = this.local.storage;
760
-
761
- if (!storage || !data) return;
762
-
763
- // Try to store the content as-is for performance
764
- // In case that some transactions are missing, a correction will be requested, but it's an edge case
765
- storage.store(data, (correction) => {
766
- if (!coValue.hasVerifiedContent()) return;
767
-
768
- const newContentPieces = coValue.verified.newContentSince(correction);
769
-
770
- if (!newContentPieces) return;
743
+ private syncQueue = new LocalTransactionsSyncQueue((content) =>
744
+ this.syncContent(content),
745
+ );
746
+ syncHeader = this.syncQueue.syncHeader;
747
+ syncLocalTransaction = this.syncQueue.syncTransaction;
771
748
 
772
- storage.store(newContentPieces, (response) => {
773
- logger.error(
774
- "Correction requested by storage after sending a correction content",
775
- {
776
- response,
777
- knownState: coValue.knownState(),
778
- },
779
- );
780
- });
781
- });
782
- }
749
+ syncContent(content: NewContentMessage) {
750
+ const coValue = this.local.getCoValue(content.id);
783
751
 
784
- syncCoValue(coValue: CoValueCore) {
785
- this.requestedSyncs.delete(coValue.id);
752
+ this.storeContent(content);
786
753
 
787
- if (this.local.storage && coValue.hasVerifiedContent()) {
788
- const knownState = this.local.storage.getKnownState(coValue.id);
789
- const newContentPieces = coValue.verified.newContentSince(knownState);
790
-
791
- this.storeCoValue(coValue, newContentPieces);
792
- }
754
+ const contentKnownState = knownStateFromContent(content);
793
755
 
794
756
  for (const peer of this.peersInPriorityOrder()) {
795
757
  if (peer.closed) continue;
@@ -803,7 +765,11 @@ export class SyncManager {
803
765
  continue;
804
766
  }
805
767
 
806
- this.sendNewContentIncludingDependencies(coValue.id, peer);
768
+ // We assume that the peer already knows anything before this content
769
+ // Any eventual reconciliation will be handled through the known state messages exchange
770
+ this.trySendToPeer(peer, content);
771
+ peer.combineOptimisticWith(coValue.id, contentKnownState);
772
+ peer.trackToldKnownState(coValue.id);
807
773
  }
808
774
 
809
775
  for (const peer of this.getPeers()) {
@@ -811,6 +777,20 @@ export class SyncManager {
811
777
  }
812
778
  }
813
779
 
780
+ private storeContent(content: NewContentMessage) {
781
+ const storage = this.local.storage;
782
+
783
+ if (!storage) return;
784
+
785
+ // Try to store the content as-is for performance
786
+ // In case that some transactions are missing, a correction will be requested, but it's an edge case
787
+ storage.store(content, (correction) => {
788
+ return this.local
789
+ .getCoValue(content.id)
790
+ .verified?.newContentSince(correction);
791
+ });
792
+ }
793
+
814
794
  waitForSyncWithPeer(peerId: PeerID, id: RawCoID, timeout: number) {
815
795
  const { syncState } = this;
816
796
  const currentSyncState = syncState.getCurrentSyncState(peerId, id);