@dxos/echo-pipeline 0.3.8-next.f4e0086 → 0.3.9-main.03b62b6

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 (44) hide show
  1. package/dist/lib/browser/{chunk-KD6OVLI6.mjs → chunk-CLHJJVQE.mjs} +253 -49
  2. package/dist/lib/browser/chunk-CLHJJVQE.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-XWUUSV4Y.cjs +2931 -0
  8. package/dist/lib/node/chunk-XWUUSV4Y.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +30 -2727
  10. package/dist/lib/node/index.cjs.map +4 -4
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/testing/index.cjs +93 -2798
  13. package/dist/lib/node/testing/index.cjs.map +4 -4
  14. package/dist/types/src/automerge/automerge-host.d.ts +15 -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 +3 -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 +6 -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 +52 -35
  32. package/src/automerge/automerge-host.test.ts +42 -0
  33. package/src/automerge/automerge-host.ts +212 -0
  34. package/src/automerge/index.ts +5 -0
  35. package/src/db-host/data-service-host.ts +15 -1
  36. package/src/db-host/data-service.ts +15 -1
  37. package/src/index.ts +1 -0
  38. package/src/space/data-pipeline.ts +49 -2
  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-KD6OVLI6.mjs.map +0 -7
  43. package/testing.d.ts +0 -11
  44. package/testing.js +0 -5
