@streamr/dht 100.0.0-pretestnet.6 → 100.0.0-rc.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/README.md +90 -8
- package/dist/package.json +70 -0
- package/dist/src/connection/Connection.d.ts +1 -0
- package/dist/src/connection/Connection.js +10 -3
- package/dist/src/connection/Connection.js.map +1 -1
- package/dist/src/connection/ConnectionLockHandler.d.ts +14 -14
- package/dist/src/connection/ConnectionLockHandler.js +22 -16
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +7 -6
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +4 -6
- package/dist/src/connection/ConnectionLockRpcRemote.js +10 -37
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +19 -20
- package/dist/src/connection/ConnectionManager.js +132 -117
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +3 -2
- package/dist/src/connection/ConnectorFacade.js +10 -3
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +3 -1
- package/dist/src/connection/Handshaker.js +18 -9
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/IConnection.d.ts +2 -7
- package/dist/src/connection/IConnection.js +1 -8
- package/dist/src/connection/IConnection.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +5 -7
- package/dist/src/connection/ManagedConnection.js +42 -26
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +130 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +3 -0
- package/dist/src/connection/connectivityRequestHandler.js +101 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.d.ts +0 -3
- package/dist/src/connection/simulator/Simulator.js +34 -69
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +27 -25
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.js +16 -12
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/simulator/pings.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +2 -3
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +40 -27
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +0 -2
- package/dist/src/connection/webrtc/WebrtcConnector.js +24 -25
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +27 -20
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +3 -6
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -5
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +1 -2
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +6 -4
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +14 -7
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +3 -9
- package/dist/src/connection/websocket/WebsocketConnector.js +142 -74
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +5 -3
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +9 -12
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -7
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +7 -44
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.d.ts +1 -4
- package/dist/src/connection/websocket/WebsocketServer.js +49 -40
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{ServerWebsocket.d.ts → WebsocketServerConnection.d.ts} +8 -4
- package/dist/src/connection/websocket/WebsocketServerConnection.js +97 -0
- package/dist/src/connection/websocket/WebsocketServerConnection.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +45 -49
- package/dist/src/dht/DhtNode.js +229 -317
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +10 -9
- package/dist/src/dht/DhtNodeRpcLocal.js +19 -11
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +15 -10
- package/dist/src/dht/DhtNodeRpcRemote.js +38 -21
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +7 -6
- package/dist/src/dht/ExternalApiRpcLocal.js +9 -13
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +6 -5
- package/dist/src/dht/ExternalApiRpcRemote.js +10 -7
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +61 -0
- package/dist/src/dht/PeerManager.js +288 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +2 -2
- package/dist/src/dht/contact/Contact.js +4 -3
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +8 -8
- package/dist/src/dht/contact/ContactList.js +12 -7
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +4 -4
- package/dist/src/dht/contact/RandomContactList.js +13 -12
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/RingContactList.d.ts +31 -0
- package/dist/src/dht/contact/RingContactList.js +133 -0
- package/dist/src/dht/contact/RingContactList.js.map +1 -0
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +8 -7
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +16 -13
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +29 -15
- package/dist/src/dht/contact/SortedContactList.js +79 -42
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/contact/ringIdentifiers.d.ts +16 -0
- package/dist/src/dht/contact/ringIdentifiers.js +54 -0
- package/dist/src/dht/contact/ringIdentifiers.js.map +1 -0
- package/dist/src/dht/discovery/DiscoverySession.d.ts +8 -18
- package/dist/src/dht/discovery/DiscoverySession.js +32 -48
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +16 -12
- package/dist/src/dht/discovery/PeerDiscovery.js +82 -39
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +29 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js +123 -0
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +36 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +166 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +14 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +26 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcRemote.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +44 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +187 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +14 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +20 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.d.ts +6 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js +21 -0
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/DuplicateDetector.d.ts +2 -4
- package/dist/src/dht/routing/DuplicateDetector.js +10 -15
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +11 -27
- package/dist/src/dht/routing/Router.js +92 -58
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -4
- package/dist/src/dht/routing/RouterRpcLocal.js +17 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcRemote.js +29 -20
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +29 -21
- package/dist/src/dht/routing/RoutingSession.js +93 -68
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/RoutingTablesCache.d.ts +24 -0
- package/dist/src/dht/routing/RoutingTablesCache.js +46 -0
- package/dist/src/dht/routing/RoutingTablesCache.js.map +1 -0
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +9 -14
- package/dist/src/dht/store/LocalDataStore.js +46 -72
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +33 -0
- package/dist/src/dht/store/StoreManager.js +182 -0
- package/dist/src/dht/store/StoreManager.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +11 -41
- package/dist/src/dht/store/StoreRpcLocal.js +27 -234
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +6 -7
- package/dist/src/dht/store/StoreRpcRemote.js +10 -20
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +5 -3
- package/dist/src/exports.js +10 -8
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/AddressTools.js +2 -0
- package/dist/src/helpers/AddressTools.js.map +1 -1
- package/dist/src/helpers/Connectivity.js.map +1 -1
- package/dist/src/helpers/MapWithTtl.d.ts +14 -0
- package/dist/src/helpers/MapWithTtl.js +60 -0
- package/dist/src/helpers/MapWithTtl.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptor.d.ts +3 -0
- package/dist/src/helpers/createPeerDescriptor.js +57 -0
- package/dist/src/helpers/createPeerDescriptor.js.map +1 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.d.ts +2 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js +30 -0
- package/dist/src/helpers/createPeerDescriptorSignaturePayload.js.map +1 -0
- package/dist/src/helpers/debugHelpers.js.map +1 -1
- package/dist/src/helpers/errors.js +2 -0
- package/dist/src/helpers/errors.js.map +1 -1
- package/dist/src/helpers/offering.d.ts +4 -0
- package/dist/src/helpers/offering.js +18 -0
- package/dist/src/helpers/offering.js.map +1 -0
- package/dist/src/helpers/protoClasses.js +2 -3
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/helpers/protoToString.js.map +1 -1
- package/dist/src/helpers/version.d.ts +6 -0
- package/dist/src/helpers/version.js +38 -0
- package/dist/src/helpers/version.js.map +1 -0
- package/dist/src/identifiers.d.ts +10 -0
- package/dist/src/identifiers.js +31 -0
- package/dist/src/identifiers.js.map +1 -0
- package/dist/src/proto/google/protobuf/any.js +8 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.js +2 -4
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.js +10 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +46 -49
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +63 -54
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +226 -232
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +146 -168
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +26 -29
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +2 -2
- package/dist/src/rpc-protocol/DhtCallContext.js +8 -0
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/rpc-protocol/DhtRpcOptions.d.ts +2 -1
- package/dist/src/transport/ITransport.d.ts +10 -2
- package/dist/src/transport/ITransport.js +5 -0
- package/dist/src/transport/ITransport.js.map +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.js +3 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +4 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +19 -12
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/jest.config.js +4 -1
- package/karma-setup.js +2 -0
- package/karma.config.js +13 -9
- package/package.json +16 -13
- package/protos/DhtRpc.proto +76 -76
- package/src/connection/Connection.ts +6 -1
- package/src/connection/ConnectionLockHandler.ts +30 -22
- package/src/connection/ConnectionLockRpcLocal.ts +7 -12
- package/src/connection/ConnectionLockRpcRemote.ts +9 -19
- package/src/connection/ConnectionManager.ts +132 -138
- package/src/connection/ConnectorFacade.ts +10 -9
- package/src/connection/Handshaker.ts +23 -11
- package/src/connection/IConnection.ts +2 -8
- package/src/connection/ManagedConnection.ts +34 -35
- package/src/connection/connectivityChecker.ts +109 -0
- package/src/connection/connectivityRequestHandler.ts +103 -0
- package/src/connection/simulator/Simulator.ts +6 -37
- package/src/connection/simulator/SimulatorConnection.ts +23 -25
- package/src/connection/simulator/SimulatorConnector.ts +11 -11
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +17 -25
- package/src/connection/webrtc/NodeWebrtcConnection.ts +24 -26
- package/src/connection/webrtc/WebrtcConnector.ts +18 -29
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +29 -24
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +4 -14
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/AutoCertifierClientFacade.ts +4 -6
- package/src/connection/websocket/ClientWebsocket.ts +10 -4
- package/src/connection/websocket/WebsocketConnector.ts +120 -97
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +13 -15
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -27
- package/src/connection/websocket/WebsocketServer.ts +50 -53
- package/src/connection/websocket/WebsocketServerConnection.ts +104 -0
- package/src/dht/DhtNode.ts +289 -410
- package/src/dht/DhtNodeRpcLocal.ts +25 -17
- package/src/dht/DhtNodeRpcRemote.ts +43 -27
- package/src/dht/ExternalApiRpcLocal.ts +30 -17
- package/src/dht/ExternalApiRpcRemote.ts +14 -11
- package/src/dht/PeerManager.ts +339 -0
- package/src/dht/contact/Contact.ts +4 -4
- package/src/dht/contact/ContactList.ts +11 -10
- package/src/dht/contact/RandomContactList.ts +15 -15
- package/src/dht/contact/RingContactList.ts +151 -0
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +16 -19
- package/src/dht/contact/SortedContactList.ts +120 -72
- package/src/dht/contact/ringIdentifiers.ts +62 -0
- package/src/dht/discovery/DiscoverySession.ts +36 -61
- package/src/dht/discovery/PeerDiscovery.ts +98 -43
- package/src/dht/discovery/RingDiscoverySession.ts +160 -0
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +246 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +34 -0
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +43 -0
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +232 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +35 -0
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +30 -0
- package/src/dht/routing/DuplicateDetector.ts +9 -21
- package/src/dht/routing/Router.ts +95 -92
- package/src/dht/routing/RouterRpcLocal.ts +16 -18
- package/src/dht/routing/RouterRpcRemote.ts +26 -24
- package/src/dht/routing/RoutingSession.ts +119 -98
- package/src/dht/routing/RoutingTablesCache.ts +58 -0
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +47 -77
- package/src/dht/store/StoreManager.ts +209 -0
- package/src/dht/store/StoreRpcLocal.ts +39 -308
- package/src/dht/store/StoreRpcRemote.ts +13 -31
- package/src/exports.ts +13 -3
- package/src/helpers/AddressTools.ts +2 -0
- package/src/helpers/MapWithTtl.ts +71 -0
- package/src/helpers/createPeerDescriptor.ts +57 -0
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +28 -0
- package/src/helpers/offering.ts +15 -0
- package/src/helpers/protoClasses.ts +4 -6
- package/src/helpers/version.ts +32 -0
- package/src/identifiers.ts +29 -0
- package/src/proto/google/protobuf/any.ts +4 -4
- package/src/proto/google/protobuf/empty.ts +2 -4
- package/src/proto/google/protobuf/timestamp.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +65 -68
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +27 -30
- package/src/proto/packages/dht/protos/DhtRpc.ts +297 -313
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/rpc-protocol/DhtCallContext.ts +2 -2
- package/src/rpc-protocol/DhtRpcOptions.ts +2 -1
- package/src/transport/ITransport.ts +11 -2
- package/src/transport/ListeningRpcCommunicator.ts +1 -1
- package/src/transport/RoutingRpcCommunicator.ts +21 -14
- package/test/RandomGraphSimulation.ts +3 -2
- package/test/benchmark/Find.test.ts +13 -28
- package/test/benchmark/KademliaCorrectness.test.ts +24 -28
- package/test/benchmark/RingCorrectness.test.ts +157 -0
- package/test/benchmark/SortedContactListBenchmark.test.ts +151 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +72 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +9 -9
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +11 -11
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +29 -25
- package/test/data/generateGroundTruthData.ts +7 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +11 -15
- package/test/end-to-end/Layer0.test.ts +19 -22
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +19 -21
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +26 -28
- package/test/end-to-end/Layer0Webrtc.test.ts +19 -19
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +10 -18
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +8 -15
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +2 -2
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +8 -9
- package/test/end-to-end/memory-leak.test.ts +19 -24
- package/test/integration/ConnectionLocking.test.ts +66 -60
- package/test/integration/ConnectionManager.test.ts +43 -63
- package/test/integration/ConnectivityChecking.test.ts +52 -0
- package/test/integration/DhtJoinPeerDiscovery.test.ts +8 -12
- package/test/integration/DhtNodeExternalAPI.test.ts +17 -21
- package/test/integration/DhtNodeRpcRemote.test.ts +19 -26
- package/test/integration/DhtRpc.test.ts +20 -24
- package/test/integration/Find.test.ts +10 -12
- package/test/integration/Layer1-scale.test.ts +25 -37
- package/test/integration/Mock-Layer1-Layer0.test.ts +39 -59
- package/test/integration/MultipleEntryPointJoining.test.ts +14 -14
- package/test/integration/ReplicateData.test.ts +106 -0
- package/test/integration/RouteMessage.test.ts +42 -68
- package/test/integration/RouterRpcRemote.test.ts +19 -24
- package/test/integration/ScaleDownDht.test.ts +14 -12
- package/test/integration/SimultaneousConnections.test.ts +112 -111
- package/test/integration/Store.test.ts +43 -27
- package/test/integration/StoreAndDelete.test.ts +36 -48
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +59 -0
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +17 -37
- package/test/integration/StoreRpcRemote.test.ts +20 -32
- package/test/integration/WebrtcConnectionManagement.test.ts +39 -25
- package/test/integration/WebrtcConnectorRpc.test.ts +6 -11
- package/test/integration/WebsocketConnectionManagement.test.ts +87 -22
- package/test/integration/WebsocketConnectorRpc.test.ts +14 -24
- package/test/integration/{RpcErrors.test.ts → rpc-connections-over-webrpc.test.ts} +15 -26
- package/test/unit/AddressTools.test.ts +4 -0
- package/test/unit/ConnectivityHelpers.test.ts +9 -17
- package/test/unit/DuplicateDetector.test.ts +8 -5
- package/test/unit/LocalDataStore.test.ts +78 -75
- package/test/unit/PeerManager.test.ts +33 -0
- package/test/unit/RandomContactList.test.ts +12 -9
- package/test/unit/RecursiveOperationManager.test.ts +157 -0
- package/test/unit/RecursiveOperationSession.test.ts +68 -0
- package/test/unit/Router.test.ts +52 -35
- package/test/unit/RoutingSession.test.ts +79 -0
- package/test/unit/SortedContactList.test.ts +61 -30
- package/test/unit/StoreManager.test.ts +138 -0
- package/test/unit/WebsocketConnector.test.ts +27 -35
- package/test/unit/connectivityRequestHandler.test.ts +104 -0
- package/test/unit/createPeerDescriptor.test.ts +69 -0
- package/test/unit/customMatchers.test.ts +16 -0
- package/test/unit/version.test.ts +18 -0
- package/test/utils/FakeTransport.ts +47 -0
- package/test/utils/customMatchers.ts +71 -0
- package/test/utils/mock/MockRpcCommunicator.ts +7 -0
- package/test/utils/mock/Router.ts +13 -3
- package/test/utils/mock/Transport.ts +1 -1
- package/test/utils/mock/mockDataEntry.ts +38 -0
- package/test/utils/utils.ts +104 -107
- package/tsconfig.browser.json +2 -1
- package/tsconfig.jest.json +4 -2
- package/tsconfig.node.json +4 -2
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/connection/websocket/ServerWebsocket.js +0 -100
- package/dist/src/connection/websocket/ServerWebsocket.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/dht/find/FindRpcLocal.d.ts +0 -14
- package/dist/src/dht/find/FindRpcLocal.js +0 -25
- package/dist/src/dht/find/FindRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSession.d.ts +0 -44
- package/dist/src/dht/find/FindSession.js +0 -145
- package/dist/src/dht/find/FindSession.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +0 -12
- package/dist/src/dht/find/FindSessionRpcLocal.js +0 -17
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +0 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +0 -6
- package/dist/src/dht/find/FindSessionRpcRemote.js +0 -21
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +0 -1
- package/dist/src/dht/find/Finder.d.ts +0 -49
- package/dist/src/dht/find/Finder.js +0 -184
- package/dist/src/dht/find/Finder.js.map +0 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +0 -6
- package/dist/src/dht/routing/FindRpcRemote.js +0 -41
- package/dist/src/dht/routing/FindRpcRemote.js.map +0 -1
- package/dist/src/helpers/PeerID.d.ts +0 -24
- package/dist/src/helpers/PeerID.js +0 -78
- package/dist/src/helpers/PeerID.js.map +0 -1
- package/dist/src/helpers/UUID.d.ts +0 -8
- package/dist/src/helpers/UUID.js +0 -36
- package/dist/src/helpers/UUID.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js +0 -14
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +0 -6
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +0 -23
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
- package/src/connection/websocket/ServerWebsocket.ts +0 -114
- package/src/dht/find/FindRpcLocal.ts +0 -35
- package/src/dht/find/FindSession.ts +0 -178
- package/src/dht/find/FindSessionRpcLocal.ts +0 -25
- package/src/dht/find/FindSessionRpcRemote.ts +0 -30
- package/src/dht/find/Finder.ts +0 -275
- package/src/dht/routing/FindRpcRemote.ts +0 -40
- package/src/helpers/PeerID.ts +0 -88
- package/src/helpers/UUID.ts +0 -35
- package/src/helpers/kademliaId.ts +0 -8
- package/src/helpers/peerIdFromPeerDescriptor.ts +0 -20
- package/test/integration/MigrateData.test.ts +0 -204
- package/test/unit/Finder.test.ts +0 -110
- package/test/unit/PeerID.test.ts +0 -22
- package/test/unit/UUID.test.ts +0 -55
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
2
|
import {
|
|
3
3
|
PeerDescriptor,
|
|
4
|
-
WebsocketConnectionRequest
|
|
5
|
-
WebsocketConnectionResponse
|
|
4
|
+
WebsocketConnectionRequest
|
|
6
5
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
7
6
|
import { IWebsocketConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
8
7
|
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
9
8
|
import { ManagedConnection } from '../ManagedConnection'
|
|
9
|
+
import { Empty } from '../../proto/google/protobuf/empty'
|
|
10
|
+
import { getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
11
|
+
import { DhtAddress } from '../../identifiers'
|
|
10
12
|
|
|
11
13
|
interface WebsocketConnectorRpcLocalConfig {
|
|
12
|
-
canConnect: (peerDescriptor: PeerDescriptor) => boolean
|
|
13
14
|
connect: (targetPeerDescriptor: PeerDescriptor) => ManagedConnection
|
|
15
|
+
hasConnection: (nodeId: DhtAddress) => boolean
|
|
14
16
|
onNewConnection: (connection: ManagedConnection) => boolean
|
|
15
17
|
abortSignal: AbortSignal
|
|
16
18
|
}
|
|
@@ -23,19 +25,15 @@ export class WebsocketConnectorRpcLocal implements IWebsocketConnectorRpc {
|
|
|
23
25
|
this.config = config
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
public async requestConnection(_request: WebsocketConnectionRequest, context: ServerCallContext): Promise<
|
|
28
|
+
public async requestConnection(_request: WebsocketConnectionRequest, context: ServerCallContext): Promise<Empty> {
|
|
29
|
+
if (this.config.abortSignal.aborted) {
|
|
30
|
+
return {}
|
|
31
|
+
}
|
|
27
32
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
28
|
-
if (this.config.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
const connection = this.config.connect(senderPeerDescriptor)
|
|
34
|
-
this.config.onNewConnection(connection)
|
|
35
|
-
})
|
|
36
|
-
return { accepted: true }
|
|
37
|
-
} else {
|
|
38
|
-
return { accepted: false }
|
|
33
|
+
if (!this.config.hasConnection(getNodeIdFromPeerDescriptor(senderPeerDescriptor))) {
|
|
34
|
+
const connection = this.config.connect(senderPeerDescriptor)
|
|
35
|
+
this.config.onNewConnection(connection)
|
|
39
36
|
}
|
|
37
|
+
return {}
|
|
40
38
|
}
|
|
41
39
|
}
|
|
@@ -1,39 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
|
-
PeerDescriptor,
|
|
3
2
|
WebsocketConnectionRequest
|
|
4
3
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import { IWebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
6
4
|
import { Logger } from '@streamr/utils'
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import { getNodeIdFromPeerDescriptor } from '../../
|
|
10
|
-
import { Remote } from '../../dht/contact/Remote'
|
|
5
|
+
import { RpcRemote } from '../../dht/contact/RpcRemote'
|
|
6
|
+
import { WebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
|
+
import { getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
11
8
|
|
|
12
9
|
const logger = new Logger(module)
|
|
13
10
|
|
|
14
|
-
export class WebsocketConnectorRpcRemote extends
|
|
11
|
+
export class WebsocketConnectorRpcRemote extends RpcRemote<WebsocketConnectorRpcClient> {
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
localPeerDescriptor: PeerDescriptor,
|
|
18
|
-
remotePeerDescriptor: PeerDescriptor,
|
|
19
|
-
client: ProtoRpcClient<IWebsocketConnectorRpcClient>
|
|
20
|
-
) {
|
|
21
|
-
super(localPeerDescriptor, remotePeerDescriptor, 'DUMMY', client)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async requestConnection(ip: string, port: number): Promise<boolean> {
|
|
13
|
+
async requestConnection(): Promise<void> {
|
|
25
14
|
logger.trace(`Requesting WebSocket connection from ${getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor())}`)
|
|
26
|
-
const request: WebsocketConnectionRequest = {
|
|
27
|
-
ip,
|
|
28
|
-
port
|
|
29
|
-
}
|
|
15
|
+
const request: WebsocketConnectionRequest = {}
|
|
30
16
|
const options = this.formDhtRpcOptions()
|
|
31
|
-
|
|
32
|
-
const res = await this.getClient().requestConnection(request, options)
|
|
33
|
-
return res.accepted
|
|
34
|
-
} catch (err) {
|
|
35
|
-
logger.debug(new Err.WebsocketConnectionRequestRejected('WebsocketConnectionRequest rejected', err).stack!)
|
|
36
|
-
return false
|
|
37
|
-
}
|
|
17
|
+
return this.getClient().requestConnection(request, options)
|
|
38
18
|
}
|
|
39
19
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createServer as createHttpServer, Server as HttpServer, IncomingMessage, ServerResponse } from 'http'
|
|
2
2
|
import { createServer as createHttpsServer, Server as HttpsServer } from 'https'
|
|
3
3
|
import EventEmitter from 'eventemitter3'
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
4
|
+
import WebSocket from 'ws'
|
|
5
|
+
import { WebsocketServerConnection } from './WebsocketServerConnection'
|
|
6
6
|
import { ConnectionSourceEvents } from '../IConnectionSource'
|
|
7
7
|
import { Logger, asAbortable } from '@streamr/utils'
|
|
8
8
|
import { createSelfSignedCertificate } from '@streamr/autocertifier-client'
|
|
@@ -10,17 +10,11 @@ import { WebsocketServerStartError } from '../../helpers/errors'
|
|
|
10
10
|
import { PortRange, TlsCertificate } from '../ConnectionManager'
|
|
11
11
|
import { range } from 'lodash'
|
|
12
12
|
import fs from 'fs'
|
|
13
|
-
import {
|
|
13
|
+
import { v4 as uuid } from 'uuid'
|
|
14
|
+
import { parse } from 'url'
|
|
14
15
|
|
|
15
16
|
const logger = new Logger(module)
|
|
16
17
|
|
|
17
|
-
// NodeJsWsServer is declared as a global in test-browser Electron tests
|
|
18
|
-
// in preload.js using "window.NodeJsWsServer = require('websocket').server".
|
|
19
|
-
// This is done in order to use the real nodejs websocket server in tests
|
|
20
|
-
// instead of a dummy polyfill.
|
|
21
|
-
|
|
22
|
-
declare class NodeJsWsServer extends WsServer { }
|
|
23
|
-
|
|
24
18
|
interface WebsocketServerConfig {
|
|
25
19
|
portRange: PortRange
|
|
26
20
|
enableTls: boolean
|
|
@@ -31,26 +25,20 @@ interface WebsocketServerConfig {
|
|
|
31
25
|
export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
32
26
|
|
|
33
27
|
private httpServer?: HttpServer | HttpsServer
|
|
34
|
-
private wsServer?:
|
|
28
|
+
private wsServer?: WebSocket.Server
|
|
35
29
|
private readonly abortController = new AbortController()
|
|
36
|
-
private readonly
|
|
37
|
-
private readonly tlsCertificate?: TlsCertificate
|
|
38
|
-
private readonly enableTls: boolean
|
|
39
|
-
private readonly maxMessageSize: number
|
|
30
|
+
private readonly config: WebsocketServerConfig
|
|
40
31
|
|
|
41
32
|
constructor(config: WebsocketServerConfig) {
|
|
42
33
|
super()
|
|
43
|
-
this.
|
|
44
|
-
this.enableTls = config.enableTls
|
|
45
|
-
this.tlsCertificate = config.tlsCertificate
|
|
46
|
-
this.maxMessageSize = config.maxMessageSize ?? 1048576
|
|
34
|
+
this.config = config
|
|
47
35
|
}
|
|
48
36
|
|
|
49
37
|
public async start(): Promise<number> {
|
|
50
|
-
const ports = range(this.portRange.min, this.portRange.max + 1)
|
|
38
|
+
const ports = range(this.config.portRange.min, this.config.portRange.max + 1)
|
|
51
39
|
for (const port of ports) {
|
|
52
40
|
try {
|
|
53
|
-
await asAbortable(this.startServer(port, this.enableTls), this.abortController.signal)
|
|
41
|
+
await asAbortable(this.startServer(port, this.config.enableTls), this.abortController.signal)
|
|
54
42
|
return port
|
|
55
43
|
} catch (err) {
|
|
56
44
|
if (err.originalError?.code === 'EADDRINUSE') {
|
|
@@ -60,7 +48,9 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
60
48
|
}
|
|
61
49
|
}
|
|
62
50
|
}
|
|
63
|
-
throw new WebsocketServerStartError(
|
|
51
|
+
throw new WebsocketServerStartError(
|
|
52
|
+
`Failed to start WebSocket server on any port in range: ${this.config.portRange.min}-${this.config.portRange.min}`
|
|
53
|
+
)
|
|
64
54
|
}
|
|
65
55
|
|
|
66
56
|
// If tlsCertificate has been given the tls boolean is ignored
|
|
@@ -72,15 +62,16 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
72
62
|
response.end()
|
|
73
63
|
}
|
|
74
64
|
return new Promise((resolve, reject) => {
|
|
75
|
-
if (this.tlsCertificate) {
|
|
65
|
+
if (this.config.tlsCertificate) {
|
|
76
66
|
this.httpServer = createHttpsServer({
|
|
77
|
-
key: fs.readFileSync(this.tlsCertificate.privateKeyFileName),
|
|
78
|
-
cert: fs.readFileSync(this.tlsCertificate.certFileName)
|
|
67
|
+
key: fs.readFileSync(this.config.tlsCertificate.privateKeyFileName),
|
|
68
|
+
cert: fs.readFileSync(this.config.tlsCertificate.certFileName)
|
|
79
69
|
}, requestListener)
|
|
80
70
|
} else if (!tls) {
|
|
81
71
|
this.httpServer = createHttpServer(requestListener)
|
|
82
72
|
} else {
|
|
83
|
-
|
|
73
|
+
// TODO use config option or named constant?
|
|
74
|
+
const certificate = createSelfSignedCertificate('streamr-self-signed-' + uuid(), 1000)
|
|
84
75
|
this.httpServer = createHttpsServer({
|
|
85
76
|
key: certificate.serverKey,
|
|
86
77
|
cert: certificate.serverCert
|
|
@@ -91,22 +82,26 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
91
82
|
return true
|
|
92
83
|
}
|
|
93
84
|
|
|
94
|
-
this.wsServer = this.createWsServer(
|
|
85
|
+
this.wsServer = this.createWsServer()
|
|
95
86
|
|
|
96
|
-
this.wsServer.on('
|
|
87
|
+
this.wsServer.on('connection', (ws: WebSocket, request: IncomingMessage) => {
|
|
88
|
+
logger.trace(`New connection from ${request.socket.remoteAddress}`)
|
|
97
89
|
if (!originIsAllowed()) {
|
|
98
90
|
// Make sure we only accept requests from an allowed origin
|
|
99
|
-
|
|
100
|
-
logger.trace('IConnection from origin ' + request.origin + ' rejected.')
|
|
91
|
+
ws.close()
|
|
92
|
+
logger.trace('IConnection from origin ' + request.headers.origin + ' rejected.')
|
|
101
93
|
return
|
|
102
94
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
logger.trace('IConnection accepted.')
|
|
95
|
+
this.emit('connected', new WebsocketServerConnection(ws, parse(request.url!), request.socket.remoteAddress!))
|
|
96
|
+
})
|
|
107
97
|
|
|
108
|
-
|
|
98
|
+
this.httpServer.on('upgrade', (request, socket, head) => {
|
|
99
|
+
logger.trace('Received upgrade request for ' + request.url)
|
|
100
|
+
this.wsServer!.handleUpgrade(request, socket, head, (ws: WebSocket) => {
|
|
101
|
+
this.wsServer!.emit('connection', ws, request)
|
|
102
|
+
})
|
|
109
103
|
})
|
|
104
|
+
|
|
110
105
|
this.httpServer.once('error', (err: Error) => {
|
|
111
106
|
reject(new WebsocketServerStartError('Starting Websocket server failed', err))
|
|
112
107
|
})
|
|
@@ -136,28 +131,30 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
136
131
|
this.abortController.abort()
|
|
137
132
|
this.removeAllListeners()
|
|
138
133
|
return new Promise((resolve, _reject) => {
|
|
139
|
-
this.wsServer
|
|
140
|
-
this.
|
|
134
|
+
this.wsServer!.close()
|
|
135
|
+
for (const ws of this.wsServer!.clients) {
|
|
136
|
+
ws.terminate()
|
|
137
|
+
}
|
|
138
|
+
this.httpServer?.once('close', () => {
|
|
139
|
+
// removeAllListeners is maybe not needed?
|
|
140
|
+
this.httpServer?.removeAllListeners()
|
|
141
141
|
resolve()
|
|
142
142
|
})
|
|
143
|
+
this.httpServer?.close()
|
|
144
|
+
// the close method "Stops the server from accepting new connections and closes all
|
|
145
|
+
// connections connected to this server which are not sending a request or waiting for a
|
|
146
|
+
// response." (https://nodejs.org/api/http.html#serverclosecallback)
|
|
147
|
+
// i.e. we need to call closeAllConnections() to close the rest of the connections
|
|
148
|
+
// (in practice this closes the active websocket connections)
|
|
149
|
+
this.httpServer?.closeAllConnections()
|
|
143
150
|
})
|
|
144
151
|
}
|
|
145
152
|
|
|
146
|
-
private createWsServer(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
autoAcceptConnections: false,
|
|
153
|
-
maxReceivedMessageSize: this.maxMessageSize
|
|
154
|
-
})
|
|
155
|
-
} else {
|
|
156
|
-
return this.wsServer = new WsServer({
|
|
157
|
-
httpServer,
|
|
158
|
-
autoAcceptConnections: false,
|
|
159
|
-
maxReceivedMessageSize: this.maxMessageSize
|
|
160
|
-
})
|
|
161
|
-
}
|
|
153
|
+
private createWsServer(): WebSocket.Server {
|
|
154
|
+
const maxPayload = this.config.maxMessageSize ?? 1048576
|
|
155
|
+
return this.wsServer = new WebSocket.Server({
|
|
156
|
+
noServer: true,
|
|
157
|
+
maxPayload
|
|
158
|
+
})
|
|
162
159
|
}
|
|
163
160
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
|
|
3
|
+
import WebSocket from 'ws'
|
|
4
|
+
import { Logger } from '@streamr/utils'
|
|
5
|
+
import { Url } from 'url'
|
|
6
|
+
import { CUSTOM_GOING_AWAY, GOING_AWAY } from './ClientWebsocket'
|
|
7
|
+
import { createRandomConnectionId } from '../Connection'
|
|
8
|
+
|
|
9
|
+
const logger = new Logger(module)
|
|
10
|
+
|
|
11
|
+
export class WebsocketServerConnection extends EventEmitter<ConnectionEvents> implements IConnection {
|
|
12
|
+
|
|
13
|
+
public readonly connectionId: ConnectionID
|
|
14
|
+
public readonly connectionType = ConnectionType.WEBSOCKET_SERVER
|
|
15
|
+
public readonly resourceURL: Url
|
|
16
|
+
public readonly remoteIpAddress: string
|
|
17
|
+
private socket?: WebSocket
|
|
18
|
+
private stopped = false
|
|
19
|
+
|
|
20
|
+
constructor(socket: WebSocket, resourceURL: Url, remoteAddress: string) {
|
|
21
|
+
super()
|
|
22
|
+
|
|
23
|
+
this.onMessage = this.onMessage.bind(this)
|
|
24
|
+
this.onClose = this.onClose.bind(this)
|
|
25
|
+
this.onError = this.onError.bind(this)
|
|
26
|
+
|
|
27
|
+
this.resourceURL = resourceURL
|
|
28
|
+
this.connectionId = createRandomConnectionId()
|
|
29
|
+
this.remoteIpAddress = remoteAddress
|
|
30
|
+
|
|
31
|
+
socket.on('message', this.onMessage)
|
|
32
|
+
socket.on('close', this.onClose)
|
|
33
|
+
socket.on('error', this.onError)
|
|
34
|
+
|
|
35
|
+
this.socket = socket
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private onMessage(message: WebSocket.RawData, isBinary: boolean): void {
|
|
39
|
+
if (!isBinary) {
|
|
40
|
+
logger.trace('Received string Message')
|
|
41
|
+
} else {
|
|
42
|
+
logger.trace('Websocket server received Message')
|
|
43
|
+
this.emit('data', new Uint8Array(message as Buffer))
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private onClose(reasonCode: number, description: string): void {
|
|
48
|
+
logger.trace('Peer ' + this.remoteIpAddress + ' disconnected.')
|
|
49
|
+
this.doDisconnect(reasonCode, description)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private onError(error: Error): void {
|
|
53
|
+
this.emit('error', error.name)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private stopListening(): void {
|
|
57
|
+
this.socket?.off('message', this.onMessage)
|
|
58
|
+
this.socket?.off('close', this.onClose)
|
|
59
|
+
this.socket?.off('error', this.onError)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private doDisconnect(reasonCode: number, description: string): void {
|
|
63
|
+
this.stopped = true
|
|
64
|
+
this.stopListening()
|
|
65
|
+
this.socket = undefined
|
|
66
|
+
const gracefulLeave = (reasonCode === GOING_AWAY) || (reasonCode === CUSTOM_GOING_AWAY)
|
|
67
|
+
this.emit('disconnected', gracefulLeave, reasonCode, description)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public send(data: Uint8Array): void {
|
|
71
|
+
// TODO: no need to check this.socket as it is always defined when stopped is false?
|
|
72
|
+
if (!this.stopped && this.socket) {
|
|
73
|
+
this.socket.send(data, { binary: true })
|
|
74
|
+
} else {
|
|
75
|
+
logger.debug('Tried to call send() on a stopped socket')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public async close(gracefulLeave: boolean): Promise<void> {
|
|
81
|
+
this.emit('disconnected', gracefulLeave, undefined, 'close() called')
|
|
82
|
+
this.removeAllListeners()
|
|
83
|
+
if (!this.stopped) {
|
|
84
|
+
this.socket?.close(gracefulLeave ? GOING_AWAY : undefined)
|
|
85
|
+
} else {
|
|
86
|
+
logger.debug('Tried to close a stopped connection')
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// TODO could rename to "closeSilently?"
|
|
91
|
+
public destroy(): void {
|
|
92
|
+
if (!this.stopped) {
|
|
93
|
+
this.removeAllListeners()
|
|
94
|
+
if (this.socket) {
|
|
95
|
+
this.stopListening()
|
|
96
|
+
this.socket.close()
|
|
97
|
+
this.socket = undefined
|
|
98
|
+
}
|
|
99
|
+
this.stopped = true
|
|
100
|
+
} else {
|
|
101
|
+
logger.debug('Tried to destroy() a stopped connection')
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|