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 (#67)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: API refactored to use async/await

chore: update node version
fix: better verifications on create()
chore: deps and linting
  • Loading branch information
hacdias authored and jacobheun committed Jul 12, 2019
1 parent 6745a6b commit 63edac1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 69 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 @@ -87,15 +87,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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
"bugs": "https://github.com/libp2p/js-peer-info/issues",
"homepage": "https://github.com/libp2p/js-peer-info",
"devDependencies": {
"aegir": "^18.2.2",
"aegir": "^19.0.5",
"buffer-loader": "0.1.0",
"bundlesize": "~0.17.0",
"bundlesize": "~0.18.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
},
"dependencies": {
"mafmt": "^6.0.7",
"multiaddr": "^6.0.6",
"peer-id": "~0.12.2",
"peer-id": "~0.13.1",
"unique-by": "^1.0.0"
},
"contributors": [
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 Down Expand Up @@ -41,27 +41,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 (peerId == null) {
peerId = await PeerId.create()
} else if (!PeerId.isPeerId(peerId)) {
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 63edac1

Please sign in to comment.