diff --git a/.aegir.js b/.aegir.js deleted file mode 100644 index e3151ab..0000000 --- a/.aegir.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -const path = require('path') - -module.exports = { - webpack: { - resolve: { - alias: { - 'node-forge': path.resolve( - path.dirname(require.resolve('libp2p-crypto')), - '../vendor/forge.bundle.js' - ) - } - } - } -} diff --git a/.gitignore b/.gitignore index 5b1fd19..907c78a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,3 @@ build/Release node_modules dist -lib \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index e1d6320..7d22efa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,16 @@ sudo: false language: node_js -node_js: - - 4 - - 5 + +matrix: + include: + - node_js: 4 + env: CXX=g++-4.8 + - node_js: 6 + env: + - SAUCE=true + - CXX=g++-4.8 + - node_js: stable + env: CXX=g++-4.8 # Make sure we have new NPM. before_install: @@ -22,3 +30,11 @@ before_script: after_success: - npm run coverage-publish + +addons: + firefox: latest + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 diff --git a/README.md b/README.md index 9b0ba3b..544d8f1 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ -peer-info JavaScript implementation -=================================== +# js-peer-info [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) -[![Build Status](https://travis-ci.org/diasdavid/js-peer-info.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-info) -[![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-peer-info?branch=master) -[![Dependency Status](https://david-dm.org/diasdavid/js-peer-info.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-info) +[![Build Status](https://travis-ci.org/libp2p/js-peer-info.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-info) +[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-info?branch=master) +[![Dependency Status](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) +[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) +![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) +![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square) + +[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-peer-info.svg)](https://saucelabs.com/u/ipfs-js-peer-info) > A PeerInfo object contains information about a > [PeerID](https://github.com/libp2p/js-peer-id) and its @@ -14,7 +18,49 @@ peer-info JavaScript implementation > [IPFS](https://github.com/ipfs/ipfs) and > [libp2p](https://github.com/libp2p/js-libp2p). -# Example +## Table of Contents + +- [Installation](#installation) + - [npm](#npm) + - [Node.JS, Browserify, Webpack](#nodejs-browserify-webpack) + - [Browser: ` + + +``` + +## Usage ```js const PeerInfo = require('peer-info') @@ -32,31 +78,41 @@ peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001')) peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209')) ``` -# API +## API ```js const PeerInfo = require('peer-info') ``` -## const peer = new PeerInfo() +### `PeerInfo.create([id, ] callback)` -Creates a new PeerInfo instance and also generates a new underlying -[PeerID](https://github.com/libp2p/js-peer-id) for it. +- `id: PeerID`, optional +- `callback: Function` -## const peer = new PeerInfo(peerId) +Creates a new PeerInfo instance and if no `id` is passed it +generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id) +for it. + +### `new PeerInfo(id)` + +- `id: PeerID` Creates a new PeerInfo instance from an existing PeerID. -## peer.multiaddrs +### `multiaddrs` A list of multiaddresses instances that `peer` can be reached at. -## peer.multiaddr.add(addr) +### `multiaddr.add(addr)` + +- `addr: Multiaddr` Adds a new multiaddress that `peer` can be reached at. `addr` is an instance of a [multiaddr](https://github.com/libp2p/js-multiaddr). -## peer.multiaddr.addSafe(addr) +### `multiaddr.addSafe(addr)` + +- `addr: Multiaddr` The `addSafe` call, in comparison to `add`, will only add the multiaddr to `multiaddrs` if the same multiaddr tries to be added twice. @@ -67,40 +123,26 @@ peers which will not provide a useful multiaddr to be shared to the rest of the network (e.g. a multiaddr referring to a peer inside a LAN being shared to the outside world). -## peer.multiaddr.rm(addr) +### `multiaddr.rm(addr)` + +- `addr: Multiaddr` Removes a multiaddress instance `addr` from `peer`. -## peer.multiaddr.replace(existing, fresh) +### `multiaddr.replace(existing, fresh)` + +- `existing: Multiaddr` +- `fresh: Multiaddr` Removes the array of multiaddresses `existing` from `peer`, and adds the array of multiaddresses `fresh`. +## Contribute -# Installation - -## npm - -```sh -> npm i peer-info -``` - -## Node.JS, Browserify, Webpack - -```JavaScript -var PeerInfo = require('peer-info') -``` - -## Browser: ` - - -``` +Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. -# License +## License -MIT +[MIT © David Dias](LICENSE) diff --git a/package.json b/package.json index bf86189..139ce39 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,13 @@ "name": "peer-info", "version": "0.7.1", "description": "IPFS Peer abstraction JavaScript implementation", - "main": "lib/index.js", - "jsnext:main": "src/index.js", + "main": "src/index.js", "scripts": { "lint": "aegir-lint", "build": "aegir-build", "test": "aegir-test", - "test:node": "aegir-test node", - "test:browser": "aegir-test browser", + "test:node": "aegir-test --env node", + "test:browser": "aegir-test --env browser", "release": "aegir-release", "release-minor": "aegir-release --type minor", "release-major": "aegir-release --type major", @@ -18,34 +17,33 @@ }, "repository": { "type": "git", - "url": "https://github.com/diasdavid/js-peer-info.git" + "url": "https://github.com/libp2p/js-peer-info.git" }, "keywords": [ "IPFS" ], "engines": { - "node": "^4.3.0" + "node": ">=4.0.0" }, "author": "David Dias ", "license": "MIT", "bugs": { - "url": "https://github.com/diasdavid/js-peer-info/issues" + "url": "https://github.com/libp2p/js-peer-info/issues" }, - "homepage": "https://github.com/diasdavid/js-peer-info", + "homepage": "https://github.com/libp2p/js-peer-info", "pre-commit": [ "lint", "test" ], "devDependencies": { - "aegir": "^8.0.0", + "aegir": "^9.0.1", "buffer-loader": "0.0.1", "chai": "^3.5.0", "pre-commit": "^1.1.3" }, "dependencies": { - "babel-runtime": "^6.11.6", "multiaddr": "^2.0.3", - "peer-id": "^0.7.0" + "peer-id": "^0.8.0" }, "contributors": [ "David Dias ", @@ -55,4 +53,4 @@ "dignifiedquire ", "greenkeeperio-bot " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 3a5c10d..5888834 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ const Id = require('peer-id') const multiaddr = require('multiaddr') -exports = module.exports = Peer +exports = module.exports = PeerInfo function ensureMultiaddr (addr) { if (multiaddr.isMultiaddr(addr)) { @@ -14,17 +14,17 @@ function ensureMultiaddr (addr) { } // Peer represents a peer on the IPFS network -function Peer (peerId) { - if (!(this instanceof Peer)) { - return new Peer(peerId) +function PeerInfo (peerId) { + if (!(this instanceof PeerInfo)) { + return new PeerInfo(peerId) } if (!peerId) { - this.id = Id.create() - } else { - this.id = peerId + throw new Error('Missing peerId. Use Peer.create(cb) to create one') } + this.id = peerId + this.multiaddrs = [] const observedMultiaddrs = [] @@ -91,3 +91,21 @@ function Peer (peerId) { // TODO: add features to fetch multiaddr using filters // look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js } + +PeerInfo.create = (id, callback) => { + if (typeof id === 'function') { + callback = id + id = null + + Id.create((err, id) => { + if (err) { + return callback(err) + } + + callback(null, new PeerInfo(id)) + }) + return + } + + callback(null, new PeerInfo(id)) +} diff --git a/test/peer-info.spec.js b/test/index.spec.js similarity index 73% rename from test/peer-info.spec.js rename to test/index.spec.js index 26aecf0..85e7f60 100644 --- a/test/peer-info.spec.js +++ b/test/index.spec.js @@ -6,48 +6,72 @@ const PeerId = require('peer-id') const Multiaddr = require('multiaddr') const PeerInfo = require('../src') -describe('peer-info', function () { - this.timeout(20000) - - it('create with Id', () => { - const id = PeerId.create() - const pi = new PeerInfo(id) - expect(pi).to.exist - expect(pi.id).to.exist - expect(pi.id).to.deep.equal(id) +describe('peer-info', () => { + let pi + + beforeEach((done) => { + PeerInfo.create((err, _pi) => { + if (err) { + return done(err) + } + pi = _pi + done() + }) }) - it('create without passing an Id', () => { - const pi = new PeerInfo() - expect(pi).to.exist - expect(pi.id).to.exist + it('create with Id', (done) => { + PeerId.create((err, id) => { + expect(err).to.not.exist + const pi = new PeerInfo(id) + const pi2 = PeerInfo(id) + expect(pi.id).to.exist + expect(pi.id).to.deep.equal(id) + expect(pi2).to.exist + expect(pi2.id).to.exist + expect(pi2.id).to.deep.equal(id) + done() + }) }) - it('create without "new"', () => { - const pi = PeerInfo() - expect(pi).to.exist - expect(pi.id).to.exist + it('throws when not passing an Id', () => { + expect( + () => new PeerInfo() + ).to.throw() + }) + + it('PeerInfo.create', (done) => { + PeerInfo.create((err, pi) => { + expect(err).to.not.exist + expect(pi.id).to.exist + done() + }) + }) + + it('PeerInfo.create with existing id', (done) => { + PeerId.create((err, id) => { + expect(err).to.not.exist + PeerInfo.create(id, (err, pi) => { + expect(err).to.not.exist + expect(pi.id).to.exist + expect(pi.id).to.deep.equal(id) + done() + }) + }) }) it('add multiaddr', () => { - 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) }) it('add multiaddr that are buffers', () => { - const pi = new PeerInfo() - expect(pi).to.exist const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') pi.multiaddr.add(mh.buffer) expect(pi.multiaddrs[0] instanceof Multiaddr).to.equal(true) }) it('add repeated multiaddr', () => { - 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) @@ -56,8 +80,6 @@ describe('peer-info', function () { }) it('rm multiaddr', () => { - 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) @@ -66,8 +88,6 @@ describe('peer-info', function () { }) it('addSafe - avoid multiaddr explosion', () => { - const pi = new PeerInfo() - expect(pi).to.exist const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/9002') const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/9009') @@ -81,8 +101,6 @@ describe('peer-info', function () { }) it('addSafe - multiaddr that are buffers', () => { - const pi = new PeerInfo() - expect(pi).to.exist const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001') pi.multiaddr.addSafe(mh.buffer) pi.multiaddr.addSafe(mh.buffer) @@ -90,8 +108,6 @@ describe('peer-info', function () { }) it('replace multiaddr', () => { - const pi = new PeerInfo() - expect(pi).to.exist const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003') @@ -115,8 +131,6 @@ describe('peer-info', function () { }) it('replace multiaddr (no arrays)', () => { - const pi = new PeerInfo() - expect(pi).to.exist const mh1 = Multiaddr('/ip4/127.0.0.1/tcp/5001') const mh2 = Multiaddr('/ip4/127.0.0.1/tcp/5002') const mh3 = Multiaddr('/ip4/127.0.0.1/tcp/5003')