@dxos/echo-pipeline 0.3.9-main.604ae36 → 0.3.9-main.622f05b

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.
@@ -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: 34,
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: 35,
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: 41,
415
+ L: 42,
406
416
  S: this,
407
417
  C: (f, a) => f(...a)
408
418
  });
@@ -422,7 +432,7 @@ var DataServiceImpl = class {
422
432
  subscribe(request) {
423
433
  invariant3(request.spaceKey, void 0, {
424
434
  F: __dxlog_file3,
425
- L: 63,
435
+ L: 64,
426
436
  S: this,
427
437
  A: [
428
438
  "request.spaceKey",
@@ -435,7 +445,7 @@ var DataServiceImpl = class {
435
445
  write(request) {
436
446
  invariant3(request.spaceKey, void 0, {
437
447
  F: __dxlog_file3,
438
- L: 70,
448
+ L: 71,
439
449
  S: this,
440
450
  A: [
441
451
  "request.spaceKey",
@@ -444,7 +454,7 @@ var DataServiceImpl = class {
444
454
  });
445
455
  invariant3(request.batch, void 0, {
446
456
  F: __dxlog_file3,
447
- L: 71,
457
+ L: 72,
448
458
  S: this,
449
459
  A: [
450
460
  "request.batch",
@@ -457,7 +467,7 @@ var DataServiceImpl = class {
457
467
  flush(request) {
458
468
  invariant3(request.spaceKey, void 0, {
459
469
  F: __dxlog_file3,
460
- L: 78,
470
+ L: 79,
461
471
  S: this,
462
472
  A: [
463
473
  "request.spaceKey",
@@ -467,11 +477,15 @@ var DataServiceImpl = class {
467
477
  const host = this._subscriptions.getDataService(request.spaceKey) ?? raise(new Error(`space not found: ${request.spaceKey}`));
468
478
  return host.flush();
469
479
  }
480
+ // Automerge specific.
481
+ async getHostInfo(request) {
482
+ return this._automergeHost.getHostInfo();
483
+ }
470
484
  syncRepo(request) {
471
- throw new Error("Method not implemented.");
485
+ return this._automergeHost.syncRepo(request);
472
486
  }
473
487
  sendSyncMessage(request) {
474
- throw new Error("Method not implemented.");
488
+ return this._automergeHost.sendSyncMessage(request);
475
489
  }
476
490
  };
477
491
 
@@ -2673,7 +2687,7 @@ var SpaceManager = class SpaceManager2 {
2673
2687
  id: this._instanceId
2674
2688
  }), {
2675
2689
  F: __dxlog_file13,
2676
- L: 97,
2690
+ L: 100,
2677
2691
  S: this,
2678
2692
  C: (f, a) => f(...a)
2679
2693
  });
@@ -2681,7 +2695,7 @@ var SpaceManager = class SpaceManager2 {
2681
2695
  spaceKey: metadata.genesisFeedKey
2682
2696
  }, {
2683
2697
  F: __dxlog_file13,
2684
- L: 98,
2698
+ L: 101,
2685
2699
  S: this,
2686
2700
  C: (f, a) => f(...a)
2687
2701
  });
@@ -2711,7 +2725,7 @@ var SpaceManager = class SpaceManager2 {
2711
2725
  id: this._instanceId
2712
2726
  }), {
2713
2727
  F: __dxlog_file13,
2714
- L: 126,
2728
+ L: 129,
2715
2729
  S: this,
2716
2730
  C: (f, a) => f(...a)
2717
2731
  });
@@ -2732,6 +2746,8 @@ SpaceManager = _ts_decorate8([
2732
2746
  import { Repo, NetworkAdapter, StorageAdapter, cbor } from "@dxos/automerge/automerge-repo";
2733
2747
  import { Stream as Stream2 } from "@dxos/codec-protobuf";
2734
2748
  import { invariant as invariant10 } from "@dxos/invariant";
2749
+ import { log as log13 } from "@dxos/log";
2750
+ import { AutomergeReplicator } from "@dxos/teleport-extension-automerge-replicator";
2735
2751
  import { arrayToBuffer as arrayToBuffer2, bufferToArray } from "@dxos/util";
2736
2752
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/echo/echo-pipeline/src/automerge/automerge-host.ts";
2737
2753
  var AutomergeHost = class {
@@ -2741,39 +2757,62 @@ var AutomergeHost = class {
2741
2757
  this._storage = new AutomergeStorageAdapter(storageDirectory);
2742
2758
  this._repo = new Repo({
2743
2759
  network: [
2744
- // this._meshNetwork,
2745
- this._clientNetwork
2760
+ this._clientNetwork,
2761
+ this._meshNetwork
2746
2762
  ],
2747
2763
  storage: this._storage,
2748
2764
  // TODO(dmaretskyi): Share based on HALO permissions and space affinity.
2749
2765
  sharePolicy: async (peerId, documentId) => true
2750
2766
  });
2767
+ this._clientNetwork.ready();
2768
+ this._meshNetwork.ready();
2751
2769
  }
2752
2770
  get repo() {
2753
2771
  return this._repo;
2754
2772
  }
2773
+ async close() {
2774
+ await this._clientNetwork.close();
2775
+ }
2776
+ //
2777
+ // Methods for client-services.
2778
+ //
2755
2779
  syncRepo(request) {
2756
2780
  return this._clientNetwork.syncRepo(request);
2757
2781
  }
2758
2782
  sendSyncMessage(request) {
2759
2783
  return this._clientNetwork.sendSyncMessage(request);
2760
2784
  }
2785
+ getHostInfo() {
2786
+ return this._clientNetwork.getHostInfo();
2787
+ }
2788
+ //
2789
+ // Mesh replication.
2790
+ //
2791
+ createExtension() {
2792
+ return this._meshNetwork.createExtension();
2793
+ }
2761
2794
  };
2762
2795
  var LocalHostNetworkAdapter = class extends NetworkAdapter {
2763
2796
  constructor() {
2764
- super();
2797
+ super(...arguments);
2765
2798
  this._peers = /* @__PURE__ */ new Map();
2799
+ }
2800
+ /**
2801
+ * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
2802
+ */
2803
+ ready() {
2766
2804
  this.emit("ready", {
2767
2805
  network: this
2768
2806
  });
2769
2807
  }
2770
2808
  connect(peerId) {
2809
+ this.peerId = peerId;
2771
2810
  }
2772
2811
  send(message) {
2773
2812
  const peer = this._peers.get(message.targetId);
2774
2813
  invariant10(peer, "Peer not found.", {
2775
2814
  F: __dxlog_file14,
2776
- L: 83,
2815
+ L: 108,
2777
2816
  S: this,
2778
2817
  A: [
2779
2818
  "peer",
@@ -2782,15 +2821,18 @@ var LocalHostNetworkAdapter = class extends NetworkAdapter {
2782
2821
  });
2783
2822
  peer.send(message);
2784
2823
  }
2824
+ async close() {
2825
+ this._peers.forEach((peer) => peer.disconnect());
2826
+ this.emit("close");
2827
+ }
2785
2828
  disconnect() {
2786
- throw new Error("Method not implemented.");
2787
2829
  }
2788
2830
  syncRepo({ id, syncMessage }) {
2789
2831
  const peerId = this._getPeerId(id);
2790
2832
  return new Stream2(({ next, close }) => {
2791
2833
  invariant10(!this._peers.has(peerId), "Peer already connected.", {
2792
2834
  F: __dxlog_file14,
2793
- L: 95,
2835
+ L: 126,
2794
2836
  S: this,
2795
2837
  A: [
2796
2838
  "!this._peers.has(peerId)",
@@ -2821,19 +2863,95 @@ var LocalHostNetworkAdapter = class extends NetworkAdapter {
2821
2863
  const message = cbor.decode(syncMessage);
2822
2864
  this.emit("message", message);
2823
2865
  }
2866
+ getHostInfo() {
2867
+ invariant10(this.peerId, "Peer id not set.", {
2868
+ F: __dxlog_file14,
2869
+ L: 155,
2870
+ S: this,
2871
+ A: [
2872
+ "this.peerId",
2873
+ "'Peer id not set.'"
2874
+ ]
2875
+ });
2876
+ return {
2877
+ peerId: this.peerId
2878
+ };
2879
+ }
2824
2880
  _getPeerId(id) {
2825
2881
  return id;
2826
2882
  }
2827
2883
  };
2828
2884
  var MeshNetworkAdapter = class extends NetworkAdapter {
2885
+ constructor() {
2886
+ super(...arguments);
2887
+ this._extensions = /* @__PURE__ */ new Map();
2888
+ }
2889
+ /**
2890
+ * Emits `ready` event. That signals to `Repo` that it can start using the adapter.
2891
+ */
2892
+ ready() {
2893
+ this.emit("ready", {
2894
+ network: this
2895
+ });
2896
+ }
2829
2897
  connect(peerId) {
2830
- throw new Error("Method not implemented.");
2898
+ this.peerId = peerId;
2831
2899
  }
2832
2900
  send(message) {
2833
- throw new Error("Method not implemented.");
2901
+ const receiverId = message.targetId;
2902
+ const extension = this._extensions.get(receiverId);
2903
+ invariant10(extension, "Extension not found.", {
2904
+ F: __dxlog_file14,
2905
+ L: 190,
2906
+ S: this,
2907
+ A: [
2908
+ "extension",
2909
+ "'Extension not found.'"
2910
+ ]
2911
+ });
2912
+ extension.sendSyncMessage({
2913
+ payload: cbor.encode(message)
2914
+ }).catch((err) => log13.catch(err, void 0, {
2915
+ F: __dxlog_file14,
2916
+ L: 191,
2917
+ S: this,
2918
+ C: (f, a) => f(...a)
2919
+ }));
2834
2920
  }
2835
2921
  disconnect() {
2836
- throw new Error("Method not implemented.");
2922
+ }
2923
+ createExtension() {
2924
+ invariant10(this.peerId, "Peer id not set.", {
2925
+ F: __dxlog_file14,
2926
+ L: 199,
2927
+ S: this,
2928
+ A: [
2929
+ "this.peerId",
2930
+ "'Peer id not set.'"
2931
+ ]
2932
+ });
2933
+ let peerInfo;
2934
+ const extension = new AutomergeReplicator({
2935
+ peerId: this.peerId
2936
+ }, {
2937
+ onStartReplication: async (info) => {
2938
+ peerInfo = info;
2939
+ this._extensions.set(info.id, extension);
2940
+ this.emit("peer-candidate", {
2941
+ peerId: info.id
2942
+ });
2943
+ },
2944
+ onSyncMessage: async ({ payload }) => {
2945
+ const message = cbor.decode(payload);
2946
+ this.emit("message", message);
2947
+ },
2948
+ onClose: async () => {
2949
+ peerInfo && this.emit("peer-disconnected", {
2950
+ peerId: peerInfo.id
2951
+ });
2952
+ }
2953
+ });
2954
+ return extension;
2837
2955
  }
2838
2956
  };
2839
2957
  var AutomergeStorageAdapter = class extends StorageAdapter {
@@ -2845,6 +2963,9 @@ var AutomergeStorageAdapter = class extends StorageAdapter {
2845
2963
  const filename = this._getFilename(key);
2846
2964
  const file = this._directory.getOrCreateFile(filename);
2847
2965
  const { size } = await file.stat();
2966
+ if (!size || size === 0) {
2967
+ return void 0;
2968
+ }
2848
2969
  const buffer = await file.read(0, size);
2849
2970
  return bufferToArray(buffer);
2850
2971
  }
@@ -2916,4 +3037,4 @@ export {
2916
3037
  SpaceManager,
2917
3038
  AutomergeHost
2918
3039
  };
2919
- //# sourceMappingURL=chunk-CLHJJVQE.mjs.map
3040
+ //# sourceMappingURL=chunk-6XOL2DVG.mjs.map