@@ -122,7 +122,7 @@ var DataServiceHost = class {
122
122
  const { batch, meta } = message;
123
123
  invariant2(!meta.clientTag, "Unexpected client tag in mutation message", {
124
124
  F: __dxlog_file2,
125
- L: 131,
125
+ L: 137,
126
126
  S: this,
127
127
  A: [
128
128
  "!(meta as any).clientTag",
@@ -134,7 +134,7 @@ var DataServiceHost = class {
134
134
  meta
135
135
  }, {
136
136
  F: __dxlog_file2,
137
- L: 132,
137
+ L: 138,
138
138
  S: this,
139
139
  C: (f, a) => f(...a)
140
140
  });
@@ -178,7 +178,7 @@ var DataServiceHost = class {
178
178
  async write(request) {
179
179
  invariant2(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
180
180
  F: __dxlog_file2,
181
- L: 177,
181
+ L: 183,
182
182
  S: this,
183
183
  A: [
184
184
  "!this._ctx.disposed",
@@ -187,7 +187,7 @@ var DataServiceHost = class {
187
187
  });
188
188
  invariant2(this._writeStream, "Cannot write mutations in readonly mode", {
189
189
  F: __dxlog_file2,
190
- L: 178,
190
+ L: 184,
191
191
  S: this,
192
192
  A: [
193
193
  "this._writeStream",
@@ -199,7 +199,7 @@ var DataServiceHost = class {
199
199
  objectCount: request.batch.objects?.length ?? 0
200
200
  }, {
201
201
  F: __dxlog_file2,
202
- L: 180,
202
+ L: 186,
203
203
  S: this,
204
204
  C: (f, a) => f(...a)
205
205
  });
@@ -213,7 +213,7 @@ var DataServiceHost = class {
213
213
  seq: receipt2.seq
214
214
  }, {
215
215
  F: __dxlog_file2,
216
- L: 189,
216
+ L: 195,
217
217
  S: this,
218
218
  C: (f, a) => f(...a)
219
219
  });
@@ -229,6 +229,12 @@ var DataServiceHost = class {
229
229
  async flush() {
230
230
  await this._flush();
231
231
  }
232
+ syncRepo(request) {
233
+ throw new Error("Method not implemented.");
234
+ }
235
+ sendSyncMessage(request) {
236
+ throw new Error("Method not implemented.");
237
+ }
232
238
  };
233
239
  var createDataMessage = (batch) => ({
234
240
  batch: {
@@ -375,13 +381,13 @@ var DataServiceSubscriptions = class {
375
381
  spaceKey
376
382
  }, {
377
383
  F: __dxlog_file3,
378
- L: 31,
384
+ L: 34,
379
385
  S: this,
380
386
  C: (f, a) => f(...a)
381
387
  });
382
388
  invariant3(!this._spaces.has(spaceKey), void 0, {
383
389
  F: __dxlog_file3,
384
- L: 32,
390
+ L: 35,
385
391
  S: this,
386
392
  A: [
387
393
  "!this._spaces.has(spaceKey)",
@@ -396,7 +402,7 @@ var DataServiceSubscriptions = class {
396
402
  spaceKey
397
403
  }, {
398
404
  F: __dxlog_file3,
399
- L: 38,
405
+ L: 41,
400
406
  S: this,
401
407
  C: (f, a) => f(...a)
402
408
  });
@@ -409,13 +415,14 @@ var DataServiceSubscriptions = class {
409
415
  }
410
416
  };
411
417
  var DataServiceImpl = class {
412
- constructor(_subscriptions) {
418
+ constructor(_subscriptions, _automergeHost) {
413
419
  this._subscriptions = _subscriptions;
420
+ this._automergeHost = _automergeHost;
414
421
  }
415
422
  subscribe(request) {
416
423
  invariant3(request.spaceKey, void 0, {
417
424
  F: __dxlog_file3,
418
- L: 57,
425
+ L: 63,
419
426
  S: this,
420
427
  A: [
421
428
  "request.spaceKey",
@@ -428,7 +435,7 @@ var DataServiceImpl = class {
428
435
  write(request) {
429
436
  invariant3(request.spaceKey, void 0, {
430
437
  F: __dxlog_file3,
431
- L: 64,
438
+ L: 70,
432
439
  S: this,
433
440
  A: [
434
441
  "request.spaceKey",
@@ -437,7 +444,7 @@ var DataServiceImpl = class {
437
444
  });
438
445
  invariant3(request.batch, void 0, {
439
446
  F: __dxlog_file3,
440
- L: 65,
447
+ L: 71,
441
448
  S: this,
442
449
  A: [
443
450
  "request.batch",
@@ -450,7 +457,7 @@ var DataServiceImpl = class {
450
457
  flush(request) {
451
458
  invariant3(request.spaceKey, void 0, {
452
459
  F: __dxlog_file3,
453
- L: 72,
460
+ L: 78,
454
461
  S: this,
455
462
  A: [
456
463
  "request.spaceKey",
@@ -460,6 +467,12 @@ var DataServiceImpl = class {
460
467
  const host = this._subscriptions.getDataService(request.spaceKey) ?? raise(new Error(`space not found: ${request.spaceKey}`));
461
468
  return host.flush();
462
469
  }
470
+ syncRepo(request) {
471
+ throw new Error("Method not implemented.");
472
+ }
473
+ sendSyncMessage(request) {
474
+ throw new Error("Method not implemented.");
475
+ }
463
476
  };
464
477
 
465
478
  // packages/core/echo/echo-pipeline/src/metadata/metadata-store.ts
@@ -1505,7 +1518,7 @@ var DataPipeline = class DataPipeline2 {
1505
1518
  write: (data, options) => {
1506
1519
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1507
1520
  F: __dxlog_file9,
1508
- L: 159,
1521
+ L: 164,
1509
1522
  S: this,
1510
1523
  A: [
1511
1524
  "this._pipeline",
@@ -1514,7 +1527,7 @@ var DataPipeline = class DataPipeline2 {
1514
1527
  });
1515
1528
  invariant8(this.currentEpoch, "Epoch is not initialized.", {
1516
1529
  F: __dxlog_file9,
1517
- L: 160,
1530
+ L: 165,
1518
1531
  S: this,
1519
1532
  A: [
1520
1533
  "this.currentEpoch",
@@ -1540,7 +1553,7 @@ var DataPipeline = class DataPipeline2 {
1540
1553
  }
1541
1554
  log8("close", void 0, {
1542
1555
  F: __dxlog_file9,
1543
- L: 184,
1556
+ L: 189,
1544
1557
  S: this,
1545
1558
  C: (f, a) => f(...a)
1546
1559
  });
@@ -1555,7 +1568,7 @@ var DataPipeline = class DataPipeline2 {
1555
1568
  } catch (err) {
1556
1569
  log8.catch(err, void 0, {
1557
1570
  F: __dxlog_file9,
1558
- L: 197,
1571
+ L: 202,
1559
1572
  S: this,
1560
1573
  C: (f, a) => f(...a)
1561
1574
  });
@@ -1580,7 +1593,7 @@ var DataPipeline = class DataPipeline2 {
1580
1593
  let messageCounter = 0;
1581
1594
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1582
1595
  F: __dxlog_file9,
1583
- L: 222,
1596
+ L: 228,
1584
1597
  S: this,
1585
1598
  A: [
1586
1599
  "this._pipeline",
@@ -1596,7 +1609,7 @@ var DataPipeline = class DataPipeline2 {
1596
1609
  seq
1597
1610
  }, {
1598
1611
  F: __dxlog_file9,
1599
- L: 228,
1612
+ L: 234,
1600
1613
  S: this,
1601
1614
  C: (f, a) => f(...a)
1602
1615
  });
@@ -1608,7 +1621,7 @@ var DataPipeline = class DataPipeline2 {
1608
1621
  feedKey
1609
1622
  }, {
1610
1623
  F: __dxlog_file9,
1611
- L: 234,
1624
+ L: 240,
1612
1625
  S: this,
1613
1626
  C: (f, a) => f(...a)
1614
1627
  });
@@ -1631,7 +1644,7 @@ var DataPipeline = class DataPipeline2 {
1631
1644
  spaceKey: this._params.spaceKey.toHex()
1632
1645
  }, {
1633
1646
  F: __dxlog_file9,
1634
- L: 251,
1647
+ L: 257,
1635
1648
  S: this,
1636
1649
  C: (f, a) => f(...a)
1637
1650
  });
@@ -1640,22 +1653,22 @@ var DataPipeline = class DataPipeline2 {
1640
1653
  } catch (err) {
1641
1654
  log8.catch(err, void 0, {
1642
1655
  F: __dxlog_file9,
1643
- L: 261,
1656
+ L: 267,
1644
1657
  S: this,
1645
1658
  C: (f, a) => f(...a)
1646
1659
  });
1647
1660
  }
1648
1661
  span.end();
1649
- if (++messageCounter > 1e3) {
1662
+ if (++messageCounter > 100) {
1650
1663
  messageCounter = 0;
1651
- await sleep(1);
1664
+ await idle(1e3);
1652
1665
  }
1653
1666
  }
1654
1667
  }
1655
1668
  _createSnapshot() {
1656
1669
  invariant8(this.databaseHost, "Database backend is not initialized.", {
1657
1670
  F: __dxlog_file9,
1658
- L: 275,
1671
+ L: 281,
1659
1672
  S: this,
1660
1673
  A: [
1661
1674
  "this.databaseHost",
@@ -1684,7 +1697,7 @@ var DataPipeline = class DataPipeline2 {
1684
1697
  } catch (err) {
1685
1698
  log8.warn("Failed to cache properties", err, {
1686
1699
  F: __dxlog_file9,
1687
- L: 304,
1700
+ L: 310,
1688
1701
  S: this,
1689
1702
  C: (f, a) => f(...a)
1690
1703
  });
@@ -1713,14 +1726,14 @@ var DataPipeline = class DataPipeline2 {
1713
1726
  if (err instanceof CancelledError) {
1714
1727
  log8("Epoch processing cancelled.", void 0, {
1715
1728
  F: __dxlog_file9,
1716
- L: 340,
1729
+ L: 346,
1717
1730
  S: this,
1718
1731
  C: (f, a) => f(...a)
1719
1732
  });
1720
1733
  } else {
1721
1734
  log8.catch(err, void 0, {
1722
1735
  F: __dxlog_file9,
1723
- L: 342,
1736
+ L: 348,
1724
1737
  S: this,
1725
1738
  C: (f, a) => f(...a)
1726
1739
  });
@@ -1733,6 +1746,9 @@ var DataPipeline = class DataPipeline2 {
1733
1746
  return;
1734
1747
  }
1735
1748
  await this._processEpoch(ctx, epoch.subject.assertion);
1749
+ if (epoch.subject.assertion.snapshotCid === void 0) {
1750
+ epoch.subject.assertion.snapshotCid = this.appliedEpoch?.subject.assertion.snapshotCid;
1751
+ }
1736
1752
  this.appliedEpoch = epoch;
1737
1753
  this.onNewEpoch.emit(epoch);
1738
1754
  });
@@ -1740,7 +1756,7 @@ var DataPipeline = class DataPipeline2 {
1740
1756
  async _processEpoch(ctx, epoch) {
1741
1757
  invariant8(this._isOpen, "Space is closed.", {
1742
1758
  F: __dxlog_file9,
1743
- L: 361,
1759
+ L: 372,
1744
1760
  S: this,
1745
1761
  A: [
1746
1762
  "this._isOpen",
@@ -1749,7 +1765,7 @@ var DataPipeline = class DataPipeline2 {
1749
1765
  });
1750
1766
  invariant8(this._pipeline, void 0, {
1751
1767
  F: __dxlog_file9,
1752
- L: 362,
1768
+ L: 373,
1753
1769
  S: this,
1754
1770
  A: [
1755
1771
  "this._pipeline",
@@ -1761,7 +1777,7 @@ var DataPipeline = class DataPipeline2 {
1761
1777
  epoch: omit(epoch, "proof")
1762
1778
  }, {
1763
1779
  F: __dxlog_file9,
1764
- L: 365,
1780
+ L: 376,
1765
1781
  S: this,
1766
1782
  C: (f, a) => f(...a)
1767
1783
  });
@@ -1771,7 +1787,7 @@ var DataPipeline = class DataPipeline2 {
1771
1787
  }
1772
1788
  log8("restarting pipeline from epoch", void 0, {
1773
1789
  F: __dxlog_file9,
1774
- L: 371,
1790
+ L: 382,
1775
1791
  S: this,
1776
1792
  C: (f, a) => f(...a)
1777
1793
  });
@@ -1782,7 +1798,7 @@ var DataPipeline = class DataPipeline2 {
1782
1798
  async waitUntilTimeframe(timeframe) {
1783
1799
  invariant8(this._pipeline, "Pipeline is not initialized.", {
1784
1800
  F: __dxlog_file9,
1785
- L: 378,
1801
+ L: 389,
1786
1802
  S: this,
1787
1803
  A: [
1788
1804
  "this._pipeline",
@@ -1794,7 +1810,7 @@ var DataPipeline = class DataPipeline2 {
1794
1810
  async createEpoch() {
1795
1811
  invariant8(this._pipeline, void 0, {
1796
1812
  F: __dxlog_file9,
1797
- L: 384,
1813
+ L: 395,
1798
1814
  S: this,
1799
1815
  A: [
1800
1816
  "this._pipeline",
@@ -1803,7 +1819,7 @@ var DataPipeline = class DataPipeline2 {
1803
1819
  });
1804
1820
  invariant8(this.currentEpoch, void 0, {
1805
1821
  F: __dxlog_file9,
1806
- L: 385,
1822
+ L: 396,
1807
1823
  S: this,
1808
1824
  A: [
1809
1825
  "this.currentEpoch",
@@ -1834,7 +1850,7 @@ var DataPipeline = class DataPipeline2 {
1834
1850
  } catch (err) {
1835
1851
  log8.catch(err, void 0, {
1836
1852
  F: __dxlog_file9,
1837
- L: 415,
1853
+ L: 426,
1838
1854
  S: this,
1839
1855
  C: (f, a) => f(...a)
1840
1856
  });
@@ -1864,6 +1880,31 @@ DataPipeline = _ts_decorate4([
1864
1880
  trackLeaks("open", "close"),
1865
1881
  trace.resource()
1866
1882
  ], DataPipeline);
1883
+ var idle = async (timeout) => {
1884
+ if (!("scheduler" in globalThis && typeof globalThis.scheduler.postTask === "function")) {
1885
+ await sleep(1);
1886
+ return;
1887
+ }
1888
+ await new Promise((resolve) => {
1889
+ const cleanup = () => {
1890
+ clearTimeout(timer);
1891
+ controller.abort();
1892
+ };
1893
+ const controller = new AbortController();
1894
+ void globalThis.scheduler.postTask(() => {
1895
+ cleanup();
1896
+ resolve();
1897
+ }, {
1898
+ priority: "background",
1899
+ signal: controller.signal
1900
+ }).catch(() => {
1901
+ });
1902
+ const timer = setTimeout(() => {
1903
+ cleanup();
1904
+ resolve();
1905
+ }, timeout);
1906
+ });
1907
+ };
1867
1908
 
1868
1909
  // packages/core/echo/echo-pipeline/src/space/space.ts
1869
1910
  import { Event as Event5, synchronized as synchronized4, trackLeaks as trackLeaks3, Lock } from "@dxos/async";
@@ -2406,6 +2447,7 @@ var SpaceProtocol = class {
2406
2447
  constructor({ topic, swarmIdentity, networkManager, onSessionAuth, onAuthFailure, blobStore }) {
2407
2448
  this._feeds = /* @__PURE__ */ new Set();
2408
2449
  this._sessions = new ComplexMap5(PublicKey6.hash);
2450
+ this._spaceKey = topic;
2409
2451
  this._networkManager = networkManager;
2410
2452
  this._swarmIdentity = swarmIdentity;
2411
2453
  this._onSessionAuth = onSessionAuth;
@@ -2430,7 +2472,7 @@ var SpaceProtocol = class {
2430
2472
  key: feed.key
2431
2473
  }, {
2432
2474
  F: __dxlog_file12,
2433
- L: 96,
2475
+ L: 99,
2434
2476
  S: this,
2435
2477
  C: (f, a) => f(...a)
2436
2478
  });
@@ -2453,7 +2495,7 @@ var SpaceProtocol = class {
2453
2495
  await this.blobSync.open();
2454
2496
  log11("starting...", void 0, {
2455
2497
  F: __dxlog_file12,
2456
- L: 122,
2498
+ L: 125,
2457
2499
  S: this,
2458
2500
  C: (f, a) => f(...a)
2459
2501
  });
@@ -2463,11 +2505,11 @@ var SpaceProtocol = class {
2463
2505
  peerId: this._swarmIdentity.peerKey,
2464
2506
  topic,
2465
2507
  topology: new MMSTTopology(topologyConfig),
2466
- label: `space swarm ${topic.truncate()}`
2508
+ label: `swarm ${topic.truncate()} for space ${this._spaceKey.truncate()}`
2467
2509
  });
2468
2510
  log11("started", void 0, {
2469
2511
  F: __dxlog_file12,
2470
- L: 132,
2512
+ L: 135,
2471
2513
  S: this,
2472
2514
  C: (f, a) => f(...a)
2473
2515
  });
@@ -2477,14 +2519,14 @@ var SpaceProtocol = class {
2477
2519
  if (this._connection) {
2478
2520
  log11("stopping...", void 0, {
2479
2521
  F: __dxlog_file12,
2480
- L: 139,
2522
+ L: 142,
2481
2523
  S: this,
2482
2524
  C: (f, a) => f(...a)
2483
2525
  });
2484
2526
  await this._connection.close();
2485
2527
  log11("stopped", void 0, {
2486
2528
  F: __dxlog_file12,
2487
- L: 141,
2529
+ L: 144,
2488
2530
  S: this,
2489
2531
  C: (f, a) => f(...a)
2490
2532
  });
@@ -2543,15 +2585,15 @@ var SpaceProtocolSession = class {
2543
2585
  get stream() {
2544
2586
  return this._teleport.stream;
2545
2587
  }
2546
- async open() {
2547
- await this._teleport.open();
2588
+ async open(sessionId) {
2589
+ await this._teleport.open(sessionId);
2548
2590
  this._teleport.addExtension("dxos.mesh.teleport.auth", new AuthExtension({
2549
2591
  provider: this._swarmIdentity.credentialProvider,
2550
2592
  verifier: this._swarmIdentity.credentialAuthenticator,
2551
2593
  onAuthSuccess: () => {
2552
2594
  log11("Peer authenticated", void 0, {
2553
2595
  F: __dxlog_file12,
2554
- L: 238,
2596
+ L: 241,
2555
2597
  S: this,
2556
2598
  C: (f, a) => f(...a)
2557
2599
  });
@@ -2569,7 +2611,7 @@ var SpaceProtocolSession = class {
2569
2611
  async close() {
2570
2612
  log11("close", void 0, {
2571
2613
  F: __dxlog_file12,
2572
- L: 254,
2614
+ L: 257,
2573
2615
  S: this,
2574
2616
  C: (f, a) => f(...a)
2575
2617
  });
@@ -2686,6 +2728,167 @@ SpaceManager = _ts_decorate8([
2686
2728
  trackLeaks4("open", "close")
2687
2729
  ], SpaceManager);
2688
2730
 
2731
+ // packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts
2732
+ import { Repo, NetworkAdapter, StorageAdapter, cbor } from "@dxos/automerge/automerge-repo";
2733
+ import { Stream as Stream2 } from "@dxos/codec-protobuf";
2734
+ import { invariant as invariant10 } from "@dxos/invariant";
2735
+ import { arrayToBuffer as arrayToBuffer2, bufferToArray } from "@dxos/util";
2736
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
2737
+ var AutomergeHost = class {
2738
+ constructor(storageDirectory) {
2739
+ this._meshNetwork = new MeshNetworkAdapter();
2740
+ this._clientNetwork = new LocalHostNetworkAdapter();
2741
+ this._storage = new AutomergeStorageAdapter(storageDirectory);
2742
+ this._repo = new Repo({
2743
+ network: [
2744
+ // this._meshNetwork,
2745
+ this._clientNetwork
2746
+ ],
2747
+ storage: this._storage,
2748
+ // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
2749
+ sharePolicy: async (peerId, documentId) => true
2750
+ });
2751
+ }
2752
+ get repo() {
2753
+ return this._repo;
2754
+ }
2755
+ syncRepo(request) {
2756
+ return this._clientNetwork.syncRepo(request);
2757
+ }
2758
+ sendSyncMessage(request) {
2759
+ return this._clientNetwork.sendSyncMessage(request);
2760
+ }
2761
+ };
2762
+ var LocalHostNetworkAdapter = class extends NetworkAdapter {
2763
+ constructor() {
2764
+ super();
2765
+ this._peers = /* @__PURE__ */ new Map();
2766
+ this.emit("ready", {
2767
+ network: this
2768
+ });
2769
+ }
2770
+ connect(peerId) {
2771
+ }
2772
+ send(message) {
2773
+ const peer = this._peers.get(message.targetId);
2774
+ invariant10(peer, "Peer not found.", {
2775
+ F: __dxlog_file14,
2776
+ L: 83,
2777
+ S: this,
2778
+ A: [
2779
+ "peer",
2780
+ "'Peer not found.'"
2781
+ ]
2782
+ });
2783
+ peer.send(message);
2784
+ }
2785
+ disconnect() {
2786
+ throw new Error("Method not implemented.");
2787
+ }
2788
+ syncRepo({ id, syncMessage }) {
2789
+ const peerId = this._getPeerId(id);
2790
+ return new Stream2(({ next, close }) => {
2791
+ invariant10(!this._peers.has(peerId), "Peer already connected.", {
2792
+ F: __dxlog_file14,
2793
+ L: 95,
2794
+ S: this,
2795
+ A: [
2796
+ "!this._peers.has(peerId)",
2797
+ "'Peer already connected.'"
2798
+ ]
2799
+ });
2800
+ this._peers.set(peerId, {
2801
+ connected: true,
2802
+ send: (message) => {
2803
+ next({
2804
+ syncMessage: cbor.encode(message)
2805
+ });
2806
+ },
2807
+ disconnect: () => {
2808
+ this._peers.delete(peerId);
2809
+ close();
2810
+ this.emit("peer-disconnected", {
2811
+ peerId
2812
+ });
2813
+ }
2814
+ });
2815
+ this.emit("peer-candidate", {
2816
+ peerId
2817
+ });
2818
+ });
2819
+ }
2820
+ async sendSyncMessage({ id, syncMessage }) {
2821
+ const message = cbor.decode(syncMessage);
2822
+ this.emit("message", message);
2823
+ }
2824
+ _getPeerId(id) {
2825
+ return id;
2826
+ }
2827
+ };
2828
+ var MeshNetworkAdapter = class extends NetworkAdapter {
2829
+ connect(peerId) {
2830
+ throw new Error("Method not implemented.");
2831
+ }
2832
+ send(message) {
2833
+ throw new Error("Method not implemented.");
2834
+ }
2835
+ disconnect() {
2836
+ throw new Error("Method not implemented.");
2837
+ }
2838
+ };
2839
+ var AutomergeStorageAdapter = class extends StorageAdapter {
2840
+ constructor(_directory) {
2841
+ super();
2842
+ this._directory = _directory;
2843
+ }
2844
+ async load(key) {
2845
+ const filename = this._getFilename(key);
2846
+ const file = this._directory.getOrCreateFile(filename);
2847
+ const { size } = await file.stat();
2848
+ const buffer = await file.read(0, size);
2849
+ return bufferToArray(buffer);
2850
+ }
2851
+ async save(key, data) {
2852
+ const filename = this._getFilename(key);
2853
+ const file = this._directory.getOrCreateFile(filename);
2854
+ await file.write(0, arrayToBuffer2(data));
2855
+ await file.truncate?.(data.length);
2856
+ await file.flush?.();
2857
+ }
2858
+ async remove(key) {
2859
+ const filename = this._getFilename(key);
2860
+ const file = this._directory.getOrCreateFile(filename);
2861
+ await file.truncate?.(0);
2862
+ }
2863
+ async loadRange(keyPrefix) {
2864
+ const filename = this._getFilename(keyPrefix);
2865
+ const entries = await this._directory.list();
2866
+ return Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2867
+ const file = this._directory.getOrCreateFile(entry);
2868
+ const { size } = await file.stat();
2869
+ const buffer = await file.read(0, size);
2870
+ return {
2871
+ key: this._getKeyFromFilename(entry),
2872
+ data: bufferToArray(buffer)
2873
+ };
2874
+ }));
2875
+ }
2876
+ async removeRange(keyPrefix) {
2877
+ const filename = this._getFilename(keyPrefix);
2878
+ const entries = await this._directory.list();
2879
+ await Promise.all(entries.filter((entry) => entry.startsWith(filename)).map(async (entry) => {
2880
+ const file = this._directory.getOrCreateFile(filename);
2881
+ await file.truncate?.(0);
2882
+ }));
2883
+ }
2884
+ _getFilename(key) {
2885
+ return key.map((k) => k.replaceAll("%", "%25").replaceAll("-", "%2D")).join("-");
2886
+ }
2887
+ _getKeyFromFilename(filename) {
2888
+ return filename.split("-").map((k) => k.replaceAll("%2D", "-").replaceAll("%25", "%"));
2889
+ }
2890
+ };
2891
+
2689
2892
  export {
2690
2893
  codec,
2691
2894
  valueEncoding,
@@ -2710,6 +2913,7 @@ export {
2710
2913
  SpaceProtocol,
2711
2914
  AuthStatus,
2712
2915
  SpaceProtocolSession,
2713
- SpaceManager
2916
+ SpaceManager,
2917
+ AutomergeHost
2714
2918
  };
2715
- //# sourceMappingURL=chunk-KD6OVLI6.mjs.map
2919
+ //# sourceMappingURL=chunk-CLHJJVQE.mjs.map