From c940f2d3841e54103cedde55c5e7da32424b7994 Mon Sep 17 00:00:00 2001 From: robertkiel Date: Wed, 15 Apr 2020 09:52:58 +0200 Subject: [PATCH] fix: add null check in libp2p.hangUp() test: add hangup test and fix linting --- src/index.js | 13 ++++++++++--- test/dialing/direct.spec.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index ef7168a7..46943dce 100644 --- a/src/index.js +++ b/src/index.js @@ -311,10 +311,17 @@ class Libp2p extends EventEmitter { * @param {PeerInfo|PeerId|multiaddr|string} peer the peer to close connections to * @returns {Promise} */ - hangUp (peer) { + async hangUp (peer) { const peerInfo = getPeerInfo(peer, this.peerStore) - return Promise.all( - this.registrar.connections.get(peerInfo.id.toB58String()).map(connection => { + + const connections = this.registrar.connections.get(peerInfo.id.toB58String()) + + if (!connections) { + return + } + + await Promise.all( + connections.map(connection => { return connection.close() }) ) diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index f6c716cb..cbf47fc1 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -371,6 +371,19 @@ describe('Dialing (direct, WebSockets)', () => { expect(connection.stat.timeline.close).to.exist() }) + it('should be able to use hangup when no connection exists', async () => { + libp2p = new Libp2p({ + peerInfo, + modules: { + transport: [Transport], + streamMuxer: [Muxer], + connEncryption: [Crypto] + } + }) + + await libp2p.hangUp(remoteAddr) + }) + it('should abort pending dials on stop', async () => { libp2p = new Libp2p({ peerInfo,