cojson 0.8.0 → 0.8.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 (210) hide show
  1. package/.turbo/turbo-build.log +11 -3
  2. package/CHANGELOG.md +13 -0
  3. package/dist/native/PeerState.js.map +1 -0
  4. package/dist/native/PriorityBasedMessageQueue.js.map +1 -0
  5. package/dist/native/base64url.js.map +1 -0
  6. package/dist/native/base64url.test.js.map +1 -0
  7. package/dist/native/coValue.js.map +1 -0
  8. package/dist/native/coValueCore.js.map +1 -0
  9. package/dist/native/coValues/account.js.map +1 -0
  10. package/dist/native/coValues/coList.js.map +1 -0
  11. package/dist/native/coValues/coMap.js.map +1 -0
  12. package/dist/native/coValues/coStream.js.map +1 -0
  13. package/dist/native/coValues/group.js.map +1 -0
  14. package/dist/native/coreToCoValue.js.map +1 -0
  15. package/dist/{crypto → native/crypto}/PureJSCrypto.js +4 -0
  16. package/dist/native/crypto/PureJSCrypto.js.map +1 -0
  17. package/dist/{crypto → native/crypto}/crypto.js +1 -1
  18. package/dist/native/crypto/crypto.js.map +1 -0
  19. package/dist/native/ids.js.map +1 -0
  20. package/dist/{index.js → native/index.native.js} +3 -4
  21. package/dist/native/index.native.js.map +1 -0
  22. package/dist/native/jsonStringify.js.map +1 -0
  23. package/dist/{jsonValue.js.map → native/jsonValue.js.map} +1 -1
  24. package/dist/{localNode.js → native/localNode.js} +50 -33
  25. package/dist/native/localNode.js.map +1 -0
  26. package/dist/native/media.js.map +1 -0
  27. package/dist/native/permissions.js.map +1 -0
  28. package/dist/native/priority.js.map +1 -0
  29. package/dist/native/storage/FileSystem.js.map +1 -0
  30. package/dist/{storage → native/storage}/chunksAndKnownStates.js +1 -1
  31. package/dist/native/storage/chunksAndKnownStates.js.map +1 -0
  32. package/dist/native/storage/index.js.map +1 -0
  33. package/dist/native/streamUtils.js.map +1 -0
  34. package/dist/native/sync.js.map +1 -0
  35. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
  36. package/dist/native/typeUtils/expectGroup.js.map +1 -0
  37. package/dist/native/typeUtils/isAccountID.js.map +1 -0
  38. package/dist/native/typeUtils/isCoValue.js.map +1 -0
  39. package/dist/web/PeerState.js +66 -0
  40. package/dist/web/PeerState.js.map +1 -0
  41. package/dist/web/PriorityBasedMessageQueue.js +51 -0
  42. package/dist/web/PriorityBasedMessageQueue.js.map +1 -0
  43. package/dist/web/base64url.js +59 -0
  44. package/dist/web/base64url.js.map +1 -0
  45. package/dist/web/base64url.test.js +25 -0
  46. package/dist/web/base64url.test.js.map +1 -0
  47. package/dist/web/coValue.js +19 -0
  48. package/dist/web/coValue.js.map +1 -0
  49. package/dist/web/coValueCore.js +693 -0
  50. package/dist/web/coValueCore.js.map +1 -0
  51. package/dist/web/coValues/account.js +96 -0
  52. package/dist/web/coValues/account.js.map +1 -0
  53. package/dist/web/coValues/coList.js +393 -0
  54. package/dist/web/coValues/coList.js.map +1 -0
  55. package/dist/web/coValues/coMap.js +197 -0
  56. package/dist/web/coValues/coMap.js.map +1 -0
  57. package/dist/web/coValues/coStream.js +220 -0
  58. package/dist/web/coValues/coStream.js.map +1 -0
  59. package/dist/web/coValues/group.js +250 -0
  60. package/dist/web/coValues/group.js.map +1 -0
  61. package/dist/web/coreToCoValue.js +42 -0
  62. package/dist/web/coreToCoValue.js.map +1 -0
  63. package/dist/web/crypto/PureJSCrypto.js +93 -0
  64. package/dist/web/crypto/PureJSCrypto.js.map +1 -0
  65. package/dist/{crypto → web/crypto}/WasmCrypto.js +3 -0
  66. package/dist/web/crypto/WasmCrypto.js.map +1 -0
  67. package/dist/web/crypto/crypto.js +154 -0
  68. package/dist/web/crypto/crypto.js.map +1 -0
  69. package/dist/web/ids.js +17 -0
  70. package/dist/web/ids.js.map +1 -0
  71. package/dist/web/index.web.js +40 -0
  72. package/dist/web/index.web.js.map +1 -0
  73. package/dist/web/jsonStringify.js +57 -0
  74. package/dist/web/jsonStringify.js.map +1 -0
  75. package/dist/web/jsonValue.js +2 -0
  76. package/dist/web/jsonValue.js.map +1 -0
  77. package/dist/web/localNode.js +453 -0
  78. package/dist/web/localNode.js.map +1 -0
  79. package/dist/web/media.js +2 -0
  80. package/dist/web/media.js.map +1 -0
  81. package/dist/web/permissions.js +206 -0
  82. package/dist/web/permissions.js.map +1 -0
  83. package/dist/web/priority.js +31 -0
  84. package/dist/web/priority.js.map +1 -0
  85. package/dist/web/storage/FileSystem.js +58 -0
  86. package/dist/web/storage/FileSystem.js.map +1 -0
  87. package/dist/web/storage/chunksAndKnownStates.js +100 -0
  88. package/dist/web/storage/chunksAndKnownStates.js.map +1 -0
  89. package/dist/web/storage/index.js +348 -0
  90. package/dist/web/storage/index.js.map +1 -0
  91. package/dist/web/streamUtils.js +41 -0
  92. package/dist/web/streamUtils.js.map +1 -0
  93. package/dist/web/sync.js +504 -0
  94. package/dist/web/sync.js.map +1 -0
  95. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js +5 -0
  96. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
  97. package/dist/web/typeUtils/expectGroup.js +13 -0
  98. package/dist/web/typeUtils/expectGroup.js.map +1 -0
  99. package/dist/web/typeUtils/isAccountID.js +4 -0
  100. package/dist/web/typeUtils/isAccountID.js.map +1 -0
  101. package/dist/web/typeUtils/isCoValue.js +11 -0
  102. package/dist/web/typeUtils/isCoValue.js.map +1 -0
  103. package/package.json +25 -8
  104. package/src/coValue.ts +1 -1
  105. package/src/coValueCore.ts +4 -1
  106. package/src/coValues/account.ts +9 -3
  107. package/src/crypto/PureJSCrypto.ts +5 -0
  108. package/src/crypto/WasmCrypto.ts +4 -0
  109. package/src/crypto/crypto.ts +7 -2
  110. package/src/{index.ts → index.native.ts} +4 -5
  111. package/src/index.web.ts +152 -0
  112. package/src/jsonValue.ts +25 -0
  113. package/src/localNode.ts +74 -55
  114. package/src/storage/chunksAndKnownStates.ts +1 -1
  115. package/src/storage/index.ts +1 -1
  116. package/src/tests/coList.test.ts +1 -1
  117. package/src/tests/coMap.test.ts +1 -1
  118. package/src/tests/coStream.test.ts +2 -1
  119. package/src/tests/cryptoImpl.test.ts +24 -2
  120. package/src/tests/group.test.ts +6 -8
  121. package/src/tests/permissions.test.ts +66 -18
  122. package/src/tests/priority.test.ts +44 -15
  123. package/tsconfig.json +5 -4
  124. package/tsconfig.native.json +12 -0
  125. package/tsconfig.web.json +11 -0
  126. package/dist/PeerState.js.map +0 -1
  127. package/dist/PriorityBasedMessageQueue.js.map +0 -1
  128. package/dist/base64url.js.map +0 -1
  129. package/dist/base64url.test.js.map +0 -1
  130. package/dist/coValue.js.map +0 -1
  131. package/dist/coValueCore.js.map +0 -1
  132. package/dist/coValues/account.js.map +0 -1
  133. package/dist/coValues/coList.js.map +0 -1
  134. package/dist/coValues/coMap.js.map +0 -1
  135. package/dist/coValues/coStream.js.map +0 -1
  136. package/dist/coValues/group.js.map +0 -1
  137. package/dist/coreToCoValue.js.map +0 -1
  138. package/dist/crypto/PureJSCrypto.js.map +0 -1
  139. package/dist/crypto/WasmCrypto.js.map +0 -1
  140. package/dist/crypto/crypto.js.map +0 -1
  141. package/dist/ids.js.map +0 -1
  142. package/dist/index.js.map +0 -1
  143. package/dist/jsonStringify.js.map +0 -1
  144. package/dist/localNode.js.map +0 -1
  145. package/dist/media.js.map +0 -1
  146. package/dist/permissions.js.map +0 -1
  147. package/dist/priority.js.map +0 -1
  148. package/dist/storage/FileSystem.js.map +0 -1
  149. package/dist/storage/chunksAndKnownStates.js.map +0 -1
  150. package/dist/storage/index.js.map +0 -1
  151. package/dist/streamUtils.js.map +0 -1
  152. package/dist/sync.js.map +0 -1
  153. package/dist/tests/PeerState.test.js +0 -80
  154. package/dist/tests/PeerState.test.js.map +0 -1
  155. package/dist/tests/PriorityBasedMessageQueue.test.js +0 -97
  156. package/dist/tests/PriorityBasedMessageQueue.test.js.map +0 -1
  157. package/dist/tests/account.test.js +0 -58
  158. package/dist/tests/account.test.js.map +0 -1
  159. package/dist/tests/coList.test.js +0 -76
  160. package/dist/tests/coList.test.js.map +0 -1
  161. package/dist/tests/coMap.test.js +0 -136
  162. package/dist/tests/coMap.test.js.map +0 -1
  163. package/dist/tests/coStream.test.js +0 -205
  164. package/dist/tests/coStream.test.js.map +0 -1
  165. package/dist/tests/coValueCore.test.js +0 -124
  166. package/dist/tests/coValueCore.test.js.map +0 -1
  167. package/dist/tests/crypto.test.js +0 -118
  168. package/dist/tests/crypto.test.js.map +0 -1
  169. package/dist/tests/cryptoImpl.test.js +0 -113
  170. package/dist/tests/cryptoImpl.test.js.map +0 -1
  171. package/dist/tests/group.test.js +0 -34
  172. package/dist/tests/group.test.js.map +0 -1
  173. package/dist/tests/permissions.test.js +0 -1059
  174. package/dist/tests/permissions.test.js.map +0 -1
  175. package/dist/tests/priority.test.js +0 -61
  176. package/dist/tests/priority.test.js.map +0 -1
  177. package/dist/tests/sync.test.js +0 -1186
  178. package/dist/tests/sync.test.js.map +0 -1
  179. package/dist/tests/testUtils.js +0 -59
  180. package/dist/tests/testUtils.js.map +0 -1
  181. package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +0 -1
  182. package/dist/typeUtils/expectGroup.js.map +0 -1
  183. package/dist/typeUtils/isAccountID.js.map +0 -1
  184. package/dist/typeUtils/isCoValue.js.map +0 -1
  185. /package/dist/{PeerState.js → native/PeerState.js} +0 -0
  186. /package/dist/{PriorityBasedMessageQueue.js → native/PriorityBasedMessageQueue.js} +0 -0
  187. /package/dist/{base64url.js → native/base64url.js} +0 -0
  188. /package/dist/{base64url.test.js → native/base64url.test.js} +0 -0
  189. /package/dist/{coValue.js → native/coValue.js} +0 -0
  190. /package/dist/{coValueCore.js → native/coValueCore.js} +0 -0
  191. /package/dist/{coValues → native/coValues}/account.js +0 -0
  192. /package/dist/{coValues → native/coValues}/coList.js +0 -0
  193. /package/dist/{coValues → native/coValues}/coMap.js +0 -0
  194. /package/dist/{coValues → native/coValues}/coStream.js +0 -0
  195. /package/dist/{coValues → native/coValues}/group.js +0 -0
  196. /package/dist/{coreToCoValue.js → native/coreToCoValue.js} +0 -0
  197. /package/dist/{ids.js → native/ids.js} +0 -0
  198. /package/dist/{jsonStringify.js → native/jsonStringify.js} +0 -0
  199. /package/dist/{jsonValue.js → native/jsonValue.js} +0 -0
  200. /package/dist/{media.js → native/media.js} +0 -0
  201. /package/dist/{permissions.js → native/permissions.js} +0 -0
  202. /package/dist/{priority.js → native/priority.js} +0 -0
  203. /package/dist/{storage → native/storage}/FileSystem.js +0 -0
  204. /package/dist/{storage → native/storage}/index.js +0 -0
  205. /package/dist/{streamUtils.js → native/streamUtils.js} +0 -0
  206. /package/dist/{sync.js → native/sync.js} +0 -0
  207. /package/dist/{typeUtils → native/typeUtils}/accountOrAgentIDfromSessionID.js +0 -0
  208. /package/dist/{typeUtils → native/typeUtils}/expectGroup.js +0 -0
  209. /package/dist/{typeUtils → native/typeUtils}/isAccountID.js +0 -0
  210. /package/dist/{typeUtils → native/typeUtils}/isCoValue.js +0 -0
