Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
feat: update to latest apis
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 3, 2017
1 parent d1d9de0 commit 9a4dea4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 84 deletions.
36 changes: 13 additions & 23 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const gulp = require('gulp')
const multiaddr = require('multiaddr')
const Node = require('libp2p-ipfs-nodejs')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
Expand All @@ -17,44 +16,35 @@ gulp.task('libnode:start', (done) => {
let count = 0
const ready = () => ++count === 2 ? done() : null

sigServer.start({
port: 15555
}, (err, _server) => {
sigServer.start({ port: 15555 }, (err, _server) => {
if (err) {
throw err
}
server = _server
ready()
})

PeerId.createFromJSON(rawPeer, gotId)

function gotId (err, pid) {
PeerId.createFromJSON(rawPeer, (err, peerId) => {
if (err) {
return done(err)
}
const peer = new PeerInfo(pid)
const peer = new PeerInfo(peerId)

const ma = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
peer.multiaddr.add(ma)
peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')

node = new Node(peer)
node.start(() => {
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
ready()
})
}
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
node.start(() => ready())
})
})

gulp.task('libnode:stop', (done) => {
setTimeout(() => {
node.stop((err) => {
if (err) {
throw err
}
server.stop(done)
})
}, 2000)
setTimeout(() => node.stop((err) => {
if (err) {
return done(err)
}
server.stop(done)
}), 2000)
})

gulp.task('test:browser:before', ['libnode:start'])
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"scripts": {
"lint": "aegir-lint",
"test": "gulp test --dom",
"test": "gulp test:browser --dom",
"test:node": "gulp test:node",
"test:browser": "gulp test:browser --dom",
"build": "gulp build",
Expand All @@ -31,12 +31,12 @@
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-browser#readme",
"devDependencies": {
"aegir": "^11.0.1",
"async": "^2.2.0",
"async": "^2.3.0",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"gulp": "^3.9.1",
"libp2p-ipfs-nodejs": "~0.22.0",
"peer-id": "~0.8.6",
"libp2p-ipfs-nodejs": "~0.23.0",
"peer-id": "~0.8.7",
"pre-commit": "^1.2.2",
"pull-goodbye": "0.0.1",
"pull-serializer": "^0.3.2",
Expand All @@ -55,7 +55,7 @@
"mafmt": "^2.1.8",
"multiaddr": "^2.3.0",
"peer-book": "~0.4.0",
"peer-id": "~0.8.6",
"peer-id": "~0.8.7",
"peer-info": "~0.9.2"
},
"contributors": [
Expand All @@ -66,4 +66,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"kumavis <aaron@kumavis.me>"
]
}
}
2 changes: 0 additions & 2 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict'

require('./websockets-only')
38 changes: 14 additions & 24 deletions test/webrtc-star-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const parallel = require('async/parallel')
Expand All @@ -20,18 +19,18 @@ describe('libp2p-ipfs-browser (webrtc only)', () => {

it('create two peerInfo with webrtc-star addrs', (done) => {
parallel([
(cb) => PeerId.create(cb),
(cb) => PeerId.create(cb)
(cb) => PeerId.create({ bits: 1024 }, cb),
(cb) => PeerId.create({ bits: 1024 }, cb)
], (err, ids) => {
expect(err).to.not.exist()

peer1 = new PeerInfo(ids[0])
const mh1 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + ids[0].toB58String())
peer1.multiaddr.add(mh1)
const ma1 = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + ids[0].toB58String()
peer1.multiaddrs.add(ma1)

peer2 = new PeerInfo(ids[1])
const mh2 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + ids[1].toB58String())
peer2.multiaddr.add(mh2)
const ma2 = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + ids[1].toB58String()
peer2.multiaddrs.add(ma2)

done()
})
Expand All @@ -45,20 +44,13 @@ describe('libp2p-ipfs-browser (webrtc only)', () => {

it('listen on the two libp2p nodes', (done) => {
parallel([
(cb) => {
node1.start(cb)
},
(cb) => {
node2.start(cb)
}
(cb) => node1.start(cb),
(cb) => node2.start(cb)
], done)
})

it('handle a protocol on the first node', (done) => {
node2.handle('/echo/1.0.0', (protocol, conn) => {
pull(conn, conn)
})
done()
it('handle a protocol on the first node', () => {
node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
})

it('dial from the second node to the first node', (done) => {
Expand All @@ -69,10 +61,9 @@ describe('libp2p-ipfs-browser (webrtc only)', () => {
function check () {
const text = 'hello'
const peers1 = node1.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers1)).to.have.length(1)

const peers2 = node2.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers2)).to.have.length(1)

pull(
Expand All @@ -95,8 +86,7 @@ describe('libp2p-ipfs-browser (webrtc only)', () => {

function check () {
const peers = node1.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(0)
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
done()
}
Expand All @@ -118,8 +108,8 @@ describe('libp2p-ipfs-browser (webrtc only)', () => {
expect(err).to.not.exist()

const peer3 = new PeerInfo(id3)
const mh3 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + id3.toB58String())
peer3.multiaddr.add(mh3)
const ma3 = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/' + id3.toB58String()
peer3.multiaddrs.add(ma3)

node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check))
node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check))
Expand Down
52 changes: 23 additions & 29 deletions test/websockets-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const multiaddr = require('multiaddr')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const pull = require('pull-stream')
Expand All @@ -20,39 +19,37 @@ describe('libp2p-ipfs-browser (websockets only)', () => {
let nodeA

before((done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id)
const ma = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id

PeerId.createFromPrivKey(rawPeer.privKey, (err, id) => {
if (err) {
return done(err)
}

peerB = new PeerInfo(id)
peerB.multiaddr.add(mh)
peerB.multiaddrs.add(ma)
done()
})
})

after((done) => {
nodeA.stop(done)
})
after((done) => nodeA.stop(done))

it('create libp2pNode', (done) => {
PeerInfo.create((err, info) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
info.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
nodeA = new Node(info)
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')

nodeA = new Node(peerInfo)
done()
})
})

it('create libp2pNode with multiplex only', (done) => {
PeerInfo.create((err, info) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
const b = new Node(info, null, {muxer: ['multiplex']})
expect(b.modules.connection.muxer).to.eql([
require('libp2p-multiplex')
])

const b = new Node(peerInfo, null, { muxer: ['multiplex'] })
expect(b.modules.connection.muxer).to.eql([require('libp2p-multiplex')])
done()
})
})
Expand All @@ -64,48 +61,47 @@ describe('libp2p-ipfs-browser (websockets only)', () => {
// General connectivity tests

it('libp2p.dial using Multiaddr nodeA to nodeB', (done) => {
nodeA.dial(peerB.multiaddrs[0], (err) => {
nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => {
expect(err).to.not.exist()
// Some time for Identify to finish
setTimeout(check, 500)

setTimeout(check, 500) // Some time for Identify to finish

function check () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(1)
done()
}
})
})

it('libp2p.dial using Multiaddr on Protocol nodeA to nodeB', (done) => {
nodeA.dial(peerB.multiaddrs[0], '/echo/1.0.0', (err, conn) => {
nodeA.dial(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist()

const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(1)

pull(
pull.values([Buffer('hey')]),
conn,
pull.collect((err, data) => {
expect(err).to.not.exist()
expect(data).to.be.eql([Buffer('hey')])
expect(data).to.eql([Buffer('hey')])
done()
})
)
})
})

it('libp2p.hangUp using Multiaddr nodeA to nodeB', (done) => {
nodeA.hangUp(peerB.multiaddrs[0], (err) => {
nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => {
expect(err).to.not.exist()

setTimeout(check, 500)

function check () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(0)
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
done()
}
Expand All @@ -115,16 +111,14 @@ describe('libp2p-ipfs-browser (websockets only)', () => {
it('libp2p.dial using PeerInfo nodeA to nodeB', (done) => {
nodeA.dial(peerB, (err) => {
expect(err).to.not.exist()
// Some time for Identify to finish
setTimeout(check, 500)

setTimeout(check, 500) // Some time for Identify to finish

function check () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(1)
done()
}
// TODO confirm that we got the pubkey through identify
})
})

Expand Down Expand Up @@ -155,7 +149,7 @@ describe('libp2p-ipfs-browser (websockets only)', () => {
function check () {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(0)
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
done()
}
Expand Down

0 comments on commit 9a4dea4

Please sign in to comment.