refactor: consolidation multiaddr dial methods

This commit is contained in:
Jacob Heun 2019-12-04 17:05:09 +01:00
parent a37c5c0144
commit c4be5f4aaf
No known key found for this signature in database
GPG Key ID: CA5A94C15809879F
3 changed files with 9 additions and 22 deletions

View File

@ -41,31 +41,18 @@ class Dialer {
this.tokens = [...new Array(concurrency)].map((_, index) => index) 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<Connection>}
*/
connectToMultiaddr (addr, options = {}) {
addr = multiaddr(addr)
return this.connectToMultiaddrs([addr], options)
}
/** /**
* Connects to the first success of a given list of `Multiaddr`. `addrs` should * 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. * include the id of the peer being dialed, it will be used for encryption verification.
* *
* @param {Array<Multiaddr>} addrs * @param {Array<Multiaddr>|Multiaddr} addrs
* @param {object} [options] * @param {object} [options]
* @param {AbortSignal} [options.signal] An AbortController signal * @param {AbortSignal} [options.signal] An AbortController signal
* @returns {Promise<Connection>} * @returns {Promise<Connection>}
*/ */
async connectToMultiaddrs (addrs, options = {}) { async connectToMultiaddr (addrs, options = {}) {
if (!Array.isArray(addrs)) addrs = [multiaddr(addrs)]
const dialAction = (addr, options) => { const dialAction = (addr, options) => {
if (options.signal.aborted) throw errCode(new Error('already aborted'), 'ERR_ALREADY_ABORTED') if (options.signal.aborted) throw errCode(new Error('already aborted'), 'ERR_ALREADY_ABORTED')
return this.transportManager.dial(addr, options) return this.transportManager.dial(addr, options)
@ -112,7 +99,7 @@ class Dialer {
// TODO: ensure the peer id is on the multiaddr // TODO: ensure the peer id is on the multiaddr
return this.connectToMultiaddrs(addrs, options) return this.connectToMultiaddr(addrs, options)
} }
getTokens (num) { getTokens (num) {

View File

@ -165,7 +165,7 @@ describe('Dialing (direct, TCP)', () => {
}) })
// Perform 3 multiaddr dials // Perform 3 multiaddr dials
dialer.connectToMultiaddrs([remoteAddr, remoteAddr, remoteAddr]) dialer.connectToMultiaddr([remoteAddr, remoteAddr, remoteAddr])
// Let the call stack run // Let the call stack run
await delay(0) 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) const remotePeer = new PeerInfo(remoteLibp2p.peerInfo.id)
remotePeer.multiaddrs.add(remoteAddr) remotePeer.multiaddrs.add(remoteAddr)
@ -262,7 +262,7 @@ describe('Dialing (direct, TCP)', () => {
expect(stream).to.exist() expect(stream).to.exist()
expect(protocol).to.equal('/echo/1.0.0') expect(protocol).to.equal('/echo/1.0.0')
await connection.close() 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 () => { it('should be able to use hangup to close connections', async () => {

View File

@ -153,7 +153,7 @@ describe('Dialing (direct, WebSockets)', () => {
}) })
// Perform 3 multiaddr dials // Perform 3 multiaddr dials
dialer.connectToMultiaddrs([remoteAddr, remoteAddr, remoteAddr]) dialer.connectToMultiaddr([remoteAddr, remoteAddr, remoteAddr])
// Let the call stack run // Let the call stack run
await delay(0) await delay(0)