@streamr/dht 102.0.0-beta.0 → 102.0.0-beta.2

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 (264) hide show
  1. package/dist/generated/packages/dht/protos/DhtRpc.d.ts +16 -8
  2. package/dist/generated/packages/dht/protos/DhtRpc.js +7 -5
  3. package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -1
  4. package/dist/package.json +15 -16
  5. package/dist/src/connection/ConnectionManager.js +18 -8
  6. package/dist/src/connection/ConnectionManager.js.map +1 -1
  7. package/dist/src/connection/Handshaker.d.ts +1 -1
  8. package/dist/src/connection/Handshaker.js +9 -5
  9. package/dist/src/connection/Handshaker.js.map +1 -1
  10. package/dist/src/connection/ManagedConnection.js +17 -7
  11. package/dist/src/connection/ManagedConnection.js.map +1 -1
  12. package/dist/src/connection/connectivityChecker.js +20 -10
  13. package/dist/src/connection/connectivityChecker.js.map +1 -1
  14. package/dist/src/connection/connectivityRequestHandler.js +3 -3
  15. package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
  16. package/dist/src/connection/simulator/Simulator.js +3 -2
  17. package/dist/src/connection/simulator/Simulator.js.map +1 -1
  18. package/dist/src/connection/simulator/pings.d.ts +1 -1
  19. package/dist/src/connection/simulator/pings.js +3 -3
  20. package/dist/src/connection/simulator/pings.js.map +1 -1
  21. package/dist/src/connection/webrtc/NodeWebrtcConnection.js +0 -2
  22. package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
  23. package/dist/src/connection/webrtc/WebrtcConnector.js +19 -9
  24. package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
  25. package/dist/src/connection/webrtc/iceServerAsString.js +1 -2
  26. package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
  27. package/dist/src/connection/websocket/AbstractWebsocketClientConnection.d.ts +0 -2
  28. package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +0 -1
  29. package/dist/src/connection/websocket/WebsocketServerConnection.d.ts +0 -1
  30. package/dist/src/connection/websocket/WebsocketServerConnector.js +28 -18
  31. package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -1
  32. package/dist/src/dht/DhtNode.d.ts +1 -0
  33. package/dist/src/dht/DhtNode.js +3 -2
  34. package/dist/src/dht/DhtNode.js.map +1 -1
  35. package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -1
  36. package/dist/src/dht/PeerManager.d.ts +2 -1
  37. package/dist/src/dht/PeerManager.js +2 -1
  38. package/dist/src/dht/PeerManager.js.map +1 -1
  39. package/dist/src/dht/contact/SortedContactList.js +1 -1
  40. package/dist/src/dht/contact/SortedContactList.js.map +1 -1
  41. package/dist/src/dht/discovery/DiscoverySession.d.ts +0 -1
  42. package/dist/src/dht/discovery/PeerDiscovery.d.ts +0 -1
  43. package/dist/src/dht/discovery/RingDiscoverySession.d.ts +0 -1
  44. package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +2 -2
  45. package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
  46. package/dist/src/dht/routing/RoutingSession.js +2 -2
  47. package/dist/src/dht/routing/RoutingSession.js.map +1 -1
  48. package/dist/src/dht/routing/RoutingTablesCache.d.ts +1 -1
  49. package/dist/src/dht/store/LocalDataStore.js +1 -1
  50. package/dist/src/dht/store/LocalDataStore.js.map +1 -1
  51. package/dist/src/dht/store/StoreManager.d.ts +1 -1
  52. package/dist/src/dht/store/StoreRpcLocal.d.ts +1 -1
  53. package/dist/src/helpers/AddressTools.js +2 -3
  54. package/dist/src/helpers/AddressTools.js.map +1 -1
  55. package/dist/src/helpers/debugHelpers.js +2 -2
  56. package/dist/src/helpers/debugHelpers.js.map +1 -1
  57. package/dist/src/helpers/protoClasses.d.ts +1 -1
  58. package/dist/src/helpers/protoClasses.js.map +1 -1
  59. package/dist/src/helpers/protoToString.js +1 -2
  60. package/dist/src/helpers/protoToString.js.map +1 -1
  61. package/dist/src/helpers/version.d.ts +1 -1
  62. package/dist/src/helpers/version.js +4 -4
  63. package/dist/src/helpers/version.js.map +1 -1
  64. package/eslint.config.mjs +12 -0
  65. package/jest.config.ts +12 -0
  66. package/package.json +15 -16
  67. package/protos/DhtRpc.proto +6 -4
  68. package/.eslintignore +0 -5
  69. package/.eslintrc +0 -3
  70. package/generated/google/protobuf/any.ts +0 -326
  71. package/generated/google/protobuf/empty.ts +0 -81
  72. package/generated/google/protobuf/timestamp.ts +0 -287
  73. package/generated/packages/dht/protos/DhtRpc.client.ts +0 -419
  74. package/generated/packages/dht/protos/DhtRpc.server.ts +0 -165
  75. package/generated/packages/dht/protos/DhtRpc.ts +0 -1266
  76. package/generated/packages/proto-rpc/protos/ProtoRpc.ts +0 -108
  77. package/jest.config.js +0 -5
  78. package/src/connection/Connection.ts +0 -28
  79. package/src/connection/ConnectionLockRpcLocal.ts +0 -78
  80. package/src/connection/ConnectionLockRpcRemote.ts +0 -64
  81. package/src/connection/ConnectionLockStates.ts +0 -131
  82. package/src/connection/ConnectionManager.ts +0 -661
  83. package/src/connection/ConnectionsView.ts +0 -8
  84. package/src/connection/ConnectorFacade.ts +0 -217
  85. package/src/connection/Handshaker.ts +0 -205
  86. package/src/connection/IConnection.ts +0 -40
  87. package/src/connection/ManagedConnection.ts +0 -113
  88. package/src/connection/OutputBuffer.ts +0 -28
  89. package/src/connection/PendingConnection.ts +0 -68
  90. package/src/connection/connectivityChecker.ts +0 -108
  91. package/src/connection/connectivityRequestHandler.ts +0 -116
  92. package/src/connection/simulator/Simulator.ts +0 -368
  93. package/src/connection/simulator/SimulatorConnection.ts +0 -137
  94. package/src/connection/simulator/SimulatorConnector.ts +0 -98
  95. package/src/connection/simulator/SimulatorTransport.ts +0 -15
  96. package/src/connection/simulator/pings.ts +0 -42
  97. package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
  98. package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
  99. package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -247
  100. package/src/connection/webrtc/WebrtcConnector.ts +0 -234
  101. package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
  102. package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
  103. package/src/connection/webrtc/iceServerAsString.ts +0 -15
  104. package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
  105. package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
  106. package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
  107. package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
  108. package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
  109. package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
  110. package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
  111. package/src/connection/websocket/WebsocketServer.ts +0 -164
  112. package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
  113. package/src/connection/websocket/WebsocketServerConnector.ts +0 -286
  114. package/src/dht/DhtNode.ts +0 -678
  115. package/src/dht/DhtNodeRpcLocal.ts +0 -84
  116. package/src/dht/DhtNodeRpcRemote.ts +0 -107
  117. package/src/dht/ExternalApiRpcLocal.ts +0 -58
  118. package/src/dht/ExternalApiRpcRemote.ts +0 -41
  119. package/src/dht/PeerManager.ts +0 -303
  120. package/src/dht/contact/Contact.ts +0 -19
  121. package/src/dht/contact/ContactList.ts +0 -43
  122. package/src/dht/contact/RandomContactList.ts +0 -56
  123. package/src/dht/contact/RingContactList.ts +0 -143
  124. package/src/dht/contact/RpcRemote.ts +0 -72
  125. package/src/dht/contact/SortedContactList.ts +0 -173
  126. package/src/dht/contact/getClosestNodes.ts +0 -24
  127. package/src/dht/contact/ringIdentifiers.ts +0 -62
  128. package/src/dht/discovery/DiscoverySession.ts +0 -129
  129. package/src/dht/discovery/PeerDiscovery.ts +0 -244
  130. package/src/dht/discovery/RingDiscoverySession.ts +0 -148
  131. package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
  132. package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
  133. package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
  134. package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
  135. package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
  136. package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
  137. package/src/dht/routing/DuplicateDetector.ts +0 -34
  138. package/src/dht/routing/Router.ts +0 -246
  139. package/src/dht/routing/RouterRpcLocal.ts +0 -78
  140. package/src/dht/routing/RouterRpcRemote.ts +0 -80
  141. package/src/dht/routing/RoutingSession.ts +0 -243
  142. package/src/dht/routing/RoutingTablesCache.ts +0 -60
  143. package/src/dht/routing/getPreviousPeer.ts +0 -6
  144. package/src/dht/store/LocalDataStore.ts +0 -84
  145. package/src/dht/store/StoreManager.ts +0 -170
  146. package/src/dht/store/StoreRpcLocal.ts +0 -89
  147. package/src/dht/store/StoreRpcRemote.ts +0 -32
  148. package/src/exports.ts +0 -33
  149. package/src/helpers/AddressTools.ts +0 -28
  150. package/src/helpers/Connectivity.ts +0 -19
  151. package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
  152. package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
  153. package/src/helpers/createPeerDescriptor.ts +0 -57
  154. package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
  155. package/src/helpers/debugHelpers.ts +0 -9
  156. package/src/helpers/errors.ts +0 -49
  157. package/src/helpers/offering.ts +0 -15
  158. package/src/helpers/protoClasses.ts +0 -57
  159. package/src/helpers/protoToString.ts +0 -21
  160. package/src/helpers/version.ts +0 -32
  161. package/src/identifiers.ts +0 -29
  162. package/src/rpc-protocol/DhtCallContext.ts +0 -14
  163. package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
  164. package/src/transport/ITransport.ts +0 -37
  165. package/src/transport/ListeningRpcCommunicator.ts +0 -32
  166. package/src/transport/RoutingRpcCommunicator.ts +0 -66
  167. package/src/types/ServiceID.ts +0 -1
  168. package/src/types/textencoding.d.ts +0 -6
  169. package/test/benchmark/Find.test.ts +0 -72
  170. package/test/benchmark/KademliaCorrectness.test.ts +0 -114
  171. package/test/benchmark/RingCorrectness.test.ts +0 -157
  172. package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
  173. package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
  174. package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
  175. package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
  176. package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
  177. package/test/end-to-end/Layer0.test.ts +0 -76
  178. package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
  179. package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
  180. package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
  181. package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
  182. package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
  183. package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
  184. package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
  185. package/test/end-to-end/memory-leak.test.ts +0 -80
  186. package/test/integration/ConnectionLocking.test.ts +0 -182
  187. package/test/integration/ConnectionManager.test.ts +0 -528
  188. package/test/integration/ConnectivityChecking.test.ts +0 -53
  189. package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
  190. package/test/integration/DhtNode.test.ts +0 -66
  191. package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
  192. package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
  193. package/test/integration/DhtRpc.test.ts +0 -121
  194. package/test/integration/Find.test.ts +0 -45
  195. package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
  196. package/test/integration/Layer1-scale.test.ts +0 -189
  197. package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
  198. package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
  199. package/test/integration/ReplicateData.test.ts +0 -104
  200. package/test/integration/RouteMessage.test.ts +0 -230
  201. package/test/integration/RouterRpcRemote.test.ts +0 -77
  202. package/test/integration/SimultaneousConnections.test.ts +0 -316
  203. package/test/integration/Store.test.ts +0 -85
  204. package/test/integration/StoreAndDelete.test.ts +0 -77
  205. package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
  206. package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
  207. package/test/integration/StoreRpcRemote.test.ts +0 -54
  208. package/test/integration/WebrtcConnectionManagement.test.ts +0 -219
  209. package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
  210. package/test/integration/Websocket.test.ts +0 -65
  211. package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
  212. package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
  213. package/test/integration/rpc-connections-over-webrtc.test.ts +0 -158
  214. package/test/kademlia-simulation/data/nodeids.json +0 -13002
  215. package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
  216. package/test/types/global.d.ts +0 -1
  217. package/test/unit/AddressTools.test.ts +0 -44
  218. package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
  219. package/test/unit/ConnectionManager.test.ts +0 -65
  220. package/test/unit/ConnectivityHelpers.test.ts +0 -61
  221. package/test/unit/DiscoverySession.test.ts +0 -87
  222. package/test/unit/DuplicateDetector.test.ts +0 -31
  223. package/test/unit/Handshaker.test.ts +0 -169
  224. package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
  225. package/test/unit/LocalDataStore.test.ts +0 -108
  226. package/test/unit/ManagedConnection.test.ts +0 -58
  227. package/test/unit/PeerManager.test.ts +0 -93
  228. package/test/unit/PendingConnection.test.ts +0 -57
  229. package/test/unit/ProtobufMessage.test.ts +0 -21
  230. package/test/unit/RandomContactList.test.ts +0 -58
  231. package/test/unit/RecursiveOperationManager.test.ts +0 -161
  232. package/test/unit/RecursiveOperationSession.test.ts +0 -68
  233. package/test/unit/Router.test.ts +0 -137
  234. package/test/unit/RoutingSession.test.ts +0 -79
  235. package/test/unit/SortedContactList.test.ts +0 -115
  236. package/test/unit/StoreManager.test.ts +0 -146
  237. package/test/unit/StoreRpcLocal.test.ts +0 -167
  238. package/test/unit/WebrtcConnection.test.ts +0 -29
  239. package/test/unit/WebrtcConnector.test.ts +0 -56
  240. package/test/unit/WebsocketClientConnector.test.ts +0 -101
  241. package/test/unit/WebsocketServer.test.ts +0 -66
  242. package/test/unit/WebsocketServerConnector.test.ts +0 -102
  243. package/test/unit/connectivityRequestHandler.test.ts +0 -104
  244. package/test/unit/createPeerDescriptor.test.ts +0 -69
  245. package/test/unit/customMatchers.test.ts +0 -16
  246. package/test/unit/getClosestNodes.test.ts +0 -30
  247. package/test/unit/version.test.ts +0 -18
  248. package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
  249. package/test/utils/FakeConnectorFacade.ts +0 -41
  250. package/test/utils/FakeRpcCommunicator.ts +0 -23
  251. package/test/utils/FakeTransport.ts +0 -79
  252. package/test/utils/customMatchers.ts +0 -71
  253. package/test/utils/mock/MockConnection.ts +0 -26
  254. package/test/utils/mock/MockConnectionsView.ts +0 -18
  255. package/test/utils/mock/MockRouter.ts +0 -62
  256. package/test/utils/mock/MockRpcCommunicator.ts +0 -7
  257. package/test/utils/mock/MockTransport.ts +0 -26
  258. package/test/utils/mock/mockDataEntry.ts +0 -38
  259. package/test/utils/topology.ts +0 -80
  260. package/test/utils/utils.ts +0 -268
  261. package/tsconfig.browser.json +0 -17
  262. package/tsconfig.jest.json +0 -25
  263. package/tsconfig.json +0 -3
  264. package/tsconfig.node.json +0 -24
