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

Commit f4ed523

Browse files
committed
feat: async await
License: MIT Signed-off-by: Henrique Dias <[email protected]>
1 parent e83cfa8 commit f4ed523

File tree

4 files changed

+40
-71
lines changed

4 files changed

+40
-71
lines changed

README.md

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

89-
### `PeerInfo.create([id, ] callback)`
89+
### `PeerInfo.create([id])`
9090

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

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

97+
Returns `Promise<PeerInfo>`.
98+
9899
### `new PeerInfo(id)`
99100

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

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
"test"
3838
],
3939
"devDependencies": {
40-
"aegir": "^13.1.0",
41-
"buffer-loader": "0.0.1",
42-
"chai": "^4.1.2",
40+
"aegir": "^17.0.1",
41+
"buffer-loader": "0.1.0",
42+
"chai": "^4.2.0",
4343
"dirty-chai": "^2.0.1"
4444
},
4545
"dependencies": {
4646
"multiaddr": "^5.0.0",
47-
"mafmt": "^6.0.0",
47+
"mafmt": "^6.0.2",
4848
"lodash.uniqby": "^4.7.0",
49-
"peer-id": "~0.10.7"
49+
"peer-id": "libp2p/js-peer-id#6318f2a"
5050
},
5151
"contributors": [
5252
"Arnaud <[email protected]>",

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()
@@ -34,27 +34,14 @@ class PeerInfo {
3434
}
3535
}
3636

37-
PeerInfo.create = (peerId, callback) => {
38-
if (typeof peerId === 'function') {
39-
callback = peerId
40-
peerId = null
41-
42-
PeerId.create((err, id) => {
43-
if (err) {
44-
return callback(err)
45-
}
46-
47-
callback(null, new PeerInfo(id))
48-
})
49-
return
37+
PeerInfo.create = async (peerId) => {
38+
if (typeof peerId === 'undefined') {
39+
peerId = await PeerId.create()
40+
} else if (peerId && typeof peerId.toJSON !== 'function') {
41+
peerId = await PeerId.createFromJSON(peerId)
5042
}
5143

52-
// Already a PeerId instance
53-
if (typeof peerId.toJSON === 'function') {
54-
callback(null, new PeerInfo(peerId))
55-
} else {
56-
PeerId.createFromJSON(peerId, (err, id) => callback(err, new PeerInfo(id)))
57-
}
44+
return new PeerInfo(peerId)
5845
}
5946

6047
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)