cojson 0.7.35 → 0.8.3

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 (222) hide show
  1. package/.turbo/turbo-build.log +2 -7
  2. package/.turbo/turbo-lint.log +4 -0
  3. package/CHANGELOG.md +13 -0
  4. package/dist/{PeerState.js → native/PeerState.js} +13 -5
  5. package/dist/native/PeerState.js.map +1 -0
  6. package/dist/native/PriorityBasedMessageQueue.js.map +1 -0
  7. package/dist/native/base64url.js.map +1 -0
  8. package/dist/native/base64url.test.js.map +1 -0
  9. package/dist/native/coValue.js.map +1 -0
  10. package/dist/{coValueCore.js → native/coValueCore.js} +0 -6
  11. package/dist/native/coValueCore.js.map +1 -0
  12. package/dist/{coValues → native/coValues}/account.js +2 -3
  13. package/dist/native/coValues/account.js.map +1 -0
  14. package/dist/native/coValues/coList.js.map +1 -0
  15. package/dist/native/coValues/coMap.js.map +1 -0
  16. package/dist/native/coValues/coStream.js.map +1 -0
  17. package/dist/{coValues → native/coValues}/group.js +8 -8
  18. package/dist/native/coValues/group.js.map +1 -0
  19. package/dist/native/coreToCoValue.js.map +1 -0
  20. package/dist/{crypto → native/crypto}/PureJSCrypto.js +4 -0
  21. package/dist/native/crypto/PureJSCrypto.js.map +1 -0
  22. package/dist/{crypto → native/crypto}/crypto.js +4 -1
  23. package/dist/native/crypto/crypto.js.map +1 -0
  24. package/dist/native/ids.js.map +1 -0
  25. package/dist/{index.js → native/index.native.js} +4 -6
  26. package/dist/native/index.native.js.map +1 -0
  27. package/dist/native/jsonStringify.js.map +1 -0
  28. package/dist/{jsonValue.js.map → native/jsonValue.js.map} +1 -1
  29. package/dist/{localNode.js → native/localNode.js} +44 -38
  30. package/dist/native/localNode.js.map +1 -0
  31. package/dist/native/media.js.map +1 -0
  32. package/dist/native/permissions.js.map +1 -0
  33. package/dist/native/priority.js.map +1 -0
  34. package/dist/native/storage/FileSystem.js.map +1 -0
  35. package/dist/{storage → native/storage}/chunksAndKnownStates.js +1 -1
  36. package/dist/native/storage/chunksAndKnownStates.js.map +1 -0
  37. package/dist/native/storage/index.js.map +1 -0
  38. package/dist/native/streamUtils.js.map +1 -0
  39. package/dist/native/sync.js.map +1 -0
  40. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
  41. package/dist/native/typeUtils/expectGroup.js.map +1 -0
  42. package/dist/native/typeUtils/isAccountID.js.map +1 -0
  43. package/dist/native/typeUtils/isCoValue.js.map +1 -0
  44. package/dist/web/PeerState.js +66 -0
  45. package/dist/web/PeerState.js.map +1 -0
  46. package/dist/web/PriorityBasedMessageQueue.js +51 -0
  47. package/dist/web/PriorityBasedMessageQueue.js.map +1 -0
  48. package/dist/web/base64url.js +59 -0
  49. package/dist/web/base64url.js.map +1 -0
  50. package/dist/web/base64url.test.js +25 -0
  51. package/dist/web/base64url.test.js.map +1 -0
  52. package/dist/web/coValue.js +19 -0
  53. package/dist/web/coValue.js.map +1 -0
  54. package/dist/web/coValueCore.js +693 -0
  55. package/dist/web/coValueCore.js.map +1 -0
  56. package/dist/web/coValues/account.js +96 -0
  57. package/dist/web/coValues/account.js.map +1 -0
  58. package/dist/web/coValues/coList.js +393 -0
  59. package/dist/web/coValues/coList.js.map +1 -0
  60. package/dist/web/coValues/coMap.js +197 -0
  61. package/dist/web/coValues/coMap.js.map +1 -0
  62. package/dist/web/coValues/coStream.js +220 -0
  63. package/dist/web/coValues/coStream.js.map +1 -0
  64. package/dist/web/coValues/group.js +250 -0
  65. package/dist/web/coValues/group.js.map +1 -0
  66. package/dist/web/coreToCoValue.js +42 -0
  67. package/dist/web/coreToCoValue.js.map +1 -0
  68. package/dist/web/crypto/PureJSCrypto.js +93 -0
  69. package/dist/web/crypto/PureJSCrypto.js.map +1 -0
  70. package/dist/{crypto → web/crypto}/WasmCrypto.js +3 -0
  71. package/dist/web/crypto/WasmCrypto.js.map +1 -0
  72. package/dist/web/crypto/crypto.js +154 -0
  73. package/dist/web/crypto/crypto.js.map +1 -0
  74. package/dist/web/ids.js +17 -0
  75. package/dist/web/ids.js.map +1 -0
  76. package/dist/web/index.web.js +40 -0
  77. package/dist/web/index.web.js.map +1 -0
  78. package/dist/web/jsonStringify.js +57 -0
  79. package/dist/web/jsonStringify.js.map +1 -0
  80. package/dist/web/jsonValue.js +2 -0
  81. package/dist/web/jsonValue.js.map +1 -0
  82. package/dist/web/localNode.js +442 -0
  83. package/dist/web/localNode.js.map +1 -0
  84. package/dist/web/media.js +2 -0
  85. package/dist/web/media.js.map +1 -0
  86. package/dist/web/permissions.js +206 -0
  87. package/dist/web/permissions.js.map +1 -0
  88. package/dist/web/priority.js +31 -0
  89. package/dist/web/priority.js.map +1 -0
  90. package/dist/web/storage/FileSystem.js +58 -0
  91. package/dist/web/storage/FileSystem.js.map +1 -0
  92. package/dist/web/storage/chunksAndKnownStates.js +100 -0
  93. package/dist/web/storage/chunksAndKnownStates.js.map +1 -0
  94. package/dist/web/storage/index.js +348 -0
  95. package/dist/web/storage/index.js.map +1 -0
  96. package/dist/web/streamUtils.js +41 -0
  97. package/dist/web/streamUtils.js.map +1 -0
  98. package/dist/web/sync.js +504 -0
  99. package/dist/web/sync.js.map +1 -0
  100. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js +5 -0
  101. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
  102. package/dist/web/typeUtils/expectGroup.js +13 -0
  103. package/dist/web/typeUtils/expectGroup.js.map +1 -0
  104. package/dist/web/typeUtils/isAccountID.js +4 -0
  105. package/dist/web/typeUtils/isAccountID.js.map +1 -0
  106. package/dist/web/typeUtils/isCoValue.js +11 -0
  107. package/dist/web/typeUtils/isCoValue.js.map +1 -0
  108. package/package.json +25 -8
  109. package/src/PeerState.ts +18 -7
  110. package/src/coValue.ts +1 -1
  111. package/src/coValueCore.ts +9 -14
  112. package/src/coValues/account.ts +12 -7
  113. package/src/coValues/coList.ts +4 -4
  114. package/src/coValues/coMap.ts +3 -3
  115. package/src/coValues/coStream.ts +8 -8
  116. package/src/coValues/group.ts +15 -11
  117. package/src/crypto/PureJSCrypto.ts +5 -0
  118. package/src/crypto/WasmCrypto.ts +4 -0
  119. package/src/crypto/crypto.ts +11 -1
  120. package/src/ids.ts +2 -2
  121. package/src/{index.ts → index.native.ts} +7 -8
  122. package/src/index.web.ts +152 -0
  123. package/src/localNode.ts +75 -67
  124. package/src/permissions.ts +5 -5
  125. package/src/storage/chunksAndKnownStates.ts +1 -1
  126. package/src/storage/index.ts +1 -1
  127. package/src/tests/account.test.ts +1 -2
  128. package/src/tests/coList.test.ts +1 -1
  129. package/src/tests/coMap.test.ts +1 -1
  130. package/src/tests/coStream.test.ts +2 -1
  131. package/src/tests/cryptoImpl.test.ts +24 -2
  132. package/src/tests/group.test.ts +6 -8
  133. package/src/tests/permissions.test.ts +90 -43
  134. package/src/tests/priority.test.ts +44 -15
  135. package/src/tests/sync.test.ts +8 -8
  136. package/src/tests/testUtils.ts +1 -2
  137. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +3 -3
  138. package/src/typeUtils/isAccountID.ts +2 -2
  139. package/tsconfig.json +5 -4
  140. package/tsconfig.native.json +12 -0
  141. package/tsconfig.web.json +11 -0
  142. package/dist/PeerState.js.map +0 -1
  143. package/dist/PriorityBasedMessageQueue.js.map +0 -1
  144. package/dist/base64url.js.map +0 -1
  145. package/dist/base64url.test.js.map +0 -1
  146. package/dist/coValue.js.map +0 -1
  147. package/dist/coValueCore.js.map +0 -1
  148. package/dist/coValues/account.js.map +0 -1
  149. package/dist/coValues/coList.js.map +0 -1
  150. package/dist/coValues/coMap.js.map +0 -1
  151. package/dist/coValues/coStream.js.map +0 -1
  152. package/dist/coValues/group.js.map +0 -1
  153. package/dist/coreToCoValue.js.map +0 -1
  154. package/dist/crypto/PureJSCrypto.js.map +0 -1
  155. package/dist/crypto/WasmCrypto.js.map +0 -1
  156. package/dist/crypto/crypto.js.map +0 -1
  157. package/dist/ids.js.map +0 -1
  158. package/dist/index.js.map +0 -1
  159. package/dist/jsonStringify.js.map +0 -1
  160. package/dist/localNode.js.map +0 -1
  161. package/dist/media.js.map +0 -1
  162. package/dist/permissions.js.map +0 -1
  163. package/dist/priority.js.map +0 -1
  164. package/dist/storage/FileSystem.js.map +0 -1
  165. package/dist/storage/chunksAndKnownStates.js.map +0 -1
  166. package/dist/storage/index.js.map +0 -1
  167. package/dist/streamUtils.js.map +0 -1
  168. package/dist/sync.js.map +0 -1
  169. package/dist/tests/PeerState.test.js +0 -80
  170. package/dist/tests/PeerState.test.js.map +0 -1
  171. package/dist/tests/PriorityBasedMessageQueue.test.js +0 -97
  172. package/dist/tests/PriorityBasedMessageQueue.test.js.map +0 -1
  173. package/dist/tests/account.test.js +0 -59
  174. package/dist/tests/account.test.js.map +0 -1
  175. package/dist/tests/coList.test.js +0 -76
  176. package/dist/tests/coList.test.js.map +0 -1
  177. package/dist/tests/coMap.test.js +0 -136
  178. package/dist/tests/coMap.test.js.map +0 -1
  179. package/dist/tests/coStream.test.js +0 -205
  180. package/dist/tests/coStream.test.js.map +0 -1
  181. package/dist/tests/coValueCore.test.js +0 -124
  182. package/dist/tests/coValueCore.test.js.map +0 -1
  183. package/dist/tests/crypto.test.js +0 -118
  184. package/dist/tests/crypto.test.js.map +0 -1
  185. package/dist/tests/cryptoImpl.test.js +0 -113
  186. package/dist/tests/cryptoImpl.test.js.map +0 -1
  187. package/dist/tests/group.test.js +0 -34
  188. package/dist/tests/group.test.js.map +0 -1
  189. package/dist/tests/permissions.test.js +0 -1060
  190. package/dist/tests/permissions.test.js.map +0 -1
  191. package/dist/tests/priority.test.js +0 -61
  192. package/dist/tests/priority.test.js.map +0 -1
  193. package/dist/tests/sync.test.js +0 -1187
  194. package/dist/tests/sync.test.js.map +0 -1
  195. package/dist/tests/testUtils.js +0 -60
  196. package/dist/tests/testUtils.js.map +0 -1
  197. package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +0 -1
  198. package/dist/typeUtils/expectGroup.js.map +0 -1
  199. package/dist/typeUtils/isAccountID.js.map +0 -1
  200. package/dist/typeUtils/isCoValue.js.map +0 -1
  201. /package/dist/{PriorityBasedMessageQueue.js → native/PriorityBasedMessageQueue.js} +0 -0
  202. /package/dist/{base64url.js → native/base64url.js} +0 -0
  203. /package/dist/{base64url.test.js → native/base64url.test.js} +0 -0
  204. /package/dist/{coValue.js → native/coValue.js} +0 -0
  205. /package/dist/{coValues → native/coValues}/coList.js +0 -0
  206. /package/dist/{coValues → native/coValues}/coMap.js +0 -0
  207. /package/dist/{coValues → native/coValues}/coStream.js +0 -0
  208. /package/dist/{coreToCoValue.js → native/coreToCoValue.js} +0 -0
  209. /package/dist/{ids.js → native/ids.js} +0 -0
  210. /package/dist/{jsonStringify.js → native/jsonStringify.js} +0 -0
  211. /package/dist/{jsonValue.js → native/jsonValue.js} +0 -0
  212. /package/dist/{media.js → native/media.js} +0 -0
  213. /package/dist/{permissions.js → native/permissions.js} +0 -0
  214. /package/dist/{priority.js → native/priority.js} +0 -0
  215. /package/dist/{storage → native/storage}/FileSystem.js +0 -0
  216. /package/dist/{storage → native/storage}/index.js +0 -0
  217. /package/dist/{streamUtils.js → native/streamUtils.js} +0 -0
  218. /package/dist/{sync.js → native/sync.js} +0 -0
  219. /package/dist/{typeUtils → native/typeUtils}/accountOrAgentIDfromSessionID.js +0 -0
  220. /package/dist/{typeUtils → native/typeUtils}/expectGroup.js +0 -0
  221. /package/dist/{typeUtils → native/typeUtils}/isAccountID.js +0 -0
  222. /package/dist/{typeUtils → native/typeUtils}/isCoValue.js +0 -0
