Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

fix: throw on invalid multiaddr to constructor #934

Merged
merged 10 commits into from
Jan 29, 2019
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const sendRequest = require('./utils/send-request')

function ipfsClient (hostOrMultiaddr, port, opts) {
const config = getConfig()

try {
const maddr = multiaddr(hostOrMultiaddr).nodeAddress()
config.host = maddr.address
config.port = maddr.port
} catch (e) {
if (typeof hostOrMultiaddr === 'string') {
if (typeof hostOrMultiaddr === 'string') {
if (hostOrMultiaddr[0] === '/') {
// throws if multiaddr is malformed or can't be converted to a nodeAddress
const maddr = multiaddr(hostOrMultiaddr).nodeAddress()
config.host = maddr.address
config.port = maddr.port
} else {
config.host = hostOrMultiaddr
config.port = port && typeof port !== 'object' ? port : config.port
}
Expand Down
8 changes: 8 additions & 0 deletions test/constructor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,13 @@ describe('ipfs-http-client constructor tests', () => {

clientWorks(ipfsClient(splitted[2], splitted[4], { protocol: 'http' }), done)
})

it('error in invalid mutliaddr', () => {
const splitted = apiAddr.split('/')

expect(() => ipfsClient('/dns4')).to.throw('invalid address')
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw('multiaddr must have a valid format')
})
})
})