Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #64 from diasdavid/add/webrtc-star
Browse files Browse the repository at this point in the history
restructure and add spdy to browser tests
  • Loading branch information
daviddias committed May 23, 2016
2 parents 09acdab + d771a12 commit c53068c
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 111 deletions.
16 changes: 13 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const Id = require('peer-id')
const WebSockets = require('libp2p-websockets')

const Swarm = require('./src')
// const spdy = require('libp2p-spdy')
const multiaddr = require('multiaddr')

const sigServer = require('libp2p-webrtc-star/src/signalling-server')

let swarmA
let swarmB
let sigS

gulp.task('test:browser:before', (done) => {
function createListenerA (cb) {
Expand All @@ -31,16 +35,21 @@ gulp.task('test:browser:before', (done) => {
swarmB = new Swarm(peerB)

swarmB.transport.add('ws', new WebSockets())
swarmB.transport.listen('ws', {}, null, cb)
swarmB.transport.listen('ws', {}, null, () => {
// swarmB.connection.addStreamMuxer(spdy)
// swarmB.connection.reuse()
cb()
})

swarmB.handle('/echo/1.0.0', echo)
}

let count = 0
const ready = () => ++count === 2 ? done() : null
const ready = () => ++count === 3 ? done() : null

createListenerA(ready)
createListenerB(ready)
sigS = sigServer.start(15555, ready)

function echo (conn) {
conn.pipe(conn)
Expand All @@ -49,10 +58,11 @@ gulp.task('test:browser:before', (done) => {

gulp.task('test:browser:after', (done) => {
let count = 0
const ready = () => ++count === 2 ? done() : null
const ready = () => ++count === 3 ? done() : null

swarmA.transport.close('ws', ready)
swarmB.transport.close('ws', ready)
sigS.stop(ready)
})

require('aegir/gulp')(gulp)
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@
"node": "^4.3.0"
},
"devDependencies": {
"aegir": "^3.0.4",
"bl": "^1.1.2",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"aegir": "^3.0.4",
"gulp": "^3.9.1",
"istanbul": "^0.4.3",
"libp2p-multiplex": "^0.2.1",
"libp2p-spdy": "^0.5.0",
"libp2p-tcp": "^0.5.1",
"libp2p-websockets": "^0.5.0",
"libp2p-spdy": "^0.6.1",
"libp2p-tcp": "^0.6.0",
"libp2p-webrtc-star": "^0.1.3",
"libp2p-websockets": "^0.6.0",
"pre-commit": "^1.1.2",
"stream-pair": "^1.0.3"
"stream-pair": "^1.0.3",
"webrtcsupport": "^2.2.0"
},
"dependencies": {
"babel-runtime": "^6.6.1",
"browserify-zlib": "github:ipfs/browserify-zlib",
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
Expand Down Expand Up @@ -79,4 +82,4 @@
"Richard Littauer <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
2 changes: 1 addition & 1 deletion src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = function dial (swarm) {
const tKeys = swarm.availableTransports(pi)

if (tKeys.length === 0) {
return cb(new Error('No available tranport to dial to'))
return cb(new Error('No available transport to dial to'))
}

nextTransport(tKeys.shift())
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ function Swarm (peerInfo) {
return Object.keys(this.transports).filter((ts) => {
// ipfs multiaddrs are not dialable so we drop them here
let dialable = addrs.map((addr) => {
// webrtc-star needs the /ipfs/QmHash
if (addr.toString().indexOf('webrtc-star') > 0) {
return addr
}

if (contains(addr.protoNames(), 'ipfs')) {
return addr.decapsulate('ipfs')
}
Expand Down
5 changes: 5 additions & 0 deletions src/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ module.exports = function (swarm) {
// for the given transport `tp`.
function dialables (tp, multiaddrs) {
return tp.filter(multiaddrs.map((addr) => {
// webrtc-star needs the /ipfs/QmHash
if (addr.toString().indexOf('webrtc-star') > 0) {
return addr
}

// ipfs multiaddrs are not dialable so we drop them here
if (contains(addr.protoNames(), 'ipfs')) {
return addr.decapsulate('ipfs')
Expand Down
48 changes: 48 additions & 0 deletions test/browser-00-transport-websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
const bl = require('bl')

const Swarm = require('../src')

describe('transport - websockets', function () {
this.timeout(10000)

var swarm

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

it('add', (done) => {
swarm.transport.add('ws', new WebSockets(), () => {
expect(Object.keys(swarm.transports).length).to.equal(1)
done()
})
})

it('dial', (done) => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9100/ws')

const conn = swarm.transport.dial('ws', ma, (err, conn) => {
expect(err).to.not.exist
})

conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()
})
})
90 changes: 90 additions & 0 deletions test/browser-01-transport-webrtc-star.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const peerId = require('peer-id')
const PeerInfo = require('peer-info')
const WebRTCStar = require('libp2p-webrtc-star')
const bl = require('bl')
const parallel = require('run-parallel')

const Swarm = require('../src')

describe('transport - webrtc-star', function () {
this.timeout(10000)

let swarm1
let peer1

let swarm2
let peer2

before(() => {
const id1 = peerId.createFromB58String('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooA')
peer1 = new PeerInfo(id1)
const mh1 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooA')
peer1.multiaddr.add(mh1)

const id2 = peerId.createFromB58String('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooB')
peer2 = new PeerInfo(id2)
const mh2 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooB')
peer2.multiaddr.add(mh2)

swarm1 = new Swarm(peer1)
swarm2 = new Swarm(peer2)
})

it('add WebRTCStar transport to swarm 1', (done) => {
swarm1.transport.add('wstar', new WebRTCStar(), () => {
expect(Object.keys(swarm1.transports).length).to.equal(1)
done()
})
})

it('add WebRTCStar transport to swarm 2', (done) => {
swarm2.transport.add('wstar', new WebRTCStar(), () => {
expect(Object.keys(swarm2.transports).length).to.equal(1)
done()
})
})

it('listen on swarm 1', (done) => {
swarm1.transport.listen('wstar', {}, (conn) => {
conn.pipe(conn)
}, done)
})

it('listen on swarm 2', (done) => {
swarm2.transport.listen('wstar', {}, (conn) => {
conn.pipe(conn)
}, done)
})

it('dial', (done) => {
swarm1.transport.dial('wstar', peer2.multiaddrs[0], (err, conn) => {
expect(err).to.not.exist

const text = 'Hello World'
conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal(text)
done()
}))

conn.write(text)
conn.end()
})
})

it('close', (done) => {
parallel([
(cb) => {
swarm1.transport.close('wstar', cb)
},
(cb) => {
swarm2.transport.close('wstar', cb)
}
], done)
})
})
73 changes: 73 additions & 0 deletions test/browser-02-swarm-with-muxing-plus-websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
// const spdy = require('libp2p-spdy')

const Swarm = require('../src')

describe('high level API (swarm with spdy + websockets)', function () {
this.timeout(10000)

var swarm
var peerDst

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

it('add spdy', () => {
// swarm.connection.addStreamMuxer(spdy)
// swarm.connection.reuse()
})

it('add ws', () => {
swarm.transport.add('ws', new WebSockets())
expect(Object.keys(swarm.transports).length).to.equal(1)
})

it('create Dst peer info', () => {
const b58IdDst = 'QmRy1iU6BHmG5Hd8rnPhPL98cy1W1przUSTAMcGDq9yAAV'
// use a pre generated Id to save time
const idDst = Id.createFromB58String(b58IdDst)
peerDst = new Peer(idDst)

const ma = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
peerDst.multiaddr.add(ma)
})

it('dial to warm a conn', (done) => {
swarm.dial(peerDst, (err) => {
expect(err).to.not.exist
done()
})
})

it('dial on protocol, use warmed conn', (done) => {
swarm.dial(peerDst, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist
conn.end()
conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})

it('close', (done) => {
// cause CI is slow
setTimeout(() => {
swarm.close(done)
}, 1000)
})

// TODO - test that the listener (node.js peer) can dial back
// do that by dialing on a protocol to activate that behaviour
// like libp2p-spdy tests
})
Loading

0 comments on commit c53068c

Please sign in to comment.