diff --git a/package.json b/package.json index 79f291f..adbfaf8 100644 --- a/package.json +++ b/package.json @@ -45,22 +45,22 @@ "istanbul": "^0.4.3", "libp2p-multiplex": "^0.2.1", "libp2p-spdy": "^0.3.1", - "libp2p-tcp": "^0.5.0", - "libp2p-websockets": "^0.4.1", - "multiaddr": "^1.4.0", - "peer-id": "^0.6.6", - "peer-info": "^0.6.2", + "libp2p-tcp": "^0.5.1", + "libp2p-websockets": "^0.4.3", "pre-commit": "^1.1.2", "stream-pair": "^1.0.3" }, "dependencies": { - "async": "^2.0.0-rc.4", "babel-runtime": "^6.6.1", "duplex-passthrough": "github:diasdavid/duplex-passthrough", "ip-address": "^5.8.0", "lodash.contains": "^2.4.3", + "multiaddr": "^1.4.0", "multistream-select": "^0.6.5", - "protocol-buffers-stream": "^1.3.1" + "peer-id": "^0.6.6", + "peer-info": "^0.6.2", + "protocol-buffers-stream": "^1.3.1", + "run-parallel": "^1.1.6" }, "aegir": { "webpack": { @@ -79,4 +79,4 @@ "Pau Ramon Revilla ", "Richard Littauer " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 676575d..0f0bb3d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,12 @@ 'use strict' -const async = require('async') const multistream = require('multistream-select') const identify = require('./identify') const DuplexPassThrough = require('duplex-passthrough') const contains = require('lodash.contains') const util = require('util') const EE = require('events').EventEmitter +const parallel = require('run-parallel') exports = module.exports = Swarm @@ -118,7 +118,13 @@ function Swarm (peerInfo) { } this.transport.close = (key, callback) => { - this.transports[key].close(callback) + const transport = this.transports[key] + + if (!transport) { + return callback(new Error(`Trying to close non existing transport: ${key}`)) + } + + transport.close(callback) } // connections -- @@ -372,14 +378,9 @@ function Swarm (peerInfo) { this.muxedConns[key].muxer.end() }) - async.each( - Object.keys(this.transports), - (key, cb) => this.transports[key].close(cb), - () => { - // Ignoring close errors - callback() - } - ) + parallel(Object.keys(this.transports).map((key) => { + return (cb) => this.transports[key].close(cb) + }), callback) } } diff --git a/test/01-transport-tcp.node.js b/test/01-transport-tcp.node.js index 3358210..2b18105 100644 --- a/test/01-transport-tcp.node.js +++ b/test/01-transport-tcp.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -92,15 +93,10 @@ describe('transport - tcp', function () { }) it('close', (done) => { - var count = 0 - swarmA.transport.close('tcp', closed) - swarmB.transport.close('tcp', closed) - - function closed () { - if (++count === 2) { - done() - } - } + parallel([ + (cb) => swarmA.transport.close('tcp', cb), + (cb) => swarmB.transport.close('tcp', cb) + ], done) }) it('support port 0', (done) => { diff --git a/test/03-transport-websockets.node.js b/test/03-transport-websockets.node.js index 926f4c7..a1a06e1 100644 --- a/test/03-transport-websockets.node.js +++ b/test/03-transport-websockets.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -88,14 +89,9 @@ describe('transport - websockets', function () { }) it('close', (done) => { - var count = 0 - swarmA.transport.close('ws', closed) - swarmB.transport.close('ws', closed) - - function closed () { - if (++count === 2) { - done() - } - } + parallel([ + (cb) => swarmA.transport.close('ws', cb), + (cb) => swarmB.transport.close('ws', cb) + ], done) }) }) diff --git a/test/04-muxing-multiplex.node.js b/test/04-muxing-multiplex.node.js index d8a8507..0f0e18d 100644 --- a/test/04-muxing-multiplex.node.js +++ b/test/04-muxing-multiplex.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp') const multiplex = require('libp2p-spdy') describe('stream muxing with multiplex (on TCP)', function () { - this.timeout(20000) + this.timeout(60 * 1000) var swarmA var peerA @@ -37,35 +38,22 @@ describe('stream muxing with multiplex (on TCP)', function () { swarmC = new Swarm(peerC) swarmA.transport.add('tcp', new TCP()) - swarmA.transport.listen('tcp', {}, null, ready) - swarmB.transport.add('tcp', new TCP()) - swarmB.transport.listen('tcp', {}, null, ready) - swarmC.transport.add('tcp', new TCP()) - swarmC.transport.listen('tcp', {}, null, ready) - var counter = 0 - - function ready () { - if (++counter === 3) { - done() - } - } + parallel([ + (cb) => swarmA.transport.listen('tcp', {}, null, cb), + (cb) => swarmB.transport.listen('tcp', {}, null, cb), + (cb) => swarmC.transport.listen('tcp', {}, null, cb) + ], done) }) after((done) => { - var counter = 0 - - swarmA.close(closed) - swarmB.close(closed) - swarmC.close(closed) - - function closed () { - if (++counter === 3) { - done() - } - } + parallel([ + (cb) => swarmA.close(cb), + (cb) => swarmB.close(cb), + (cb) => swarmC.close(cb) + ], done) }) it('add', (done) => { diff --git a/test/05-muxing-spdy.node.js b/test/05-muxing-spdy.node.js index a824dc2..9ed39a4 100644 --- a/test/05-muxing-spdy.node.js +++ b/test/05-muxing-spdy.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -10,7 +11,7 @@ const TCP = require('libp2p-tcp') const spdy = require('libp2p-spdy') describe('stream muxing with spdy (on TCP)', function () { - this.timeout(20000) + this.timeout(60 * 1000) var swarmA var peerA @@ -37,35 +38,22 @@ describe('stream muxing with spdy (on TCP)', function () { swarmC = new Swarm(peerC) swarmA.transport.add('tcp', new TCP()) - swarmA.transport.listen('tcp', {}, null, ready) - swarmB.transport.add('tcp', new TCP()) - swarmB.transport.listen('tcp', {}, null, ready) - swarmC.transport.add('tcp', new TCP()) - swarmC.transport.listen('tcp', {}, null, ready) - var counter = 0 - - function ready () { - if (++counter === 3) { - done() - } - } + parallel([ + (cb) => swarmA.transport.listen('tcp', {}, null, cb), + (cb) => swarmB.transport.listen('tcp', {}, null, cb), + (cb) => swarmC.transport.listen('tcp', {}, null, cb) + ], done) }) after((done) => { - var counter = 0 - - swarmA.close(closed) - swarmB.close(closed) - // swarmC.close(closed) - - function closed () { - if (++counter === 2) { - done() - } - } + parallel([ + (cb) => swarmA.close(cb), + (cb) => swarmB.close(cb), + (cb) => swarmC.close(cb) + ], done) }) it('add', (done) => { @@ -130,7 +118,8 @@ describe('stream muxing with spdy (on TCP)', function () { }) it('close one end, make sure the other does not blow', (done) => { - swarmC.close(() => { + swarmC.close((err) => { + if (err) throw err // to make sure it has time to propagate setTimeout(done, 1000) }) diff --git a/test/08-swarm-without-muxing.node.js b/test/08-swarm-without-muxing.node.js index 39f4675..7463eb0 100644 --- a/test/08-swarm-without-muxing.node.js +++ b/test/08-swarm-without-muxing.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -42,16 +43,10 @@ describe('high level API - 1st without stream multiplexing (on TCP)', function ( }) after((done) => { - var counter = 0 - - swarmA.close(closed) - swarmB.close(closed) - - function closed () { - if (++counter === 2) { - done() - } - } + parallel([ + (cb) => swarmA.close(cb), + (cb) => swarmB.close(cb) + ], done) }) it('handle a protocol', (done) => { diff --git a/test/09-swarm-with-muxing.node.js b/test/09-swarm-with-muxing.node.js index 3c5a00f..f904ea8 100644 --- a/test/09-swarm-with-muxing.node.js +++ b/test/09-swarm-with-muxing.node.js @@ -3,6 +3,7 @@ const expect = require('chai').expect +const parallel = require('run-parallel') const multiaddr = require('multiaddr') const Peer = require('peer-info') const Swarm = require('../src') @@ -45,19 +46,13 @@ describe('high level API - with everything mixed all together!', function () { }) after((done) => { - var counter = 0 - - swarmA.close(closed) - swarmB.close(closed) - // swarmC.close(closed) - swarmD.close(closed) - swarmE.close(closed) - - function closed () { - if (++counter === 4) { - done() - } - } + parallel([ + (cb) => swarmA.close(cb), + (cb) => swarmB.close(cb), + // (cb) => swarmC.close(cb), + (cb) => swarmD.close(cb), + (cb) => swarmE.close(cb) + ], done) }) it('add tcp', (done) => { @@ -214,7 +209,9 @@ describe('high level API - with everything mixed all together!', function () { }) it('close a muxer emits event', (done) => { - swarmC.close(() => {}) + swarmC.close((err) => { + if (err) throw err + }) swarmA.once('peer-mux-closed', (peerInfo) => { done() }) diff --git a/test/browser.js b/test/browser.js index bcd92cb..dc3ea2b 100644 --- a/test/browser.js +++ b/test/browser.js @@ -73,8 +73,7 @@ describe('high level API - 1st without stream multiplexing (on websockets)', fun }) after((done) => { - done() - // swarm.close(done) + swarm.close(done) }) it('add ws', (done) => {