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

Commit b6725b0

Browse files
hacdiasachingbrain
authored andcommitted
feat: async await
BREAKING CHANGE: API refactored to use async/await chore: update node version fix: better verifications on create() chore: deps and linting License: MIT Signed-off-by: Henrique Dias <[email protected]>
1 parent 6745a6b commit b6725b0

File tree

4 files changed

+38
-69
lines changed

4 files changed

+38
-69
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- [Browser: `<script>` Tag](#browser-script-tag)
2424
- [Usage](#usage)
2525
- [API](#api)
26-
- [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback)
26+
- [`PeerInfo.create([id])`](#peerinfocreateid)
2727
- [`new PeerInfo(id)`](#new-peerinfoid)
2828
- [`.connect(ma)`](#connectma)
2929
- [`.disconnect()`](#connectma)
@@ -87,15 +87,16 @@ peer.multiaddrs.add('/sonic/bfsk/697/1209')
8787
const PeerInfo = require('peer-info')
8888
```
8989

90-
### `PeerInfo.create([id, ] callback)`
90+
### `PeerInfo.create([id])`
9191

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

9594
Creates a new PeerInfo instance and if no `id` is passed it
9695
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
9796
for it.
9897

98+
Returns `Promise<PeerInfo>`.
99+
99100
### `new PeerInfo(id)`
100101

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
"bugs": "https://github.com/libp2p/js-peer-info/issues",
3434
"homepage": "https://github.com/libp2p/js-peer-info",
3535
"devDependencies": {
36-
"aegir": "^18.2.2",
36+
"aegir": "^19.0.5",
3737
"buffer-loader": "0.1.0",
38-
"bundlesize": "~0.17.0",
38+
"bundlesize": "~0.18.0",
3939
"chai": "^4.2.0",
4040
"dirty-chai": "^2.0.1"
4141
},
4242
"dependencies": {
4343
"mafmt": "^6.0.7",
4444
"multiaddr": "^6.0.6",
45-
"peer-id": "~0.12.2",
45+
"peer-id": "~0.13.1",
4646
"unique-by": "^1.0.0"
4747
},
4848
"contributors": [

src/index.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const assert = require('assert')
88
// Peer represents a peer on the IPFS network
99
class PeerInfo {
1010
constructor (peerId) {
11-
assert(peerId, 'Missing peerId. Use Peer.create(cb) to create one')
11+
assert(peerId, 'Missing peerId. Use Peer.create() to create one')
1212

1313
this.id = peerId
1414
this.multiaddrs = new MultiaddrSet()
@@ -41,27 +41,14 @@ class PeerInfo {
4141
}
4242
}
4343

44-
PeerInfo.create = (peerId, callback) => {
45-
if (typeof peerId === 'function') {
46-
callback = peerId
47-
peerId = null
48-
49-
PeerId.create((err, id) => {
50-
if (err) {
51-
return callback(err)
52-
}
53-
54-
callback(null, new PeerInfo(id))
55-
})
56-
return
44+
PeerInfo.create = async (peerId) => {
45+
if (peerId == null) {
46+
peerId = await PeerId.create()
47+
} else if (!PeerId.isPeerId(peerId)) {
48+
peerId = await PeerId.createFromJSON(peerId)
5749
}
5850

59-
// Already a PeerId instance
60-
if (typeof peerId.toJSON === 'function') {
61-
callback(null, new PeerInfo(peerId))
62-
} else {
63-
PeerId.createFromJSON(peerId, (err, id) => callback(err, new PeerInfo(id)))
64-
}
51+
return new PeerInfo(peerId)
6552
}
6653

6754
PeerInfo.isPeerInfo = (peerInfo) => {

test/index.spec.js

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,20 @@ const peerIdJSON = require('./peer-test.json')
1414
describe('peer-info', () => {
1515
let pi
1616

17-
beforeEach((done) => {
18-
Id.create({ bits: 512 }, (err, id) => {
19-
if (err) {
20-
return done(err)
21-
}
22-
pi = new Info(id)
23-
done()
24-
})
17+
beforeEach(async () => {
18+
const id = await Id.create({ bits: 512 })
19+
pi = new Info(id)
2520
})
2621

27-
it('create with Id class', (done) => {
28-
Id.create({ bits: 512 }, (err, id) => {
29-
expect(err).to.not.exist()
30-
const pi = new Info(id)
31-
const pi2 = new Info(id)
32-
expect(pi.id).to.exist()
33-
expect(pi.id).to.eql(id)
34-
expect(pi2).to.exist()
35-
expect(pi2.id).to.exist()
36-
expect(pi2.id).to.eql(id)
37-
done()
38-
})
22+
it('create with Id class', async () => {
23+
const id = await Id.create({ bits: 512 })
24+
const pi = new Info(id)
25+
const pi2 = new Info(id)
26+
expect(pi.id).to.exist()
27+
expect(pi.id).to.eql(id)
28+
expect(pi2).to.exist()
29+
expect(pi2.id).to.exist()
30+
expect(pi2.id).to.eql(id)
3931
})
4032

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

51-
it('.create', function (done) {
43+
it('.create', async function () {
5244
this.timeout(20 * 1000)
53-
Info.create((err, pi) => {
54-
expect(err).to.not.exist()
55-
expect(pi.id).to.exist()
56-
done()
57-
})
45+
const info = await Info.create()
46+
expect(info.id).to.exist()
5847
})
5948

60-
it('create with Id as JSON', (done) => {
61-
Info.create(peerIdJSON, (err, pi) => {
62-
expect(err).to.not.exist()
63-
expect(pi.id).to.exist()
64-
expect(pi.id.toJSON()).to.eql(peerIdJSON)
65-
done()
66-
})
49+
it('create with Id as JSON', async () => {
50+
const info = await Info.create(peerIdJSON)
51+
expect(info.id).to.exist()
52+
expect(info.id.toJSON()).to.eql(peerIdJSON)
6753
})
6854

69-
it('.create with existing id', (done) => {
70-
Id.create({ bits: 512 }, (err, id) => {
71-
expect(err).to.not.exist()
72-
Info.create(id, (err, pi) => {
73-
expect(err).to.not.exist()
74-
expect(pi.id).to.exist()
75-
expect(pi.id.isEqual(id)).to.equal(true)
76-
done()
77-
})
78-
})
55+
it('.create with existing id', async () => {
56+
const id = await Id.create({ bits: 512 })
57+
const info = await Info.create(id)
58+
expect(info.id).to.exist()
59+
expect(info.id.isEqual(id)).to.equal(true)
7960
})
8061

8162
it('add multiaddr', () => {

0 commit comments

Comments
 (0)