Skip to content

Commit

Permalink
chore: revert recursive option for now
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Oct 12, 2020
1 parent f064934 commit a944939
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ declare class Multiaddr {
/**
* Resolve multiaddr if containing resolvable hostname.
*/
resolve(options?: object): Promise<Array<Multiaddr>>
resolve(): Promise<Array<Multiaddr>>
}

declare namespace Multiaddr {
Expand Down
17 changes: 2 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ Multiaddr.prototype.equals = function equals (addr) {
* Resolve multiaddr if containing resolvable hostname.
*
* @param {object} options
* @param {bool} [options.recursive = true] recursive resolve until no resolvable multiaddr reached
* @returns {Promise<Array<Multiaddr>>}
* @example
* const mh1 = Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
Expand All @@ -385,7 +384,7 @@ Multiaddr.prototype.equals = function equals (addr) {
* // <Multiaddr 04934b535391020fa1cc03a503221220c10f9319dac35c270a6b74cd644cb3acfc1f6efc8c821f8eb282599fd1814f64 - /ip4/147.75.83.83/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb>
* // ]
*/
Multiaddr.prototype.resolve = async function resolve ({ recursive = true } = {}) {
Multiaddr.prototype.resolve = async function resolve () {
const resolvableProto = this.protos().find((p) => p.resolvable)

// Multiaddr is not resolvable?
Expand All @@ -399,19 +398,7 @@ Multiaddr.prototype.resolve = async function resolve ({ recursive = true } = {})
}

const addresses = await resolver(this)
const newMultiaddrs = addresses.map(a => Multiaddr(a))

if (!recursive) {
return newMultiaddrs
}

// Inject Resolvers and resolve
const recursiveMultiaddrs = await Promise.all(newMultiaddrs.map((nm) => {
nm.resolvers = this.resolvers
return nm.resolve()
}))

return recursiveMultiaddrs.flat()
return addresses.map(a => Multiaddr(a))
}

/**
Expand Down
30 changes: 18 additions & 12 deletions test/resolvers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('multiaddr resolve', () => {
ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)

// Resolve
const resolvedMas = await ma.resolve({ recursive: false })
const resolvedMas = await ma.resolve()

expect(resolvedMas).to.have.length(dnsaddrStub1.length)
resolvedMas.forEach((ma, index) => {
Expand All @@ -60,7 +60,7 @@ describe('multiaddr resolve', () => {
})
})

it('can resolve dnsaddr with peerId recursively', async () => {
it('can resolve dnsaddr with peerId', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')

const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
Expand All @@ -73,15 +73,11 @@ describe('multiaddr resolve', () => {
// Resolve
const resolvedMas = await ma.resolve()

expect(resolvedMas).to.have.length(dnsaddrStub2.length)
resolvedMas.forEach((ma, index) => {
const stubAddr = dnsaddrStub2[index][0].split('=')[1]

expect(ma.equals(multiaddr(stubAddr))).to.equal(true)
})
expect(resolvedMas).to.have.length(1)
expect(resolvedMas[0].equals(multiaddr('/dnsaddr/ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'))).to.eql(true)
})

it('can resolve dnsaddr with peerId not recursively', async () => {
it('can resolve dnsaddr with peerId two levels', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')

const stub = sinon.stub(Resolver.prototype, 'resolveTxt')
Expand All @@ -92,10 +88,20 @@ describe('multiaddr resolve', () => {
ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)

// Resolve
const resolvedMas = await ma.resolve({ recursive: false })
const resolvedInitialMas = await ma.resolve()
const resolvedSecondMas = await Promise.all(resolvedInitialMas.map(nm => {
nm.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
return nm.resolve()
}))
// @ts-ignore
const resolvedMas = resolvedSecondMas.flat()

expect(resolvedMas).to.have.length(1)
expect(resolvedMas[0].equals(multiaddr('/dnsaddr/ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb'))).to.eql(true)
expect(resolvedMas).to.have.length(dnsaddrStub2.length)
resolvedMas.forEach((ma, index) => {
const stubAddr = dnsaddrStub2[index][0].split('=')[1]

expect(ma.equals(multiaddr(stubAddr))).to.equal(true)
})
})
})
})

0 comments on commit a944939

Please sign in to comment.