cojson 0.1.11 → 0.2.0

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 (56) hide show
  1. package/dist/base64url.js +15 -2
  2. package/dist/base64url.js.map +1 -1
  3. package/dist/coValue.d.ts +1 -1
  4. package/dist/coValueCore.d.ts +9 -3
  5. package/dist/coValueCore.js +100 -7
  6. package/dist/coValueCore.js.map +1 -1
  7. package/dist/coValues/coList.js +2 -1
  8. package/dist/coValues/coList.js.map +1 -1
  9. package/dist/coValues/coMap.d.ts +7 -12
  10. package/dist/coValues/coMap.js +2 -1
  11. package/dist/coValues/coMap.js.map +1 -1
  12. package/dist/coValues/coStream.d.ts +11 -2
  13. package/dist/coValues/coStream.js +59 -15
  14. package/dist/coValues/coStream.js.map +1 -1
  15. package/dist/crypto.d.ts +11 -3
  16. package/dist/crypto.js +51 -16
  17. package/dist/crypto.js.map +1 -1
  18. package/dist/group.d.ts +1 -1
  19. package/dist/index.d.ts +6 -3
  20. package/dist/index.js +5 -3
  21. package/dist/index.js.map +1 -1
  22. package/dist/jsonStringify.d.ts +6 -0
  23. package/dist/jsonStringify.js +57 -0
  24. package/dist/jsonStringify.js.map +1 -0
  25. package/dist/jsonValue.d.ts +1 -1
  26. package/dist/media.d.ts +8 -0
  27. package/dist/media.js +2 -0
  28. package/dist/media.js.map +1 -0
  29. package/dist/node.js +1 -1
  30. package/dist/permissions.js +4 -2
  31. package/dist/permissions.js.map +1 -1
  32. package/dist/sync.js +8 -2
  33. package/dist/sync.js.map +1 -1
  34. package/package.json +3 -4
  35. package/src/account.test.ts +5 -0
  36. package/src/base64url.ts +16 -6
  37. package/src/coValue.test.ts +28 -0
  38. package/src/coValue.ts +1 -1
  39. package/src/coValueCore.test.ts +16 -10
  40. package/src/coValueCore.ts +148 -9
  41. package/src/coValues/coList.ts +2 -1
  42. package/src/coValues/coMap.ts +11 -12
  43. package/src/coValues/coStream.ts +83 -21
  44. package/src/crypto.test.ts +5 -0
  45. package/src/crypto.ts +68 -19
  46. package/src/group.test.ts +5 -1
  47. package/src/group.ts +1 -1
  48. package/src/index.ts +7 -1
  49. package/src/jsonStringify.ts +66 -0
  50. package/src/jsonValue.ts +1 -1
  51. package/src/media.ts +9 -0
  52. package/src/node.ts +1 -1
  53. package/src/permissions.test.ts +5 -1
  54. package/src/permissions.ts +5 -2
  55. package/src/sync.test.ts +22 -24
  56. package/src/sync.ts +16 -2
package/src/sync.test.ts CHANGED
@@ -1,23 +1,21 @@
1
1
  import { newRandomSessionID } from "./coValueCore.js";
2
2
  import { LocalNode } from "./node.js";
3
- import { Peer, PeerID, SyncMessage } from "./sync.js";
3
+ import { SyncMessage } from "./sync.js";
4
4
  import { expectMap } from "./coValue.js";
5
5
  import { MapOpPayload } from "./coValues/coMap.js";
6
6
  import { Group } from "./group.js";
7
- import {
8
- ReadableStream,
9
- WritableStream,
10
- TransformStream,
11
- } from "isomorphic-streams";
12
7
  import {
13
8
  randomAnonymousAccountAndSessionID,
14
9
  shouldNotResolve,
15
10
  } from "./testUtils.js";
16
- import {
17
- connectedPeers,
18
- newStreamPair
19
- } from "./streamUtils.js";
11
+ import { connectedPeers, newStreamPair } from "./streamUtils.js";
20
12
  import { AccountID } from "./account.js";
13
+ import { cojsonReady } from "./index.js";
14
+ import { stableStringify } from "./jsonStringify.js";
15
+
16
+ beforeEach(async () => {
17
+ await cojsonReady;
18
+ });
21
19
 
22
20
  test("Node replies with initial tx and header to empty subscribe", async () => {
23
21
  const [admin, session] = randomAnonymousAccountAndSessionID();
@@ -84,13 +82,13 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
84
82
  privacy: "trusting" as const,
85
83
  madeAt: map.core.sessions[node.currentSessionID]!
86
84
  .transactions[0]!.madeAt,
87
- changes: [
85
+ changes: stableStringify([
88
86
  {
89
87
  op: "set",
90
88
  key: "hello",
91
89
  value: "world",
92
90
  } satisfies MapOpPayload<string, string>,
93
- ],
91
+ ]),
94
92
  },
95
93
  ],
96
94
  lastSignature:
