@dxos/network-manager 0.1.0

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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/dist/src/connection-log.d.ts +23 -0
  3. package/dist/src/connection-log.d.ts.map +1 -0
  4. package/dist/src/connection-log.js +98 -0
  5. package/dist/src/connection-log.js.map +1 -0
  6. package/dist/src/index.d.ts +9 -0
  7. package/dist/src/index.d.ts.map +1 -0
  8. package/dist/src/index.js +28 -0
  9. package/dist/src/index.js.map +1 -0
  10. package/dist/src/network-manager.d.ts +80 -0
  11. package/dist/src/network-manager.d.ts.map +1 -0
  12. package/dist/src/network-manager.js +123 -0
  13. package/dist/src/network-manager.js.map +1 -0
  14. package/dist/src/network-manager.test.d.ts +2 -0
  15. package/dist/src/network-manager.test.d.ts.map +1 -0
  16. package/dist/src/network-manager.test.js +621 -0
  17. package/dist/src/network-manager.test.js.map +1 -0
  18. package/dist/src/presence.test.d.ts +2 -0
  19. package/dist/src/presence.test.d.ts.map +1 -0
  20. package/dist/src/presence.test.js +60 -0
  21. package/dist/src/presence.test.js.map +1 -0
  22. package/dist/src/protocol-factory.d.ts +30 -0
  23. package/dist/src/protocol-factory.d.ts.map +1 -0
  24. package/dist/src/protocol-factory.js +64 -0
  25. package/dist/src/protocol-factory.js.map +1 -0
  26. package/dist/src/signal/index.d.ts +4 -0
  27. package/dist/src/signal/index.d.ts.map +1 -0
  28. package/dist/src/signal/index.js +23 -0
  29. package/dist/src/signal/index.js.map +1 -0
  30. package/dist/src/signal/integration.test.d.ts +2 -0
  31. package/dist/src/signal/integration.test.d.ts.map +1 -0
  32. package/dist/src/signal/integration.test.js +105 -0
  33. package/dist/src/signal/integration.test.js.map +1 -0
  34. package/dist/src/signal/message-router.d.ts +39 -0
  35. package/dist/src/signal/message-router.d.ts.map +1 -0
  36. package/dist/src/signal/message-router.js +135 -0
  37. package/dist/src/signal/message-router.js.map +1 -0
  38. package/dist/src/signal/message-router.test.d.ts +2 -0
  39. package/dist/src/signal/message-router.test.d.ts.map +1 -0
  40. package/dist/src/signal/message-router.test.js +211 -0
  41. package/dist/src/signal/message-router.test.js.map +1 -0
  42. package/dist/src/signal/signal-connection.d.ts +21 -0
  43. package/dist/src/signal/signal-connection.d.ts.map +1 -0
  44. package/dist/src/signal/signal-connection.js +6 -0
  45. package/dist/src/signal/signal-connection.js.map +1 -0
  46. package/dist/src/signal/signal-messaging.d.ts +34 -0
  47. package/dist/src/signal/signal-messaging.d.ts.map +1 -0
  48. package/dist/src/signal/signal-messaging.js +6 -0
  49. package/dist/src/signal/signal-messaging.js.map +1 -0
  50. package/dist/src/swarm/connection.d.ts +69 -0
  51. package/dist/src/swarm/connection.d.ts.map +1 -0
  52. package/dist/src/swarm/connection.js +187 -0
  53. package/dist/src/swarm/connection.js.map +1 -0
  54. package/dist/src/swarm/index.d.ts +4 -0
  55. package/dist/src/swarm/index.d.ts.map +1 -0
  56. package/dist/src/swarm/index.js +23 -0
  57. package/dist/src/swarm/index.js.map +1 -0
  58. package/dist/src/swarm/swarm-mapper.d.ts +30 -0
  59. package/dist/src/swarm/swarm-mapper.d.ts.map +1 -0
  60. package/dist/src/swarm/swarm-mapper.js +90 -0
  61. package/dist/src/swarm/swarm-mapper.js.map +1 -0
  62. package/dist/src/swarm/swarm.d.ts +64 -0
  63. package/dist/src/swarm/swarm.d.ts.map +1 -0
  64. package/dist/src/swarm/swarm.js +276 -0
  65. package/dist/src/swarm/swarm.js.map +1 -0
  66. package/dist/src/swarm/swarm.test.d.ts +2 -0
  67. package/dist/src/swarm/swarm.test.d.ts.map +1 -0
  68. package/dist/src/swarm/swarm.test.js +145 -0
  69. package/dist/src/swarm/swarm.test.js.map +1 -0
  70. package/dist/src/testing/index.d.ts +2 -0
  71. package/dist/src/testing/index.d.ts.map +1 -0
  72. package/dist/src/testing/index.js +21 -0
  73. package/dist/src/testing/index.js.map +1 -0
  74. package/dist/src/testing/test-protocol.d.ts +59 -0
  75. package/dist/src/testing/test-protocol.d.ts.map +1 -0
  76. package/dist/src/testing/test-protocol.js +140 -0
  77. package/dist/src/testing/test-protocol.js.map +1 -0
  78. package/dist/src/topology/fully-connected-topology.d.ts +11 -0
  79. package/dist/src/topology/fully-connected-topology.d.ts.map +1 -0
  80. package/dist/src/topology/fully-connected-topology.js +34 -0
  81. package/dist/src/topology/fully-connected-topology.js.map +1 -0
  82. package/dist/src/topology/index.d.ts +5 -0
  83. package/dist/src/topology/index.d.ts.map +1 -0
  84. package/dist/src/topology/index.js +24 -0
  85. package/dist/src/topology/index.js.map +1 -0
  86. package/dist/src/topology/mmst-topology.d.ts +31 -0
  87. package/dist/src/topology/mmst-topology.d.ts.map +1 -0
  88. package/dist/src/topology/mmst-topology.js +73 -0
  89. package/dist/src/topology/mmst-topology.js.map +1 -0
  90. package/dist/src/topology/star-topology.d.ts +13 -0
  91. package/dist/src/topology/star-topology.d.ts.map +1 -0
  92. package/dist/src/topology/star-topology.js +60 -0
  93. package/dist/src/topology/star-topology.js.map +1 -0
  94. package/dist/src/topology/topology.d.ts +56 -0
  95. package/dist/src/topology/topology.d.ts.map +1 -0
  96. package/dist/src/topology/topology.js +6 -0
  97. package/dist/src/topology/topology.js.map +1 -0
  98. package/dist/src/transport/index.d.ts +6 -0
  99. package/dist/src/transport/index.d.ts.map +1 -0
  100. package/dist/src/transport/index.js +25 -0
  101. package/dist/src/transport/index.js.map +1 -0
  102. package/dist/src/transport/memory-transport.d.ts +32 -0
  103. package/dist/src/transport/memory-transport.d.ts.map +1 -0
  104. package/dist/src/transport/memory-transport.js +98 -0
  105. package/dist/src/transport/memory-transport.js.map +1 -0
  106. package/dist/src/transport/memory-transport.test.d.ts +2 -0
  107. package/dist/src/transport/memory-transport.test.d.ts.map +1 -0
  108. package/dist/src/transport/memory-transport.test.js +84 -0
  109. package/dist/src/transport/memory-transport.test.js.map +1 -0
  110. package/dist/src/transport/transport.d.ts +38 -0
  111. package/dist/src/transport/transport.d.ts.map +1 -0
  112. package/dist/src/transport/transport.js +6 -0
  113. package/dist/src/transport/transport.js.map +1 -0
  114. package/dist/src/transport/webrtc-transport-proxy.d.ts +48 -0
  115. package/dist/src/transport/webrtc-transport-proxy.d.ts.map +1 -0
  116. package/dist/src/transport/webrtc-transport-proxy.js +139 -0
  117. package/dist/src/transport/webrtc-transport-proxy.js.map +1 -0
  118. package/dist/src/transport/webrtc-transport-proxy.test.d.ts +2 -0
  119. package/dist/src/transport/webrtc-transport-proxy.test.d.ts.map +1 -0
  120. package/dist/src/transport/webrtc-transport-proxy.test.js +245 -0
  121. package/dist/src/transport/webrtc-transport-proxy.test.js.map +1 -0
  122. package/dist/src/transport/webrtc-transport-service.d.ts +15 -0
  123. package/dist/src/transport/webrtc-transport-service.d.ts.map +1 -0
  124. package/dist/src/transport/webrtc-transport-service.js +97 -0
  125. package/dist/src/transport/webrtc-transport-service.js.map +1 -0
  126. package/dist/src/transport/webrtc-transport.d.ts +34 -0
  127. package/dist/src/transport/webrtc-transport.d.ts.map +1 -0
  128. package/dist/src/transport/webrtc-transport.js +100 -0
  129. package/dist/src/transport/webrtc-transport.js.map +1 -0
  130. package/dist/src/transport/webrtc-transport.test.d.ts +2 -0
  131. package/dist/src/transport/webrtc-transport.test.d.ts.map +1 -0
  132. package/dist/src/transport/webrtc-transport.test.js +80 -0
  133. package/dist/src/transport/webrtc-transport.test.js.map +1 -0
  134. package/dist/src/transport/webrtc.d.ts +6 -0
  135. package/dist/src/transport/webrtc.d.ts.map +1 -0
  136. package/dist/src/transport/webrtc.js +17 -0
  137. package/dist/src/transport/webrtc.js.map +1 -0
  138. package/dist/src/transport/wrtc-simple-peer.test.d.ts +2 -0
  139. package/dist/src/transport/wrtc-simple-peer.test.d.ts.map +1 -0
  140. package/dist/src/transport/wrtc-simple-peer.test.js +24 -0
  141. package/dist/src/transport/wrtc-simple-peer.test.js.map +1 -0
  142. package/dist/src/types.d.ts +6 -0
  143. package/dist/src/types.d.ts.map +1 -0
  144. package/dist/src/types.js +6 -0
  145. package/dist/src/types.js.map +1 -0
  146. package/package.json +58 -0
  147. package/src/connection-log.ts +102 -0
  148. package/src/globals.d.ts +7 -0
  149. package/src/index.ts +12 -0
  150. package/src/network-manager.test.ts +764 -0
  151. package/src/network-manager.ts +204 -0
  152. package/src/presence.test.ts +78 -0
  153. package/src/protocol-factory.ts +83 -0
  154. package/src/signal/index.ts +7 -0
  155. package/src/signal/integration.test.ts +129 -0
  156. package/src/signal/message-router.test.ts +274 -0
  157. package/src/signal/message-router.ts +200 -0
  158. package/src/signal/signal-connection.ts +20 -0
  159. package/src/signal/signal-messaging.ts +38 -0
  160. package/src/swarm/connection.ts +206 -0
  161. package/src/swarm/index.ts +7 -0
  162. package/src/swarm/swarm-mapper.ts +120 -0
  163. package/src/swarm/swarm.test.ts +192 -0
  164. package/src/swarm/swarm.ts +328 -0
  165. package/src/testing/index.ts +5 -0
  166. package/src/testing/test-protocol.ts +160 -0
  167. package/src/topology/fully-connected-topology.ts +38 -0
  168. package/src/topology/index.ts +8 -0
  169. package/src/topology/mmst-topology.ts +105 -0
  170. package/src/topology/star-topology.ts +67 -0
  171. package/src/topology/topology.ts +69 -0
  172. package/src/transport/index.ts +9 -0
  173. package/src/transport/memory-transport.test.ts +110 -0
  174. package/src/transport/memory-transport.ts +128 -0
  175. package/src/transport/transport.ts +47 -0
  176. package/src/transport/webrtc-transport-proxy.test.ts +290 -0
  177. package/src/transport/webrtc-transport-proxy.ts +167 -0
  178. package/src/transport/webrtc-transport-service.ts +115 -0
  179. package/src/transport/webrtc-transport.test.ts +112 -0
  180. package/src/transport/webrtc-transport.ts +122 -0
  181. package/src/transport/webrtc.ts +15 -0
  182. package/src/transport/wrtc-simple-peer.test.ts +24 -0
  183. package/src/types.ts +10 -0
  184. package/src/typings.d.ts +7 -0
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2020 DXOS.org
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SwarmMapper = void 0;
7
+ const async_1 = require("@dxos/async");
8
+ const keys_1 = require("@dxos/keys");
9
+ const log_1 = require("@dxos/log");
10
+ const util_1 = require("@dxos/util");
11
+ class SwarmMapper {
12
+ // prettier-ignore
13
+ constructor(_swarm, _presence) {
14
+ this._swarm = _swarm;
15
+ this._presence = _presence;
16
+ this._subscriptions = new async_1.EventSubscriptions();
17
+ this._connectionSubscriptions = new util_1.ComplexMap(keys_1.PublicKey.hash);
18
+ this._peers = new util_1.ComplexMap(keys_1.PublicKey.hash);
19
+ this.mapUpdated = new async_1.Event();
20
+ this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
21
+ this._update();
22
+ this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
23
+ this._update();
24
+ }));
25
+ }));
26
+ this._subscriptions.add(_swarm.connectionRemoved.on((connection) => {
27
+ var _a;
28
+ (_a = this._connectionSubscriptions.get(connection.remoteId)) === null || _a === void 0 ? void 0 : _a();
29
+ this._connectionSubscriptions.delete(connection.remoteId);
30
+ this._update();
31
+ }));
32
+ if (_presence) {
33
+ const cb = () => {
34
+ this._update();
35
+ };
36
+ this._subscriptions.add(_presence.graphUpdated.on(cb));
37
+ }
38
+ this._update();
39
+ }
40
+ get peers() {
41
+ return Array.from(this._peers.values());
42
+ }
43
+ _update() {
44
+ this._peers.clear();
45
+ this._peers.set(this._swarm.ownPeerId, {
46
+ id: this._swarm.ownPeerId,
47
+ state: 'ME',
48
+ connections: []
49
+ });
50
+ for (const connection of this._swarm.connections) {
51
+ this._peers.set(connection.remoteId, {
52
+ id: connection.remoteId,
53
+ state: connection.state,
54
+ connections: [this._swarm.ownPeerId]
55
+ });
56
+ }
57
+ if (this._presence) {
58
+ this._presence.graph.forEachNode((node) => {
59
+ const id = keys_1.PublicKey.fromHex(node.id);
60
+ if (this._peers.has(id)) {
61
+ return;
62
+ }
63
+ this._peers.set(id, {
64
+ id,
65
+ state: 'INDIRECTLY_CONNECTED',
66
+ connections: []
67
+ });
68
+ });
69
+ this._presence.graph.forEachLink((link) => {
70
+ const from = keys_1.PublicKey.from(link.fromId);
71
+ const to = keys_1.PublicKey.from(link.toId);
72
+ // Ignore connections to self, they are already handled.
73
+ if (!from.equals(this._swarm.ownPeerId) && !to.equals(this._swarm.ownPeerId)) {
74
+ this._peers.get(from).connections.push(to);
75
+ }
76
+ });
77
+ }
78
+ (0, log_1.log)('graph changed', {
79
+ directConnections: this._swarm.connections.length,
80
+ totalPeersInSwarm: this._peers.size
81
+ }, { file: "packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts", line: 109 });
82
+ this.mapUpdated.emit(Array.from(this._peers.values()));
83
+ }
84
+ destroy() {
85
+ Array.from(this._connectionSubscriptions.values()).forEach((cb) => cb());
86
+ this._subscriptions.clear();
87
+ }
88
+ }
89
+ exports.SwarmMapper = SwarmMapper;
90
+ //# sourceMappingURL=swarm-mapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm-mapper.js","sourceRoot":"","sources":["../../../src/swarm/swarm-mapper.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;AAEF,uCAAwD;AACxD,qCAAuC;AACvC,mCAAgC;AAEhC,qCAAwC;AAqBxC,MAAa,WAAW;IAYtB,kBAAkB;IAClB,YACmB,MAAa,EACb,SAAqC;QADrC,WAAM,GAAN,MAAM,CAAO;QACb,cAAS,GAAT,SAAS,CAA4B;QAdvC,mBAAc,GAAG,IAAI,0BAAkB,EAAE,CAAC;QAC1C,6BAAwB,GAAG,IAAI,iBAAU,CAAyB,gBAAS,CAAC,IAAI,CAAC,CAAC;QAElF,WAAM,GAAG,IAAI,iBAAU,CAAsB,gBAAS,CAAC,IAAI,CAAC,CAAC;QAMrE,eAAU,GAAG,IAAI,aAAK,EAAc,CAAC;QAO5C,IAAI,CAAC,cAAc,CAAC,GAAG,CACrB,MAAM,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC,UAAU,EAAE,EAAE;YACvC,I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C;iBAC7C;YACH,CAAC,CAAC,CAAC;SACJ;QACD,IAAA,SAAG,EAAC,eAAe,EAAE;YACnB,iBAAiB,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM;YACjD,iBAAiB,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;SACpC,sFAAC,CAAC;QACH,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACzD,CAAC;IAED,OAAO;QACL,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,wBAAwB,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;QACzE,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;CACF;AA1FD,kCA0FC"}
@@ -0,0 +1,64 @@
1
+ import { Event } from '@dxos/async';
2
+ import { ErrorStream } from '@dxos/debug';
3
+ import { PublicKey } from '@dxos/keys';
4
+ import { Messenger } from '@dxos/messaging';
5
+ import { SwarmEvent } from '@dxos/protocols/proto/dxos/mesh/signal';
6
+ import { Answer } from '@dxos/protocols/proto/dxos/mesh/swarm';
7
+ import { ProtocolProvider } from '../network-manager';
8
+ import { OfferMessage, SignalMessage } from '../signal/signal-messaging';
9
+ import { Topology } from '../topology';
10
+ import { TransportFactory } from '../transport';
11
+ import { Topic } from '../types';
12
+ import { Connection } from './connection';
13
+ /**
14
+ * A single peer's view of the swarm.
15
+ * Manages a set of connections implemented by simple-peer instances.
16
+ * Routes signal events and maintains swarm topology.
17
+ */
18
+ export declare class Swarm {
19
+ private readonly _topic;
20
+ private readonly _ownPeerId;
21
+ private _topology;
22
+ private readonly _protocolProvider;
23
+ private readonly _messenger;
24
+ private readonly _transportFactory;
25
+ private readonly _label;
26
+ private readonly _connections;
27
+ private readonly _discoveredPeers;
28
+ private readonly _swarmMessenger;
29
+ /**
30
+ * Unique id of the swarm, local to the current peer, generated when swarm is joined.
31
+ */
32
+ readonly id: PublicKey;
33
+ /**
34
+ * New connection to a peer is started.
35
+ */
36
+ readonly connectionAdded: Event<Connection>;
37
+ /**
38
+ * Connection to a peer is dropped.
39
+ */
40
+ readonly connectionRemoved: Event<Connection>;
41
+ /**
42
+ * Connection is established to a new peer.
43
+ */
44
+ readonly connected: Event<PublicKey>;
45
+ readonly errors: ErrorStream;
46
+ constructor(_topic: PublicKey, _ownPeerId: PublicKey, _topology: Topology, _protocolProvider: ProtocolProvider, _messenger: Messenger, _transportFactory: TransportFactory, _label: string | undefined);
47
+ get connections(): Connection[];
48
+ get ownPeerId(): PublicKey;
49
+ /**
50
+ * Custom label assigned to this swarm. Used in devtools to display human-readable names for swarms.
51
+ */
52
+ get label(): string | undefined;
53
+ get topic(): Topic;
54
+ onSwarmEvent(swarmEvent: SwarmEvent): void;
55
+ onOffer(message: OfferMessage): Promise<Answer>;
56
+ onSignal(message: SignalMessage): Promise<void>;
57
+ setTopology(topology: Topology): Promise<void>;
58
+ destroy(): Promise<void>;
59
+ private _getSwarmController;
60
+ private _initiateConnection;
61
+ private _createConnection;
62
+ private _closeConnection;
63
+ }
64
+ //# sourceMappingURL=swarm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm.d.ts","sourceRoot":"","sources":["../../../src/swarm/swarm.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAS,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAG/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oB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;IAED,IAAI,KAAK,IAAI,KAAK,CAEjB;IAED,YAAY,CAAC,UAAU,EAAE,UAAU;IAe7B,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAoD/C,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAiB/C,WAAW,CAAC,QAAQ,EAAE,QAAQ;IAe9B,OAAO;IAMb,OAAO,CAAC,mBAAmB;YAoBb,mBAAmB;IAsBjC,OAAO,CAAC,iBAAiB;YA4DX,gBAAgB;CAW/B"}
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2020 DXOS.org
4
+ //
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Swarm = void 0;
10
+ const node_assert_1 = __importDefault(require("node:assert"));
11
+ const async_1 = require("@dxos/async");
12
+ const crypto_1 = require("@dxos/crypto");
13
+ const debug_1 = require("@dxos/debug");
14
+ const keys_1 = require("@dxos/keys");
15
+ const log_1 = require("@dxos/log");
16
+ const util_1 = require("@dxos/util");
17
+ const signal_1 = require("../signal");
18
+ const connection_1 = require("./connection");
19
+ const INITIATION_DELAY = 100;
20
+ // TODO(burdon): Factor out.
21
+ const getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
22
+ /**
23
+ * A single peer's view of the swarm.
24
+ * Manages a set of connections implemented by simple-peer instances.
25
+ * Routes signal events and maintains swarm topology.
26
+ */
27
+ class Swarm {
28
+ // TODO(burdon): Split up properties.
29
+ constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
30
+ this._topic = _topic;
31
+ this._ownPeerId = _ownPeerId;
32
+ this._topology = _topology;
33
+ this._protocolProvider = _protocolProvider;
34
+ this._messenger = _messenger;
35
+ this._transportFactory = _transportFactory;
36
+ this._label = _label;
37
+ this._connections = new util_1.ComplexMap(keys_1.PublicKey.hash);
38
+ this._discoveredPeers = new util_1.ComplexSet(keys_1.PublicKey.hash);
39
+ /**
40
+ * Unique id of the swarm, local to the current peer, generated when swarm is joined.
41
+ */
42
+ this.id = keys_1.PublicKey.random();
43
+ /**
44
+ * New connection to a peer is started.
45
+ */
46
+ this.connectionAdded = new async_1.Event();
47
+ /**
48
+ * Connection to a peer is dropped.
49
+ */
50
+ this.connectionRemoved = new async_1.Event();
51
+ /**
52
+ * Connection is established to a new peer.
53
+ */
54
+ this.connected = new async_1.Event();
55
+ this.errors = new debug_1.ErrorStream();
56
+ (0, log_1.log)('creating swarm', { topic: this._topic, peerId: _ownPeerId }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 72 });
57
+ _topology.init(this._getSwarmController());
58
+ this._swarmMessenger = new signal_1.MessageRouter({
59
+ sendMessage: async (msg) => await this._messenger.sendMessage(msg),
60
+ onSignal: async (msg) => await this.onSignal(msg),
61
+ onOffer: async (msg) => await this.onOffer(msg),
62
+ topic: this._topic
63
+ });
64
+ this._messenger
65
+ .listen({
66
+ peerId: this._ownPeerId,
67
+ payloadType: 'dxos.mesh.swarm.SwarmMessage',
68
+ onMessage: async (message) => await this._swarmMessenger.receiveMessage(message)
69
+ })
70
+ .catch((error) => log_1.log.catch(error, {}, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 88 }));
71
+ }
72
+ get connections() {
73
+ return Array.from(this._connections.values());
74
+ }
75
+ get ownPeerId() {
76
+ return this._ownPeerId;
77
+ }
78
+ /**
79
+ * Custom label assigned to this swarm. Used in devtools to display human-readable names for swarms.
80
+ */
81
+ get label() {
82
+ return this._label;
83
+ }
84
+ get topic() {
85
+ return this._topic;
86
+ }
87
+ onSwarmEvent(swarmEvent) {
88
+ (0, log_1.log)('swarm event', { topic: this._topic, swarmEvent }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 111 });
89
+ if (swarmEvent.peerAvailable) {
90
+ const peerId = keys_1.PublicKey.from(swarmEvent.peerAvailable.peer);
91
+ (0, log_1.log)('new peer', { topic: this._topic, peerId }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 114 });
92
+ if (!peerId.equals(this._ownPeerId)) {
93
+ this._discoveredPeers.add(peerId);
94
+ }
95
+ }
96
+ else if (swarmEvent.peerLeft) {
97
+ this._discoveredPeers.delete(keys_1.PublicKey.from(swarmEvent.peerLeft.peer));
98
+ }
99
+ this._topology.update();
100
+ }
101
+ async onOffer(message) {
102
+ var _a, _b;
103
+ (0, log_1.log)('offer', { topic: this._topic, message }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 126 });
104
+ // Id of the peer offering us the connection.
105
+ (0, node_assert_1.default)(message.author);
106
+ const remoteId = message.author;
107
+ if (!((_a = message.recipient) === null || _a === void 0 ? void 0 : _a.equals(this._ownPeerId))) {
108
+ (0, log_1.log)('rejecting offer with incorrect peerId', { topic: this._topic, message }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 131 });
109
+ return { accept: false };
110
+ }
111
+ if (!((_b = message.topic) === null || _b === void 0 ? void 0 : _b.equals(this._topic))) {
112
+ (0, log_1.log)('rejecting offer with incorrect topic', { topic: this._topic, message }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 135 });
113
+ return { accept: false };
114
+ }
115
+ // Check if we are already trying to connect to that peer.
116
+ if (this._connections.has(remoteId)) {
117
+ // Peer with the highest Id closes it's connection, and accepts remote peer's offer.
118
+ if (remoteId.toHex() < this._ownPeerId.toHex()) {
119
+ (0, log_1.log)("closing local connection and accepting remote peer's offer", {
120
+ topic: this._topic,
121
+ peerId: this._ownPeerId
122
+ }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 143 });
123
+ // Close our connection and accept remote peer's connection.
124
+ await this._closeConnection(remoteId).catch((err) => {
125
+ this.errors.raise(err);
126
+ });
127
+ }
128
+ else {
129
+ // Continue with our origination attempt, the remote peer will close it's connection and accept ours.
130
+ return { accept: true };
131
+ }
132
+ }
133
+ let accept = false;
134
+ if (await this._topology.onOffer(remoteId)) {
135
+ if (!this._connections.has(remoteId)) {
136
+ // Connection might have been already established.
137
+ (0, node_assert_1.default)(message.sessionId);
138
+ const connection = this._createConnection(false, message.author, message.sessionId);
139
+ try {
140
+ connection.open();
141
+ }
142
+ catch (err) {
143
+ this.errors.raise(err);
144
+ }
145
+ accept = true;
146
+ }
147
+ }
148
+ this._topology.update();
149
+ return { accept };
150
+ }
151
+ async onSignal(message) {
152
+ var _a, _b;
153
+ (0, log_1.log)('signal', { topic: this._topic, message }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 178 });
154
+ (0, node_assert_1.default)((_a = message.recipient) === null || _a === void 0 ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
155
+ (0, node_assert_1.default)((_b = message.topic) === null || _b === void 0 ? void 0 : _b.equals(this._topic));
156
+ (0, node_assert_1.default)(message.author);
157
+ const connection = this._connections.get(message.author);
158
+ if (!connection) {
159
+ (0, log_1.log)('dropping signal message for non-existent connection', { topic: this._topic, message }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 187 });
160
+ return;
161
+ }
162
+ await connection.signal(message);
163
+ }
164
+ async setTopology(topology) {
165
+ if (topology === this._topology) {
166
+ return;
167
+ }
168
+ (0, log_1.log)('setting topology', {
169
+ topic: this._topic,
170
+ previous: getClassName(this._topology),
171
+ topology: getClassName(topology)
172
+ }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 198 });
173
+ await this._topology.destroy();
174
+ this._topology = topology;
175
+ this._topology.init(this._getSwarmController());
176
+ this._topology.update();
177
+ }
178
+ async destroy() {
179
+ (0, log_1.log)('destroying', { topic: this._topic }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 210 });
180
+ await this._topology.destroy();
181
+ await Promise.all(Array.from(this._connections.keys()).map((key) => this._closeConnection(key)));
182
+ }
183
+ _getSwarmController() {
184
+ return {
185
+ getState: () => ({
186
+ ownPeerId: this._ownPeerId,
187
+ connected: Array.from(this._connections.keys()),
188
+ candidates: Array.from(this._discoveredPeers.keys()).filter((key) => !this._connections.has(key))
189
+ }),
190
+ connect: (peer) => this._initiateConnection(peer),
191
+ disconnect: async (peer) => {
192
+ try {
193
+ await this._closeConnection(peer);
194
+ }
195
+ catch (err) {
196
+ this.errors.raise(err);
197
+ }
198
+ this._topology.update();
199
+ }
200
+ };
201
+ }
202
+ // TODO(burdon): Make async.
203
+ async _initiateConnection(remoteId) {
204
+ // It is likely that the other peer will also try to connect to us at the same time.
205
+ // If our peerId is higher, we will wait for a bit so that other peer has a chance to connect first.
206
+ if (remoteId.toHex() < this._ownPeerId.toHex()) {
207
+ await (0, async_1.sleep)(INITIATION_DELAY);
208
+ }
209
+ if (this._connections.has(remoteId)) {
210
+ // Do nothing if peer is already connected.
211
+ return;
212
+ }
213
+ const sessionId = keys_1.PublicKey.random();
214
+ (0, log_1.log)('initiating...', { topic: this._topic, peerId: remoteId, sessionId }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 249 });
215
+ const connection = this._createConnection(true, remoteId, sessionId);
216
+ connection.initiate();
217
+ this._topology.update();
218
+ (0, log_1.log)('initiated', { topic: this._topic }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 253 });
219
+ }
220
+ // TODO(burdon): Make async.
221
+ _createConnection(initiator, remoteId, sessionId) {
222
+ (0, log_1.log)('creating connection', { topic: this._topic, peerId: this._ownPeerId, remoteId, initiator }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 258 });
223
+ (0, node_assert_1.default)(!this._connections.has(remoteId), 'Peer already connected.');
224
+ const connection = new connection_1.Connection(this._topic, this._ownPeerId, remoteId, sessionId, initiator, this._swarmMessenger, this._protocolProvider({ channel: (0, crypto_1.discoveryKey)(this._topic), initiator }), this._transportFactory);
225
+ this._connections.set(remoteId, connection);
226
+ this.connectionAdded.emit(connection);
227
+ connection.errors.handle((err) => {
228
+ // TODO(burdon): Change to warn? Why does this fail during tests?
229
+ (0, log_1.log)('connection failed', { topic: this._topic, peerId: this._ownPeerId, remoteId, initiator, err }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 277 });
230
+ this._closeConnection(remoteId).catch((err) => this.errors.raise(err));
231
+ });
232
+ connection.stateChanged.on((state) => {
233
+ var _a;
234
+ switch (state) {
235
+ case connection_1.ConnectionState.CONNECTED: {
236
+ this.connected.emit(remoteId);
237
+ break;
238
+ }
239
+ case connection_1.ConnectionState.REJECTED: {
240
+ // If the peer rejected our connection remove it from the set of candidates.
241
+ this._discoveredPeers.delete(remoteId);
242
+ break;
243
+ }
244
+ case connection_1.ConnectionState.ACCEPTED: {
245
+ this._topology.update();
246
+ break;
247
+ }
248
+ case connection_1.ConnectionState.CLOSED: {
249
+ (0, log_1.log)('connection closed', { topic: this._topic, peerId: this._ownPeerId, remoteId, initiator }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 300 });
250
+ // Connection might have been already closed or replace by a different one.
251
+ // Only remove the connection if it has the same session id.
252
+ if ((_a = this._connections.get(remoteId)) === null || _a === void 0 ? void 0 : _a.sessionId.equals(sessionId)) {
253
+ this._connections.delete(remoteId);
254
+ this.connectionRemoved.emit(connection);
255
+ this._topology.update();
256
+ }
257
+ break;
258
+ }
259
+ }
260
+ });
261
+ return connection;
262
+ }
263
+ // TODO(burdon): Make async.
264
+ async _closeConnection(peerId) {
265
+ (0, log_1.log)('closing...', { topic: this._topic, peerId }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 318 });
266
+ const connection = this._connections.get(peerId);
267
+ if (!connection) {
268
+ return;
269
+ }
270
+ this._connections.delete(peerId);
271
+ await connection.close();
272
+ (0, log_1.log)('closed', { topic: this._topic }, { file: "packages/core/mesh/network-manager/src/swarm/swarm.ts", line: 326 });
273
+ }
274
+ }
275
+ exports.Swarm = Swarm;
276
+ //# source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GAAG,CAAC,MAAM,CAAC,CAAC;QACjD,IAAI,CAAC,UAAU,EAAE;YACf,OAAO;SACR;QAED,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACjC,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;QACzB,IAAA,SAAG,EAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,+EAAC,CAAC;IACxC,CAAC;CACF;AArSD,sBAqSC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=swarm.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm.test.d.ts","sourceRoot":"","sources":["../../../src/swarm/swarm.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ //
3
+ // Copyright 2020 DXOS.org
4
+ //
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ // @dxos/mocha platform=nodejs
10
+ const earljs_1 = require("earljs");
11
+ const wait_for_expect_1 = __importDefault(require("wait-for-expect"));
12
+ const async_1 = require("@dxos/async");
13
+ const keys_1 = require("@dxos/keys");
14
+ const log_1 = require("@dxos/log");
15
+ const mesh_protocol_1 = require("@dxos/mesh-protocol");
16
+ const messaging_1 = require("@dxos/messaging");
17
+ const testutils_1 = require("@dxos/testutils");
18
+ const topology_1 = require("../topology");
19
+ const transport_1 = require("../transport");
20
+ const swarm_1 = require("./swarm");
21
+ describe('Swarm', function () {
22
+ const context = new messaging_1.MemorySignalManagerContext();
23
+ const setupSwarm = () => {
24
+ const topic = keys_1.PublicKey.random();
25
+ const peerId1 = keys_1.PublicKey.random();
26
+ const peerId2 = keys_1.PublicKey.random();
27
+ const swarm1 = new swarm_1.Swarm(topic, peerId1, new topology_1.FullyConnectedTopology(), () => new mesh_protocol_1.Protocol(), new messaging_1.Messenger({ signalManager: new messaging_1.MemorySignalManager(context) }), (0, transport_1.createWebRTCTransportFactory)(), undefined);
28
+ const swarm2 = new swarm_1.Swarm(topic, peerId2, new topology_1.FullyConnectedTopology(), () => new mesh_protocol_1.Protocol(), new messaging_1.Messenger({ signalManager: new messaging_1.MemorySignalManager(context) }), (0, transport_1.createWebRTCTransportFactory)(), undefined);
29
+ (0, testutils_1.afterTest)(async () => {
30
+ await swarm1.destroy();
31
+ await swarm2.destroy();
32
+ });
33
+ return { swarm1, swarm2, peerId1, peerId2 };
34
+ };
35
+ it('connects two peers in a swarm', async function () {
36
+ const { swarm1, swarm2, peerId1, peerId2 } = setupSwarm();
37
+ (0, earljs_1.expect)(swarm1.connections.length).toEqual(0);
38
+ (0, earljs_1.expect)(swarm2.connections.length).toEqual(0);
39
+ const promise = Promise.all([
40
+ (0, async_1.asyncTimeout)(swarm1.connected.waitForCount(1), 3000, new Error('Swarm1 connect timeout.')),
41
+ (0, async_1.asyncTimeout)(swarm2.connected.waitForCount(1), 3000, new Error('Swarm2 connect timeout.'))
42
+ ]);
43
+ // Behavior of the Signal Server.
44
+ swarm1.onSwarmEvent({
45
+ peerAvailable: {
46
+ peer: peerId1.asUint8Array(),
47
+ since: new Date()
48
+ }
49
+ });
50
+ swarm1.onSwarmEvent({
51
+ peerAvailable: {
52
+ peer: peerId2.asUint8Array(),
53
+ since: new Date()
54
+ }
55
+ });
56
+ (0, log_1.log)('Candidates changed', {}, { file: "packages/core/mesh/network-manager/src/swarm/swarm.test.ts", line: 83 });
57
+ await promise;
58
+ (0, log_1.log)('Swarms connected', {}, { file: "packages/core/mesh/network-manager/src/swarm/swarm.test.ts", line: 85 });
59
+ const swarm1Connection = swarm1.connections[0];
60
+ const swarm2Connection = swarm2.connections[0];
61
+ const onData = (0, earljs_1.mockFn)().returns(undefined);
62
+ swarm2Connection.transport.peer.on('data', onData);
63
+ const data = Buffer.from('1234');
64
+ swarm1Connection.transport.peer.send(data);
65
+ await (0, wait_for_expect_1.default)(() => {
66
+ (0, earljs_1.expect)(onData).toHaveBeenCalledWith([data]);
67
+ });
68
+ }).timeout(5000);
69
+ it('two peers try to originate connections to each other simultaneously', async function () {
70
+ const { swarm1, swarm2, peerId1, peerId2 } = setupSwarm();
71
+ (0, earljs_1.expect)(swarm1.connections.length).toEqual(0);
72
+ (0, earljs_1.expect)(swarm2.connections.length).toEqual(0);
73
+ const connectPromises = Promise.all([swarm1.connected.waitForCount(1), swarm2.connected.waitForCount(1)]);
74
+ swarm1.onSwarmEvent({
75
+ peerAvailable: {
76
+ peer: peerId2.asUint8Array(),
77
+ since: new Date()
78
+ }
79
+ });
80
+ swarm2.onSwarmEvent({
81
+ peerAvailable: {
82
+ peer: peerId1.asUint8Array(),
83
+ since: new Date()
84
+ }
85
+ });
86
+ await connectPromises;
87
+ }).timeout(5000);
88
+ it('second peer discovered after delay', async function () {
89
+ const { swarm1, swarm2, peerId1, peerId2 } = setupSwarm();
90
+ (0, earljs_1.expect)(swarm1.connections.length).toEqual(0);
91
+ (0, earljs_1.expect)(swarm2.connections.length).toEqual(0);
92
+ const connectPromises = Promise.all([swarm1.connected.waitForCount(1), swarm2.connected.waitForCount(1)]);
93
+ swarm1.onSwarmEvent({
94
+ peerAvailable: {
95
+ peer: peerId2.asUint8Array(),
96
+ since: new Date()
97
+ }
98
+ });
99
+ await (0, async_1.sleep)(15);
100
+ swarm2.onSwarmEvent({
101
+ peerAvailable: {
102
+ peer: peerId1.asUint8Array(),
103
+ since: new Date()
104
+ }
105
+ });
106
+ await connectPromises;
107
+ const swarm1Connection = swarm1.connections[0];
108
+ const swarm2Connection = swarm2.connections[0];
109
+ const onData = (0, earljs_1.mockFn)().returns(undefined);
110
+ swarm2Connection.transport.peer.on('data', onData);
111
+ const data = Buffer.from('1234');
112
+ swarm1Connection.transport.peer.send(data);
113
+ await (0, wait_for_expect_1.default)(() => {
114
+ (0, earljs_1.expect)(onData).toHaveBeenCalledWith([data]);
115
+ });
116
+ }).timeout(10000);
117
+ it('swarming with message router', async function () {
118
+ const { swarm1, swarm2, peerId2 } = setupSwarm();
119
+ const promise = Promise.all([
120
+ (0, async_1.asyncTimeout)(swarm1.connected.waitForCount(1), 3000, new Error('Swarm1 connect timeout.')),
121
+ (0, async_1.asyncTimeout)(swarm2.connected.waitForCount(1), 3000, new Error('Swarm2 connect timeout.'))
122
+ ]);
123
+ swarm1.onSwarmEvent({
124
+ peerAvailable: {
125
+ peer: peerId2.asUint8Array(),
126
+ since: new Date()
127
+ }
128
+ });
129
+ (0, log_1.log)('Candidates changed', {}, { file: "packages/core/mesh/network-manager/src/swarm/swarm.test.ts", line: 175 });
130
+ await promise;
131
+ (0, log_1.log)('Swarms connected', {}, { file: "packages/core/mesh/network-manager/src/swarm/swarm.test.ts", line: 177 });
132
+ const swarm1Connection = swarm1.connections[0];
133
+ const swarm2Connection = swarm2.connections[0];
134
+ const onData = (0, earljs_1.mockFn)().returns(undefined);
135
+ swarm2Connection.transport.peer.on('data', onData);
136
+ const data = Buffer.from('1234');
137
+ swarm1Connection.transport.peer.send(data);
138
+ await (0, wait_for_expect_1.default)(() => {
139
+ (0, earljs_1.expect)(onData).toHaveBeenCalledWith([data]);
140
+ });
141
+ await swarm1.destroy();
142
+ await swarm2.destroy();
143
+ }).timeout(5000);
144
+ });
145
+ //# sourceMappingURL=swarm.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm.test.js","sourceRoot":"","sources":["../../../src/swarm/swarm.test.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;AAEF,8BAA8B;AAE9B,mCAAwC;AACxC,sEAA4C;AAE5C,uCAAkD;AAClD,qCAAuC;AACvC,mCAAgC;AAChC,uDAA+C;AAC/C,+CAA6F;AAC7F,+CAA4C;AAE5C,0CAAqD;AACrD,4CAA6E;AAC7E,mCAAgC;AAEhC,QAAQ,CAAC,OAAO,EAAE;IAChB,MAAM,OAAO,GAAG,IAAI,sCAA0B,EAAE,CAAC;IAEjD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AAC;QAEzE,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAChC,gBAAgB,CAAC,SAA6B,CAAC,IAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjE,MAAM,IAAA,yBAAa,EAAC,GAAG,EAAE;YACvB,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,oBAAoB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;QACH,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;QACvB,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC,CAAC,CAAC,OAAO,CAAC,IAAK,CAAC,CAAC;AACpB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './test-protocol';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/testing/index.ts"],"names":[],"mappings":"AAIA,cAAc,iBAAiB,CAAC"}