feat: add libp2p.connections getter (#522)

* fix: make hangup accept what the API says it does

* feat: add libp2p.connections getter

* chore: fix typo
This commit is contained in:
Jacob Heun
2019-12-16 19:24:35 +01:00
parent 4ca481b869
commit 6445fda050
4 changed files with 62 additions and 9 deletions

View File

@ -240,6 +240,15 @@ class Libp2p extends EventEmitter {
return this._isStarted
}
/**
* Gets a Map of the current connections. The keys are the stringified
* `PeerId` of the peer. The value is an array of Connections to that peer.
* @returns {Map<string, Connection[]>}
*/
get connections () {
return this.registrar.connections
}
/**
* Dials to the provided peer. If successful, the `PeerInfo` of the
* peer will be added to the nodes `peerStore`
@ -288,12 +297,13 @@ class Libp2p extends EventEmitter {
/**
* Disconnects all connections to the given `peer`
*
* @param {PeerId} peer The PeerId to close connections to
* @param {PeerInfo|PeerId|multiaddr|string} peer the peer to close connections to
* @returns {Promise<void>}
*/
hangUp (peer) {
const peerInfo = getPeerInfo(peer, this.peerStore)
return Promise.all(
this.registrar.connections.get(peer.toString()).map(connection => {
this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
return connection.close()
})
)