@dxos/network-manager 0.1.57 → 0.1.58-main.0009f6a

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 (56) hide show
  1. package/dist/lib/browser/{chunk-UKMTAO4L.mjs → chunk-26KK6XOP.mjs} +266 -140
  2. package/dist/lib/browser/chunk-26KK6XOP.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +10 -7
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +837 -577
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +1125 -865
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection.d.ts +3 -1
  15. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  16. package/dist/types/src/swarm/peer.d.ts +3 -3
  17. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  18. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  19. package/dist/types/src/swarm/swarm.d.ts +2 -2
  20. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  21. package/dist/types/src/testing/test-builder.d.ts +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  23. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  24. package/dist/types/src/tests/tcp-transport.test.d.ts +2 -0
  25. package/dist/types/src/tests/tcp-transport.test.d.ts.map +1 -0
  26. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  27. package/dist/types/src/transport/index.d.ts +1 -0
  28. package/dist/types/src/transport/index.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  30. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  31. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  32. package/dist/types/src/transport/tcp-transport.d.ts +23 -0
  33. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
  34. package/dist/types/src/transport/transport.d.ts +2 -1
  35. package/dist/types/src/transport/transport.d.ts.map +1 -1
  36. package/package.json +18 -18
  37. package/src/swarm/connection-limiter.ts +2 -2
  38. package/src/swarm/connection.test.ts +1 -1
  39. package/src/swarm/connection.ts +54 -27
  40. package/src/swarm/peer.ts +11 -3
  41. package/src/swarm/swarm-mapper.ts +4 -7
  42. package/src/swarm/swarm.test.ts +3 -3
  43. package/src/swarm/swarm.ts +3 -3
  44. package/src/testing/test-builder.ts +6 -2
  45. package/src/tests/basic-test-suite.ts +1 -1
  46. package/src/tests/memory-transport.test.ts +2 -2
  47. package/src/tests/tcp-transport.test.ts +67 -0
  48. package/src/tests/webrtc-transport.test.ts +1 -1
  49. package/src/topology/star-topology.ts +1 -4
  50. package/src/transport/index.ts +1 -0
  51. package/src/transport/simplepeer-transport-proxy.ts +48 -21
  52. package/src/transport/simplepeer-transport-service.ts +9 -5
  53. package/src/transport/simplepeer-transport.ts +43 -4
  54. package/src/transport/tcp-transport.ts +107 -0
  55. package/src/transport/transport.ts +1 -0
  56. package/dist/lib/browser/chunk-UKMTAO4L.mjs.map +0 -7
@@ -513,37 +513,148 @@ module.exports = __toCommonJS(testing_exports);
513
513
 
514
514
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
515
515
  var import_keys14 = require("@dxos/keys");
516
- var import_log16 = require("@dxos/log");
516
+ var import_log17 = require("@dxos/log");
517
517
  var import_messaging2 = require("@dxos/messaging");
518
- var import_protocols6 = require("@dxos/protocols");
518
+ var import_protocols9 = require("@dxos/protocols");
519
519
  var import_services2 = require("@dxos/protocols/proto/dxos/client/services");
520
520
  var import_rpc = require("@dxos/rpc");
521
521
  var import_util11 = require("@dxos/util");
522
522
 
523
+ // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
524
+ var import_async = require("@dxos/async");
525
+ var import_keys = require("@dxos/keys");
526
+ var import_log = require("@dxos/log");
527
+ var import_teleport2 = require("@dxos/teleport");
528
+ var import_util = require("@dxos/util");
529
+
530
+ // packages/core/mesh/network-manager/src/wire-protocol.ts
531
+ var import_teleport = require("@dxos/teleport");
532
+ var createTeleportProtocolFactory = (onConnection) => {
533
+ return (params) => {
534
+ const teleport = new import_teleport.Teleport(params);
535
+ return {
536
+ stream: teleport.stream,
537
+ open: async () => {
538
+ await teleport.open();
539
+ await onConnection(teleport);
540
+ },
541
+ close: async () => {
542
+ await teleport.close();
543
+ },
544
+ abort: async () => {
545
+ await teleport.abort();
546
+ }
547
+ };
548
+ };
549
+ };
550
+
551
+ // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
552
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
553
+ var TestWireProtocol = class {
554
+ constructor(peerId, _extensionFactory = () => []) {
555
+ this.peerId = peerId;
556
+ this._extensionFactory = _extensionFactory;
557
+ this.connections = new import_util.ComplexMap(import_keys.PublicKey.hash);
558
+ this.streamConnections = new import_util.ComplexMap(import_keys.PublicKey.hash);
559
+ this.connected = new import_async.Event();
560
+ this.disconnected = new import_async.Event();
561
+ this.otherConnections = new import_util.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
562
+ this.factory = createTeleportProtocolFactory(async (teleport) => {
563
+ (0, import_log.log)("create", {
564
+ remotePeerId: teleport.remotePeerId
565
+ }, {
566
+ F: __dxlog_file,
567
+ L: 38,
568
+ S: this,
569
+ C: (f, a) => f(...a)
570
+ });
571
+ const extension = new import_teleport2.TestExtension({
572
+ onClose: async () => {
573
+ this.connections.delete(teleport.remotePeerId);
574
+ this.disconnected.emit(teleport.remotePeerId);
575
+ }
576
+ });
577
+ this.connections.set(teleport.remotePeerId, extension);
578
+ teleport.addExtension("test", extension);
579
+ this.connected.emit(teleport.remotePeerId);
580
+ const streamExtension = new import_teleport2.TestExtensionWithStreams({
581
+ onClose: async () => {
582
+ this.streamConnections.delete(teleport.remotePeerId);
583
+ }
584
+ });
585
+ this.streamConnections.set(teleport.remotePeerId, streamExtension);
586
+ teleport.addExtension("test-stream", streamExtension);
587
+ for (const { name, extension: extension2 } of this._extensionFactory()) {
588
+ this.otherConnections.set({
589
+ remotePeerId: teleport.remotePeerId,
590
+ extension: name
591
+ }, extension2);
592
+ teleport.addExtension(name, extension2);
593
+ }
594
+ });
595
+ }
596
+ async waitForConnection(peerId) {
597
+ if (this.connections.has(peerId)) {
598
+ return this.connections.get(peerId);
599
+ }
600
+ (0, import_log.log)("waitForConnection", {
601
+ peerId
602
+ }, {
603
+ F: __dxlog_file,
604
+ L: 67,
605
+ S: this,
606
+ C: (f, a) => f(...a)
607
+ });
608
+ await (0, import_async.asyncTimeout)(
609
+ this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
610
+ // TODO(nf): Make this configurable.
611
+ 1e4
612
+ );
613
+ return this.connections.get(peerId);
614
+ }
615
+ async testConnection(peerId, message) {
616
+ const connection = await this.waitForConnection(peerId);
617
+ await connection.test(message);
618
+ }
619
+ async openStream(peerId, streamTag, streamLoadInterval, streamLoadChunkSize) {
620
+ if (!this.streamConnections.has(peerId)) {
621
+ throw new Error("Connection does not exist.");
622
+ }
623
+ const connection = this.streamConnections.get(peerId);
624
+ return connection.addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag);
625
+ }
626
+ async closeStream(peerId, streamTag) {
627
+ if (!this.streamConnections.has(peerId)) {
628
+ throw new Error("Connection does not exist.");
629
+ }
630
+ const connection = this.streamConnections.get(peerId);
631
+ return connection.closeStream(streamTag);
632
+ }
633
+ };
634
+
523
635
  // packages/core/mesh/network-manager/src/network-manager.ts
524
- var import_async7 = require("@dxos/async");
636
+ var import_async8 = require("@dxos/async");
525
637
  var import_invariant6 = require("@dxos/invariant");
526
- var import_keys8 = require("@dxos/keys");
527
- var import_log7 = require("@dxos/log");
638
+ var import_keys9 = require("@dxos/keys");
639
+ var import_log8 = require("@dxos/log");
528
640
  var import_messaging = require("@dxos/messaging");
529
- var import_protocols4 = require("@dxos/protocols");
641
+ var import_protocols6 = require("@dxos/protocols");
530
642
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
531
- var import_util6 = require("@dxos/util");
643
+ var import_util7 = require("@dxos/util");
532
644
 
533
645
  // packages/core/mesh/network-manager/src/connection-log.ts
534
- var import_async6 = require("@dxos/async");
646
+ var import_async7 = require("@dxos/async");
535
647
  var import_debug3 = require("@dxos/debug");
536
- var import_keys7 = require("@dxos/keys");
537
- var import_util5 = require("@dxos/util");
648
+ var import_keys8 = require("@dxos/keys");
649
+ var import_util6 = require("@dxos/util");
538
650
 
539
651
  // packages/core/mesh/network-manager/src/swarm/connection.ts
540
- var import_async = require("@dxos/async");
652
+ var import_async2 = require("@dxos/async");
541
653
  var import_context = require("@dxos/context");
542
654
  var import_debug = require("@dxos/debug");
543
- var import_errors = require("@dxos/errors");
544
655
  var import_invariant = require("@dxos/invariant");
545
- var import_keys = require("@dxos/keys");
546
- var import_log = require("@dxos/log");
656
+ var import_keys2 = require("@dxos/keys");
657
+ var import_log2 = require("@dxos/log");
547
658
  var import_protocols = require("@dxos/protocols");
548
659
  function _ts_decorate(decorators, target, key, desc) {
549
660
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -555,7 +666,7 @@ function _ts_decorate(decorators, target, key, desc) {
555
666
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
556
667
  return c > 3 && r && Object.defineProperty(target, key, r), r;
557
668
  }
558
- var __dxlog_file = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection.ts";
669
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
559
670
  var STARTING_SIGNALLING_DELAY = 10;
560
671
  var MAX_SIGNALLING_DELAY = 300;
561
672
  var ConnectionState;
@@ -597,6 +708,8 @@ var ConnectionState;
597
708
  */
598
709
  "CLOSED"
599
710
  ] = "CLOSED";
711
+ ConnectionState6["ABORTING"] = "ABORTING";
712
+ ConnectionState6["ABORTED"] = "ABORTED";
600
713
  })(ConnectionState || (ConnectionState = {}));
