@streamr/dht 100.2.5-beta.1 → 101.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/dist/package.json +8 -8
  2. package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
  3. package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
  4. package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
  5. package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
  6. package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
  7. package/dist/src/connection/ConnectionManager.d.ts +4 -6
  8. package/dist/src/connection/ConnectionManager.js +128 -103
  9. package/dist/src/connection/ConnectionManager.js.map +1 -1
  10. package/dist/src/connection/ConnectorFacade.d.ts +15 -14
  11. package/dist/src/connection/ConnectorFacade.js +70 -52
  12. package/dist/src/connection/ConnectorFacade.js.map +1 -1
  13. package/dist/src/connection/Handshaker.d.ts +9 -2
  14. package/dist/src/connection/Handshaker.js +117 -27
  15. package/dist/src/connection/Handshaker.js.map +1 -1
  16. package/dist/src/connection/ManagedConnection.d.ts +13 -38
  17. package/dist/src/connection/ManagedConnection.js +31 -252
  18. package/dist/src/connection/ManagedConnection.js.map +1 -1
  19. package/dist/src/connection/OutputBuffer.d.ts +9 -0
  20. package/dist/src/connection/OutputBuffer.js +26 -0
  21. package/dist/src/connection/OutputBuffer.js.map +1 -0
  22. package/dist/src/connection/PendingConnection.d.ts +19 -0
  23. package/dist/src/connection/PendingConnection.js +59 -0
  24. package/dist/src/connection/PendingConnection.js.map +1 -0
  25. package/dist/src/connection/connectivityChecker.js +3 -3
  26. package/dist/src/connection/connectivityChecker.js.map +1 -1
  27. package/dist/src/connection/connectivityRequestHandler.js +2 -2
  28. package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
  29. package/dist/src/connection/simulator/Simulator.d.ts +1 -3
  30. package/dist/src/connection/simulator/Simulator.js +1 -4
  31. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  32. package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
  33. package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
  34. package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
  35. package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
  36. package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
  37. package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
  38. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
  39. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  40. package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
  41. package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
  42. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  43. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
  44. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
  45. package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
  46. package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
  47. package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
  48. package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
  49. package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
  50. package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
  51. package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
  52. package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
  53. package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
  54. package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
  55. package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
  56. package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
  57. package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
  58. package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
  59. package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
  60. package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
  61. package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
  62. package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
  63. package/dist/src/connection/websocket/WebsocketServer.js +11 -11
  64. package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
  65. package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
  66. package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
  67. package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
  68. package/dist/src/dht/DhtNode.d.ts +4 -4
  69. package/dist/src/dht/DhtNode.js +85 -84
  70. package/dist/src/dht/DhtNode.js.map +1 -1
  71. package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
  72. package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
  73. package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
  74. package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
  75. package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
  76. package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
  77. package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
  78. package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
  79. package/dist/src/dht/PeerManager.d.ts +4 -4
  80. package/dist/src/dht/PeerManager.js +22 -22
  81. package/dist/src/dht/PeerManager.js.map +1 -1
  82. package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
  83. package/dist/src/dht/contact/SortedContactList.js +9 -9
  84. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  85. package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
  86. package/dist/src/dht/discovery/DiscoverySession.js +21 -21
  87. package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
  88. package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
  89. package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
  90. package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
  91. package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
  92. package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
  93. package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
  94. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
  95. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
  96. package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
  97. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
  98. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
  99. package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
  100. package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
  101. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
  102. package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
  103. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
  104. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
  105. package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
  106. package/dist/src/dht/routing/Router.d.ts +3 -3
  107. package/dist/src/dht/routing/Router.js +20 -20
  108. package/dist/src/dht/routing/Router.js.map +1 -1
  109. package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
  110. package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
  111. package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
  112. package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
  113. package/dist/src/dht/routing/RoutingSession.js +24 -24
  114. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  115. package/dist/src/dht/store/StoreManager.d.ts +3 -3
  116. package/dist/src/dht/store/StoreManager.js +25 -25
  117. package/dist/src/dht/store/StoreManager.js.map +1 -1
  118. package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
  119. package/dist/src/dht/store/StoreRpcLocal.js +12 -12
  120. package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
  121. package/dist/src/exports.d.ts +3 -0
  122. package/dist/src/exports.js +5 -1
  123. package/dist/src/exports.js.map +1 -1
  124. package/dist/src/proto/google/protobuf/any.d.ts +5 -8
  125. package/dist/src/proto/google/protobuf/any.js.map +1 -1
  126. package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
  127. package/dist/src/proto/google/protobuf/empty.js.map +1 -1
  128. package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
  129. package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
  130. package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
  131. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
  132. package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
  133. package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
  134. package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
  135. package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
  136. package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
  137. package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
  138. package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
  139. package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
  140. package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
  141. package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
  142. package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
  143. package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
  144. package/package.json +8 -8
  145. package/protos/DhtRpc.proto +1 -1
  146. package/src/connection/ConnectionLockRpcLocal.ts +9 -9
  147. package/src/connection/ConnectionLockRpcRemote.ts +1 -1
  148. package/src/connection/ConnectionManager.ts +153 -111
  149. package/src/connection/ConnectorFacade.ts +84 -61
  150. package/src/connection/Handshaker.ts +131 -27
  151. package/src/connection/ManagedConnection.ts +41 -304
  152. package/src/connection/OutputBuffer.ts +28 -0
  153. package/src/connection/PendingConnection.ts +68 -0
  154. package/src/connection/connectivityChecker.ts +2 -2
  155. package/src/connection/connectivityRequestHandler.ts +1 -1
  156. package/src/connection/simulator/Simulator.ts +1 -5
  157. package/src/connection/simulator/SimulatorConnection.ts +1 -2
  158. package/src/connection/simulator/SimulatorConnector.ts +34 -33
  159. package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
  160. package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
  161. package/src/connection/webrtc/WebrtcConnector.ts +73 -62
  162. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
  163. package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
  164. package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
  165. package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
  166. package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
  167. package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
  168. package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
  169. package/src/connection/websocket/WebsocketServer.ts +18 -14
  170. package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
  171. package/src/dht/DhtNode.ts +90 -89
  172. package/src/dht/DhtNodeRpcLocal.ts +11 -11
  173. package/src/dht/ExternalApiRpcLocal.ts +6 -6
  174. package/src/dht/ExternalApiRpcRemote.ts +2 -2
  175. package/src/dht/PeerManager.ts +24 -24
  176. package/src/dht/contact/SortedContactList.ts +10 -10
  177. package/src/dht/discovery/DiscoverySession.ts +24 -24
  178. package/src/dht/discovery/PeerDiscovery.ts +47 -45
  179. package/src/dht/discovery/RingDiscoverySession.ts +23 -23
  180. package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
  181. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
  182. package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
  183. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
  184. package/src/dht/routing/Router.ts +21 -21
  185. package/src/dht/routing/RouterRpcLocal.ts +17 -17
  186. package/src/dht/routing/RoutingSession.ts +26 -26
  187. package/src/dht/store/StoreManager.ts +27 -27
  188. package/src/dht/store/StoreRpcLocal.ts +13 -13
  189. package/src/exports.ts +3 -0
  190. package/src/proto/google/protobuf/any.ts +6 -9
  191. package/src/proto/google/protobuf/empty.ts +2 -1
  192. package/src/proto/google/protobuf/timestamp.ts +2 -11
  193. package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
  194. package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
  195. package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
  196. package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
  197. package/src/transport/ListeningRpcCommunicator.ts +3 -3
  198. package/src/transport/RoutingRpcCommunicator.ts +3 -3
  199. package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
  200. package/test/integration/ConnectionManager.test.ts +3 -2
  201. package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
  202. package/test/integration/SimultaneousConnections.test.ts +2 -2
  203. package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
  204. package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
  205. package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
  206. package/test/unit/ConnectionManager.test.ts +64 -0
  207. package/test/unit/DiscoverySession.test.ts +1 -1
  208. package/test/unit/Handshaker.test.ts +169 -0
  209. package/test/unit/ManagedConnection.test.ts +58 -0
  210. package/test/unit/PendingConnection.test.ts +57 -0
  211. package/test/unit/WebrtcConnector.test.ts +56 -0
  212. package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
  213. package/test/unit/WebsocketServerConnector.test.ts +102 -0
  214. package/test/utils/FakeConnectorFacade.ts +41 -0
  215. package/test/utils/mock/MockConnection.ts +26 -0
  216. package/test/utils/utils.ts +2 -2
  217. package/dist/src/connection/IConnectionSource.d.ts +0 -4
  218. package/dist/src/connection/IConnectionSource.js +0 -3
  219. package/dist/src/connection/IConnectionSource.js.map +0 -1
  220. package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
  221. package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
  222. package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
  223. package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
  224. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
  225. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
  226. package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
  227. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
  228. package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
  229. package/src/connection/IConnectionSource.ts +0 -6
  230. package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
  231. package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
