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

Commit

Permalink
feat: async await
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Oct 30, 2018
1 parent e83cfa8 commit f4ed523
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 71 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api)
- [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback)
- [`PeerInfo.create([id])`](#peerinfocreateid)
- [`new PeerInfo(id)`](#new-peerinfoid)
- [`.connect(ma)`](#connectma)
- [`.disconnect()`](#connectma)
Expand Down Expand Up @@ -86,15 +86,16 @@ peer.multiaddrs.add('/sonic/bfsk/697/1209')
const PeerInfo = require('peer-info')
```

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

- `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
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
for it.

Returns `Promise<PeerInfo>`.

### `new PeerInfo(id)`

- `id: PeerId` - instance of PeerId (optional)
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
"test"
],
"devDependencies": {
"aegir": "^13.1.0",
"buffer-loader": "0.0.1",
"chai": "^4.1.2",
"aegir": "^17.0.1",
"buffer-loader": "0.1.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"multiaddr": "^5.0.0",
"mafmt": "^6.0.0",
"mafmt": "^6.0.2",
"lodash.uniqby": "^4.7.0",
"peer-id": "~0.10.7"
"peer-id": "libp2p/js-peer-id#6318f2a"
},
"contributors": [
"Arnaud <arnaud.valensi@gmail.com>",
Expand Down
27 changes: 7 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const assert = require('assert')
// Peer represents a peer on the IPFS network
class PeerInfo {
constructor (peerId) {
assert(peerId, 'Missing peerId. Use Peer.create(cb) to create one')
assert(peerId, 'Missing peerId. Use Peer.create() to create one')

this.id = peerId
this.multiaddrs = new MultiaddrSet()
Expand All @@ -34,27 +34,14 @@ class PeerInfo {
}
}

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

PeerId.create((err, id) => {
if (err) {
return callback(err)
}

callback(null, new PeerInfo(id))
})
return
PeerInfo.create = async (peerId) => {
if (typeof peerId === 'undefined') {
peerId = await PeerId.create()
} else if (peerId && typeof peerId.toJSON !== 'function') {
peerId = await PeerId.createFromJSON(peerId)
}

// 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)))
}
return new PeerInfo(peerId)
}

PeerInfo.isPeerInfo = (peerInfo) => {
Expand Down
67 changes: 24 additions & 43 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,20 @@ const peerIdJSON = require('./peer-test.json')
describe('peer-info', () => {
let pi

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

it('create with Id class', (done) => {
Id.create({bits: 512}, (err, id) => {
expect(err).to.not.exist()
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()
expect(pi2.id).to.exist()
expect(pi2.id).to.eql(id)
done()
})
it('create with Id class', async () => {
const id = await Id.create({bits: 512})
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()
expect(pi2.id).to.exist()
expect(pi2.id).to.eql(id)
})

it('throws when not passing an Id', () => {
Expand All @@ -48,34 +40,23 @@ describe('peer-info', () => {
expect(Info.isPeerInfo('bananas')).to.equal(false)
})

it('.create', function (done) {
it('.create', async function () {
this.timeout(20 * 1000)
Info.create((err, pi) => {
expect(err).to.not.exist()
expect(pi.id).to.exist()
done()
})
const info = await Info.create()
expect(info.id).to.exist()
})

it('create with Id as JSON', (done) => {
Info.create(peerIdJSON, (err, pi) => {
expect(err).to.not.exist()
expect(pi.id).to.exist()
expect(pi.id.toJSON()).to.eql(peerIdJSON)
done()
})
it('create with Id as JSON', async () => {
const info = await Info.create(peerIdJSON)
expect(info.id).to.exist()
expect(info.id.toJSON()).to.eql(peerIdJSON)
})

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)
done()
})
})
it('.create with existing id', async () => {
const id = await Id.create({bits: 512})
const info = await Info.create(id)
expect(info.id).to.exist()
expect(info.id.isEqual(id)).to.equal(true)
})

it('add multiaddr', () => {
Expand Down

0 comments on commit f4ed523

Please sign in to comment.