@streamr/dht 0.0.1-tatum.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.
- package/.eslintignore +5 -0
- package/.eslintrc +3 -0
- package/README.md +38 -0
- package/dist/src/connection/Connection.d.ts +11 -0
- package/dist/src/connection/Connection.js +23 -0
- package/dist/src/connection/Connection.js.map +1 -0
- package/dist/src/connection/ConnectionLockHandler.d.ts +23 -0
- package/dist/src/connection/ConnectionLockHandler.js +94 -0
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +94 -0
- package/dist/src/connection/ConnectionManager.js +554 -0
- package/dist/src/connection/ConnectionManager.js.map +1 -0
- package/dist/src/connection/ConnectivityChecker.d.ts +17 -0
- package/dist/src/connection/ConnectivityChecker.js +187 -0
- package/dist/src/connection/ConnectivityChecker.js.map +1 -0
- package/dist/src/connection/Handshaker.d.ts +19 -0
- package/dist/src/connection/Handshaker.js +77 -0
- package/dist/src/connection/Handshaker.js.map +1 -0
- package/dist/src/connection/IConnection.d.ts +38 -0
- package/dist/src/connection/IConnection.js +19 -0
- package/dist/src/connection/IConnection.js.map +1 -0
- package/dist/src/connection/IConnectionSource.d.ts +4 -0
- package/dist/src/connection/IConnectionSource.js +3 -0
- package/dist/src/connection/IConnectionSource.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +60 -0
- package/dist/src/connection/ManagedConnection.js +352 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -0
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +7 -0
- package/dist/src/connection/ManagedWebRtcConnection.js +20 -0
- package/dist/src/connection/ManagedWebRtcConnection.js.map +1 -0
- package/dist/src/connection/RemoteConnectionLocker.d.ts +14 -0
- package/dist/src/connection/RemoteConnectionLocker.js +93 -0
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -0
- package/dist/src/connection/Simulator/Simulator.d.ts +42 -0
- package/dist/src/connection/Simulator/Simulator.js +325 -0
- package/dist/src/connection/Simulator/Simulator.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnection.d.ts +19 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js +118 -0
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +17 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js +72 -0
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -0
- package/dist/src/connection/Simulator/SimulatorTransport.d.ts +6 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js +11 -0
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -0
- package/dist/src/connection/Simulator/pings.d.ts +21 -0
- package/dist/src/connection/Simulator/pings.js +61 -0
- package/dist/src/connection/Simulator/pings.js.map +1 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.d.ts +20 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js +9 -0
- package/dist/src/connection/WebRTC/IWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +47 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +233 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +12 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +74 -0
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +47 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js +227 -0
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +2 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js +18 -0
- package/dist/src/connection/WebRTC/iceServerAsString.js.map +1 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.d.ts +15 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js +113 -0
- package/dist/src/connection/WebSocket/ClientWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +9 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +63 -0
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.d.ts +18 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js +103 -0
- package/dist/src/connection/WebSocket/ServerWebSocket.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +31 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js +202 -0
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +9 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js +101 -0
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +153 -0
- package/dist/src/dht/DhtNode.js +599 -0
- package/dist/src/dht/DhtNode.js.map +1 -0
- package/dist/src/dht/DhtPeer.d.ts +18 -0
- package/dist/src/dht/DhtPeer.js +74 -0
- package/dist/src/dht/DhtPeer.js.map +1 -0
- package/dist/src/dht/ExternalApi.d.ts +8 -0
- package/dist/src/dht/ExternalApi.js +26 -0
- package/dist/src/dht/ExternalApi.js.map +1 -0
- package/dist/src/dht/RemoteExternalApi.d.ts +6 -0
- package/dist/src/dht/RemoteExternalApi.js +26 -0
- package/dist/src/dht/RemoteExternalApi.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +22 -0
- package/dist/src/dht/contact/Contact.js +25 -0
- package/dist/src/dht/contact/Contact.js.map +1 -0
- package/dist/src/dht/contact/RandomContactList.d.ts +20 -0
- package/dist/src/dht/contact/RandomContactList.js +78 -0
- package/dist/src/dht/contact/RandomContactList.js.map +1 -0
- package/dist/src/dht/contact/Remote.d.ts +15 -0
- package/dist/src/dht/contact/Remote.js +24 -0
- package/dist/src/dht/contact/Remote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +35 -0
- package/dist/src/dht/contact/SortedContactList.js +156 -0
- package/dist/src/dht/contact/SortedContactList.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +36 -0
- package/dist/src/dht/discovery/DiscoverySession.js +116 -0
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -0
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +42 -0
- package/dist/src/dht/discovery/PeerDiscovery.js +157 -0
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -0
- package/dist/src/dht/find/RecursiveFindSession.d.ts +46 -0
- package/dist/src/dht/find/RecursiveFindSession.js +142 -0
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -0
- package/dist/src/dht/find/RecursiveFinder.d.ts +54 -0
- package/dist/src/dht/find/RecursiveFinder.js +180 -0
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +6 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +25 -0
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -0
- package/dist/src/dht/routing/DuplicateDetector.d.ts +13 -0
- package/dist/src/dht/routing/DuplicateDetector.js +41 -0
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -0
- package/dist/src/dht/routing/RemoteRouter.d.ts +8 -0
- package/dist/src/dht/routing/RemoteRouter.js +106 -0
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +60 -0
- package/dist/src/dht/routing/Router.js +207 -0
- package/dist/src/dht/routing/Router.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.d.ts +42 -0
- package/dist/src/dht/routing/RoutingSession.js +178 -0
- package/dist/src/dht/routing/RoutingSession.js.map +1 -0
- package/dist/src/dht/store/DataStore.d.ts +45 -0
- package/dist/src/dht/store/DataStore.js +244 -0
- package/dist/src/dht/store/DataStore.js.map +1 -0
- package/dist/src/dht/store/LocalDataStore.d.ts +19 -0
- package/dist/src/dht/store/LocalDataStore.js +104 -0
- package/dist/src/dht/store/LocalDataStore.js.map +1 -0
- package/dist/src/dht/store/RemoteStore.d.ts +8 -0
- package/dist/src/dht/store/RemoteStore.js +44 -0
- package/dist/src/dht/store/RemoteStore.js.map +1 -0
- package/dist/src/exports.d.ts +19 -0
- package/dist/src/exports.js +41 -0
- package/dist/src/exports.js.map +1 -0
- package/dist/src/helpers/AddressTools.d.ts +2 -0
- package/dist/src/helpers/AddressTools.js +31 -0
- package/dist/src/helpers/AddressTools.js.map +1 -0
- package/dist/src/helpers/PeerID.d.ts +25 -0
- package/dist/src/helpers/PeerID.js +84 -0
- package/dist/src/helpers/PeerID.js.map +1 -0
- package/dist/src/helpers/UUID.d.ts +7 -0
- package/dist/src/helpers/UUID.js +32 -0
- package/dist/src/helpers/UUID.js.map +1 -0
- package/dist/src/helpers/debugHelpers.d.ts +3 -0
- package/dist/src/helpers/debugHelpers.js +11 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -0
- package/dist/src/helpers/errors.d.ts +72 -0
- package/dist/src/helpers/errors.js +95 -0
- package/dist/src/helpers/errors.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +5 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +17 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/protoClasses.d.ts +2 -0
- package/dist/src/helpers/protoClasses.js +35 -0
- package/dist/src/helpers/protoClasses.js.map +1 -0
- package/dist/src/helpers/protoToString.d.ts +2 -0
- package/dist/src/helpers/protoToString.js +20 -0
- package/dist/src/helpers/protoToString.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.d.ts +173 -0
- package/dist/src/proto/google/protobuf/any.js +155 -0
- package/dist/src/proto/google/protobuf/any.js.map +1 -0
- package/dist/src/proto/google/protobuf/empty.d.ts +32 -0
- package/dist/src/proto/google/protobuf/empty.js +34 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -0
- package/dist/src/proto/google/protobuf/timestamp.d.ts +149 -0
- package/dist/src/proto/google/protobuf/timestamp.js +136 -0
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +320 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +245 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +1089 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +710 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +145 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/src/proto/tests.d.ts +39 -0
- package/dist/src/proto/tests.js +34 -0
- package/dist/src/proto/tests.js.map +1 -0
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +12 -0
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +8 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js +3 -0
- package/dist/src/rpc-protocol/DhtRpcOptions.js.map +1 -0
- package/dist/src/transport/ITransport.d.ts +22 -0
- package/dist/src/transport/ITransport.js +3 -0
- package/dist/src/transport/ITransport.js.map +1 -0
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +6 -0
- package/dist/src/transport/ListeningRpcCommunicator.js +14 -0
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +8 -0
- package/dist/src/transport/RoutingRpcCommunicator.js +52 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -0
- package/jest.config.js +2 -0
- package/karma.config.js +20 -0
- package/package.json +64 -0
- package/proto.sh +3 -0
- package/protos/DhtRpc.proto +330 -0
- package/protos/tests.proto +16 -0
- package/src/connection/Connection.ts +23 -0
- package/src/connection/ConnectionLockHandler.ts +105 -0
- package/src/connection/ConnectionManager.ts +676 -0
- package/src/connection/ConnectivityChecker.ts +173 -0
- package/src/connection/Handshaker.ts +92 -0
- package/src/connection/IConnection.ts +47 -0
- package/src/connection/IConnectionSource.ts +6 -0
- package/src/connection/ManagedConnection.ts +398 -0
- package/src/connection/ManagedWebRtcConnection.ts +27 -0
- package/src/connection/RemoteConnectionLocker.ts +88 -0
- package/src/connection/Simulator/Simulator.ts +399 -0
- package/src/connection/Simulator/SimulatorConnection.ts +137 -0
- package/src/connection/Simulator/SimulatorConnector.ts +104 -0
- package/src/connection/Simulator/SimulatorTransport.ts +9 -0
- package/src/connection/Simulator/pings.ts +42 -0
- package/src/connection/WebRTC/BrowserWebRtcConnection.ts +227 -0
- package/src/connection/WebRTC/IWebRtcConnection.ts +24 -0
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +256 -0
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +93 -0
- package/src/connection/WebRTC/WebRtcConnector.ts +306 -0
- package/src/connection/WebRTC/iceServerAsString.ts +15 -0
- package/src/connection/WebSocket/ClientWebSocket.ts +118 -0
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +49 -0
- package/src/connection/WebSocket/ServerWebSocket.ts +119 -0
- package/src/connection/WebSocket/WebSocketConnector.ts +264 -0
- package/src/connection/WebSocket/WebSocketServer.ts +97 -0
- package/src/dht/DhtNode.ts +776 -0
- package/src/dht/DhtPeer.ts +96 -0
- package/src/dht/ExternalApi.ts +29 -0
- package/src/dht/RemoteExternalApi.ts +25 -0
- package/src/dht/contact/Contact.ts +36 -0
- package/src/dht/contact/RandomContactList.ts +92 -0
- package/src/dht/contact/Remote.ts +40 -0
- package/src/dht/contact/SortedContactList.ts +196 -0
- package/src/dht/discovery/DiscoverySession.ts +150 -0
- package/src/dht/discovery/PeerDiscovery.ts +162 -0
- package/src/dht/find/RecursiveFindSession.ts +178 -0
- package/src/dht/find/RecursiveFinder.ts +272 -0
- package/src/dht/find/RemoteRecursiveFindSession.ts +33 -0
- package/src/dht/routing/DuplicateDetector.ts +53 -0
- package/src/dht/routing/RemoteRouter.ts +115 -0
- package/src/dht/routing/Router.ts +266 -0
- package/src/dht/routing/RoutingSession.ts +222 -0
- package/src/dht/store/DataStore.ts +321 -0
- package/src/dht/store/LocalDataStore.ts +114 -0
- package/src/dht/store/RemoteStore.ts +58 -0
- package/src/exports.ts +19 -0
- package/src/helpers/AddressTools.ts +26 -0
- package/src/helpers/PeerID.ts +95 -0
- package/src/helpers/UUID.ts +30 -0
- package/src/helpers/debugHelpers.ts +9 -0
- package/src/helpers/errors.ts +49 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +14 -0
- package/src/helpers/protoClasses.ts +63 -0
- package/src/helpers/protoToString.ts +21 -0
- package/src/proto/google/protobuf/any.ts +319 -0
- package/src/proto/google/protobuf/empty.ts +84 -0
- package/src/proto/google/protobuf/timestamp.ts +281 -0
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +373 -0
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +148 -0
- package/src/proto/packages/dht/protos/DhtRpc.ts +1399 -0
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +108 -0
- package/src/proto/tests.ts +52 -0
- package/src/rpc-protocol/DhtCallContext.ts +15 -0
- package/src/rpc-protocol/DhtRpcOptions.ts +9 -0
- package/src/transport/ITransport.ts +31 -0
- package/src/transport/ListeningRpcCommunicator.ts +14 -0
- package/src/transport/RoutingRpcCommunicator.ts +59 -0
- package/src/types/glogal.d.ts +1 -0
- package/src/types/textencoding.d.ts +7 -0
- package/test/RandomGraphSimulation.ts +52 -0
- package/test/benchmark/KademliaCorrectness.test.ts +115 -0
- package/test/benchmark/RecursiveFind.test.ts +87 -0
- package/test/benchmark/any.test.ts +28 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +32 -0
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +94 -0
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +125 -0
- package/test/data/generateGroundTruthData.ts +70 -0
- package/test/end-to-end/Layer0-Layer1.test.ts +87 -0
- package/test/end-to-end/Layer0.test.ts +60 -0
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +94 -0
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +134 -0
- package/test/end-to-end/Layer0WebRTC.test.ts +98 -0
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +69 -0
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +73 -0
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +62 -0
- package/test/integration/ConnectionLocking.test.ts +166 -0
- package/test/integration/ConnectionManager.test.ts +291 -0
- package/test/integration/DhtNodeExternalAPI.test.ts +43 -0
- package/test/integration/DhtPeer.test.ts +73 -0
- package/test/integration/DhtRpc.test.ts +131 -0
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +46 -0
- package/test/integration/DhtWithMockConnections.test.ts +46 -0
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +47 -0
- package/test/integration/Layer1-scale.test.ts +200 -0
- package/test/integration/MigrateData.test.ts +203 -0
- package/test/integration/Mock-Layer1-Layer0.test.ts +106 -0
- package/test/integration/MultipleEntryPointJoining.test.ts +105 -0
- package/test/integration/RecursiveFind.test.ts +50 -0
- package/test/integration/RemoteRouter.test.ts +83 -0
- package/test/integration/RemoteStore.test.ts +66 -0
- package/test/integration/RouteMessage.test.ts +254 -0
- package/test/integration/RpcErrors.test.ts +153 -0
- package/test/integration/ScaleDownDht.test.ts +66 -0
- package/test/integration/SimultaneousConnections.test.ts +308 -0
- package/test/integration/Store.test.ts +72 -0
- package/test/integration/StoreAndDelete.test.ts +93 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +71 -0
- package/test/integration/WebRtcConnectionManagement.test.ts +205 -0
- package/test/integration/WebRtcConnectorRpc.test.ts +145 -0
- package/test/integration/WebSocket.test.ts +64 -0
- package/test/integration/WebSocketConnectionManagement.test.ts +131 -0
- package/test/integration/WebSocketConnectorRpc.test.ts +86 -0
- package/test/kademlia-simulation/data/nodeids.json +13002 -0
- package/test/kademlia-simulation/data/orderedneighbors.json +1001 -0
- package/test/unit/AddressTools.test.ts +40 -0
- package/test/unit/DuplicateDetector.test.ts +29 -0
- package/test/unit/LocalDataStore.test.ts +107 -0
- package/test/unit/PeerID.test.ts +22 -0
- package/test/unit/ProtobufMessage.test.ts +21 -0
- package/test/unit/RandomContactList.test.ts +87 -0
- package/test/unit/RecursiveFinder.test.ts +112 -0
- package/test/unit/Router.test.ts +124 -0
- package/test/unit/SortedContactList.test.ts +127 -0
- package/test/unit/UUID.test.ts +49 -0
- package/test/unit/WebSocketServer.test.ts +42 -0
- package/test/utils/mock/RecursiveFinder.ts +19 -0
- package/test/utils/mock/Router.ts +53 -0
- package/test/utils/mock/Transport.ts +26 -0
- package/test/utils/utils.ts +311 -0
- package/tsconfig.browser.json +15 -0
- package/tsconfig.jest.json +19 -0
- package/tsconfig.json +3 -0
- package/tsconfig.node.json +18 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
2
|
+
import { LatencyType, Simulator } from '../../src/connection/Simulator/Simulator'
|
|
3
|
+
import { Message, MessageType, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
4
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
5
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
6
|
+
import { ConnectionType } from '../../src/connection/IConnection'
|
|
7
|
+
import { ITransport } from '../../src/transport/ITransport'
|
|
8
|
+
import * as Err from '../../src/helpers/errors'
|
|
9
|
+
import { SimulatorTransport } from '../../src/exports'
|
|
10
|
+
|
|
11
|
+
describe('WebRTC Connection Management', () => {
|
|
12
|
+
|
|
13
|
+
let manager1: ConnectionManager
|
|
14
|
+
let manager2: ConnectionManager
|
|
15
|
+
|
|
16
|
+
let simulator: Simulator
|
|
17
|
+
|
|
18
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
19
|
+
kademliaId: PeerID.fromString('peer1').value,
|
|
20
|
+
nodeName: 'peer1',
|
|
21
|
+
type: NodeType.NODEJS,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
25
|
+
kademliaId: PeerID.fromString('peer2').value,
|
|
26
|
+
nodeName: 'peer2',
|
|
27
|
+
type: NodeType.NODEJS,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let connectorTransport1: ITransport
|
|
31
|
+
let connectorTransport2: ITransport
|
|
32
|
+
|
|
33
|
+
beforeEach(async () => {
|
|
34
|
+
|
|
35
|
+
simulator = new Simulator(LatencyType.FIXED, 500)
|
|
36
|
+
|
|
37
|
+
connectorTransport1 = new SimulatorTransport(peerDescriptor1, simulator)
|
|
38
|
+
manager1 = new ConnectionManager({ transportLayer: connectorTransport1, nodeName: peerDescriptor1.nodeName })
|
|
39
|
+
|
|
40
|
+
connectorTransport2 = new SimulatorTransport(peerDescriptor2, simulator)
|
|
41
|
+
manager2 = new ConnectionManager({ transportLayer: connectorTransport2, nodeName: peerDescriptor2.nodeName })
|
|
42
|
+
|
|
43
|
+
await manager1.start((_msg) => peerDescriptor1)
|
|
44
|
+
await manager2.start((_msg) => peerDescriptor2)
|
|
45
|
+
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
afterEach(async () => {
|
|
49
|
+
await Promise.all([
|
|
50
|
+
manager1.stop(),
|
|
51
|
+
manager2.stop(),
|
|
52
|
+
connectorTransport1.stop(),
|
|
53
|
+
connectorTransport2.stop()
|
|
54
|
+
])
|
|
55
|
+
simulator.stop()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const serviceId = 'dummy'
|
|
59
|
+
|
|
60
|
+
// TODO: fix flaky test, ticket NET-911
|
|
61
|
+
it('Peer1 can open WebRTC Datachannels', (done) => {
|
|
62
|
+
const dummyMessage: Message = {
|
|
63
|
+
serviceId: 'unknown',
|
|
64
|
+
body: {
|
|
65
|
+
oneofKind: 'rpcMessage',
|
|
66
|
+
rpcMessage: RpcMessage.create()
|
|
67
|
+
},
|
|
68
|
+
messageType: MessageType.RPC,
|
|
69
|
+
messageId: 'mockerer'
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
manager2.on('message', (message: Message) => {
|
|
73
|
+
expect(message.messageId).toEqual('mockerer')
|
|
74
|
+
expect(manager1.getConnection(peerDescriptor2)!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
75
|
+
expect(manager2.getConnection(peerDescriptor1)!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
76
|
+
|
|
77
|
+
done()
|
|
78
|
+
})
|
|
79
|
+
dummyMessage.targetDescriptor = peerDescriptor2
|
|
80
|
+
manager1.send(dummyMessage).catch((e) => {
|
|
81
|
+
throw e
|
|
82
|
+
})
|
|
83
|
+
}, 60000)
|
|
84
|
+
|
|
85
|
+
it('Peer2 can open WebRTC Datachannel', (done) => {
|
|
86
|
+
const dummyMessage: Message = {
|
|
87
|
+
serviceId: serviceId,
|
|
88
|
+
body: {
|
|
89
|
+
oneofKind: 'rpcMessage',
|
|
90
|
+
rpcMessage: RpcMessage.create()
|
|
91
|
+
},
|
|
92
|
+
messageType: MessageType.RPC,
|
|
93
|
+
messageId: 'mockerer'
|
|
94
|
+
}
|
|
95
|
+
manager1.on('message', (message: Message) => {
|
|
96
|
+
expect(message.messageId).toEqual('mockerer')
|
|
97
|
+
expect(manager1.getConnection(peerDescriptor2)!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
98
|
+
expect(manager2.getConnection(peerDescriptor1)!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
99
|
+
|
|
100
|
+
done()
|
|
101
|
+
})
|
|
102
|
+
dummyMessage.targetDescriptor = peerDescriptor1
|
|
103
|
+
manager2.send(dummyMessage)
|
|
104
|
+
}, 60000)
|
|
105
|
+
|
|
106
|
+
it('Connecting to self throws', async () => {
|
|
107
|
+
const dummyMessage: Message = {
|
|
108
|
+
serviceId: serviceId,
|
|
109
|
+
body: {
|
|
110
|
+
oneofKind: 'rpcMessage',
|
|
111
|
+
rpcMessage: RpcMessage.create()
|
|
112
|
+
},
|
|
113
|
+
messageType: MessageType.RPC,
|
|
114
|
+
messageId: 'mockerer'
|
|
115
|
+
}
|
|
116
|
+
dummyMessage.targetDescriptor = peerDescriptor1
|
|
117
|
+
await expect(manager1.send(dummyMessage))
|
|
118
|
+
.rejects
|
|
119
|
+
.toEqual(new Err.CannotConnectToSelf('Cannot send to self'))
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('Connects and disconnects webrtc connections', async () => {
|
|
123
|
+
const msg: Message = {
|
|
124
|
+
serviceId: serviceId,
|
|
125
|
+
messageType: MessageType.RPC,
|
|
126
|
+
messageId: '1',
|
|
127
|
+
body: {
|
|
128
|
+
oneofKind: 'rpcMessage',
|
|
129
|
+
rpcMessage: RpcMessage.create()
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const dataPromise = new Promise<void>((resolve, _reject) => {
|
|
134
|
+
manager2.on('message', async (message: Message) => {
|
|
135
|
+
expect(message.messageType).toBe(MessageType.RPC)
|
|
136
|
+
resolve()
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const connectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
141
|
+
manager1.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
142
|
+
//expect(message.messageType).toBe(MessageType.RPC)
|
|
143
|
+
resolve()
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
const connectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
148
|
+
manager2.on('connected', (_peerDescriptor: PeerDescriptor) => {
|
|
149
|
+
resolve()
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const disconnectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
154
|
+
manager1.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
155
|
+
resolve()
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const disconnectedPromise2 = new Promise<void>((resolve, _reject) => {
|
|
160
|
+
manager2.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
161
|
+
resolve()
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
msg.targetDescriptor = peerDescriptor2
|
|
166
|
+
manager1.send(msg).catch((_e) => { })
|
|
167
|
+
|
|
168
|
+
await Promise.all([dataPromise, connectedPromise1, connectedPromise2])
|
|
169
|
+
|
|
170
|
+
// @ts-expect-error private field
|
|
171
|
+
manager1.closeConnection(peerDescriptor2)
|
|
172
|
+
|
|
173
|
+
await Promise.all([disconnectedPromise1, disconnectedPromise2])
|
|
174
|
+
|
|
175
|
+
}, 20000)
|
|
176
|
+
|
|
177
|
+
it('Disconnects webrtcconnection while being connected', async () => {
|
|
178
|
+
const msg: Message = {
|
|
179
|
+
serviceId: serviceId,
|
|
180
|
+
messageType: MessageType.RPC,
|
|
181
|
+
messageId: '1',
|
|
182
|
+
body: {
|
|
183
|
+
oneofKind: 'rpcMessage',
|
|
184
|
+
rpcMessage: RpcMessage.create()
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const disconnectedPromise1 = new Promise<void>((resolve, _reject) => {
|
|
189
|
+
manager1.on('disconnected', (_peerDescriptor: PeerDescriptor) => {
|
|
190
|
+
resolve()
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
msg.targetDescriptor = peerDescriptor2
|
|
195
|
+
manager1.send(msg).catch((e) => {
|
|
196
|
+
expect(e.code).toEqual('CONNECTION_FAILED')
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
// @ts-expect-error private field
|
|
200
|
+
manager1.closeConnection(peerDescriptor2)
|
|
201
|
+
|
|
202
|
+
await disconnectedPromise1
|
|
203
|
+
|
|
204
|
+
}, 20000)
|
|
205
|
+
})
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { ProtoRpcClient, RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
|
+
import { WebRtcConnectorServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
3
|
+
import {
|
|
4
|
+
IceCandidate,
|
|
5
|
+
PeerDescriptor,
|
|
6
|
+
RtcAnswer,
|
|
7
|
+
RtcOffer,
|
|
8
|
+
WebRtcConnectionRequest
|
|
9
|
+
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
10
|
+
import { Empty } from '../../src/proto/google/protobuf/empty'
|
|
11
|
+
import { generateId } from '../utils/utils'
|
|
12
|
+
import { IWebRtcConnectorService } from '../../src/proto/packages/dht/protos/DhtRpc.server'
|
|
13
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
14
|
+
import { DhtCallContext } from '../../src/rpc-protocol/DhtCallContext'
|
|
15
|
+
import { waitForCondition } from '@streamr/utils'
|
|
16
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
17
|
+
|
|
18
|
+
describe('WebRTC rpc messages', () => {
|
|
19
|
+
let rpcCommunicator1: RpcCommunicator
|
|
20
|
+
let rpcCommunicator2: RpcCommunicator
|
|
21
|
+
let client: ProtoRpcClient<WebRtcConnectorServiceClient>
|
|
22
|
+
|
|
23
|
+
let requestConnectionCounter: number
|
|
24
|
+
let rtcOfferCounter: number
|
|
25
|
+
let rtcAnswerCounter: number
|
|
26
|
+
let iceCandidateCounter: number
|
|
27
|
+
|
|
28
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
29
|
+
kademliaId: generateId('peer1'),
|
|
30
|
+
type: 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
34
|
+
kademliaId: generateId('peer2'),
|
|
35
|
+
type: 0
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
requestConnectionCounter = 0
|
|
40
|
+
rtcOfferCounter = 0
|
|
41
|
+
rtcAnswerCounter = 0
|
|
42
|
+
iceCandidateCounter = 0
|
|
43
|
+
|
|
44
|
+
rpcCommunicator1 = new RpcCommunicator()
|
|
45
|
+
const serverFunctions: IWebRtcConnectorService = {
|
|
46
|
+
|
|
47
|
+
requestConnection: async (_urequest: WebRtcConnectionRequest, _context: ServerCallContext): Promise<Empty> => {
|
|
48
|
+
requestConnectionCounter += 1
|
|
49
|
+
const res: Empty = {}
|
|
50
|
+
return res
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
rtcOffer: async (_urequest: RtcOffer, _context: ServerCallContext): Promise<Empty> => {
|
|
54
|
+
rtcOfferCounter += 1
|
|
55
|
+
const res: Empty = {}
|
|
56
|
+
return res
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
rtcAnswer: async (_urequest: RtcAnswer, _context: ServerCallContext): Promise<Empty> => {
|
|
60
|
+
rtcAnswerCounter += 1
|
|
61
|
+
const res: Empty = {}
|
|
62
|
+
return res
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
iceCandidate: async (_urequest: IceCandidate, _context: ServerCallContext): Promise<Empty> => {
|
|
66
|
+
iceCandidateCounter += 1
|
|
67
|
+
const res: Empty = {}
|
|
68
|
+
return res
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
rpcCommunicator2 = new RpcCommunicator()
|
|
73
|
+
rpcCommunicator2.registerRpcNotification(RtcOffer, 'rtcOffer', serverFunctions.rtcOffer)
|
|
74
|
+
rpcCommunicator2.registerRpcNotification(RtcAnswer, 'rtcAnswer', serverFunctions.rtcAnswer)
|
|
75
|
+
rpcCommunicator2.registerRpcNotification(IceCandidate, 'iceCandidate', serverFunctions.iceCandidate)
|
|
76
|
+
rpcCommunicator2.registerRpcNotification(WebRtcConnectionRequest, 'requestConnection', serverFunctions.requestConnection)
|
|
77
|
+
|
|
78
|
+
rpcCommunicator1.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
79
|
+
rpcCommunicator2.handleIncomingMessage(message)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
rpcCommunicator2.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
83
|
+
rpcCommunicator1.handleIncomingMessage(message)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
client = toProtoRpcClient(new WebRtcConnectorServiceClient(rpcCommunicator1.getRpcClientTransport()))
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
afterEach(async () => {
|
|
90
|
+
await rpcCommunicator1.stop()
|
|
91
|
+
await rpcCommunicator2.stop()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('send connectionRequest', async () => {
|
|
95
|
+
client.requestConnection({
|
|
96
|
+
requester: peerDescriptor1,
|
|
97
|
+
target: peerDescriptor2,
|
|
98
|
+
connectionId: 'connectionRequest'
|
|
99
|
+
},
|
|
100
|
+
{ targetDescriptor: peerDescriptor2, notification: true }
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
await waitForCondition(() => requestConnectionCounter === 1)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('send rtcOffer', async () => {
|
|
107
|
+
client.rtcOffer({
|
|
108
|
+
requester: peerDescriptor1,
|
|
109
|
+
target: peerDescriptor2,
|
|
110
|
+
connectionId: 'rtcOffer',
|
|
111
|
+
description: 'aaaaaa'
|
|
112
|
+
},
|
|
113
|
+
{ targetDescriptor: peerDescriptor2, notification: true }
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
await waitForCondition(() => rtcOfferCounter === 1)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('send rtcAnswer', async () => {
|
|
120
|
+
client.rtcAnswer({
|
|
121
|
+
requester: peerDescriptor1,
|
|
122
|
+
target: peerDescriptor2,
|
|
123
|
+
connectionId: 'rtcOffer',
|
|
124
|
+
description: 'aaaaaa'
|
|
125
|
+
},
|
|
126
|
+
{ targetDescriptor: peerDescriptor2, notification: true }
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
await waitForCondition(() => rtcAnswerCounter === 1)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('send iceCandidate', async () => {
|
|
133
|
+
client.iceCandidate({
|
|
134
|
+
requester: peerDescriptor1,
|
|
135
|
+
target: peerDescriptor2,
|
|
136
|
+
connectionId: 'rtcOffer',
|
|
137
|
+
candidate: 'aaaaaa',
|
|
138
|
+
mid: 'asdasdasdasdasd'
|
|
139
|
+
},
|
|
140
|
+
{ targetDescriptor: peerDescriptor2, notification: true }
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
await waitForCondition(() => iceCandidateCounter === 1)
|
|
144
|
+
})
|
|
145
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import { WebSocketServer } from '../../src/connection/WebSocket/WebSocketServer'
|
|
4
|
+
import { IConnection } from '../../src/connection/IConnection'
|
|
5
|
+
import { ClientWebSocket } from '../../src/connection/WebSocket/ClientWebSocket'
|
|
6
|
+
import { Logger } from '@streamr/utils'
|
|
7
|
+
|
|
8
|
+
const logger = new Logger(module)
|
|
9
|
+
|
|
10
|
+
describe('WebSocket', () => {
|
|
11
|
+
|
|
12
|
+
const webSocketServer = new WebSocketServer()
|
|
13
|
+
const clientWebSocket = new ClientWebSocket()
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
await webSocketServer.start(9999)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('Happy path', (done) => {
|
|
20
|
+
|
|
21
|
+
webSocketServer.on('connected', (serverConnection: IConnection) => {
|
|
22
|
+
const time = Date.now()
|
|
23
|
+
logger.info('server side sendind msg at ' + time)
|
|
24
|
+
serverConnection.send(Uint8Array.from([1, 2, 3, 4]))
|
|
25
|
+
|
|
26
|
+
const time2 = Date.now()
|
|
27
|
+
logger.info('server side setting listeners at ' + time2)
|
|
28
|
+
|
|
29
|
+
serverConnection.on('data', (bytes: Uint8Array) => {
|
|
30
|
+
const time = Date.now()
|
|
31
|
+
logger.info('server side receiving message at ' + time)
|
|
32
|
+
|
|
33
|
+
logger.info('server received:' + JSON.stringify(bytes))
|
|
34
|
+
|
|
35
|
+
expect(bytes.toString()).toBe('1,2,3,4')
|
|
36
|
+
logger.info('calling done()')
|
|
37
|
+
done()
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
clientWebSocket.on('connected', () => {
|
|
42
|
+
const time = Date.now()
|
|
43
|
+
logger.info('client side setting listeners at ' + time)
|
|
44
|
+
|
|
45
|
+
clientWebSocket.on('data', (bytes: Uint8Array) => {
|
|
46
|
+
const time = Date.now()
|
|
47
|
+
logger.info('client side receiving message at ' + time)
|
|
48
|
+
|
|
49
|
+
logger.info('client received: ' + JSON.stringify(bytes))
|
|
50
|
+
expect(bytes.toString()).toBe('1,2,3,4')
|
|
51
|
+
|
|
52
|
+
const time2 = Date.now()
|
|
53
|
+
logger.info('client side sendind msg at ' + time2)
|
|
54
|
+
clientWebSocket.send(Uint8Array.from([1, 2, 3, 4]))
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
clientWebSocket.connect('ws://127.0.0.1:9999')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
afterAll(async () => {
|
|
62
|
+
await webSocketServer.stop()
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* eslint-disable promise/no-nesting */
|
|
2
|
+
|
|
3
|
+
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
4
|
+
import { Simulator } from '../../src/connection/Simulator/Simulator'
|
|
5
|
+
import { Message, MessageType, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
6
|
+
import { PeerID } from '../../src/helpers/PeerID'
|
|
7
|
+
import { ConnectionType } from '../../src/connection/IConnection'
|
|
8
|
+
import { ITransport } from '../../src/transport/ITransport'
|
|
9
|
+
import * as Err from '../../src/helpers/errors'
|
|
10
|
+
import { waitForCondition } from '@streamr/utils'
|
|
11
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
12
|
+
|
|
13
|
+
describe('WebSocket Connection Management', () => {
|
|
14
|
+
|
|
15
|
+
const serviceId = 'test'
|
|
16
|
+
let wsServerManager: ConnectionManager
|
|
17
|
+
let noWsServerManager: ConnectionManager
|
|
18
|
+
|
|
19
|
+
const simulator = new Simulator()
|
|
20
|
+
|
|
21
|
+
const wsServerConnectorPeerDescriptor: PeerDescriptor = {
|
|
22
|
+
kademliaId: PeerID.fromString('peerWithServer').value,
|
|
23
|
+
type: NodeType.NODEJS,
|
|
24
|
+
websocket: {
|
|
25
|
+
ip: '127.0.0.1',
|
|
26
|
+
port: 12223
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const noWsServerConnectorPeerDescriptor: PeerDescriptor = {
|
|
31
|
+
kademliaId: PeerID.fromString('peerWithoutServer').value,
|
|
32
|
+
type: NodeType.NODEJS,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let connectorTransport1: ITransport
|
|
36
|
+
let connectorTransport2: ITransport
|
|
37
|
+
|
|
38
|
+
beforeEach(async () => {
|
|
39
|
+
|
|
40
|
+
connectorTransport1 = new ConnectionManager({ ownPeerDescriptor: wsServerConnectorPeerDescriptor, simulator: simulator })
|
|
41
|
+
connectorTransport2 = new ConnectionManager({ ownPeerDescriptor: noWsServerConnectorPeerDescriptor, simulator: simulator })
|
|
42
|
+
|
|
43
|
+
const config1 = {
|
|
44
|
+
transportLayer: connectorTransport1,
|
|
45
|
+
webSocketHost: '127.0.0.1',
|
|
46
|
+
webSocketPort: 12223,
|
|
47
|
+
}
|
|
48
|
+
const config2 = {
|
|
49
|
+
transportLayer: connectorTransport2
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
wsServerManager = new ConnectionManager(config1)
|
|
53
|
+
noWsServerManager = new ConnectionManager(config2)
|
|
54
|
+
|
|
55
|
+
await wsServerManager.start((_response) => wsServerConnectorPeerDescriptor)
|
|
56
|
+
await noWsServerManager.start((_response) => noWsServerConnectorPeerDescriptor)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
afterEach(async () => {
|
|
60
|
+
await wsServerManager.stop()
|
|
61
|
+
await noWsServerManager.stop()
|
|
62
|
+
await connectorTransport1.stop()
|
|
63
|
+
await connectorTransport2.stop()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('Can open connections to serverless peer', (done) => {
|
|
67
|
+
const dummyMessage: Message = {
|
|
68
|
+
serviceId: serviceId,
|
|
69
|
+
body: {
|
|
70
|
+
oneofKind: 'rpcMessage',
|
|
71
|
+
rpcMessage: RpcMessage.create()
|
|
72
|
+
},
|
|
73
|
+
messageType: MessageType.RPC,
|
|
74
|
+
messageId: 'mockerer',
|
|
75
|
+
targetDescriptor: noWsServerConnectorPeerDescriptor
|
|
76
|
+
}
|
|
77
|
+
noWsServerManager.on('message', (message: Message) => {
|
|
78
|
+
expect(message.messageId).toEqual('mockerer')
|
|
79
|
+
expect(wsServerManager.getConnection(noWsServerConnectorPeerDescriptor)!.connectionType).toEqual(ConnectionType.WEBSOCKET_SERVER)
|
|
80
|
+
expect(noWsServerManager.getConnection(wsServerConnectorPeerDescriptor)!.connectionType).toEqual(ConnectionType.WEBSOCKET_CLIENT)
|
|
81
|
+
|
|
82
|
+
done()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
wsServerManager.send(dummyMessage)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('Can open connections to peer with server', async () => {
|
|
89
|
+
const dummyMessage: Message = {
|
|
90
|
+
serviceId: serviceId,
|
|
91
|
+
body: {
|
|
92
|
+
oneofKind: 'rpcMessage',
|
|
93
|
+
rpcMessage: RpcMessage.create()
|
|
94
|
+
},
|
|
95
|
+
messageType: MessageType.RPC,
|
|
96
|
+
messageId: 'mockerer',
|
|
97
|
+
targetDescriptor: wsServerConnectorPeerDescriptor
|
|
98
|
+
}
|
|
99
|
+
await noWsServerManager.send(dummyMessage)
|
|
100
|
+
await waitForCondition(
|
|
101
|
+
() => {
|
|
102
|
+
return (!!wsServerManager.getConnection(noWsServerConnectorPeerDescriptor)
|
|
103
|
+
&& wsServerManager.getConnection(noWsServerConnectorPeerDescriptor)!.connectionType === ConnectionType.WEBSOCKET_SERVER)
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
await waitForCondition(
|
|
107
|
+
() => noWsServerManager.getConnection(wsServerConnectorPeerDescriptor)!.connectionType === ConnectionType.WEBSOCKET_CLIENT
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('Connecting to self throws', async () => {
|
|
112
|
+
const dummyMessage: Message = {
|
|
113
|
+
serviceId: serviceId,
|
|
114
|
+
body: {
|
|
115
|
+
oneofKind: 'rpcMessage',
|
|
116
|
+
rpcMessage: RpcMessage.create()
|
|
117
|
+
},
|
|
118
|
+
messageType: MessageType.RPC,
|
|
119
|
+
messageId: 'mockerer',
|
|
120
|
+
targetDescriptor: noWsServerConnectorPeerDescriptor
|
|
121
|
+
}
|
|
122
|
+
await expect(noWsServerManager.send(dummyMessage))
|
|
123
|
+
.rejects
|
|
124
|
+
.toEqual(new Err.CannotConnectToSelf('Cannot send to self'))
|
|
125
|
+
|
|
126
|
+
dummyMessage.targetDescriptor = wsServerConnectorPeerDescriptor
|
|
127
|
+
await expect(wsServerManager.send(dummyMessage))
|
|
128
|
+
.rejects
|
|
129
|
+
.toEqual(new Err.CannotConnectToSelf('Cannot send to self'))
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ProtoRpcClient, RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
|
+
import { WebSocketConnectorServiceClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
3
|
+
import { generateId } from '../utils/utils'
|
|
4
|
+
import {
|
|
5
|
+
PeerDescriptor,
|
|
6
|
+
WebSocketConnectionRequest,
|
|
7
|
+
WebSocketConnectionResponse
|
|
8
|
+
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
9
|
+
import { MockWebSocketConnectorRpc } from '../utils/utils'
|
|
10
|
+
import { DhtCallContext } from '../../src/rpc-protocol/DhtCallContext'
|
|
11
|
+
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
12
|
+
|
|
13
|
+
describe('WebSocketConnectorRpc', () => {
|
|
14
|
+
let rpcCommunicator1: RpcCommunicator
|
|
15
|
+
let rpcCommunicator2: RpcCommunicator
|
|
16
|
+
let client1: ProtoRpcClient<WebSocketConnectorServiceClient>
|
|
17
|
+
let client2: ProtoRpcClient<WebSocketConnectorServiceClient>
|
|
18
|
+
|
|
19
|
+
const peerDescriptor1: PeerDescriptor = {
|
|
20
|
+
kademliaId: generateId('peer1'),
|
|
21
|
+
type: 0
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const peerDescriptor2: PeerDescriptor = {
|
|
25
|
+
kademliaId: generateId('peer2'),
|
|
26
|
+
type: 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
rpcCommunicator1 = new RpcCommunicator()
|
|
31
|
+
rpcCommunicator1.registerRpcMethod(
|
|
32
|
+
WebSocketConnectionRequest,
|
|
33
|
+
WebSocketConnectionResponse,
|
|
34
|
+
'requestConnection',
|
|
35
|
+
MockWebSocketConnectorRpc.requestConnection
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
rpcCommunicator2 = new RpcCommunicator()
|
|
39
|
+
rpcCommunicator2.registerRpcMethod(
|
|
40
|
+
WebSocketConnectionRequest,
|
|
41
|
+
WebSocketConnectionResponse,
|
|
42
|
+
'requestConnection',
|
|
43
|
+
MockWebSocketConnectorRpc.requestConnection
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
rpcCommunicator1.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
47
|
+
rpcCommunicator2.handleIncomingMessage(message)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
rpcCommunicator2.on('outgoingMessage', (message: RpcMessage, _requestId: string, _ucallContext?: DhtCallContext) => {
|
|
51
|
+
rpcCommunicator1.handleIncomingMessage(message)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
client1 = toProtoRpcClient(new WebSocketConnectorServiceClient(rpcCommunicator1.getRpcClientTransport()))
|
|
55
|
+
client2 = toProtoRpcClient(new WebSocketConnectorServiceClient(rpcCommunicator2.getRpcClientTransport()))
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
afterEach(async () => {
|
|
59
|
+
await rpcCommunicator1.stop()
|
|
60
|
+
await rpcCommunicator2.stop()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('Happy path', async () => {
|
|
64
|
+
const response1 = client1.requestConnection({
|
|
65
|
+
requester: peerDescriptor1,
|
|
66
|
+
target: peerDescriptor2,
|
|
67
|
+
ip: '127.0.0.1',
|
|
68
|
+
port: 9099
|
|
69
|
+
},
|
|
70
|
+
{ targetDescriptor: peerDescriptor2 },
|
|
71
|
+
)
|
|
72
|
+
const res1 = await response1
|
|
73
|
+
await (expect(res1.accepted)).toEqual(true)
|
|
74
|
+
|
|
75
|
+
const response2 = client2.requestConnection({
|
|
76
|
+
requester: peerDescriptor2,
|
|
77
|
+
target: peerDescriptor1,
|
|
78
|
+
ip: '127.0.0.1',
|
|
79
|
+
port: 9111
|
|
80
|
+
},
|
|
81
|
+
{ targetDescriptor: peerDescriptor1 },
|
|
82
|
+
)
|
|
83
|
+
const res2 = await response2
|
|
84
|
+
await (expect(res2.accepted)).toEqual(true)
|
|
85
|
+
})
|
|
86
|
+
})
|