package/src/PeerState.ts CHANGED
@@ -7,12 +7,25 @@ import {
7
7
  import { CoValueKnownState, Peer, SyncMessage } from "./sync.js";
8
8
 
9
9
  export class PeerState {
10
+ constructor(private peer: Peer) {}
11
+
10
12
  readonly optimisticKnownStates: { [id: RawCoID]: CoValueKnownState } = {};
11
13
  readonly toldKnownState: Set<RawCoID> = new Set();
12
- readonly id = this.peer.id;
13
- readonly role = this.peer.role;
14
- readonly priority = this.peer.priority;
15
- readonly crashOnClose = this.peer.crashOnClose;
14
+ get id() {
15
+ return this.peer.id;
16
+ }
17
+
18
+ get role() {
19
+ return this.peer.role;
20
+ }
21
+
22
+ get priority() {
23
+ return this.peer.priority;
24
+ }
25
+
26
+ get crashOnClose() {
27
+ return this.peer.crashOnClose;
28
+ }
16
29
 
17
30
  /**
18
31
  * We set as default priority HIGH to handle all the messages without a
@@ -24,8 +37,6 @@ export class PeerState {
24
37
  private processing = false;
25
38
  public closed = false;
26
39
 
27
- constructor(private peer: Peer) {}
28
-
29
40
  async processQueue() {
30
41
  if (this.processing) {
31
42
  return;
@@ -37,7 +48,7 @@ export class PeerState {
37
48
  let entry: QueueEntry | undefined;
38
49
  while ((entry = this.queue.pull())) {
39
50
  // Awaiting the push to send one message at a time
40
- // This way when the peer is "under pressure" we can enqueue all
51
+ // This way when the peer is "under pressure" we can enqueue all
41
52
  // the coming messages and organize them by priority
42
53
  await this.peer.outgoing
43
54
  .push(entry.msg)
package/src/coValue.ts CHANGED
@@ -5,7 +5,7 @@ import { RawBinaryCoStream, RawCoStream } from "./coValues/coStream.js";
5
5
  import { RawCoList } from "./coValues/coList.js";
6
6
  import { CoValueCore } from "./coValueCore.js";
7
7
  import { RawGroup } from "./coValues/group.js";
8
- import { RawAccount, Profile } from "./index.js";
8
+ import { RawAccount, RawProfile as Profile } from "./coValues/account.js";
9
9
 
10
10
  export type CoID<T extends RawCoValue> = RawCoID & {
11
11
  readonly __type: T;
@@ -10,7 +10,6 @@ import {
10
10
  SignerID,
11
11
  } from "./crypto/crypto.js";
12
12
  import { JsonObject, JsonValue } from "./jsonValue.js";
13
- import { base58 } from "@scure/base";
14
13
  import {
15
14
  PermissionsDef as RulesetDef,
16
15
  determineValidTransactions,
@@ -19,8 +18,8 @@ import {
19
18
  import { RawGroup } from "./coValues/group.js";
20
19
  import { LocalNode, ResolveAccountAgentError } from "./localNode.js";
21
20
  import { CoValueKnownState, NewContentMessage } from "./sync.js";
22
- import { AgentID, RawCoID, SessionID, TransactionID } from "./ids.js";
23
- import { AccountID, ControlledAccountOrAgent } from "./coValues/account.js";
21
+ import { RawCoID, SessionID, TransactionID } from "./ids.js";
22
+ import { RawAccountID, ControlledAccountOrAgent } from "./coValues/account.js";
24
23
  import { Stringified, parseJSON, stableStringify } from "./jsonStringify.js";
25
24
  import { coreToCoValue } from "./coreToCoValue.js";
26
25
  import { expectGroup } from "./typeUtils/expectGroup.js";
@@ -42,8 +41,11 @@ export type CoValueHeader = {
42
41
  type: AnyRawCoValue["type"];
43
42
  ruleset: RulesetDef;
44
43
  meta: JsonObject | null;
45
- createdAt: `2${string}` | null;
46
- uniqueness: `z${string}` | null;
44
+ } & CoValueUniqueness;
45
+
46
+ export type CoValueUniqueness = {
47
+ uniqueness: JsonValue;
48
+ createdAt?: `2${string}` | null;
47
49
  };
48
50
 
49
51
  export function idforHeader(
@@ -54,13 +56,6 @@ export function idforHeader(
54
56
  return `co_z${hash.slice("shortHash_z".length)}`;
55
57
  }
56
58
 
57
- export function newRandomSessionID(accountID: AccountID | AgentID): SessionID {
58
- return `${accountID}_session_z${base58.encode(
59
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
- (globalThis as any).crypto.getRandomValues(new Uint8Array(8)),
61
- )}`;
62
- }
63
-
64
59
  type SessionLog = {
65
60
  transactions: Transaction[];
66
61
  lastHash?: Hash;
@@ -993,7 +988,7 @@ export class CoValueCore {
993
988
  return this.header.ruleset.type === "group"
994
989
  ? expectGroup(this.getCurrentContent())
995
990
  .keys()
996
- .filter((k): k is AccountID => k.startsWith("co_"))
991
+ .filter((k): k is RawAccountID => k.startsWith("co_"))
997
992
  : this.header.ruleset.type === "ownedByGroup"
998
993
  ? [
999
994
  this.header.ruleset.group,
@@ -1005,7 +1000,7 @@ export class CoValueCore {
1005
1000
  ),
1006
1001
  )
1007
1002
  .filter(
1008
- (session): session is AccountID =>
1003
+ (session): session is RawAccountID =>
1009
1004
  isAccountID(session) && session !== this.id,
1010
1005
  ),
1011
1006
  ),
@@ -1,4 +1,8 @@
1
- import { CoValueCore, CoValueHeader } 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,
@@ -63,7 +67,7 @@ export class RawAccount<
63
67
  }
64
68
 
65
69
  export interface ControlledAccountOrAgent {
66
- id: AccountID | AgentID;
70
+ id: RawAccountID | AgentID;
67
71
  agentSecret: AgentSecret;
68
72
 
69
73
  currentAgentID: () => Result<AgentID, InvalidAccountAgentIDError>;
@@ -92,8 +96,10 @@ 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() {
96
- return this.core.node.createGroup();
99
+ createGroup(
100
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique(),
101
+ ) {
102
+ return this.core.node.createGroup(uniqueness);
97
103
  }
98
104
 
99
105
  async acceptInvite<T extends RawCoValue>(
@@ -133,7 +139,6 @@ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
133
139
  }
134
140
  }
135
141
 
136
- /** @hidden */
137
142
  export class ControlledAgent implements ControlledAccountOrAgent {
138
143
  constructor(
139
144
  public agentSecret: AgentSecret,
@@ -170,7 +175,7 @@ export class ControlledAgent implements ControlledAccountOrAgent {
170
175
  }
171
176
 
172
177
  export type AccountMeta = { type: "account" };
173
- export type AccountID = CoID<RawAccount>;
178
+ export type RawAccountID = CoID<RawAccount>;
174
179
 
175
180
  export type ProfileShape = {
176
181
  name: string;
@@ -4,7 +4,7 @@ import { isCoValue } from "../typeUtils/isCoValue.js";
4
4
  import { CoValueCore } from "../coValueCore.js";
5
5
  import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
6
6
  import { AgentID, SessionID, TransactionID } from "../ids.js";
7
- import { AccountID } from "./account.js";
7
+ import { RawAccountID } from "./account.js";
8
8
  import { RawGroup } from "./group.js";
9
9
 
10
10
  type OpID = TransactionID & { changeIdx: number };
@@ -325,7 +325,7 @@ export class RawCoListView<
325
325
  /** @category 5. Edit history */
326
326
  editAt(idx: number):
327
327
  | {
328
- by: AccountID | AgentID;
328
+ by: RawAccountID | AgentID;
329
329
  tx: TransactionID;
330
330
  at: Date;
331
331
  value: Item;
@@ -351,13 +351,13 @@ export class RawCoListView<
351
351
 
352
352
  /** @category 5. Edit history */
353
353
  deletionEdits(): {
354
- by: AccountID | AgentID;
354
+ by: RawAccountID | AgentID;
355
355
  tx: TransactionID;
356
356
  at: Date;
357
357
  // TODO: add indices that are now before and after the deleted item
358
358
  }[] {
359
359
  const edits: {
360
- by: AccountID | AgentID;
360
+ by: RawAccountID | AgentID;
361
361
  tx: TransactionID;
362
362
  at: Date;
363
363
  }[] = [];
@@ -4,7 +4,7 @@ import { CoID, RawCoValue } from "../coValue.js";
4
4
  import { isCoValue } from "../typeUtils/isCoValue.js";
5
5
  import { CoValueCore } from "../coValueCore.js";
6
6
  import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
7
- import { AccountID } from "./account.js";
7
+ import { RawAccountID } from "./account.js";
8
8
  import type { RawGroup } from "./group.js";
9
9
 
10
10
  type MapOp<K extends string, V extends JsonValue | undefined> = {
@@ -196,7 +196,7 @@ export class RawCoMapView<
196
196
  n: number,
197
197
  ):
198
198
  | {
199
- by: AccountID | AgentID;
199
+ by: RawAccountID | AgentID;
200
200
  tx: TransactionID;
201
201
  at: Date;
202
202
  value?: Shape[K];
@@ -226,7 +226,7 @@ export class RawCoMapView<
226
226
  key: K,
227
227
  ):
228
228
  | {
229
- by: AccountID | AgentID;
229
+ by: RawAccountID | AgentID;
230
230
  tx: TransactionID;
231
231
  at: Date;
232
232
  value?: Shape[K];
@@ -7,7 +7,7 @@ import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfrom
7
7
  import { RawGroup } from "./group.js";
8
8
  import { AgentID, SessionID, TransactionID } from "../ids.js";
9
9
  import { base64URLtoBytes, bytesToBase64url } from "../base64url.js";
10
- import { AccountID } from "./account.js";
10
+ import { RawAccountID } from "./account.js";
11
11
 
12
12
  export type BinaryStreamInfo = {
13
13
  mimeType: string;
@@ -116,7 +116,7 @@ export class RawCoStreamView<
116
116
  return Object.keys(this.items) as SessionID[];
117
117
  }
118
118
 
119
- accounts(): Set<AccountID> {
119
+ accounts(): Set<RawAccountID> {
120
120
  return new Set(
121
121
  this.sessions()
122
122
  .map(accountOrAgentIDfromSessionID)
@@ -129,7 +129,7 @@ export class RawCoStreamView<
129
129
  n: number,
130
130
  ):
131
131
  | {
132
- by: AccountID | AgentID;
132
+ by: RawAccountID | AgentID;
133
133
  tx: TransactionID;
134
134
  at: Date;
135
135
  value: Item;
@@ -151,7 +151,7 @@ export class RawCoStreamView<
151
151
 
152
152
  lastItemIn(sessionID: SessionID):
153
153
  | {
154
- by: AccountID | AgentID;
154
+ by: RawAccountID | AgentID;
155
155
  tx: TransactionID;
156
156
  at: Date;
157
157
  value: Item;
@@ -175,9 +175,9 @@ export class RawCoStreamView<
175
175
  }
176
176
  }
177
177
 
178
- lastItemBy(account: AccountID | AgentID):
178
+ lastItemBy(account: RawAccountID | AgentID):
179
179
  | {
180
- by: AccountID | AgentID;
180
+ by: RawAccountID | AgentID;
181
181
  tx: TransactionID;
182
182
  at: Date;
183
183
  value: Item;
@@ -185,7 +185,7 @@ export class RawCoStreamView<
185
185
  | undefined {
186
186
  let latestItem:
187
187
  | {
188
- by: AccountID | AgentID;
188
+ by: RawAccountID | AgentID;
189
189
  tx: TransactionID;
190
190
  at: Date;
191
191
  value: Item;
@@ -210,7 +210,7 @@ export class RawCoStreamView<
210
210
  return latestItem;
211
211
  }
212
212
 
213
- *itemsBy(account: AccountID | AgentID) {
213
+ *itemsBy(account: RawAccountID | AgentID) {
214
214
  // TODO: this can be made more lazy without a huge collect and sort
215
215
  const items = [
216
216
  ...Object.keys(this.items).flatMap((sessionID) =>
@@ -5,9 +5,10 @@ import { JsonObject } from "../jsonValue.js";
5
5
  import { RawBinaryCoStream, RawCoStream } from "./coStream.js";
6
6
  import { Encrypted, KeyID, KeySecret, Sealed } from "../crypto/crypto.js";
7
7
  import { AgentID, isAgentID } from "../ids.js";
8
- import { RawAccount, AccountID, ControlledAccountOrAgent } from "./account.js";
8
+ import { RawAccount, RawAccountID, ControlledAccountOrAgent } from "./account.js";
9
9
  import { Role } from "../permissions.js";
10
10
  import { base58 } from "@scure/base";
11
+ import { CoValueUniqueness } from "../coValueCore.js";
11
12
 
12
13
  export const EVERYONE = "everyone" as const;
13
14
  export type Everyone = "everyone";
@@ -15,10 +16,10 @@ export type Everyone = "everyone";
15
16
  export type GroupShape = {
16
17
  profile: CoID<RawCoMap> | null;
17
18
  root: CoID<RawCoMap> | null;
18
- [key: AccountID | AgentID]: Role;
19
+ [key: RawAccountID | AgentID]: Role;
19
20
  [EVERYONE]?: Role;
20
21
  readKey?: KeyID;
21
- [revelationFor: `${KeyID}_for_${AccountID | AgentID}`]: Sealed<KeySecret>;
22
+ [revelationFor: `${KeyID}_for_${RawAccountID | AgentID}`]: Sealed<KeySecret>;
22
23
  [revelationFor: `${KeyID}_for_${Everyone}`]: KeySecret;
23
24
  [oldKeyForNewKey: `${KeyID}_for_${KeyID}`]: Encrypted<
24
25
  KeySecret,
@@ -55,12 +56,12 @@ export class RawGroup<
55
56
  *
56
57
  * @category 1. Role reading
57
58
  */
58
- roleOf(accountID: AccountID): Role | undefined {
59
+ roleOf(accountID: RawAccountID): Role | undefined {
59
60
  return this.roleOfInternal(accountID);
60
61
  }
61
62
 
62
63
  /** @internal */
63
- roleOfInternal(accountID: AccountID | AgentID): Role | undefined {
64
+ roleOfInternal(accountID: RawAccountID | AgentID): Role | undefined {
64
65
  return this.get(accountID);
65
66
  }
66
67
 
@@ -156,7 +157,7 @@ export class RawGroup<
156
157
  } else {
157
158
  return false;
158
159
  }
159
- }) as (AccountID | AgentID)[];
160
+ }) as (RawAccountID | AgentID)[];
160
161
 
161
162
  const maybeCurrentReadKey = this.core.getCurrentReadKey();
162
163
 
@@ -255,6 +256,7 @@ export class RawGroup<
255
256
  init?: M["_shape"],
256
257
  meta?: M["headerMeta"],
257
258
  initPrivacy: "trusting" | "private" = "private",
259
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
258
260
  ): M {
259
261
  const map = this.core.node
260
262
  .createCoValue({
@@ -264,7 +266,7 @@ export class RawGroup<
264
266
  group: this.id,
265
267
  },
266
268
  meta: meta || null,
267
- ...this.core.crypto.createdNowUnique(),
269
+ ...uniqueness
268
270
  })
269
271
  .getCurrentContent() as M;
270
272
 
@@ -287,6 +289,7 @@ export class RawGroup<
287
289
  init?: L["_item"][],
288
290
  meta?: L["headerMeta"],
289
291
  initPrivacy: "trusting" | "private" = "private",
292
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
290
293
  ): L {
291
294
  const list = this.core.node
292
295
  .createCoValue({
@@ -296,7 +299,7 @@ export class RawGroup<
296
299
  group: this.id,
297
300
  },
298
301
  meta: meta || null,
299
- ...this.core.crypto.createdNowUnique(),
302
+ ...uniqueness
300
303
  })
301
304
  .getCurrentContent() as L;
302
305
 
@@ -310,7 +313,7 @@ export class RawGroup<
310
313
  }
311
314
 
312
315
  /** @category 3. Value creation */
313
- createStream<C extends RawCoStream>(meta?: C["headerMeta"]): C {
316
+ createStream<C extends RawCoStream>(meta?: C["headerMeta"], uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()): C {
314
317
  return this.core.node
315
318
  .createCoValue({
316
319
  type: "costream",
@@ -319,7 +322,7 @@ export class RawGroup<
319
322
  group: this.id,
320
323
  },
321
324
  meta: meta || null,
322
- ...this.core.crypto.createdNowUnique(),
325
+ ...uniqueness
323
326
  })
324
327
  .getCurrentContent() as C;
325
328
  }
@@ -327,6 +330,7 @@ export class RawGroup<
327
330
  /** @category 3. Value creation */
328
331
  createBinaryStream<C extends RawBinaryCoStream>(
329
332
  meta: C["headerMeta"] = { type: "binary" },
333
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
330
334
  ): C {
331
335
  return this.core.node
332
336
  .createCoValue({
@@ -336,7 +340,7 @@ export class RawGroup<
336
340
  group: this.id,
337
341
  },
338
342
  meta: meta,
339
- ...this.core.crypto.createdNowUnique(),
343
+ ...uniqueness
340
344
  })
341
345
  .getCurrentContent() as C;
342
346
  }
@@ -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,6 +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 } from "../coValues/account.js";
6
+ import { SessionID } from "../ids.js";
5
7
 
6
8
  export type SignerSecret = `signerSecret_z${string}`;
7
9
  export type SignerID = `signer_z${string}`;
@@ -102,6 +104,7 @@ export abstract class CryptoProvider<Blake3State = any> {
102
104
  }
103
105
 
104
106
  abstract emptyBlake3State(): Blake3State;
107
+ abstract cloneBlake3State(state: Blake3State): Blake3State;
105
108
  abstract blake3HashOnce(data: Uint8Array): Uint8Array;
106
109
  abstract blake3HashOnceWithContext(
107
110
  data: Uint8Array,
@@ -290,6 +293,10 @@ export abstract class CryptoProvider<Blake3State = any> {
290
293
  }),
291
294
  )}`;
292
295
  }
296
+
297
+ newRandomSessionID(accountID: RawAccountID | AgentID): SessionID {
298
+ return `${accountID}_session_z${base58.encode(this.randomBytes(8))}`;
299
+ }
293
300
  }
294
301
 
295
302
  export type Hash = `hash_z${string}`;
@@ -318,7 +325,10 @@ export class StreamingHash {
318
325
  }
319
326
 
320
327
  clone(): StreamingHash {
321
- return new StreamingHash(this.crypto, new Uint8Array(this.state));
328
+ return new StreamingHash(
329
+ this.crypto,
330
+ this.crypto.cloneBlake3State(this.state),
331
+ );
322
332
  }
323
333
  }
324
334
 
package/src/ids.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AccountID } from "./coValues/account.js";
1
+ import { RawAccountID } from "./coValues/account.js";
2
2
  import { base58 } from "@scure/base";
3
3
  import { shortHashLength } from "./crypto/crypto.js";
4
4
 
@@ -28,4 +28,4 @@ export function isAgentID(id: string): id is AgentID {
28
28
  );
29
29
  }
30
30
 
31
- export type SessionID = `${AccountID | AgentID}_session_z${string}`;
31
+ export type SessionID = `${RawAccountID | AgentID}_session_z${string}`;
@@ -1,6 +1,8 @@
1
+ import { PureJSCrypto } from "./crypto/PureJSCrypto.js";
2
+
1
3
  import {
2
4
  CoValueCore,
3
- newRandomSessionID,
5
+ type CoValueUniqueness,
4
6
  MAX_RECOMMENDED_TX_SIZE,
5
7
  idforHeader,
6
8
  } from "./coValueCore.js";
@@ -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";
@@ -50,7 +50,7 @@ import type {
50
50
  import { DisconnectedError, PingTimeoutError } from "./sync.js";
51
51
  import type { AgentSecret } from "./crypto/crypto.js";
52
52
  import type {
53
- AccountID,
53
+ RawAccountID,
54
54
  AccountMeta,
55
55
  RawAccountMigration,
56
56
  } from "./coValues/account.js";
@@ -65,7 +65,6 @@ import { getPriorityFromHeader } from "./priority.js";
65
65
 
66
66
  /** @hidden */
67
67
  export const cojsonInternals = {
68
- newRandomSessionID,
69
68
  connectedPeers,
70
69
  rawCoIDtoBytes,
71
70
  rawCoIDfromBytes,
@@ -98,7 +97,7 @@ export {
98
97
  CoID,
99
98
  AnyRawCoValue,
100
99
  RawAccount,
101
- AccountID,
100
+ RawAccountID,
102
101
  AccountMeta,
103
102
  RawAccountMigration,
104
103
  RawProfile as Profile,
@@ -116,11 +115,10 @@ export {
116
115
  AgentSecret,
117
116
  InviteSecret,
118
117
  CryptoProvider,
119
- WasmCrypto,
120
- PureJSCrypto,
121
118
  SyncMessage,
122
119
  isRawCoID,
123
120
  LSMStorage,
121
+ PureJSCrypto,
124
122
  };
125
123
 
126
124
  export type {
@@ -132,6 +130,7 @@ export type {
132
130
  OutgoingSyncQueue,
133
131
  DisconnectedError,
134
132
  PingTimeoutError,
133
+ CoValueUniqueness,
135
134
  };
136
135
 
137
136
  // eslint-disable-next-line @typescript-eslint/no-namespace