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

Commit 6414c1e

Browse files
feat: add multiplex to default muxers
This adds the possibility to set muxers via `options.muxer` in the constructor. BREAKING CHANGE: Prefer multiplex over spdy, as it works better with go-ipfs
1 parent eed8c8f commit 6414c1e

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-nodejs#readme",
3939
"devDependencies": {
4040
"aegir": "^10.0.0",
41-
"async": "^2.1.4",
41+
"async": "^2.1.5",
4242
"chai": "^3.5.0",
4343
"pre-commit": "^1.2.2",
4444
"pull-stream": "^3.5.0"
4545
},
4646
"dependencies": {
4747
"libp2p": "~0.5.4",
4848
"libp2p-mdns": "~0.6.1",
49-
"libp2p-multiplex": "~0.4.0",
49+
"libp2p-multiplex": "~0.4.1",
5050
"libp2p-railing": "~0.4.1",
5151
"libp2p-secio": "~0.6.7",
5252
"libp2p-spdy": "~0.10.4",
53-
"libp2p-swarm": "~0.26.17",
53+
"libp2p-swarm": "~0.26.18",
5454
"libp2p-tcp": "~0.9.3",
5555
"libp2p-webrtc-star": "~0.8.8",
5656
"libp2p-websockets": "~0.9.2",
@@ -72,4 +72,4 @@
7272
"kumavis <kumavis@users.noreply.github.com>",
7373
"varunagarwal315 <varunagarwal315@gmail.com>"
7474
]
75-
}
75+
}

src/index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ const multiplex = require('libp2p-multiplex')
1111
const secio = require('libp2p-secio')
1212
const libp2p = require('libp2p')
1313

14+
function mapMuxers (list) {
15+
return list.map((pref) => {
16+
if (typeof pref !== 'string') {
17+
return pref
18+
}
19+
switch (pref.trim().toLowerCase()) {
20+
case 'spdy':
21+
return spdy
22+
case 'multiplex':
23+
return multiplex
24+
default:
25+
throw new Error(pref + ' muxer not available')
26+
}
27+
})
28+
}
29+
30+
function getMuxers (options) {
31+
if (process.env.LIBP2P_MUXER) {
32+
const muxerPrefs = process.env.LIBP2P_MUXER
33+
return mapMuxers(muxerPrefs.split(','))
34+
} else if (options) {
35+
return mapMuxers(options)
36+
} else {
37+
return [multiplex, spdy]
38+
}
39+
}
40+
1441
class Node extends libp2p {
1542
constructor (peerInfo, peerBook, options) {
1643
options = options || {}
@@ -23,16 +50,7 @@ class Node extends libp2p {
2350
webRTCStar
2451
],
2552
connection: {
26-
muxer: process.env.LIBP2P_MUXER ? (() => {
27-
const muxerPrefs = process.env.LIBP2P_MUXER
28-
return muxerPrefs.split(',').map((pref) => {
29-
switch (pref) {
30-
case 'spdy': return spdy
31-
case 'multiplex': return multiplex
32-
default: throw new Error(pref + ' muxer not available')
33-
}
34-
})
35-
})() : [spdy],
53+
muxer: getMuxers(options.muxer),
3654
crypto: [
3755
secio
3856
]

test/libp2p.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ describe('libp2p-ipfs-nodejs', () => {
138138
})
139139
})
140140

141+
it('create libp2pNode with multiplex only', (done) => {
142+
const old = process.env.LIBP2P_MUXER
143+
process.env.LIBP2P_MUXER = ''
144+
PeerInfo.create((err, info) => {
145+
expect(err).to.not.exist
146+
const b = new Node(info, null, {muxer: ['multiplex']})
147+
expect(b.modules.connection.muxer).to.eql([
148+
require('libp2p-multiplex')
149+
])
150+
151+
if (old) {
152+
process.env.LIBP2P_MUXER = old
153+
}
154+
done()
155+
})
156+
})
157+
141158
it('mdns discovery', (done) => {
142159
nodeA.discovery.once('peer', (peerInfo) => {
143160
expect(nodeB.peerInfo.id.toB58String())

0 commit comments

Comments
 (0)