@dxos/echo-pipeline 0.3.9-main.cea1d3b → 0.3.9-main.d4ef8aa

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-MHHS3KW4.mjs} +260 -49
  2. package/dist/lib/browser/chunk-MHHS3KW4.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-FOMFGLQJ.cjs} +254 -51
  8. package/dist/lib/node/chunk-FOMFGLQJ.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 +16 -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 +235 -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
@@ -1,5 +1,12 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
+ // inject-globals:@inject-globals
4
+ import {
5
+ global,
6
+ Buffer,
7
+ process
8
+ } from "@dxos/node-std/inject-globals";
9
+
3
10
  // packages/core/echo/echo-pipeline/src/common/codec.ts
4
11
  import { createCodecEncoding } from "@dxos/hypercore";
5
12
  import { schema } from "@dxos/protocols";
@@ -122,7 +129,7 @@ var DataServiceHost = class {
122
129
  const { batch, meta } = message;
123
130
  invariant2(!meta.clientTag, "Unexpected client tag in mutation message", {
124
131
  F: __dxlog_file2,
125
- L: 137,
132
+ L: 138,
126
133
  S: this,
127
134
  A: [
128
135
  "!(meta as any).clientTag",
@@ -134,7 +141,7 @@ var DataServiceHost = class {
134
141
  meta
135
142
  }, {
136
143
  F: __dxlog_file2,
137
- L: 138,
144
+ L: 139,
138
145
  S: this,
139
146
  C: (f, a) => f(...a)
140
147
  });
@@ -178,7 +185,7 @@ var DataServiceHost = class {
178
185
  async write(request) {
179
186
  invariant2(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
180
187
  F: __dxlog_file2,
181
- L: 183,
188
+ L: 184,
182
189
  S: this,
183
190
  A: [
184
191
  "!this._ctx.disposed",
@@ -187,7 +194,7 @@ var DataServiceHost = class {
187
194
  });
188
195
  invariant2(this._writeStream, "Cannot write mutations in readonly mode", {
189
196
  F: __dxlog_file2,
190
- L: 184,
197
+ L: 185,
191
198
  S: this,
192
199
  A: [
193
200
  "this._writeStream",
@@ -199,7 +206,7 @@ var DataServiceHost = class {
199
206
  objectCount: request.batch.objects?.length ?? 0
200
207
  }, {
201
208
  F: __dxlog_file2,
202
- L: 186,
209
+ L: 187,
203
210
  S: this,
204
211
  C: (f, a) => f(...a)
205
212
  });
@@ -213,7 +220,7 @@ var DataServiceHost = class {
213
220
  seq: receipt2.seq
214
221
  }, {
215
222
  F: __dxlog_file2,
216
- L: 195,
223
+ L: 196,
217
224
  S: this,
218
225
  C: (f, a) => f(...a)
219
226
  });
@@ -229,6 +236,9 @@ var DataServiceHost = class {
229
236
  async flush() {
230
237
  await this._flush();
231
238
  }
239
+ getHostInfo() {
240
+ throw new Error("Method not implemented.");
241
+ }
232
242
  syncRepo(request) {
233
243
  throw new Error("Method not implemented.");
234
244
  }
@@ -381,13 +391,13 @@ var DataServiceSubscriptions = class {
381
391
  spaceKey
382
392
  }, {
383
393
  F: __dxlog_file3,
384
- L: 33,
394
+ L: 35,
385
395
  S: this,
386
396
  C: (f, a) => f(...a)
387
397
  });
388
398
  invariant3(!this._spaces.has(spaceKey), void 0, {
389
399
  F: __dxlog_file3,
390
- L: 34,
400
+ L: 36,
391
401
  S: this,
392
402
  A: [
393
403
  "!this._spaces.has(spaceKey)",
@@ -402,7 +412,7 @@ var DataServiceSubscriptions = class {
402
412
  spaceKey
403
413
  }, {
404
414
  F: __dxlog_file3,
405
- L: 40,
415
+ L: 42,
406
416
  S: this,
407
417
  C: (f, a) => f(...a)
408
418
  });
@@ -415,13 +425,14 @@ var DataServiceSubscriptions = class {
415
425
  }
416
426
  };
417
427
  var DataServiceImpl = class {
418
- constructor(_subscriptions) {
428
+ constructor(_subscriptions, _automergeHost) {
419
429
  this._subscriptions = _subscriptions;
430
+ this._automergeHost = _automergeHost;
420
431
  }
421
432
  subscribe(request) {
422
433
  invariant3(request.spaceKey, void 0, {
423
434
  F: __dxlog_file3,
424
- L: 59,
435
+ L: 64,
425
436
  S: this,
426
437
  A: [
427
438
  "request.spaceKey",
@@ -434,7 +445,7 @@ var DataServiceImpl = class {
434
445
  write(request) {
435
446
  invariant3(request.spaceKey, void 0, {
436
447
  F: __dxlog_file3,
437
- L: 66,
448
+ L: 71,
438
449
  S: this,
439
450
  A: [
440
451
  "request.spaceKey",
@@ -443,7 +454,7 @@ var DataServiceImpl = class {
443
454
  });
444
455
  invariant3(request.batch, void 0, {
445
456
  F: __dxlog_file3,
446
- L: 67,
457
+ L: 72,
447
458
  S: this,
448
459
  A: [
449
460
  "request.batch",
@@ -456,7 +467,7 @@ var DataServiceImpl = class {
456
467
  flush(request) {
457
468
  invariant3(request.spaceKey, void 0, {
458
469
  F: __dxlog_file3,
459
- L: 74,
470
+ L: 79,
460
471
  S: this,
461
472
  A: [
462
473
  "request.spaceKey",
@@ -466,11 +477,15 @@ var DataServiceImpl = class {
466
477
  const host = this._subscriptions.getDataService(request.spaceKey) ?? raise(new Error(`space not found: ${request.spaceKey}`));
467
478
  return host.flush();
468
479
  }
480
+ // Automerge specific.
481
+ async getHostInfo(request) {
482
+ return this._automergeHost.getHostInfo();
483
+ }
469
484
  syncRepo(request) {
470
- throw new Error("Method not implemented.");
485
+ return this._automergeHost.syncRepo(request);
471
486
  }
472
487
  sendSyncMessage(request) {
473
- throw new Error("Method not implemented.");
488
+ return this._automergeHost.sendSyncMessage(request);
474
489
  }
475
490
  };
476
491
 
@@ -1517,7 +1532,7 @@ var DataPipeline = class DataPipeline2 {
1517
1532
  write: (data, options) => {
1518
1533
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1519
1534
  F: __dxlog_file9,
1520
- L: 159,
1535
+ L: 164,
1521
1536
  S: this,
1522
1537
  A: [
1523
1538
  "this._pipeline",
@@ -1526,7 +1541,7 @@ var DataPipeline = class DataPipeline2 {
1526
1541
  });
1527
1542
  invariant8(this.currentEpoch, "Epoch is not initialized.", {
1528
1543
  F: __dxlog_file9,
1529
- L: 160,
1544
+ L: 165,
1530
1545
  S: this,
1531
1546
  A: [
1532
1547
  "this.currentEpoch",
@@ -1552,7 +1567,7 @@ var DataPipeline = class DataPipeline2 {
1552
1567
  }
1553
1568
  log8("close", void 0, {
1554
1569
  F: __dxlog_file9,
1555
- L: 184,
1570
+ L: 189,
1556
1571
  S: this,
1557
1572
  C: (f, a) => f(...a)
1558
1573
  });
@@ -1567,7 +1582,7 @@ var DataPipeline = class DataPipeline2 {
1567
1582
  } catch (err) {
1568
1583
  log8.catch(err, void 0, {
1569
1584
  F: __dxlog_file9,
1570
- L: 197,
1585
+ L: 202,
1571
1586
  S: this,
1572
1587
  C: (f, a) => f(...a)
1573
1588
  });
@@ -1592,7 +1607,7 @@ var DataPipeline = class DataPipeline2 {
1592
1607
  let messageCounter = 0;
1593
1608
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1594
1609
  F: __dxlog_file9,
1595
- L: 223,
1610
+ L: 228,
1596
1611
  S: this,
1597
1612
  A: [
1598
1613
  "this._pipeline",
@@ -1608,7 +1623,7 @@ var DataPipeline = class DataPipeline2 {
1608
1623
  seq
1609
1624
  }, {
1610
1625
  F: __dxlog_file9,
1611
- L: 229,
1626
+ L: 234,
1612
1627
  S: this,
1613
1628
  C: (f, a) => f(...a)
1614
1629
  });
@@ -1620,7 +1635,7 @@ var DataPipeline = class DataPipeline2 {
1620
1635
  feedKey
1621
1636
  }, {
1622
1637
  F: __dxlog_file9,
1623
- L: 235,
1638
+ L: 240,
1624
1639
  S: this,
1625
1640
  C: (f, a) => f(...a)
1626
1641
  });
@@ -1643,7 +1658,7 @@ var DataPipeline = class DataPipeline2 {
1643
1658
  spaceKey: this._params.spaceKey.toHex()
1644
1659
  }, {
1645
1660
  F: __dxlog_file9,
1646
- L: 252,
1661
+ L: 257,
1647
1662
  S: this,
1648
1663
  C: (f, a) => f(...a)
1649
1664
  });
@@ -1652,7 +1667,7 @@ var DataPipeline = class DataPipeline2 {
1652
1667
  } catch (err) {
1653
1668
  log8.catch(err, void 0, {
1654
1669
  F: __dxlog_file9,
1655
- L: 262,
1670
+ L: 267,
1656
1671
  S: this,
1657
1672
  C: (f, a) => f(...a)
1658
1673
  });
@@ -1667,7 +1682,7 @@ var DataPipeline = class DataPipeline2 {
1667
1682
  _createSnapshot() {
1668
1683
  invariant8(this.databaseHost, "Database backend is not initialized.", {
1669
1684
  F: __dxlog_file9,
1670
- L: 276,
1685
+ L: 281,
1671
1686
  S: this,
1672
1687
  A: [
1673
1688
  "this.databaseHost",
@@ -1696,7 +1711,7 @@ var DataPipeline = class DataPipeline2 {
1696
1711
  } catch (err) {
1697
1712
  log8.warn("Failed to cache properties", err, {
1698
1713
  F: __dxlog_file9,
1699
- L: 305,
1714
+ L: 310,
1700
1715
  S: this,
1701
1716
  C: (f, a) => f(...a)
1702
1717
  });
@@ -1725,14 +1740,14 @@ var DataPipeline = class DataPipeline2 {
1725
1740
  if (err instanceof CancelledError) {
1726
1741
  log8("Epoch processing cancelled.", void 0, {
1727
1742
  F: __dxlog_file9,
1728
- L: 341,
1743
+ L: 346,
1729
1744
  S: this,
1730
1745
  C: (f, a) => f(...a)
1731
1746
  });
1732
1747
  } else {
1733
1748
  log8.catch(err, void 0, {
1734
1749
  F: __dxlog_file9,
1735
- L: 343,
1750
+ L: 348,
1736
1751
  S: this,
1737
1752
  C: (f, a) => f(...a)
1738
1753
  });
@@ -1745,6 +1760,9 @@ var DataPipeline = class DataPipeline2 {
1745
1760
  return;
1746
1761
  }
1747
1762
  await this._processEpoch(ctx, epoch.subject.assertion);
1763
+ if (epoch.subject.assertion.snapshotCid === void 0) {
1764
+ epoch.subject.assertion.snapshotCid = this.appliedEpoch?.subject.assertion.snapshotCid;
1765
+ }
1748
1766
  this.appliedEpoch = epoch;
1749
1767
  this.onNewEpoch.emit(epoch);
1750
1768
  });
@@ -1752,7 +1770,7 @@ var DataPipeline = class DataPipeline2 {
1752
1770
  async _processEpoch(ctx, epoch) {
1753
1771
  invariant8(this._isOpen, "Space is closed.", {
1754
1772
  F: __dxlog_file9,
1755
- L: 362,
1773
+ L: 372,
1756
1774
  S: this,
1757
1775
  A: [
1758
1776
  "this._isOpen",
@@ -1761,7 +1779,7 @@ var DataPipeline = class DataPipeline2 {
1761
1779
  });
1762
1780
  invariant8(this._pipeline, void 0, {
1763
1781
  F: __dxlog_file9,
1764
- L: 363,
1782
+ L: 373,
1765
1783
  S: this,
1766
1784
  A: [
1767
1785
  "this._pipeline",
@@ -1773,7 +1791,7 @@ var DataPipeline = class DataPipeline2 {
1773
1791
  epoch: omit(epoch, "proof")
1774
1792
  }, {
1775
1793
  F: __dxlog_file9,
1776
- L: 366,
1794
+ L: 376,
1777
1795
  S: this,
1778
1796
  C: (f, a) => f(...a)
1779
1797
  });
@@ -1783,7 +1801,7 @@ var DataPipeline = class DataPipeline2 {
1783
1801
  }
1784
1802
  log8("restarting pipeline from epoch", void 0, {
1785
1803
  F: __dxlog_file9,
1786
- L: 372,
1804
+ L: 382,
1787
1805
  S: this,
1788
1806
  C: (f, a) => f(...a)
1789
1807
  });
@@ -1794,7 +1812,7 @@ var DataPipeline = class DataPipeline2 {
1794
1812
  async waitUntilTimeframe(timeframe) {
1795
1813
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1796
1814
  F: __dxlog_file9,
1797
- L: 379,
1815
+ L: 389,
1798
1816
  S: this,
1799
1817
  A: [
1800
1818
  "this._pipeline",
@@ -1806,7 +1824,7 @@ var DataPipeline = class DataPipeline2 {
1806
1824
  async createEpoch() {
1807
1825
  invariant8(this._pipeline, void 0, {
1808
1826
  F: __dxlog_file9,
1809
- L: 385,
1827
+ L: 395,
1810
1828
  S: this,
1811
1829
  A: [
1812
1830
  "this._pipeline",
@@ -1815,7 +1833,7 @@ var DataPipeline = class DataPipeline2 {
1815
1833
  });
1816
1834
  invariant8(this.currentEpoch, void 0, {
1817
1835
  F: __dxlog_file9,
1818
- L: 386,
1836
+ L: 396,
1819
1837
  S: this,
1820
1838
  A: [
1821
1839
  "this.currentEpoch",
@@ -1846,7 +1864,7 @@ var DataPipeline = class DataPipeline2 {
1846
1864
  } catch (err) {
1847
1865
  log8.catch(err, void 0, {
1848
1866
  F: __dxlog_file9,
1849
- L: 416,
1867
+ L: 426,
1850
1868
  S: this,
1851
1869
  C: (f, a) => f(...a)
1852
1870
  });
@@ -2443,6 +2461,7 @@ var SpaceProtocol = class {
2443
2461
  constructor({ topic, swarmIdentity, networkManager, onSessionAuth, onAuthFailure, blobStore }) {
2444
2462
  this._feeds = /* @__PURE__ */ new Set();
2445
2463
  this._sessions = new ComplexMap5(PublicKey6.hash);
2464
+ this._spaceKey = topic;
2446
2465
  this._networkManager = networkManager;
2447
2466
  this._swarmIdentity = swarmIdentity;
2448
2467
  this._onSessionAuth = onSessionAuth;
@@ -2467,7 +2486,7 @@ var SpaceProtocol = class {
2467
2486
  key: feed.key
2468
2487
  }, {
2469
2488
  F: __dxlog_file12,
2470
- L: 96,
2489
+ L: 99,
2471
2490
  S: this,
2472
2491
  C: (f, a) => f(...a)
2473
2492
  });
@@ -2490,7 +2509,7 @@ var SpaceProtocol = class {
2490
2509
  await this.blobSync.open();
2491
2510
  log11("starting...", void 0, {
2492
2511
  F: __dxlog_file12,
2493
- L: 122,
2512
+ L: 125,
2494
2513
  S: this,
2495
2514
  C: (f, a) => f(...a)
2496
2515
  });
@@ -2500,11 +2519,11 @@ var SpaceProtocol = class {
2500
2519
  peerId: this._swarmIdentity.peerKey,
2501
2520
  topic,
2502
2521
  topology: new MMSTTopology(topologyConfig),
2503
- label: `space swarm ${topic.truncate()}`
2522
+ label: `swarm ${topic.truncate()} for space ${this._spaceKey.truncate()}`
2504
2523
  });
2505
2524
  log11("started", void 0, {
2506
2525
  F: __dxlog_file12,
2507
- L: 132,
2526
+ L: 135,
2508
2527
  S: this,
2509
2528
  C: (f, a) => f(...a)
2510
2529
  });
@@ -2514,14 +2533,14 @@ var SpaceProtocol = class {
2514
2533
  if (this._connection) {
2515
2534
  log11("stopping...", void 0, {
2516
2535
  F: __dxlog_file12,
2517
- L: 139,
2536
+ L: 142,
2518
2537
  S: this,
2519
2538
  C: (f, a) => f(...a)
2520
2539
  });
2521
2540
  await this._connection.close();
2522
2541
  log11("stopped", void 0, {
2523
2542
  F: __dxlog_file12,
2524
- L: 141,
2543
+ L: 144,
2525
2544
  S: this,
2526
2545
  C: (f, a) => f(...a)
2527
2546
  });
@@ -2580,15 +2599,15 @@ var SpaceProtocolSession = class {
2580
2599
  get stream() {
2581
2600
  return this._teleport.stream;
2582
2601
  }
2583
- async open() {
2584
- await this._teleport.open();
2602
+ async open(sessionId) {
2603
+ await this._teleport.open(sessionId);
2585
2604
  this._teleport.addExtension("dxos.mesh.teleport.auth", new AuthExtension({
2586
2605
  provider: this._swarmIdentity.credentialProvider,
2587
2606
  verifier: this._swarmIdentity.credentialAuthenticator,
2588
2607
  onAuthSuccess: () => {
2589
2608
  log11("Peer authenticated", void 0, {
2590
2609
  F: __dxlog_file12,
2591
- L: 238,
2610
+ L: 241,
2592
2611
  S: this,
2593
2612
  C: (f, a) => f(...a)
2594
2613
  });
@@ -2606,7 +2625,7 @@ var SpaceProtocolSession = class {
2606
2625
  async close() {
2607
2626
  log11("close", void 0, {
2608
2627
  F: __dxlog_file12,
2609
- L: 254,
2628
+ L: 257,
2610
2629
  S: this,
2611
2630
  C: (f, a) => f(...a)
2612
2631
  });
@@ -2723,6 +2742,197 @@ SpaceManager = _ts_decorate8([
2723
2742
  trackLeaks4("open", "close")
2724
2743
  ], SpaceManager);
2725
2744
 
2745
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
2746
+ import { Repo, NetworkAdapter, StorageAdapter, cbor } from "@dxos/automerge/automerge-repo";
2747
+ import { Stream as Stream2 } from "@dxos/codec-protobuf";
2748
+ import { invariant as invariant10 } from "@dxos/invariant";
2749
+ import { arrayToBuffer as arrayToBuffer2, bufferToArray } from "@dxos/util";
2750
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
2751
+ var AutomergeHost = class {
2752
+ constructor(storageDirectory) {
2753
+ this._meshNetwork = new MeshNetworkAdapter();
2754
+ this._clientNetwork = new LocalHostNetworkAdapter();
2755
+ this._storage = new AutomergeStorageAdapter(storageDirectory);
2756
+ this._repo = new Repo({
2757
+ network: [
2758
+ // this._meshNetwork,
2759
+ this._clientNetwork
2760
+ ],
2761
+ storage: this._storage,
2762
+ // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
2763
+ sharePolicy: async (peerId, documentId) => true
2764
+ });
2765
+ this._clientNetwork.ready();
2766
+ }
2767
+ get repo() {
2768
+ return this._repo;
2769
+ }
2770
+ //
2771
+ // Methods for client-services.
2772
+ //
2773
+ syncRepo(request) {
2774
+ return this._clientNetwork.syncRepo(request);
2775
+ }
2776
+ sendSyncMessage(request) {
2777
+ return this._clientNetwork.sendSyncMessage(request);
2778
+ }
2779
+ getHostInfo() {
2780
+ return this._clientNetwork.getHostInfo();
2781
+ }
2782
+ };
2783
+ var LocalHostNetworkAdapter = class extends NetworkAdapter {
2784
+ constructor() {
2785
+ super(...arguments);
2786
+ this._peers = /* @__PURE__ */ new Map();
2787
+ }
2788
+ /**
2789
+ * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
2790
+ */
2791
+ ready() {
2792
+ this.emit("ready", {
2793
+ network: this
2794
+ });
2795
+ }
2796
+ connect(peerId) {
2797
+ this.peerId = peerId;
2798
+ }
2799
+ send(message) {
2800
+ const peer = this._peers.get(message.targetId);
2801
+ invariant10(peer, "Peer not found.", {
2802
+ F: __dxlog_file14,
2803
+ L: 96,
2804
+ S: this,
2805
+ A: [
2806
+ "peer",
2807
+ "'Peer not found.'"
2808
+ ]
2809
+ });
2810
+ peer.send(message);
2811
+ }
2812
+ disconnect() {
2813
+ this._peers.forEach((peer) => peer.disconnect());
2814
+ }
2815
+ syncRepo({ id, syncMessage }) {
2816
+ const peerId = this._getPeerId(id);
2817
+ return new Stream2(({ next, close }) => {
2818
+ invariant10(!this._peers.has(peerId), "Peer already connected.", {
2819
+ F: __dxlog_file14,
2820
+ L: 108,
2821
+ S: this,
2822
+ A: [
2823
+ "!this._peers.has(peerId)",
2824
+ "'Peer already connected.'"
2825
+ ]
2826
+ });
2827
+ this._peers.set(peerId, {
2828
+ connected: true,
2829
+ send: (message) => {
2830
+ next({
2831
+ syncMessage: cbor.encode(message)
2832
+ });
2833
+ },
2834
+ disconnect: () => {
2835
+ this._peers.delete(peerId);
2836
+ close();
2837
+ this.emit("peer-disconnected", {
2838
+ peerId
2839
+ });
2840
+ }
2841
+ });
2842
+ this.emit("peer-candidate", {
2843
+ peerId
2844
+ });
2845
+ });
2846
+ }
2847
+ async sendSyncMessage({ id, syncMessage }) {
2848
+ const message = cbor.decode(syncMessage);
2849
+ this.emit("message", message);
2850
+ }
2851
+ getHostInfo() {
2852
+ invariant10(this.peerId, "Peer id not set.", {
2853
+ F: __dxlog_file14,
2854
+ L: 137,
2855
+ S: this,
2856
+ A: [
2857
+ "this.peerId",
2858
+ "'Peer id not set.'"
2859
+ ]
2860
+ });
2861
+ return {
2862
+ peerId: this.peerId
2863
+ };
2864
+ }
2865
+ _getPeerId(id) {
2866
+ return id;
2867
+ }
2868
+ };
2869
+ var MeshNetworkAdapter = class extends NetworkAdapter {
2870
+ connect(peerId) {
2871
+ throw new Error("Method not implemented.");
2872
+ }
2873
+ send(message) {
2874
+ throw new Error("Method not implemented.");
2875
+ }
2876
+ disconnect() {
2877
+ throw new Error("Method not implemented.");
2878
+ }
2879
+ };
2880
+ var AutomergeStorageAdapter = class extends StorageAdapter {
2881
+ constructor(_directory) {
2882
+ super();
2883
+ this._directory = _directory;
2884
+ }
2885
+ async load(key) {
2886
+ const filename = this._getFilename(key);
2887
+ const file = this._directory.getOrCreateFile(filename);
2888
+ const { size } = await file.stat();
2889
+ if (!size || size === 0) {
2890
+ return void 0;
2891
+ }
2892
+ const buffer = await file.read(0, size);
2893
+ return bufferToArray(buffer);
2894
+ }
2895
+ async save(key, data) {
2896
+ const filename = this._getFilename(key);
2897
+ const file = this._directory.getOrCreateFile(filename);
2898
+ await file.write(0, arrayToBuffer2(data));
2899
+ await file.truncate?.(data.length);
2900
+ await file.flush?.();
2901
+ }
2902
+ async remove(key) {
2903
+ const filename = this._getFilename(key);
2904
+ const file = this._directory.getOrCreateFile(filename);
2905
+ await file.truncate?.(0);
2906
+ }
2907
+ async loadRange(keyPrefix) {
2908
+ const filename = this._getFilename(keyPrefix);
2909
+ const entries = await this._directory.list();
2910
+ return Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2911
+ const file = this._directory.getOrCreateFile(entry);
2912
+ const { size } = await file.stat();
2913
+ const buffer = await file.read(0, size);
2914
+ return {
2915
+ key: this._getKeyFromFilename(entry),
2916
+ data: bufferToArray(buffer)
2917
+ };
2918
+ }));
2919
+ }
2920
+ async removeRange(keyPrefix) {
2921
+ const filename = this._getFilename(keyPrefix);
2922
+ const entries = await this._directory.list();
2923
+ await Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2924
+ const file = this._directory.getOrCreateFile(filename);
2925
+ await file.truncate?.(0);
2926
+ }));
2927
+ }
2928
+ _getFilename(key) {
2929
+ return key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-");
2930
+ }
2931
+ _getKeyFromFilename(filename) {
2932
+ return filename.split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"));
2933
+ }
2934
+ };
2935
+
2726
2936
  export {
2727
2937
  codec,
2728
2938
  valueEncoding,
@@ -2747,6 +2957,7 @@ export {
2747
2957
  SpaceProtocol,
2748
2958
  AuthStatus,
2749
2959
  SpaceProtocolSession,
2750
- SpaceManager
2960
+ SpaceManager,
2961
+ AutomergeHost
2751
2962
  };
2752
- //# sourceMappingURL=chunk-MPEQ7BV3.mjs.map
2963
+ //# sourceMappingURL=chunk-MHHS3KW4.mjs.map