From c4be5f4aaf2e64d4548602e3aaa148fb2d6a94ac Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 4 Dec 2019 17:05:09 +0100 Subject: [PATCH] refactor: consolidation multiaddr dial methods --- src/dialer.js | 23 +++++------------------ test/dialing/direct.node.js | 6 +++--- test/dialing/direct.spec.js | 2 +- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/dialer.js b/src/dialer.js index 3cb7ae00..f8c0bc52 100644 --- a/src/dialer.js +++ b/src/dialer.js @@ -41,31 +41,18 @@ class Dialer { this.tokens = [...new Array(concurrency)].map((_, index) => index) } - /** - * Connects to a given `Multiaddr`. `addr` should include the id of the peer being - * dialed, it will be used for encryption verification. - * - * @param {Multiaddr} addr The address to dial - * @param {object} [options] - * @param {AbortSignal} [options.signal] An AbortController signal - * @returns {Promise} - */ - connectToMultiaddr (addr, options = {}) { - addr = multiaddr(addr) - - return this.connectToMultiaddrs([addr], options) - } - /** * Connects to the first success of a given list of `Multiaddr`. `addrs` should * include the id of the peer being dialed, it will be used for encryption verification. * - * @param {Array} addrs + * @param {Array|Multiaddr} addrs * @param {object} [options] * @param {AbortSignal} [options.signal] An AbortController signal * @returns {Promise} */ - async connectToMultiaddrs (addrs, options = {}) { + async connectToMultiaddr (addrs, options = {}) { + if (!Array.isArray(addrs)) addrs = [multiaddr(addrs)] + const dialAction = (addr, options) => { if (options.signal.aborted) throw errCode(new Error('already aborted'), 'ERR_ALREADY_ABORTED') return this.transportManager.dial(addr, options) @@ -112,7 +99,7 @@ class Dialer { // TODO: ensure the peer id is on the multiaddr - return this.connectToMultiaddrs(addrs, options) + return this.connectToMultiaddr(addrs, options) } getTokens (num) { diff --git a/test/dialing/direct.node.js b/test/dialing/direct.node.js index bac268c6..6df08cd6 100644 --- a/test/dialing/direct.node.js +++ b/test/dialing/direct.node.js @@ -165,7 +165,7 @@ describe('Dialing (direct, TCP)', () => { }) // Perform 3 multiaddr dials - dialer.connectToMultiaddrs([remoteAddr, remoteAddr, remoteAddr]) + dialer.connectToMultiaddr([remoteAddr, remoteAddr, remoteAddr]) // Let the call stack run await delay(0) @@ -252,7 +252,7 @@ describe('Dialing (direct, TCP)', () => { } }) - sinon.spy(libp2p.dialer, 'connectToMultiaddrs') + sinon.spy(libp2p.dialer, 'connectToMultiaddr') const remotePeer = new PeerInfo(remoteLibp2p.peerInfo.id) remotePeer.multiaddrs.add(remoteAddr) @@ -262,7 +262,7 @@ describe('Dialing (direct, TCP)', () => { expect(stream).to.exist() expect(protocol).to.equal('/echo/1.0.0') await connection.close() - expect(libp2p.dialer.connectToMultiaddrs.callCount).to.equal(1) + expect(libp2p.dialer.connectToMultiaddr.callCount).to.equal(1) }) it('should be able to use hangup to close connections', async () => { diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index 6626d54d..d4aceac1 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -153,7 +153,7 @@ describe('Dialing (direct, WebSockets)', () => { }) // Perform 3 multiaddr dials - dialer.connectToMultiaddrs([remoteAddr, remoteAddr, remoteAddr]) + dialer.connectToMultiaddr([remoteAddr, remoteAddr, remoteAddr]) // Let the call stack run await delay(0)