Compare commits

..

1 Commits

Author SHA1 Message Date
ed2dbd9bea fix: return empty array when no multiaddrs are known
Returning `undefined` makes the address length check in [dialler/index.js](https://github.com/libp2p/js-libp2p/blob/master/src/dialer/index.js#L73-L75)
fail with `cannot read property length of undefined` so the change
here is to always return an array, but it might be empty if we don't
know any multiaddrs for the given peer.
2020-07-20 10:51:29 +01:00
7 changed files with 9 additions and 20 deletions

View File

@ -760,7 +760,7 @@ Get the known [`Addresses`][address] of a provided peer.
| Type | Description |
|------|-------------|
| `Array<Address>|undefined` | Array of peer's [`Addresses`][address] containing the multiaddr and its metadata if available, otherwise undefined |
| `Array<Address>` | Array of peer's [`Addresses`][address] containing the multiaddr and its metadata |
#### Example
@ -797,7 +797,7 @@ Get the known `Multiaddr` of a provided peer. All returned multiaddrs will inclu
| Type | Description |
|------|-------------|
| `Array<Multiaddr>|undefined` | Array of peer's multiaddr if available, otherwise undefined |
| `Array<Multiaddr>` | Array of peer's multiaddr |
#### Example

View File

@ -304,7 +304,7 @@ const node = await Libp2p.create({
},
config: {
peerDiscovery: {
[WebRTCStar.tag]: {
webRTCStar: {
enabled: true
}
}

View File

@ -112,7 +112,7 @@ class Dialer {
this.peerStore.addressBook.add(id, multiaddrs)
}
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(id) || []
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(id)
// If received a multiaddr to dial, it should be the first to use
// But, if we know other multiaddrs for the peer, we should try them too.

View File

@ -168,9 +168,8 @@ class AddressBook extends Book {
/**
* Get the known multiaddrs for a given peer. All returned multiaddrs
* will include the encapsulated `PeerId` of the peer.
* Returns `undefined` if there are no known multiaddrs for the given peer.
* @param {PeerId} peerId
* @returns {Array<Multiaddr>|undefined}
* @returns {Array<Multiaddr>}
*/
getMultiaddrsForPeer (peerId) {
if (!PeerId.isPeerId(peerId)) {
@ -180,7 +179,7 @@ class AddressBook extends Book {
const record = this.data.get(peerId.toB58String())
if (!record) {
return undefined
return []
}
return record.map((address) => {

View File

@ -77,9 +77,8 @@ class Book {
/**
* Get the known data of a provided peer.
* Returns `undefined` if there is no available data for the given peer.
* @param {PeerId} peerId
* @returns {Array<Data>|undefined}
* @returns {Array<Data>}
*/
get (peerId) {
if (!PeerId.isPeerId(peerId)) {

View File

@ -96,15 +96,6 @@ describe('Dialing (direct, TCP)', () => {
.and.to.have.nested.property('._errors[0].code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
})
it('should fail to connect if peer has no known addresses', async () => {
const dialer = new Dialer({ transportManager: localTM, peerStore })
const peerId = await PeerId.createFromJSON(Peers[1])
await expect(dialer.connectToPeer(peerId))
.to.eventually.be.rejectedWith(Error)
.and.to.have.nested.property('.code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
})
it('should be able to connect to a given peer id', async () => {
const peerStore = new PeerStore()
const dialer = new Dialer({

View File

@ -323,10 +323,10 @@ describe('addressBook', () => {
throw new Error('invalid peerId should throw error')
})
it('returns undefined if no multiaddrs are known for the provided peer', () => {
it('returns empty array if no multiaddrs are known for the provided peer', () => {
const addresses = ab.getMultiaddrsForPeer(peerId)
expect(addresses).to.not.exist()
expect(addresses).to.be.empty()
})
it('returns the multiaddrs stored', () => {