cojson 0.7.0-alpha.37 → 0.7.0-alpha.38

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 (106) hide show
  1. package/.eslintrc.cjs +3 -2
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +1 -34
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +1106 -0
  6. package/CHANGELOG.md +6 -0
  7. package/README.md +3 -1
  8. package/dist/base64url.test.js +25 -0
  9. package/dist/base64url.test.js.map +1 -0
  10. package/dist/coValueCore.js +16 -15
  11. package/dist/coValueCore.js.map +1 -1
  12. package/dist/coValues/account.js +16 -15
  13. package/dist/coValues/account.js.map +1 -1
  14. package/dist/coValues/group.js +13 -14
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/coreToCoValue.js.map +1 -1
  17. package/dist/crypto/PureJSCrypto.js +89 -0
  18. package/dist/crypto/PureJSCrypto.js.map +1 -0
  19. package/dist/crypto/WasmCrypto.js +127 -0
  20. package/dist/crypto/WasmCrypto.js.map +1 -0
  21. package/dist/crypto/crypto.js +151 -0
  22. package/dist/crypto/crypto.js.map +1 -0
  23. package/dist/ids.js +4 -2
  24. package/dist/ids.js.map +1 -1
  25. package/dist/index.js +4 -8
  26. package/dist/index.js.map +1 -1
  27. package/dist/jsonStringify.js.map +1 -1
  28. package/dist/localNode.js +24 -24
  29. package/dist/localNode.js.map +1 -1
  30. package/dist/permissions.js.map +1 -1
  31. package/dist/storage/FileSystem.js +2 -2
  32. package/dist/storage/FileSystem.js.map +1 -1
  33. package/dist/storage/chunksAndKnownStates.js +2 -2
  34. package/dist/storage/chunksAndKnownStates.js.map +1 -1
  35. package/dist/storage/index.js.map +1 -1
  36. package/dist/sync.js +6 -2
  37. package/dist/sync.js.map +1 -1
  38. package/dist/tests/account.test.js +58 -0
  39. package/dist/tests/account.test.js.map +1 -0
  40. package/dist/tests/coList.test.js +76 -0
  41. package/dist/tests/coList.test.js.map +1 -0
  42. package/dist/tests/coMap.test.js +136 -0
  43. package/dist/tests/coMap.test.js.map +1 -0
  44. package/dist/tests/coStream.test.js +172 -0
  45. package/dist/tests/coStream.test.js.map +1 -0
  46. package/dist/tests/coValueCore.test.js +114 -0
  47. package/dist/tests/coValueCore.test.js.map +1 -0
  48. package/dist/tests/crypto.test.js +118 -0
  49. package/dist/tests/crypto.test.js.map +1 -0
  50. package/dist/tests/cryptoImpl.test.js +113 -0
  51. package/dist/tests/cryptoImpl.test.js.map +1 -0
  52. package/dist/tests/group.test.js +34 -0
  53. package/dist/tests/group.test.js.map +1 -0
  54. package/dist/tests/permissions.test.js +1060 -0
  55. package/dist/tests/permissions.test.js.map +1 -0
  56. package/dist/tests/sync.test.js +816 -0
  57. package/dist/tests/sync.test.js.map +1 -0
  58. package/dist/tests/testUtils.js +10 -9
  59. package/dist/tests/testUtils.js.map +1 -1
  60. package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  61. package/dist/typeUtils/isAccountID.js.map +1 -1
  62. package/dist/typeUtils/isCoValue.js.map +1 -1
  63. package/package.json +14 -28
  64. package/src/base64url.test.ts +6 -6
  65. package/src/coValue.ts +1 -1
  66. package/src/coValueCore.ts +87 -85
  67. package/src/coValues/account.ts +26 -28
  68. package/src/coValues/coList.ts +10 -10
  69. package/src/coValues/coMap.ts +10 -10
  70. package/src/coValues/coStream.ts +17 -17
  71. package/src/coValues/group.ts +93 -109
  72. package/src/coreToCoValue.ts +5 -2
  73. package/src/crypto/PureJSCrypto.ts +200 -0
  74. package/src/crypto/WasmCrypto.ts +259 -0
  75. package/src/crypto/crypto.ts +336 -0
  76. package/src/ids.ts +8 -7
  77. package/src/index.ts +11 -19
  78. package/src/jsonStringify.ts +6 -4
  79. package/src/jsonValue.ts +2 -2
  80. package/src/localNode.ts +86 -80
  81. package/src/media.ts +3 -3
  82. package/src/permissions.ts +14 -16
  83. package/src/storage/FileSystem.ts +31 -30
  84. package/src/storage/chunksAndKnownStates.ts +24 -17
  85. package/src/storage/index.ts +41 -37
  86. package/src/streamUtils.ts +12 -12
  87. package/src/sync.ts +56 -40
  88. package/src/tests/account.test.ts +8 -12
  89. package/src/tests/coList.test.ts +19 -25
  90. package/src/tests/coMap.test.ts +25 -30
  91. package/src/tests/coStream.test.ts +28 -38
  92. package/src/tests/coValueCore.test.ts +35 -36
  93. package/src/tests/crypto.test.ts +66 -72
  94. package/src/tests/cryptoImpl.test.ts +183 -0
  95. package/src/tests/group.test.ts +16 -17
  96. package/src/tests/permissions.test.ts +237 -254
  97. package/src/tests/sync.test.ts +119 -120
  98. package/src/tests/testUtils.ts +22 -19
  99. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +1 -2
  100. package/src/typeUtils/expectGroup.ts +1 -1
  101. package/src/typeUtils/isAccountID.ts +0 -1
  102. package/src/typeUtils/isCoValue.ts +1 -2
  103. package/tsconfig.json +0 -1
  104. package/dist/crypto.js +0 -255
  105. package/dist/crypto.js.map +0 -1
  106. package/src/crypto.ts +0 -485
