@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,173 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConnectivityRequest, ConnectivityResponse,
|
|
3
|
+
Message, MessageType, PeerDescriptor
|
|
4
|
+
} from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { ConnectionEvents, IConnection } from './IConnection'
|
|
6
|
+
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
7
|
+
import * as Err from '../helpers/errors'
|
|
8
|
+
import { ClientWebSocket } from './WebSocket/ClientWebSocket'
|
|
9
|
+
import { v4 } from 'uuid'
|
|
10
|
+
import { NatType } from './ConnectionManager'
|
|
11
|
+
import { ServerWebSocket } from './WebSocket/ServerWebSocket'
|
|
12
|
+
|
|
13
|
+
const logger = new Logger(module)
|
|
14
|
+
|
|
15
|
+
// Class for handling both client and server side of the connectivity
|
|
16
|
+
// checks. This is attached to all ServerWebSockets to listen to
|
|
17
|
+
// ConnectivityRequest messages.
|
|
18
|
+
|
|
19
|
+
export enum ConnectionMode { REQUEST = 'connectivityRequest', PROBE = 'connectivityProbe' }
|
|
20
|
+
export class ConnectivityChecker {
|
|
21
|
+
|
|
22
|
+
private static readonly CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivitychecker'
|
|
23
|
+
private static readonly CONNECTIVITY_CHECKER_TIMEOUT = 5000
|
|
24
|
+
private stopped = false
|
|
25
|
+
private webSocketPort?: number
|
|
26
|
+
|
|
27
|
+
constructor(webSocketPort?: number) {
|
|
28
|
+
this.webSocketPort = webSocketPort
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async sendConnectivityRequest(entryPoint: PeerDescriptor): Promise<ConnectivityResponse> {
|
|
32
|
+
let outgoingConnection: IConnection
|
|
33
|
+
try {
|
|
34
|
+
outgoingConnection = await this.connectAsync({
|
|
35
|
+
host: entryPoint.websocket?.ip, port: entryPoint.websocket?.port, timeoutMs: 1000,
|
|
36
|
+
mode: ConnectionMode.REQUEST
|
|
37
|
+
})
|
|
38
|
+
} catch (e) {
|
|
39
|
+
logger.error('Failed to connect to the entrypoints')
|
|
40
|
+
throw new Err.ConnectionFailed('Failed to connect to the entrypoints', e)
|
|
41
|
+
}
|
|
42
|
+
// send connectivity request
|
|
43
|
+
const connectivityRequestMessage: ConnectivityRequest = { port: this.webSocketPort! }
|
|
44
|
+
const msg: Message = {
|
|
45
|
+
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
46
|
+
messageType: MessageType.CONNECTIVITY_REQUEST, messageId: v4(),
|
|
47
|
+
body: {
|
|
48
|
+
oneofKind: 'connectivityRequest',
|
|
49
|
+
connectivityRequest: connectivityRequestMessage
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const responseAwaiter = () => {
|
|
53
|
+
return new Promise((resolve: (res: ConnectivityResponse) => void, reject) => {
|
|
54
|
+
const timeoutId = setTimeout(() => {
|
|
55
|
+
outgoingConnection.close('OTHER')
|
|
56
|
+
reject(new Err.ConnectivityResponseTimeout('timeout'))
|
|
57
|
+
}, ConnectivityChecker.CONNECTIVITY_CHECKER_TIMEOUT)
|
|
58
|
+
const listener = (bytes: Uint8Array) => {
|
|
59
|
+
outgoingConnection.close('OTHER')
|
|
60
|
+
const message: Message = Message.fromBinary(bytes)
|
|
61
|
+
if (message.body.oneofKind === 'connectivityResponse') {
|
|
62
|
+
const connectivityResponseMessage = message.body.connectivityResponse
|
|
63
|
+
outgoingConnection!.off('data', listener)
|
|
64
|
+
clearTimeout(timeoutId)
|
|
65
|
+
resolve(connectivityResponseMessage)
|
|
66
|
+
} else {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
outgoingConnection!.on('data', listener)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const retPromise = responseAwaiter()
|
|
75
|
+
logger.trace('trying to send connectivity request')
|
|
76
|
+
outgoingConnection.send(Message.toBinary(msg))
|
|
77
|
+
logger.debug('connectivity request sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
78
|
+
return await retPromise
|
|
79
|
+
} catch (e) {
|
|
80
|
+
logger.error('error getting connectivityresponse')
|
|
81
|
+
throw e
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public listenToIncomingConnectivityRequests(connectionToListenTo: ServerWebSocket): void {
|
|
86
|
+
connectionToListenTo.on('data', (data: Uint8Array) => {
|
|
87
|
+
logger.trace('server received data')
|
|
88
|
+
const message = Message.fromBinary(data)
|
|
89
|
+
if (message.body.oneofKind === 'connectivityRequest') {
|
|
90
|
+
logger.trace('received connectivity request')
|
|
91
|
+
this.handleIncomingConnectivityRequest(connectionToListenTo, message.body.connectivityRequest).then(() => {
|
|
92
|
+
logger.trace('handleIncomingConnectivityRequest ok')
|
|
93
|
+
return
|
|
94
|
+
}).catch((e) => {
|
|
95
|
+
logger.error('handleIncomingConnectivityRequest' + e)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async handleIncomingConnectivityRequest(
|
|
102
|
+
connection: ServerWebSocket,
|
|
103
|
+
connectivityRequest: ConnectivityRequest
|
|
104
|
+
): Promise<void> {
|
|
105
|
+
if (this.stopped) {
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
let outgoingConnection: IConnection | undefined
|
|
109
|
+
let connectivityResponseMessage: ConnectivityResponse | undefined
|
|
110
|
+
try {
|
|
111
|
+
outgoingConnection = await this.connectAsync({
|
|
112
|
+
host: connection.getRemoteAddress(),
|
|
113
|
+
port: connectivityRequest.port, timeoutMs: 1000, mode: ConnectionMode.PROBE
|
|
114
|
+
})
|
|
115
|
+
} catch (err) {
|
|
116
|
+
logger.debug('error', { err })
|
|
117
|
+
connectivityResponseMessage = {
|
|
118
|
+
openInternet: false,
|
|
119
|
+
ip: (connection as ServerWebSocket).getRemoteAddress(),
|
|
120
|
+
natType: NatType.UNKNOWN
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (outgoingConnection) {
|
|
124
|
+
outgoingConnection.close('OTHER')
|
|
125
|
+
logger.trace('Connectivity test produced positive result, communicating reply to the requester')
|
|
126
|
+
connectivityResponseMessage = {
|
|
127
|
+
openInternet: true,
|
|
128
|
+
ip: (connection as ServerWebSocket).getRemoteAddress(),
|
|
129
|
+
natType: NatType.OPEN_INTERNET,
|
|
130
|
+
websocket: { ip: (connection as ServerWebSocket).getRemoteAddress(), port: connectivityRequest.port }
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const msg: Message = {
|
|
134
|
+
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
135
|
+
messageType: MessageType.CONNECTIVITY_RESPONSE, messageId: v4(),
|
|
136
|
+
body: {
|
|
137
|
+
oneofKind: 'connectivityResponse',
|
|
138
|
+
connectivityResponse: connectivityResponseMessage!
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
connection.send(Message.toBinary(msg))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// eslint-disable-next-line class-methods-use-this
|
|
145
|
+
private async connectAsync({ host, port, url, timeoutMs, mode }:
|
|
146
|
+
{ host?: string, port?: number, url?: string, timeoutMs: number, mode: ConnectionMode } =
|
|
147
|
+
{ timeoutMs: 1000, mode: ConnectionMode.REQUEST }
|
|
148
|
+
): Promise<IConnection> {
|
|
149
|
+
const socket = new ClientWebSocket()
|
|
150
|
+
let address = ''
|
|
151
|
+
if (url) {
|
|
152
|
+
address = url
|
|
153
|
+
} else if (host && port) {
|
|
154
|
+
address = 'ws://' + host + ':' + port
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
address += '?' + mode + '=true'
|
|
158
|
+
|
|
159
|
+
let result: RunAndRaceEventsReturnType<ConnectionEvents>
|
|
160
|
+
try {
|
|
161
|
+
result = await runAndRaceEvents3<ConnectionEvents>([
|
|
162
|
+
() => { socket.connect(address) }],
|
|
163
|
+
socket, ['connected', 'error'],
|
|
164
|
+
timeoutMs)
|
|
165
|
+
} catch (e) {
|
|
166
|
+
throw (new Err.ConnectionFailed('WebSocket connection timed out'))
|
|
167
|
+
}
|
|
168
|
+
if (result.winnerName === 'error') {
|
|
169
|
+
throw (new Err.ConnectionFailed('Could not open WebSocket connection'))
|
|
170
|
+
}
|
|
171
|
+
return socket
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Logger } from '@streamr/utils'
|
|
2
|
+
import { EventEmitter } from 'eventemitter3'
|
|
3
|
+
import { v4 } from 'uuid'
|
|
4
|
+
import { Message, HandshakeRequest, HandshakeResponse, MessageType, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
+
import { IConnection } from './IConnection'
|
|
6
|
+
|
|
7
|
+
const logger = new Logger(module)
|
|
8
|
+
|
|
9
|
+
interface HandshakerEvents {
|
|
10
|
+
handshakeRequest: (peerDescriptor: PeerDescriptor) => void
|
|
11
|
+
handshakeCompleted: (peerDescriptor: PeerDescriptor) => void
|
|
12
|
+
handshakeFailed: (reason?: string) => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class Handshaker extends EventEmitter<HandshakerEvents> {
|
|
16
|
+
|
|
17
|
+
private static readonly HANDSHAKER_SERVICE_ID = 'system/handshaker'
|
|
18
|
+
private ownPeerDescriptor: PeerDescriptor
|
|
19
|
+
private protocolVersion: string
|
|
20
|
+
private connection: IConnection
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
ownPeerDescriptor: PeerDescriptor,
|
|
24
|
+
protocolVersion: string,
|
|
25
|
+
connection: IConnection
|
|
26
|
+
) {
|
|
27
|
+
super()
|
|
28
|
+
this.ownPeerDescriptor = ownPeerDescriptor
|
|
29
|
+
this.protocolVersion = protocolVersion
|
|
30
|
+
this.connection = connection
|
|
31
|
+
this.connection.on('data', this.onData)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private onData = (data: Uint8Array) => {
|
|
35
|
+
const message = Message.fromBinary(data)
|
|
36
|
+
if (message.body.oneofKind === 'handshakeRequest') {
|
|
37
|
+
logger.trace('handshake request received')
|
|
38
|
+
const handshake = message.body.handshakeRequest
|
|
39
|
+
this.emit('handshakeRequest', handshake.peerDescriptor!)
|
|
40
|
+
}
|
|
41
|
+
if (message.body.oneofKind === 'handshakeResponse') {
|
|
42
|
+
logger.trace('handshake response received')
|
|
43
|
+
const handshake = message.body.handshakeResponse
|
|
44
|
+
if (handshake.responseError) {
|
|
45
|
+
this.emit('handshakeFailed', handshake.responseError)
|
|
46
|
+
} else {
|
|
47
|
+
this.emit('handshakeCompleted', handshake.peerDescriptor!)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public sendHandshakeRequest(): void {
|
|
53
|
+
const outgoingHandshake: HandshakeRequest = {
|
|
54
|
+
sourceId: this.ownPeerDescriptor.kademliaId,
|
|
55
|
+
protocolVersion: this.protocolVersion,
|
|
56
|
+
peerDescriptor: this.ownPeerDescriptor
|
|
57
|
+
}
|
|
58
|
+
const msg: Message = {
|
|
59
|
+
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
60
|
+
messageType: MessageType.HANDSHAKE_REQUEST,
|
|
61
|
+
messageId: v4(),
|
|
62
|
+
body: {
|
|
63
|
+
oneofKind: 'handshakeRequest',
|
|
64
|
+
handshakeRequest: outgoingHandshake
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.connection.send(Message.toBinary(msg))
|
|
68
|
+
logger.trace('handshake request sent')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public sendHandshakeResponse(error?: string): void {
|
|
72
|
+
const outgoingHandshakeResponse: HandshakeResponse = {
|
|
73
|
+
sourceId: this.ownPeerDescriptor.kademliaId,
|
|
74
|
+
protocolVersion: this.protocolVersion,
|
|
75
|
+
peerDescriptor: this.ownPeerDescriptor
|
|
76
|
+
}
|
|
77
|
+
if (error) {
|
|
78
|
+
outgoingHandshakeResponse.responseError = error
|
|
79
|
+
}
|
|
80
|
+
const msg: Message = {
|
|
81
|
+
serviceId: Handshaker.HANDSHAKER_SERVICE_ID,
|
|
82
|
+
messageType: MessageType.HANDSHAKE_RESPONSE,
|
|
83
|
+
messageId: v4(),
|
|
84
|
+
body: {
|
|
85
|
+
oneofKind: 'handshakeResponse',
|
|
86
|
+
handshakeResponse: outgoingHandshakeResponse
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.connection.send(Message.toBinary(msg))
|
|
90
|
+
logger.trace('handshake request sent')
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { UUID } from '../helpers/UUID'
|
|
2
|
+
import { DisconnectionType } from '../transport/ITransport'
|
|
3
|
+
|
|
4
|
+
export interface ConnectionEvents {
|
|
5
|
+
data: (bytes: Uint8Array) => void
|
|
6
|
+
connected: () => void
|
|
7
|
+
disconnected: (disconnectionType: DisconnectionType, code?: number, reason?: string) => void
|
|
8
|
+
error: (name: string) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum ConnectionType {
|
|
12
|
+
WEBSOCKET_SERVER = 'websocket-server',
|
|
13
|
+
WEBSOCKET_CLIENT = 'websocket-client',
|
|
14
|
+
WEBRTC = 'webrtc',
|
|
15
|
+
SIMULATOR_SERVER = 'simulator-server',
|
|
16
|
+
SIMULATOR_CLIENT = 'simulator-client',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ConnectionIDKey = string & { readonly __brand: 'connectionIDKey' } // Nominal typing
|
|
20
|
+
|
|
21
|
+
export class ConnectionID extends UUID {
|
|
22
|
+
toMapKey(): ConnectionIDKey {
|
|
23
|
+
return this.toString() as ConnectionIDKey
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IConnection {
|
|
28
|
+
|
|
29
|
+
on(event: 'data', listener: (bytes: Uint8Array) => void): this
|
|
30
|
+
on(event: 'error', listener: (name: string) => void): this
|
|
31
|
+
on(event: 'connected', listener: () => void): this
|
|
32
|
+
on(event: 'disconnected', listener: (disconnectionType: DisconnectionType, code?: number, reason?: string) => void): this
|
|
33
|
+
|
|
34
|
+
once(event: 'data', listener: (bytes: Uint8Array) => void): this
|
|
35
|
+
once(event: 'error', listener: (name: string) => void): this
|
|
36
|
+
once(event: 'connected', listener: () => void): this
|
|
37
|
+
once(event: 'disconnected', listener: (disconnectionType: DisconnectionType, code?: number, reason?: string) => void): this
|
|
38
|
+
|
|
39
|
+
off(event: 'data', listener: (bytes: Uint8Array) => void): void
|
|
40
|
+
off(event: 'error', listener: (name: string) => void): void
|
|
41
|
+
off(event: 'connected', listener: () => void): void
|
|
42
|
+
off(event: 'disconnected', listener: (disconnectionType: DisconnectionType, code?: number, reason?: string) => void): void
|
|
43
|
+
|
|
44
|
+
send(data: Uint8Array): void
|
|
45
|
+
close(disconnectionType: DisconnectionType): Promise<void>
|
|
46
|
+
destroy(): void
|
|
47
|
+
}
|