diff --git a/.travis.yml b/.travis.yml index a362414..649404b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ + language: node_js node_js: - "4.0" @@ -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 \ No newline at end of file diff --git a/src/index.js b/src/index.js index 9c527eb..f070abd 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } diff --git a/tests/peer-info-test.js b/tests/peer-info-test.js new file mode 100644 index 0000000..b142493 --- /dev/null +++ b/tests/peer-info-test.js @@ -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() + }) +}) diff --git a/tests/peer-test.js b/tests/peer-test.js deleted file mode 100644 index 61666a7..0000000 --- a/tests/peer-test.js +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-env mocha */ - -const expect = require('chai').expect -const PeerId = require('peer-id') -const Multiaddr = require('multiaddr') -const PeerInfo = require('../src') - -it('create peer-info', function (done) { - this.timeout(6000) - const id = PeerId.create() - const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') - const pi = new PeerInfo(id, mh) - expect(pi).to.exist - done() -})