package/src/sync.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Signature } from "./crypto.js";
1
+ import { Signature } from "./crypto/crypto.js";
2
2
  import { CoValueHeader, Transaction } from "./coValueCore.js";
3
3
  import { CoValueCore } from "./coValueCore.js";
4
4
  import { LocalNode } from "./localNode.js";
@@ -82,7 +82,7 @@ export interface PeerState {
82
82
 
83
83
  export function combinedKnownStates(
84
84
  stateA: CoValueKnownState,
85
- stateB: CoValueKnownState
85
+ stateB: CoValueKnownState,
86
86
  ): CoValueKnownState {
87
87
  const sessionStates: CoValueKnownState["sessions"] = {};
88
88
 
@@ -108,7 +108,11 @@ export function combinedKnownStates(
108
108
  export class SyncManager {
109
109
  peers: { [key: PeerID]: PeerState } = {};
110
110
  local: LocalNode;
111
- requestedSyncs: { [id: RawCoID]: {done: Promise<void>, nRequestsThisTick: number} | undefined } = {};
111
+ requestedSyncs: {
112
+ [id: RawCoID]:
113
+ | { done: Promise<void>; nRequestsThisTick: number }
114
+ | undefined;
115
+ } = {};
112
116
 
113
117
  constructor(local: LocalNode) {
114
118
  this.local = local;
@@ -175,7 +179,7 @@ export class SyncManager {
175
179
  id,
176
180
  "from",
177
181
  peer.id,
178
- e
182
+ e,
179
183
  );
180
184
  resolve();
181
185
  });
@@ -203,7 +207,7 @@ export class SyncManager {
203
207
  throw new Error(
204
208
  `Unknown message type ${
205
209
  (msg as { action: "string" }).action
206
- }`
210
+ }`,
207
211
  );
208
212
  }
209
213
  }
@@ -243,15 +247,21 @@ export class SyncManager {
243
247
  async tellUntoldKnownStateIncludingDependencies(
244
248
  id: RawCoID,
245
249
  peer: PeerState,
246
- asDependencyOf?: RawCoID
250
+ asDependencyOf?: RawCoID,
247
251
  ) {
248
252
  const coValue = this.local.expectCoValueLoaded(id);
249
253
 
250
- await Promise.all(coValue.getDependedOnCoValues().map(dependentCoID => this.tellUntoldKnownStateIncludingDependencies(
251
- dependentCoID,
252
- peer,
253
- asDependencyOf || id
254
- )));
254
+ await Promise.all(
255
+ coValue
256
+ .getDependedOnCoValues()
257
+ .map((dependentCoID) =>
258
+ this.tellUntoldKnownStateIncludingDependencies(
259
+ dependentCoID,
260
+ peer,
261
+ asDependencyOf || id,
262
+ ),
263
+ ),
264
+ );
255
265
 
256
266
  if (!peer.toldKnownState.has(id)) {
257
267
  await this.trySendToPeer(peer, {
@@ -267,10 +277,16 @@ export class SyncManager {
267
277
  async sendNewContentIncludingDependencies(id: RawCoID, peer: PeerState) {
268
278
  const coValue = this.local.expectCoValueLoaded(id);
269
279
 
270
- await Promise.all(coValue.getDependedOnCoValues().map(id => this.sendNewContentIncludingDependencies(id, peer)));
280
+ await Promise.all(
281
+ coValue
282
+ .getDependedOnCoValues()
283
+ .map((id) =>
284
+ this.sendNewContentIncludingDependencies(id, peer),
285
+ ),
286
+ );
271
287
 
272
288
  const newContentPieces = coValue.newContentSince(
273
- peer.optimisticKnownStates[id]
289
+ peer.optimisticKnownStates[id],
274
290
  );
275
291
 
276
292
  if (newContentPieces) {
@@ -302,7 +318,7 @@ export class SyncManager {
302
318
 
303
319
  peer.optimisticKnownStates[id] = combinedKnownStates(
304
320
  optimisticKnownStateBefore,
305
- coValue.knownState()
321
+ coValue.knownState(),
306
322
  );
307
323
  }
308
324
  }
@@ -323,7 +339,7 @@ export class SyncManager {
323
339
  if (peer.role === "server") {
324
340
  const initialSync = async () => {
325
341
  for (const id of Object.keys(
326
- this.local.coValues
342
+ this.local.coValues,
327
343
  ) as RawCoID[]) {
328
344
  // console.log("subscribing to after peer added", id, peer.id)
329
345
  await this.subscribeToIncludingDependencies(id, peerState);
@@ -350,9 +366,9 @@ export class SyncManager {
350
366
  JSON.stringify(msg, (k, v) =>
351
367
  k === "changes" || k === "encryptedChanges"
352
368
  ? v.slice(0, 20) + "..."
353
- : v
369
+ : v,
354
370
  ),
355
- e
371
+ e,
356
372
  );
357
373
  });
358
374
  // await new Promise<void>((resolve) => {
@@ -365,9 +381,9 @@ export class SyncManager {
365
381
  JSON.stringify(msg, (k, v) =>
366
382
  k === "changes" || k === "encryptedChanges"
367
383
  ? v.slice(0, 20) + "..."
368
- : v
384
+ : v,
369
385
  ),
370
- e
386
+ e,
371
387
  );
372
388
  if (peerState.delayOnError) {
373
389
  await new Promise<void>((resolve) => {
@@ -417,8 +433,8 @@ export class SyncManager {
417
433
  `Error writing to peer ${peer.id}, disconnecting`,
418
434
  {
419
435
  cause: e,
420
- }
421
- )
436
+ },
437
+ ),
422
438
  );
423
439
  delete this.peers[peer.id];
424
440
  });
@@ -470,7 +486,7 @@ export class SyncManager {
470
486
 
471
487
  peer.optimisticKnownStates[msg.id] = combinedKnownStates(
472
488
  peer.optimisticKnownStates[msg.id] || emptyKnownState(msg.id),
473
- knownStateIn(msg)
489
+ knownStateIn(msg),
474
490
  );
475
491
 
476
492
  if (!entry) {
@@ -481,18 +497,18 @@ export class SyncManager {
481
497
  .catch((e) => {
482
498
  console.error(
483
499
  `Error loading coValue ${msg.id} to create loading state, as dependency of ${msg.asDependencyOf}`,
484
- e
500
+ e,
485
501
  );
486
502
  });
487
503
  entry = this.local.coValues[msg.id]!; // must exist after loadCoValueCore
488
504
  } else {
489
505
  throw new Error(
490
- "Expected coValue dependency entry to be created, missing subscribe?"
506
+ "Expected coValue dependency entry to be created, missing subscribe?",
491
507
  );
492
508
  }
493
509
  } else {
494
510
  throw new Error(
495
- "Expected coValue entry to be created, missing subscribe?"
511
+ "Expected coValue entry to be created, missing subscribe?",
496
512
  );
497
513
  }
498
514
  }
@@ -516,7 +532,7 @@ export class SyncManager {
516
532
  // );
517
533
  if (
518
534
  Object.values(entry.firstPeerState).every(
519
- (s) => s.type === "unavailable"
535
+ (s) => s.type === "unavailable",
520
536
  )
521
537
  ) {
522
538
  entry.resolve("unavailable");
@@ -533,7 +549,7 @@ export class SyncManager {
533
549
 
534
550
  if (!entry) {
535
551
  throw new Error(
536
- "Expected coValue entry to be created, missing subscribe?"
552
+ "Expected coValue entry to be created, missing subscribe?",
537
553
  );
538
554
  }
539
555
 
@@ -543,7 +559,7 @@ export class SyncManager {
543
559
 
544
560
  if (!peerOptimisticKnownState) {
545
561
  throw new Error(
546
- "Expected optimisticKnownState to be set for coValue we receive new content for"
562
+ "Expected optimisticKnownState to be set for coValue we receive new content for",
547
563
  );
548
564
  }
549
565
 
@@ -578,7 +594,7 @@ export class SyncManager {
578
594
  let invalidStateAssumed = false;
579
595
 
580
596
  for (const [sessionID, newContentForSession] of Object.entries(
581
- msg.new
597
+ msg.new,
582
598
  ) as [SessionID, SessionNewContent][]) {
583
599
  const ourKnownTxIdx =
584
600
  coValue.sessionLogs.get(sessionID)?.transactions.length;
@@ -605,7 +621,7 @@ export class SyncManager {
605
621
  sessionID,
606
622
  newTransactions,
607
623
  undefined,
608
- newContentForSession.lastSignature
624
+ newContentForSession.lastSignature,
609
625
  );
610
626
  const after = performance.now();
611
627
  if (after - before > 80) {
@@ -613,7 +629,7 @@ export class SyncManager {
613
629
  .map((t) =>
614
630
  t.privacy === "private"
615
631
  ? t.encryptedChanges.length
616
- : t.changes.length
632
+ : t.changes.length,
617
633
  )
618
634
  .reduce((a, b) => a + b, 0);
619
635
  console.log(
@@ -623,16 +639,16 @@ export class SyncManager {
623
639
  (1000 * totalTxLength) /
624
640
  (after - before) /
625
641
  (1024 * 1024)
626
- ).toFixed(2)} MB/s`
642
+ ).toFixed(2)} MB/s`,
627
643
  );
628
644
  }
629
645
 
630
646
  const theirTotalnTxs = Object.values(
631
- peer.optimisticKnownStates[msg.id]?.sessions || {}
647
+ peer.optimisticKnownStates[msg.id]?.sessions || {},
632
648
  ).reduce((sum, nTxs) => sum + nTxs, 0);
633
649
  const ourTotalnTxs = [...coValue.sessionLogs.values()].reduce(
634
650
  (sum, session) => sum + session.transactions.length,
635
- 0
651
+ 0,
636
652
  );
637
653
 
638
654
  entry.onProgress?.(ourTotalnTxs / theirTotalnTxs);
@@ -644,8 +660,8 @@ export class SyncManager {
644
660
  JSON.stringify(newTransactions, (k, v) =>
645
661
  k === "changes" || k === "encryptedChanges"
646
662
  ? v.slice(0, 20) + "..."
647
- : v
648
- )
663
+ : v,
664
+ ),
649
665
  );
650
666
  continue;
651
667
  }
@@ -653,7 +669,7 @@ export class SyncManager {
653
669
  peerOptimisticKnownState.sessions[sessionID] = Math.max(
654
670
  peerOptimisticKnownState.sessions[sessionID] || 0,
655
671
  newContentForSession.after +
656
- newContentForSession.newTransactions.length
672
+ newContentForSession.newTransactions.length,
657
673
  );
658
674
  }
659
675
 
@@ -720,17 +736,17 @@ export class SyncManager {
720
736
  if (optimisticKnownState) {
721
737
  await this.tellUntoldKnownStateIncludingDependencies(
722
738
  coValue.id,
723
- peer
739
+ peer,
724
740
  );
725
741
  await this.sendNewContentIncludingDependencies(
726
742
  coValue.id,
727
- peer
743
+ peer,
728
744
  );
729
745
  } else if (peer.role === "server") {
730
746
  await this.subscribeToIncludingDependencies(coValue.id, peer);
731
747
  await this.sendNewContentIncludingDependencies(
732
748
  coValue.id,
733
- peer
749
+ peer,
734
750
  );
735
751
  }
736
752
  }
@@ -1,23 +1,16 @@
1
- import { expect, test, beforeEach } from "vitest";
1
+ import { expect, test } from "vitest";
2
2
  import { newRandomSessionID } from "../coValueCore.js";
3
- import { cojsonReady } from "../index.js";
4
3
  import { LocalNode } from "../localNode.js";
5
4
  import { connectedPeers } from "../streamUtils.js";
5
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
6
6
 
7
- import { webcrypto } from "node:crypto";
8
- if (!("crypto" in globalThis)) {
9
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- (globalThis as any).crypto = webcrypto;
11
- }
12
-
13
- beforeEach(async () => {
14
- await cojsonReady;
15
- });
7
+ const Crypto = await WasmCrypto.create();
16
8
 
17
9
  test("Can create a node while creating a new account with profile", async () => {
18
10
  const { node, accountID, accountSecret, sessionID } =
19
11
  await LocalNode.withNewlyCreatedAccount({
20
12
  creationProps: { name: "Hermes Puggington" },
13
+ crypto: Crypto,
21
14
  });
22
15
 
23
16
  expect(node).not.toBeNull();
@@ -26,13 +19,14 @@ test("Can create a node while creating a new account with profile", async () =>
26
19
  expect(sessionID).not.toBeNull();
27
20
 
28
21
  expect(node.expectProfileLoaded(accountID).get("name")).toEqual(
29
- "Hermes Puggington"
22
+ "Hermes Puggington",
30
23
  );
31
24
  });
32
25
 
33
26
  test("A node with an account can create groups and and objects within them", async () => {
34
27
  const { node, accountID } = await LocalNode.withNewlyCreatedAccount({
35
28
  creationProps: { name: "Hermes Puggington" },
29
+ crypto: Crypto,
36
30
  });
37
31
 
38
32
  const group = await node.createGroup();
@@ -48,6 +42,7 @@ test("Can create account with one node, and then load it on another", async () =
48
42
  const { node, accountID, accountSecret } =
49
43
  await LocalNode.withNewlyCreatedAccount({
50
44
  creationProps: { name: "Hermes Puggington" },
45
+ crypto: Crypto,
51
46
  });
52
47
 
53
48
  const group = await node.createGroup();
@@ -70,6 +65,7 @@ test("Can create account with one node, and then load it on another", async () =
70
65
  accountSecret,
71
66
  sessionID: newRandomSessionID(accountID),
72
67
  peersToLoadFrom: [node1asPeer],
68
+ crypto: Crypto,
73
69
  });
74
70
 
75
71
  const map2 = await node2.load(map.id);
@@ -1,30 +1,19 @@
1
- import { expect, test, beforeEach } from "vitest";
2
- import { expectList, expectMap, expectStream } from "../coValue.js";
3
- import { RawBinaryCoStream } from "../coValues/coStream.js";
4
- import { createdNowUnique } from "../crypto.js";
5
- import { MAX_RECOMMENDED_TX_SIZE, cojsonReady } from "../index.js";
1
+ import { expect, test } from "vitest";
2
+ import { expectList } from "../coValue.js";
3
+ import { WasmCrypto } from "../index.js";
6
4
  import { LocalNode } from "../localNode.js";
7
- import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
8
5
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
9
6
 
10
- import { webcrypto } from "node:crypto";
11
- if (!("crypto" in globalThis)) {
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- (globalThis as any).crypto = webcrypto;
14
- }
15
-
16
- beforeEach(async () => {
17
- await cojsonReady;
18
- });
7
+ const Crypto = await WasmCrypto.create();
19
8
 
20
9
  test("Empty CoList works", () => {
21
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
10
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
22
11
 
23
12
  const coValue = node.createCoValue({
24
13
  type: "colist",
25
14
  ruleset: { type: "unsafeAllowAll" },
26
15
  meta: null,
27
- ...createdNowUnique(),
16
+ ...Crypto.createdNowUnique(),
28
17
  });
29
18
 
30
19
  const content = expectList(coValue.getCurrentContent());
@@ -34,13 +23,13 @@ test("Empty CoList works", () => {
34
23
  });
35
24
 
36
25
  test("Can append, prepend, delete and replace items in CoList", () => {
37
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
26
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
38
27
 
39
28
  const coValue = node.createCoValue({
40
29
  type: "colist",
41
30
  ruleset: { type: "unsafeAllowAll" },
42
31
  meta: null,
43
- ...createdNowUnique(),
32
+ ...Crypto.createdNowUnique(),
44
33
  });
45
34
 
46
35
  const content = expectList(coValue.getCurrentContent());
@@ -54,19 +43,24 @@ test("Can append, prepend, delete and replace items in CoList", () => {
54
43
  content.prepend("hooray", 3, "trusting");
55
44
  expect(content.toJSON()).toEqual(["hello", "beautiful", "world", "hooray"]);
56
45
  content.replace(2, "universe", "trusting");
57
- expect(content.toJSON()).toEqual(["hello", "beautiful", "universe", "hooray"]);
46
+ expect(content.toJSON()).toEqual([
47
+ "hello",
48
+ "beautiful",
49
+ "universe",
50
+ "hooray",
51
+ ]);
58
52
  content.delete(2, "trusting");
59
53
  expect(content.toJSON()).toEqual(["hello", "beautiful", "hooray"]);
60
54
  });
61
55
 
62
56
  test("Push is equivalent to append after last item", () => {
63
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
57
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
64
58
 
65
59
  const coValue = node.createCoValue({
66
60
  type: "colist",
67
61
  ruleset: { type: "unsafeAllowAll" },
68
62
  meta: null,
69
- ...createdNowUnique(),
63
+ ...Crypto.createdNowUnique(),
70
64
  });
71
65
 
72
66
  const content = expectList(coValue.getCurrentContent());
@@ -82,13 +76,13 @@ test("Push is equivalent to append after last item", () => {
82
76
  });
83
77
 
84
78
  test("Can push into empty list", () => {
85
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
79
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
86
80
 
87
81
  const coValue = node.createCoValue({
88
82
  type: "colist",
89
83
  ruleset: { type: "unsafeAllowAll" },
90
84
  meta: null,
91
- ...createdNowUnique(),
85
+ ...Crypto.createdNowUnique(),
92
86
  });
93
87
 
94
88
  const content = expectList(coValue.getCurrentContent());
@@ -97,4 +91,4 @@ test("Can push into empty list", () => {
97
91
 
98
92
  content.append("hello", undefined, "trusting");
99
93
  expect(content.toJSON()).toEqual(["hello"]);
100
- });
94
+ });
@@ -1,31 +1,20 @@
1
- import { expect, test, beforeEach } from "vitest";
2
- import { expectList, expectMap, expectStream } from "../coValue.js";
3
- import { RawBinaryCoStream } from "../coValues/coStream.js";
4
- import { createdNowUnique } from "../crypto.js";
5
- import { MAX_RECOMMENDED_TX_SIZE, cojsonReady } from "../index.js";
1
+ import { expect, test } from "vitest";
2
+ import { expectMap } from "../coValue.js";
3
+ import { WasmCrypto } from "../index.js";
6
4
  import { LocalNode } from "../localNode.js";
7
5
  import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
8
6
  import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
9
7
 
10
- import { webcrypto } from "node:crypto";
11
- if (!("crypto" in globalThis)) {
12
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
- (globalThis as any).crypto = webcrypto;
14
- }
15
-
16
- beforeEach(async () => {
17
- await cojsonReady;
18
- });
19
-
8
+ const Crypto = await WasmCrypto.create();
20
9
 
21
10
  test("Empty CoMap works", () => {
22
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
11
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
23
12
 
24
13
  const coValue = node.createCoValue({
25
14
  type: "comap",
26
15
  ruleset: { type: "unsafeAllowAll" },
27
16
  meta: null,
28
- ...createdNowUnique(),
17
+ ...Crypto.createdNowUnique(),
29
18
  });
30
19
 
31
20
  const content = expectMap(coValue.getCurrentContent());
@@ -36,13 +25,13 @@ test("Empty CoMap works", () => {
36
25
  });
37
26
 
38
27
  test("Can insert and delete CoMap entries in edit()", () => {
39
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
28
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
40
29
 
41
30
  const coValue = node.createCoValue({
42
31
  type: "comap",
43
32
  ruleset: { type: "unsafeAllowAll" },
44
33
  meta: null,
45
- ...createdNowUnique(),
34
+ ...Crypto.createdNowUnique(),
46
35
  });
47
36
 
48
37
  const content = expectMap(coValue.getCurrentContent());
@@ -60,13 +49,13 @@ test("Can insert and delete CoMap entries in edit()", () => {
60
49
  });
61
50
 
62
51
  test("Can get CoMap entry values at different points in time", () => {
63
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
52
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
64
53
 
65
54
  const coValue = node.createCoValue({
66
55
  type: "comap",
67
56
  ruleset: { type: "unsafeAllowAll" },
68
57
  meta: null,
69
- ...createdNowUnique(),
58
+ ...Crypto.createdNowUnique(),
70
59
  });
71
60
 
72
61
  const content = expectMap(coValue.getCurrentContent());
@@ -74,13 +63,19 @@ test("Can get CoMap entry values at different points in time", () => {
74
63
  expect(content.type).toEqual("comap");
75
64
 
76
65
  const beforeA = Date.now();
77
- while (Date.now() < beforeA + 10) {}
66
+ while (Date.now() < beforeA + 10) {
67
+ /* hot sleep */
68
+ }
78
69
  content.set("hello", "A", "trusting");
79
70
  const beforeB = Date.now();
80
- while (Date.now() < beforeB + 10) {}
71
+ while (Date.now() < beforeB + 10) {
72
+ /* hot sleep */
73
+ }
81
74
  content.set("hello", "B", "trusting");
82
75
  const beforeC = Date.now();
83
- while (Date.now() < beforeC + 10) {}
76
+ while (Date.now() < beforeC + 10) {
77
+ /* hot sleep */
78
+ }
84
79
  content.set("hello", "C", "trusting");
85
80
  expect(content.get("hello")).toEqual("C");
86
81
  expect(content.atTime(Date.now()).get("hello")).toEqual("C");
@@ -90,13 +85,13 @@ test("Can get CoMap entry values at different points in time", () => {
90
85
  });
91
86
 
92
87
  test("Can get all historic values of key in CoMap", () => {
93
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
88
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
94
89
 
95
90
  const coValue = node.createCoValue({
96
91
  type: "comap",
97
92
  ruleset: { type: "unsafeAllowAll" },
98
93
  meta: null,
99
- ...createdNowUnique(),
94
+ ...Crypto.createdNowUnique(),
100
95
  });
101
96
 
102
97
  const content = expectMap(coValue.getCurrentContent());
@@ -140,13 +135,13 @@ test("Can get all historic values of key in CoMap", () => {
140
135
  });
141
136
 
142
137
  test("Can get last tx ID for a key in CoMap", () => {
143
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
138
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
144
139
 
145
140
  const coValue = node.createCoValue({
146
141
  type: "comap",
147
142
  ruleset: { type: "unsafeAllowAll" },
148
143
  meta: null,
149
- ...createdNowUnique(),
144
+ ...Crypto.createdNowUnique(),
150
145
  });
151
146
 
152
147
  const content = expectMap(coValue.getCurrentContent());
@@ -157,11 +152,11 @@ test("Can get last tx ID for a key in CoMap", () => {
157
152
  content.set("hello", "A", "trusting");
158
153
  const sessionID = content.lastEditAt("hello")?.tx.sessionID;
159
154
  expect(sessionID && accountOrAgentIDfromSessionID(sessionID)).toEqual(
160
- node.account.id
155
+ node.account.id,
161
156
  );
162
157
  expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(0);
163
158
  content.set("hello", "B", "trusting");
164
159
  expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(1);
165
160
  content.set("hello", "C", "trusting");
166
161
  expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(2);
167
- });
162
+ });