From cbc5e2157c8ae06b23ce1e96b9939a71f5896e23 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 23 Feb 2017 00:08:43 +0100 Subject: [PATCH 1/5] feat: add multiplex to default muxers --- package.json | 4 ++-- src/index.js | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index d119c13..813be95 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "dependencies": { "libp2p": "~0.5.4", "libp2p-mdns": "~0.6.1", - "libp2p-multiplex": "~0.4.0", + "libp2p-multiplex": "~0.4.1", "libp2p-railing": "~0.4.1", "libp2p-secio": "~0.6.7", "libp2p-spdy": "~0.10.4", @@ -72,4 +72,4 @@ "kumavis ", "varunagarwal315 " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index aec8b88..0651300 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,23 @@ const multiplex = require('libp2p-multiplex') const secio = require('libp2p-secio') const libp2p = require('libp2p') +function getMuxers (options) { + if (process.env.LIBP2P_MUXER) { + const muxerPrefs = process.env.LIBP2P_MUXER + return muxerPrefs.split(',').map((pref) => { + switch (pref) { + case 'spdy': return spdy + case 'multiplex': return multiplex + default: throw new Error(pref + ' muxer not available') + } + }) + } else if (options) { + return options + } else { + return [spdy, multiplex] + } +} + class Node extends libp2p { constructor (peerInfo, peerBook, options) { options = options || {} @@ -23,16 +40,7 @@ class Node extends libp2p { webRTCStar ], connection: { - muxer: process.env.LIBP2P_MUXER ? (() => { - const muxerPrefs = process.env.LIBP2P_MUXER - return muxerPrefs.split(',').map((pref) => { - switch (pref) { - case 'spdy': return spdy - case 'multiplex': return multiplex - default: throw new Error(pref + ' muxer not available') - } - }) - })() : [spdy], + muxer: getMuxers(), crypto: [ secio ] From b698f5262efef67aecc80885abe675894fd46f60 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 24 Feb 2017 13:40:16 +0100 Subject: [PATCH 2/5] fix muxer options --- package.json | 2 +- src/index.js | 28 +++++++++++++++++++--------- test/libp2p.spec.js | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 813be95..02a0ad5 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "homepage": "https://github.com/ipfs/js-libp2p-ipfs-nodejs#readme", "devDependencies": { "aegir": "^10.0.0", - "async": "^2.1.4", + "async": "^2.1.5", "chai": "^3.5.0", "pre-commit": "^1.2.2", "pull-stream": "^3.5.0" diff --git a/src/index.js b/src/index.js index 0651300..e8e76b1 100644 --- a/src/index.js +++ b/src/index.js @@ -11,18 +11,28 @@ const multiplex = require('libp2p-multiplex') const secio = require('libp2p-secio') const libp2p = require('libp2p') +function mapMuxers (list) { + return list.map((pref) => { + if (typeof pref !== 'string') { + return pref + } + switch (pref.trim().toLowerCase()) { + case 'spdy': + return spdy + case 'multiplex': + return multiplex + default: + throw new Error(pref + ' muxer not available') + } + }) +} + function getMuxers (options) { if (process.env.LIBP2P_MUXER) { const muxerPrefs = process.env.LIBP2P_MUXER - return muxerPrefs.split(',').map((pref) => { - switch (pref) { - case 'spdy': return spdy - case 'multiplex': return multiplex - default: throw new Error(pref + ' muxer not available') - } - }) + return mapMuxers(muxerPrefs.split(',')) } else if (options) { - return options + return mapMuxers(options) } else { return [spdy, multiplex] } @@ -40,7 +50,7 @@ class Node extends libp2p { webRTCStar ], connection: { - muxer: getMuxers(), + muxer: getMuxers(options.muxer), crypto: [ secio ] diff --git a/test/libp2p.spec.js b/test/libp2p.spec.js index 4585f0a..629c0a0 100644 --- a/test/libp2p.spec.js +++ b/test/libp2p.spec.js @@ -138,6 +138,21 @@ describe('libp2p-ipfs-nodejs', () => { }) }) + it('create libp2pNode with multiplex only', (done) => { + const old = process.env.LIBP2P_MUXER + process.env.LIBP2P_MUXER = '' + PeerInfo.create((err, info) => { + expect(err).to.not.exist + const b = new Node(info, null, {muxer: ['multiplex']}) + expect(b.modules.connection.muxer).to.eql([ + require('libp2p-multiplex') + ]) + + process.env.LIBP2P_MUXER = old + done() + }) + }) + it('mdns discovery', (done) => { nodeA.discovery.once('peer', (peerInfo) => { expect(nodeB.peerInfo.id.toB58String()) From b25e18ac9278bd6836bf5b9d2beb414b780bb373 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 24 Feb 2017 15:41:38 +0100 Subject: [PATCH 3/5] default to multiplex --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e8e76b1..39db2a9 100644 --- a/src/index.js +++ b/src/index.js @@ -34,7 +34,7 @@ function getMuxers (options) { } else if (options) { return mapMuxers(options) } else { - return [spdy, multiplex] + return [multiplex, spdy] } } From 88338fc0f4f274c110349dcfcd136e7cebd319f2 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 24 Feb 2017 16:51:34 +0100 Subject: [PATCH 4/5] update libp2p-swarm --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 02a0ad5..5f98c53 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "libp2p-railing": "~0.4.1", "libp2p-secio": "~0.6.7", "libp2p-spdy": "~0.10.4", - "libp2p-swarm": "~0.26.17", + "libp2p-swarm": "~0.26.18", "libp2p-tcp": "~0.9.3", "libp2p-webrtc-star": "~0.8.8", "libp2p-websockets": "~0.9.2", From ffc3e607295fade3419b74e2d72baf0a2c2ab130 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 24 Feb 2017 17:51:30 +0100 Subject: [PATCH 5/5] fix test --- test/libp2p.spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/libp2p.spec.js b/test/libp2p.spec.js index 629c0a0..45028a5 100644 --- a/test/libp2p.spec.js +++ b/test/libp2p.spec.js @@ -148,7 +148,9 @@ describe('libp2p-ipfs-nodejs', () => { require('libp2p-multiplex') ]) - process.env.LIBP2P_MUXER = old + if (old) { + process.env.LIBP2P_MUXER = old + } done() }) })