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

new features: add and remove multiaddrs, avoid adding repeating, addSafe mode to avoid multiaddr explosion #6

Merged
merged 2 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

language: node_js
node_js:
- "4.0"
Expand All @@ -9,5 +10,13 @@ branches:
before_install:
- npm i -g npm
# Workaround for a permissions issue with Travis virtual machine images

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

script:
- npm test
- npm test
60 changes: 52 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,63 @@
* Peer represents a peer on the IPFS network
*/

const Id = require('peer-id')

exports = module.exports = Peer

function Peer (id, multiaddrs) {
var self = this
function Peer (peerId) {
if (!(this instanceof Peer)) {
return new Peer(peerId)
}

if (!peerId) {
this.id = Id.create()
} else {
this.id = peerId
}

this.multiaddrs = []
const observedMultiaddrs = []

this.multiaddr = {}

this.multiaddr.add = (multiaddr) => {
var exists = false
this.multiaddrs.some((m, i) => {
if (m.toString() === multiaddr.toString()) {
exists = true
return true
}
})
if (!exists) {
this.multiaddrs.push(multiaddr)
}
}

if (!(self instanceof Peer)) {
throw new Error('Peer must be called with new')
// to prevent multiaddr explosion
this.multiaddr.addSafe = (multiaddr) => {
var check = false
observedMultiaddrs.some((m, i) => {
if (m.toString() === multiaddr.toString()) {
this.multiaddr.add(multiaddr)
observedMultiaddrs.splice(i, 1)
check = true
}
})
if (!check) {
observedMultiaddrs.push(multiaddr)
}
}

if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
this.multiaddr.rm = (multiaddr) => {
this.multiaddrs.some((m, i) => {
if (m.toString() === multiaddr.toString()) {
this.multiaddrs.splice(i, 1)
return true
}
})
}

self.id = id
self.multiaddrs = multiaddrs
// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}
68 changes: 68 additions & 0 deletions tests/peer-info-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-env mocha */

const expect = require('chai').expect
const Id = require('peer-id')
const Multiaddr = require('multiaddr')
const PeerInfo = require('../src')

describe('peer-info', function (done) {
this.timeout(10000)

it('create with Id', (done) => {
const id = Id.create()
const pi = new PeerInfo(id)
expect(pi).to.exist
expect(pi.id).to.exist
expect(pi.id).to.deep.equal(id)
done()
})

it('create without passing an Id', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
expect(pi.id).to.exist
done()
})

it('add multiaddr', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
pi.multiaddr.add(mh)
expect(pi.multiaddrs.length).to.equal(1)
done()
})

it('add repeated multiaddr', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
pi.multiaddr.add(mh)
expect(pi.multiaddrs.length).to.equal(1)
pi.multiaddr.add(mh)
expect(pi.multiaddrs.length).to.equal(1)
done()
})

it('rm multiaddr', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
pi.multiaddr.add(mh)
expect(pi.multiaddrs.length).to.equal(1)
pi.multiaddr.rm(mh)
expect(pi.multiaddrs.length).to.equal(0)
done()
})

it('addSafe - avoid multiaddr explosion', (done) => {
const pi = new PeerInfo()
expect(pi).to.exist
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
pi.multiaddr.addSafe(mh)
expect(pi.multiaddrs.length).to.equal(0)
pi.multiaddr.addSafe(mh)
expect(pi.multiaddrs.length).to.equal(1)
done()
})
})
15 changes: 0 additions & 15 deletions tests/peer-test.js

This file was deleted.