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

feat: add multiplex to default muxers #78

Merged
merged 5 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
"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"
},
"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",
"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",
Expand All @@ -72,4 +72,4 @@
"kumavis <[email protected]>",
"varunagarwal315 <[email protected]>"
]
}
}
38 changes: 28 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ 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 mapMuxers(muxerPrefs.split(','))
} else if (options) {
return mapMuxers(options)
} else {
return [multiplex, spdy]
}
}

class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
Expand All @@ -23,16 +50,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(options.muxer),
crypto: [
secio
]
Expand Down
17 changes: 17 additions & 0 deletions test/libp2p.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ 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')
])

if (old) {
process.env.LIBP2P_MUXER = old
}
done()
})
})

it('mdns discovery', (done) => {
nodeA.discovery.once('peer', (peerInfo) => {
expect(nodeB.peerInfo.id.toB58String())
Expand Down