@dxos/echo-pipeline 0.3.9-main.b7e6a67 → 0.3.9-main.c7cd0ec

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 (43) hide show
  1. package/dist/lib/browser/{chunk-MPEQ7BV3.mjs → chunk-RV3JOJQ4.mjs} +266 -49
  2. package/dist/lib/browser/chunk-RV3JOJQ4.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +3 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +7 -2
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/{chunk-YJAADRTG.cjs → chunk-QIIIHTAB.cjs} +260 -51
  8. package/dist/lib/node/chunk-QIIIHTAB.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +27 -25
  10. package/dist/lib/node/index.cjs.map +2 -2
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/testing/index.cjs +18 -14
  13. package/dist/lib/node/testing/index.cjs.map +3 -3
  14. package/dist/types/src/automerge/automerge-host.d.ts +17 -0
  15. package/dist/types/src/automerge/automerge-host.d.ts.map +1 -0
  16. package/dist/types/src/automerge/automerge-host.test.d.ts +2 -0
  17. package/dist/types/src/automerge/automerge-host.test.d.ts.map +1 -0
  18. package/dist/types/src/automerge/index.d.ts +2 -0
  19. package/dist/types/src/automerge/index.d.ts.map +1 -0
  20. package/dist/types/src/db-host/data-service-host.d.ts +2 -1
  21. package/dist/types/src/db-host/data-service-host.d.ts.map +1 -1
  22. package/dist/types/src/db-host/data-service.d.ts +5 -2
  23. package/dist/types/src/db-host/data-service.d.ts.map +1 -1
  24. package/dist/types/src/index.d.ts +1 -0
  25. package/dist/types/src/index.d.ts.map +1 -1
  26. package/dist/types/src/space/data-pipeline.d.ts +4 -0
  27. package/dist/types/src/space/data-pipeline.d.ts.map +1 -1
  28. package/dist/types/src/space/space-protocol.d.ts +2 -1
  29. package/dist/types/src/space/space-protocol.d.ts.map +1 -1
  30. package/dist/types/src/testing/util.d.ts.map +1 -1
  31. package/package.json +32 -31
  32. package/src/automerge/automerge-host.test.ts +122 -0
  33. package/src/automerge/automerge-host.ts +245 -0
  34. package/src/automerge/index.ts +5 -0
  35. package/src/db-host/data-service-host.ts +5 -0
  36. package/src/db-host/data-service.ts +14 -3
  37. package/src/index.ts +1 -0
  38. package/src/space/data-pipeline.ts +10 -0
  39. package/src/space/space-protocol.ts +6 -3
  40. package/src/testing/util.ts +4 -1
  41. package/src/tests/database.test.ts +4 -1
  42. package/dist/lib/browser/chunk-MPEQ7BV3.mjs.map +0 -7
  43. package/dist/lib/node/chunk-YJAADRTG.cjs.map +0 -7
