@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,227 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { WebRtcConnectionEvents, IWebRtcConnection, RtcDescription } from './IWebRtcConnection'
|
|
3
|
+
import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
|
|
4
|
+
import { Logger } from '@streamr/utils'
|
|
5
|
+
import { DisconnectionType } from '../../transport/ITransport'
|
|
6
|
+
|
|
7
|
+
const logger = new Logger(module)
|
|
8
|
+
|
|
9
|
+
export const WEB_RTC_CLEANUP = new class {
|
|
10
|
+
// eslint-disable-next-line class-methods-use-this
|
|
11
|
+
cleanUp(): void {
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Events = WebRtcConnectionEvents & ConnectionEvents
|
|
16
|
+
|
|
17
|
+
export class NodeWebRtcConnection extends EventEmitter<Events> implements IWebRtcConnection, IConnection {
|
|
18
|
+
|
|
19
|
+
public connectionId: ConnectionID = new ConnectionID()
|
|
20
|
+
public readonly connectionType: ConnectionType = ConnectionType.WEBRTC
|
|
21
|
+
|
|
22
|
+
// We need to keep track of connection state ourselves because
|
|
23
|
+
// RTCPeerConnection.connectionState is not supported on Firefox
|
|
24
|
+
|
|
25
|
+
private lastState: RTCPeerConnectionState = 'connecting'
|
|
26
|
+
|
|
27
|
+
private iceServers = []
|
|
28
|
+
private peerConnection?: RTCPeerConnection
|
|
29
|
+
private dataChannel?: RTCDataChannel
|
|
30
|
+
private makingOffer = false
|
|
31
|
+
private isOffering = false
|
|
32
|
+
private closed = false
|
|
33
|
+
|
|
34
|
+
public start(isOffering: boolean): void {
|
|
35
|
+
this.isOffering = isOffering
|
|
36
|
+
const urls: RTCIceServer[] = this.iceServers.map(({ url, port, username, password }) => ({
|
|
37
|
+
urls: `${url}:${port}`,
|
|
38
|
+
username,
|
|
39
|
+
credential: password
|
|
40
|
+
}))
|
|
41
|
+
this.peerConnection = new RTCPeerConnection({ iceServers: urls })
|
|
42
|
+
|
|
43
|
+
this.peerConnection.onicecandidate = (event) => {
|
|
44
|
+
if (event.candidate && event.candidate.sdpMid) {
|
|
45
|
+
this.emit('localCandidate', event.candidate.candidate, event.candidate.sdpMid)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.peerConnection.onicegatheringstatechange = () => {
|
|
50
|
+
logger.trace(`conn.onGatheringStateChange: ${this.peerConnection?.iceGatheringState}`)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isOffering) {
|
|
54
|
+
this.peerConnection.onnegotiationneeded = async () => {
|
|
55
|
+
try {
|
|
56
|
+
if (this.peerConnection) {
|
|
57
|
+
this.makingOffer = true
|
|
58
|
+
try {
|
|
59
|
+
await this.peerConnection.setLocalDescription()
|
|
60
|
+
} catch (err) {
|
|
61
|
+
logger.warn('error', { err })
|
|
62
|
+
}
|
|
63
|
+
if (this.peerConnection.localDescription) {
|
|
64
|
+
this.emit('localDescription', this.peerConnection.localDescription?.sdp, this.peerConnection.localDescription?.type)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger.error('error', { err })
|
|
69
|
+
} finally {
|
|
70
|
+
this.makingOffer = false
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const dataChannel = this.peerConnection.createDataChannel('streamrDataChannel')
|
|
75
|
+
this.setupDataChannel(dataChannel)
|
|
76
|
+
} else {
|
|
77
|
+
this.peerConnection.ondatachannel = (event) => {
|
|
78
|
+
this.setupDataChannel(event.channel)
|
|
79
|
+
logger.trace('connection.onDataChannel')
|
|
80
|
+
this.openDataChannel(event.channel)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public async setRemoteDescription(description: string, type: string): Promise<void> {
|
|
86
|
+
const offerCollision = (type.toLowerCase() === RtcDescription.OFFER) && (this.makingOffer || !this.peerConnection ||
|
|
87
|
+
this.peerConnection.signalingState != 'stable')
|
|
88
|
+
|
|
89
|
+
const ignoreOffer = this.isOffering && offerCollision
|
|
90
|
+
if (ignoreOffer) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
await this.peerConnection?.setRemoteDescription({ sdp: description, type: type.toLowerCase() as RTCSdpType })
|
|
95
|
+
} catch (err) {
|
|
96
|
+
logger.warn('error', { err })
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (type.toLowerCase() === RtcDescription.OFFER && this.peerConnection) {
|
|
100
|
+
try {
|
|
101
|
+
await this.peerConnection.setLocalDescription()
|
|
102
|
+
} catch (err) {
|
|
103
|
+
logger.warn('error', { err })
|
|
104
|
+
}
|
|
105
|
+
if (this.peerConnection.localDescription) {
|
|
106
|
+
this.emit('localDescription', this.peerConnection.localDescription.sdp, this.peerConnection.localDescription.type)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public addRemoteCandidate(candidate: string, mid: string): void {
|
|
112
|
+
try {
|
|
113
|
+
this.peerConnection?.addIceCandidate({ candidate: candidate, sdpMid: mid }).then(() => { return }).catch((err: any) => {
|
|
114
|
+
logger.warn('error', { err })
|
|
115
|
+
})
|
|
116
|
+
} catch (err) {
|
|
117
|
+
logger.warn('error', { err })
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public isOpen(): boolean {
|
|
122
|
+
return this.lastState === 'connected'
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// IConnection implementation
|
|
126
|
+
|
|
127
|
+
public async close(disconnectionType: DisconnectionType, reason?: string): Promise<void> {
|
|
128
|
+
this.doClose(disconnectionType, reason)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private doClose(disconnectionType: DisconnectionType, reason?: string): void {
|
|
132
|
+
if (!this.closed) {
|
|
133
|
+
this.closed = true
|
|
134
|
+
this.lastState = 'closed'
|
|
135
|
+
|
|
136
|
+
this.stopListening()
|
|
137
|
+
this.emit('disconnected', disconnectionType, undefined, reason)
|
|
138
|
+
|
|
139
|
+
this.removeAllListeners()
|
|
140
|
+
|
|
141
|
+
if (this.dataChannel) {
|
|
142
|
+
try {
|
|
143
|
+
this.dataChannel.close()
|
|
144
|
+
} catch (e) {
|
|
145
|
+
logger.warn(`dc.close() errored: ${e}`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.dataChannel = undefined
|
|
150
|
+
|
|
151
|
+
if (this.peerConnection) {
|
|
152
|
+
try {
|
|
153
|
+
this.peerConnection.close()
|
|
154
|
+
} catch (e) {
|
|
155
|
+
logger.warn(`conn.close() errored: ${e}`)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
this.peerConnection = undefined
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public destroy(): void {
|
|
164
|
+
this.removeAllListeners()
|
|
165
|
+
this.doClose('OTHER')
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public send(data: Uint8Array): void {
|
|
169
|
+
if (this.lastState === 'connected') {
|
|
170
|
+
this.dataChannel?.send(data as Buffer)
|
|
171
|
+
} else {
|
|
172
|
+
logger.warn('Tried to send on a connection with last state ' + this.lastState)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private setupDataChannel(dataChannel: RTCDataChannel): void {
|
|
177
|
+
dataChannel.onopen = () => {
|
|
178
|
+
logger.trace('dc.onOpen')
|
|
179
|
+
this.openDataChannel(dataChannel)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
dataChannel.onclose = () => {
|
|
183
|
+
logger.trace('dc.onClosed')
|
|
184
|
+
this.doClose('OTHER')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
dataChannel.onerror = (err) => {
|
|
188
|
+
logger.warn(`dc.onError: ${err}`)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
dataChannel.onbufferedamountlow = () => {
|
|
192
|
+
//this.emitLowBackpressure()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
dataChannel.onmessage = (msg) => {
|
|
196
|
+
logger.trace('dc.onmessage')
|
|
197
|
+
this.emit('data', new Uint8Array(msg.data))
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private stopListening() {
|
|
202
|
+
if (this.dataChannel) {
|
|
203
|
+
this.dataChannel.onopen = null
|
|
204
|
+
this.dataChannel.onclose = null
|
|
205
|
+
this.dataChannel.onerror = null
|
|
206
|
+
this.dataChannel.onbufferedamountlow = null
|
|
207
|
+
this.dataChannel.onmessage = null
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (this.peerConnection) {
|
|
211
|
+
this.peerConnection.onicecandidate = null
|
|
212
|
+
this.peerConnection.onicegatheringstatechange = null
|
|
213
|
+
this.peerConnection.onnegotiationneeded = null
|
|
214
|
+
this.peerConnection.ondatachannel = null
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private openDataChannel(dataChannel: RTCDataChannel): void {
|
|
219
|
+
this.dataChannel = dataChannel
|
|
220
|
+
this.lastState = 'connected'
|
|
221
|
+
this.emit('connected')
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public setConnectionId(connectionID: string): void {
|
|
225
|
+
this.connectionId = new ConnectionID(connectionID)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface WebRtcConnectionEvents {
|
|
2
|
+
localDescription: (description: string, type: string) => void
|
|
3
|
+
localCandidate: (candidate: string, mid: string) => void
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export enum RtcDescription {
|
|
7
|
+
OFFER = 'offer',
|
|
8
|
+
ANSWER = 'answer'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface IWebRtcConnection {
|
|
12
|
+
start(isOffering: boolean): void
|
|
13
|
+
|
|
14
|
+
on(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
15
|
+
on(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
16
|
+
once(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
17
|
+
once(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
18
|
+
off(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
19
|
+
off(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
20
|
+
|
|
21
|
+
setRemoteDescription(description: string, type: string): Promise<void>
|
|
22
|
+
addRemoteCandidate(candidate: string, mid: string): void
|
|
23
|
+
isOpen(): boolean
|
|
24
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { IWebRtcConnection, WebRtcConnectionEvents } from './IWebRtcConnection'
|
|
2
|
+
import { ConnectionType, IConnection, ConnectionID, ConnectionEvents } from '../IConnection'
|
|
3
|
+
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
|
+
import EventEmitter from 'eventemitter3'
|
|
5
|
+
import nodeDatachannel, { DataChannel, DescriptionType, PeerConnection } from 'node-datachannel'
|
|
6
|
+
import { Logger } from '@streamr/utils'
|
|
7
|
+
import { IllegalRTCPeerConnectionState } from '../../helpers/errors'
|
|
8
|
+
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
9
|
+
import { DisconnectionType } from '../../transport/ITransport'
|
|
10
|
+
import { iceServerAsString } from './iceServerAsString'
|
|
11
|
+
import { IceServer } from './WebRtcConnector'
|
|
12
|
+
|
|
13
|
+
const logger = new Logger(module)
|
|
14
|
+
|
|
15
|
+
const MAX_MESSAGE_SIZE = 1048576
|
|
16
|
+
|
|
17
|
+
export const WEB_RTC_CLEANUP = new class {
|
|
18
|
+
// eslint-disable-next-line class-methods-use-this
|
|
19
|
+
cleanUp(): void {
|
|
20
|
+
nodeDatachannel.cleanup()
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Params {
|
|
25
|
+
remotePeerDescriptor: PeerDescriptor
|
|
26
|
+
bufferThresholdHigh?: number
|
|
27
|
+
bufferThresholdLow?: number
|
|
28
|
+
connectingTimeout?: number
|
|
29
|
+
iceServers?: IceServer[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Re-defined accoring to https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts
|
|
33
|
+
// because importing single dom definitions in not possible
|
|
34
|
+
|
|
35
|
+
enum RTCPeerConnectionStateEnum {
|
|
36
|
+
closed = 'closed',
|
|
37
|
+
connected = 'connected',
|
|
38
|
+
connecting = 'connecting',
|
|
39
|
+
disconnected = 'disconnected',
|
|
40
|
+
failed = 'failed',
|
|
41
|
+
new = 'new'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
nodeDatachannel.initLogger('Fatal')
|
|
45
|
+
|
|
46
|
+
type RTCPeerConnectionState = keyof typeof RTCPeerConnectionStateEnum
|
|
47
|
+
|
|
48
|
+
type Events = WebRtcConnectionEvents & ConnectionEvents
|
|
49
|
+
|
|
50
|
+
export class NodeWebRtcConnection extends EventEmitter<Events> implements IConnection, IWebRtcConnection {
|
|
51
|
+
|
|
52
|
+
public connectionId: ConnectionID
|
|
53
|
+
private connection?: PeerConnection
|
|
54
|
+
private dataChannel?: DataChannel
|
|
55
|
+
private lastState: RTCPeerConnectionState = 'connecting'
|
|
56
|
+
private remoteDescriptionSet = false
|
|
57
|
+
private connectingTimeoutRef?: NodeJS.Timeout
|
|
58
|
+
|
|
59
|
+
public readonly connectionType: ConnectionType = ConnectionType.WEBRTC
|
|
60
|
+
private readonly iceServers: IceServer[]
|
|
61
|
+
private readonly bufferThresholdHigh: number // TODO: buffer handling must be implemented before production use
|
|
62
|
+
private readonly bufferThresholdLow: number
|
|
63
|
+
private readonly connectingTimeout: number
|
|
64
|
+
private readonly remotePeerDescriptor: PeerDescriptor
|
|
65
|
+
private closed = false
|
|
66
|
+
|
|
67
|
+
constructor(params: Params) {
|
|
68
|
+
super()
|
|
69
|
+
this.connectionId = new ConnectionID()
|
|
70
|
+
this.iceServers = params.iceServers || []
|
|
71
|
+
this.bufferThresholdHigh = params.bufferThresholdHigh || 2 ** 17
|
|
72
|
+
this.bufferThresholdLow = params.bufferThresholdLow || 2 ** 15
|
|
73
|
+
this.connectingTimeout = params.connectingTimeout || 20000
|
|
74
|
+
this.remotePeerDescriptor = params.remotePeerDescriptor
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public start(isOffering: boolean): void {
|
|
78
|
+
logger.trace(`Staring new connection for peer: ${this.remotePeerDescriptor.kademliaId.toString()}`)
|
|
79
|
+
const hexId = keyFromPeerDescriptor(this.remotePeerDescriptor)
|
|
80
|
+
logger.trace(`Staring new connection for peer: ${hexId} offering: ${isOffering}`)
|
|
81
|
+
this.connection = new PeerConnection(hexId, {
|
|
82
|
+
iceServers: this.iceServers.map(iceServerAsString),
|
|
83
|
+
maxMessageSize: MAX_MESSAGE_SIZE
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
this.connectingTimeoutRef = setTimeout(() => {
|
|
87
|
+
logger.trace('connectingTimeout, this.closed === ' + this.closed)
|
|
88
|
+
this.doClose('OTHER')
|
|
89
|
+
}, this.connectingTimeout)
|
|
90
|
+
|
|
91
|
+
this.connection.onStateChange((state: string) => this.onStateChange(state))
|
|
92
|
+
this.connection.onGatheringStateChange((_state: string) => {})
|
|
93
|
+
|
|
94
|
+
this.connection.onLocalDescription((description: string, type: DescriptionType) => {
|
|
95
|
+
this.emit('localDescription', description, type.toString())
|
|
96
|
+
})
|
|
97
|
+
this.connection.onLocalCandidate((candidate: string, mid: string) => {
|
|
98
|
+
this.emit('localCandidate', candidate, mid)
|
|
99
|
+
})
|
|
100
|
+
if (isOffering) {
|
|
101
|
+
const dataChannel = this.connection.createDataChannel('streamrDataChannel')
|
|
102
|
+
this.setupDataChannel(dataChannel)
|
|
103
|
+
} else {
|
|
104
|
+
this.connection.onDataChannel((dataChannel) => this.onDataChannel(dataChannel))
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public async setRemoteDescription(description: string, type: string): Promise<void> {
|
|
109
|
+
if (this.connection) {
|
|
110
|
+
try {
|
|
111
|
+
logger.trace(`Setting remote descriptor for peer: ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
112
|
+
this.connection!.setRemoteDescription(description, type as DescriptionType)
|
|
113
|
+
this.remoteDescriptionSet = true
|
|
114
|
+
} catch (err) {
|
|
115
|
+
logger.warn(`Failed to set remote descriptor for peer ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
this.doClose('OTHER', `Tried to set description for non-existent connection`)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public addRemoteCandidate(candidate: string, mid: string): void {
|
|
123
|
+
if (this.connection) {
|
|
124
|
+
if (this.remoteDescriptionSet) {
|
|
125
|
+
try {
|
|
126
|
+
logger.trace(`Setting remote candidate for peer: ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
127
|
+
this.connection!.addRemoteCandidate(candidate, mid)
|
|
128
|
+
} catch (err) {
|
|
129
|
+
logger.warn(`Failed to set remote candidate for peer ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`)
|
|
130
|
+
this.doClose('OTHER')
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
this.doClose('OTHER', `Tried to set candidate before description`)
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
this.doClose('OTHER', `Tried to set candidate for non-existent connection`)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public send(data: Uint8Array): void {
|
|
141
|
+
if (this.isOpen()) {
|
|
142
|
+
try {
|
|
143
|
+
this.dataChannel!.sendMessageBinary(data as Buffer)
|
|
144
|
+
} catch (err) {
|
|
145
|
+
logger.warn('Failed to send binary message to ' + keyFromPeerDescriptor(this.remotePeerDescriptor) + err)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public async close(disconnectionType: DisconnectionType, reason?: string): Promise<void> {
|
|
151
|
+
this.doClose(disconnectionType, reason)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private doClose(disconnectionType: DisconnectionType, reason?: string): void {
|
|
155
|
+
if (!this.closed) {
|
|
156
|
+
logger.trace(
|
|
157
|
+
`Closing Node WebRTC Connection to ${keyFromPeerDescriptor(this.remotePeerDescriptor)}`
|
|
158
|
+
+ `${reason ? `, reason: ${reason}` : ''}`
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
this.closed = true
|
|
162
|
+
|
|
163
|
+
this.emit('disconnected', disconnectionType, undefined, reason)
|
|
164
|
+
this.removeAllListeners()
|
|
165
|
+
|
|
166
|
+
if (this.connectingTimeoutRef) {
|
|
167
|
+
clearTimeout(this.connectingTimeoutRef)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (this.dataChannel) {
|
|
171
|
+
try {
|
|
172
|
+
logger.trace('closing datachannel')
|
|
173
|
+
this.dataChannel.close()
|
|
174
|
+
} catch (e) {
|
|
175
|
+
logger.warn('dc.close() errored: %s', e)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (this.connection) {
|
|
180
|
+
try {
|
|
181
|
+
this.connection.close()
|
|
182
|
+
} catch (e) {
|
|
183
|
+
logger.warn('conn.close() errored: %s', e)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public destroy(): void {
|
|
190
|
+
this.removeAllListeners()
|
|
191
|
+
this.doClose('OTHER')
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private onDataChannel(dataChannel: DataChannel): void {
|
|
195
|
+
this.openDataChannel(dataChannel)
|
|
196
|
+
this.setupDataChannel(dataChannel)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private setupDataChannel(dataChannel: DataChannel): void {
|
|
200
|
+
dataChannel.setBufferedAmountLowThreshold(this.bufferThresholdLow)
|
|
201
|
+
dataChannel.onOpen(() => {
|
|
202
|
+
logger.trace(`dc.onOpened`)
|
|
203
|
+
this.openDataChannel(dataChannel)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
dataChannel.onClosed(() => {
|
|
207
|
+
logger.trace(`dc.closed`)
|
|
208
|
+
this.doClose('OTHER', 'DataChannel closed')
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
dataChannel.onError((err) => logger.error('error', { err }))
|
|
212
|
+
|
|
213
|
+
dataChannel.onBufferedAmountLow(() => {
|
|
214
|
+
logger.trace(`dc.onBufferedAmountLow`)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
dataChannel.onMessage((msg) => {
|
|
218
|
+
logger.trace(`dc.onMessage`)
|
|
219
|
+
this.emit('data', msg as Buffer)
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private openDataChannel(dataChannel: DataChannel): void {
|
|
224
|
+
if (this.connectingTimeoutRef) {
|
|
225
|
+
clearTimeout(this.connectingTimeoutRef)
|
|
226
|
+
}
|
|
227
|
+
this.dataChannel = dataChannel
|
|
228
|
+
logger.trace(`DataChannel opened for peer ${this.remotePeerDescriptor.kademliaId.toString()}`)
|
|
229
|
+
this.emit('connected')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private onStateChange(state: string): void {
|
|
233
|
+
logger.trace('onStateChange ' + state)
|
|
234
|
+
if (!Object.keys(RTCPeerConnectionStateEnum).filter((s) => isNaN(+s)).includes(state)) {
|
|
235
|
+
throw new IllegalRTCPeerConnectionState('NodeWebRtcConnection used an unknown state: ' + state)
|
|
236
|
+
} else {
|
|
237
|
+
this.lastState = state as RTCPeerConnectionState
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (state === RTCPeerConnectionStateEnum.closed
|
|
241
|
+
|| state === RTCPeerConnectionStateEnum.disconnected
|
|
242
|
+
|| state === RTCPeerConnectionStateEnum.failed
|
|
243
|
+
) {
|
|
244
|
+
this.doClose('OTHER')
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
isOpen(): boolean {
|
|
250
|
+
return !this.closed && this.lastState === 'connected' && !!this.dataChannel
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public setConnectionId(connectionID: string): void {
|
|
254
|
+
this.connectionId = new ConnectionID(connectionID)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IceCandidate,
|
|
3
|
+
PeerDescriptor,
|
|
4
|
+
RtcAnswer,
|
|
5
|
+
RtcOffer,
|
|
6
|
+
WebRtcConnectionRequest
|
|
7
|
+
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
8
|
+
import { IWebRtcConnectorServiceClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
9
|
+
import { DhtRpcOptions } from '../../rpc-protocol/DhtRpcOptions'
|
|
10
|
+
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
11
|
+
import { Logger } from '@streamr/utils'
|
|
12
|
+
|
|
13
|
+
const logger = new Logger(module)
|
|
14
|
+
|
|
15
|
+
export class RemoteWebrtcConnector {
|
|
16
|
+
|
|
17
|
+
private peerDescriptor: PeerDescriptor
|
|
18
|
+
private client: ProtoRpcClient<IWebRtcConnectorServiceClient>
|
|
19
|
+
|
|
20
|
+
constructor(peerDescriptor: PeerDescriptor, client: ProtoRpcClient<IWebRtcConnectorServiceClient>) {
|
|
21
|
+
this.peerDescriptor = peerDescriptor
|
|
22
|
+
this.client = client
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
requestConnection(sourceDescriptor: PeerDescriptor, connectionId: string): void {
|
|
26
|
+
const request: WebRtcConnectionRequest = {
|
|
27
|
+
target: this.peerDescriptor,
|
|
28
|
+
requester: sourceDescriptor,
|
|
29
|
+
connectionId
|
|
30
|
+
}
|
|
31
|
+
const options: DhtRpcOptions = {
|
|
32
|
+
sourceDescriptor: sourceDescriptor as PeerDescriptor,
|
|
33
|
+
targetDescriptor: this.peerDescriptor as PeerDescriptor,
|
|
34
|
+
notification: true
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.client.requestConnection(request, options).catch((_e) => {
|
|
38
|
+
logger.trace('Failed to send requestConnection')
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
sendRtcOffer(sourceDescriptor: PeerDescriptor, description: string, connectionId: string): void {
|
|
43
|
+
const request: RtcOffer = {
|
|
44
|
+
target: this.peerDescriptor,
|
|
45
|
+
requester: sourceDescriptor,
|
|
46
|
+
connectionId,
|
|
47
|
+
description
|
|
48
|
+
}
|
|
49
|
+
const options: DhtRpcOptions = {
|
|
50
|
+
sourceDescriptor: sourceDescriptor as PeerDescriptor,
|
|
51
|
+
targetDescriptor: this.peerDescriptor as PeerDescriptor,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.client.rtcOffer(request, options).catch((_e) => {
|
|
55
|
+
logger.trace('Failed to send rtcOffer')
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
sendRtcAnswer(sourceDescriptor: PeerDescriptor, description: string, connectionId: string): void {
|
|
60
|
+
const request: RtcAnswer = {
|
|
61
|
+
target: this.peerDescriptor,
|
|
62
|
+
requester: sourceDescriptor,
|
|
63
|
+
connectionId,
|
|
64
|
+
description
|
|
65
|
+
}
|
|
66
|
+
const options: DhtRpcOptions = {
|
|
67
|
+
sourceDescriptor: sourceDescriptor as PeerDescriptor,
|
|
68
|
+
targetDescriptor: this.peerDescriptor as PeerDescriptor,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.client.rtcAnswer(request, options).catch((_e) => {
|
|
72
|
+
logger.trace('Failed to send rtcAnswer')
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
sendIceCandidate(sourceDescriptor: PeerDescriptor, candidate: string, mid: string, connectionId: string): void {
|
|
77
|
+
const request: IceCandidate = {
|
|
78
|
+
target: this.peerDescriptor,
|
|
79
|
+
requester: sourceDescriptor,
|
|
80
|
+
connectionId,
|
|
81
|
+
mid,
|
|
82
|
+
candidate
|
|
83
|
+
}
|
|
84
|
+
const options: DhtRpcOptions = {
|
|
85
|
+
sourceDescriptor: sourceDescriptor as PeerDescriptor,
|
|
86
|
+
targetDescriptor: this.peerDescriptor as PeerDescriptor,
|
|
87
|
+
}
|
|
88
|
+
this.client.iceCandidate(request, options).catch((_e) => {
|
|
89
|
+
logger.trace('Failed to send iceCandidate')
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|