@dxos/echo-pipeline 0.3.9-main.eb5df3b → 0.3.9-main.ee839a8

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.
@@ -26,8 +26,8 @@ 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_XWUUSV4Y_exports = {};
30
- __export(chunk_XWUUSV4Y_exports, {
29
+ var chunk_2RIO52RK_exports = {};
30
+ __export(chunk_2RIO52RK_exports, {
31
31
  AuthExtension: () => AuthExtension,
32
32
  AuthStatus: () => AuthStatus,
33
33
  AutomergeHost: () => AutomergeHost,
@@ -54,7 +54,7 @@ __export(chunk_XWUUSV4Y_exports, {
54
54
  startAfter: () => startAfter,
55
55
  valueEncoding: () => valueEncoding
56
56
  });
57
- module.exports = __toCommonJS(chunk_XWUUSV4Y_exports);
57
+ module.exports = __toCommonJS(chunk_2RIO52RK_exports);
58
58
  var import_hypercore = require("@dxos/hypercore");
59
59
  var import_protocols = require("@dxos/protocols");
60
60
  var import_invariant = require("@dxos/invariant");
@@ -150,6 +150,8 @@ var import_util9 = require("@dxos/util");
150
150
  var import_automerge_repo = require("@dxos/automerge/automerge-repo");
151
151
  var import_codec_protobuf2 = require("@dxos/codec-protobuf");
152
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");
153
155
  var import_util10 = require("@dxos/util");
154
156
  var codec = import_protocols.schema.getCodecForType("dxos.echo.feed.FeedMessage");
155
157
  var valueEncoding = (0, import_hypercore.createCodecEncoding)(codec);
@@ -257,7 +259,7 @@ var DataServiceHost = class {
257
259
  const { batch, meta } = message;
258
260
  (0, import_invariant2.invariant)(!meta.clientTag, "Unexpected client tag in mutation message", {
259
261
  F: __dxlog_file2,
260
- L: 137,
262
+ L: 138,
261
263
  S: this,
262
264
  A: [
263
265
  "!(meta as any).clientTag",
@@ -269,7 +271,7 @@ var DataServiceHost = class {
269
271
  meta
270
272
  }, {
271
273
  F: __dxlog_file2,
272
- L: 138,
274
+ L: 139,
273
275
  S: this,
274
276
  C: (f, a) => f(...a)
275
277
  });
@@ -313,7 +315,7 @@ var DataServiceHost = class {
313
315
  async write(request) {
314
316
  (0, import_invariant2.invariant)(!this._ctx.disposed, "Cannot write to closed DataServiceHost", {
315
317
  F: __dxlog_file2,
316
- L: 183,
318
+ L: 184,
317
319
  S: this,
318
320
  A: [
319
321
  "!this._ctx.disposed",
@@ -322,7 +324,7 @@ var DataServiceHost = class {
322
324
  });
323
325
  (0, import_invariant2.invariant)(this._writeStream, "Cannot write mutations in readonly mode", {
324
326
  F: __dxlog_file2,
325
- L: 184,
327
+ L: 185,
326
328
  S: this,
327
329
  A: [
328
330
  "this._writeStream",
@@ -334,7 +336,7 @@ var DataServiceHost = class {
334
336
  objectCount: request.batch.objects?.length ?? 0
335
337
  }, {
336
338
  F: __dxlog_file2,
337
- L: 186,
339
+ L: 187,
338
340
  S: this,
339
341
  C: (f, a) => f(...a)
340
342
  });
@@ -348,7 +350,7 @@ var DataServiceHost = class {
348
350
  seq: receipt2.seq
349
351
  }, {
350
352
  F: __dxlog_file2,
351
- L: 195,
353
+ L: 196,
352
354
  S: this,
353
355
  C: (f, a) => f(...a)
354
356
  });
@@ -364,6 +366,9 @@ var DataServiceHost = class {
364
366
  async flush() {
365
367
  await this._flush();
366
368
  }
369
+ getHostInfo() {
370
+ throw new Error("Method not implemented.");
371
+ }
367
372
  syncRepo(request) {
368
373
  throw new Error("Method not implemented.");
369
374
  }
@@ -496,13 +501,13 @@ var DataServiceSubscriptions = class {
496
501
  spaceKey
497
502
  }, {
498
503
  F: __dxlog_file3,
499
- L: 34,
504
+ L: 35,
500
505
  S: this,
501
506
  C: (f, a) => f(...a)
502
507
  });
503
508
  (0, import_invariant3.invariant)(!this._spaces.has(spaceKey), void 0, {
504
509
  F: __dxlog_file3,
505
- L: 35,
510
+ L: 36,
506
511
  S: this,
507
512
  A: [
508
513
  "!this._spaces.has(spaceKey)",
@@ -517,7 +522,7 @@ var DataServiceSubscriptions = class {
517
522
  spaceKey
518
523
  }, {
519
524
  F: __dxlog_file3,
520
- L: 41,
525
+ L: 42,
521
526
  S: this,
522
527
  C: (f, a) => f(...a)
523
528
  });
@@ -537,7 +542,7 @@ var DataServiceImpl = class {
537
542
  subscribe(request) {
538
543
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
539
544
  F: __dxlog_file3,
540
- L: 63,
545
+ L: 64,
541
546
  S: this,
542
547
  A: [
543
548
  "request.spaceKey",
@@ -550,7 +555,7 @@ var DataServiceImpl = class {
550
555
  write(request) {
551
556
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
552
557
  F: __dxlog_file3,
553
- L: 70,
558
+ L: 71,
554
559
  S: this,
555
560
  A: [
556
561
  "request.spaceKey",
@@ -559,7 +564,7 @@ var DataServiceImpl = class {
559
564
  });
560
565
  (0, import_invariant3.invariant)(request.batch, void 0, {
561
566
  F: __dxlog_file3,
562
- L: 71,
567
+ L: 72,
563
568
  S: this,
564
569
  A: [
565
570
  "request.batch",
@@ -572,7 +577,7 @@ var DataServiceImpl = class {
572
577
  flush(request) {
573
578
  (0, import_invariant3.invariant)(request.spaceKey, void 0, {
574
579
  F: __dxlog_file3,
575
- L: 78,
580
+ L: 79,
576
581
  S: this,
577
582
  A: [
578
583
  "request.spaceKey",
@@ -582,11 +587,15 @@ var DataServiceImpl = class {
582
587
  const host = this._subscriptions.getDataService(request.spaceKey) ?? (0, import_debug.raise)(new Error(`space not found: ${request.spaceKey}`));
583
588
  return host.flush();
584
589
  }
590
+ // Automerge specific.
591
+ async getHostInfo(request) {
592
+ return this._automergeHost.getHostInfo();
593
+ }
585
594
  syncRepo(request) {
586
- throw new Error("Method not implemented.");
595
+ return this._automergeHost.syncRepo(request);
587
596
  }
588
597
  sendSyncMessage(request) {
589
- throw new Error("Method not implemented.");
598
+ return this._automergeHost.sendSyncMessage(request);
590
599
  }
591
600
  };
592
601
  function _ts_decorate(decorators, target, key, desc) {
@@ -2691,7 +2700,7 @@ var SpaceManager = class SpaceManager2 {
2691
2700
  id: this._instanceId
2692
2701
  }), {
2693
2702
  F: __dxlog_file13,
2694
- L: 97,
2703
+ L: 100,
2695
2704
  S: this,
2696
2705
  C: (f, a) => f(...a)
2697
2706
  });
@@ -2699,7 +2708,7 @@ var SpaceManager = class SpaceManager2 {
2699
2708
  spaceKey: metadata.genesisFeedKey
2700
2709
  }, {
2701
2710
  F: __dxlog_file13,
2702
- L: 98,
2711
+ L: 101,
2703
2712
  S: this,
2704
2713
  C: (f, a) => f(...a)
2705
2714
  });
@@ -2729,7 +2738,7 @@ var SpaceManager = class SpaceManager2 {
2729
2738
  id: this._instanceId
2730
2739
  }), {
2731
2740
  F: __dxlog_file13,
2732
- L: 126,
2741
+ L: 129,
2733
2742
  S: this,
2734
2743
  C: (f, a) => f(...a)
2735
2744
  });
@@ -2753,39 +2762,62 @@ var AutomergeHost = class {
2753
2762
  this._storage = new AutomergeStorageAdapter(storageDirectory);
2754
2763
  this._repo = new import_automerge_repo.Repo({
2755
2764
  network: [
2756
- // this._meshNetwork,
2757
- this._clientNetwork
2765
+ this._clientNetwork,
2766
+ this._meshNetwork
2758
2767
  ],
2759
2768
  storage: this._storage,
2760
2769
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
2761
2770
  sharePolicy: async (peerId, documentId) => true
2762
2771
  });
2772
+ this._clientNetwork.ready();
2773
+ this._meshNetwork.ready();
2763
2774
  }
2764
2775
  get repo() {
2765
2776
  return this._repo;
2766
2777
  }
2778
+ async close() {
2779
+ await this._clientNetwork.close();
2780
+ }
2781
+ //
2782
+ // Methods for client-services.
2783
+ //
2767
2784
  syncRepo(request) {
2768
2785
  return this._clientNetwork.syncRepo(request);
2769
2786
  }
2770
2787
  sendSyncMessage(request) {
2771
2788
  return this._clientNetwork.sendSyncMessage(request);
2772
2789
  }
2790
+ getHostInfo() {
2791
+ return this._clientNetwork.getHostInfo();
2792
+ }
2793
+ //
2794
+ // Mesh replication.
2795
+ //
2796
+ createExtension() {
2797
+ return this._meshNetwork.createExtension();
2798
+ }
2773
2799
  };
2774
2800
  var LocalHostNetworkAdapter = class extends import_automerge_repo.NetworkAdapter {
2775
2801
  constructor() {
2776
- super();
2802
+ super(...arguments);
2777
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() {
2778
2809
  this.emit("ready", {
2779
2810
  network: this
2780
2811
  });
2781
2812
  }
2782
2813
  connect(peerId) {
2814
+ this.peerId = peerId;
2783
2815
  }
2784
2816
  send(message) {
2785
2817
  const peer = this._peers.get(message.targetId);
2786
2818
  (0, import_invariant10.invariant)(peer, "Peer not found.", {
2787
2819
  F: __dxlog_file14,
2788
- L: 83,
2820
+ L: 108,
2789
2821
  S: this,
2790
2822
  A: [
2791
2823
  "peer",
@@ -2794,15 +2826,18 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo.NetworkAdapter
2794
2826
  });
2795
2827
  peer.send(message);
2796
2828
  }
2829
+ async close() {
2830
+ this._peers.forEach((peer) => peer.disconnect());
2831
+ this.emit("close");
2832
+ }
2797
2833
  disconnect() {
2798
- throw new Error("Method not implemented.");
2799
2834
  }
2800
2835
  syncRepo({ id, syncMessage }) {
2801
2836
  const peerId = this._getPeerId(id);
2802
2837
  return new import_codec_protobuf2.Stream(({ next, close }) => {
2803
2838
  (0, import_invariant10.invariant)(!this._peers.has(peerId), "Peer already connected.", {
2804
2839
  F: __dxlog_file14,
2805
- L: 95,
2840
+ L: 126,
2806
2841
  S: this,
2807
2842
  A: [
2808
2843
  "!this._peers.has(peerId)",
@@ -2833,19 +2868,95 @@ var LocalHostNetworkAdapter = class extends import_automerge_repo.NetworkAdapter
2833
2868
  const message = import_automerge_repo.cbor.decode(syncMessage);
2834
2869
  this.emit("message", message);
2835
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
+ }
2836
2885
  _getPeerId(id) {
2837
2886
  return id;
2838
2887
  }
2839
2888
  };
2840
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
+ }
2841
2902
  connect(peerId) {
2842
- throw new Error("Method not implemented.");
2903
+ this.peerId = peerId;
2843
2904
  }
2844
2905
  send(message) {
2845
- throw new Error("Method not implemented.");
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
+ }));
2846
2925
  }
2847
2926
  disconnect() {
2848
- throw new Error("Method not implemented.");
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;
2849
2960
  }
2850
2961
  };
2851
2962
  var AutomergeStorageAdapter = class extends import_automerge_repo.StorageAdapter {
@@ -2857,6 +2968,9 @@ var AutomergeStorageAdapter = class extends import_automerge_repo.StorageAdapter
2857
2968
  const filename = this._getFilename(key);
2858
2969
  const file = this._directory.getOrCreateFile(filename);
2859
2970
  const { size } = await file.stat();
2971
+ if (!size || size === 0) {
2972
+ return void 0;
2973
+ }
2860
2974
  const buffer = await file.read(0, size);
2861
2975
  return (0, import_util10.bufferToArray)(buffer);
2862
2976
  }
@@ -2928,4 +3042,4 @@ var AutomergeStorageAdapter = class extends import_automerge_repo.StorageAdapter
2928
3042
  startAfter,
2929
3043
  valueEncoding
2930
3044
  });
2931
- //# sourceMappingURL=chunk-XWUUSV4Y.cjs.map
3045
+ //# sourceMappingURL=chunk-2RIO52RK.cjs.map