@@ -26,10 +26,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_YJAADRTG_exports = {};
30
- __export(chunk_YJAADRTG_exports, {
29
+ var chunk_QIIIHTAB_exports = {};
30
+ __export(chunk_QIIIHTAB_exports, {
31
31
  AuthExtension: () => AuthExtension,
32
32
  AuthStatus: () => AuthStatus,
33
+ AutomergeHost: () => AutomergeHost,
33
34
  DataPipeline: () => DataPipeline,
34
35
  DataServiceHost: () => DataServiceHost,
35
36
  DataServiceImpl: () => DataServiceImpl,
@@ -53,7 +54,7 @@ __export(chunk_YJAADRTG_exports, {
53
54
  startAfter: () => startAfter,
54
55
  valueEncoding: () => valueEncoding
55
56
  });
56
- module.exports = __toCommonJS(chunk_YJAADRTG_exports);
57
+ module.exports = __toCommonJS(chunk_QIIIHTAB_exports);
57
58
  var import_hypercore = require("@dxos/hypercore");
58
59
  var import_protocols = require("@dxos/protocols");
59
60
  var import_invariant = require("@dxos/invariant");
@@ -146,6 +147,10 @@ var import_keys7 = require("@dxos/keys");
146
147
  var import_log12 = require("@dxos/log");
147
148
  var import_protocols7 = require("@dxos/protocols");
148
149
  var import_util9 = require("@dxos/util");
150
+ var import_automerge_repo = require("@dxos/automerge/automerge-repo");
151
+ var import_codec_protobuf2 = require("@dxos/codec-protobuf");
152
+ var import_invariant10 = require("@dxos/invariant");
153
+ var import_util10 = require("@dxos/util");
149
154
  var codec = import_protocols.schema.getCodecForType("dxos.echo.feed.FeedMessage");
150
155
  var valueEncoding = (0, import_hypercore.createCodecEncoding)(codec);
151
156
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/common/feeds.ts";
@@ -252,7 +257,7 @@ var DataServiceHost = class {
252
257
  const { batch, meta } = message;
253
258
  (0, import_invariant2.invariant)(!meta.clientTag, "Unexpected client tag in mutation message", {
254
259
  F: __dxlog_file2,
255
- L: 137,
260
+ L: 138,
256
261
  S: this,
257
262
  A: [
258
263
  "!(meta as any).clientTag",
@@ -264,7 +269,7 @@ var DataServiceHost = class {
264
269
  meta
265
270
  }, {
266
271
  F: __dxlog_file2,
267
- L: 138,
272
+ L: 139,
268
273
  S: this,
269
274
  C: (f, a) => f(...a)
270
275
  });
@@ -308,7 +313,7 @@ var DataServiceHost = class {
308
313
  async write(request) {
309
314
  (0, import_invariant2.invariant)(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
310
315
  F: __dxlog_file2,
311
- L: 183,
316
+ L: 184,
312
317
  S: this,
313
318
  A: [
314
319
  "!this._ctx.disposed",
@@ -317,7 +322,7 @@ var DataServiceHost = class {
317
322
  });
318
323
  (0, import_invariant2.invariant)(this._writeStream, "Cannot write mutations in readonly mode", {
319
324
  F: __dxlog_file2,
320
- L: 184,
325
+ L: 185,
321
326
  S: this,
322
327
  A: [
323
328
  "this._writeStream",
@@ -329,7 +334,7 @@ var DataServiceHost = class {
329
334
  objectCount: request.batch.objects?.length ?? 0
330
335
  }, {
331
336
  F: __dxlog_file2,
332
- L: 186,
337
+ L: 187,
333
338
  S: this,
334
339
  C: (f, a) => f(...a)
335
340
  });
@@ -343,7 +348,7 @@ var DataServiceHost = class {
343
348
  seq: receipt2.seq
344
349
  }, {
345
350
  F: __dxlog_file2,
346
- L: 195,
351
+ L: 196,
347
352
  S: this,
348
353
  C: (f, a) => f(...a)
349
354
  });
@@ -359,6 +364,9 @@ var DataServiceHost = class {
359
364
  async flush() {
360
365
  await this._flush();
361
366
  }
367
+ getHostInfo() {
368
+ throw new Error("Method not implemented.");
369
+ }
362
370
  syncRepo(request) {
363
371
  throw new Error("Method not implemented.");
364
372
  }
@@ -491,13 +499,13 @@ var DataServiceSubscriptions = class {
491
499
  spaceKey
492
500
  }, {
493
501
  F: __dxlog_file3,
494
- L: 33,
502
+ L: 35,
495
503
  S: this,
496
504
  C: (f, a) => f(...a)
497
505
  });
498
506
  (0, import_invariant3.invariant)(!this._spaces.has(spaceKey), void 0, {
499
507
  F: __dxlog_file3,
500
- L: 34,
508
+ L: 36,
501
509
  S: this,
502
510
  A: [
503
511
  "!this._spaces.has(spaceKey)",
@@ -512,7 +520,7 @@ var DataServiceSubscriptions = class {
512
520
  spaceKey
513
521
  }, {
514
522
  F: __dxlog_file3,
515
- L: 40,
523
+ L: 42,
516
524
  S: this,
517
525
  C: (f, a) => f(...a)
518
526
  });
@@ -525,13 +533,14 @@ var DataServiceSubscriptions = class {
525
533
  }
526
534
  };
527
535
  var DataServiceImpl = class {
528
- constructor(_subscriptions) {
536
+ constructor(_subscriptions, _automergeHost) {
529
537
  this._subscriptions = _subscriptions;
538
+ this._automergeHost = _automergeHost;
530
539
  }
531
540
  subscribe(request) {
532
541
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
533
542
  F: __dxlog_file3,
534
- L: 59,
543
+ L: 64,
535
544
  S: this,
536
545
  A: [
537
546
  "request.spaceKey",
@@ -544,7 +553,7 @@ var DataServiceImpl = class {
544
553
  write(request) {
545
554
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
546
555
  F: __dxlog_file3,
547
- L: 66,
556
+ L: 71,
548
557
  S: this,
549
558
  A: [
550
559
  "request.spaceKey",
@@ -553,7 +562,7 @@ var DataServiceImpl = class {
553
562
  });
554
563
  (0, import_invariant3.invariant)(request.batch, void 0, {
555
564
  F: __dxlog_file3,
556
- L: 67,
565
+ L: 72,
557
566
  S: this,
558
567
  A: [
559
568
  "request.batch",
@@ -566,7 +575,7 @@ var DataServiceImpl = class {
566
575
  flush(request) {
567
576
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
568
577
  F: __dxlog_file3,
569
- L: 74,
578
+ L: 79,
570
579
  S: this,
571
580
  A: [
572
581
  "request.spaceKey",
@@ -576,11 +585,15 @@ var DataServiceImpl = class {
576
585
  const host = this._subscriptions.getDataService(request.spaceKey) ?? (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
577
586
  return host.flush();
578
587
  }
588
+ // Automerge specific.
589
+ async getHostInfo(request) {
590
+ return this._automergeHost.getHostInfo();
591
+ }
579
592
  syncRepo(request) {
580
- throw new Error("Method not implemented.");
593
+ return this._automergeHost.syncRepo(request);
581
594
  }
582
595
  sendSyncMessage(request) {
583
- throw new Error("Method not implemented.");
596
+ return this._automergeHost.sendSyncMessage(request);
584
597
  }
585
598
  };
586
599
  function _ts_decorate(decorators, target, key, desc) {
@@ -1570,7 +1583,7 @@ var DataPipeline = class DataPipeline2 {
1570
1583
  write: (data, options) => {
1571
1584
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1572
1585
  F: __dxlog_file9,
1573
- L: 159,
1586
+ L: 164,
1574
1587
  S: this,
1575
1588
  A: [
1576
1589
  "this._pipeline",
@@ -1579,7 +1592,7 @@ var DataPipeline = class DataPipeline2 {
1579
1592
  });
1580
1593
  (0, import_invariant8.invariant)(this.currentEpoch, "Epoch is not initialized.", {
1581
1594
  F: __dxlog_file9,
1582
- L: 160,
1595
+ L: 165,
1583
1596
  S: this,
1584
1597
  A: [
1585
1598
  "this.currentEpoch",
@@ -1605,7 +1618,7 @@ var DataPipeline = class DataPipeline2 {
1605
1618
  }
1606
1619
  (0, import_log8.log)("close", void 0, {
1607
1620
  F: __dxlog_file9,
1608
- L: 184,
1621
+ L: 189,
1609
1622
  S: this,
1610
1623
  C: (f, a) => f(...a)
1611
1624
  });
@@ -1620,7 +1633,7 @@ var DataPipeline = class DataPipeline2 {
1620
1633
  } catch (err) {
1621
1634
  import_log8.log.catch(err, void 0, {
1622
1635
  F: __dxlog_file9,
1623
- L: 197,
1636
+ L: 202,
1624
1637
  S: this,
1625
1638
  C: (f, a) => f(...a)
1626
1639
  });
@@ -1645,7 +1658,7 @@ var DataPipeline = class DataPipeline2 {
1645
1658
  let messageCounter = 0;
1646
1659
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1647
1660
  F: __dxlog_file9,
1648
- L: 223,
1661
+ L: 228,
1649
1662
  S: this,
1650
1663
  A: [
1651
1664
  "this._pipeline",
@@ -1661,7 +1674,7 @@ var DataPipeline = class DataPipeline2 {
1661
1674
  seq
1662
1675
  }, {
1663
1676
  F: __dxlog_file9,
1664
- L: 229,
1677
+ L: 234,
1665
1678
  S: this,
1666
1679
  C: (f, a) => f(...a)
1667
1680
  });
@@ -1673,7 +1686,7 @@ var DataPipeline = class DataPipeline2 {
1673
1686
  feedKey
1674
1687
  }, {
1675
1688
  F: __dxlog_file9,
1676
- L: 235,
1689
+ L: 240,
1677
1690
  S: this,
1678
1691
  C: (f, a) => f(...a)
1679
1692
  });
@@ -1696,7 +1709,7 @@ var DataPipeline = class DataPipeline2 {
1696
1709
  spaceKey: this._params.spaceKey.toHex()
1697
1710
  }, {
1698
1711
  F: __dxlog_file9,
1699
- L: 252,
1712
+ L: 257,
1700
1713
  S: this,
1701
1714
  C: (f, a) => f(...a)
1702
1715
  });
@@ -1705,7 +1718,7 @@ var DataPipeline = class DataPipeline2 {
1705
1718
  } catch (err) {
1706
1719
  import_log8.log.catch(err, void 0, {
1707
1720
  F: __dxlog_file9,
1708
- L: 262,
1721
+ L: 267,
1709
1722
  S: this,
1710
1723
  C: (f, a) => f(...a)
1711
1724
  });
@@ -1720,7 +1733,7 @@ var DataPipeline = class DataPipeline2 {
1720
1733
  _createSnapshot() {
1721
1734
  (0, import_invariant8.invariant)(this.databaseHost, "Database backend is not initialized.", {
1722
1735
  F: __dxlog_file9,
1723
- L: 276,
1736
+ L: 281,
1724
1737
  S: this,
1725
1738
  A: [
1726
1739
  "this.databaseHost",
@@ -1749,7 +1762,7 @@ var DataPipeline = class DataPipeline2 {
1749
1762
  } catch (err) {
1750
1763
  import_log8.log.warn("Failed to cache properties", err, {
1751
1764
  F: __dxlog_file9,
1752
- L: 305,
1765
+ L: 310,
1753
1766
  S: this,
1754
1767
  C: (f, a) => f(...a)
1755
1768
  });
@@ -1778,14 +1791,14 @@ var DataPipeline = class DataPipeline2 {
1778
1791
  if (err instanceof import_protocols6.CancelledError) {
1779
1792
  (0, import_log8.log)("Epoch processing cancelled.", void 0, {
1780
1793
  F: __dxlog_file9,
1781
- L: 341,
1794
+ L: 346,
1782
1795
  S: this,
1783
1796
  C: (f, a) => f(...a)
1784
1797
  });
1785
1798
  } else {
1786
1799
  import_log8.log.catch(err, void 0, {
1787
1800
  F: __dxlog_file9,
1788
- L: 343,
1801
+ L: 348,
1789
1802
  S: this,
1790
1803
  C: (f, a) => f(...a)
1791
1804
  });
@@ -1798,6 +1811,9 @@ var DataPipeline = class DataPipeline2 {
1798
1811
  return;
1799
1812
  }
1800
1813
  await this._processEpoch(ctx, epoch.subject.assertion);
1814
+ if (epoch.subject.assertion.snapshotCid === void 0) {
1815
+ epoch.subject.assertion.snapshotCid = this.appliedEpoch?.subject.assertion.snapshotCid;
1816
+ }
1801
1817
  this.appliedEpoch = epoch;
1802
1818
  this.onNewEpoch.emit(epoch);
1803
1819
  });
@@ -1805,7 +1821,7 @@ var DataPipeline = class DataPipeline2 {
1805
1821
  async _processEpoch(ctx, epoch) {
1806
1822
  (0, import_invariant8.invariant)(this._isOpen, "Space is closed.", {
1807
1823
  F: __dxlog_file9,
1808
- L: 362,
1824
+ L: 372,
1809
1825
  S: this,
1810
1826
  A: [
1811
1827
  "this._isOpen",
@@ -1814,7 +1830,7 @@ var DataPipeline = class DataPipeline2 {
1814
1830
  });
1815
1831
  (0, import_invariant8.invariant)(this._pipeline, void 0, {
1816
1832
  F: __dxlog_file9,
1817
- L: 363,
1833
+ L: 373,
1818
1834
  S: this,
1819
1835
  A: [
1820
1836
  "this._pipeline",
@@ -1826,7 +1842,7 @@ var DataPipeline = class DataPipeline2 {
1826
1842
  epoch: (0, import_log8.omit)(epoch, "proof")
1827
1843
  }, {
1828
1844
  F: __dxlog_file9,
1829
- L: 366,
1845
+ L: 376,
1830
1846
  S: this,
1831
1847
  C: (f, a) => f(...a)
1832
1848
  });
@@ -1836,7 +1852,7 @@ var DataPipeline = class DataPipeline2 {
1836
1852
  }
1837
1853
  (0, import_log8.log)("restarting pipeline from epoch", void 0, {
1838
1854
  F: __dxlog_file9,
1839
- L: 372,
1855
+ L: 382,
1840
1856
  S: this,
1841
1857
  C: (f, a) => f(...a)
1842
1858
  });
@@ -1847,7 +1863,7 @@ var DataPipeline = class DataPipeline2 {
1847
1863
  async waitUntilTimeframe(timeframe) {
1848
1864
  (0, import_invariant8.invariant)(this._pipeline, "Pipeline is not initialized.", {
1849
1865
  F: __dxlog_file9,
1850
- L: 379,
1866
+ L: 389,
1851
1867
  S: this,
1852
1868
  A: [
1853
1869
  "this._pipeline",
@@ -1859,7 +1875,7 @@ var DataPipeline = class DataPipeline2 {
1859
1875
  async createEpoch() {
1860
1876
  (0, import_invariant8.invariant)(this._pipeline, void 0, {
1861
1877
  F: __dxlog_file9,
1862
- L: 385,
1878
+ L: 395,
1863
1879
  S: this,
1864
1880
  A: [
1865
1881
  "this._pipeline",
@@ -1868,7 +1884,7 @@ var DataPipeline = class DataPipeline2 {
1868
1884
  });
1869
1885
  (0, import_invariant8.invariant)(this.currentEpoch, void 0, {
1870
1886
  F: __dxlog_file9,
1871
- L: 386,
1887
+ L: 396,
1872
1888
  S: this,
1873
1889
  A: [
1874
1890
  "this.currentEpoch",
@@ -1899,7 +1915,7 @@ var DataPipeline = class DataPipeline2 {
1899
1915
  } catch (err) {
1900
1916
  import_log8.log.catch(err, void 0, {
1901
1917
  F: __dxlog_file9,
1902
- L: 416,
1918
+ L: 426,
1903
1919
  S: this,
1904
1920
  C: (f, a) => f(...a)
1905
1921
  });
@@ -2465,6 +2481,7 @@ var SpaceProtocol = class {
2465
2481
  constructor({ topic, swarmIdentity, networkManager, onSessionAuth, onAuthFailure, blobStore }) {
2466
2482
  this._feeds = /* @__PURE__ */ new Set();
2467
2483
  this._sessions = new import_util8.ComplexMap(import_keys6.PublicKey.hash);
2484
+ this._spaceKey = topic;
2468
2485
  this._networkManager = networkManager;
2469
2486
  this._swarmIdentity = swarmIdentity;
2470
2487
  this._onSessionAuth = onSessionAuth;
@@ -2489,7 +2506,7 @@ var SpaceProtocol = class {
2489
2506
  key: feed.key
2490
2507
  }, {
2491
2508
  F: __dxlog_file12,
2492
- L: 96,
2509
+ L: 99,
2493
2510
  S: this,
2494
2511
  C: (f, a) => f(...a)
2495
2512
  });
@@ -2512,7 +2529,7 @@ var SpaceProtocol = class {
2512
2529
  await this.blobSync.open();
2513
2530
  (0, import_log11.log)("starting...", void 0, {
2514
2531
  F: __dxlog_file12,
2515
- L: 122,
2532
+ L: 125,
2516
2533
  S: this,
2517
2534
  C: (f, a) => f(...a)
2518
2535
  });
@@ -2522,11 +2539,11 @@ var SpaceProtocol = class {
2522
2539
  peerId: this._swarmIdentity.peerKey,
2523
2540
  topic,
2524
2541
  topology: new import_network_manager.MMSTTopology(topologyConfig),
2525
- label: `space swarm ${topic.truncate()}`
2542
+ label: `swarm ${topic.truncate()} for space ${this._spaceKey.truncate()}`
2526
2543
  });
2527
2544
  (0, import_log11.log)("started", void 0, {
2528
2545
  F: __dxlog_file12,
2529
- L: 132,
2546
+ L: 135,
2530
2547
  S: this,
2531
2548
  C: (f, a) => f(...a)
2532
2549
  });
@@ -2536,14 +2553,14 @@ var SpaceProtocol = class {
2536
2553
  if (this._connection) {
2537
2554
  (0, import_log11.log)("stopping...", void 0, {
2538
2555
  F: __dxlog_file12,
2539
- L: 139,
2556
+ L: 142,
2540
2557
  S: this,
2541
2558
  C: (f, a) => f(...a)
2542
2559
  });
2543
2560
  await this._connection.close();
2544
2561
  (0, import_log11.log)("stopped", void 0, {
2545
2562
  F: __dxlog_file12,
2546
- L: 141,
2563
+ L: 144,
2547
2564
  S: this,
2548
2565
  C: (f, a) => f(...a)
2549
2566
  });
@@ -2601,15 +2618,15 @@ var SpaceProtocolSession = class {
2601
2618
  get stream() {
2602
2619
  return this._teleport.stream;
2603
2620
  }
2604
- async open() {
2605
- await this._teleport.open();
2621
+ async open(sessionId) {
2622
+ await this._teleport.open(sessionId);
2606
2623
  this._teleport.addExtension("dxos.mesh.teleport.auth", new AuthExtension({
2607
2624
  provider: this._swarmIdentity.credentialProvider,
2608
2625
  verifier: this._swarmIdentity.credentialAuthenticator,
2609
2626
  onAuthSuccess: () => {
2610
2627
  (0, import_log11.log)("Peer authenticated", void 0, {
2611
2628
  F: __dxlog_file12,
2612
- L: 238,
2629
+ L: 241,
2613
2630
  S: this,
2614
2631
  C: (f, a) => f(...a)
2615
2632
  });
@@ -2627,7 +2644,7 @@ var SpaceProtocolSession = class {
2627
2644
  async close() {
2628
2645
  (0, import_log11.log)("close", void 0, {
2629
2646
  F: __dxlog_file12,
2630
- L: 254,
2647
+ L: 257,
2631
2648
  S: this,
2632
2649
  C: (f, a) => f(...a)
2633
2650
  });
@@ -2735,10 +2752,202 @@ _ts_decorate8([
2735
2752
  SpaceManager = _ts_decorate8([
2736
2753
  (0, import_async9.trackLeaks)("open", "close")
2737
2754
  ], SpaceManager);
2755
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
2756
+ var AutomergeHost = class {
2757
+ constructor(storageDirectory) {
2758
+ this._meshNetwork = new MeshNetworkAdapter();
2759
+ this._clientNetwork = new LocalHostNetworkAdapter();
2760
+ this._storage = new AutomergeStorageAdapter(storageDirectory);
2761
+ this._repo = new import_automerge_repo.Repo({
2762
+ network: [
2763
+ // this._meshNetwork,
2764
+ this._clientNetwork
2765
+ ],
2766
+ storage: this._storage,
2767
+ // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
2768
+ sharePolicy: async (peerId, documentId) => true
2769
+ });
2770
+ this._clientNetwork.ready();
2771
+ }
2772
+ get repo() {
2773
+ return this._repo;
2774
+ }
2775
+ async close() {
2776
+ await this._clientNetwork.close();
2777
+ }
2778
+ //
2779
+ // Methods for client-services.
2780
+ //
2781
+ syncRepo(request) {
2782
+ return this._clientNetwork.syncRepo(request);
2783
+ }
2784
+ sendSyncMessage(request) {
2785
+ return this._clientNetwork.sendSyncMessage(request);
2786
+ }
2787
+ getHostInfo() {
2788
+ return this._clientNetwork.getHostInfo();
2789
+ }
2790
+ };
2791
+ var LocalHostNetworkAdapter = class extends import_automerge_repo.NetworkAdapter {
2792
+ constructor() {
2793
+ super(...arguments);
2794
+ this._peers = /* @__PURE__ */ new Map();
2795
+ }
2796
+ /**
2797
+ * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
2798
+ */
2799
+ ready() {
2800
+ this.emit("ready", {
2801
+ network: this
2802
+ });
2803
+ }
2804
+ connect(peerId) {
2805
+ this.peerId = peerId;
2806
+ }
2807
+ send(message) {
2808
+ const peer = this._peers.get(message.targetId);
2809
+ (0, import_invariant10.invariant)(peer, "Peer not found.", {
2810
+ F: __dxlog_file14,
2811
+ L: 100,
2812
+ S: this,
2813
+ A: [
2814
+ "peer",
2815
+ "'Peer not found.'"
2816
+ ]
2817
+ });
2818
+ peer.send(message);
2819
+ }
2820
+ async close() {
2821
+ this._peers.forEach((peer) => peer.disconnect());
2822
+ this.emit("close");
2823
+ }
2824
+ disconnect() {
2825
+ }
2826
+ syncRepo({ id, syncMessage }) {
2827
+ const peerId = this._getPeerId(id);
2828
+ return new import_codec_protobuf2.Stream(({ next, close }) => {
2829
+ (0, import_invariant10.invariant)(!this._peers.has(peerId), "Peer already connected.", {
2830
+ F: __dxlog_file14,
2831
+ L: 118,
2832
+ S: this,
2833
+ A: [
2834
+ "!this._peers.has(peerId)",
2835
+ "'Peer already connected.'"
2836
+ ]
2837
+ });
2838
+ this._peers.set(peerId, {
2839
+ connected: true,
2840
+ send: (message) => {
2841
+ next({
2842
+ syncMessage: import_automerge_repo.cbor.encode(message)
2843
+ });
2844
+ },
2845
+ disconnect: () => {
2846
+ this._peers.delete(peerId);
2847
+ close();
2848
+ this.emit("peer-disconnected", {
2849
+ peerId
2850
+ });
2851
+ }
2852
+ });
2853
+ this.emit("peer-candidate", {
2854
+ peerId
2855
+ });
2856
+ });
2857
+ }
2858
+ async sendSyncMessage({ id, syncMessage }) {
2859
+ const message = import_automerge_repo.cbor.decode(syncMessage);
2860
+ this.emit("message", message);
2861
+ }
2862
+ getHostInfo() {
2863
+ (0, import_invariant10.invariant)(this.peerId, "Peer id not set.", {
2864
+ F: __dxlog_file14,
2865
+ L: 147,
2866
+ S: this,
2867
+ A: [
2868
+ "this.peerId",
2869
+ "'Peer id not set.'"
2870
+ ]
2871
+ });
2872
+ return {
2873
+ peerId: this.peerId
2874
+ };
2875
+ }
2876
+ _getPeerId(id) {
2877
+ return id;
2878
+ }
2879
+ };
2880
+ var MeshNetworkAdapter = class extends import_automerge_repo.NetworkAdapter {
2881
+ connect(peerId) {
2882
+ throw new Error("Method not implemented.");
2883
+ }
2884
+ send(message) {
2885
+ throw new Error("Method not implemented.");
2886
+ }
2887
+ disconnect() {
2888
+ throw new Error("Method not implemented.");
2889
+ }
2890
+ };
2891
+ var AutomergeStorageAdapter = class extends import_automerge_repo.StorageAdapter {
2892
+ constructor(_directory) {
2893
+ super();
2894
+ this._directory = _directory;
2895
+ }
2896
+ async load(key) {
2897
+ const filename = this._getFilename(key);
2898
+ const file = this._directory.getOrCreateFile(filename);
2899
+ const { size } = await file.stat();
2900
+ if (!size || size === 0) {
2901
+ return void 0;
2902
+ }
2903
+ const buffer = await file.read(0, size);
2904
+ return (0, import_util10.bufferToArray)(buffer);
2905
+ }
2906
+ async save(key, data) {
2907
+ const filename = this._getFilename(key);
2908
+ const file = this._directory.getOrCreateFile(filename);
2909
+ await file.write(0, (0, import_util10.arrayToBuffer)(data));
2910
+ await file.truncate?.(data.length);
2911
+ await file.flush?.();
2912
+ }
2913
+ async remove(key) {
2914
+ const filename = this._getFilename(key);
2915
+ const file = this._directory.getOrCreateFile(filename);
2916
+ await file.truncate?.(0);
2917
+ }
2918
+ async loadRange(keyPrefix) {
2919
+ const filename = this._getFilename(keyPrefix);
2920
+ const entries = await this._directory.list();
2921
+ return Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2922
+ const file = this._directory.getOrCreateFile(entry);
2923
+ const { size } = await file.stat();
2924
+ const buffer = await file.read(0, size);
2925
+ return {
2926
+ key: this._getKeyFromFilename(entry),
2927
+ data: (0, import_util10.bufferToArray)(buffer)
2928
+ };
2929
+ }));
2930
+ }
2931
+ async removeRange(keyPrefix) {
2932
+ const filename = this._getFilename(keyPrefix);
2933
+ const entries = await this._directory.list();
2934
+ await Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2935
+ const file = this._directory.getOrCreateFile(filename);
2936
+ await file.truncate?.(0);
2937
+ }));
2938
+ }
2939
+ _getFilename(key) {
2940
+ return key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-");
2941
+ }
2942
+ _getKeyFromFilename(filename) {
2943
+ return filename.split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"));
2944
+ }
2945
+ };
2738
2946
  // Annotate the CommonJS export names for ESM import in node:
2739
2947
  0 && (module.exports = {
2740
2948
  AuthExtension,
2741
2949
  AuthStatus,
2950
+ AutomergeHost,
2742
2951
  DataPipeline,
2743
2952
  DataServiceHost,
2744
2953
  DataServiceImpl,
@@ -2762,4 +2971,4 @@ SpaceManager = _ts_decorate8([
2762
2971
  startAfter,
2763
2972
  valueEncoding
2764
2973
  });
2765
- //# sourceMappingURL=chunk-YJAADRTG.cjs.map
2974
+ //# sourceMappingURL=chunk-QIIIHTAB.cjs.map