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

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