@@ -1,108 +0,0 @@
1
- import { Logger, RunAndRaceEventsReturnType, runAndRaceEvents3 } from '@streamr/utils'
2
- import { v4 } from 'uuid'
3
- import * as Err from '../helpers/errors'
4
- import {
5
- ConnectivityRequest, ConnectivityResponse,
6
- Message, PeerDescriptor
7
- } from '../../generated/packages/dht/protos/DhtRpc'
8
- import { ConnectionEvents, IConnection } from './IConnection'
9
- import { WebsocketClientConnection } from './websocket/NodeWebsocketClientConnection'
10
- import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketClientConnector'
11
- import { isMaybeSupportedVersion } from '../helpers/version'
12
-
13
- const logger = new Logger(module)
14
-
15
- // TODO use options option or named constant?
16
- export const connectAsync = async ({ url, allowSelfSignedCertificate, timeoutMs = 1000 }:
17
- { url: string, allowSelfSignedCertificate: boolean, timeoutMs?: number }
18
- ): Promise<IConnection> => {
19
- const socket = new WebsocketClientConnection()
20
- let result: RunAndRaceEventsReturnType<ConnectionEvents>
21
- try {
22
- result = await runAndRaceEvents3<ConnectionEvents>([
23
- () => { socket.connect(url, allowSelfSignedCertificate) }],
24
- socket, ['connected', 'error'],
25
- timeoutMs)
26
- } catch {
27
- throw new Err.ConnectionFailed('WebSocket connection timed out')
28
- }
29
- if (result.winnerName === 'error') {
30
- throw new Err.ConnectionFailed('Could not open WebSocket connection')
31
- }
32
- return socket
33
- }
34
-
35
- export const CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivity-checker'
36
- const CONNECTIVITY_CHECKER_TIMEOUT = 5000
37
-
38
- export const sendConnectivityRequest = async (
39
- request: ConnectivityRequest,
40
- entryPoint: PeerDescriptor
41
- ): Promise<ConnectivityResponse> => {
42
- let outgoingConnection: IConnection
43
- const wsServerInfo = {
44
- host: entryPoint.websocket!.host,
45
- port: entryPoint.websocket!.port,
46
- tls: entryPoint.websocket!.tls,
47
- }
48
- const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityRequest')
49
- logger.debug(`Attempting connectivity check with entrypoint ${url}`)
50
- try {
51
- outgoingConnection = await connectAsync({
52
- url,
53
- allowSelfSignedCertificate: request.allowSelfSignedCertificate
54
- })
55
- } catch (e) {
56
- throw new Err.ConnectionFailed(`Failed to connect to entrypoint for connectivity check: ${url}`, e)
57
- }
58
- // send connectivity request
59
- const msg: Message = {
60
- serviceId: CONNECTIVITY_CHECKER_SERVICE_ID,
61
- messageId: v4(),
62
- body: {
63
- oneofKind: 'connectivityRequest',
64
- connectivityRequest: request
65
- }
66
- }
67
- const responseAwaiter = () => {
68
- return new Promise((resolve: (res: ConnectivityResponse) => void, reject) => {
69
- const timeoutId = setTimeout(() => {
70
- // TODO should we have some handling for this floating promise?
71
- outgoingConnection.close(false)
72
- reject(new Err.ConnectivityResponseTimeout('timeout'))
73
- }, CONNECTIVITY_CHECKER_TIMEOUT)
74
- const listener = (bytes: Uint8Array) => {
75
- // TODO should we have some handling for this floating promise?
76
- outgoingConnection.close(false)
77
- try {
78
- const message: Message = Message.fromBinary(bytes)
79
- if (message.body.oneofKind === 'connectivityResponse') {
80
- logger.debug('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
81
- const connectivityResponseMessage = message.body.connectivityResponse
82
- const remoteVersion = connectivityResponseMessage.version
83
- outgoingConnection!.off('data', listener)
84
- clearTimeout(timeoutId)
85
- if (isMaybeSupportedVersion(remoteVersion)) {
86
- resolve(connectivityResponseMessage)
87
- } else {
88
- // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
89
- reject(`Unsupported version: ${remoteVersion}`)
90
- }
91
- }
92
- } catch (err) {
93
- logger.trace('Could not parse message', { err })
94
- }
95
- }
96
- outgoingConnection!.on('data', listener)
97
- })
98
- }
99
- try {
100
- const retPromise = responseAwaiter()
101
- outgoingConnection.send(Message.toBinary(msg))
102
- logger.trace('ConnectivityRequest sent: ' + JSON.stringify(Message.toJson(msg)))
103
- return await retPromise
104
- } catch (e) {
105
- logger.error('error getting connectivityresponse')
106
- throw e
107
- }
108
- }
@@ -1,116 +0,0 @@
1
- import { ipv4ToNumber, Logger } from '@streamr/utils'
2
- import { v4 } from 'uuid'
3
- import {
4
- ConnectivityRequest,
5
- ConnectivityResponse,
6
- Message
7
- } from '../../generated/packages/dht/protos/DhtRpc'
8
- import { NatType } from './ConnectionManager'
9
- import { CONNECTIVITY_CHECKER_SERVICE_ID, connectAsync } from './connectivityChecker'
10
- import { IConnection } from './IConnection'
11
- import { WebsocketServerConnection } from './websocket/WebsocketServerConnection'
12
- import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketClientConnector'
13
- import { LOCAL_PROTOCOL_VERSION } from '../helpers/version'
14
- import { GeoIpLocator } from '@streamr/geoip-location'
15
-
16
- export const DISABLE_CONNECTIVITY_PROBE = 0
17
-
18
- const logger = new Logger(module)
19
-
20
- export const attachConnectivityRequestHandler = (connectionToListenTo: WebsocketServerConnection, geoIpLocator?: GeoIpLocator): void => {
21
- connectionToListenTo.on('data', async (data: Uint8Array) => {
22
- logger.trace('server received data')
23
- try {
24
- const message = Message.fromBinary(data)
25
- if (message.body.oneofKind === 'connectivityRequest') {
26
- logger.trace('ConnectivityRequest received: ' + JSON.stringify(Message.toJson(message)))
27
- try {
28
- await handleIncomingConnectivityRequest(connectionToListenTo,
29
- message.body.connectivityRequest, geoIpLocator)
30
- logger.trace('handleIncomingConnectivityRequest ok')
31
- } catch (err1) {
32
- logger.error('handleIncomingConnectivityRequest', { err: err1 })
33
- }
34
- }
35
- } catch (err2) {
36
- logger.trace('Could not parse message', { err: err2 })
37
- }
38
- })
39
- }
40
-
41
- const handleIncomingConnectivityRequest = async (
42
- connection: WebsocketServerConnection,
43
- connectivityRequest: ConnectivityRequest,
44
- geoIpLocator?: GeoIpLocator
45
- ): Promise<void> => {
46
- const host = connectivityRequest.host ?? connection.getRemoteIpAddress()
47
- const ipAddress = connection.getRemoteIpAddress()
48
- let connectivityResponse: ConnectivityResponse
49
- if (connectivityRequest.port !== DISABLE_CONNECTIVITY_PROBE) {
50
- connectivityResponse = await connectivityProbe(connectivityRequest, ipAddress, host)
51
- } else {
52
- logger.trace('ConnectivityRequest port is 0, replying without connectivityProbe')
53
- connectivityResponse = {
54
- host,
55
- natType: NatType.UNKNOWN,
56
- ipAddress: ipv4ToNumber(ipAddress),
57
- version: LOCAL_PROTOCOL_VERSION
58
- }
59
- }
60
- if (geoIpLocator !== undefined) {
61
- const location = geoIpLocator.lookup(ipAddress)
62
- if (location !== undefined) {
63
- connectivityResponse.latitude = location.latitude
64
- connectivityResponse.longitude = location.longitude
65
- }
66
- }
67
- const msg: Message = {
68
- serviceId: CONNECTIVITY_CHECKER_SERVICE_ID,
69
- messageId: v4(),
70
- body: {
71
- oneofKind: 'connectivityResponse',
72
- connectivityResponse
73
- }
74
- }
75
- connection.send(Message.toBinary(msg))
76
- logger.trace('ConnectivityResponse sent: ' + JSON.stringify(Message.toJson(msg)))
77
- }
78
-
79
- const connectivityProbe = async (connectivityRequest: ConnectivityRequest, ipAddress: string, host: string): Promise<ConnectivityResponse> => {
80
- let outgoingConnection: IConnection | undefined
81
- let connectivityResponseMessage: ConnectivityResponse
82
- try {
83
- const wsServerInfo = {
84
- host,
85
- port: connectivityRequest.port,
86
- tls: connectivityRequest.tls
87
- }
88
- const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityProbe')
89
- logger.trace(`Attempting Connectivity Check to ${url}`)
90
- outgoingConnection = await connectAsync({
91
- url,
92
- allowSelfSignedCertificate: connectivityRequest.allowSelfSignedCertificate
93
- })
94
- logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
95
- connectivityResponseMessage = {
96
- host,
97
- natType: NatType.OPEN_INTERNET,
98
- websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls },
99
- ipAddress: ipv4ToNumber(ipAddress),
100
- version: LOCAL_PROTOCOL_VERSION
101
- }
102
- } catch (err) {
103
- logger.debug('error', { err })
104
- connectivityResponseMessage = {
105
- host,
106
- natType: NatType.UNKNOWN,
107
- ipAddress: ipv4ToNumber(ipAddress),
108
- version: LOCAL_PROTOCOL_VERSION
109
- }
110
- }
111
- if (outgoingConnection) {
112
- // TODO should we have some handling for this floating promise?
113
- outgoingConnection.close(false)
114
- }
115
- return connectivityResponseMessage
116
- }
@@ -1,368 +0,0 @@
1
- /* eslint-disable @typescript-eslint/parameter-properties */
2
- import { PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
3
- import { SimulatorConnector } from './SimulatorConnector'
4
- import { SimulatorConnection } from './SimulatorConnection'
5
- import { ConnectionID } from '../IConnection'
6
- import { Logger } from '@streamr/utils'
7
- import { getRegionDelayMatrix } from './pings'
8
- import Heap from 'heap'
9
- import { debugVars } from '../../helpers/debugHelpers'
10
- import { DhtAddress, toNodeId } from '../../identifiers'
11
-
12
- const logger = new Logger(module)
13
-
14
- export enum LatencyType { NONE = 'NONE', RANDOM = 'RANDOM', REAL = 'REAL', FIXED = 'FIXED' }
15
-
16
- // One-way 'pipe' of messages
17
-
18
- class Association {
19
- public sourceConnection: SimulatorConnection
20
- public destinationConnection?: SimulatorConnection
21
- private lastOperationAt: number = 0
22
- private closing = false
23
-
24
- constructor(sourceConnection: SimulatorConnection,
25
- destinationConnection?: SimulatorConnection,
26
- public connectedCallback?: (error?: string) => void
27
- ) {
28
- this.sourceConnection = sourceConnection
29
- this.destinationConnection = destinationConnection
30
- }
31
-
32
- public setDestinationConnection(connection: SimulatorConnection) {
33
- this.destinationConnection = connection
34
- }
35
-
36
- public getLastOperationAt(): number {
37
- return this.lastOperationAt
38
- }
39
-
40
- public setLastOperationAt(executionTime: number): void {
41
- this.lastOperationAt = executionTime
42
- }
43
-
44
- public setClosing(): void {
45
- this.closing = true
46
- }
47
-
48
- public isClosing(): boolean {
49
- return this.closing
50
- }
51
- }
52
-
53
- class SimulatorOperation {
54
- private static objectCounter = 0
55
- public objectId = 0
56
-
57
- constructor(
58
- public executionTime: number,
59
- public association: Association
60
- ) {
61
- this.objectId = SimulatorOperation.objectCounter
62
- SimulatorOperation.objectCounter++
63
- }
64
- }
65
-
66
- class ConnectOperation extends SimulatorOperation {
67
- constructor(
68
- executionTime: number,
69
- association: Association,
70
- public sourceConnection: SimulatorConnection,
71
- public targetDescriptor: PeerDescriptor) {
72
-
73
- super(executionTime, association)
74
- }
75
- }
76
-
77
- class SendOperation extends SimulatorOperation {
78
- constructor(
79
- executionTime: number,
80
- association: Association,
81
- public data: Uint8Array
82
- ) {
83
- super(executionTime, association)
84
- }
85
- }
86
-
87
- class CloseOperation extends SimulatorOperation {
88
- }
89
-
90
- export class Simulator {
91
- private stopped = false
92
- private connectors: Map<DhtAddress, SimulatorConnector> = new Map()
93
- private latencyTable?: Array<Array<number>>
94
- private associations: Map<ConnectionID, Association> = new Map()
95
-
96
- private latencyType: LatencyType
97
- private fixedLatency?: number
98
-
99
- private loopCounter = 0
100
- private MAX_LOOPS = 1000
101
-
102
- private operationQueue: Heap<SimulatorOperation> = new Heap<SimulatorOperation>((a: SimulatorOperation, b: SimulatorOperation) => {
103
- if ((a.executionTime - b.executionTime) === 0) {
104
- return (a.objectId - b.objectId)
105
- } else {
106
- return (a.executionTime - b.executionTime)
107
- }
108
- })
109
-
110
- private simulatorTimeout?: NodeJS.Timeout
111
-
112
- constructor(latencyType: LatencyType = LatencyType.NONE, fixedLatency?: number) {
113
- this.latencyType = latencyType
114
- this.fixedLatency = fixedLatency
115
-
116
- if (this.latencyType === LatencyType.REAL) {
117
- this.latencyTable = getRegionDelayMatrix()
118
- }
119
-
120
- if ((this.latencyType === LatencyType.FIXED) && (this.fixedLatency === undefined)) {
121
- throw new Error('LatencyType.FIXED requires the desired latency to be given as second parameter')
122
- }
123
-
124
- this.generateExecutionTime = this.generateExecutionTime.bind(this)
125
- this.getLatency = this.getLatency.bind(this)
126
- this.executeCloseOperation = this.executeCloseOperation.bind(this)
127
- this.executeConnectOperation = this.executeConnectOperation.bind(this)
128
- this.executeSendOperation = this.executeSendOperation.bind(this)
129
- this.executeQueuedOperations = this.executeQueuedOperations.bind(this)
130
- this.accept = this.accept.bind(this)
131
- this.send = this.send.bind(this)
132
- this.close = this.close.bind(this)
133
- this.scheduleNextTimeout = this.scheduleNextTimeout.bind(this)
134
- this.scheduleOperation = this.scheduleOperation.bind(this)
135
- }
136
-
137
- private generateExecutionTime(association: Association, sourceRegion: number | undefined, targetRegion: number | undefined): number {
138
- let executionTime = Date.now() + this.getLatency(sourceRegion, targetRegion)
139
- if (association.getLastOperationAt() > executionTime) {
140
- executionTime = association.getLastOperationAt()
141
- }
142
-
143
- return executionTime
144
- }
145
-
146
- private getLatency(sourceRegion: number | undefined, targetRegion: number | undefined): number {
147
- let latency: number = 0
148
-
149
- if (this.latencyType === LatencyType.FIXED) {
150
- latency = this.fixedLatency!
151
- }
152
-
153
- if (this.latencyType === LatencyType.REAL) {
154
-
155
- if (sourceRegion === undefined || targetRegion === undefined || sourceRegion > 15 || targetRegion > 15) {
156
- logger.error('invalid region index given to Simulator')
157
- throw new Error('invalid region index given to Simulator')
158
- }
159
-
160
- latency = this.latencyTable![sourceRegion][targetRegion]
161
- }
162
- if (this.latencyType === LatencyType.RANDOM) {
163
- latency = Math.random() * (250 - 5) + 5
164
- }
165
-
166
- return latency
167
- }
168
-
169
- public accept(sourceConnection: SimulatorConnection, targetConnection: SimulatorConnection): void {
170
-
171
- const sourceAssociation = this.associations.get(sourceConnection.connectionId)
172
-
173
- if (!sourceAssociation) {
174
- logger.error('source association not found in accept()')
175
- return
176
- }
177
- sourceAssociation.setDestinationConnection(targetConnection)
178
-
179
- const targetAssociation = new Association(targetConnection, sourceConnection)
180
- this.associations.set(targetConnection.connectionId, targetAssociation)
181
-
182
- sourceAssociation.connectedCallback!()
183
- }
184
-
185
- public addConnector(connector: SimulatorConnector): void {
186
- this.connectors.set(toNodeId(connector.getPeerDescriptor()), connector)
187
- }
188
-
189
- private executeConnectOperation(operation: ConnectOperation): void {
190
- const target = this.connectors.get(toNodeId(operation.targetDescriptor))
191
-
192
- if (!target) {
193
- logger.error('Target connector not found when executing connect operation')
194
- return operation.association.connectedCallback!('Target connector not found')
195
- }
196
-
197
- target.handleIncomingConnection(operation.sourceConnection)
198
- }
199
-
200
- private executeCloseOperation(operation: CloseOperation): void {
201
-
202
- if (this.stopped) {
203
- return
204
- }
205
-
206
- const target = operation.association.destinationConnection
207
-
208
- let counterAssociation: Association | undefined
209
-
210
- if (target) {
211
- counterAssociation = this.associations.get(target.connectionId)
212
- }
213
-
214
- if (!target || !counterAssociation) {
215
- this.associations.delete(operation.association.sourceConnection.connectionId)
216
-
217
- } else if (!counterAssociation.isClosing()) {
218
- target.handleIncomingDisconnection()
219
- this.close(target)
220
- } else {
221
- // this is the 'ack' of the CloseOperation to the original closer
222
- this.associations.delete(target.connectionId)
223
- this.associations.delete(operation.association.sourceConnection.connectionId)
224
- }
225
- }
226
-
227
- private executeSendOperation(operation: SendOperation): void {
228
-
229
- if (this.stopped) {
230
- return
231
- }
232
-
233
- const target = operation.association.destinationConnection
234
- target!.handleIncomingData(operation.data)
235
-
236
- }
237
-
238
- private executeQueuedOperations(): void {
239
- const currentTime = Date.now()
240
- while (this.operationQueue.size() > 0
241
- && this.operationQueue.peek()!.executionTime <= currentTime) {
242
- const operation = this.operationQueue.pop()
243
-
244
- if (operation instanceof ConnectOperation) {
245
- this.executeConnectOperation(operation)
246
- } else if (operation instanceof CloseOperation) {
247
- this.executeCloseOperation(operation)
248
- } else if (operation instanceof SendOperation) {
249
- this.executeSendOperation(operation)
250
- } else {
251
- logger.error('Unknown SimulatorOperation')
252
- }
253
-
254
- this.loopCounter++
255
- if (this.loopCounter >= this.MAX_LOOPS) {
256
- this.loopCounter = 0
257
- setTimeout(() => this.executeQueuedOperations(), 0)
258
- return
259
- }
260
- }
261
-
262
- this.scheduleNextTimeout()
263
- }
264
-
265
- private scheduleNextTimeout(): void {
266
- if (this.simulatorTimeout) {
267
- clearTimeout(this.simulatorTimeout)
268
- this.simulatorTimeout = undefined
269
- }
270
-
271
- const currentTime = Date.now()
272
-
273
- const firstOperation = this.operationQueue.peek()
274
-
275
- if (!firstOperation) {
276
- return
277
- }
278
-
279
- const firstOperationTime = firstOperation.executionTime
280
- const timeDifference = firstOperationTime - currentTime
281
-
282
- this.simulatorTimeout = setTimeout(this.executeQueuedOperations, timeDifference)
283
- }
284
-
285
- private scheduleOperation(operation: SimulatorOperation) {
286
- this.operationQueue.push(operation)
287
- this.scheduleNextTimeout()
288
- }
289
-
290
- public connect(sourceConnection: SimulatorConnection, targetDescriptor: PeerDescriptor, connectedCallback: (error?: string) => void): void {
291
-
292
- if (this.stopped) {
293
- logger.error('connect() called on a stopped simulator ' + (new Error().stack))
294
- return
295
- }
296
- debugVars['simulatorHeapSize'] = this.operationQueue.size()
297
-
298
- const association = new Association(sourceConnection, undefined, connectedCallback)
299
- this.associations.set(sourceConnection.connectionId, association)
300
-
301
- const executionTime = this.generateExecutionTime(association, sourceConnection.localPeerDescriptor.region, targetDescriptor.region)
302
- association.setLastOperationAt(executionTime)
303
-
304
- const operation = new ConnectOperation(executionTime, association,
305
- sourceConnection, targetDescriptor)
306
-
307
- this.scheduleOperation(operation)
308
- }
309
-
310
- public close(sourceConnection: SimulatorConnection): void {
311
-
312
- if (this.stopped) {
313
- return
314
- }
315
-
316
- const association = this.associations.get(sourceConnection.connectionId)
317
- if (!association) {
318
- return
319
- }
320
-
321
- association.setClosing()
322
-
323
- const executionTime = this.generateExecutionTime(association,
324
- sourceConnection.localPeerDescriptor.region,
325
- sourceConnection.getPeerDescriptor()?.region)
326
- association.setLastOperationAt(executionTime)
327
-
328
- const operation = new CloseOperation(executionTime, association)
329
-
330
- this.scheduleOperation(operation)
331
- }
332
-
333
- public send(sourceConnection: SimulatorConnection, data: Uint8Array): void {
334
-
335
- if (this.stopped) {
336
- return
337
- }
338
-
339
- const association = this.associations.get(sourceConnection.connectionId)
340
- if (!association) {
341
- return
342
- }
343
-
344
- if (association.isClosing()) {
345
- logger.trace('Tried to call send() on a closing association')
346
- return
347
- }
348
-
349
- const executionTime = this.generateExecutionTime(association,
350
- sourceConnection.localPeerDescriptor.region,
351
- association.destinationConnection!.localPeerDescriptor.region)
352
-
353
- association.setLastOperationAt(executionTime)
354
-
355
- const operation = new SendOperation(executionTime, association, data)
356
-
357
- this.scheduleOperation(operation)
358
- }
359
-
360
- public stop(): void {
361
- this.stopped = true
362
- logger.info(this.associations.size + ' associations in the beginning of stop()')
363
-
364
- if (this.simulatorTimeout) {
365
- clearTimeout(this.simulatorTimeout)
366
- }
367
- }
368
- }