Skip to content

Commit 23e8293

Browse files
committed
feat: use libp2p-switch
1 parent 68c170a commit 23e8293

7 files changed

+56
-56
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dependencies": {
4040
"async": "^2.6.0",
4141
"libp2p-ping": "~0.6.0",
42-
"libp2p-swarm": "~0.35.1",
42+
"libp2p-switch": "~0.36.0",
4343
"mafmt": "^3.0.2",
4444
"multiaddr": "^3.0.2",
4545
"peer-book": "~0.5.4",
@@ -53,13 +53,13 @@
5353
"dirty-chai": "^2.0.1",
5454
"electron-webrtc": "~0.3.0",
5555
"libp2p-circuit": "~0.1.4",
56-
"libp2p-kad-dht": "~0.6.0",
57-
"libp2p-mdns": "~0.9.1",
56+
"libp2p-kad-dht": "~0.6.3",
57+
"libp2p-mdns": "~0.9.2",
5858
"libp2p-multiplex": "~0.5.1",
5959
"libp2p-railing": "~0.7.1",
6060
"libp2p-secio": "~0.9.1",
6161
"libp2p-spdy": "~0.11.0",
62-
"libp2p-tcp": "~0.11.2",
62+
"libp2p-tcp": "~0.11.5",
6363
"libp2p-webrtc-star": "~0.13.3",
6464
"libp2p-websockets": "~0.10.4",
6565
"libp2p-websocket-star": "~0.7.2",

src/index.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const each = require('async/each')
88
const series = require('async/series')
99

