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

Commit

Permalink
feat: support PeerInfo.create(IdJSON, cb) as well (#61)
Browse files Browse the repository at this point in the history
* feat: support PeerInfo.create(IdJSON, cb) as well

* test: bump timeout and refactor tests
  • Loading branch information
daviddias authored Dec 14, 2017
1 parent 8547cb0 commit 5b8b160
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
- [`.multiaddrs.add(addr)`](#multiaddraddaddr)
- [`.multiaddrs.addSafe(addr)`](#multiaddraddsafeaddr)
- [`.multiaddrs.forEach(fn)`](#multiaddrforeachfn)
- [`.multaiddrs.size`]((#multiaddrsize)
- [`.multiaddrs.has()`]((#multiaddrhas)
- [`.multiaddrs.size`](#multiaddrsize)
- [`.multiaddrs.has()`](#multiaddrhas)
- [`.multiaddrs.delete(addr)`](#multiaddrdeleteaddr)
- [`.multiaddrs.toArray()`](#multiaddrtoarray)
- [`.multiaddrs.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh)
Expand Down Expand Up @@ -84,7 +84,7 @@ const PeerInfo = require('peer-info')

### `PeerInfo.create([id, ] callback)`

- `id: PeerID`, optional
- `id` optional - can be a PeerId or a JSON object(will be parsed with https://github.com/libp2p/js-peer-id#createfromjsonobj)
- `callback: Function` with signature `function (err, peerInfo) {}`

Creates a new PeerInfo instance and if no `id` is passed it
Expand All @@ -93,9 +93,9 @@ for it.

### `new PeerInfo(id)`

- `id: PeerID`
- `id: PeerId` - instance of PeerId (optional)

Creates a new PeerInfo instance from an existing PeerID.
Creates a new PeerInfo instance from an existing PeerId.

### `multiaddrs`

Expand Down
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Id = require('peer-id')
const PeerId = require('peer-id')
const ensureMultiaddr = require('./utils').ensureMultiaddr
const MultiaddrSet = require('./multiaddr-set')
const assert = require('assert')
Expand Down Expand Up @@ -34,12 +34,12 @@ class PeerInfo {
}
}

PeerInfo.create = (id, callback) => {
if (typeof id === 'function') {
callback = id
id = null
PeerInfo.create = (peerId, callback) => {
if (typeof peerId === 'function') {
callback = peerId
peerId = null

Id.create((err, id) => {
PeerId.create((err, id) => {
if (err) {
return callback(err)
}
Expand All @@ -49,7 +49,12 @@ PeerInfo.create = (id, callback) => {
return
}

callback(null, new PeerInfo(id))
// Already a PeerId instance
if (typeof peerId.toJSON === 'function') {
callback(null, new PeerInfo(peerId))
} else {
PeerId.createFromJSON(peerId, (err, id) => callback(err, new PeerInfo(id)))
}
}

PeerInfo.isPeerInfo = (peerInfo) => {
Expand Down
45 changes: 28 additions & 17 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const expect = chai.expect
const PeerId = require('peer-id')
const Id = require('peer-id')
const Multiaddr = require('multiaddr')
const PeerInfo = require('../src')
const Info = require('../src')
const peerIdJSON = require('./peer-test.json')

describe('peer-info', () => {
let pi

beforeEach((done) => {
PeerId.create({bits: 512}, (err, id) => {
Id.create({bits: 512}, (err, id) => {
if (err) {
return done(err)
}
pi = new PeerInfo(id)
pi = new Info(id)
done()
})
})

it('create with Id', (done) => {
PeerId.create({bits: 512}, (err, id) => {
it('create with Id class', (done) => {
Id.create({bits: 512}, (err, id) => {
expect(err).to.not.exist()
const pi = new PeerInfo(id)
const pi2 = new PeerInfo(id)
const pi = new Info(id)
const pi2 = new Info(id)
expect(pi.id).to.exist()
expect(pi.id).to.eql(id)
expect(pi2).to.exist()
Expand All @@ -37,27 +38,37 @@ describe('peer-info', () => {
})

it('throws when not passing an Id', () => {
expect(() => new PeerInfo()).to.throw()
expect(() => new Info()).to.throw()
})

it('isPeerInfo', () => {
expect(PeerInfo.isPeerInfo(pi)).to.equal(true)
expect(PeerInfo.isPeerInfo(pi.id)).to.equal(false)
expect(PeerInfo.isPeerInfo('bananas')).to.equal(false)
expect(Info.isPeerInfo(pi)).to.equal(true)
expect(Info.isPeerInfo(pi.id)).to.equal(false)
expect(Info.isPeerInfo('bananas')).to.equal(false)
})

it('PeerInfo.create', (done) => {
PeerInfo.create({bits: 512}, (err, pi) => {
it('.create', function (done) {
this.timeout(20 * 1000)
Info.create((err, pi) => {
expect(err).to.not.exist()
expect(pi.id).to.exist()
done()
})
})

it('PeerInfo.create with existing id', (done) => {
PeerId.create({bits: 512}, (err, id) => {
it('create with Id as JSON', (done) => {
Info.create(peerIdJSON, (err, pi) => {
expect(err).to.not.exist()
PeerInfo.create(id, (err, pi) => {
expect(pi.id).to.exist()
expect(pi.id.toJSON()).to.eql(peerIdJSON)
done()
})
})

it('.create with existing id', (done) => {
Id.create({bits: 512}, (err, id) => {
expect(err).to.not.exist()
Info.create(id, (err, pi) => {
expect(err).to.not.exist()
expect(pi.id).to.exist()
expect(pi.id.isEqual(id)).to.equal(true)
Expand Down
5 changes: 5 additions & 0 deletions test/peer-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmVmZNvjFdRoPqg27UqK56KVZabrN4VRinHF2Jp1FNmHMo",
"privKey": "CAASpgkwggSiAgEAAoIBAQCNSaB/V5Q0FNJ0KrCJNeeZtn/dAua3dXtbDePYl4SA2nQkrn/TNxbkDzmo2CeKB0/40D1tiinv+0PWpln1F3fHAWJcGHhOdsEzueH0AhUhFzz8sInFNAa7kcRDCMmWeR6Sd17Gu0Soettd9U9ldEGcpG0ZrLRyUSjb9K+L/9JbRu157IJw0lVBDm42AtmLIGivbPMXGgYCcKly0ZsNuLEgOdlvN8FNU1NGcjifcbCv9IwcX2TnfbGsv0JGbR7wwvFy18TmVX0Woo2kbFPXa23W0o1anWhpkTaThp6f3rjqUgUXxkCj+3bYSknURfUq0IK//sC5Q7TSYz9PDHxPlro/AgMBAAECggEAL1lMU0C7ZxTPTqT70o/hFM9nXF7biQHzHB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