@@ -162,13 +160,13 @@ test("Node replies with only new tx to subscribe with some known state", async (
162
160
  privacy: "trusting" as const,
163
161
  madeAt: map.core.sessions[node.currentSessionID]!
164
162
  .transactions[1]!.madeAt,
165
- changes: [
163
+ changes: stableStringify([
166
164
  {
167
165
  op: "set",
168
166
  key: "goodbye",
169
167
  value: "world",
170
168
  } satisfies MapOpPayload<string, string>,
171
- ],
169
+ ]),
172
170
  },
173
171
  ],
174
172
  lastSignature:
@@ -251,13 +249,13 @@ test("After subscribing, node sends own known state and new txs to peer", async
251
249
  privacy: "trusting" as const,
252
250
  madeAt: map.core.sessions[node.currentSessionID]!
253
251
  .transactions[0]!.madeAt,
254
- changes: [
252
+ changes: stableStringify([
255
253
  {
256
254
  op: "set",
257
255
  key: "hello",
258
256
  value: "world",
259
257
  } satisfies MapOpPayload<string, string>,
260
- ],
258
+ ]),
261
259
  },
262
260
  ],
263
261
  lastSignature:
@@ -283,13 +281,13 @@ test("After subscribing, node sends own known state and new txs to peer", async
283
281
  privacy: "trusting" as const,
284
282
  madeAt: map.core.sessions[node.currentSessionID]!
285
283
  .transactions[1]!.madeAt,
286
- changes: [
284
+ changes: stableStringify([
287
285
  {
288
286
  op: "set",
289
287
  key: "goodbye",
290
288
  value: "world",
291
289
  } satisfies MapOpPayload<string, string>,
292
- ],
290
+ ]),
293
291
  },
294
292
  ],
295
293
  lastSignature:
@@ -362,13 +360,13 @@ test("Client replies with known new content to tellKnownState from server", asyn
362
360
  privacy: "trusting" as const,
363
361
  madeAt: map.core.sessions[node.currentSessionID]!
364
362
  .transactions[0]!.madeAt,
365
- changes: [
363
+ changes: stableStringify([
366
364
  {
367
365
  op: "set",
368
366
  key: "hello",
369
367
  value: "world",
370
368
  } satisfies MapOpPayload<string, string>,
371
- ],
369
+ ]),
372
370
  },
373
371
  ],
374
372
  lastSignature:
@@ -465,13 +463,13 @@ test("No matter the optimistic known state, node respects invalid known state me
465
463
  privacy: "trusting" as const,
466
464
  madeAt: map.core.sessions[node.currentSessionID]!
467
465
  .transactions[1]!.madeAt,
468
- changes: [
466
+ changes: stableStringify([
469
467
  {
470
468
  op: "set",
471
469
  key: "goodbye",
472
470
  value: "world",
473
471
  } satisfies MapOpPayload<string, string>,
474
- ],
472
+ ]),
475
473
  },
476
474
  ],
477
475
  lastSignature:
@@ -568,13 +566,13 @@ test("If we add a server peer, all updates to all coValues are sent to it, even
568
566
  privacy: "trusting" as const,
569
567
  madeAt: map.core.sessions[node.currentSessionID]!
570
568
  .transactions[0]!.madeAt,
571
- changes: [
569
+ changes: stableStringify([
572
570
  {
573
571
  op: "set",
574
572
  key: "hello",
575
573
  value: "world",
576
574
  } satisfies MapOpPayload<string, string>,
577
- ],
575
+ ]),
578
576
  },
579
577
  ],
580
578
  lastSignature:
package/src/sync.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  WritableStreamDefaultWriter,
10
10
  } from "isomorphic-streams";
11
11
  import { RawCoID, SessionID } from "./ids.js";
12
+ import { stableStringify } from "./jsonStringify.js";
12
13
 
13
14
  export type CoValueKnownState = {
14
15
  id: RawCoID;
@@ -268,7 +269,6 @@ export class SyncManager {
268
269
  );
269
270
  }
270
271
  }
271
- console.log("DONE!!!");
272
272
  } catch (e) {
273
273
  console.error(`Error reading from peer ${peer.id}`, e);
274
274
  }
@@ -445,12 +445,26 @@ export class SyncManager {
445
445
  const newTransactions =
446
446
  newContentForSession.newTransactions.slice(alreadyKnownOffset);
447
447
 
448
- const success = coValue.tryAddTransactions(
448
+ const before = performance.now();
449
+ const success = await coValue.tryAddTransactionsAsync(
449
450
  sessionID,
450
451
  newTransactions,
451
452
  undefined,
452
453
  newContentForSession.lastSignature
453
454
  );
455
+ const after = performance.now();
456
+ if (after - before > 10) {
457
+ const totalTxLength = newTransactions.map(t => stableStringify(t)!.length).reduce((a, b) => a + b, 0);
458
+ console.log(
459
+ "Adding incoming transactions took",
460
+ after - before,
461
+ "ms",
462
+ totalTxLength,
463
+ "bytes = ",
464
+ "bandwidth: MB/s",
465
+ (1000 * totalTxLength / (after - before)) / (1024 * 1024)
466
+ );
467
+ }
454
468
 
455
469
  if (!success) {
456
470
  console.error("Failed to add transactions", newTransactions);