@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,137 +0,0 @@
1
- import { ConnectionType, IConnection } from '../IConnection'
2
- import { Simulator } from './Simulator'
3
- import { Message, PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
4
- import { Connection } from '../Connection'
5
- import { Logger } from '@streamr/utils'
6
- import { protoToString } from '../../helpers/protoToString'
7
- import { toNodeId } from '../../identifiers'
8
-
9
- const logger = new Logger(module)
10
-
11
- export class SimulatorConnection extends Connection implements IConnection {
12
-
13
- private stopped = false
14
- public localPeerDescriptor: PeerDescriptor
15
- private targetPeerDescriptor: PeerDescriptor
16
- private simulator: Simulator
17
-
18
- constructor(
19
- localPeerDescriptor: PeerDescriptor,
20
- targetPeerDescriptor: PeerDescriptor,
21
- connectionType: ConnectionType,
22
- simulator: Simulator
23
- ) {
24
- super(connectionType)
25
-
26
- this.localPeerDescriptor = localPeerDescriptor
27
- this.setPeerDescriptor(targetPeerDescriptor)
28
- this.targetPeerDescriptor = targetPeerDescriptor
29
- this.connectionType = connectionType
30
- this.simulator = simulator
31
-
32
- this.send = this.send.bind(this)
33
- this.close = this.close.bind(this)
34
- this.connect = this.connect.bind(this)
35
- this.handleIncomingData = this.handleIncomingData.bind(this)
36
- this.handleIncomingDisconnection = this.handleIncomingDisconnection.bind(this)
37
- this.destroy = this.destroy.bind(this)
38
- this.doDisconnect = this.doDisconnect.bind(this)
39
- }
40
-
41
- public send(data: Uint8Array): void {
42
- logger.trace('send()')
43
- if (!this.stopped) {
44
-
45
- this.simulator.send(this, data)
46
-
47
- } else {
48
- const localNodeId = toNodeId(this.localPeerDescriptor)
49
- const targetNodeId = toNodeId(this.targetPeerDescriptor)
50
- logger.error(localNodeId + ', ' + targetNodeId + 'tried to send() on a stopped connection')
51
- }
52
- }
53
-
54
- public async close(gracefulLeave: boolean): Promise<void> {
55
- const localNodeId = toNodeId(this.localPeerDescriptor)
56
- const targetNodeId = toNodeId(this.targetPeerDescriptor)
57
-
58
- logger.trace(localNodeId + ', ' + targetNodeId + ' close()')
59
- if (!this.stopped) {
60
- logger.trace(localNodeId + ', ' + targetNodeId + ' close() not stopped')
61
- this.stopped = true
62
-
63
- try {
64
- logger.trace(localNodeId + ', ' + targetNodeId + ' close() calling simulator.disconnect()')
65
- this.simulator.close(this)
66
- logger.trace(localNodeId + ', ' + targetNodeId + ' close() simulator.disconnect returned')
67
- } catch (e) {
68
- logger.trace(localNodeId + ', ' + targetNodeId + 'close aborted' + e)
69
- } finally {
70
- logger.trace(localNodeId + ', ' + targetNodeId + ' calling this.doDisconnect')
71
- this.doDisconnect(gracefulLeave)
72
- }
73
-
74
- } else {
75
- logger.trace(localNodeId + ', ' + targetNodeId + ' close() tried to close a stopped connection')
76
- }
77
- }
78
-
79
- public connect(): void {
80
- if (!this.stopped) {
81
- logger.trace('connect() called')
82
-
83
- this.simulator.connect(this, this.targetPeerDescriptor, (error?: string) => {
84
- if (error !== undefined) {
85
- logger.trace(error)
86
- this.doDisconnect(false)
87
- } else {
88
- this.emit('connected')
89
- }
90
- })
91
- } else {
92
- logger.trace('tried to connect() a stopped connection')
93
- }
94
- }
95
-
96
- public handleIncomingData(data: Uint8Array): void {
97
- if (!this.stopped) {
98
- logger.trace('handleIncomingData() ' + protoToString(Message.fromBinary(data), Message))
99
- this.emit('data', data)
100
- } else {
101
- logger.trace('tried to call handleIncomingData() a stopped connection')
102
- }
103
- }
104
-
105
- public handleIncomingDisconnection(): void {
106
- if (!this.stopped) {
107
- const localNodeId = toNodeId(this.localPeerDescriptor)
108
- logger.trace(localNodeId + ' handleIncomingDisconnection()')
109
- this.stopped = true
110
- this.doDisconnect(false)
111
- } else {
112
- logger.trace('tried to call handleIncomingDisconnection() a stopped connection')
113
- }
114
- }
115
-
116
- public destroy(): void {
117
- const localNodeId = toNodeId(this.localPeerDescriptor)
118
- if (!this.stopped) {
119
- logger.trace(localNodeId + ' destroy()')
120
- this.removeAllListeners()
121
- this.close(false).catch((_e) => { })
122
- } else {
123
- logger.trace(localNodeId + ' tried to call destroy() a stopped connection')
124
- }
125
- }
126
-
127
- private doDisconnect(gracefulLeave: boolean) {
128
- const localNodeId = toNodeId(this.localPeerDescriptor)
129
- const targetNodeId = toNodeId(this.targetPeerDescriptor)
130
- logger.trace(localNodeId + ' doDisconnect()')
131
- this.stopped = true
132
-
133
- logger.trace(localNodeId + ', ' + targetNodeId + ' doDisconnect emitting')
134
-
135
- this.emit('disconnected', gracefulLeave)
136
- }
137
- }
@@ -1,98 +0,0 @@
1
- import { ConnectionType } from '../IConnection'
2
-
3
- import {
4
- HandshakeError,
5
- PeerDescriptor,
6
- } from '../../../generated/packages/dht/protos/DhtRpc'
7
- import { Logger } from '@streamr/utils'
8
- import { Simulator } from './Simulator'
9
- import { SimulatorConnection } from './SimulatorConnection'
10
- import { DhtAddress, toNodeId } from '../../identifiers'
11
- import { acceptHandshake, createIncomingHandshaker, createOutgoingHandshaker, rejectHandshake } from '../Handshaker'
12
- import { PendingConnection } from '../PendingConnection'
13
-
14
- const logger = new Logger(module)
15
-
16
- export class SimulatorConnector {
17
-
18
- private connectingConnections: Map<DhtAddress, PendingConnection> = new Map()
19
- private stopped = false
20
- private localPeerDescriptor: PeerDescriptor
21
- private simulator: Simulator
22
- private onNewConnection: (connection: PendingConnection) => boolean
23
-
24
- constructor(
25
- localPeerDescriptor: PeerDescriptor,
26
- simulator: Simulator,
27
- onNewConnection: (connection: PendingConnection) => boolean,
28
- ) {
29
- this.localPeerDescriptor = localPeerDescriptor
30
- this.simulator = simulator
31
- this.onNewConnection = onNewConnection
32
- }
33
-
34
- public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
35
- logger.trace('connect() ' + toNodeId(targetPeerDescriptor))
36
- const nodeId = toNodeId(targetPeerDescriptor)
37
- const existingConnection = this.connectingConnections.get(nodeId)
38
- if (existingConnection) {
39
- return existingConnection
40
- }
41
-
42
- const connection = new SimulatorConnection(this.localPeerDescriptor, targetPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, this.simulator)
43
-
44
- const pendingConnection = new PendingConnection(targetPeerDescriptor)
45
- createOutgoingHandshaker(this.localPeerDescriptor, pendingConnection, connection, targetPeerDescriptor)
46
- this.connectingConnections.set(nodeId, pendingConnection)
47
- const delFunc = () => {
48
- this.connectingConnections.delete(nodeId)
49
- connection.off('disconnected', delFunc)
50
- pendingConnection.off('connected', delFunc)
51
- pendingConnection.off('disconnected', delFunc)
52
- }
53
- connection.once('disconnected', delFunc)
54
- pendingConnection.once('connected', delFunc)
55
- pendingConnection.once('disconnected', delFunc)
56
-
57
- connection.connect()
58
- return pendingConnection
59
- }
60
-
61
- public getPeerDescriptor(): PeerDescriptor {
62
- return this.localPeerDescriptor
63
- }
64
-
65
- public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
66
- // connection is incoming, so remotePeerDescriptor is localPeerDescriptor
67
- const remotePeerDescriptor = sourceConnection.localPeerDescriptor
68
- const remoteNodeId = toNodeId(sourceConnection.localPeerDescriptor)
69
- logger.trace(remoteNodeId + ' incoming connection, stopped: ' + this.stopped)
70
- if (this.stopped) {
71
- return
72
- }
73
- const connection = new SimulatorConnection(this.localPeerDescriptor, remotePeerDescriptor, ConnectionType.SIMULATOR_SERVER, this.simulator)
74
-
75
- const pendingConnection = new PendingConnection(remotePeerDescriptor)
76
- const handshaker = createIncomingHandshaker(this.localPeerDescriptor, pendingConnection, connection)
77
- logger.trace('connected')
78
-
79
- handshaker.once('handshakeRequest', () => {
80
- logger.trace(remoteNodeId + ' incoming handshake request')
81
-
82
- if (this.onNewConnection(pendingConnection)) {
83
- logger.trace(remoteNodeId + ' calling acceptHandshake')
84
- acceptHandshake(handshaker, pendingConnection, connection)
85
- } else {
86
- rejectHandshake(pendingConnection, connection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
87
- }
88
- })
89
-
90
- this.simulator.accept(sourceConnection, connection)
91
- }
92
-
93
- public async stop(): Promise<void> {
94
- this.stopped = true
95
- const conns = Array.from(this.connectingConnections.values())
96
- await Promise.allSettled(conns.map(async (conn) => conn.close(false)))
97
- }
98
- }
@@ -1,15 +0,0 @@
1
- import { MetricsContext } from '@streamr/utils'
2
- import { PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
3
- import { ConnectionManager } from '../ConnectionManager'
4
- import { Simulator } from './Simulator'
5
- import { SimulatorConnectorFacade } from '../ConnectorFacade'
6
-
7
- export class SimulatorTransport extends ConnectionManager {
8
- constructor(localPeerDescriptor: PeerDescriptor, simulator: Simulator) {
9
- super({
10
- createConnectorFacade: () => new SimulatorConnectorFacade(localPeerDescriptor, simulator),
11
- metricsContext: new MetricsContext(),
12
- allowIncomingPrivateConnections: false
13
- })
14
- }
15
- }
@@ -1,42 +0,0 @@
1
- /* eslint-disable max-len, @typescript-eslint/prefer-for-of */
2
-
3
- // This file contains a 16x16 matrix of ping times measured between
4
- // amazon regions. Converted to typescript from the file pings.csv
5
- // in order to make loading into browser easier.
6
-
7
- export enum RegionIndex { eu_central_1, eu_west_1, eu_west_2, eu_west_3, eu_north_1, us_east_1, us_east_2, us_west_1, us_west_2, ca_central_1, ap_south_1, ap_northeast_2, ap_southeast_1, ap_southeast_2, ap_northeast_1, sa_east_1 }
8
-
9
- export const regionPingMatrix = [
10
- [0.325, 23.163, 12.942, 8.923, 20.699, 89.208, 99.98, 150.077, 139.224, 90.454, 117.0, 226.823, 154.592, 286.753, 222.719, 205.563],
11
- [24.911, 0.195, 10.022, 18.544, 39.802, 66.161, 93.877, 136.271, 127.362, 73.733, 122.187, 229.064, 177.256, 254.277, 200.207, 186.817],
12
- [12.879, 9.994, 0.167, 8.626, 31.896, 74.663, 85.231, 136.185, 125.328, 77.811, 111.29, 236.34, 164.357, 262.577, 207.745, 187.383],
13
- [9.007, 16.823, 8.761, 0.191, 28.449, 81.969, 92.599, 140.961, 132.269, 85.444, 107.421, 243.981, 167.17, 279.322, 214.088, 197.986],
14
- [20.661, 40.0, 31.176, 27.645, 0.137, 104.171, 115.704, 165.01, 155.741, 107.635, 136.75, 267.321, 173.951, 292.266, 238.492, 216.048],
15
- [89.076, 67.701, 75.414, 81.598, 104.866, 0.284, 11.501, 61.662, 78.612, 14.115, 186.697, 174.546, 210.93, 196.071, 145.622, 114.919],
16
- [100.018, 93.968, 85.06, 92.582, 115.719, 11.514, 0.184, 51.668, 48.447, 25.808, 195.433, 160.031, 197.27, 186.913, 133.115, 127.954],
17
- [148.544, 137.18, 136.137, 141.333, 164.73, 60.257, 50.886, 0.197, 21.509, 77.668, 229.32, 134.858, 174.472, 137.329, 107.566, 175.944],
18
- [137.885, 128.304, 125.316, 132.271, 155.726, 63.376, 48.457, 20.743, 0.293, 59.645, 218.285, 126.988, 162.72, 138.599, 100.203, 174.612],
19
- [90.494, 78.203, 77.998, 84.348, 109.549, 14.136, 25.917, 77.323, 59.692, 0.163, 187.854, 169.253, 206.944, 195.467, 141.054, 126.962],
20
- [120.19, 119.978, 112.023, 107.434, 139.361, 185.043, 195.679, 233.008, 216.025, 186.888, 0.177, 130.21, 56.019, 145.643, 128.57, 298.98],
21
- [226.296, 229.187, 237.024, 243.839, 266.919, 174.727, 160.066, 136.623, 124.799, 169.854, 129.131, 0.189, 81.065, 139.967, 33.24, 284.578],
22
- [154.691, 178.279, 170.997, 154.261, 173.776, 211.511, 197.551, 173.735, 162.834, 206.659, 56.964, 68.853, 0.215, 91.601, 69.684, 323.146],
23
- [289.784, 253.234, 261.752, 277.504, 292.125, 198.099, 185.709, 136.413, 138.613, 194.986, 142.484, 147.706, 91.615, 0.156, 114.518, 311.12],
24
- [223.926, 201.084, 210.133, 216.779, 237.762, 145.336, 132.887, 108.26, 98.057, 142.613, 122.716, 33.185, 68.547, 114.901, 0.162, 257.403],
25
- [205.555, 178.082, 186.533, 195.774, 216.701, 117.089, 128.001, 175.761, 176.018, 125.551, 303.904, 285.782, 323.15, 309.618, 255.904, 0.262]
26
- ]
27
-
28
- export function getRegionDelayMatrix(): Array<Array<number>> {
29
- const ret = []
30
- for (let i = 0; i < regionPingMatrix.length; i++) {
31
- const row = []
32
- for (let j = 0; j < regionPingMatrix[i].length; j++) {
33
- row.push(regionPingMatrix[i][j] / 2)
34
- }
35
- ret.push(row)
36
- }
37
- return ret
38
- }
39
-
40
- export function getRandomRegion(): number {
41
- return Math.floor(Math.random() * 15)
42
- }
@@ -1,242 +0,0 @@
1
- import EventEmitter from 'eventemitter3'
2
- import { WebrtcConnectionEvents, IWebrtcConnection, RtcDescription } from './IWebrtcConnection'
3
- import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
4
- import { Logger } from '@streamr/utils'
5
- import { EARLY_TIMEOUT, IceServer } from './WebrtcConnector'
6
- import { createRandomConnectionId } from '../Connection'
7
-
8
- enum DisconnectedRtcPeerConnectionStateEnum {
9
- DISCONNECTED = 'disconnected',
10
- FAILED = 'failed',
11
- CLOSED = 'closed',
12
- }
13
-
14
- const logger = new Logger(module)
15
-
16
- type Events = WebrtcConnectionEvents & ConnectionEvents
17
-
18
- interface Params {
19
- iceServers?: IceServer[]
20
- }
21
-
22
- export class NodeWebrtcConnection extends EventEmitter<Events> implements IWebrtcConnection, IConnection {
23
-
24
- public connectionId: ConnectionID
25
- public readonly connectionType: ConnectionType = ConnectionType.WEBRTC
26
- // We need to keep track of connection state ourselves because
27
- // RTCPeerConnection.connectionState is not supported on Firefox
28
- private lastState: RTCPeerConnectionState = 'connecting'
29
- private readonly iceServers: IceServer[]
30
- private peerConnection?: RTCPeerConnection
31
- private dataChannel?: RTCDataChannel
32
- private makingOffer = false
33
- private isOffering = false
34
- private closed = false
35
- private earlyTimeout: NodeJS.Timeout
36
-
37
- constructor(params: Params) {
38
- super()
39
- this.connectionId = createRandomConnectionId()
40
- this.iceServers = params.iceServers ?? []
41
- this.earlyTimeout = setTimeout(() => {
42
- this.doClose(false, 'timed out due to remote descriptor not being set')
43
- }, EARLY_TIMEOUT)
44
- }
45
-
46
- public start(isOffering: boolean): void {
47
- this.isOffering = isOffering
48
- const urls: RTCIceServer[] = this.iceServers.map(({ url, port, username, password }) => ({
49
- urls: `${url}:${port}`,
50
- username,
51
- credential: password
52
- }))
53
- this.peerConnection = new RTCPeerConnection({ iceServers: urls })
54
-
55
- this.peerConnection.onicecandidate = (event) => {
56
- if ((event.candidate !== null) && (event.candidate.sdpMid !== null)) {
57
- this.emit('localCandidate', event.candidate.candidate, event.candidate.sdpMid)
58
- }
59
- }
60
-
61
- this.peerConnection.onicegatheringstatechange = () => {
62
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
63
- logger.trace(`conn.onGatheringStateChange: ${this.peerConnection?.iceGatheringState}`)
64
- }
65
-
66
- this.peerConnection.onconnectionstatechange = () => this.onStateChange()
67
-
68
- if (isOffering) {
69
- this.peerConnection.onnegotiationneeded = async () => {
70
- if (this.peerConnection !== undefined) {
71
- this.makingOffer = true
72
- try {
73
- await this.peerConnection.setLocalDescription()
74
- } catch (err) {
75
- logger.warn('Failed to set local description', { err })
76
- }
77
- if (this.peerConnection.localDescription !== null) {
78
- this.emit('localDescription', this.peerConnection.localDescription?.sdp, this.peerConnection.localDescription?.type)
79
- }
80
- }
81
- this.makingOffer = false
82
- }
83
-
84
- const dataChannel = this.peerConnection.createDataChannel('streamrDataChannel')
85
- this.setupDataChannel(dataChannel)
86
- } else {
87
- this.peerConnection.ondatachannel = (event) => {
88
- this.setupDataChannel(event.channel)
89
- }
90
- }
91
- }
92
-
93
- public async setRemoteDescription(description: string, type: string): Promise<void> {
94
- const offerCollision = (type.toLowerCase() === RtcDescription.OFFER) && (this.makingOffer || (this.peerConnection === undefined) ||
95
- this.peerConnection.signalingState != 'stable')
96
-
97
- const ignoreOffer = this.isOffering && offerCollision
98
- if (ignoreOffer) {
99
- return
100
- }
101
- try {
102
- await this.peerConnection?.setRemoteDescription({ sdp: description, type: type.toLowerCase() as RTCSdpType })
103
- clearTimeout(this.earlyTimeout)
104
- } catch (err) {
105
- logger.warn('Failed to set remote description', { err })
106
- }
107
-
108
- if ((type.toLowerCase() === RtcDescription.OFFER) && (this.peerConnection !== undefined)) {
109
- try {
110
- await this.peerConnection.setLocalDescription()
111
- } catch (err) {
112
- logger.warn('Failed to set local description', { err })
113
- }
114
- if (this.peerConnection.localDescription !== null) {
115
- this.emit('localDescription', this.peerConnection.localDescription.sdp, this.peerConnection.localDescription.type)
116
- }
117
- }
118
- }
119
-
120
- public addRemoteCandidate(candidate: string, mid: string): void {
121
- this.peerConnection?.addIceCandidate({ candidate: candidate, sdpMid: mid })
122
- .catch((err) => {
123
- logger.warn('Failed to add ICE candidate', { err })
124
- })
125
- }
126
-
127
- public isOpen(): boolean {
128
- return this.lastState === 'connected'
129
- }
130
-
131
- // IConnection implementation
132
-
133
- public async close(gracefulLeave: boolean, reason?: string): Promise<void> {
134
- this.doClose(gracefulLeave, reason)
135
- }
136
-
137
- private doClose(gracefulLeave: boolean, reason?: string): void {
138
- if (!this.closed) {
139
- this.closed = true
140
- this.lastState = 'closed'
141
- clearTimeout(this.earlyTimeout)
142
-
143
- this.stopListening()
144
- this.emit('disconnected', gracefulLeave, undefined, reason)
145
-
146
- this.removeAllListeners()
147
-
148
- if (this.dataChannel !== undefined) {
149
- try {
150
- this.dataChannel.close()
151
- } catch (err) {
152
- logger.warn('Failed to close data channel', { err })
153
- }
154
- }
155
-
156
- this.dataChannel = undefined
157
-
158
- if (this.peerConnection !== undefined) {
159
- try {
160
- this.peerConnection.close()
161
- } catch (err) {
162
- logger.warn('Failed to close connection', { err })
163
- }
164
- }
165
- this.peerConnection = undefined
166
-
167
- }
168
- }
169
-
170
- public destroy(): void {
171
- this.removeAllListeners()
172
- this.doClose(false)
173
- }
174
-
175
- public send(data: Uint8Array): void {
176
- if (this.lastState === 'connected') {
177
- this.dataChannel?.send(data as Buffer)
178
- } else {
179
- logger.warn('Tried to send on a connection with last state ' + this.lastState)
180
- }
181
- }
182
-
183
- private setupDataChannel(dataChannel: RTCDataChannel): void {
184
- this.dataChannel = dataChannel
185
- this.dataChannel.binaryType = 'arraybuffer'
186
-
187
- dataChannel.onopen = () => {
188
- logger.trace('dc.onOpen')
189
- this.onDataChannelOpen()
190
- }
191
-
192
- dataChannel.onclose = () => {
193
- logger.trace('dc.onClosed')
194
- this.doClose(false)
195
- }
196
-
197
- dataChannel.onerror = (err) => {
198
- logger.warn('Data channel error', { err })
199
- }
200
-
201
- dataChannel.onmessage = (msg) => {
202
- logger.trace('dc.onmessage')
203
- this.emit('data', new Uint8Array(msg.data))
204
- }
205
- }
206
-
207
- private stopListening() {
208
- if (this.dataChannel !== undefined) {
209
- this.dataChannel.onopen = null
210
- this.dataChannel.onclose = null
211
- this.dataChannel.onerror = null
212
- this.dataChannel.onbufferedamountlow = null
213
- this.dataChannel.onmessage = null
214
- }
215
-
216
- if (this.peerConnection !== undefined) {
217
- this.peerConnection.onconnectionstatechange = null
218
- this.peerConnection.onicecandidate = null
219
- this.peerConnection.onicegatheringstatechange = null
220
- this.peerConnection.onnegotiationneeded = null
221
- this.peerConnection.ondatachannel = null
222
- }
223
- }
224
-
225
- private onDataChannelOpen(): void {
226
- this.lastState = 'connected'
227
- this.emit('connected')
228
- }
229
-
230
- private onStateChange(): void {
231
- if (this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.CLOSED
232
- || this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.DISCONNECTED
233
- || this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.FAILED
234
- ) {
235
- this.doClose(false)
236
- }
237
- }
238
-
239
- public setConnectionId(connectionId: ConnectionID): void {
240
- this.connectionId = connectionId
241
- }
242
- }
@@ -1,24 +0,0 @@
1
- export interface WebrtcConnectionEvents {
2
- localDescription: (description: string, type: string) => void
3
- localCandidate: (candidate: string, mid: string) => void
4
- }
5
-
6
- export enum RtcDescription {
7
- OFFER = 'offer',
8
- ANSWER = 'answer'
9
- }
10
-
11
- export interface IWebrtcConnection {
12
- start(isOffering: boolean): void
13
-
14
- on(event: 'localDescription', listener: (description: string, type: string) => void): this
15
- on(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
16
- once(event: 'localDescription', listener: (description: string, type: string) => void): this
17
- once(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
18
- off(event: 'localDescription', listener: (description: string, type: string) => void): this
19
- off(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
20
-
21
- setRemoteDescription(description: string, type: string): Promise<void>
22
- addRemoteCandidate(candidate: string, mid: string): void
23
- isOpen(): boolean
24
- }