Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit a3389f0

Browse files
authored
Merge pull request #16 from libp2p/fix/multiaddr
fix: ensure all addresses are multiaddr
2 parents 8f43e7f + 2a27346 commit a3389f0

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
"aegir": "^8.0.0",
4141
"buffer-loader": "0.0.1",
4242
"chai": "^3.5.0",
43-
"multiaddr": "^2.0.2",
44-
"pre-commit": "^1.1.2"
43+
"pre-commit": "^1.1.3"
4544
},
4645
"dependencies": {
47-
"babel-runtime": "^6.6.1",
46+
"babel-runtime": "^6.11.6",
47+
"multiaddr": "^2.0.3",
4848
"peer-id": "^0.7.0"
4949
},
5050
"contributors": [
@@ -54,4 +54,4 @@
5454
"Stephen Whitmore <stephen.whitmore@gmail.com>",
5555
"dignifiedquire <dignifiedquire@gmail.com>"
5656
]
57-
}
57+
}

src/index.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
'use strict'
22

33
const Id = require('peer-id')
4+
const multiaddr = require('multiaddr')
45

56
exports = module.exports = Peer
67

8+
function ensureMultiaddr (addr) {
9+
if (multiaddr.isMultiaddr(addr)) {
10+
return addr
11+
}
12+
13+
return multiaddr(addr)
14+
}
15+
716
// Peer represents a peer on the IPFS network
817
function Peer (peerId) {
918
if (!(this instanceof Peer)) {
@@ -21,37 +30,43 @@ function Peer (peerId) {
2130

2231
this.multiaddr = {}
2332

24-
this.multiaddr.add = (multiaddr) => {
33+
this.multiaddr.add = (addr) => {
34+
addr = ensureMultiaddr(addr)
35+
2536
var exists = false
2637
this.multiaddrs.some((m, i) => {
27-
if (m.toString() === multiaddr.toString()) {
38+
if (m.equals(addr)) {
2839
exists = true
2940
return true
3041
}
3142
})
3243
if (!exists) {
33-
this.multiaddrs.push(multiaddr)
44+
this.multiaddrs.push(addr)
3445
}
3546
}
3647

3748
// to prevent multiaddr explosion
38-
this.multiaddr.addSafe = (multiaddr) => {
49+
this.multiaddr.addSafe = (addr) => {
50+
addr = ensureMultiaddr(addr)
51+
3952
var check = false
4053
observedMultiaddrs.some((m, i) => {
41-
if (m.toString() === multiaddr.toString()) {
42-
this.multiaddr.add(multiaddr)
54+
if (m.equals(addr)) {
55+
this.multiaddr.add(addr)
4356
observedMultiaddrs.splice(i, 1)
4457
check = true
4558
}
4659
})
4760
if (!check) {
48-
observedMultiaddrs.push(multiaddr)
61+
observedMultiaddrs.push(addr)
4962
}
5063
}
5164

52-
this.multiaddr.rm = (multiaddr) => {
65+
this.multiaddr.rm = (addr) => {
66+
addr = ensureMultiaddr(addr)
67+
5368
this.multiaddrs.some((m, i) => {
54-
if (m.toString() === multiaddr.toString()) {
69+
if (m.equals(addr)) {
5570
this.multiaddrs.splice(i, 1)
5671
return true
5772
}

test/peer-info.spec.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,63 @@ const PeerInfo = require('../src')
99
describe('peer-info', function () {
1010
this.timeout(20000)
1111

12-
it('create with Id', (done) => {
12+
it('create with Id', () => {
1313
const id = PeerId.create()
1414
const pi = new PeerInfo(id)
1515
expect(pi).to.exist
1616
expect(pi.id).to.exist
1717
expect(pi.id).to.deep.equal(id)
18-
done()
1918
})
2019

21-
it('create without passing an Id', (done) => {
20+
it('create without passing an Id', () => {
2221
const pi = new PeerInfo()
2322
expect(pi).to.exist
2423
expect(pi.id).to.exist
25-
done()
2624
})
2725

28-
it('create without "new"', (done) => {
26+
it('create without "new"', () => {
2927
const pi = PeerInfo()
3028
expect(pi).to.exist
3129
expect(pi.id).to.exist
32-
done()
3330
})
3431

35-
it('add multiaddr', (done) => {
32+
it('add multiaddr', () => {
3633
const pi = new PeerInfo()
3734
expect(pi).to.exist
3835
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
3936
pi.multiaddr.add(mh)
4037
expect(pi.multiaddrs.length).to.equal(1)
41-
done()
4238
})
4339

44-
it('add repeated multiaddr', (done) => {
40+
it('add multiaddr that are buffers', () => {
41+
const pi = new PeerInfo()
42+
expect(pi).to.exist
43+
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
44+
pi.multiaddr.add(mh.buffer)
45+
expect(pi.multiaddrs[0] instanceof Multiaddr).to.equal(true)
46+
})
47+
48+
it('add repeated multiaddr', () => {
4549
const pi = new PeerInfo()
4650
expect(pi).to.exist
4751
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
4852
pi.multiaddr.add(mh)
4953
expect(pi.multiaddrs.length).to.equal(1)
5054
pi.multiaddr.add(mh)
5155
expect(pi.multiaddrs.length).to.equal(1)
52-
done()
5356
})
5457

55-
it('rm multiaddr', (done) => {
58+
it('rm multiaddr', () => {
5659
const pi = new PeerInfo()
5760
expect(pi).to.exist
5861
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
5962
pi.multiaddr.add(mh)
6063
expect(pi.multiaddrs.length).to.equal(1)
6164
pi.multiaddr.rm(mh)
6265
expect(pi.multiaddrs.length).to.equal(0)
63-
done()
6466
})
6567

66-
it('addSafe - avoid multiaddr explosion', (done) => {
68+
it('addSafe - avoid multiaddr explosion', () => {
6769
const pi = new PeerInfo()
6870
expect(pi).to.exist
6971
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
@@ -76,10 +78,18 @@ describe('peer-info', function () {
7678
pi.multiaddr.addSafe(mh2)
7779
pi.multiaddr.addSafe(mh3)
7880
expect(pi.multiaddrs.length).to.equal(1)
79-
done()
8081
})
8182

82-
it('replace multiaddr', (done) => {
83+
it('addSafe - multiaddr that are buffers', () => {
84+
const pi = new PeerInfo()
85+
expect(pi).to.exist
86+
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
87+
pi.multiaddr.addSafe(mh.buffer)
88+
pi.multiaddr.addSafe(mh.buffer)
89+
expect(pi.multiaddrs[0] instanceof Multiaddr).to.equal(true)
90+
})
91+
92+
it('replace multiaddr', () => {
8393
const pi = new PeerInfo()
8494
expect(pi).to.exist
8595
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
@@ -102,11 +112,9 @@ describe('peer-info', function () {
102112
pi.multiaddr.replace(old, fresh)
103113

104114
expect(pi.multiaddrs.length).to.equal(4)
105-
106-
done()
107115
})
108116

109-
it('replace multiaddr (no arrays)', (done) => {
117+
it('replace multiaddr (no arrays)', () => {
110118
const pi = new PeerInfo()
111119
expect(pi).to.exist
112120
const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001')
@@ -128,7 +136,5 @@ describe('peer-info', function () {
128136
pi.multiaddr.replace(old, fresh)
129137

130138
expect(pi.multiaddrs.length).to.equal(4)
131-
132-
done()
133139
})
134140
})

0 commit comments

Comments
 (0)