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

Commit

Permalink
wip: start async await
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
  • Loading branch information
hacdias committed Oct 30, 2018
1 parent e83cfa8 commit 5b37e25
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
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 <[email protected]>",
Expand Down
15 changes: 14 additions & 1 deletion 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,6 +34,18 @@ class PeerInfo {
}
}

PeerInfo.create = async (peerId) => {
if (typeof peerId === 'undefined') {
peerId = await PeerId.create()
return new PeerInfo(peerId)
} else if (peerId && typeof peerId.toJSON === 'function') {
return new PeerInfo(peerId)
}

return PeerId.createFromJSON(peerId)
}

/*
PeerInfo.create = (peerId, callback) => {
if (typeof peerId === 'function') {
callback = peerId
Expand All @@ -56,6 +68,7 @@ PeerInfo.create = (peerId, callback) => {
PeerId.createFromJSON(peerId, (err, id) => callback(err, new PeerInfo(id)))
}
}
*/

PeerInfo.isPeerInfo = (peerInfo) => {
return Boolean(typeof peerInfo === 'object' &&
Expand Down
60 changes: 23 additions & 37 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,25 +40,19 @@ 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) => {
/*it('.create with existing id', (done) => {
Id.create({bits: 512}, (err, id) => {
expect(err).to.not.exist()
Info.create(id, (err, pi) => {
Expand All @@ -76,7 +62,7 @@ describe('peer-info', () => {
done()
})
})
})
})*/

it('add multiaddr', () => {
const ma = Multiaddr('/ip4/127.0.0.1/tcp/5001')
Expand Down

0 comments on commit 5b37e25

Please sign in to comment.