@@ -43,7 +43,10 @@ export type CoValueHeader = {
43
43
  meta: JsonObject | null;
44
44
  } & CoValueUniqueness;
45
45
 
46
- export type CoValueUniqueness = {uniqueness: JsonValue, createdAt?: `2${string}` | null}
46
+ export type CoValueUniqueness = {
47
+ uniqueness: JsonValue;
48
+ createdAt?: `2${string}` | null;
49
+ };
47
50
 
48
51
  export function idforHeader(
49
52
  header: CoValueHeader,
@@ -1,4 +1,8 @@
1
- import { CoValueCore, CoValueHeader, CoValueUniqueness } from "../coValueCore.js";
1
+ import {
2
+ CoValueCore,
3
+ CoValueHeader,
4
+ CoValueUniqueness,
5
+ } from "../coValueCore.js";
2
6
  import { CoID, RawCoValue } from "../coValue.js";
3
7
  import {
4
8
  AgentSecret,
@@ -11,9 +15,9 @@ import {
11
15
  import { AgentID } from "../ids.js";
12
16
  import { RawCoMap } from "./coMap.js";
13
17
  import { RawGroup, InviteSecret } from "./group.js";
14
- import { LocalNode } from "../index.js";
15
18
  import { JsonObject } from "../jsonValue.js";
16
19
  import { err, ok, Result } from "neverthrow";
20
+ import { LocalNode } from "../localNode.js";
17
21
 
18
22
  export function accountHeaderForInitialAgentSecret(
19
23
  agentSecret: AgentSecret,
@@ -92,7 +96,9 @@ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
92
96
  * Creates a new group (with the current account as the group's first admin).
93
97
  * @category 1. High-level
94
98
  */
95
- createGroup(uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()) {
99
+ createGroup(
100
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique(),
101
+ ) {
96
102
  return this.core.node.createGroup(uniqueness);
97
103
  }
98
104
 
@@ -36,6 +36,11 @@ export class PureJSCrypto extends CryptoProvider<Blake3State> {
36
36
  return blake3.create({});
37
37
  }
38
38
 
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ cloneBlake3State(state: any): Blake3State {
41
+ return state.clone();
42
+ }
43
+
39
44
  blake3HashOnce(data: Uint8Array) {
40
45
  return blake3(data);
41
46
  }
@@ -64,6 +64,10 @@ export class WasmCrypto extends CryptoProvider<Uint8Array> {
64
64
  return this.blake3Instance.init().save();
65
65
  }
66
66
 
67
+ cloneBlake3State(state: Uint8Array): Uint8Array {
68
+ return this.blake3Instance.load(state).save();
69
+ }
70
+
67
71
  blake3HashOnce(data: Uint8Array) {
68
72
  return this.blake3Instance.init().update(data).digest("binary");
69
73
  }
@@ -2,7 +2,8 @@ import { JsonValue } from "../jsonValue.js";
2
2
  import { base58 } from "@scure/base";
3
3
  import { AgentID, RawCoID, TransactionID } from "../ids.js";
4
4
  import { Stringified, parseJSON, stableStringify } from "../jsonStringify.js";
5
- import { RawAccountID, SessionID } from "../index.js";
5
+ import { RawAccountID } from "../coValues/account.js";
6
+ import { SessionID } from "../ids.js";
6
7
 
7
8
  export type SignerSecret = `signerSecret_z${string}`;
8
9
  export type SignerID = `signer_z${string}`;
@@ -103,6 +104,7 @@ export abstract class CryptoProvider<Blake3State = any> {
103
104
  }
104
105
 
105
106
  abstract emptyBlake3State(): Blake3State;
107
+ abstract cloneBlake3State(state: Blake3State): Blake3State;
106
108
  abstract blake3HashOnce(data: Uint8Array): Uint8Array;
107
109
  abstract blake3HashOnceWithContext(
108
110
  data: Uint8Array,
@@ -323,7 +325,10 @@ export class StreamingHash {
323
325
  }
324
326
 
325
327
  clone(): StreamingHash {
326
- return new StreamingHash(this.crypto, new Uint8Array(this.state));
328
+ return new StreamingHash(
329
+ this.crypto,
330
+ this.crypto.cloneBlake3State(this.state),
331
+ );
327
332
  }
328
333
  }
329
334
 
@@ -1,3 +1,5 @@
1
+ import { PureJSCrypto } from "./crypto/PureJSCrypto.js";
2
+
1
3
  import {
2
4
  CoValueCore,
3
5
  type CoValueUniqueness,
@@ -16,8 +18,6 @@ import {
16
18
  StreamingHash,
17
19
  CryptoProvider,
18
20
  } from "./crypto/crypto.js";
19
- import { WasmCrypto } from "./crypto/WasmCrypto.js";
20
- import { PureJSCrypto } from "./crypto/PureJSCrypto.js";
21
21
  import { connectedPeers, Channel } from "./streamUtils.js";
22
22
  import { ControlledAgent, RawControlledAccount } from "./coValues/account.js";
23
23
  import type { Role } from "./permissions.js";
@@ -115,11 +115,10 @@ export {
115
115
  AgentSecret,
116
116
  InviteSecret,
117
117
  CryptoProvider,
118
- WasmCrypto,
119
- PureJSCrypto,
120
118
  SyncMessage,
121
119
  isRawCoID,
122
120
  LSMStorage,
121
+ PureJSCrypto,
123
122
  };
124
123
 
125
124
  export type {
@@ -131,7 +130,7 @@ export type {
131
130
  OutgoingSyncQueue,
132
131
  DisconnectedError,
133
132
  PingTimeoutError,
134
- CoValueUniqueness
133
+ CoValueUniqueness,
135
134
  };
136
135
 
137
136
  // eslint-disable-next-line @typescript-eslint/no-namespace
@@ -0,0 +1,152 @@
1
+ import { WasmCrypto } from "./crypto/WasmCrypto.js";
2
+
3
+ import {
4
+ CoValueCore,
5
+ type CoValueUniqueness,
6
+ MAX_RECOMMENDED_TX_SIZE,
7
+ idforHeader,
8
+ } from "./coValueCore.js";
9
+ import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
10
+ import { LocalNode } from "./localNode.js";
11
+ import { type RawCoValue } from "./coValue.js";
12
+ import { RawCoMap } from "./coValues/coMap.js";
13
+ import { RawCoList } from "./coValues/coList.js";
14
+ import { RawCoStream, RawBinaryCoStream } from "./coValues/coStream.js";
15
+ import {
16
+ secretSeedLength,
17
+ shortHashLength,
18
+ StreamingHash,
19
+ CryptoProvider,
20
+ } from "./crypto/crypto.js";
21
+ import { connectedPeers, Channel } from "./streamUtils.js";
22
+ import { ControlledAgent, RawControlledAccount } from "./coValues/account.js";
23
+ import type { Role } from "./permissions.js";
24
+ import { rawCoIDtoBytes, rawCoIDfromBytes, isRawCoID } from "./ids.js";
25
+ import { RawGroup, EVERYONE } from "./coValues/group.js";
26
+ import type { Everyone } from "./coValues/group.js";
27
+ import { base64URLtoBytes, bytesToBase64url } from "./base64url.js";
28
+ import { parseJSON } from "./jsonStringify.js";
29
+ import {
30
+ RawAccount,
31
+ RawProfile,
32
+ accountHeaderForInitialAgentSecret,
33
+ } from "./coValues/account.js";
34
+ import { expectGroup } from "./typeUtils/expectGroup.js";
35
+ import { isAccountID } from "./typeUtils/isAccountID.js";
36
+
37
+ import type { SessionID, AgentID } from "./ids.js";
38
+ import type { CoID, AnyRawCoValue } from "./coValue.js";
39
+ import type {
40
+ BinaryStreamInfo,
41
+ BinaryCoStreamMeta,
42
+ } from "./coValues/coStream.js";
43
+ import type { JsonValue } from "./jsonValue.js";
44
+ import type {
45
+ SyncMessage,
46
+ Peer,
47
+ IncomingSyncStream,
48
+ OutgoingSyncQueue,
49
+ } from "./sync.js";
50
+ import { DisconnectedError, PingTimeoutError } from "./sync.js";
51
+ import type { AgentSecret } from "./crypto/crypto.js";
52
+ import type {
53
+ RawAccountID,
54
+ AccountMeta,
55
+ RawAccountMigration,
56
+ } from "./coValues/account.js";
57
+ import type { InviteSecret } from "./coValues/group.js";
58
+ import type * as Media from "./media.js";
59
+
60
+ type Value = JsonValue | AnyRawCoValue;
61
+
62
+ import { LSMStorage, BlockFilename, WalFilename } from "./storage/index.js";
63
+ import { FileSystem } from "./storage/FileSystem.js";
64
+ import { getPriorityFromHeader } from "./priority.js";
65
+
66
+ /** @hidden */
67
+ export const cojsonInternals = {
68
+ connectedPeers,
69
+ rawCoIDtoBytes,
70
+ rawCoIDfromBytes,
71
+ secretSeedLength,
72
+ shortHashLength,
73
+ expectGroup,
74
+ base64URLtoBytes,
75
+ bytesToBase64url,
76
+ parseJSON,
77
+ accountOrAgentIDfromSessionID,
78
+ isAccountID,
79
+ accountHeaderForInitialAgentSecret,
80
+ idforHeader,
81
+ StreamingHash,
82
+ Channel,
83
+ getPriorityFromHeader,
84
+ };
85
+
86
+ export {
87
+ LocalNode,
88
+ RawGroup,
89
+ Role,
90
+ EVERYONE,
91
+ Everyone,
92
+ RawCoMap,
93
+ RawCoList,
94
+ RawCoStream,
95
+ RawBinaryCoStream,
96
+ RawCoValue,
97
+ CoID,
98
+ AnyRawCoValue,
99
+ RawAccount,
100
+ RawAccountID,
101
+ AccountMeta,
102
+ RawAccountMigration,
103
+ RawProfile as Profile,
104
+ SessionID,
105
+ Media,
106
+ CoValueCore,
107
+ ControlledAgent,
108
+ RawControlledAccount,
109
+ MAX_RECOMMENDED_TX_SIZE,
110
+ JsonValue,
111
+ Peer,
112
+ BinaryStreamInfo,
113
+ BinaryCoStreamMeta,
114
+ AgentID,
115
+ AgentSecret,
116
+ InviteSecret,
117
+ CryptoProvider,
118
+ SyncMessage,
119
+ isRawCoID,
120
+ LSMStorage,
121
+ WasmCrypto,
122
+ };
123
+
124
+ export type {
125
+ Value,
126
+ FileSystem,
127
+ BlockFilename,
128
+ WalFilename,
129
+ IncomingSyncStream,
130
+ OutgoingSyncQueue,
131
+ DisconnectedError,
132
+ PingTimeoutError,
133
+ CoValueUniqueness,
134
+ };
135
+
136
+ // eslint-disable-next-line @typescript-eslint/no-namespace
137
+ export namespace CojsonInternalTypes {
138
+ export type CoValueKnownState = import("./sync.js").CoValueKnownState;
139
+ export type DoneMessage = import("./sync.js").DoneMessage;
140
+ export type KnownStateMessage = import("./sync.js").KnownStateMessage;
141
+ export type LoadMessage = import("./sync.js").LoadMessage;
142
+ export type NewContentMessage = import("./sync.js").NewContentMessage;
143
+ export type CoValueHeader = import("./coValueCore.js").CoValueHeader;
144
+ export type Transaction = import("./coValueCore.js").Transaction;
145
+ export type TransactionID = import("./ids.js").TransactionID;
146
+ export type Signature = import("./crypto/crypto.js").Signature;
147
+ export type RawCoID = import("./ids.js").RawCoID;
148
+ export type ProfileShape = import("./coValues/account.js").ProfileShape;
149
+ export type SealerSecret = import("./crypto/crypto.js").SealerSecret;
150
+ export type SignerSecret = import("./crypto/crypto.js").SignerSecret;
151
+ export type JsonObject = import("./jsonValue.js").JsonObject;
152
+ }
package/src/jsonValue.ts CHANGED
@@ -4,3 +4,28 @@ export type JsonAtom = string | number | boolean | null;
4
4
  export type JsonValue = JsonAtom | JsonArray | JsonObject | RawCoID;
5
5
  export type JsonArray = JsonValue[] | readonly JsonValue[];
6
6
  export type JsonObject = { [key: string]: JsonValue | undefined };
7
+
8
+ type AtLeastOne<T, U = {[K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U];
9
+ type ExcludeEmpty<T> = T extends AtLeastOne<T> ? T : never;
10
+
11
+ export type CoJsonValue<T> = JsonValue | CoJsonObjectWithIndex<T> | CoJsonArray<T>;
12
+ export type CoJsonArray<T> = CoJsonValue<T>[] | readonly CoJsonValue<T>[];
13
+
14
+ /**
15
+ * Since we are forcing Typescript to elaborate the indexes from the given type passing
16
+ * non-object values to CoJsonObjectWithIndex will return an empty object
17
+ * E.g.
18
+ * CoJsonObjectWithIndex<() => void> --> {}
19
+ * CoJsonObjectWithIndex<RegExp> --> {}
20
+ *
21
+ * Applying the ExcludeEmpty type here to make sure we don't accept functions or non-serializable values
22
+ */
23
+ export type CoJsonObjectWithIndex<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue1L<T[K]> | undefined }>;
24
+
25
+ /**
26
+ * Manually handling the nested interface types to not get into infinite recursion issues.
27
+ */
28
+ export type CoJsonValue1L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue2L<T[K]> | undefined }> | JsonValue;
29
+ export type CoJsonValue2L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue3L<T[K]> | undefined }> | JsonValue;
30
+ export type CoJsonValue3L<T> = ExcludeEmpty<{ [K in keyof T & string]: CoJsonValue4L<T[K]> | undefined }> | JsonValue;
31
+ export type CoJsonValue4L<T> = ExcludeEmpty<{ [K in keyof T & string]: JsonValue | undefined }> | JsonValue;
package/src/localNode.ts CHANGED
@@ -21,10 +21,11 @@ import {
21
21
  ControlledAgent,
22
22
  RawAccountID,
23
23
  RawProfile,
24
+ RawProfile as Profile,
24
25
  RawAccountMigration,
25
26
  InvalidAccountAgentIDError,
26
27
  } from "./coValues/account.js";
27
- import { Profile, RawCoValue } from "./index.js";
28
+ import { RawCoValue } from "./coValue.js";
28
29
  import { expectGroup } from "./typeUtils/expectGroup.js";
29
30
  import { err, ok, okAsync, Result, ResultAsync } from "neverthrow";
30
31
 
@@ -180,66 +181,71 @@ export class LocalNode {
180
181
  crypto: CryptoProvider;
181
182
  migration?: RawAccountMigration<Meta>;
182
183
  }): Promise<LocalNode> {
183
- const loadingNode = new LocalNode(
184
- new ControlledAgent(accountSecret, crypto),
185
- crypto.newRandomSessionID(accountID),
186
- crypto,
187
- );
188
-
189
- for (const peer of peersToLoadFrom) {
190
- loadingNode.syncManager.addPeer(peer);
191
- }
184
+ try {
185
+ const loadingNode = new LocalNode(
186
+ new ControlledAgent(accountSecret, crypto),
187
+ crypto.newRandomSessionID(accountID),
188
+ crypto,
189
+ );
192
190
 
193
- const accountPromise = loadingNode.load(accountID);
191
+ for (const peer of peersToLoadFrom) {
192
+ loadingNode.syncManager.addPeer(peer);
193
+ }
194
194
 
195
- const account = await accountPromise;
195
+ const accountPromise = loadingNode.load(accountID);
196
196
 
197
- if (account === "unavailable") {
198
- throw new Error("Account unavailable from all peers");
199
- }
197
+ const account = await accountPromise;
200
198
 
201
- const controlledAccount = new RawControlledAccount(
202
- account.core,
203
- accountSecret,
204
- );
199
+ if (account === "unavailable") {
200
+ throw new Error("Account unavailable from all peers");
201
+ }
205
202
 
206
- // since this is all synchronous, we can just swap out nodes for the SyncManager
207
- const node = loadingNode.testWithDifferentAccount(
208
- controlledAccount,
209
- sessionID || crypto.newRandomSessionID(accountID),
210
- );
211
- node.syncManager = loadingNode.syncManager;
212
- node.syncManager.local = node;
203
+ const controlledAccount = new RawControlledAccount(
204
+ account.core,
205
+ accountSecret,
206
+ );
213
207
 
214
- controlledAccount.core.node = node;
215
- node.coValues[accountID] = {
216
- state: "loaded",
217
- coValue: controlledAccount.core,
218
- };
219
- controlledAccount.core._cachedContent = undefined;
208
+ // since this is all synchronous, we can just swap out nodes for the SyncManager
209
+ const node = loadingNode.testWithDifferentAccount(
210
+ controlledAccount,
211
+ sessionID || crypto.newRandomSessionID(accountID),
212
+ );
213
+ node.syncManager = loadingNode.syncManager;
214
+ node.syncManager.local = node;
215
+
216
+ controlledAccount.core.node = node;
217
+ node.coValues[accountID] = {
218
+ state: "loaded",
219
+ coValue: controlledAccount.core,
220
+ };
221
+ controlledAccount.core._cachedContent = undefined;
222
+
223
+ const profileID = account.get("profile");
224
+ if (!profileID) {
225
+ throw new Error("Account has no profile");
226
+ }
227
+ const profile = await node.load(profileID);
220
228
 
221
- const profileID = account.get("profile");
222
- if (!profileID) {
223
- throw new Error("Account has no profile");
224
- }
225
- const profile = await node.load(profileID);
229
+ if (profile === "unavailable") {
230
+ throw new Error("Profile unavailable from all peers");
231
+ }
226
232
 
227
- if (profile === "unavailable") {
228
- throw new Error("Profile unavailable from all peers");
229
- }
233
+ if (migration) {
234
+ await migration(
235
+ controlledAccount as RawControlledAccount<Meta>,
236
+ node,
237
+ );
238
+ node.account = new RawControlledAccount(
239
+ controlledAccount.core,
240
+ controlledAccount.agentSecret,
241
+ );
242
+ }
230
243
 
231
- if (migration) {
232
- await migration(
233
- controlledAccount as RawControlledAccount<Meta>,
234
- node,
235
- );
236
- node.account = new RawControlledAccount(
237
- controlledAccount.core,
238
- controlledAccount.agentSecret,
239
- );
244
+ return node;
245
+ } catch (e) {
246
+ console.error("Error withLoadedAccount", e);
247
+ throw e;
240
248
  }
241
-
242
- return node;
243
249
  }
244
250
 
245
251
  /** @internal */
@@ -540,8 +546,19 @@ export class LocalNode {
540
546
  return ok(id);
541
547
  }
542
548
 
543
- const coValue = this.expectCoValueLoaded(id, expectation);
544
-
549
+ let coValue: CoValueCore;
550
+
551
+ try {
552
+ coValue = this.expectCoValueLoaded(id, expectation);
553
+ } catch (e) {
554
+ return err({
555
+ type: "ErrorLoadingCoValueCore",
556
+ expectation,
557
+ id,
558
+ error: e,
559
+ } satisfies LoadCoValueCoreError);
560
+ }
561
+
545
562
  if (
546
563
  coValue.header.type !== "comap" ||
547
564
  coValue.header.ruleset.type !== "group" ||
@@ -606,12 +623,14 @@ export class LocalNode {
606
623
  /**
607
624
  * @deprecated use Account.createGroup() instead
608
625
  */
609
- createGroup(uniqueness: CoValueUniqueness = this.crypto.createdNowUnique()): RawGroup {
626
+ createGroup(
627
+ uniqueness: CoValueUniqueness = this.crypto.createdNowUnique(),
628
+ ): RawGroup {
610
629
  const groupCoValue = this.createCoValue({
611
630
  type: "comap",
612
631
  ruleset: { type: "group", initialAdmin: this.account.id },
613
632
  meta: null,
614
- ...uniqueness
633
+ ...uniqueness,
615
634
  });
616
635
 
617
636
  const group = expectGroup(groupCoValue.getCurrentContent());
@@ -1,5 +1,5 @@
1
1
  import { RawCoID, SessionID } from "../ids.js";
2
- import { MAX_RECOMMENDED_TX_SIZE } from "../index.js";
2
+ import { MAX_RECOMMENDED_TX_SIZE } from "../coValueCore.js";
3
3
  import { getPriorityFromHeader } from "../priority.js";
4
4
  import { CoValueKnownState, NewContentMessage } from "../sync.js";
5
5
  import { CoValueChunk } from "./index.js";
@@ -8,7 +8,7 @@ import {
8
8
  OutgoingSyncQueue,
9
9
  Peer,
10
10
  } from "../sync.js";
11
- import { CoID, RawCoValue } from "../index.js";
11
+ import { CoID, RawCoValue } from "../coValue.js";
12
12
  import { connectedPeers } from "../streamUtils.js";
13
13
  import {
14
14
  chunkToKnownState,
@@ -1,8 +1,8 @@
1
1
  import { expect, test } from "vitest";
2
2
  import { expectList } from "../coValue.js";
3
- import { WasmCrypto } from "../index.js";
4
3
  import { LocalNode } from "../localNode.js";
5
4
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
5
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
6
6
 
7
7
  const Crypto = await WasmCrypto.create();
8
8
 
@@ -1,9 +1,9 @@
1
1
  import { expect, test } from "vitest";
2
2
  import { expectMap } from "../coValue.js";
3
- import { WasmCrypto } from "../index.js";
4
3
  import { LocalNode } from "../localNode.js";
5
4
  import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
6
5
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
6
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
7
7
 
8
8
  const Crypto = await WasmCrypto.create();
9
9
 
@@ -1,9 +1,10 @@
1
1
  import { expect, test, describe } from "vitest";
2
2
  import { expectStream } from "../coValue.js";
3
3
  import { RawBinaryCoStream } from "../coValues/coStream.js";
4
- import { MAX_RECOMMENDED_TX_SIZE, WasmCrypto } from "../index.js";
4
+ import { MAX_RECOMMENDED_TX_SIZE } from "../coValueCore.js";
5
5
  import { LocalNode } from "../localNode.js";
6
6
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
7
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
7
8
 
8
9
  const Crypto = await WasmCrypto.create();
9
10
 
@@ -1,8 +1,8 @@
1
- import { KeySecret } from "../crypto/crypto.js";
1
+ import { KeySecret, StreamingHash } from "../crypto/crypto.js";
2
2
  import { WasmCrypto } from "../crypto/WasmCrypto.js";
3
3
  import { PureJSCrypto } from "../crypto/PureJSCrypto.js";
4
4
  import { describe, test, expect } from "vitest";
5
- import { SessionID } from "../index.js";
5
+ import { SessionID } from "../ids.js";
6
6
 
7
7
  describe.each([
8
8
  { impl: await WasmCrypto.create(), name: "Wasm" },
@@ -180,4 +180,26 @@ describe.each([
180
180
  ),
181
181
  ).toThrow();
182
182
  });
183
+
184
+ test("StreamingHash clone", () => {
185
+ const originalHash = new StreamingHash(impl);
186
+ originalHash.update({ foo: "bar" });
187
+
188
+ const clonedHash = originalHash.clone();
189
+
190
+ // Update the original hash
191
+ originalHash.update({ baz: "qux" });
192
+
193
+ // Update the cloned hash differently
194
+ clonedHash.update({ quux: "corge" });
195
+
196
+ // The digests should be different
197
+ expect(originalHash.digest()).not.toEqual(clonedHash.digest());
198
+
199
+ // The cloned hash should match a new hash with the same updates
200
+ const newHash = new StreamingHash(impl);
201
+ newHash.update({ foo: "bar" });
202
+ newHash.update({ quux: "corge" });
203
+ expect(clonedHash.digest()).toEqual(newHash.digest());
204
+ });
183
205
  });
@@ -1,13 +1,11 @@
1
1
  import { expect, test } from "vitest";
2
- import {
3
- LocalNode,
4
- RawCoMap,
5
- RawCoList,
6
- RawCoStream,
7
- RawBinaryCoStream,
8
- WasmCrypto,
9
- } from "../index.js";
10
2
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
3
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
4
+ import { LocalNode } from "../localNode.js";
5
+ import { RawCoList } from "../coValues/coList.js";
6
+ import { RawCoMap } from "../coValues/coMap.js";
7
+ import { RawCoStream } from "../coValues/coStream.js";
8
+ import { RawBinaryCoStream } from "../coValues/coStream.js";
11
9
 
12
10
  const Crypto = await WasmCrypto.create();
13
11