Skip to content

Commit 6ca19c5

Browse files
authored
feat: add libp2p.connections getter (#522)
* fix: make hangup accept what the API says it does * feat: add libp2p.connections getter * chore: fix typo
1 parent 24c3ce6 commit 6ca19c5

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

doc/API.md

+31-5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ const libp2p = await Libp2p.create(options)
181181
await libp2p.stop()
182182
```
183183

184+
### connections
185+
186+
A Getter that returns a Map of the current Connections libp2p has to other peers.
187+
188+
`libp2p.connections`
189+
190+
#### Returns
191+
192+
| Type | Description |
193+
|------|-------------|
194+
| `Map<string, Array<Connection>>` | A map of [`PeerId`][peer-id] strings to [`Connection`][connection] Arrays |
195+
196+
#### Example
197+
198+
```js
199+
for (const [peerId, connections] of libp2p.connections) {
200+
for (const connection of connections) {
201+
console.log(peerId, connection.remoteAddr.toString())
202+
// Logs the PeerId string and the observed remote multiaddr of each Connection
203+
}
204+
}
205+
```
206+
184207
### dial
185208

186209
Dials to another peer in the network and establishes the connection.
@@ -191,15 +214,15 @@ Dials to another peer in the network and establishes the connection.
191214

192215
| Name | Type | Description |
193216
|------|------|-------------|
194-
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
217+
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
195218
| [options] | `Object` | dial options |
196219
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
197220

198221
#### Returns
199222

200223
| Type | Description |
201224
|------|-------------|
202-
| `Promise<Connection>` | Promise resolves with the [Connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) instance |
225+
| `Promise<Connection>` | Promise resolves with the [Connection][connection] instance |
203226

204227
#### Example
205228

@@ -226,7 +249,7 @@ Dials to another peer in the network and selects a protocol to communicate with
226249

227250
| Name | Type | Description |
228251
|------|------|-------------|
229-
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
252+
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to dial |
230253
| protocols | `String|Array<String>` | A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made. (e.g '/ipfs/bitswap/1.1.0') |
231254
| [options] | `Object` | dial options |
232255
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
@@ -259,7 +282,7 @@ Attempts to gracefully close an open connection to the given peer. If the connec
259282

260283
| Name | Type | Description |
261284
|------|------|-------------|
262-
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId](https://github.com/libp2p/js-peer-id), [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to hang up |
285+
| peer | [PeerInfo](https://github.com/libp2p/js-peer-info), [PeerId][peer-id], [multiaddr](https://github.com/multiformats/js-multiaddr), `string` | peer to hang up |
263286

264287
#### Returns
265288

@@ -355,7 +378,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e
355378

356379
| Name | Type | Description |
357380
|------|------|-------------|
358-
| peerId | [`PeerId`](https://github.com/libp2p/js-peer-id) | ID of the peer to find |
381+
| peerId | [`PeerId`][peer-id] | ID of the peer to find |
359382
| options | `Object` | operation options |
360383
| options.timeout | `number` | maximum time the query should run |
361384

@@ -773,3 +796,6 @@ console.log(peerStats.toJSON())
773796
- `['60000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 1 minute interval.
774797
- `['300000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval.
775798
- `['900000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval.
799+
800+
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
801+
[peer-id]: https://github.com/libp2p/js-peer-id

src/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ class Libp2p extends EventEmitter {
240240
return this._isStarted
241241
}
242242

243+
/**
244+
* Gets a Map of the current connections. The keys are the stringified
245+
* `PeerId` of the peer. The value is an array of Connections to that peer.
246+
* @returns {Map<string, Connection[]>}
247+
*/
248+
get connections () {
249+
return this.registrar.connections
250+
}
251+
243252
/**
244253
* Dials to the provided peer. If successful, the `PeerInfo` of the
245254
* peer will be added to the nodes `peerStore`
@@ -288,12 +297,13 @@ class Libp2p extends EventEmitter {
288297
/**
289298
* Disconnects all connections to the given `peer`
290299
*
291-
* @param {PeerId} peer The PeerId to close connections to
300+
* @param {PeerInfo|PeerId|multiaddr|string} peer the peer to close connections to
292301
* @returns {Promise<void>}
293302
*/
294303
hangUp (peer) {
304+
const peerInfo = getPeerInfo(peer, this.peerStore)
295305
return Promise.all(
296-
this.registrar.connections.get(peer.toString()).map(connection => {
306+
this.registrar.connections.get(peerInfo.id.toString()).map(connection => {
297307
return connection.close()
298308
})
299309
)

test/dialing/direct.node.js

+17
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@ describe('Dialing (direct, TCP)', () => {
296296
expect(connection.stat.timeline.close).to.exist()
297297
})
298298

299+
it('should be able to use hangup by address string to close connections', async () => {
300+
libp2p = new Libp2p({
301+
peerInfo,
302+
modules: {
303+
transport: [Transport],
304+
streamMuxer: [Muxer],
305+
connEncryption: [Crypto]
306+
}
307+
})
308+
309+
const connection = await libp2p.dial(`${remoteAddr.toString()}/p2p/${remotePeerInfo.id.toString()}`)
310+
expect(connection).to.exist()
311+
expect(connection.stat.timeline.close).to.not.exist()
312+
await libp2p.hangUp(connection.remotePeer)
313+
expect(connection.stat.timeline.close).to.exist()
314+
})
315+
299316
it('should use the protectors when provided for connecting', async () => {
300317
const protector = new Protector(swarmKeyBuffer)
301318
libp2p = new Libp2p({

test/registrar/registrar.node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ describe('registrar on dial', () => {
6161
}))
6262

6363
await libp2p.dial(remoteAddr)
64-
expect(libp2p.registrar.connections.size).to.equal(1)
64+
expect(libp2p.connections.size).to.equal(1)
6565

6666
sinon.spy(libp2p.registrar, 'close')
6767

6868
await libp2p.stop()
6969
expect(libp2p.registrar.close.callCount).to.equal(1)
70-
expect(libp2p.registrar.connections.size).to.equal(0)
70+
expect(libp2p.connections.size).to.equal(0)
7171
})
7272
})

0 commit comments

Comments
 (0)