Skip to content

Commit

Permalink
chore: address review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Oct 13, 2020
1 parent e309d53 commit aae492f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 25 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ js-multiaddr
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api)
- [Resolvers](#resolvers)
- [Contribute](#contribute)
- [License](#license)

Expand Down Expand Up @@ -117,6 +118,25 @@ $ node

https://multiformats.github.io/js-multiaddr/

## Resolvers

`multiaddr` allows multiaddrs to be resolved when appropriate resolvers are provided. This module already has resolvers available, but you can also create your own. To provide multiaddr resolvers you can do:

```js
const multiaddr = require('multiaddr')
const resolvers = require('multiaddr/src/resolvers')

multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
```

The available resolvers are:

| Name | type | Description |
|-------------|------|-------------|
| `dnsaddrResolver` | `dnsaddr` | dnsaddr resolution with TXT Records |

A resolver receives a `Multiaddr` as a parameter and returns a `Promise<Array<string>>`.

## Contribute

Contributions welcome. Please check out [the issues](https://github.com/multiformats/js-multiaddr/issues).
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"docs": "aegir docs",
"size": "aegir build -b"
},
"browser": {
"./src/resolvers/dns.js": "./src/resolvers/dns.browser.js"
},
"files": [
"src",
"dist"
Expand Down
6 changes: 4 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ declare class Multiaddr {

bytes: Uint8Array;

resolvers: Map<string, (addr: Multiaddr) => Promise<Array<string>>>

/**
* Returns Multiaddr as a String
*/
Expand Down Expand Up @@ -162,6 +160,8 @@ declare class Multiaddr {
}

declare namespace Multiaddr {
const resolvers: Map < string, (addr: Multiaddr) => Promise < Array < string >>>

/**
* Creates a Multiaddr from a node-friendly address object
*/
Expand Down Expand Up @@ -201,4 +201,6 @@ declare namespace Multiaddr {
*/
declare function Multiaddr(input?: MultiaddrInput): Multiaddr;



export = Multiaddr;
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const inspect = Symbol.for('nodejs.util.inspect.custom')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayEquals = require('uint8arrays/equals')

const resolvers = new Map()

/**
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
* a Uint8Array, String or another Multiaddr instance
Expand Down Expand Up @@ -46,8 +48,6 @@ const Multiaddr = withIs.proto(function (addr) {
} else {
throw new Error('addr must be a string, Buffer, or another Multiaddr')
}

this.resolvers = new Map()
}, { className: 'Multiaddr', symbolName: '@multiformats/js-multiaddr/multiaddr' })

/**
Expand Down Expand Up @@ -392,7 +392,7 @@ Multiaddr.prototype.resolve = async function resolve () {
return [this]
}

const resolver = this.resolvers.get(resolvableProto.name)
const resolver = resolvers.get(resolvableProto.name)
if (!resolver) {
throw errCode(new Error(`no available resolver for ${resolvableProto.name}`), 'ERR_NO_AVAILABLE_RESOLVER')
}
Expand Down Expand Up @@ -551,4 +551,5 @@ Multiaddr.resolve = function resolve (addr) {
return Promise.reject(new Error('not implemented yet'))
}

Multiaddr.resolvers = resolvers
exports = module.exports = Multiaddr
3 changes: 0 additions & 3 deletions src/resolvers/dns.browser.js

This file was deleted.

10 changes: 9 additions & 1 deletion src/resolvers/dns.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
'use strict'

module.exports = require('dns').promises
let dns

try {
dns = require('dns').promises
} catch (err) {
dns = require('dns-over-http-resolver')
}

module.exports = dns
5 changes: 4 additions & 1 deletion src/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

const Multiaddr = require('..') // eslint-disable-line no-unused-vars
const protocols = require('../protocols-table')

const { code: dnsaddrCode } = protocols('dnsaddr')

/**
* Resolver for dnsaddr addresses.
Expand All @@ -13,7 +16,7 @@ async function dnsaddrResolver (addr) {
const resolver = new Resolver()

const peerId = addr.getPeerId()
const hostname = addr.toString().split('dnsaddr')[1].split('/')[1]
const [, hostname] = addr.stringTuples().find(([proto]) => proto === dnsaddrCode) || []

const records = await resolver.resolveTxt(`_dnsaddr.${hostname}`)
// @ts-ignore
Expand Down
29 changes: 17 additions & 12 deletions test/resolvers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ const dnsaddrStub2 = [
]

describe('multiaddr resolve', () => {
it('should throw if no resolver is available', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')

// Resolve
await expect(ma.resolve()).to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NO_AVAILABLE_RESOLVER')
})

describe('dnsaddr', () => {
afterEach(() => {
sinon.restore()
before(() => {
// Set resolvers
multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
})

it('should throw if no resolver is available', async () => {
const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')

// Resolve
await expect(ma.resolve()).to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NO_AVAILABLE_RESOLVER')
afterEach(() => {
sinon.restore()
})

it('can resolve dnsaddr without no peerId', async () => {
Expand All @@ -47,7 +52,7 @@ describe('multiaddr resolve', () => {
stub.onCall(0).returns(Promise.resolve(dnsaddrStub1))

// Set resolvers
ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
// ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)

// Resolve
const resolvedMas = await ma.resolve()
Expand All @@ -68,7 +73,7 @@ describe('multiaddr resolve', () => {
stub.onCall(1).returns(Promise.resolve(dnsaddrStub2))

// Set resolvers
ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
// ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)

// Resolve
const resolvedMas = await ma.resolve()
Expand All @@ -85,12 +90,12 @@ describe('multiaddr resolve', () => {
stub.onCall(1).returns(Promise.resolve(dnsaddrStub2))

// Set resolvers
ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
// ma.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)

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

0 comments on commit aae492f

Please sign in to comment.