1010
const Ping = require('libp2p-ping')
11-
const Swarm = require('libp2p-swarm')
11+
const Switch = require('libp2p-switch')
1212
const PeerId = require('peer-id')
1313
const PeerInfo = require('peer-info')
1414
const PeerBook = require('peer-book')
@@ -31,28 +31,28 @@ class Node extends EventEmitter {
3131

3232
this._isStarted = false
3333

34-
this.swarm = new Swarm(this.peerInfo, this.peerBook)
34+
this.switch = new Switch(this.peerInfo, this.peerBook)
3535

3636
// Attach stream multiplexers
3737
if (this.modules.connection && this.modules.connection.muxer) {
3838
let muxers = this.modules.connection.muxer
3939
muxers = Array.isArray(muxers) ? muxers : [muxers]
40-
muxers.forEach((muxer) => this.swarm.connection.addStreamMuxer(muxer))
40+
muxers.forEach((muxer) => this.switch.connection.addStreamMuxer(muxer))
4141

4242
// If muxer exists, we can use Identify
43-
this.swarm.connection.reuse()
43+
this.switch.connection.reuse()
4444

4545
// If muxer exists, we can use Relay for listening/dialing
46-
this.swarm.connection.enableCircuitRelay(_options.relay)
46+
this.switch.connection.enableCircuitRelay(_options.relay)
4747

4848
// Received incommind dial and muxer upgrade happened,
4949
// reuse this muxed connection
50-
this.swarm.on('peer-mux-established', (peerInfo) => {
50+
this.switch.on('peer-mux-established', (peerInfo) => {
5151
this.emit('peer:connect', peerInfo)
5252
this.peerBook.put(peerInfo)
5353
})
5454

55-
this.swarm.on('peer-mux-closed', (peerInfo) => {
55+
this.switch.on('peer-mux-closed', (peerInfo) => {
5656
this.emit('peer:disconnect', peerInfo)
5757
})
5858
}
@@ -62,7 +62,7 @@ class Node extends EventEmitter {
6262
let cryptos = this.modules.connection.crypto
6363
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
6464
cryptos.forEach((crypto) => {
65-
this.swarm.connection.crypto(crypto.tag, crypto.encrypt)
65+
this.switch.connection.crypto(crypto.tag, crypto.encrypt)
6666
})
6767
}
6868

@@ -77,11 +77,11 @@ class Node extends EventEmitter {
7777
}
7878

7979
// Mount default protocols
80-
Ping.mount(this.swarm)
80+
Ping.mount(this.switch)
8181

8282
// dht provided components (peerRouting, contentRouting, dht)
8383
if (_modules.DHT) {
84-
this._dht = new this.modules.DHT(this.swarm, {
84+
this._dht = new this.modules.DHT(this.switch, {
8585
kBucketSize: 20,
8686
datastore: _options.DHT && _options.DHT.datastore
8787
})
@@ -167,7 +167,7 @@ class Node extends EventEmitter {
167167
const multiaddrs = this.peerInfo.multiaddrs.toArray()
168168
transports.forEach((transport) => {
169169
if (transport.filter(multiaddrs).length > 0) {
170-
this.swarm.transport.add(
170+
this.switch.transport.add(
171171
transport.tag || transport.constructor.name, transport)
172172
} else if (transport.constructor &&
173173
transport.constructor.name === 'WebSockets') {
@@ -178,11 +178,11 @@ class Node extends EventEmitter {
178178
})
179179

180180
series([
181-
(cb) => this.swarm.listen(cb),
181+
(cb) => this.switch.start(cb),
182182
(cb) => {
183183
if (ws) {
184184
// always add dialing on websockets
185-
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
185+
this.switch.transport.add(ws.tag || ws.constructor.name, ws)
186186
}
187187

188188
// all transports need to be setup before discover starts
@@ -237,7 +237,7 @@ class Node extends EventEmitter {
237237
}
238238
cb()
239239
},
240-
(cb) => this.swarm.close(cb),
240+
(cb) => this.switch.stop(cb),
241241
(cb) => {
242242
this.emit('stop')
243243
cb()
@@ -259,7 +259,7 @@ class Node extends EventEmitter {
259259
return callback(err)
260260
}
261261

262-
callback(null, new Ping(this.swarm, peerInfo))
262+
callback(null, new Ping(this.switch, peerInfo))
263263
})
264264
}
265265

@@ -276,7 +276,7 @@ class Node extends EventEmitter {
276276
return callback(err)
277277
}
278278

279-
this.swarm.dial(peerInfo, protocol, (err, conn) => {
279+
this.switch.dial(peerInfo, protocol, (err, conn) => {
280280
if (err) {
281281
return callback(err)
282282
}
@@ -294,16 +294,16 @@ class Node extends EventEmitter {
294294
return callback(err)
295295
}
296296

297-
this.swarm.hangUp(peerInfo, callback)
297+
this.switch.hangUp(peerInfo, callback)
298298
})
299299
}
300300

301301
handle (protocol, handlerFunc, matchFunc) {
302-
this.swarm.handle(protocol, handlerFunc, matchFunc)
302+
this.switch.handle(protocol, handlerFunc, matchFunc)
303303
}
304304

305305
unhandle (protocol) {
306-
this.swarm.unhandle(protocol)
306+
this.switch.unhandle(protocol)
307307
}
308308

309309
/*

test/circuit-relay.node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('circuit relay', function () {
3939
node.start((err) => {
4040
expect(err).to.not.exist()
4141

42-
handlerSpies.push(sinon.spy(node.swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
42+
handlerSpies.push(sinon.spy(node.switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
4343
cb(node)
4444
})
4545
})

test/stream-muxing.node.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ describe('stream muxing', () => {
200200
(cb) => setup(cb),
201201
(cb) => {
202202
// it will just 'warm up a conn'
203-
expect(Object.keys(nodeA.swarm.muxers)).to.have.length(1)
204-
expect(Object.keys(nodeB.swarm.muxers)).to.have.length(1)
203+
expect(Object.keys(nodeA.switch.muxers)).to.have.length(1)
204+
expect(Object.keys(nodeB.switch.muxers)).to.have.length(1)
205205

206206
nodeA.dial(nodeB.peerInfo, (err) => {
207207
expect(err).to.not.exist()
208-
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
208+
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
209209
cb()
210210
})
211211
},

test/transports.browser.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('transports', () => {
105105
function check () {
106106
const peers = nodeA.peerBook.getAll()
107107
expect(Object.keys(peers)).to.have.length(1)
108-
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
108+
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
109109
done()
110110
}
111111
})
@@ -153,7 +153,7 @@ describe('transports', () => {
153153
const peers = nodeA.peerBook.getAll()
154154
expect(err).to.not.exist()
155155
expect(Object.keys(peers)).to.have.length(1)
156-
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
156+
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
157157
done()
158158
}
159159
})
@@ -280,7 +280,7 @@ describe('transports', () => {
280280
function check () {
281281
const peers = node1.peerBook.getAll()
282282
expect(Object.keys(peers)).to.have.length(1)
283-
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
283+
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
284284
done()
285285
}
286286
})
@@ -291,8 +291,8 @@ describe('transports', () => {
291291

292292
function check () {
293293
if (++counter === 3) {
294-
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
295-
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
294+
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
295+
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
296296
done()
297297
}
298298
}
@@ -389,7 +389,7 @@ describe('transports', () => {
389389
function check () {
390390
const peers = node1.peerBook.getAll()
391391
expect(Object.keys(peers)).to.have.length(1)
392-
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
392+
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
393393
done()
394394
}
395395
})
@@ -400,8 +400,8 @@ describe('transports', () => {
400400

401401
function check () {
402402
if (++counter === 3) {
403-
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
404-
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
403+
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
404+
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
405405
done()
406406
}
407407
}

0 commit comments

Comments
 (0)