diff --git a/gulpfile.js b/gulpfile.js index d87b017..a210210 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -4,6 +4,7 @@ const gulp = require('gulp') const PeerInfo = require('peer-info') const PeerId = require('peer-id') const WebSockets = require('libp2p-websockets') +const pull = require('pull-stream') const Swarm = require('./src') const spdy = require('libp2p-spdy') @@ -60,7 +61,7 @@ gulp.task('test:browser:before', (done) => { sigS = sigServer.start(15555, ready) function echo (conn) { - conn.pipe(conn) + pull(conn, conn) } }) diff --git a/package.json b/package.json index 5541107..2f0072c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "libp2p-swarm", "version": "0.22.3", "description": "libp2p swarm implementation in JavaScript", - "main": "lib/index.js", + "main": "src/index.js", "jsnext:main": "src/index.js", "scripts": { "lint": "gulp lint", @@ -37,35 +37,31 @@ "node": "^4.3.0" }, "devDependencies": { - "aegir": "^4.0.0", + "aegir": "^7.0.1", "buffer-loader": "0.0.1", "chai": "^3.5.0", "gulp": "^3.9.1", - "istanbul": "^0.4.3", "libp2p-multiplex": "^0.2.1", "libp2p-spdy": "^0.8.1", "libp2p-tcp": "^0.7.4", "libp2p-webrtc-star": "^0.3.2", "libp2p-websockets": "^0.7.1", "pre-commit": "^1.1.3", + "pull-goodbye": "0.0.1", "pull-stream": "^3.4.3", - "stream-pair": "^1.0.3", "webrtcsupport": "^2.2.0" }, "dependencies": { "babel-runtime": "^6.9.0", - "bl": "^1.1.2", "browserify-zlib": "github:ipfs/browserify-zlib", "debug": "^2.2.0", - "duplexify": "^3.4.3", "interface-connection": "^0.1.7", "ip-address": "^5.8.0", - "length-prefixed-stream": "^1.5.0", "libp2p-identify": "^0.1.3", "libp2p-secio": "^0.3.0", "lodash.contains": "^2.4.3", "multiaddr": "^2.0.2", - "multistream-select": "^0.9.0", + "multistream-select": "^0.10.0", "peer-id": "^0.7.0", "peer-info": "^0.7.0", "protocol-buffers": "^3.1.6", diff --git a/test/01-transport-tcp.node.js b/test/01-transport-tcp.node.js index b434665..cc6327e 100644 --- a/test/01-transport-tcp.node.js +++ b/test/01-transport-tcp.node.js @@ -12,8 +12,6 @@ const pull = require('pull-stream') const Swarm = require('../src') describe('transport - tcp', function () { - this.timeout(10000) - let swarmA let swarmB let peerA = new Peer() diff --git a/test/03-transport-websockets.node.js b/test/03-transport-websockets.node.js index c507a06..d071800 100644 --- a/test/03-transport-websockets.node.js +++ b/test/03-transport-websockets.node.js @@ -6,13 +6,13 @@ const expect = require('chai').expect const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') -const Swarm = require('../src') const WebSockets = require('libp2p-websockets') -const bl = require('bl') +const pull = require('pull-stream') +const goodbye = require('pull-goodbye') -describe.skip('transport - websockets', function () { - this.timeout(10000) +const Swarm = require('../src') +describe('transport - websockets', function () { var swarmA var swarmB var peerA = new Peer() @@ -37,10 +37,10 @@ describe.skip('transport - websockets', function () { it('listen', (done) => { parallel([ (cb) => swarmA.transport.listen('ws', {}, (conn) => { - conn.pipe(conn) + pull(conn, conn) }, cb), (cb) => swarmB.transport.listen('ws', {}, (conn) => { - conn.pipe(conn) + pull(conn, conn) }, cb) ], () => { expect(peerA.multiaddrs.length).to.equal(1) @@ -63,24 +63,31 @@ describe.skip('transport - websockets', function () { const conn = swarmA.transport.dial('ws', multiaddr('/ip4/127.0.0.1/tcp/9999/ws'), (err, conn) => { expect(err).to.not.exist }) - conn.pipe(bl((err, data) => { - expect(err).to.not.exist - done() - })) - conn.write('hey') - conn.end() + + const s = goodbye({ + source: pull.values([Buffer('hey')]), + sink: pull.collect((err, data) => { + expect(err).to.not.exist + expect(data).to.be.eql([Buffer('hey')]) + done() + }) + }) + pull(s, conn, s) }) it('dial (conn from callback)', (done) => { swarmA.transport.dial('ws', multiaddr('/ip4/127.0.0.1/tcp/9999/ws'), (err, conn) => { expect(err).to.not.exist - conn.pipe(bl((err, data) => { - expect(err).to.not.exist - done() - })) - conn.write('hey') - conn.end() + const s = goodbye({ + source: pull.values([Buffer('hey')]), + sink: pull.collect((err, data) => { + expect(err).to.not.exist + expect(data).to.be.eql([Buffer('hey')]) + done() + }) + }) + pull(s, conn, s) }) }) diff --git a/test/04-muxing-multiplex.node.js b/test/04-muxing-multiplex.node.js index 57ec7c1..74206d9 100644 --- a/test/04-muxing-multiplex.node.js +++ b/test/04-muxing-multiplex.node.js @@ -6,20 +6,22 @@ const expect = require('chai').expect const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') -const Swarm = require('../src') const TCP = require('libp2p-tcp') const multiplex = require('libp2p-spdy') +const pull = require('pull-stream') -// TODO multiplex needs to be upgraded, like spdy, to work again -describe.skip('stream muxing with multiplex (on TCP)', function () { - this.timeout(60 * 1000) +const Swarm = require('../src') - var swarmA - var peerA - var swarmB - var peerB - var swarmC - var peerC +// Multiplex is not yet ported +// Reenable when https://github.com/libp2p/js-libp2p-multiplex/issues/14 +// is done +describe.skip('stream muxing with multiplex (on TCP)', () => { + let swarmA + let peerA + let swarmB + let peerB + let swarmC + let peerC before((done) => { peerA = new Peer() @@ -66,16 +68,17 @@ describe.skip('stream muxing with multiplex (on TCP)', function () { it('handle + dial on protocol', (done) => { swarmB.handle('/abacaxi/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) swarmA.dial(peerB, '/abacaxi/1.0.0', (err, conn) => { expect(err).to.not.exist expect(Object.keys(swarmA.muxedConns).length).to.equal(1) - conn.end() - - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', done) + pull( + pull.empty(), + conn, + pull.onEnd(done) + ) }) }) @@ -90,17 +93,18 @@ describe.skip('stream muxing with multiplex (on TCP)', function () { it('dial on protocol, reuse warmed conn', (done) => { swarmA.handle('/papaia/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) swarmB.dial(peerA, '/papaia/1.0.0', (err, conn) => { expect(err).to.not.exist expect(Object.keys(swarmB.conns).length).to.equal(0) expect(Object.keys(swarmB.muxedConns).length).to.equal(1) - conn.end() - - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', done) + pull( + pull.empty(), + conn, + pull.onEnd(done) + ) }) }) diff --git a/test/05-muxing-spdy.node.js b/test/05-muxing-spdy.node.js index 473ab0e..435482e 100644 --- a/test/05-muxing-spdy.node.js +++ b/test/05-muxing-spdy.node.js @@ -13,17 +13,15 @@ const pull = require('pull-stream') const spdy = require('libp2p-spdy') -describe('stream muxing with spdy (on TCP)', function () { - this.timeout(60 * 1000) - - var swarmA - var peerA - var swarmB - var peerB - var swarmC - var peerC - var swarmD - var peerD +describe('stream muxing with spdy (on TCP)', () => { + let swarmA + let peerA + let swarmB + let peerB + let swarmC + let peerC + let swarmD + let peerD before((done) => { peerA = new Peer() @@ -174,10 +172,10 @@ describe('stream muxing with spdy (on TCP)', function () { // TODO: create a similar version, but that spawns a swarm in a // different proc it.skip('blow up a socket, with WebSockets', (done) => { - var swarmE - var peerE - var swarmF - var peerF + let swarmE + let peerE + let swarmF + let peerF peerE = new Peer() peerF = new Peer() diff --git a/test/06-conn-upgrade-secio.node.js b/test/06-conn-upgrade-secio.node.js index 06e363f..09d8056 100644 --- a/test/06-conn-upgrade-secio.node.js +++ b/test/06-conn-upgrade-secio.node.js @@ -12,15 +12,13 @@ const pull = require('pull-stream') const Swarm = require('../src') -describe('secio conn upgrade (on TCP)', function () { - this.timeout(60 * 1000) - - var swarmA - var peerA - var swarmB - var peerB - var swarmC - var peerC +describe('secio conn upgrade (on TCP)', () => { + let swarmA + let peerA + let swarmB + let peerB + let swarmC + let peerC before((done) => { peerA = new Peer() diff --git a/test/08-swarm-without-muxing.node.js b/test/08-swarm-without-muxing.node.js index 6cbaf7e..bbbc0b9 100644 --- a/test/08-swarm-without-muxing.node.js +++ b/test/08-swarm-without-muxing.node.js @@ -10,13 +10,11 @@ const pull = require('pull-stream') const Swarm = require('../src') -describe('high level API - 1st without stream multiplexing (on TCP)', function () { - this.timeout(20000) - - var swarmA - var peerA - var swarmB - var peerB +describe('high level API - 1st without stream multiplexing (on TCP)', () => { + let swarmA + let peerA + let swarmB + let peerB before((done) => { peerA = new Peer() diff --git a/test/09-swarm-with-muxing.node.js b/test/09-swarm-with-muxing.node.js index 37ed4be..7ae6f7a 100644 --- a/test/09-swarm-with-muxing.node.js +++ b/test/09-swarm-with-muxing.node.js @@ -6,24 +6,24 @@ const expect = require('chai').expect const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') -const Swarm = require('../src') const TCP = require('libp2p-tcp') const WebSockets = require('libp2p-websockets') const spdy = require('libp2p-spdy') +const pull = require('pull-stream') -describe.skip('high level API - with everything mixed all together!', function () { - this.timeout(100000) +const Swarm = require('../src') - var swarmA // tcp - var peerA - var swarmB // tcp+ws - var peerB - var swarmC // tcp+ws - var peerC - var swarmD // ws - var peerD - var swarmE // ws - var peerE +describe('high level API - with everything mixed all together!', () => { + let swarmA // tcp + let peerA + let swarmB // tcp+ws + let peerB + let swarmC // tcp+ws + let peerC + let swarmD // ws + let peerD + let swarmE // ws + let peerE before((done) => { peerA = new Peer() @@ -140,16 +140,17 @@ describe.skip('high level API - with everything mixed all together!', function ( it('dial from tcp to tcp+ws, on protocol', (done) => { swarmB.handle('/anona/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) swarmA.dial(peerB, '/anona/1.0.0', (err, conn) => { expect(err).to.not.exist expect(Object.keys(swarmA.muxedConns).length).to.equal(1) - conn.end() - - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', done) + pull( + pull.empty(), + conn, + pull.onEnd(done) + ) }) }) @@ -163,37 +164,42 @@ describe.skip('high level API - with everything mixed all together!', function ( it('dial from ws to ws', (done) => { swarmE.handle('/abacaxi/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) swarmD.dial(peerE, '/abacaxi/1.0.0', (err, conn) => { expect(err).to.not.exist expect(Object.keys(swarmD.muxedConns).length).to.equal(1) - conn.end() - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', () => { - setTimeout(() => { - expect(Object.keys(swarmE.muxedConns).length).to.equal(1) - done() - }, 1000) - }) + pull( + pull.empty(), + conn, + pull.onEnd((err) => { + expect(err).to.not.exist + setTimeout(() => { + expect(Object.keys(swarmE.muxedConns).length).to.equal(1) + done() + }, 1000) + }) + ) }) }) it('dial from tcp to tcp+ws (returned conn)', (done) => { swarmB.handle('/grapes/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) const conn = swarmA.dial(peerB, '/grapes/1.0.0', (err, conn) => { expect(err).to.not.exist expect(Object.keys(swarmA.muxedConns).length).to.equal(1) }) - conn.end() - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', done) + pull( + pull.empty(), + conn, + pull.onEnd(done) + ) }) it('dial from tcp+ws to tcp+ws', (done) => { @@ -202,7 +208,7 @@ describe.skip('high level API - with everything mixed all together!', function ( expect(err).to.not.exist expect(peerInfo).to.exist }) - conn.pipe(conn) + pull(conn, conn) }) swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => { @@ -212,10 +218,12 @@ describe.skip('high level API - with everything mixed all together!', function ( expect(peerInfo).to.exist }) expect(Object.keys(swarmA.muxedConns).length).to.equal(2) - conn.end() - conn.on('data', () => {}) // let it flow.. let it flooooow - conn.on('end', done) + pull( + pull.empty(), + conn, + pull.onEnd(done) + ) }) }) diff --git a/test/browser-00-transport-websockets.js b/test/browser-00-transport-websockets.js index eba6b32..08dfc66 100644 --- a/test/browser-00-transport-websockets.js +++ b/test/browser-00-transport-websockets.js @@ -6,14 +6,12 @@ const multiaddr = require('multiaddr') const Id = require('peer-id') const Peer = require('peer-info') const WebSockets = require('libp2p-websockets') -const bl = require('bl') +const pull = require('pull-stream') const Swarm = require('../src') -describe('transport - websockets', function () { - this.timeout(10000) - - var swarm +describe('transport - websockets', () => { + let swarm before(() => { const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb' @@ -37,12 +35,14 @@ describe('transport - websockets', function () { 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() + pull( + pull.values([Buffer('hey')]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist + expect(data.toString()).to.equal('hey') + done() + }) + ) }) }) diff --git a/test/browser-01-transport-webrtc-star.js b/test/browser-01-transport-webrtc-star.js index 6fe6ee3..c1084bd 100644 --- a/test/browser-01-transport-webrtc-star.js +++ b/test/browser-01-transport-webrtc-star.js @@ -6,14 +6,12 @@ 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 pull = require('pull-stream') const Swarm = require('../src') -describe('transport - webrtc-star', function () { - this.timeout(10000) - +describe('transport - webrtc-star', () => { let swarm1 let peer1 @@ -51,13 +49,13 @@ describe('transport - webrtc-star', function () { it('listen on swarm 1', (done) => { swarm1.transport.listen('wstar', {}, (conn) => { - conn.pipe(conn) + pull(conn, conn) }, done) }) it('listen on swarm 2', (done) => { swarm2.transport.listen('wstar', {}, (conn) => { - conn.pipe(conn) + pull(conn, conn) }, done) }) @@ -66,25 +64,22 @@ describe('transport - webrtc-star', function () { 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() + pull( + pull.values([text]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist + expect(data.toString()).to.equal(text) + done() + }) + ) }) }) it('close', (done) => { parallel([ - (cb) => { - swarm1.transport.close('wstar', cb) - }, - (cb) => { - swarm2.transport.close('wstar', cb) - } + (cb) => swarm1.transport.close('wstar', cb), + (cb) => swarm2.transport.close('wstar', cb) ], done) }) }) diff --git a/test/browser-02-swarm-with-muxing-plus-websockets.js b/test/browser-02-swarm-with-muxing-plus-websockets.js index f9be8bc..420fddc 100644 --- a/test/browser-02-swarm-with-muxing-plus-websockets.js +++ b/test/browser-02-swarm-with-muxing-plus-websockets.js @@ -9,6 +9,7 @@ const WebSockets = require('libp2p-websockets') const spdy = require('libp2p-spdy') const fs = require('fs') const path = require('path') +const pull = require('pull-stream') const Swarm = require('../src') @@ -55,9 +56,11 @@ describe('high level API (swarm with spdy + websockets)', function () { 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) + pull( + pull.values([Buffer('hello')]), + conn, + pull.onEnd(done) + ) }) }) diff --git a/test/browser-03-swarm-with-muxing-plus-webrtc-star.js b/test/browser-03-swarm-with-muxing-plus-webrtc-star.js index c743bb6..3694c24 100644 --- a/test/browser-03-swarm-with-muxing-plus-webrtc-star.js +++ b/test/browser-03-swarm-with-muxing-plus-webrtc-star.js @@ -7,14 +7,12 @@ const peerId = require('peer-id') const PeerInfo = require('peer-info') const WebRTCStar = require('libp2p-webrtc-star') const spdy = require('libp2p-spdy') -const bl = require('bl') const parallel = require('run-parallel') +const pull = require('pull-stream') const Swarm = require('../src') -describe('high level API (swarm with spdy + webrtc-star)', function () { - this.timeout(60 * 1000) - +describe('high level API (swarm with spdy + webrtc-star)', () => { let swarm1 let peer1 let wstar1 @@ -67,7 +65,7 @@ describe('high level API (swarm with spdy + webrtc-star)', function () { it('handle proto', () => { swarm2.handle('/echo/1.0.0', (conn) => { - conn.pipe(conn) + pull(conn, conn) }) }) @@ -77,15 +75,16 @@ describe('high level API (swarm with spdy + webrtc-star)', function () { expect(Object.keys(swarm1.muxedConns).length).to.equal(1) const text = 'Hello World' - conn.pipe(bl((err, data) => { - expect(err).to.not.exist - expect(data.toString()).to.equal(text) - expect(Object.keys(swarm2.muxedConns).length).to.equal(1) - done() - })) - - conn.write(text) - conn.end() + pull( + pull.values([text]), + conn, + pull.collect((err, data) => { + expect(err).to.not.exist + expect(data.toString()).to.equal(text) + expect(Object.keys(swarm2.muxedConns).length).to.equal(1) + done() + }) + ) }) })