@@ -1,48 +1,33 @@
1
- import { WebsocketClientConnection } from './NodeWebsocketClientConnection'
2
- import { IConnection, ConnectionType } from '../IConnection'
3
- import { ITransport } from '../../transport/ITransport'
1
+ import { GeoIpLocator } from '@streamr/geoip-location'
4
2
  import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
5
- import { WebsocketConnectorRpcLocal } from './WebsocketConnectorRpcLocal'
6
- import { WebsocketConnectorRpcRemote } from './WebsocketConnectorRpcRemote'
7
- import {
8
- ConnectivityMethod,
9
- ConnectivityResponse,
10
- HandshakeError,
11
- PeerDescriptor,
12
- WebsocketConnectionRequest
13
- } from '../../proto/packages/dht/protos/DhtRpc'
14
- import { WebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
15
- import { ipv4ToNumber, Logger, wait } from '@streamr/utils'
16
- import { ManagedConnection } from '../ManagedConnection'
3
+ import { Action, connectivityMethodToWebsocketUrl } from './WebsocketClientConnector'
17
4
  import { WebsocketServer } from './WebsocketServer'
18
- import { sendConnectivityRequest } from '../connectivityChecker'
5
+ import { areEqualPeerDescriptors, DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
6
+ import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
7
+ import { ConnectivityResponse, HandshakeError, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
19
8
  import { NatType, PortRange, TlsCertificate } from '../ConnectionManager'
9
+ import { ITransport } from '../../transport/ITransport'
10
+ import { ipv4ToNumber, Logger, wait } from '@streamr/utils'
11
+ import { attachConnectivityRequestHandler, DISABLE_CONNECTIVITY_PROBE } from '../connectivityRequestHandler'
20
12
  import { WebsocketServerConnection } from './WebsocketServerConnection'
21
- import { Handshaker } from '../Handshaker'
13
+ import { ConnectionType, IConnection } from '../IConnection'
22
14
  import queryString from 'querystring'
15
+ import { isMaybeSupportedVersion, LOCAL_PROTOCOL_VERSION } from '../../helpers/version'
23
16
  import { shuffle } from 'lodash'
24
- import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
25
- import { expectedConnectionType } from '../../helpers/Connectivity'
17
+ import { sendConnectivityRequest } from '../connectivityChecker'
18
+ import { acceptHandshake, Handshaker, rejectHandshake } from '../Handshaker'
19
+ import { WebsocketClientConnectorRpcRemote } from './WebsocketClientConnectorRpcRemote'
20
+ import { WebsocketClientConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
26
21
  import { WebsocketServerStartError } from '../../helpers/errors'
27
- import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
28
- import { DISABLE_CONNECTIVITY_PROBE, attachConnectivityRequestHandler } from '../connectivityRequestHandler'
29
22
  import * as Err from '../../helpers/errors'
30
- import { Empty } from '../../proto/google/protobuf/empty'
31
- import { DhtAddress, areEqualPeerDescriptors, getNodeIdFromPeerDescriptor } from '../../identifiers'
32
- import { LOCAL_PROTOCOL_VERSION, isMaybeSupportedVersion } from '../../helpers/version'
33
- import { GeoIpLocator } from '@streamr/geoip-location'
23
+ import { expectedConnectionType } from '../../helpers/Connectivity'
24
+ import { PendingConnection } from '../PendingConnection'
34
25
 
35
26
  const logger = new Logger(module)
36
27
 
37
- export type Action = 'connectivityRequest' | 'connectivityProbe'
38
-
39
- export const connectivityMethodToWebsocketUrl = (ws: ConnectivityMethod, action?: Action): string => {
40
- return (ws.tls ? 'wss://' : 'ws://') + ws.host + ':' + ws.port + ((action !== undefined) ? '?action=' + action : '')
41
- }
42
-
43
- export interface WebsocketConnectorConfig {
44
- transport: ITransport
45
- onNewConnection: (connection: ManagedConnection) => boolean
28
+ export interface WebsocketServerConnectorOptions {
29
+ onNewConnection: (connection: PendingConnection) => boolean
30
+ rpcCommunicator: ListeningRpcCommunicator
46
31
  hasConnection: (nodeId: DhtAddress) => boolean
47
32
  portRange?: PortRange
48
33
  maxMessageSize?: number
@@ -56,82 +41,34 @@ export interface WebsocketConnectorConfig {
56
41
  geoIpDatabaseFolder?: string
57
42
  }
58
43
 
59
- export class WebsocketConnector {
44
+ // TODO: Could delFunc be removed here if the Handshaker event based clean up works correctly now?
45
+ interface OngoingConnectionRequest {
46
+ delFunc: () => void
47
+ pendingConnection: PendingConnection
48
+ }
49
+
50
+ // TODO: Move server starting logic including autocertification and connectivity checking to WebsocketServer.ts?
51
+ export class WebsocketServerConnector {
60
52
 
61
- private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
62
- private readonly rpcCommunicator: ListeningRpcCommunicator
63
53
  private readonly websocketServer?: WebsocketServer
64
54
  private geoIpLocator?: GeoIpLocator
65
- private readonly ongoingConnectRequests: Map<DhtAddress, ManagedConnection> = new Map()
55
+ private readonly ongoingConnectRequests: Map<DhtAddress, OngoingConnectionRequest> = new Map()
66
56
  private host?: string
67
57
  private autoCertifierClient?: AutoCertifierClientFacade
68
58
  private selectedPort?: number
69
59
  private localPeerDescriptor?: PeerDescriptor
70
- private connectingConnections: Map<DhtAddress, ManagedConnection> = new Map()
71
60
  private abortController = new AbortController()
72
- private readonly config: WebsocketConnectorConfig
61
+ private readonly options: WebsocketServerConnectorOptions
73
62
 
74
- constructor(config: WebsocketConnectorConfig) {
75
- this.config = config
76
- this.websocketServer = config.portRange ? new WebsocketServer({
77
- portRange: config.portRange,
78
- tlsCertificate: config.tlsCertificate,
79
- maxMessageSize: config.maxMessageSize,
80
- enableTls: config.serverEnableTls
63
+ constructor(options: WebsocketServerConnectorOptions) {
64
+ this.options = options
65
+ this.websocketServer = options.portRange ? new WebsocketServer({
66
+ portRange: options.portRange,
67
+ tlsCertificate: options.tlsCertificate,
68
+ maxMessageSize: options.maxMessageSize,
69
+ enableTls: options.serverEnableTls
81
70
  }) : undefined
82
- this.host = config.host
83
- this.rpcCommunicator = new ListeningRpcCommunicator(WebsocketConnector.WEBSOCKET_CONNECTOR_SERVICE_ID, config.transport, {
84
- rpcRequestTimeout: 15000 // TODO use config option or named constant?
85
- })
86
- this.registerLocalRpcMethods(config)
87
- }
88
-
89
- private registerLocalRpcMethods(config: WebsocketConnectorConfig) {
90
- const rpcLocal = new WebsocketConnectorRpcLocal({
91
- connect: (targetPeerDescriptor: PeerDescriptor) => this.connect(targetPeerDescriptor),
92
- hasConnection: (nodeId: DhtAddress): boolean => (this.connectingConnections.has(nodeId)
93
- || this.ongoingConnectRequests.has(nodeId)
94
- || config.hasConnection(nodeId))
95
- ,
96
- onNewConnection: (connection: ManagedConnection) => config.onNewConnection(connection),
97
- abortSignal: this.abortController.signal
98
- })
99
- this.rpcCommunicator.registerRpcNotification(
100
- WebsocketConnectionRequest,
101
- 'requestConnection',
102
- async (req: WebsocketConnectionRequest, context: ServerCallContext): Promise<Empty> => {
103
- if (!this.abortController.signal.aborted) {
104
- return rpcLocal.requestConnection(req, context)
105
- } else {
106
- return {}
107
- }
108
- }
109
- )
110
- }
111
-
112
- private attachHandshaker(connection: IConnection) {
113
- const handshaker = new Handshaker(this.localPeerDescriptor!, connection)
114
- handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, sourceVersion: string, remotePeerDescriptor?: PeerDescriptor) => {
115
- this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, sourceVersion, remotePeerDescriptor)
116
- })
117
- }
118
-
119
- public async autoCertify(): Promise<void> {
120
- this.autoCertifierClient = new AutoCertifierClientFacade({
121
- configFile: this.config.autoCertifierConfigFile,
122
- transport: this.config.autoCertifierTransport,
123
- url: this.config.autoCertifierUrl,
124
- wsServerPort: this.selectedPort!,
125
- setHost: (hostName: string) => this.setHost(hostName),
126
- updateCertificate: (certificate: string, privateKey: string) => this.websocketServer!.updateCertificate(certificate, privateKey)
127
- })
128
- logger.trace(`AutoCertifying subdomain...`)
129
- await this.autoCertifierClient.start()
130
- }
131
-
132
- private setHost(hostName: string): void {
133
- logger.trace(`Setting host name to ${hostName}`)
134
- this.host = hostName
71
+ this.host = options.host
135
72
  }
136
73
 
137
74
  public async start(): Promise<void> {
@@ -159,8 +96,8 @@ export class WebsocketConnector {
159
96
  }
160
97
  })
161
98
 
162
- if (this.config.geoIpDatabaseFolder) {
163
- const geoIpLocator = new GeoIpLocator(this.config.geoIpDatabaseFolder)
99
+ if (this.options.geoIpDatabaseFolder) {
100
+ const geoIpLocator = new GeoIpLocator(this.options.geoIpDatabaseFolder)
164
101
  try {
165
102
  await geoIpLocator.start()
166
103
  this.geoIpLocator = geoIpLocator
@@ -174,6 +111,48 @@ export class WebsocketConnector {
174
111
  }
175
112
  }
176
113
 
114
+ private attachHandshaker(connection: IConnection) {
115
+ // TODO: use createIncomingHandshaker here?
116
+ const handshaker = new Handshaker(this.localPeerDescriptor!, connection)
117
+ handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, sourceVersion: string, remotePeerDescriptor?: PeerDescriptor) => {
118
+ this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, handshaker, sourceVersion, remotePeerDescriptor)
119
+ })
120
+ }
121
+
122
+ private onServerSocketHandshakeRequest(
123
+ sourcePeerDescriptor: PeerDescriptor,
124
+ websocketServerConnection: IConnection,
125
+ handshaker: Handshaker,
126
+ remoteVersion: string,
127
+ targetPeerDescriptor?: PeerDescriptor
128
+ ) {
129
+ const nodeId = getNodeIdFromPeerDescriptor(sourcePeerDescriptor)
130
+ if (this.ongoingConnectRequests.has(nodeId)) {
131
+ const { pendingConnection, delFunc } = this.ongoingConnectRequests.get(nodeId)!
132
+ if (!isMaybeSupportedVersion(remoteVersion)) {
133
+ rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
134
+ delFunc()
135
+ } else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
136
+ rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
137
+ delFunc()
138
+ } else {
139
+ acceptHandshake(handshaker, pendingConnection, websocketServerConnection)
140
+ }
141
+ } else {
142
+ const pendingConnection = new PendingConnection(sourcePeerDescriptor)
143
+
144
+ if (!isMaybeSupportedVersion(remoteVersion)) {
145
+ rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
146
+ } else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
147
+ rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
148
+ } else if (this.options.onNewConnection(pendingConnection)) {
149
+ acceptHandshake(handshaker, pendingConnection, websocketServerConnection)
150
+ } else {
151
+ rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
152
+ }
153
+ }
154
+ }
155
+
177
156
  public async checkConnectivity(allowSelfSignedCertificate: boolean): Promise<ConnectivityResponse> {
178
157
  // TODO: this could throw?
179
158
  if (this.abortController.signal.aborted) {
@@ -184,22 +163,22 @@ export class WebsocketConnector {
184
163
  version: LOCAL_PROTOCOL_VERSION
185
164
  }
186
165
  }
187
- if (!this.config.entrypoints || this.config.entrypoints.length === 0) {
188
- // return connectivity info given in config
166
+ if (!this.options.entrypoints || this.options.entrypoints.length === 0) {
167
+ // return connectivity info given in options
189
168
  return {
190
169
  host: this.host!,
191
170
  natType: NatType.OPEN_INTERNET,
192
171
  websocket: {
193
172
  host: this.host!,
194
173
  port: this.selectedPort!,
195
- tls: this.config.tlsCertificate !== undefined
174
+ tls: this.options.tlsCertificate !== undefined
196
175
  },
197
176
  // TODO: Resolve the given host name or or use as is if IP was given.
198
177
  ipAddress: ipv4ToNumber('127.0.0.1'),
199
178
  version: LOCAL_PROTOCOL_VERSION
200
179
  }
201
180
  }
202
- const shuffledEntrypoints = shuffle(this.config.entrypoints)
181
+ const shuffledEntrypoints = shuffle(this.options.entrypoints)
203
182
  while (shuffledEntrypoints.length > 0 && !this.abortController.signal.aborted) {
204
183
  const entryPoint = shuffledEntrypoints[0]
205
184
  try {
@@ -207,7 +186,7 @@ export class WebsocketConnector {
207
186
  const connectivityRequest = {
208
187
  port: this.selectedPort ?? DISABLE_CONNECTIVITY_PROBE,
209
188
  host: this.host,
210
- tls: this.websocketServer ? this.config.serverEnableTls : false,
189
+ tls: this.websocketServer ? this.options.serverEnableTls : false,
211
190
  allowSelfSignedCertificate
212
191
  }
213
192
  if (!this.abortController.signal.aborted) {
@@ -224,64 +203,44 @@ export class WebsocketConnector {
224
203
  }
225
204
  }
226
205
  throw new WebsocketServerStartError(
227
- `Failed to connect to the entrypoints after ${this.config.entrypoints.length} attempts\n`
228
- + `Attempted hosts: ${this.config.entrypoints.map((entry) => `${entry.websocket!.host}:${entry.websocket!.port}`).join(', ')}`
206
+ `Failed to connect to the entrypoints after ${this.options.entrypoints.length} attempts\n`
207
+ + `Attempted hosts: ${this.options.entrypoints.map((entry) => `${entry.websocket!.host}:${entry.websocket!.port}`).join(', ')}`
229
208
  )
230
209
  }
231
210
 
232
- public isPossibleToFormConnection(targetPeerDescriptor: PeerDescriptor): boolean {
233
- const connectionType = expectedConnectionType(this.localPeerDescriptor!, targetPeerDescriptor)
234
- return (connectionType === ConnectionType.WEBSOCKET_CLIENT || connectionType === ConnectionType.WEBSOCKET_SERVER)
211
+ public async autoCertify(): Promise<void> {
212
+ this.autoCertifierClient = new AutoCertifierClientFacade({
213
+ configFile: this.options.autoCertifierConfigFile,
214
+ transport: this.options.autoCertifierTransport,
215
+ url: this.options.autoCertifierUrl,
216
+ wsServerPort: this.selectedPort!,
217
+ setHost: (hostName: string) => this.setHost(hostName),
218
+ updateCertificate: (certificate: string, privateKey: string) => this.websocketServer!.updateCertificate(certificate, privateKey)
219
+ })
220
+ logger.trace(`AutoCertifying subdomain...`)
221
+ await this.autoCertifierClient.start()
235
222
  }
236
223
 
237
- public connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
238
- const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
239
- const existingConnection = this.connectingConnections.get(nodeId)
240
- if (existingConnection) {
241
- return existingConnection
242
- }
243
-
244
- if (this.localPeerDescriptor!.websocket && !targetPeerDescriptor.websocket) {
245
- return this.requestConnectionFromPeer(this.localPeerDescriptor!, targetPeerDescriptor)
246
- } else {
247
- const socket = new WebsocketClientConnection()
248
-
249
- const url = connectivityMethodToWebsocketUrl(targetPeerDescriptor.websocket!)
250
-
251
- const managedConnection = new ManagedConnection(
252
- this.localPeerDescriptor!,
253
- ConnectionType.WEBSOCKET_CLIENT,
254
- socket,
255
- undefined,
256
- targetPeerDescriptor
257
- )
258
- managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
259
-
260
- this.connectingConnections.set(nodeId, managedConnection)
261
-
262
- const delFunc = () => {
263
- if (this.connectingConnections.has(nodeId)) {
264
- this.connectingConnections.delete(nodeId)
265
- }
266
- socket.off('disconnected', delFunc)
267
- managedConnection.off('handshakeCompleted', delFunc)
268
- }
269
- socket.on('disconnected', delFunc)
270
- managedConnection.on('handshakeCompleted', delFunc)
271
-
272
- socket.connect(url, false)
224
+ private setHost(hostName: string): void {
225
+ logger.trace(`Setting host name to ${hostName}`)
226
+ this.host = hostName
227
+ }
273
228
 
274
- return managedConnection
229
+ public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
230
+ const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
231
+ if (this.ongoingConnectRequests.has(nodeId)) {
232
+ return this.ongoingConnectRequests.get(nodeId)!.pendingConnection
275
233
  }
234
+ return this.requestConnectionFromPeer(this.localPeerDescriptor!, targetPeerDescriptor)
276
235
  }
277
236
 
278
- private requestConnectionFromPeer(localPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor): ManagedConnection {
237
+ private requestConnectionFromPeer(localPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor): PendingConnection {
279
238
  setImmediate(() => {
280
- const remoteConnector = new WebsocketConnectorRpcRemote(
239
+ const remoteConnector = new WebsocketClientConnectorRpcRemote(
281
240
  localPeerDescriptor,
282
241
  targetPeerDescriptor,
283
- this.rpcCommunicator,
284
- WebsocketConnectorRpcClient
242
+ this.options.rpcCommunicator,
243
+ WebsocketClientConnectorRpcClient
285
244
  )
286
245
  remoteConnector.requestConnection().then(() => {
287
246
  logger.trace('Sent WebsocketConnectionRequest notification to peer', { targetPeerDescriptor })
@@ -291,57 +250,23 @@ export class WebsocketConnector {
291
250
  })
292
251
  })
293
252
  })
294
- const managedConnection = new ManagedConnection(
295
- this.localPeerDescriptor!,
296
- ConnectionType.WEBSOCKET_SERVER,
297
- undefined,
298
- undefined,
299
- targetPeerDescriptor
300
- )
253
+ const pendingConnection = new PendingConnection(targetPeerDescriptor)
301
254
  const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
302
- managedConnection.on('disconnected', () => this.ongoingConnectRequests.delete(nodeId))
303
- managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
304
- this.ongoingConnectRequests.set(nodeId, managedConnection)
305
- return managedConnection
306
- }
307
-
308
- private onServerSocketHandshakeRequest(
309
- sourcePeerDescriptor: PeerDescriptor,
310
- websocketServerConnection: IConnection,
311
- remoteVersion: string,
312
- targetPeerDescriptor?: PeerDescriptor
313
- ) {
314
- const nodeId = getNodeIdFromPeerDescriptor(sourcePeerDescriptor)
315
- if (this.ongoingConnectRequests.has(nodeId)) {
316
- const ongoingConnectRequest = this.ongoingConnectRequests.get(nodeId)!
317
- if (!isMaybeSupportedVersion(remoteVersion)) {
318
- ongoingConnectRequest.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
319
- } else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
320
- ongoingConnectRequest.rejectHandshake(HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
321
- } else {
322
- ongoingConnectRequest.attachImplementation(websocketServerConnection)
323
- ongoingConnectRequest.acceptHandshake()
324
- }
255
+ // TODO: can this leak?
256
+ const delFunc = () => {
257
+ pendingConnection.off('connected', delFunc)
258
+ pendingConnection.off('disconnected', delFunc)
325
259
  this.ongoingConnectRequests.delete(nodeId)
326
- } else {
327
- const managedConnection = new ManagedConnection(
328
- this.localPeerDescriptor!,
329
- ConnectionType.WEBSOCKET_SERVER,
330
- undefined,
331
- websocketServerConnection,
332
- targetPeerDescriptor
333
- )
334
- managedConnection.setRemotePeerDescriptor(sourcePeerDescriptor)
335
- if (!isMaybeSupportedVersion(remoteVersion)) {
336
- managedConnection.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
337
- } else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
338
- managedConnection.rejectHandshake(HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
339
- } else if (this.config.onNewConnection(managedConnection)) {
340
- managedConnection.acceptHandshake()
341
- } else {
342
- managedConnection.rejectHandshake(HandshakeError.DUPLICATE_CONNECTION)
343
- }
344
260
  }
261
+ pendingConnection.on('connected', delFunc)
262
+ pendingConnection.on('disconnected', delFunc)
263
+ this.ongoingConnectRequests.set(nodeId, { pendingConnection, delFunc })
264
+ return pendingConnection
265
+ }
266
+
267
+ public isPossibleToFormConnection(targetPeerDescriptor: PeerDescriptor): boolean {
268
+ const connectionType = expectedConnectionType(this.localPeerDescriptor!, targetPeerDescriptor)
269
+ return (connectionType === ConnectionType.WEBSOCKET_SERVER)
345
270
  }
346
271
 
347
272
  public setLocalPeerDescriptor(localPeerDescriptor: PeerDescriptor): void {
@@ -350,14 +275,12 @@ export class WebsocketConnector {
350
275
 
351
276
  public async destroy(): Promise<void> {
352
277
  this.abortController.abort()
353
- this.rpcCommunicator.destroy()
354
278
 
355
279
  const requests = Array.from(this.ongoingConnectRequests.values())
356
- await Promise.allSettled(requests.map((conn) => conn.close(false)))
280
+ await Promise.allSettled(requests.map((ongoingConnectRequest) => ongoingConnectRequest.pendingConnection.close(true)))
357
281
 
358
- const attempts = Array.from(this.connectingConnections.values())
359
- await Promise.allSettled(attempts.map((conn) => conn.close(false)))
360
282
  await this.websocketServer?.stop()
361
283
  await this.geoIpLocator?.stop()
362
284
  }
285
+
363
286
  }