601
714
  var Connection = class {
602
715
  constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
@@ -613,22 +726,22 @@ var Connection = class {
613
726
  this._state = ConnectionState.CREATED;
614
727
  this._incomingSignalBuffer = [];
615
728
  this._outgoingSignalBuffer = [];
616
- this.stateChanged = new import_async.Event();
729
+ this.stateChanged = new import_async2.Event();
617
730
  this.errors = new import_debug.ErrorStream();
618
- this._instanceId = import_keys.PublicKey.random().toHex();
619
- this._signalSendTask = new import_async.DeferredTask(this._ctx, async () => {
731
+ this._instanceId = import_keys2.PublicKey.random().toHex();
732
+ this._signalSendTask = new import_async2.DeferredTask(this._ctx, async () => {
620
733
  await this._flushSignalBuffer();
621
734
  });
622
735
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
623
- import_log.log.trace("dxos.mesh.connection.construct", {
736
+ import_log2.log.trace("dxos.mesh.connection.construct", {
624
737
  sessionId: this.sessionId,
625
738
  topic: this.topic,
626
739
  localPeerId: this.ownId,
627
740
  remotePeerId: this.remoteId,
628
741
  initiator: this.initiator
629
742
  }, {
630
- F: __dxlog_file,
631
- L: 114,
743
+ F: __dxlog_file2,
744
+ L: 123,
632
745
  S: this,
633
746
  C: (f, a) => f(...a)
634
747
  });
@@ -647,31 +760,31 @@ var Connection = class {
647
760
  */
648
761
  async openConnection() {
649
762
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
650
- F: __dxlog_file,
651
- L: 139,
763
+ F: __dxlog_file2,
764
+ L: 148,
652
765
  S: this,
653
766
  A: [
654
767
  "this._state === ConnectionState.INITIAL",
655
768
  "'Invalid state.'"
656
769
  ]
657
770
  });
658
- import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
771
+ import_log2.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
659
772
  id: this._instanceId
660
773
  }), {
661
- F: __dxlog_file,
662
- L: 140,
774
+ F: __dxlog_file2,
775
+ L: 149,
663
776
  S: this,
664
777
  C: (f, a) => f(...a)
665
778
  });
666
- import_log.log.trace("dxos.mesh.connection.open", {
779
+ import_log2.log.trace("dxos.mesh.connection.open", {
667
780
  sessionId: this.sessionId,
668
781
  topic: this.topic,
669
782
  localPeerId: this.ownId,
670
783
  remotePeerId: this.remoteId,
671
784
  initiator: this.initiator
672
785
  }, {
673
- F: __dxlog_file,
674
- L: 141,
786
+ F: __dxlog_file2,
787
+ L: 150,
675
788
  S: this,
676
789
  C: (f, a) => f(...a)
677
790
  });
@@ -680,17 +793,17 @@ var Connection = class {
680
793
  this.errors.raise(err);
681
794
  });
682
795
  this._protocol.stream.on("close", () => {
683
- (0, import_log.log)("protocol stream closed", void 0, {
684
- F: __dxlog_file,
685
- L: 158,
796
+ (0, import_log2.log)("protocol stream closed", void 0, {
797
+ F: __dxlog_file2,
798
+ L: 167,
686
799
  S: this,
687
800
  C: (f, a) => f(...a)
688
801
  });
689
- this.close().catch((err) => this.errors.raise(err));
802
+ this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
690
803
  });
691
804
  (0, import_invariant.invariant)(!this._transport, void 0, {
692
- F: __dxlog_file,
693
- L: 162,
805
+ F: __dxlog_file2,
806
+ L: 171,
694
807
  S: this,
695
808
  A: [
696
809
  "!this._transport",
@@ -708,34 +821,51 @@ var Connection = class {
708
821
  });
709
822
  this._transport.closed.once(() => {
710
823
  this._transport = void 0;
711
- (0, import_log.log)("abort triggered by transport close", void 0, {
712
- F: __dxlog_file,
713
- L: 176,
824
+ (0, import_log2.log)("abort triggered by transport close", void 0, {
825
+ F: __dxlog_file2,
826
+ L: 185,
714
827
  S: this,
715
828
  C: (f, a) => f(...a)
716
829
  });
717
830
  this.abort().catch((err) => this.errors.raise(err));
718
831
  });
719
832
  this._transport.errors.handle((err) => {
720
- (0, import_log.log)("transport error:", {
833
+ (0, import_log2.log)("transport error:", {
721
834
  err
722
835
  }, {
723
- F: __dxlog_file,
724
- L: 181,
836
+ F: __dxlog_file2,
837
+ L: 190,
725
838
  S: this,
726
839
  C: (f, a) => f(...a)
727
840
  });
728
841
  if (!this.closeReason) {
729
842
  this.closeReason = err?.message;
730
843
  }
731
- if (err?.message.includes("OperationError")) {
732
- (0, import_log.log)("aborting due to transport OperationError", void 0, {
733
- F: __dxlog_file,
734
- L: 187,
844
+ if (err instanceof import_protocols.ConnectionResetError) {
845
+ import_log2.log.warn("aborting due to transport ConnectionResetError", void 0, {
846
+ F: __dxlog_file2,
847
+ L: 197,
848
+ S: this,
849
+ C: (f, a) => f(...a)
850
+ });
851
+ this.abort().catch((err2) => this.errors.raise(err2));
852
+ } else if (err instanceof import_protocols.ConnectivityError) {
853
+ import_log2.log.warn("aborting due to transport ConnectivityError", void 0, {
854
+ F: __dxlog_file2,
855
+ L: 200,
735
856
  S: this,
736
857
  C: (f, a) => f(...a)
737
858
  });
738
859
  this.abort().catch((err2) => this.errors.raise(err2));
860
+ } else if (err instanceof import_protocols.UnknownProtocolError) {
861
+ import_log2.log.warn("unsure what to do with UnknownProtocolError, will keep on truckin", {
862
+ err
863
+ }, {
864
+ F: __dxlog_file2,
865
+ L: 203,
866
+ S: this,
867
+ C: (f, a) => f(...a)
868
+ });
739
869
  }
740
870
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
741
871
  this.errors.raise(err);
@@ -745,52 +875,52 @@ var Connection = class {
745
875
  void this._transport.signal(signal);
746
876
  }
747
877
  this._incomingSignalBuffer = [];
748
- import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
878
+ import_log2.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
749
879
  id: this._instanceId
750
880
  }), {
751
- F: __dxlog_file,
752
- L: 203,
881
+ F: __dxlog_file2,
882
+ L: 218,
753
883
  S: this,
754
884
  C: (f, a) => f(...a)
755
885
  });
756
886
  }
757
887
  async abort(err) {
758
- (0, import_log.log)("aborting...", {
888
+ (0, import_log2.log)("aborting...", {
759
889
  err
760
890
  }, {
761
- F: __dxlog_file,
762
- L: 209,
891
+ F: __dxlog_file2,
892
+ L: 225,
763
893
  S: this,
764
894
  C: (f, a) => f(...a)
765
895
  });
766
- if (!this.closeReason) {
767
- this.closeReason = err?.message;
768
- }
769
- if (this._state === ConnectionState.CLOSED) {
770
- (0, import_log.log)("abort ignored: already closed", this.closeReason, {
771
- F: __dxlog_file,
772
- L: 214,
896
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
897
+ (0, import_log2.log)(`abort ignored: already ${this._state}`, this.closeReason, {
898
+ F: __dxlog_file2,
899
+ L: 227,
773
900
  S: this,
774
901
  C: (f, a) => f(...a)
775
902
  });
776
903
  return;
777
904
  }
778
- this._changeState(ConnectionState.CLOSING);
905
+ this._changeState(ConnectionState.ABORTING);
906
+ if (!this.closeReason) {
907
+ this.closeReason = err?.message;
908
+ }
779
909
  await this._ctx.dispose();
780
910
  try {
781
- (0, import_log.log)("aborting protocol... ", {
911
+ (0, import_log2.log)("aborting protocol... ", {
782
912
  proto: this._protocol
783
913
  }, {
784
- F: __dxlog_file,
785
- L: 224,
914
+ F: __dxlog_file2,
915
+ L: 240,
786
916
  S: this,
787
917
  C: (f, a) => f(...a)
788
918
  });
789
919
  await this._protocol.abort();
790
920
  } catch (err2) {
791
- import_log.log.catch(err2, void 0, {
792
- F: __dxlog_file,
793
- L: 227,
921
+ import_log2.log.catch(err2, void 0, {
922
+ F: __dxlog_file2,
923
+ L: 243,
794
924
  S: this,
795
925
  C: (f, a) => f(...a)
796
926
  });
@@ -798,58 +928,70 @@ var Connection = class {
798
928
  try {
799
929
  await this._transport?.destroy();
800
930
  } catch (err2) {
801
- import_log.log.catch(err2, void 0, {
802
- F: __dxlog_file,
803
- L: 234,
931
+ import_log2.log.catch(err2, void 0, {
932
+ F: __dxlog_file2,
933
+ L: 250,
804
934
  S: this,
805
935
  C: (f, a) => f(...a)
806
936
  });
807
937
  }
808
- this._changeState(ConnectionState.CLOSED);
809
- this._callbacks?.onClosed?.(err);
938
+ try {
939
+ this._callbacks?.onClosed?.(err);
940
+ } catch (err2) {
941
+ import_log2.log.catch(err2, void 0, {
942
+ F: __dxlog_file2,
943
+ L: 256,
944
+ S: this,
945
+ C: (f, a) => f(...a)
946
+ });
947
+ }
948
+ this._changeState(ConnectionState.ABORTED);
810
949
  }
811
950
  async close(err) {
812
951
  if (!this.closeReason) {
813
952
  this.closeReason = err?.message;
814
953
  }
815
- if (this._state === ConnectionState.CLOSED) {
954
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
816
955
  return;
817
956
  }
957
+ const lastState = this._state;
818
958
  this._changeState(ConnectionState.CLOSING);
819
959
  await this._ctx.dispose();
820
- (0, import_log.log)("closing...", {
960
+ (0, import_log2.log)("closing...", {
821
961
  peerId: this.ownId
822
962
  }, {
823
- F: __dxlog_file,
824
- L: 253,
963
+ F: __dxlog_file2,
964
+ L: 278,
825
965
  S: this,
826
966
  C: (f, a) => f(...a)
827
967
  });
828
- try {
829
- await this._protocol.close();
830
- } catch (err2) {
831
- import_log.log.catch(err2, void 0, {
832
- F: __dxlog_file,
833
- L: 259,
834
- S: this,
835
- C: (f, a) => f(...a)
836
- });
837
- }
838
- try {
839
- await this._transport?.destroy();
840
- } catch (err2) {
841
- import_log.log.catch(err2, void 0, {
842
- F: __dxlog_file,
843
- L: 266,
844
- S: this,
845
- C: (f, a) => f(...a)
846
- });
968
+ if (lastState === ConnectionState.CONNECTED) {
969
+ try {
970
+ await this._protocol.close();
971
+ } catch (err2) {
972
+ import_log2.log.catch(err2, void 0, {
973
+ F: __dxlog_file2,
974
+ L: 285,
975
+ S: this,
976
+ C: (f, a) => f(...a)
977
+ });
978
+ }
979
+ try {
980
+ await this._transport?.destroy();
981
+ } catch (err2) {
982
+ import_log2.log.catch(err2, void 0, {
983
+ F: __dxlog_file2,
984
+ L: 292,
985
+ S: this,
986
+ C: (f, a) => f(...a)
987
+ });
988
+ }
847
989
  }
848
- (0, import_log.log)("closed", {
990
+ (0, import_log2.log)("closed", {
849
991
  peerId: this.ownId
850
992
  }, {
851
- F: __dxlog_file,
852
- L: 269,
993
+ F: __dxlog_file2,
994
+ L: 296,
853
995
  S: this,
854
996
  C: (f, a) => f(...a)
855
997
  });
@@ -866,7 +1008,7 @@ var Connection = class {
866
1008
  }
867
1009
  try {
868
1010
  if (process.env.NODE_ENV !== "test") {
869
- await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
1011
+ await (0, import_context.cancelWithContext)(this._ctx, (0, import_async2.sleep)(this._signallingDelay));
870
1012
  this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
871
1013
  }
872
1014
  const signals = [
@@ -885,14 +1027,14 @@ var Connection = class {
885
1027
  }
886
1028
  });
887
1029
  } catch (err) {
888
- if (err instanceof import_errors.CancelledError) {
1030
+ if (err instanceof import_protocols.CancelledError) {
889
1031
  return;
890
1032
  }
891
- import_log.log.warn("signal failed", {
1033
+ import_log2.log.warn("signal failed", {
892
1034
  err
893
1035
  }, {
894
- F: __dxlog_file,
895
- L: 306,
1036
+ F: __dxlog_file2,
1037
+ L: 333,
896
1038
  S: this,
897
1039
  C: (f, a) => f(...a)
898
1040
  });
@@ -904,8 +1046,8 @@ var Connection = class {
904
1046
  */
905
1047
  async signal(msg) {
906
1048
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
907
- F: __dxlog_file,
908
- L: 315,
1049
+ F: __dxlog_file2,
1050
+ L: 342,
909
1051
  S: this,
910
1052
  A: [
911
1053
  "msg.sessionId",
@@ -913,17 +1055,17 @@ var Connection = class {
913
1055
  ]
914
1056
  });
915
1057
  if (!msg.sessionId.equals(this.sessionId)) {
916
- (0, import_log.log)("dropping signal for incorrect session id", void 0, {
917
- F: __dxlog_file,
918
- L: 317,
1058
+ (0, import_log2.log)("dropping signal for incorrect session id", void 0, {
1059
+ F: __dxlog_file2,
1060
+ L: 344,
919
1061
  S: this,
920
1062
  C: (f, a) => f(...a)
921
1063
  });
922
1064
  return;
923
1065
  }
924
1066
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
925
- F: __dxlog_file,
926
- L: 320,
1067
+ F: __dxlog_file2,
1068
+ L: 347,
927
1069
  S: this,
928
1070
  A: [
929
1071
  "msg.data.signal || msg.data.signalBatch",
@@ -931,8 +1073,8 @@ var Connection = class {
931
1073
  ]
932
1074
  });
933
1075
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
934
- F: __dxlog_file,
935
- L: 321,
1076
+ F: __dxlog_file2,
1077
+ L: 348,
936
1078
  S: this,
937
1079
  A: [
938
1080
  "msg.author?.equals(this.remoteId)",
@@ -940,8 +1082,8 @@ var Connection = class {
940
1082
  ]
941
1083
  });
942
1084
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
943
- F: __dxlog_file,
944
- L: 322,
1085
+ F: __dxlog_file2,
1086
+ L: 349,
945
1087
  S: this,
946
1088
  A: [
947
1089
  "msg.recipient?.equals(this.ownId)",
@@ -959,34 +1101,34 @@ var Connection = class {
959
1101
  ConnectionState.CREATED,
960
1102
  ConnectionState.INITIAL
961
1103
  ].includes(this.state)) {
962
- (0, import_log.log)("buffered signal", {
1104
+ (0, import_log2.log)("buffered signal", {
963
1105
  peerId: this.ownId,
964
1106
  remoteId: this.remoteId,
965
1107
  msg: msg.data
966
1108
  }, {
967
- F: __dxlog_file,
968
- L: 331,
1109
+ F: __dxlog_file2,
1110
+ L: 358,
969
1111
  S: this,
970
1112
  C: (f, a) => f(...a)
971
1113
  });
972
1114
  this._incomingSignalBuffer.push(signal);
973
1115
  } else {
974
1116
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
975
- F: __dxlog_file,
976
- L: 334,
1117
+ F: __dxlog_file2,
1118
+ L: 361,
977
1119
  S: this,
978
1120
  A: [
979
1121
  "this._transport",
980
1122
  "'Connection not ready to accept signals.'"
981
1123
  ]
982
1124
  });
983
- (0, import_log.log)("received signal", {
1125
+ (0, import_log2.log)("received signal", {
984
1126
  peerId: this.ownId,
985
1127
  remoteId: this.remoteId,
986
1128
  msg: msg.data
987
1129
  }, {
988
- F: __dxlog_file,
989
- L: 335,
1130
+ F: __dxlog_file2,
1131
+ L: 362,
990
1132
  S: this,
991
1133
  C: (f, a) => f(...a)
992
1134
  });
@@ -998,19 +1140,19 @@ var Connection = class {
998
1140
  this._changeState(ConnectionState.INITIAL);
999
1141
  }
1000
1142
  _changeState(state) {
1001
- (0, import_log.log)("stateChanged", {
1143
+ (0, import_log2.log)("stateChanged", {
1002
1144
  from: this._state,
1003
- too: state,
1145
+ to: state,
1004
1146
  peerId: this.ownId
1005
1147
  }, {
1006
- F: __dxlog_file,
1007
- L: 346,
1148
+ F: __dxlog_file2,
1149
+ L: 373,
1008
1150
  S: this,
1009
1151
  C: (f, a) => f(...a)
1010
1152
  });
1011
1153
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1012
- F: __dxlog_file,
1013
- L: 347,
1154
+ F: __dxlog_file2,
1155
+ L: 374,
1014
1156
  S: this,
1015
1157
  A: [
1016
1158
  "state !== this._state",
@@ -1022,261 +1164,29 @@ var Connection = class {
1022
1164
  }
1023
1165
  };
1024
1166
  _ts_decorate([
1025
- import_async.synchronized
1167
+ import_async2.synchronized
1026
1168
  ], Connection.prototype, "abort", null);
1027
1169
  _ts_decorate([
1028
- import_async.synchronized
1170
+ import_async2.synchronized
1029
1171
  ], Connection.prototype, "close", null);
1030
1172
 
1031
1173
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
1032
- var import_async3 = require("@dxos/async");
1174
+ var import_async4 = require("@dxos/async");
1033
1175
  var import_context4 = require("@dxos/context");
1034
1176
  var import_debug2 = require("@dxos/debug");
1035
1177
  var import_invariant4 = require("@dxos/invariant");
1036
- var import_keys4 = require("@dxos/keys");
1037
- var import_log4 = require("@dxos/log");
1038
- var import_protocols3 = require("@dxos/protocols");
1039
- var import_util2 = require("@dxos/util");
1178
+ var import_keys5 = require("@dxos/keys");
1179
+ var import_log5 = require("@dxos/log");
1180
+ var import_protocols4 = require("@dxos/protocols");
1181
+ var import_util3 = require("@dxos/util");
1040
1182
 
1041
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1183
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1184
+ var import_async3 = require("@dxos/async");
1042
1185
  var import_context2 = require("@dxos/context");
1043
1186
  var import_invariant2 = require("@dxos/invariant");
1044
- var import_keys2 = require("@dxos/keys");
1045
- var import_log2 = require("@dxos/log");
1187
+ var import_keys3 = require("@dxos/keys");
1188
+ var import_log3 = require("@dxos/log");
1046
1189
  var import_protocols2 = require("@dxos/protocols");
1047
- var import_util = require("@dxos/util");
1048
- var __dxlog_file2 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1049
- var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1050
- var SwarmMessenger = class {
1051
- constructor({ sendMessage, onSignal, onOffer, topic }) {
1052
- this._ctx = new import_context2.Context();
1053
- this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1054
- this._sendMessage = sendMessage;
1055
- this._onSignal = onSignal;
1056
- this._onOffer = onOffer;
1057
- this._topic = topic;
1058
- }
1059
- async receiveMessage({ author, recipient, payload }) {
1060
- if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1061
- return;
1062
- }
1063
- const message = SwarmMessage.decode(payload.value);
1064
- if (!this._topic.equals(message.topic)) {
1065
- return;
1066
- }
1067
- (0, import_log2.log)("received", {
1068
- from: author,
1069
- to: recipient,
1070
- msg: message
1071
- }, {
1072
- F: __dxlog_file2,
1073
- L: 69,
1074
- S: this,
1075
- C: (f, a) => f(...a)
1076
- });
1077
- if (message.data?.offer) {
1078
- await this._handleOffer({
1079
- author,
1080
- recipient,
1081
- message
1082
- });
1083
- } else if (message.data?.answer) {
1084
- await this._resolveAnswers(message);
1085
- } else if (message.data?.signal) {
1086
- await this._handleSignal({
1087
- author,
1088
- recipient,
1089
- message
1090
- });
1091
- } else if (message.data?.signalBatch) {
1092
- await this._handleSignal({
1093
- author,
1094
- recipient,
1095
- message
1096
- });
1097
- } else {
1098
- import_log2.log.warn("unknown message", {
1099
- message
1100
- }, {
1101
- F: __dxlog_file2,
1102
- L: 80,
1103
- S: this,
1104
- C: (f, a) => f(...a)
1105
- });
1106
- }
1107
- }
1108
- async signal(message) {
1109
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1110
- F: __dxlog_file2,
1111
- L: 85,
1112
- S: this,
1113
- A: [
1114
- "message.data?.signal || message.data?.signalBatch",
1115
- "'Invalid message'"
1116
- ]
1117
- });
1118
- await this._sendReliableMessage({
1119
- author: message.author,
1120
- recipient: message.recipient,
1121
- message
1122
- });
1123
- }
1124
- async offer(message) {
1125
- const networkMessage = {
1126
- ...message,
1127
- messageId: import_keys2.PublicKey.random()
1128
- };
1129
- return new Promise((resolve, reject) => {
1130
- this._offerRecords.set(networkMessage.messageId, {
1131
- resolve
1132
- });
1133
- this._sendReliableMessage({
1134
- author: message.author,
1135
- recipient: message.recipient,
1136
- message: networkMessage
1137
- }).catch((err) => reject(err));
1138
- });
1139
- }
1140
- async _sendReliableMessage({ author, recipient, message }) {
1141
- const networkMessage = {
1142
- ...message,
1143
- // Setting unique message_id if it not specified yet.
1144
- messageId: message.messageId ?? import_keys2.PublicKey.random()
1145
- };
1146
- (0, import_log2.log)("sending", {
1147
- from: author,
1148
- to: recipient,
1149
- msg: networkMessage
1150
- }, {
1151
- F: __dxlog_file2,
1152
- L: 123,
1153
- S: this,
1154
- C: (f, a) => f(...a)
1155
- });
1156
- await this._sendMessage({
1157
- author,
1158
- recipient,
1159
- payload: {
1160
- type_url: "dxos.mesh.swarm.SwarmMessage",
1161
- value: SwarmMessage.encode(networkMessage)
1162
- }
1163
- });
1164
- }
1165
- async _resolveAnswers(message) {
1166
- (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1167
- F: __dxlog_file2,
1168
- L: 135,
1169
- S: this,
1170
- A: [
1171
- "message.data?.answer?.offerMessageId",
1172
- "'No offerMessageId'"
1173
- ]
1174
- });
1175
- const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1176
- if (offerRecord) {
1177
- this._offerRecords.delete(message.data.answer.offerMessageId);
1178
- (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
1179
- F: __dxlog_file2,
1180
- L: 139,
1181
- S: this,
1182
- A: [
1183
- "message.data?.answer",
1184
- "'No answer'"
1185
- ]
1186
- });
1187
- (0, import_log2.log)("resolving", {
1188
- answer: message.data.answer
1189
- }, {
1190
- F: __dxlog_file2,
1191
- L: 140,
1192
- S: this,
1193
- C: (f, a) => f(...a)
1194
- });
1195
- offerRecord.resolve(message.data.answer);
1196
- }
1197
- }
1198
- async _handleOffer({ author, recipient, message }) {
1199
- (0, import_invariant2.invariant)(message.data.offer, "No offer", {
1200
- F: __dxlog_file2,
1201
- L: 154,
1202
- S: this,
1203
- A: [
1204
- "message.data.offer",
1205
- "'No offer'"
1206
- ]
1207
- });
1208
- const offerMessage = {
1209
- author,
1210
- recipient,
1211
- ...message,
1212
- data: {
1213
- offer: message.data.offer
1214
- }
1215
- };
1216
- const answer = await this._onOffer(offerMessage);
1217
- answer.offerMessageId = message.messageId;
1218
- try {
1219
- await this._sendReliableMessage({
1220
- author: recipient,
1221
- recipient: author,
1222
- message: {
1223
- topic: message.topic,
1224
- sessionId: message.sessionId,
1225
- data: {
1226
- answer
1227
- }
1228
- }
1229
- });
1230
- } catch (err) {
1231
- import_log2.log.warn("error sending answer", {
1232
- err
1233
- }, {
1234
- F: __dxlog_file2,
1235
- L: 174,
1236
- S: this,
1237
- C: (f, a) => f(...a)
1238
- });
1239
- }
1240
- }
1241
- async _handleSignal({ author, recipient, message }) {
1242
- (0, import_invariant2.invariant)(message.messageId, void 0, {
1243
- F: __dxlog_file2,
1244
- L: 187,
1245
- S: this,
1246
- A: [
1247
- "message.messageId",
1248
- ""
1249
- ]
1250
- });
1251
- (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1252
- F: __dxlog_file2,
1253
- L: 188,
1254
- S: this,
1255
- A: [
1256
- "message.data.signal || message.data.signalBatch",
1257
- "'Invalid message'"
1258
- ]
1259
- });
1260
- const signalMessage = {
1261
- author,
1262
- recipient,
1263
- ...message,
1264
- data: {
1265
- signal: message.data.signal,
1266
- signalBatch: message.data.signalBatch
1267
- }
1268
- };
1269
- await this._onSignal(signalMessage);
1270
- }
1271
- };
1272
-
1273
- // packages/core/mesh/network-manager/src/swarm/peer.ts
1274
- var import_async2 = require("@dxos/async");
1275
- var import_context3 = require("@dxos/context");
1276
- var import_errors2 = require("@dxos/errors");
1277
- var import_invariant3 = require("@dxos/invariant");
1278
- var import_keys3 = require("@dxos/keys");
1279
- var import_log3 = require("@dxos/log");
1280
1190
  function _ts_decorate2(decorators, target, key, desc) {
1281
1191
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1282
1192
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1287,8 +1197,8 @@ function _ts_decorate2(decorators, target, key, desc) {
1287
1197
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1288
1198
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1289
1199
  }
1290
- var __dxlog_file3 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/peer.ts";
1291
- var ConnectionDisplacedError = class extends import_errors2.SystemError {
1200
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1201
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1292
1202
  constructor() {
1293
1203
  super("Connection displaced by remote initiator.");
1294
1204
  }
@@ -1307,16 +1217,30 @@ var Peer = class {
1307
1217
  this._callbacks = _callbacks;
1308
1218
  this._availableAfter = 0;
1309
1219
  this.availableToConnect = true;
1310
- this._ctx = new import_context3.Context();
1220
+ this._ctx = new import_context2.Context();
1311
1221
  this.advertizing = false;
1312
1222
  this.initiating = false;
1313
- this.connectionDisplaced = new import_async2.Event();
1223
+ this.connectionDisplaced = new import_async3.Event();
1314
1224
  }
1315
1225
  /**
1316
1226
  * Respond to remote offer.
1317
1227
  */
1318
1228
  async onOffer(message) {
1319
1229
  const remoteId = message.author;
1230
+ if (this.connection && ![
1231
+ ConnectionState.CREATED,
1232
+ ConnectionState.INITIAL
1233
+ ].includes(this.connection.state)) {
1234
+ import_log3.log.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
1235
+ F: __dxlog_file3,
1236
+ L: 112,
1237
+ S: this,
1238
+ C: (f, a) => f(...a)
1239
+ });
1240
+ return {
1241
+ accept: false
1242
+ };
1243
+ }
1320
1244
  if (this.connection || this.initiating) {
1321
1245
  if (remoteId.toHex() < this.localPeerId.toHex()) {
1322
1246
  (0, import_log3.log)("close local connection", {
@@ -1326,7 +1250,7 @@ var Peer = class {
1326
1250
  sessionId: this.connection?.sessionId
1327
1251
  }, {
1328
1252
  F: __dxlog_file3,
1329
- L: 117,
1253
+ L: 121,
1330
1254
  S: this,
1331
1255
  C: (f, a) => f(...a)
1332
1256
  });
@@ -1341,9 +1265,9 @@ var Peer = class {
1341
1265
  }
1342
1266
  if (await this._callbacks.onOffer(remoteId)) {
1343
1267
  if (!this.connection) {
1344
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1268
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1345
1269
  F: __dxlog_file3,
1346
- L: 137,
1270
+ L: 141,
1347
1271
  S: this,
1348
1272
  A: [
1349
1273
  "message.sessionId",
@@ -1356,7 +1280,7 @@ var Peer = class {
1356
1280
  connection.initiate();
1357
1281
  await connection.openConnection();
1358
1282
  } catch (err) {
1359
- if (!(err instanceof import_errors2.CancelledError)) {
1283
+ if (!(err instanceof import_protocols2.CancelledError)) {
1360
1284
  import_log3.log.warn("connection error", {
1361
1285
  topic: this.topic,
1362
1286
  peerId: this.localPeerId,
@@ -1364,7 +1288,7 @@ var Peer = class {
1364
1288
  err
1365
1289
  }, {
1366
1290
  F: __dxlog_file3,
1367
- L: 147,
1291
+ L: 151,
1368
1292
  S: this,
1369
1293
  C: (f, a) => f(...a)
1370
1294
  });
@@ -1384,18 +1308,18 @@ var Peer = class {
1384
1308
  * Initiate a connection to the remote peer.
1385
1309
  */
1386
1310
  async initiateConnection() {
1387
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1311
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1388
1312
  F: __dxlog_file3,
1389
- L: 164,
1313
+ L: 168,
1390
1314
  S: this,
1391
1315
  A: [
1392
1316
  "!this.initiating",
1393
1317
  "'Initiation in progress.'"
1394
1318
  ]
1395
1319
  });
1396
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1320
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1397
1321
  F: __dxlog_file3,
1398
- L: 165,
1322
+ L: 169,
1399
1323
  S: this,
1400
1324
  A: [
1401
1325
  "!this.connection",
@@ -1410,7 +1334,7 @@ var Peer = class {
1410
1334
  sessionId
1411
1335
  }, {
1412
1336
  F: __dxlog_file3,
1413
- L: 167,
1337
+ L: 171,
1414
1338
  S: this,
1415
1339
  C: (f, a) => f(...a)
1416
1340
  });
@@ -1435,14 +1359,14 @@ var Peer = class {
1435
1359
  remoteId: this.id
1436
1360
  }, {
1437
1361
  F: __dxlog_file3,
1438
- L: 183,
1362
+ L: 187,
1439
1363
  S: this,
1440
1364
  C: (f, a) => f(...a)
1441
1365
  });
1442
1366
  if (connection.state !== ConnectionState.INITIAL) {
1443
1367
  (0, import_log3.log)("ignoring response", void 0, {
1444
1368
  F: __dxlog_file3,
1445
- L: 185,
1369
+ L: 189,
1446
1370
  S: this,
1447
1371
  C: (f, a) => f(...a)
1448
1372
  });
@@ -1462,10 +1386,19 @@ var Peer = class {
1462
1386
  remoteId: this.id
1463
1387
  }, {
1464
1388
  F: __dxlog_file3,
1465
- L: 196,
1389
+ L: 200,
1466
1390
  S: this,
1467
1391
  C: (f, a) => f(...a)
1468
1392
  });
1393
+ if (err instanceof import_protocols2.TimeoutError) {
1394
+ import_log3.log.warn("aborting connection due to signalling failure", void 0, {
1395
+ F: __dxlog_file3,
1396
+ L: 202,
1397
+ S: this,
1398
+ C: (f, a) => f(...a)
1399
+ });
1400
+ await connection.abort(err);
1401
+ }
1469
1402
  await this.closeConnection(err);
1470
1403
  throw err;
1471
1404
  } finally {
@@ -1485,13 +1418,13 @@ var Peer = class {
1485
1418
  sessionId
1486
1419
  }, {
1487
1420
  F: __dxlog_file3,
1488
- L: 210,
1421
+ L: 218,
1489
1422
  S: this,
1490
1423
  C: (f, a) => f(...a)
1491
1424
  });
1492
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1425
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1493
1426
  F: __dxlog_file3,
1494
- L: 217,
1427
+ L: 225,
1495
1428
  S: this,
1496
1429
  A: [
1497
1430
  "!this.connection",
@@ -1527,7 +1460,7 @@ var Peer = class {
1527
1460
  initiator
1528
1461
  }, {
1529
1462
  F: __dxlog_file3,
1530
- L: 236,
1463
+ L: 244,
1531
1464
  S: this,
1532
1465
  C: (f, a) => f(...a)
1533
1466
  });
@@ -1540,14 +1473,14 @@ var Peer = class {
1540
1473
  initiator
1541
1474
  }, {
1542
1475
  F: __dxlog_file3,
1543
- L: 245,
1476
+ L: 253,
1544
1477
  S: this,
1545
1478
  C: (f, a) => f(...a)
1546
1479
  });
1547
1480
  this._connectionLimiter.doneConnecting(sessionId);
1548
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1481
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1549
1482
  F: __dxlog_file3,
1550
- L: 250,
1483
+ L: 258,
1551
1484
  S: this,
1552
1485
  A: [
1553
1486
  "this.connection === connection",
@@ -1562,7 +1495,7 @@ var Peer = class {
1562
1495
  initiator
1563
1496
  }, {
1564
1497
  F: __dxlog_file3,
1565
- L: 252,
1498
+ L: 260,
1566
1499
  S: this,
1567
1500
  C: (f, a) => f(...a)
1568
1501
  });
@@ -1576,7 +1509,7 @@ var Peer = class {
1576
1509
  this._availableAfter = increaseInterval(this._availableAfter);
1577
1510
  }
1578
1511
  this._callbacks.onDisconnected();
1579
- (0, import_async2.scheduleTask)(this._connectionCtx, () => {
1512
+ (0, import_async3.scheduleTask)(this._connectionCtx, () => {
1580
1513
  this.availableToConnect = true;
1581
1514
  this._callbacks.onPeerAvailable();
1582
1515
  }, this._availableAfter);
@@ -1597,7 +1530,7 @@ var Peer = class {
1597
1530
  err
1598
1531
  }, {
1599
1532
  F: __dxlog_file3,
1600
- L: 292,
1533
+ L: 300,
1601
1534
  S: this,
1602
1535
  C: (f, a) => f(...a)
1603
1536
  });
@@ -1610,82 +1543,314 @@ var Peer = class {
1610
1543
  err
1611
1544
  }, {
1612
1545
  F: __dxlog_file3,
1613
- L: 293,
1546
+ L: 301,
1547
+ S: this,
1548
+ C: (f, a) => f(...a)
1549
+ });
1550
+ void this.closeConnection(err);
1551
+ });
1552
+ this.connection = connection;
1553
+ return connection;
1554
+ }
1555
+ async closeConnection(err) {
1556
+ if (!this.connection) {
1557
+ return;
1558
+ }
1559
+ const connection = this.connection;
1560
+ (0, import_log3.log)("closing...", {
1561
+ peerId: this.id,
1562
+ sessionId: connection.sessionId
1563
+ }, {
1564
+ F: __dxlog_file3,
1565
+ L: 326,
1566
+ S: this,
1567
+ C: (f, a) => f(...a)
1568
+ });
1569
+ await connection.close(err);
1570
+ (0, import_log3.log)("closed", {
1571
+ peerId: this.id,
1572
+ sessionId: connection.sessionId
1573
+ }, {
1574
+ F: __dxlog_file3,
1575
+ L: 332,
1576
+ S: this,
1577
+ C: (f, a) => f(...a)
1578
+ });
1579
+ }
1580
+ async onSignal(message) {
1581
+ if (!this.connection) {
1582
+ (0, import_log3.log)("dropping signal message for non-existent connection", {
1583
+ message
1584
+ }, {
1585
+ F: __dxlog_file3,
1586
+ L: 337,
1587
+ S: this,
1588
+ C: (f, a) => f(...a)
1589
+ });
1590
+ return;
1591
+ }
1592
+ await this.connection.signal(message);
1593
+ }
1594
+ async destroy() {
1595
+ await this._ctx.dispose();
1596
+ (0, import_log3.log)("Destroying peer", {
1597
+ peerId: this.id,
1598
+ topic: this.topic
1599
+ }, {
1600
+ F: __dxlog_file3,
1601
+ L: 347,
1602
+ S: this,
1603
+ C: (f, a) => f(...a)
1604
+ });
1605
+ await this?.connection?.close();
1606
+ }
1607
+ };
1608
+ _ts_decorate2([
1609
+ import_async3.synchronized
1610
+ ], Peer.prototype, "destroy", null);
1611
+ var increaseInterval = (interval) => {
1612
+ if (interval === 0) {
1613
+ return 50;
1614
+ } else if (interval < 500) {
1615
+ return 500;
1616
+ } else if (interval < 1e3) {
1617
+ return 1e3;
1618
+ } else if (interval < 5e3) {
1619
+ return 5e3;
1620
+ }
1621
+ return 1e4;
1622
+ };
1623
+
1624
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1625
+ var import_context3 = require("@dxos/context");
1626
+ var import_invariant3 = require("@dxos/invariant");
1627
+ var import_keys4 = require("@dxos/keys");
1628
+ var import_log4 = require("@dxos/log");
1629
+ var import_protocols3 = require("@dxos/protocols");
1630
+ var import_util2 = require("@dxos/util");
1631
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1632
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1633
+ var SwarmMessenger = class {
1634
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1635
+ this._ctx = new import_context3.Context();
1636
+ this._offerRecords = new import_util2.ComplexMap((key) => key.toHex());
1637
+ this._sendMessage = sendMessage;
1638
+ this._onSignal = onSignal;
1639
+ this._onOffer = onOffer;
1640
+ this._topic = topic;
1641
+ }
1642
+ async receiveMessage({ author, recipient, payload }) {
1643
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1644
+ return;
1645
+ }
1646
+ const message = SwarmMessage.decode(payload.value);
1647
+ if (!this._topic.equals(message.topic)) {
1648
+ return;
1649
+ }
1650
+ (0, import_log4.log)("received", {
1651
+ from: author,
1652
+ to: recipient,
1653
+ msg: message
1654
+ }, {
1655
+ F: __dxlog_file4,
1656
+ L: 69,
1657
+ S: this,
1658
+ C: (f, a) => f(...a)
1659
+ });
1660
+ if (message.data?.offer) {
1661
+ await this._handleOffer({
1662
+ author,
1663
+ recipient,
1664
+ message
1665
+ });
1666
+ } else if (message.data?.answer) {
1667
+ await this._resolveAnswers(message);
1668
+ } else if (message.data?.signal) {
1669
+ await this._handleSignal({
1670
+ author,
1671
+ recipient,
1672
+ message
1673
+ });
1674
+ } else if (message.data?.signalBatch) {
1675
+ await this._handleSignal({
1676
+ author,
1677
+ recipient,
1678
+ message
1679
+ });
1680
+ } else {
1681
+ import_log4.log.warn("unknown message", {
1682
+ message
1683
+ }, {
1684
+ F: __dxlog_file4,
1685
+ L: 80,
1686
+ S: this,
1687
+ C: (f, a) => f(...a)
1688
+ });
1689
+ }
1690
+ }
1691
+ async signal(message) {
1692
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1693
+ F: __dxlog_file4,
1694
+ L: 85,
1695
+ S: this,
1696
+ A: [
1697
+ "message.data?.signal || message.data?.signalBatch",
1698
+ "'Invalid message'"
1699
+ ]
1700
+ });
1701
+ await this._sendReliableMessage({
1702
+ author: message.author,
1703
+ recipient: message.recipient,
1704
+ message
1705
+ });
1706
+ }
1707
+ async offer(message) {
1708
+ const networkMessage = {
1709
+ ...message,
1710
+ messageId: import_keys4.PublicKey.random()
1711
+ };
1712
+ return new Promise((resolve, reject) => {
1713
+ this._offerRecords.set(networkMessage.messageId, {
1714
+ resolve
1715
+ });
1716
+ this._sendReliableMessage({
1717
+ author: message.author,
1718
+ recipient: message.recipient,
1719
+ message: networkMessage
1720
+ }).catch((err) => reject(err));
1721
+ });
1722
+ }
1723
+ async _sendReliableMessage({ author, recipient, message }) {
1724
+ const networkMessage = {
1725
+ ...message,
1726
+ // Setting unique message_id if it not specified yet.
1727
+ messageId: message.messageId ?? import_keys4.PublicKey.random()
1728
+ };
1729
+ (0, import_log4.log)("sending", {
1730
+ from: author,
1731
+ to: recipient,
1732
+ msg: networkMessage
1733
+ }, {
1734
+ F: __dxlog_file4,
1735
+ L: 123,
1736
+ S: this,
1737
+ C: (f, a) => f(...a)
1738
+ });
1739
+ await this._sendMessage({
1740
+ author,
1741
+ recipient,
1742
+ payload: {
1743
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1744
+ value: SwarmMessage.encode(networkMessage)
1745
+ }
1746
+ });
1747
+ }
1748
+ async _resolveAnswers(message) {
1749
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1750
+ F: __dxlog_file4,
1751
+ L: 135,
1752
+ S: this,
1753
+ A: [
1754
+ "message.data?.answer?.offerMessageId",
1755
+ "'No offerMessageId'"
1756
+ ]
1757
+ });
1758
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1759
+ if (offerRecord) {
1760
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1761
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1762
+ F: __dxlog_file4,
1763
+ L: 139,
1764
+ S: this,
1765
+ A: [
1766
+ "message.data?.answer",
1767
+ "'No answer'"
1768
+ ]
1769
+ });
1770
+ (0, import_log4.log)("resolving", {
1771
+ answer: message.data.answer
1772
+ }, {
1773
+ F: __dxlog_file4,
1774
+ L: 140,
1614
1775
  S: this,
1615
1776
  C: (f, a) => f(...a)
1616
1777
  });
1617
- void this.closeConnection(err);
1618
- });
1619
- this.connection = connection;
1620
- return connection;
1621
- }
1622
- async closeConnection(err) {
1623
- if (!this.connection) {
1624
- return;
1778
+ offerRecord.resolve(message.data.answer);
1625
1779
  }
1626
- const connection = this.connection;
1627
- (0, import_log3.log)("closing...", {
1628
- peerId: this.id,
1629
- sessionId: connection.sessionId
1630
- }, {
1631
- F: __dxlog_file3,
1632
- L: 318,
1633
- S: this,
1634
- C: (f, a) => f(...a)
1635
- });
1636
- await connection.close(err);
1637
- (0, import_log3.log)("closed", {
1638
- peerId: this.id,
1639
- sessionId: connection.sessionId
1640
- }, {
1641
- F: __dxlog_file3,
1642
- L: 324,
1780
+ }
1781
+ async _handleOffer({ author, recipient, message }) {
1782
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1783
+ F: __dxlog_file4,
1784
+ L: 154,
1643
1785
  S: this,
1644
- C: (f, a) => f(...a)
1786
+ A: [
1787
+ "message.data.offer",
1788
+ "'No offer'"
1789
+ ]
1645
1790
  });
1646
- }
1647
- async onSignal(message) {
1648
- if (!this.connection) {
1649
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1650
- message
1791
+ const offerMessage = {
1792
+ author,
1793
+ recipient,
1794
+ ...message,
1795
+ data: {
1796
+ offer: message.data.offer
1797
+ }
1798
+ };
1799
+ const answer = await this._onOffer(offerMessage);
1800
+ answer.offerMessageId = message.messageId;
1801
+ try {
1802
+ await this._sendReliableMessage({
1803
+ author: recipient,
1804
+ recipient: author,
1805
+ message: {
1806
+ topic: message.topic,
1807
+ sessionId: message.sessionId,
1808
+ data: {
1809
+ answer
1810
+ }
1811
+ }
1812
+ });
1813
+ } catch (err) {
1814
+ import_log4.log.warn("error sending answer", {
1815
+ err
1651
1816
  }, {
1652
- F: __dxlog_file3,
1653
- L: 329,
1817
+ F: __dxlog_file4,
1818
+ L: 174,
1654
1819
  S: this,
1655
1820
  C: (f, a) => f(...a)
1656
1821
  });
1657
- return;
1658
1822
  }
1659
- await this.connection.signal(message);
1660
1823
  }
1661
- async destroy() {
1662
- await this._ctx.dispose();
1663
- (0, import_log3.log)("Destroying peer", {
1664
- peerId: this.id,
1665
- topic: this.topic
1666
- }, {
1667
- F: __dxlog_file3,
1668
- L: 339,
1824
+ async _handleSignal({ author, recipient, message }) {
1825
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1826
+ F: __dxlog_file4,
1827
+ L: 187,
1669
1828
  S: this,
1670
- C: (f, a) => f(...a)
1829
+ A: [
1830
+ "message.messageId",
1831
+ ""
1832
+ ]
1671
1833
  });
1672
- await this?.connection?.close();
1673
- }
1674
- };
1675
- _ts_decorate2([
1676
- import_async2.synchronized
1677
- ], Peer.prototype, "destroy", null);
1678
- var increaseInterval = (interval) => {
1679
- if (interval === 0) {
1680
- return 50;
1681
- } else if (interval < 500) {
1682
- return 500;
1683
- } else if (interval < 1e3) {
1684
- return 1e3;
1685
- } else if (interval < 5e3) {
1686
- return 5e3;
1834
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1835
+ F: __dxlog_file4,
1836
+ L: 188,
1837
+ S: this,
1838
+ A: [
1839
+ "message.data.signal || message.data.signalBatch",
1840
+ "'Invalid message'"
1841
+ ]
1842
+ });
1843
+ const signalMessage = {
1844
+ author,
1845
+ recipient,
1846
+ ...message,
1847
+ data: {
1848
+ signal: message.data.signal,
1849
+ signalBatch: message.data.signalBatch
1850
+ }
1851
+ };
1852
+ await this._onSignal(signalMessage);
1687
1853
  }
1688
- return 1e4;
1689
1854
  };
1690
1855
 
1691
1856
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
@@ -1699,7 +1864,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1699
1864
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1700
1865
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1701
1866
  }
1702
- var __dxlog_file4 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1867
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1703
1868
  var INITIATION_DELAY = 100;
1704
1869
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1705
1870
  var Swarm = class {
@@ -1717,28 +1882,28 @@ var Swarm = class {
1717
1882
  this._initiationDelay = _initiationDelay;
1718
1883
  this._ctx = new import_context4.Context();
1719
1884
  this._listeningHandle = void 0;
1720
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1721
- this._instanceId = import_keys4.PublicKey.random().toHex();
1722
- this.connectionAdded = new import_async3.Event();
1723
- this.disconnected = new import_async3.Event();
1724
- this.connected = new import_async3.Event();
1885
+ this._peers = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
1886
+ this._instanceId = import_keys5.PublicKey.random().toHex();
1887
+ this.connectionAdded = new import_async4.Event();
1888
+ this.disconnected = new import_async4.Event();
1889
+ this.connected = new import_async4.Event();
1725
1890
  this.errors = new import_debug2.ErrorStream();
1726
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
1891
+ import_log5.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.begin({
1727
1892
  id: this._instanceId,
1728
1893
  data: {
1729
1894
  topic: this._topic.toHex(),
1730
1895
  peerId: this._ownPeerId.toHex()
1731
1896
  }
1732
1897
  }), {
1733
- F: __dxlog_file4,
1898
+ F: __dxlog_file5,
1734
1899
  L: 88,
1735
1900
  S: this,
1736
1901
  C: (f, a) => f(...a)
1737
1902
  });
1738
- (0, import_log4.log)("creating swarm", {
1903
+ (0, import_log5.log)("creating swarm", {
1739
1904
  peerId: _ownPeerId
1740
1905
  }, {
1741
- F: __dxlog_file4,
1906
+ F: __dxlog_file5,
1742
1907
  L: 92,
1743
1908
  S: this,
1744
1909
  C: (f, a) => f(...a)
@@ -1750,17 +1915,17 @@ var Swarm = class {
1750
1915
  onOffer: async (msg) => await this.onOffer(msg),
1751
1916
  topic: this._topic
1752
1917
  });
1753
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
1918
+ import_log5.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.end({
1754
1919
  id: this._instanceId
1755
1920
  }), {
1756
- F: __dxlog_file4,
1921
+ F: __dxlog_file5,
1757
1922
  L: 101,
1758
1923
  S: this,
1759
1924
  C: (f, a) => f(...a)
1760
1925
  });
1761
1926
  }
1762
1927
  get connections() {
1763
- return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util2.isNotNullOrUndefined);
1928
+ return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util3.isNotNullOrUndefined);
1764
1929
  }
1765
1930
  get ownPeerId() {
1766
1931
  return this._ownPeerId;
@@ -1776,7 +1941,7 @@ var Swarm = class {
1776
1941
  }
1777
1942
  async open() {
1778
1943
  (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1779
- F: __dxlog_file4,
1944
+ F: __dxlog_file5,
1780
1945
  L: 128,
1781
1946
  S: this,
1782
1947
  A: [
@@ -1788,10 +1953,10 @@ var Swarm = class {
1788
1953
  peerId: this._ownPeerId,
1789
1954
  payloadType: "dxos.mesh.swarm.SwarmMessage",
1790
1955
  onMessage: async (message) => {
1791
- await this._swarmMessenger.receiveMessage(message).catch((err) => import_log4.log.warn("Error while receiving message", {
1956
+ await this._swarmMessenger.receiveMessage(message).catch((err) => import_log5.log.warn("Error while receiving message", {
1792
1957
  err
1793
1958
  }, {
1794
- F: __dxlog_file4,
1959
+ F: __dxlog_file5,
1795
1960
  L: 135,
1796
1961
  S: this,
1797
1962
  C: (f, a) => f(...a)
@@ -1800,8 +1965,8 @@ var Swarm = class {
1800
1965
  });
1801
1966
  }
1802
1967
  async destroy() {
1803
- (0, import_log4.log)("destroying...", void 0, {
1804
- F: __dxlog_file4,
1968
+ (0, import_log5.log)("destroying...", void 0, {
1969
+ F: __dxlog_file5,
1805
1970
  L: 141,
1806
1971
  S: this,
1807
1972
  C: (f, a) => f(...a)
@@ -1811,8 +1976,8 @@ var Swarm = class {
1811
1976
  await this._ctx.dispose();
1812
1977
  await this._topology.destroy();
1813
1978
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1814
- (0, import_log4.log)("destroyed", void 0, {
1815
- F: __dxlog_file4,
1979
+ (0, import_log5.log)("destroyed", void 0, {
1980
+ F: __dxlog_file5,
1816
1981
  L: 148,
1817
1982
  S: this,
1818
1983
  C: (f, a) => f(...a)
@@ -1820,7 +1985,7 @@ var Swarm = class {
1820
1985
  }
1821
1986
  async setTopology(topology) {
1822
1987
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1823
- F: __dxlog_file4,
1988
+ F: __dxlog_file5,
1824
1989
  L: 152,
1825
1990
  S: this,
1826
1991
  A: [
@@ -1831,11 +1996,11 @@ var Swarm = class {
1831
1996
  if (topology === this._topology) {
1832
1997
  return;
1833
1998
  }
1834
- (0, import_log4.log)("setting topology", {
1999
+ (0, import_log5.log)("setting topology", {
1835
2000
  previous: getClassName(this._topology),
1836
2001
  topology: getClassName(topology)
1837
2002
  }, {
1838
- F: __dxlog_file4,
2003
+ F: __dxlog_file5,
1839
2004
  L: 156,
1840
2005
  S: this,
1841
2006
  C: (f, a) => f(...a)
@@ -1846,17 +2011,17 @@ var Swarm = class {
1846
2011
  this._topology.update();
1847
2012
  }
1848
2013
  onSwarmEvent(swarmEvent) {
1849
- (0, import_log4.log)("swarm event", {
2014
+ (0, import_log5.log)("swarm event", {
1850
2015
  swarmEvent
1851
2016
  }, {
1852
- F: __dxlog_file4,
2017
+ F: __dxlog_file5,
1853
2018
  L: 169,
1854
2019
  S: this,
1855
2020
  C: (f, a) => f(...a)
1856
2021
  });
1857
2022
  if (this._ctx.disposed) {
1858
- (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1859
- F: __dxlog_file4,
2023
+ (0, import_log5.log)("swarm event ignored for disposed swarm", void 0, {
2024
+ F: __dxlog_file5,
1860
2025
  L: 172,
1861
2026
  S: this,
1862
2027
  C: (f, a) => f(...a)
@@ -1864,11 +2029,11 @@ var Swarm = class {
1864
2029
  return;
1865
2030
  }
1866
2031
  if (swarmEvent.peerAvailable) {
1867
- const peerId = import_keys4.PublicKey.from(swarmEvent.peerAvailable.peer);
1868
- (0, import_log4.log)("new peer", {
2032
+ const peerId = import_keys5.PublicKey.from(swarmEvent.peerAvailable.peer);
2033
+ (0, import_log5.log)("new peer", {
1869
2034
  peerId
1870
2035
  }, {
1871
- F: __dxlog_file4,
2036
+ F: __dxlog_file5,
1872
2037
  L: 178,
1873
2038
  S: this,
1874
2039
  C: (f, a) => f(...a)
@@ -1878,12 +2043,12 @@ var Swarm = class {
1878
2043
  peer.advertizing = true;
1879
2044
  }
1880
2045
  } else if (swarmEvent.peerLeft) {
1881
- const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
2046
+ const peer = this._peers.get(import_keys5.PublicKey.from(swarmEvent.peerLeft.peer));
1882
2047
  if (peer) {
1883
2048
  peer.advertizing = false;
1884
2049
  if (peer.connection?.state !== ConnectionState.CONNECTED) {
1885
- void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1886
- F: __dxlog_file4,
2050
+ void this._destroyPeer(peer.id).catch((err) => import_log5.log.catch(err, void 0, {
2051
+ F: __dxlog_file5,
1887
2052
  L: 189,
1888
2053
  S: this,
1889
2054
  C: (f, a) => f(...a)
@@ -1894,17 +2059,17 @@ var Swarm = class {
1894
2059
  this._topology.update();
1895
2060
  }
1896
2061
  async onOffer(message) {
1897
- (0, import_log4.log)("offer", {
2062
+ (0, import_log5.log)("offer", {
1898
2063
  message
1899
2064
  }, {
1900
- F: __dxlog_file4,
2065
+ F: __dxlog_file5,
1901
2066
  L: 199,
1902
2067
  S: this,
1903
2068
  C: (f, a) => f(...a)
1904
2069
  });
1905
2070
  if (this._ctx.disposed) {
1906
- (0, import_log4.log)("ignored for disposed swarm", void 0, {
1907
- F: __dxlog_file4,
2071
+ (0, import_log5.log)("ignored for disposed swarm", void 0, {
2072
+ F: __dxlog_file5,
1908
2073
  L: 201,
1909
2074
  S: this,
1910
2075
  C: (f, a) => f(...a)
@@ -1914,7 +2079,7 @@ var Swarm = class {
1914
2079
  };
1915
2080
  }
1916
2081
  (0, import_invariant4.invariant)(message.author, void 0, {
1917
- F: __dxlog_file4,
2082
+ F: __dxlog_file5,
1918
2083
  L: 206,
1919
2084
  S: this,
1920
2085
  A: [
@@ -1923,10 +2088,10 @@ var Swarm = class {
1923
2088
  ]
1924
2089
  });
1925
2090
  if (!message.recipient?.equals(this._ownPeerId)) {
1926
- (0, import_log4.log)("rejecting offer with incorrect peerId", {
2091
+ (0, import_log5.log)("rejecting offer with incorrect peerId", {
1927
2092
  message
1928
2093
  }, {
1929
- F: __dxlog_file4,
2094
+ F: __dxlog_file5,
1930
2095
  L: 208,
1931
2096
  S: this,
1932
2097
  C: (f, a) => f(...a)
@@ -1936,10 +2101,10 @@ var Swarm = class {
1936
2101
  };
1937
2102
  }
1938
2103
  if (!message.topic?.equals(this._topic)) {
1939
- (0, import_log4.log)("rejecting offer with incorrect topic", {
2104
+ (0, import_log5.log)("rejecting offer with incorrect topic", {
1940
2105
  message
1941
2106
  }, {
1942
- F: __dxlog_file4,
2107
+ F: __dxlog_file5,
1943
2108
  L: 212,
1944
2109
  S: this,
1945
2110
  C: (f, a) => f(...a)
@@ -1954,17 +2119,17 @@ var Swarm = class {
1954
2119
  return answer;
1955
2120
  }
1956
2121
  async onSignal(message) {
1957
- (0, import_log4.log)("signal", {
2122
+ (0, import_log5.log)("signal", {
1958
2123
  message
1959
2124
  }, {
1960
- F: __dxlog_file4,
2125
+ F: __dxlog_file5,
1961
2126
  L: 223,
1962
2127
  S: this,
1963
2128
  C: (f, a) => f(...a)
1964
2129
  });
1965
2130
  if (this._ctx.disposed) {
1966
- import_log4.log.info("ignored for offline swarm", void 0, {
1967
- F: __dxlog_file4,
2131
+ import_log5.log.info("ignored for offline swarm", void 0, {
2132
+ F: __dxlog_file5,
1968
2133
  L: 225,
1969
2134
  S: this,
1970
2135
  C: (f, a) => f(...a)
@@ -1972,7 +2137,7 @@ var Swarm = class {
1972
2137
  return;
1973
2138
  }
1974
2139
  (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1975
- F: __dxlog_file4,
2140
+ F: __dxlog_file5,
1976
2141
  L: 228,
1977
2142
  S: this,
1978
2143
  A: [
@@ -1981,7 +2146,7 @@ var Swarm = class {
1981
2146
  ]
1982
2147
  });
1983
2148
  (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1984
- F: __dxlog_file4,
2149
+ F: __dxlog_file5,
1985
2150
  L: 232,
1986
2151
  S: this,
1987
2152
  A: [
@@ -1990,7 +2155,7 @@ var Swarm = class {
1990
2155
  ]
1991
2156
  });
1992
2157
  (0, import_invariant4.invariant)(message.author, void 0, {
1993
- F: __dxlog_file4,
2158
+ F: __dxlog_file5,
1994
2159
  L: 233,
1995
2160
  S: this,
1996
2161
  A: [
@@ -2050,7 +2215,7 @@ var Swarm = class {
2050
2215
  }
2051
2216
  async _destroyPeer(peerId) {
2052
2217
  (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
2053
- F: __dxlog_file4,
2218
+ F: __dxlog_file5,
2054
2219
  L: 303,
2055
2220
  S: this,
2056
2221
  A: [
@@ -2072,12 +2237,12 @@ var Swarm = class {
2072
2237
  if (this._ctx.disposed) {
2073
2238
  return;
2074
2239
  }
2075
- (0, import_async3.scheduleTask)(this._ctx, async () => {
2240
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
2076
2241
  try {
2077
2242
  await this._initiateConnection(peer);
2078
2243
  } catch (err) {
2079
- (0, import_log4.log)("initiation error", err, {
2080
- F: __dxlog_file4,
2244
+ (0, import_log5.log)("initiation error", err, {
2245
+ F: __dxlog_file5,
2081
2246
  L: 329,
2082
2247
  S: this,
2083
2248
  C: (f, a) => f(...a)
@@ -2089,7 +2254,7 @@ var Swarm = class {
2089
2254
  if (this._ctx.disposed) {
2090
2255
  return;
2091
2256
  }
2092
- (0, import_async3.scheduleTask)(this._ctx, async () => {
2257
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
2093
2258
  await this._closeConnection(peer);
2094
2259
  this._topology.update();
2095
2260
  });
@@ -2102,15 +2267,15 @@ var Swarm = class {
2102
2267
  async _initiateConnection(remoteId) {
2103
2268
  const ctx = this._ctx;
2104
2269
  if (remoteId.toHex() < this._ownPeerId.toHex()) {
2105
- (0, import_log4.log)("initiation delay", {
2270
+ (0, import_log5.log)("initiation delay", {
2106
2271
  remoteId
2107
2272
  }, {
2108
- F: __dxlog_file4,
2273
+ F: __dxlog_file5,
2109
2274
  L: 356,
2110
2275
  S: this,
2111
2276
  C: (f, a) => f(...a)
2112
2277
  });
2113
- await (0, import_async3.sleep)(this._initiationDelay);
2278
+ await (0, import_async4.sleep)(this._initiationDelay);
2114
2279
  }
2115
2280
  if (ctx.disposed) {
2116
2281
  return;
@@ -2119,20 +2284,20 @@ var Swarm = class {
2119
2284
  if (peer.connection) {
2120
2285
  return;
2121
2286
  }
2122
- (0, import_log4.log)("initiating connection...", {
2287
+ (0, import_log5.log)("initiating connection...", {
2123
2288
  remoteId
2124
2289
  }, {
2125
- F: __dxlog_file4,
2290
+ F: __dxlog_file5,
2126
2291
  L: 370,
2127
2292
  S: this,
2128
2293
  C: (f, a) => f(...a)
2129
2294
  });
2130
2295
  await peer.initiateConnection();
2131
2296
  this._topology.update();
2132
- (0, import_log4.log)("initiated", {
2297
+ (0, import_log5.log)("initiated", {
2133
2298
  remoteId
2134
2299
  }, {
2135
- F: __dxlog_file4,
2300
+ F: __dxlog_file5,
2136
2301
  L: 373,
2137
2302
  S: this,
2138
2303
  C: (f, a) => f(...a)
@@ -2147,44 +2312,43 @@ var Swarm = class {
2147
2312
  }
2148
2313
  };
2149
2314
  _ts_decorate3([
2150
- import_log4.logInfo
2315
+ import_log5.logInfo
2151
2316
  ], Swarm.prototype, "_instanceId", void 0);
2152
2317
  _ts_decorate3([
2153
- import_log4.logInfo
2318
+ import_log5.logInfo
2154
2319
  ], Swarm.prototype, "ownPeerId", null);
2155
2320
  _ts_decorate3([
2156
- import_log4.logInfo
2321
+ import_log5.logInfo
2157
2322
  ], Swarm.prototype, "topic", null);
2158
2323
  _ts_decorate3([
2159
- import_async3.synchronized
2324
+ import_async4.synchronized
2160
2325
  ], Swarm.prototype, "onSwarmEvent", null);
2161
2326
  _ts_decorate3([
2162
- import_async3.synchronized
2327
+ import_async4.synchronized
2163
2328
  ], Swarm.prototype, "onOffer", null);
2164
2329
  _ts_decorate3([
2165
- import_async3.synchronized
2330
+ import_async4.synchronized
2166
2331
  ], Swarm.prototype, "goOffline", null);
2167
2332
  _ts_decorate3([
2168
- import_async3.synchronized
2333
+ import_async4.synchronized
2169
2334
  ], Swarm.prototype, "goOnline", null);
2170
2335
 
2171
2336
  // packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts
2172
- var import_async4 = require("@dxos/async");
2173
- var import_keys5 = require("@dxos/keys");
2174
- var import_log5 = require("@dxos/log");
2175
- var import_util3 = require("@dxos/util");
2176
- var __dxlog_file5 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2337
+ var import_async5 = require("@dxos/async");
2338
+ var import_keys6 = require("@dxos/keys");
2339
+ var import_log6 = require("@dxos/log");
2340
+ var import_util4 = require("@dxos/util");
2341
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2177
2342
  var SwarmMapper = class {
2178
2343
  get peers() {
2179
2344
  return Array.from(this._peers.values());
2180
2345
  }
2181
- // prettier-ignore
2182
2346
  constructor(_swarm) {
2183
2347
  this._swarm = _swarm;
2184
- this._subscriptions = new import_async4.EventSubscriptions();
2185
- this._connectionSubscriptions = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
2186
- this._peers = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
2187
- this.mapUpdated = new import_async4.Event();
2348
+ this._subscriptions = new import_async5.EventSubscriptions();
2349
+ this._connectionSubscriptions = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2350
+ this._peers = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2351
+ this.mapUpdated = new import_async5.Event();
2188
2352
  this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
2189
2353
  this._update();
2190
2354
  this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
@@ -2199,9 +2363,9 @@ var SwarmMapper = class {
2199
2363
  this._update();
2200
2364
  }
2201
2365
  _update() {
2202
- (0, import_log5.log)("updating swarm", void 0, {
2203
- F: __dxlog_file5,
2204
- L: 75,
2366
+ (0, import_log6.log)("updating swarm", void 0, {
2367
+ F: __dxlog_file6,
2368
+ L: 72,
2205
2369
  S: this,
2206
2370
  C: (f, a) => f(...a)
2207
2371
  });
@@ -2220,12 +2384,12 @@ var SwarmMapper = class {
2220
2384
  ]
2221
2385
  });
2222
2386
  }
2223
- (0, import_log5.log)("graph changed", {
2387
+ (0, import_log6.log)("graph changed", {
2224
2388
  directConnections: this._swarm.connections.length,
2225
2389
  totalPeersInSwarm: this._peers.size
2226
2390
  }, {
2227
- F: __dxlog_file5,
2228
- L: 116,
2391
+ F: __dxlog_file6,
2392
+ L: 113,
2229
2393
  S: this,
2230
2394
  C: (f, a) => f(...a)
2231
2395
  });
@@ -2239,23 +2403,23 @@ var SwarmMapper = class {
2239
2403
  };
2240
2404
 
2241
2405
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
2242
- var import_async5 = require("@dxos/async");
2406
+ var import_async6 = require("@dxos/async");
2243
2407
  var import_context5 = require("@dxos/context");
2244
- var import_errors3 = require("@dxos/errors");
2245
2408
  var import_invariant5 = require("@dxos/invariant");
2246
- var import_keys6 = require("@dxos/keys");
2247
- var import_log6 = require("@dxos/log");
2248
- var import_util4 = require("@dxos/util");
2249
- var __dxlog_file6 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2250
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
2409
+ var import_keys7 = require("@dxos/keys");
2410
+ var import_log7 = require("@dxos/log");
2411
+ var import_protocols5 = require("@dxos/protocols");
2412
+ var import_util5 = require("@dxos/util");
2413
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2414
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2251
2415
  var ConnectionLimiter = class {
2252
2416
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2253
2417
  this._ctx = new import_context5.Context();
2254
2418
  /**
2255
2419
  * Queue of promises to resolve when initiating connections amount is below the limit.
2256
2420
  */
2257
- this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2258
- this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
2421
+ this._waitingPromises = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
2422
+ this.resolveWaitingPromises = new import_async6.DeferredTask(this._ctx, async () => {
2259
2423
  Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2260
2424
  resolve();
2261
2425
  });
@@ -2267,7 +2431,7 @@ var ConnectionLimiter = class {
2267
2431
  */
2268
2432
  async connecting(sessionId) {
2269
2433
  (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
2270
- F: __dxlog_file6,
2434
+ F: __dxlog_file7,
2271
2435
  L: 48,
2272
2436
  S: this,
2273
2437
  A: [
@@ -2275,10 +2439,10 @@ var ConnectionLimiter = class {
2275
2439
  "'Peer is already waiting for connection'"
2276
2440
  ]
2277
2441
  });
2278
- (0, import_log6.log)("waiting", {
2442
+ (0, import_log7.log)("waiting", {
2279
2443
  sessionId
2280
2444
  }, {
2281
- F: __dxlog_file6,
2445
+ F: __dxlog_file7,
2282
2446
  L: 49,
2283
2447
  S: this,
2284
2448
  C: (f, a) => f(...a)
@@ -2290,10 +2454,10 @@ var ConnectionLimiter = class {
2290
2454
  });
2291
2455
  this.resolveWaitingPromises.schedule();
2292
2456
  });
2293
- (0, import_log6.log)("allow", {
2457
+ (0, import_log7.log)("allow", {
2294
2458
  sessionId
2295
2459
  }, {
2296
- F: __dxlog_file6,
2460
+ F: __dxlog_file7,
2297
2461
  L: 57,
2298
2462
  S: this,
2299
2463
  C: (f, a) => f(...a)
@@ -2303,10 +2467,10 @@ var ConnectionLimiter = class {
2303
2467
  * Rejects promise returned by `connecting` method.
2304
2468
  */
2305
2469
  doneConnecting(sessionId) {
2306
- (0, import_log6.log)("done", {
2470
+ (0, import_log7.log)("done", {
2307
2471
  sessionId
2308
2472
  }, {
2309
- F: __dxlog_file6,
2473
+ F: __dxlog_file7,
2310
2474
  L: 64,
2311
2475
  S: this,
2312
2476
  C: (f, a) => f(...a)
@@ -2314,7 +2478,7 @@ var ConnectionLimiter = class {
2314
2478
  if (!this._waitingPromises.has(sessionId)) {
2315
2479
  return;
2316
2480
  }
2317
- this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
2481
+ this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
2318
2482
  this._waitingPromises.delete(sessionId);
2319
2483
  this.resolveWaitingPromises.schedule();
2320
2484
  }
@@ -2334,8 +2498,8 @@ var ConnectionLog = class {
2334
2498
  /**
2335
2499
  * SwarmId => info
2336
2500
  */
2337
- this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
2338
- this.update = new import_async6.Event();
2501
+ this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2502
+ this.update = new import_async7.Event();
2339
2503
  }
2340
2504
  getSwarmInfo(swarmId) {
2341
2505
  return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
@@ -2345,13 +2509,13 @@ var ConnectionLog = class {
2345
2509
  }
2346
2510
  joinedSwarm(swarm) {
2347
2511
  const info = {
2348
- id: import_keys7.PublicKey.from(swarm._instanceId),
2512
+ id: import_keys8.PublicKey.from(swarm._instanceId),
2349
2513
  topic: swarm.topic,
2350
2514
  isActive: true,
2351
2515
  label: swarm.label,
2352
2516
  connections: []
2353
2517
  };
2354
- this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
2518
+ this._swarms.set(import_keys8.PublicKey.from(swarm._instanceId), info);
2355
2519
  this.update.emit();
2356
2520
  swarm.connectionAdded.on((connection) => {
2357
2521
  const connectionInfo = {
@@ -2381,24 +2545,24 @@ var ConnectionLog = class {
2381
2545
  });
2382
2546
  }
2383
2547
  leftSwarm(swarm) {
2384
- this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
2548
+ this.getSwarmInfo(import_keys8.PublicKey.from(swarm._instanceId)).isActive = false;
2385
2549
  this.update.emit();
2386
2550
  }
2387
2551
  };
2388
2552
 
2389
2553
  // packages/core/mesh/network-manager/src/network-manager.ts
2390
- var __dxlog_file7 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/network-manager.ts";
2554
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2391
2555
  var NetworkManager = class {
2392
2556
  constructor({ transportFactory, signalManager, log: log1 }) {
2393
2557
  /**
2394
2558
  * @internal
2395
2559
  */
2396
- this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2397
- this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2560
+ this._swarms = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2561
+ this._mappers = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2398
2562
  this._connectionState = import_services.ConnectionState.ONLINE;
2399
- this.connectionStateChanged = new import_async7.Event();
2400
- this.topicsUpdated = new import_async7.Event();
2401
- this._instanceId = import_keys8.PublicKey.random().toHex();
2563
+ this.connectionStateChanged = new import_async8.Event();
2564
+ this.topicsUpdated = new import_async8.Event();
2565
+ this._instanceId = import_keys9.PublicKey.random().toHex();
2402
2566
  this._transportFactory = transportFactory;
2403
2567
  this._signalManager = signalManager;
2404
2568
  this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
@@ -2432,20 +2596,20 @@ var NetworkManager = class {
2432
2596
  return this._swarms.get(topic);
2433
2597
  }
2434
2598
  async open() {
2435
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
2599
+ import_log8.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
2436
2600
  id: this._instanceId
2437
2601
  }), {
2438
- F: __dxlog_file7,
2602
+ F: __dxlog_file8,
2439
2603
  L: 130,
2440
2604
  S: this,
2441
2605
  C: (f, a) => f(...a)
2442
2606
  });
2443
2607
  await this._messenger.open();
2444
2608
  await this._signalManager.open();
2445
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
2609
+ import_log8.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
2446
2610
  id: this._instanceId
2447
2611
  }), {
2448
- F: __dxlog_file7,
2612
+ F: __dxlog_file8,
2449
2613
  L: 133,
2450
2614
  S: this,
2451
2615
  C: (f, a) => f(...a)
@@ -2454,8 +2618,8 @@ var NetworkManager = class {
2454
2618
  async close() {
2455
2619
  for (const topic of this._swarms.keys()) {
2456
2620
  await this.leaveSwarm(topic).catch((err) => {
2457
- (0, import_log7.log)(err, void 0, {
2458
- F: __dxlog_file7,
2621
+ (0, import_log8.log)(err, void 0, {
2622
+ F: __dxlog_file8,
2459
2623
  L: 139,
2460
2624
  S: this,
2461
2625
  C: (f, a) => f(...a)
@@ -2469,8 +2633,8 @@ var NetworkManager = class {
2469
2633
  * Join the swarm.
2470
2634
  */
2471
2635
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
2472
- (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
2473
- F: __dxlog_file7,
2636
+ (0, import_invariant6.invariant)(import_keys9.PublicKey.isPublicKey(topic), void 0, {
2637
+ F: __dxlog_file8,
2474
2638
  L: 157,
2475
2639
  S: this,
2476
2640
  A: [
@@ -2478,8 +2642,8 @@ var NetworkManager = class {
2478
2642
  ""
2479
2643
  ]
2480
2644
  });
2481
- (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
2482
- F: __dxlog_file7,
2645
+ (0, import_invariant6.invariant)(import_keys9.PublicKey.isPublicKey(peerId), void 0, {
2646
+ F: __dxlog_file8,
2483
2647
  L: 158,
2484
2648
  S: this,
2485
2649
  A: [
@@ -2488,7 +2652,7 @@ var NetworkManager = class {
2488
2652
  ]
2489
2653
  });
2490
2654
  (0, import_invariant6.invariant)(topology, void 0, {
2491
- F: __dxlog_file7,
2655
+ F: __dxlog_file8,
2492
2656
  L: 159,
2493
2657
  S: this,
2494
2658
  A: [
@@ -2497,7 +2661,7 @@ var NetworkManager = class {
2497
2661
  ]
2498
2662
  });
2499
2663
  (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
2500
- F: __dxlog_file7,
2664
+ F: __dxlog_file8,
2501
2665
  L: 160,
2502
2666
  S: this,
2503
2667
  A: [
@@ -2506,24 +2670,24 @@ var NetworkManager = class {
2506
2670
  ]
2507
2671
  });
2508
2672
  if (this._swarms.has(topic)) {
2509
- throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
2673
+ throw new Error(`Already connected to swarm: ${import_keys9.PublicKey.from(topic)}`);
2510
2674
  }
2511
- (0, import_log7.log)("joining", {
2512
- topic: import_keys8.PublicKey.from(topic),
2675
+ (0, import_log8.log)("joining", {
2676
+ topic: import_keys9.PublicKey.from(topic),
2513
2677
  peerId,
2514
2678
  topology: topology.toString()
2515
2679
  }, {
2516
- F: __dxlog_file7,
2680
+ F: __dxlog_file8,
2517
2681
  L: 165,
2518
2682
  S: this,
2519
2683
  C: (f, a) => f(...a)
2520
2684
  });
2521
2685
  const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
2522
2686
  swarm.errors.handle((error) => {
2523
- (0, import_log7.log)("swarm error", {
2687
+ (0, import_log8.log)("swarm error", {
2524
2688
  error
2525
2689
  }, {
2526
- F: __dxlog_file7,
2690
+ F: __dxlog_file8,
2527
2691
  L: 178,
2528
2692
  S: this,
2529
2693
  C: (f, a) => f(...a)
@@ -2535,19 +2699,19 @@ var NetworkManager = class {
2535
2699
  this._signalConnection.join({
2536
2700
  topic,
2537
2701
  peerId
2538
- }).catch((error) => import_log7.log.catch(error, void 0, {
2539
- F: __dxlog_file7,
2702
+ }).catch((error) => import_log8.log.catch(error, void 0, {
2703
+ F: __dxlog_file8,
2540
2704
  L: 187,
2541
2705
  S: this,
2542
2706
  C: (f, a) => f(...a)
2543
2707
  }));
2544
2708
  this.topicsUpdated.emit();
2545
2709
  this._connectionLog?.joinedSwarm(swarm);
2546
- (0, import_log7.log)("joined", {
2547
- topic: import_keys8.PublicKey.from(topic),
2710
+ (0, import_log8.log)("joined", {
2711
+ topic: import_keys9.PublicKey.from(topic),
2548
2712
  count: this._swarms.size
2549
2713
  }, {
2550
- F: __dxlog_file7,
2714
+ F: __dxlog_file8,
2551
2715
  L: 191,
2552
2716
  S: this,
2553
2717
  C: (f, a) => f(...a)
@@ -2563,10 +2727,10 @@ var NetworkManager = class {
2563
2727
  if (!this._swarms.has(topic)) {
2564
2728
  return;
2565
2729
  }
2566
- (0, import_log7.log)("leaving", {
2567
- topic: import_keys8.PublicKey.from(topic)
2730
+ (0, import_log8.log)("leaving", {
2731
+ topic: import_keys9.PublicKey.from(topic)
2568
2732
  }, {
2569
- F: __dxlog_file7,
2733
+ F: __dxlog_file8,
2570
2734
  L: 207,
2571
2735
  S: this,
2572
2736
  C: (f, a) => f(...a)
@@ -2583,11 +2747,11 @@ var NetworkManager = class {
2583
2747
  await swarm.destroy();
2584
2748
  this._swarms.delete(topic);
2585
2749
  await this.topicsUpdated.emit();
2586
- (0, import_log7.log)("left", {
2587
- topic: import_keys8.PublicKey.from(topic),
2750
+ (0, import_log8.log)("left", {
2751
+ topic: import_keys9.PublicKey.from(topic),
2588
2752
  count: this._swarms.size
2589
2753
  }, {
2590
- F: __dxlog_file7,
2754
+ F: __dxlog_file8,
2591
2755
  L: 221,
2592
2756
  S: this,
2593
2757
  C: (f, a) => f(...a)
@@ -2623,14 +2787,14 @@ var NetworkManager = class {
2623
2787
 
2624
2788
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
2625
2789
  var import_invariant7 = require("@dxos/invariant");
2626
- var __dxlog_file8 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2790
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2627
2791
  var FullyConnectedTopology = class {
2628
2792
  toString() {
2629
2793
  return "FullyConnectedTopology";
2630
2794
  }
2631
2795
  init(controller) {
2632
2796
  (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2633
- F: __dxlog_file8,
2797
+ F: __dxlog_file9,
2634
2798
  L: 18,
2635
2799
  S: this,
2636
2800
  A: [
@@ -2642,7 +2806,7 @@ var FullyConnectedTopology = class {
2642
2806
  }
2643
2807
  update() {
2644
2808
  (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2645
- F: __dxlog_file8,
2809
+ F: __dxlog_file9,
2646
2810
  L: 23,
2647
2811
  S: this,
2648
2812
  A: [
@@ -2665,20 +2829,20 @@ var FullyConnectedTopology = class {
2665
2829
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
2666
2830
  var import_xor_distance = __toESM(require("xor-distance"));
2667
2831
  var import_invariant8 = require("@dxos/invariant");
2668
- var import_log8 = require("@dxos/log");
2832
+ var import_log9 = require("@dxos/log");
2669
2833
 
2670
2834
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
2671
2835
  var import_invariant9 = require("@dxos/invariant");
2672
- var import_log9 = require("@dxos/log");
2836
+ var import_log10 = require("@dxos/log");
2673
2837
 
2674
2838
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
2675
2839
  var import_node_stream = require("node:stream");
2676
- var import_async8 = require("@dxos/async");
2840
+ var import_async9 = require("@dxos/async");
2677
2841
  var import_debug4 = require("@dxos/debug");
2678
2842
  var import_invariant10 = require("@dxos/invariant");
2679
- var import_keys9 = require("@dxos/keys");
2680
- var import_log10 = require("@dxos/log");
2681
- var import_util7 = require("@dxos/util");
2843
+ var import_keys10 = require("@dxos/keys");
2844
+ var import_log11 = require("@dxos/log");
2845
+ var import_util8 = require("@dxos/util");
2682
2846
  function _ts_decorate4(decorators, target, key, desc) {
2683
2847
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2684
2848
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -2689,7 +2853,7 @@ function _ts_decorate4(decorators, target, key, desc) {
2689
2853
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2690
2854
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2691
2855
  }
2692
- var __dxlog_file9 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2856
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2693
2857
  var MEMORY_TRANSPORT_DELAY = 1;
2694
2858
  var createStreamDelay = (delay) => {
2695
2859
  return new import_node_stream.Transform({
@@ -2705,26 +2869,26 @@ var MemoryTransportFactory = {
2705
2869
  var MemoryTransport = class _MemoryTransport {
2706
2870
  static {
2707
2871
  // TODO(burdon): Remove static properties (inject context into constructor).
2708
- this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2872
+ this._connections = new import_util8.ComplexMap(import_keys10.PublicKey.hash);
2709
2873
  }
2710
2874
  constructor(options) {
2711
2875
  this.options = options;
2712
- this.closed = new import_async8.Event();
2713
- this.connected = new import_async8.Event();
2876
+ this.closed = new import_async9.Event();
2877
+ this.connected = new import_async9.Event();
2714
2878
  this.errors = new import_debug4.ErrorStream();
2715
- this._instanceId = import_keys9.PublicKey.random();
2716
- this._remote = new import_async8.Trigger();
2879
+ this._instanceId = import_keys10.PublicKey.random();
2880
+ this._remote = new import_async9.Trigger();
2717
2881
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2718
2882
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2719
2883
  this._destroyed = false;
2720
- (0, import_log10.log)("creating", void 0, {
2721
- F: __dxlog_file9,
2884
+ (0, import_log11.log)("creating", void 0, {
2885
+ F: __dxlog_file10,
2722
2886
  L: 64,
2723
2887
  S: this,
2724
2888
  C: (f, a) => f(...a)
2725
2889
  });
2726
2890
  (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2727
- F: __dxlog_file9,
2891
+ F: __dxlog_file10,
2728
2892
  L: 66,
2729
2893
  S: this,
2730
2894
  A: [
@@ -2735,8 +2899,8 @@ var MemoryTransport = class _MemoryTransport {
2735
2899
  _MemoryTransport._connections.set(this._instanceId, this);
2736
2900
  if (this.options.initiator) {
2737
2901
  setTimeout(async () => {
2738
- (0, import_log10.log)("sending signal", void 0, {
2739
- F: __dxlog_file9,
2902
+ (0, import_log11.log)("sending signal", void 0, {
2903
+ F: __dxlog_file10,
2740
2904
  L: 73,
2741
2905
  S: this,
2742
2906
  C: (f, a) => f(...a)
@@ -2766,7 +2930,7 @@ var MemoryTransport = class _MemoryTransport {
2766
2930
  return;
2767
2931
  }
2768
2932
  (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2769
- F: __dxlog_file9,
2933
+ F: __dxlog_file10,
2770
2934
  L: 99,
2771
2935
  S: this,
2772
2936
  A: [
@@ -2776,8 +2940,8 @@ var MemoryTransport = class _MemoryTransport {
2776
2940
  });
2777
2941
  this._remoteConnection._remoteConnection = this;
2778
2942
  this._remoteConnection._remoteInstanceId = this._instanceId;
2779
- (0, import_log10.log)("connected", void 0, {
2780
- F: __dxlog_file9,
2943
+ (0, import_log11.log)("connected", void 0, {
2944
+ F: __dxlog_file10,
2781
2945
  L: 103,
2782
2946
  S: this,
2783
2947
  C: (f, a) => f(...a)
@@ -2794,8 +2958,8 @@ var MemoryTransport = class _MemoryTransport {
2794
2958
  }
2795
2959
  }
2796
2960
  async destroy() {
2797
- (0, import_log10.log)("closing", void 0, {
2798
- F: __dxlog_file9,
2961
+ (0, import_log11.log)("closing", void 0, {
2962
+ F: __dxlog_file10,
2799
2963
  L: 124,
2800
2964
  S: this,
2801
2965
  C: (f, a) => f(...a)
@@ -2803,8 +2967,8 @@ var MemoryTransport = class _MemoryTransport {
2803
2967
  this._destroyed = true;
2804
2968
  _MemoryTransport._connections.delete(this._instanceId);
2805
2969
  if (this._remoteConnection) {
2806
- (0, import_log10.log)("closing", void 0, {
2807
- F: __dxlog_file9,
2970
+ (0, import_log11.log)("closing", void 0, {
2971
+ F: __dxlog_file10,
2808
2972
  L: 129,
2809
2973
  S: this,
2810
2974
  C: (f, a) => f(...a)
@@ -2816,26 +2980,26 @@ var MemoryTransport = class _MemoryTransport {
2816
2980
  this._remoteConnection.closed.emit();
2817
2981
  this._remoteConnection._remoteConnection = void 0;
2818
2982
  this._remoteConnection = void 0;
2819
- (0, import_log10.log)("closed", void 0, {
2820
- F: __dxlog_file9,
2983
+ (0, import_log11.log)("closed", void 0, {
2984
+ F: __dxlog_file10,
2821
2985
  L: 147,
2822
2986
  S: this,
2823
2987
  C: (f, a) => f(...a)
2824
2988
  });
2825
2989
  }
2826
2990
  this.closed.emit();
2827
- (0, import_log10.log)("closed", void 0, {
2828
- F: __dxlog_file9,
2991
+ (0, import_log11.log)("closed", void 0, {
2992
+ F: __dxlog_file10,
2829
2993
  L: 151,
2830
2994
  S: this,
2831
2995
  C: (f, a) => f(...a)
2832
2996
  });
2833
2997
  }
2834
2998
  signal({ payload }) {
2835
- (0, import_log10.log)("received signal", {
2999
+ (0, import_log11.log)("received signal", {
2836
3000
  payload
2837
3001
  }, {
2838
- F: __dxlog_file9,
3002
+ F: __dxlog_file10,
2839
3003
  L: 155,
2840
3004
  S: this,
2841
3005
  C: (f, a) => f(...a)
@@ -2845,16 +3009,16 @@ var MemoryTransport = class _MemoryTransport {
2845
3009
  }
2846
3010
  const transportId = payload.transportId;
2847
3011
  if (transportId) {
2848
- const remoteId = import_keys9.PublicKey.fromHex(transportId);
3012
+ const remoteId = import_keys10.PublicKey.fromHex(transportId);
2849
3013
  this._remote.wake(remoteId);
2850
3014
  }
2851
3015
  }
2852
3016
  };
2853
3017
  _ts_decorate4([
2854
- import_log10.logInfo
3018
+ import_log11.logInfo
2855
3019
  ], MemoryTransport.prototype, "_instanceId", void 0);
2856
3020
  _ts_decorate4([
2857
- import_log10.logInfo
3021
+ import_log11.logInfo
2858
3022
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2859
3023
 
2860
3024
  // packages/core/mesh/network-manager/src/transport/transport.ts
@@ -2864,16 +3028,17 @@ var TransportKind;
2864
3028
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2865
3029
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2866
3030
  TransportKind2["MEMORY"] = "MEMORY";
3031
+ TransportKind2["TCP"] = "TCP";
2867
3032
  })(TransportKind || (TransportKind = {}));
2868
3033
 
2869
3034
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2870
3035
  var import_simple_peer = __toESM(require("simple-peer"));
2871
3036
  var import_tiny_invariant = __toESM(require("tiny-invariant"));
2872
- var import_async9 = require("@dxos/async");
2873
- var import_debug5 = require("@dxos/debug");
2874
- var import_keys10 = require("@dxos/keys");
2875
- var import_log11 = require("@dxos/log");
2876
- var import_protocols5 = require("@dxos/protocols");
3037
+ var import_async10 = require("@dxos/async");
3038
+ var import_debug5 = require("@dxos/debug");
3039
+ var import_keys11 = require("@dxos/keys");
3040
+ var import_log12 = require("@dxos/log");
3041
+ var import_protocols7 = require("@dxos/protocols");
2877
3042
 
2878
3043
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
2879
3044
  var wrtc = null;
@@ -2883,26 +3048,26 @@ try {
2883
3048
  }
2884
3049
 
2885
3050
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2886
- var __dxlog_file10 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
3051
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2887
3052
  var SimplePeerTransport = class {
2888
3053
  constructor(params) {
2889
3054
  this.params = params;
2890
3055
  this._closed = false;
2891
- this.closed = new import_async9.Event();
2892
- this.connected = new import_async9.Event();
3056
+ this.closed = new import_async10.Event();
3057
+ this.connected = new import_async10.Event();
2893
3058
  this.errors = new import_debug5.ErrorStream();
2894
- this._instanceId = import_keys10.PublicKey.random().toHex();
2895
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
3059
+ this._instanceId = import_keys11.PublicKey.random().toHex();
3060
+ import_log12.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
2896
3061
  id: this._instanceId
2897
3062
  }), {
2898
- F: __dxlog_file10,
2899
- L: 39,
3063
+ F: __dxlog_file11,
3064
+ L: 40,
2900
3065
  S: this,
2901
3066
  C: (f, a) => f(...a)
2902
3067
  });
2903
- (0, import_log11.log)("created connection", params, {
2904
- F: __dxlog_file10,
2905
- L: 40,
3068
+ (0, import_log12.log)("created connection", params, {
3069
+ F: __dxlog_file11,
3070
+ L: 41,
2906
3071
  S: this,
2907
3072
  C: (f, a) => f(...a)
2908
3073
  });
@@ -2913,9 +3078,9 @@ var SimplePeerTransport = class {
2913
3078
  config: this.params.webrtcConfig
2914
3079
  });
2915
3080
  this._peer.on("signal", async (data) => {
2916
- (0, import_log11.log)("signal", data, {
2917
- F: __dxlog_file10,
2918
- L: 49,
3081
+ (0, import_log12.log)("signal", data, {
3082
+ F: __dxlog_file11,
3083
+ L: 50,
2919
3084
  S: this,
2920
3085
  C: (f, a) => f(...a)
2921
3086
  });
@@ -2926,9 +3091,9 @@ var SimplePeerTransport = class {
2926
3091
  });
2927
3092
  });
2928
3093
  this._peer.on("connect", () => {
2929
- (0, import_log11.log)("connected", void 0, {
2930
- F: __dxlog_file10,
2931
- L: 54,
3094
+ (0, import_log12.log)("connected", void 0, {
3095
+ F: __dxlog_file11,
3096
+ L: 55,
2932
3097
  S: this,
2933
3098
  C: (f, a) => f(...a)
2934
3099
  });
@@ -2936,9 +3101,9 @@ var SimplePeerTransport = class {
2936
3101
  this.connected.emit();
2937
3102
  });
2938
3103
  this._peer.on("close", async () => {
2939
- (0, import_log11.log)("closed", void 0, {
2940
- F: __dxlog_file10,
2941
- L: 60,
3104
+ (0, import_log12.log)("closed", void 0, {
3105
+ F: __dxlog_file11,
3106
+ L: 61,
2942
3107
  S: this,
2943
3108
  C: (f, a) => f(...a)
2944
3109
  });
@@ -2946,38 +3111,86 @@ var SimplePeerTransport = class {
2946
3111
  this.closed.emit();
2947
3112
  });
2948
3113
  this._peer.on("error", async (err) => {
2949
- this.errors.raise(err);
3114
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
3115
+ if (err.errorDetail === "sctp-failure") {
3116
+ this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
3117
+ } else {
3118
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3119
+ }
3120
+ } else if ("code" in err) {
3121
+ import_log12.log.info("simple-peer error", err, {
3122
+ F: __dxlog_file11,
3123
+ L: 77,
3124
+ S: this,
3125
+ C: (f, a) => f(...a)
3126
+ });
3127
+ switch (err.code) {
3128
+ case "ERR_WEBRTC_SUPPORT":
3129
+ this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3130
+ break;
3131
+ case "ERR_ICE_CONNECTION_FAILURE":
3132
+ case "ERR_DATA_CHANNEL":
3133
+ case "ERR_CONNECTION_FAILURE":
3134
+ case "ERR_SIGNALING":
3135
+ this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3136
+ break;
3137
+ case "ERR_CREATE_OFFER":
3138
+ case "ERR_CREATE_ANSWER":
3139
+ case "ERR_SET_LOCAL_DESCRIPTION":
3140
+ case "ERR_SET_REMOTE_DESCRIPTION":
3141
+ case "ERR_ADD_ICE_CANDIDATE":
3142
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3143
+ break;
3144
+ default:
3145
+ this.errors.raise(new Error("unknown simple-peer error"));
3146
+ break;
3147
+ }
3148
+ } else {
3149
+ import_log12.log.info("unknown peer connection error", err, {
3150
+ F: __dxlog_file11,
3151
+ L: 101,
3152
+ S: this,
3153
+ C: (f, a) => f(...a)
3154
+ });
3155
+ this.errors.raise(err);
3156
+ }
2950
3157
  try {
2951
- if (typeof this._peer?._pc.getStats === "function") {
3158
+ if (typeof this._peer?._pc?.getStats === "function") {
2952
3159
  this._peer._pc.getStats().then((stats) => {
2953
- import_log11.log.warn("report after webrtc error", {
3160
+ import_log12.log.warn("report after webrtc error", {
2954
3161
  config: this.params.webrtcConfig,
2955
3162
  stats
2956
3163
  }, {
2957
- F: __dxlog_file10,
2958
- L: 72,
3164
+ F: __dxlog_file11,
3165
+ L: 109,
2959
3166
  S: this,
2960
3167
  C: (f, a) => f(...a)
2961
3168
  });
2962
3169
  });
2963
3170
  }
2964
- } catch {
3171
+ } catch (err2) {
3172
+ import_log12.log.catch(err2, void 0, {
3173
+ F: __dxlog_file11,
3174
+ L: 116,
3175
+ S: this,
3176
+ C: (f, a) => f(...a)
3177
+ });
2965
3178
  }
2966
3179
  await this.destroy();
2967
3180
  });
2968
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
3181
+ import_log12.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
2969
3182
  id: this._instanceId
2970
3183
  }), {
2971
- F: __dxlog_file10,
2972
- L: 82,
3184
+ F: __dxlog_file11,
3185
+ L: 121,
2973
3186
  S: this,
2974
3187
  C: (f, a) => f(...a)
2975
3188
  });
2976
3189
  }
2977
3190
  async destroy() {
2978
- (0, import_log11.log)("closing...", void 0, {
2979
- F: __dxlog_file10,
2980
- L: 86,
3191
+ (0, import_log12.log)("closing...", void 0, {
3192
+ F: __dxlog_file11,
3193
+ L: 125,
2981
3194
  S: this,
2982
3195
  C: (f, a) => f(...a)
2983
3196
  });
@@ -2985,9 +3198,9 @@ var SimplePeerTransport = class {
2985
3198
  await this._disconnectStreams();
2986
3199
  this._peer.destroy();
2987
3200
  this.closed.emit();
2988
- (0, import_log11.log)("closed", void 0, {
2989
- F: __dxlog_file10,
2990
- L: 91,
3201
+ (0, import_log12.log)("closed", void 0, {
3202
+ F: __dxlog_file11,
3203
+ L: 130,
2991
3204
  S: this,
2992
3205
  C: (f, a) => f(...a)
2993
3206
  });
@@ -3014,16 +3227,15 @@ var createSimplePeerTransportFactory = (webrtcConfig) => ({
3014
3227
  var import_node_stream2 = require("node:stream");
3015
3228
  var import_codec_protobuf = require("@dxos/codec-protobuf");
3016
3229
  var import_invariant11 = require("@dxos/invariant");
3017
- var import_keys11 = require("@dxos/keys");
3018
- var import_log12 = require("@dxos/log");
3230
+ var import_keys12 = require("@dxos/keys");
3231
+ var import_log13 = require("@dxos/log");
3019
3232
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3020
- var import_util8 = require("@dxos/util");
3021
- var __dxlog_file11 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3233
+ var import_util9 = require("@dxos/util");
3234
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3022
3235
  var SimplePeerTransportService = class {
3023
- // prettier-ignore
3024
3236
  constructor(_webrtcConfig) {
3025
3237
  this._webrtcConfig = _webrtcConfig;
3026
- this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
3238
+ this.transports = new import_util9.ComplexMap(import_keys12.PublicKey.hash);
3027
3239
  }
3028
3240
  open(request) {
3029
3241
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
@@ -3090,7 +3302,8 @@ var SimplePeerTransportService = class {
3090
3302
  const transportState = {
3091
3303
  transport,
3092
3304
  stream: duplex,
3093
- writeCallbacks: []
3305
+ writeCallbacks: [],
3306
+ state: "OPEN"
3094
3307
  };
3095
3308
  ready();
3096
3309
  this.transports.set(request.proxyId, transportState);
@@ -3099,8 +3312,8 @@ var SimplePeerTransportService = class {
3099
3312
  }
3100
3313
  async sendSignal({ proxyId, signal }) {
3101
3314
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3102
- F: __dxlog_file11,
3103
- L: 113,
3315
+ F: __dxlog_file12,
3316
+ L: 112,
3104
3317
  S: this,
3105
3318
  A: [
3106
3319
  "this.transports.has(proxyId)",
@@ -3110,9 +3323,17 @@ var SimplePeerTransportService = class {
3110
3323
  await this.transports.get(proxyId).transport.signal(signal);
3111
3324
  }
3112
3325
  async sendData({ proxyId, payload }) {
3326
+ if (this.transports.get(proxyId)?.state !== "OPEN") {
3327
+ import_log13.log.debug("transport is closed", void 0, {
3328
+ F: __dxlog_file12,
3329
+ L: 118,
3330
+ S: this,
3331
+ C: (f, a) => f(...a)
3332
+ });
3333
+ }
3113
3334
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3114
- F: __dxlog_file11,
3115
- L: 118,
3335
+ F: __dxlog_file12,
3336
+ L: 120,
3116
3337
  S: this,
3117
3338
  A: [
3118
3339
  "this.transports.has(proxyId)",
@@ -3130,10 +3351,12 @@ var SimplePeerTransportService = class {
3130
3351
  async close({ proxyId }) {
3131
3352
  await this.transports.get(proxyId)?.transport.destroy();
3132
3353
  await this.transports.get(proxyId)?.stream.end();
3133
- this.transports.delete(proxyId);
3134
- (0, import_log12.log)("Closed.", void 0, {
3135
- F: __dxlog_file11,
3136
- L: 132,
3354
+ if (this.transports.get(proxyId)) {
3355
+ this.transports.get(proxyId).state = "CLOSED";
3356
+ }
3357
+ (0, import_log13.log)("Closed.", void 0, {
3358
+ F: __dxlog_file12,
3359
+ L: 136,
3137
3360
  S: this,
3138
3361
  C: (f, a) => f(...a)
3139
3362
  });
@@ -3142,23 +3365,23 @@ var SimplePeerTransportService = class {
3142
3365
 
3143
3366
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts
3144
3367
  var import_node_stream3 = require("node:stream");
3145
- var import_async10 = require("@dxos/async");
3368
+ var import_async11 = require("@dxos/async");
3146
3369
  var import_context6 = require("@dxos/context");
3147
3370
  var import_debug6 = require("@dxos/debug");
3148
3371
  var import_invariant12 = require("@dxos/invariant");
3149
- var import_keys12 = require("@dxos/keys");
3150
- var import_log13 = require("@dxos/log");
3372
+ var import_keys13 = require("@dxos/keys");
3373
+ var import_log14 = require("@dxos/log");
3374
+ var import_protocols8 = require("@dxos/protocols");
3151
3375
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3152
- var import_util9 = require("@dxos/util");
3153
- var __dxlog_file12 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3376
+ var import_util10 = require("@dxos/util");
3377
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3154
3378
  var SimplePeerTransportProxy = class {
3155
- // prettier-ignore
3156
3379
  constructor(_params) {
3157
3380
  this._params = _params;
3158
- this._proxyId = import_keys12.PublicKey.random();
3381
+ this._proxyId = import_keys13.PublicKey.random();
3159
3382
  this._ctx = new import_context6.Context();
3160
- this.closed = new import_async10.Event();
3161
- this.connected = new import_async10.Event();
3383
+ this.closed = new import_async11.Event();
3384
+ this.connected = new import_async11.Event();
3162
3385
  this.errors = new import_debug6.ErrorStream();
3163
3386
  this._closed = false;
3164
3387
  this._serviceStream = this._params.bridgeService.open({
@@ -3167,9 +3390,9 @@ var SimplePeerTransportProxy = class {
3167
3390
  });
3168
3391
  this._serviceStream.waitUntilReady().then(() => {
3169
3392
  this._serviceStream.subscribe(async (event) => {
3170
- (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3171
- F: __dxlog_file12,
3172
- L: 51,
3393
+ (0, import_log14.log)("SimplePeerTransportProxy: event", event, {
3394
+ F: __dxlog_file13,
3395
+ L: 55,
3173
3396
  S: this,
3174
3397
  C: (f, a) => f(...a)
3175
3398
  });
@@ -3187,25 +3410,25 @@ var SimplePeerTransportProxy = class {
3187
3410
  proxyId: this._proxyId,
3188
3411
  payload: chunk
3189
3412
  }).then(() => callback(), (err) => {
3190
- import_log13.log.catch(err, void 0, {
3191
- F: __dxlog_file12,
3192
- L: 69,
3413
+ import_log14.log.catch(err, void 0, {
3414
+ F: __dxlog_file13,
3415
+ L: 76,
3193
3416
  S: this,
3194
3417
  C: (f, a) => f(...a)
3195
3418
  });
3196
3419
  });
3197
3420
  }
3198
3421
  }));
3199
- }, (error) => import_log13.log.catch(error, void 0, {
3200
- F: __dxlog_file12,
3201
- L: 75,
3422
+ }, (error) => import_log14.log.catch(error, void 0, {
3423
+ F: __dxlog_file13,
3424
+ L: 83,
3202
3425
  S: this,
3203
3426
  C: (f, a) => f(...a)
3204
3427
  }));
3205
3428
  }
3206
3429
  async _handleConnection(connectionEvent) {
3207
3430
  if (connectionEvent.error) {
3208
- this.errors.raise(new Error(connectionEvent.error));
3431
+ this.errors.raise(decodeError(connectionEvent.error));
3209
3432
  }
3210
3433
  switch (connectionEvent.state) {
3211
3434
  case import_bridge2.ConnectionState.CONNECTED: {
@@ -3219,7 +3442,7 @@ var SimplePeerTransportProxy = class {
3219
3442
  }
3220
3443
  }
3221
3444
  _handleData(dataEvent) {
3222
- this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
3445
+ this._params.stream.write((0, import_util10.arrayToBuffer)(dataEvent.payload));
3223
3446
  }
3224
3447
  async _handleSignal(signalEvent) {
3225
3448
  await this._params.sendSignal(signalEvent.payload);
@@ -3228,7 +3451,7 @@ var SimplePeerTransportProxy = class {
3228
3451
  this._params.bridgeService.sendSignal({
3229
3452
  proxyId: this._proxyId,
3230
3453
  signal
3231
- }).catch((err) => this.errors.raise(err));
3454
+ }).catch((err) => this.errors.raise(decodeError(err)));
3232
3455
  }
3233
3456
  // TODO(burdon): Move open from constructor.
3234
3457
  async destroy() {
@@ -3242,9 +3465,9 @@ var SimplePeerTransportProxy = class {
3242
3465
  proxyId: this._proxyId
3243
3466
  });
3244
3467
  } catch (err) {
3245
- import_log13.log.catch(err, void 0, {
3246
- F: __dxlog_file12,
3247
- L: 126,
3468
+ import_log14.log.catch(err, void 0, {
3469
+ F: __dxlog_file13,
3470
+ L: 134,
3248
3471
  S: this,
3249
3472
  C: (f, a) => f(...a)
3250
3473
  });
@@ -3279,8 +3502,8 @@ var SimplePeerTransportProxyFactory = class {
3279
3502
  }
3280
3503
  createTransport(options) {
3281
3504
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3282
- F: __dxlog_file12,
3283
- L: 163,
3505
+ F: __dxlog_file13,
3506
+ L: 171,
3284
3507
  S: this,
3285
3508
  A: [
3286
3509
  "this._bridgeService",
@@ -3296,15 +3519,31 @@ var SimplePeerTransportProxyFactory = class {
3296
3519
  return transport;
3297
3520
  }
3298
3521
  };
3522
+ var decodeError = (err) => {
3523
+ const message = typeof err === "string" ? err : err.message;
3524
+ if (message.includes("CONNECTION_RESET")) {
3525
+ return new import_protocols8.ConnectionResetError(message);
3526
+ } else if (message.includes("TIMEOUT")) {
3527
+ return new import_protocols8.TimeoutError(message);
3528
+ } else if (message.includes("PROTOCOL_ERROR")) {
3529
+ return new import_protocols8.ProtocolError(message);
3530
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3531
+ return new import_protocols8.ConnectivityError(message);
3532
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3533
+ return new import_protocols8.UnknownProtocolError(message);
3534
+ } else {
3535
+ return typeof err === "string" ? new Error(err) : err;
3536
+ }
3537
+ };
3299
3538
 
3300
3539
  // packages/core/mesh/network-manager/src/transport/index.ts
3301
3540
  init_datachannel();
3302
3541
 
3303
3542
  // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
3304
3543
  var import_stream = require("stream");
3305
- var import_async11 = require("@dxos/async");
3544
+ var import_async12 = require("@dxos/async");
3306
3545
  var import_debug7 = require("@dxos/debug");
3307
- var import_log14 = require("@dxos/log");
3546
+ var import_log15 = require("@dxos/log");
3308
3547
  function _ts_decorate5(decorators, target, key, desc) {
3309
3548
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3310
3549
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3315,7 +3554,7 @@ function _ts_decorate5(decorators, target, key, desc) {
3315
3554
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3316
3555
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3317
3556
  }
3318
- var __dxlog_file13 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3557
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3319
3558
  var DATACHANNEL_LABEL = "dxos.mesh.transport";
3320
3559
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3321
3560
  var MAX_MESSAGE_SIZE = 64 * 1024;
@@ -3323,11 +3562,11 @@ var LibDataChannelTransport = class {
3323
3562
  constructor(params) {
3324
3563
  this.params = params;
3325
3564
  this._closed = false;
3326
- this.closed = new import_async11.Event();
3565
+ this.closed = new import_async12.Event();
3327
3566
  this._connected = false;
3328
- this.connected = new import_async11.Event();
3567
+ this.connected = new import_async12.Event();
3329
3568
  this.errors = new import_debug7.ErrorStream();
3330
- this._readyForCandidates = new import_async11.Trigger();
3569
+ this._readyForCandidates = new import_async12.Trigger();
3331
3570
  this._writeCallback = null;
3332
3571
  this._peer = (async () => {
3333
3572
  const { RTCPeerConnection: PeerConnection2 } = await Promise.resolve().then(() => (init_datachannel(), datachannel_exports));
@@ -3336,32 +3575,32 @@ var LibDataChannelTransport = class {
3336
3575
  }
3337
3576
  const peer = new PeerConnection2(params.webrtcConfig);
3338
3577
  peer.onicecandidateerror = (event) => {
3339
- import_log14.log.error("peer.onicecandidateerror", {
3578
+ import_log15.log.error("peer.onicecandidateerror", {
3340
3579
  event
3341
3580
  }, {
3342
- F: __dxlog_file13,
3581
+ F: __dxlog_file14,
3343
3582
  L: 53,
3344
3583
  S: this,
3345
3584
  C: (f, a) => f(...a)
3346
3585
  });
3347
3586
  };
3348
3587
  peer.onconnectionstatechange = (event) => {
3349
- import_log14.log.debug("peer.onconnectionstatechange", {
3588
+ import_log15.log.debug("peer.onconnectionstatechange", {
3350
3589
  event,
3351
3590
  peerConnectionState: peer.connectionState,
3352
3591
  transportConnectionState: this._connected
3353
3592
  }, {
3354
- F: __dxlog_file13,
3593
+ F: __dxlog_file14,
3355
3594
  L: 57,
3356
3595
  S: this,
3357
3596
  C: (f, a) => f(...a)
3358
3597
  });
3359
3598
  };
3360
3599
  peer.onicecandidate = async (event) => {
3361
- import_log14.log.debug("peer.onicecandidate", {
3600
+ import_log15.log.debug("peer.onicecandidate", {
3362
3601
  event
3363
3602
  }, {
3364
- F: __dxlog_file13,
3603
+ F: __dxlog_file14,
3365
3604
  L: 66,
3366
3605
  S: this,
3367
3606
  C: (f, a) => f(...a)
@@ -3382,10 +3621,10 @@ var LibDataChannelTransport = class {
3382
3621
  }
3383
3622
  });
3384
3623
  } catch (err) {
3385
- import_log14.log.warn("signaling errror", {
3624
+ import_log15.log.warn("signaling errror", {
3386
3625
  err
3387
3626
  }, {
3388
- F: __dxlog_file13,
3627
+ F: __dxlog_file14,
3389
3628
  L: 83,
3390
3629
  S: this,
3391
3630
  C: (f, a) => f(...a)
@@ -3396,20 +3635,20 @@ var LibDataChannelTransport = class {
3396
3635
  if (params.initiator) {
3397
3636
  peer.createOffer().then(async (offer) => {
3398
3637
  if (peer.connectionState !== "connecting") {
3399
- import_log14.log.error("i am initiator but peer not in state connecting", {
3638
+ import_log15.log.error("i am initiator but peer not in state connecting", {
3400
3639
  peer
3401
3640
  }, {
3402
- F: __dxlog_file13,
3641
+ F: __dxlog_file14,
3403
3642
  L: 92,
3404
3643
  S: this,
3405
3644
  C: (f, a) => f(...a)
3406
3645
  });
3407
3646
  this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3408
3647
  }
3409
- import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3648
+ import_log15.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3410
3649
  offer
3411
3650
  }, {
3412
- F: __dxlog_file13,
3651
+ F: __dxlog_file14,
3413
3652
  L: 95,
3414
3653
  S: this,
3415
3654
  C: (f, a) => f(...a)
@@ -3428,8 +3667,8 @@ var LibDataChannelTransport = class {
3428
3667
  this.errors.raise(err);
3429
3668
  });
3430
3669
  this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3431
- import_log14.log.debug("created data channel", void 0, {
3432
- F: __dxlog_file13,
3670
+ import_log15.log.debug("created data channel", void 0, {
3671
+ F: __dxlog_file14,
3433
3672
  L: 104,
3434
3673
  S: this,
3435
3674
  C: (f, a) => f(...a)
@@ -3439,10 +3678,10 @@ var LibDataChannelTransport = class {
3439
3678
  };
3440
3679
  } else {
3441
3680
  peer.ondatachannel = (event) => {
3442
- import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3681
+ import_log15.log.debug("peer.ondatachannel (non-initiator)", {
3443
3682
  event
3444
3683
  }, {
3445
- F: __dxlog_file13,
3684
+ F: __dxlog_file14,
3446
3685
  L: 110,
3447
3686
  S: this,
3448
3687
  C: (f, a) => f(...a)
@@ -3459,8 +3698,8 @@ var LibDataChannelTransport = class {
3459
3698
  handleChannel(dataChannel) {
3460
3699
  this._channel = dataChannel;
3461
3700
  this._channel.onopen = () => {
3462
- import_log14.log.debug("dataChannel.onopen", void 0, {
3463
- F: __dxlog_file13,
3701
+ import_log15.log.debug("dataChannel.onopen", void 0, {
3702
+ F: __dxlog_file14,
3464
3703
  L: 126,
3465
3704
  S: this,
3466
3705
  C: (f, a) => f(...a)
@@ -3475,8 +3714,8 @@ var LibDataChannelTransport = class {
3475
3714
  dataChannel.send(chunk);
3476
3715
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3477
3716
  if (this._writeCallback !== null) {
3478
- import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
3479
- F: __dxlog_file13,
3717
+ import_log15.log.error("consumer trying to write before we're ready for more data", void 0, {
3718
+ F: __dxlog_file14,
3480
3719
  L: 139,
3481
3720
  S: this,
3482
3721
  C: (f, a) => f(...a)
@@ -3498,10 +3737,10 @@ var LibDataChannelTransport = class {
3498
3737
  await this._close();
3499
3738
  };
3500
3739
  this._channel.onclose = async (err) => {
3501
- import_log14.log.info("channel onclose", {
3740
+ import_log15.log.info("channel onclose", {
3502
3741
  err
3503
3742
  }, {
3504
- F: __dxlog_file13,
3743
+ F: __dxlog_file14,
3505
3744
  L: 159,
3506
3745
  S: this,
3507
3746
  C: (f, a) => f(...a)
@@ -3535,10 +3774,10 @@ var LibDataChannelTransport = class {
3535
3774
  switch (data.type) {
3536
3775
  case "offer": {
3537
3776
  if ((await this._peer).connectionState !== "new") {
3538
- import_log14.log.error("received offer but peer not in state new", {
3777
+ import_log15.log.error("received offer but peer not in state new", {
3539
3778
  peer
3540
3779
  }, {
3541
- F: __dxlog_file13,
3780
+ F: __dxlog_file14,
3542
3781
  L: 194,
3543
3782
  S: this,
3544
3783
  C: (f, a) => f(...a)
@@ -3563,10 +3802,10 @@ var LibDataChannelTransport = class {
3563
3802
  });
3564
3803
  this._readyForCandidates.wake();
3565
3804
  } catch (err) {
3566
- import_log14.log.error("can't handle offer from signalling server", {
3805
+ import_log15.log.error("can't handle offer from signalling server", {
3567
3806
  err
3568
3807
  }, {
3569
- F: __dxlog_file13,
3808
+ F: __dxlog_file14,
3570
3809
  L: 205,
3571
3810
  S: this,
3572
3811
  C: (f, a) => f(...a)
@@ -3583,10 +3822,10 @@ var LibDataChannelTransport = class {
3583
3822
  });
3584
3823
  this._readyForCandidates.wake();
3585
3824
  } catch (err) {
3586
- import_log14.log.error("can't handle answer from signalling server", {
3825
+ import_log15.log.error("can't handle answer from signalling server", {
3587
3826
  err
3588
3827
  }, {
3589
- F: __dxlog_file13,
3828
+ F: __dxlog_file14,
3590
3829
  L: 216,
3591
3830
  S: this,
3592
3831
  C: (f, a) => f(...a)
@@ -3601,11 +3840,11 @@ var LibDataChannelTransport = class {
3601
3840
  });
3602
3841
  break;
3603
3842
  default:
3604
- import_log14.log.error("unhandled signal type", {
3843
+ import_log15.log.error("unhandled signal type", {
3605
3844
  type: data.type,
3606
3845
  signal
3607
3846
  }, {
3608
- F: __dxlog_file13,
3847
+ F: __dxlog_file14,
3609
3848
  L: 227,
3610
3849
  S: this,
3611
3850
  C: (f, a) => f(...a)
@@ -3613,8 +3852,8 @@ var LibDataChannelTransport = class {
3613
3852
  this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3614
3853
  }
3615
3854
  }).catch((err) => {
3616
- import_log14.log.catch(err, void 0, {
3617
- F: __dxlog_file13,
3855
+ import_log15.log.catch(err, void 0, {
3856
+ F: __dxlog_file14,
3618
3857
  L: 232,
3619
3858
  S: this,
3620
3859
  C: (f, a) => f(...a)
@@ -3630,7 +3869,7 @@ var LibDataChannelTransport = class {
3630
3869
  }
3631
3870
  };
3632
3871
  _ts_decorate5([
3633
- import_async11.synchronized
3872
+ import_async12.synchronized
3634
3873
  ], LibDataChannelTransport.prototype, "_close", null);
3635
3874
  var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3636
3875
  createTransport: (params) => new LibDataChannelTransport({
@@ -3639,120 +3878,138 @@ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3639
3878
  })
3640
3879
  });
3641
3880
 
3642
- // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3643
- var import_async12 = require("@dxos/async");
3644
- var import_keys13 = require("@dxos/keys");
3645
- var import_log15 = require("@dxos/log");
3646
- var import_teleport2 = require("@dxos/teleport");
3647
- var import_util10 = require("@dxos/util");
3648
-
3649
- // packages/core/mesh/network-manager/src/wire-protocol.ts
3650
- var import_teleport = require("@dxos/teleport");
3651
- var createTeleportProtocolFactory = (onConnection) => {
3652
- return (params) => {
3653
- const teleport = new import_teleport.Teleport(params);
3654
- return {
3655
- stream: teleport.stream,
3656
- open: async () => {
3657
- await teleport.open();
3658
- await onConnection(teleport);
3659
- },
3660
- close: async () => {
3661
- await teleport.close();
3662
- },
3663
- abort: async () => {
3664
- await teleport.abort();
3665
- }
3666
- };
3667
- };
3881
+ // packages/core/mesh/network-manager/src/transport/tcp-transport.ts
3882
+ var import_node_net = require("node:net");
3883
+ var import_async13 = require("@dxos/async");
3884
+ var import_debug8 = require("@dxos/debug");
3885
+ var import_log16 = require("@dxos/log");
3886
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
3887
+ var TcpTransportFactory = {
3888
+ createTransport: (params) => new TcpTransport(params)
3668
3889
  };
3669
-
3670
- // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3671
- var __dxlog_file14 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
3672
- var TestWireProtocol = class {
3673
- constructor(peerId, _extensionFactory = () => []) {
3674
- this.peerId = peerId;
3675
- this._extensionFactory = _extensionFactory;
3676
- this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
3677
- this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
3678
- this.connected = new import_async12.Event();
3679
- this.disconnected = new import_async12.Event();
3680
- this.otherConnections = new import_util10.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
3681
- this.factory = createTeleportProtocolFactory(async (teleport) => {
3682
- (0, import_log15.log)("create", {
3683
- remotePeerId: teleport.remotePeerId
3684
- }, {
3685
- F: __dxlog_file14,
3686
- L: 38,
3687
- S: this,
3688
- C: (f, a) => f(...a)
3689
- });
3690
- const extension = new import_teleport2.TestExtension({
3691
- onClose: async () => {
3692
- this.connections.delete(teleport.remotePeerId);
3693
- this.disconnected.emit(teleport.remotePeerId);
3694
- }
3695
- });
3696
- this.connections.set(teleport.remotePeerId, extension);
3697
- teleport.addExtension("test", extension);
3698
- this.connected.emit(teleport.remotePeerId);
3699
- const streamExtension = new import_teleport2.TestExtensionWithStreams({
3700
- onClose: async () => {
3701
- this.streamConnections.delete(teleport.remotePeerId);
3702
- }
3890
+ var TcpTransport = class {
3891
+ constructor(options) {
3892
+ this.options = options;
3893
+ this._server = void 0;
3894
+ this._socket = void 0;
3895
+ this.closed = new import_async13.Event();
3896
+ this.connected = new import_async13.Event();
3897
+ this.errors = new import_debug8.ErrorStream();
3898
+ this._destroyed = false;
3899
+ this._connected = false;
3900
+ (0, import_log16.log)("creating", void 0, {
3901
+ F: __dxlog_file15,
3902
+ L: 34,
3903
+ S: this,
3904
+ C: (f, a) => f(...a)
3905
+ });
3906
+ if (this.options.initiator) {
3907
+ setTimeout(async () => {
3908
+ const { Server } = await import("node:net");
3909
+ this._server = new Server((socket) => {
3910
+ (0, import_log16.log)("new connection", void 0, {
3911
+ F: __dxlog_file15,
3912
+ L: 42,
3913
+ S: this,
3914
+ C: (f, a) => f(...a)
3915
+ });
3916
+ if (this._connected) {
3917
+ socket.destroy();
3918
+ }
3919
+ this._handleSocket(socket);
3920
+ });
3921
+ this._server.on("listening", () => {
3922
+ const { port: port2 } = this._server.address();
3923
+ (0, import_log16.log)("listening", {
3924
+ port: port2
3925
+ }, {
3926
+ F: __dxlog_file15,
3927
+ L: 51,
3928
+ S: this,
3929
+ C: (f, a) => f(...a)
3930
+ });
3931
+ void this.options.sendSignal({
3932
+ payload: {
3933
+ port: port2
3934
+ }
3935
+ }).catch((err) => {
3936
+ if (!this._destroyed) {
3937
+ this.errors.raise(err);
3938
+ }
3939
+ });
3940
+ });
3941
+ this._server.on("error", (err) => {
3942
+ this.errors.raise(err);
3943
+ });
3944
+ this._server.listen(0);
3703
3945
  });
3704
- this.streamConnections.set(teleport.remotePeerId, streamExtension);
3705
- teleport.addExtension("test-stream", streamExtension);
3706
- for (const { name, extension: extension2 } of this._extensionFactory()) {
3707
- this.otherConnections.set({
3708
- remotePeerId: teleport.remotePeerId,
3709
- extension: name
3710
- }, extension2);
3711
- teleport.addExtension(name, extension2);
3712
- }
3946
+ }
3947
+ }
3948
+ async destroy() {
3949
+ (0, import_log16.log)("closing", void 0, {
3950
+ F: __dxlog_file15,
3951
+ L: 72,
3952
+ S: this,
3953
+ C: (f, a) => f(...a)
3713
3954
  });
3955
+ this._socket?.destroy();
3956
+ this._server?.close();
3957
+ this._destroyed = true;
3714
3958
  }
3715
- async waitForConnection(peerId) {
3716
- if (this.connections.has(peerId)) {
3717
- return this.connections.get(peerId);
3718
- }
3719
- (0, import_log15.log)("waitForConnection", {
3720
- peerId
3959
+ signal({ payload }) {
3960
+ (0, import_log16.log)("received signal", {
3961
+ payload
3721
3962
  }, {
3722
- F: __dxlog_file14,
3723
- L: 67,
3963
+ F: __dxlog_file15,
3964
+ L: 80,
3724
3965
  S: this,
3725
3966
  C: (f, a) => f(...a)
3726
3967
  });
3727
- await (0, import_async12.asyncTimeout)(
3728
- this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
3729
- // TODO(nf): Make this configurable.
3730
- 1e4
3731
- );
3732
- return this.connections.get(peerId);
3733
- }
3734
- async testConnection(peerId, message) {
3735
- const connection = await this.waitForConnection(peerId);
3736
- await connection.test(message);
3737
- }
3738
- async openStream(peerId, streamTag, streamLoadInterval, streamLoadChunkSize) {
3739
- if (!this.streamConnections.has(peerId)) {
3740
- throw new Error("Connection does not exist.");
3968
+ if (this.options.initiator || this._connected) {
3969
+ return;
3741
3970
  }
3742
- const connection = this.streamConnections.get(peerId);
3743
- return connection.addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag);
3971
+ const socket = new import_node_net.Socket();
3972
+ this._handleSocket(socket);
3973
+ socket.connect({
3974
+ port: payload.port,
3975
+ host: "localhost"
3976
+ });
3744
3977
  }
3745
- async closeStream(peerId, streamTag) {
3746
- if (!this.streamConnections.has(peerId)) {
3747
- throw new Error("Connection does not exist.");
3748
- }
3749
- const connection = this.streamConnections.get(peerId);
3750
- return connection.closeStream(streamTag);
3978
+ _handleSocket(socket) {
3979
+ (0, import_log16.log)("handling socket", {
3980
+ remotePort: socket.remotePort,
3981
+ localPort: socket.localPort
3982
+ }, {
3983
+ F: __dxlog_file15,
3984
+ L: 91,
3985
+ S: this,
3986
+ C: (f, a) => f(...a)
3987
+ });
3988
+ this._socket = socket;
3989
+ this.connected.emit();
3990
+ this.options.stream.pipe(this._socket).pipe(this.options.stream);
3991
+ this._socket.on("connect", () => {
3992
+ (0, import_log16.log)("connected to", {
3993
+ port: this._socket?.remotePort
3994
+ }, {
3995
+ F: __dxlog_file15,
3996
+ L: 97,
3997
+ S: this,
3998
+ C: (f, a) => f(...a)
3999
+ });
4000
+ this._connected = true;
4001
+ });
4002
+ this._socket.on("error", (err) => {
4003
+ this.errors.raise(err);
4004
+ });
4005
+ this._socket.on("close", () => {
4006
+ this.closed.emit();
4007
+ });
3751
4008
  }
3752
4009
  };
3753
4010
 
3754
4011
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
3755
- var __dxlog_file15 = "/mnt/ramdisk/work/packages/core/mesh/network-manager/src/testing/test-builder.ts";
4012
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-builder.ts";
3756
4013
  var port = process.env.SIGNAL_PORT ?? 4e3;
3757
4014
  var TEST_SIGNAL_HOSTS = [
3758
4015
  {
@@ -3794,15 +4051,18 @@ var TestPeer = class {
3794
4051
  createNetworkManager(transport) {
3795
4052
  let transportFactory;
3796
4053
  if (this.testBuilder.options.signalHosts) {
3797
- import_log16.log.info(`using ${transport} transport with signal server.`, void 0, {
3798
- F: __dxlog_file15,
3799
- L: 104,
4054
+ import_log17.log.info(`using ${transport} transport with signal server.`, void 0, {
4055
+ F: __dxlog_file16,
4056
+ L: 105,
3800
4057
  S: this,
3801
4058
  C: (f, a) => f(...a)
3802
4059
  });
3803
4060
  switch (transport) {
3804
4061
  case TransportKind.MEMORY:
3805
4062
  throw new Error("Memory transport not supported with signal server.");
4063
+ case TransportKind.TCP:
4064
+ transportFactory = TcpTransportFactory;
4065
+ break;
3806
4066
  case TransportKind.SIMPLE_PEER:
3807
4067
  transportFactory = createSimplePeerTransportFactory();
3808
4068
  break;
@@ -3815,7 +4075,7 @@ var TestPeer = class {
3815
4075
  this._proxy = (0, import_rpc.createProtoRpcPeer)({
3816
4076
  port: proxyPort,
3817
4077
  requested: {
3818
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
4078
+ BridgeService: import_protocols9.schema.getService("dxos.mesh.bridge.BridgeService")
3819
4079
  },
3820
4080
  noHandshake: true,
3821
4081
  encodingOptions: {
@@ -3825,7 +4085,7 @@ var TestPeer = class {
3825
4085
  this._service = (0, import_rpc.createProtoRpcPeer)({
3826
4086
  port: servicePort,
3827
4087
  exposed: {
3828
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
4088
+ BridgeService: import_protocols9.schema.getService("dxos.mesh.bridge.BridgeService")
3829
4089
  },
3830
4090
  handlers: {
3831
4091
  BridgeService: new SimplePeerTransportService()
@@ -3842,17 +4102,17 @@ var TestPeer = class {
3842
4102
  throw new Error(`Unsupported transport: ${transport}`);
3843
4103
  }
3844
4104
  } else {
3845
- if (transport !== TransportKind.MEMORY) {
3846
- import_log16.log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`, void 0, {
3847
- F: __dxlog_file15,
3848
- L: 150,
4105
+ if (transport !== TransportKind.MEMORY && transport !== TransportKind.TCP) {
4106
+ import_log17.log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`, void 0, {
4107
+ F: __dxlog_file16,
4108
+ L: 154,
3849
4109
  S: this,
3850
4110
  C: (f, a) => f(...a)
3851
4111
  });
3852
4112
  }
3853
- import_log16.log.info(`using ${transport} transport without signal server.`, void 0, {
3854
- F: __dxlog_file15,
3855
- L: 152,
4113
+ import_log17.log.info(`using ${transport} transport without signal server.`, void 0, {
4114
+ F: __dxlog_file16,
4115
+ L: 156,
3856
4116
  S: this,
3857
4117
  C: (f, a) => f(...a)
3858
4118
  });