mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-27 03:22:15 +00:00
fix: remove innactive multiaddrs (#131)
This commit is contained in:
parent
67ca37c356
commit
1b7360fcfa
17
src/index.js
17
src/index.js
@ -156,15 +156,15 @@ class Node extends EventEmitter {
|
|||||||
// so that we can have webrtc-star addrs without adding manually the id
|
// so that we can have webrtc-star addrs without adding manually the id
|
||||||
const maOld = []
|
const maOld = []
|
||||||
const maNew = []
|
const maNew = []
|
||||||
this.peerInfo.multiaddrs.forEach((ma) => {
|
this.peerInfo.multiaddrs.toArray().forEach((ma) => {
|
||||||
if (!ma.getPeerId()) {
|
if (!ma.getPeerId()) {
|
||||||
maOld.push(ma)
|
maOld.push(ma)
|
||||||
maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()))
|
maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.peerInfo.multiaddrs.replace(maOld, maNew)
|
this.peerInfo.multiaddrs.replace(maOld, maNew)
|
||||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
|
||||||
|
|
||||||
|
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||||
transports.forEach((transport) => {
|
transports.forEach((transport) => {
|
||||||
if (transport.filter(multiaddrs).length > 0) {
|
if (transport.filter(multiaddrs).length > 0) {
|
||||||
this.swarm.transport.add(
|
this.swarm.transport.add(
|
||||||
@ -200,6 +200,19 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
|
(cb) => {
|
||||||
|
// detect which multiaddrs we don't have a transport for and remove them
|
||||||
|
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||||
|
transports.forEach((transport) => {
|
||||||
|
multiaddrs.forEach((multiaddr) => {
|
||||||
|
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
|
||||||
|
!transports.find((transport) => transport.filter(multiaddr).length > 0)) {
|
||||||
|
this.peerInfo.multiaddrs.delete(multiaddr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
cb()
|
||||||
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
this.emit('start')
|
this.emit('start')
|
||||||
cb()
|
cb()
|
||||||
|
40
test/multiaddr-trim.js
Normal file
40
test/multiaddr-trim.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
chai.use(require('dirty-chai'))
|
||||||
|
const expect = chai.expect
|
||||||
|
const series = require('async/series')
|
||||||
|
const createNode = require('./utils/node').createNode
|
||||||
|
|
||||||
|
describe('multiaddr trim', () => {
|
||||||
|
it('non used multiaddrs get trimmed', (done) => {
|
||||||
|
let node
|
||||||
|
|
||||||
|
series([
|
||||||
|
(cb) => createNode([
|
||||||
|
'/ip4/0.0.0.0/tcp/999/wss/p2p-webrtc-direct',
|
||||||
|
'/ip4/127.0.0.1/tcp/55555/ws',
|
||||||
|
'/ip4/0.0.0.0/tcp/0/'
|
||||||
|
], (err, _node) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
node = _node
|
||||||
|
const multiaddrs = node.peerInfo.multiaddrs.toArray()
|
||||||
|
// multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||||
|
expect(multiaddrs).to.have.length(3)
|
||||||
|
cb()
|
||||||
|
}),
|
||||||
|
(cb) => node.start(cb)
|
||||||
|
], (err) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
|
const multiaddrs = node.peerInfo.multiaddrs.toArray()
|
||||||
|
// console.log('--')
|
||||||
|
// multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||||
|
|
||||||
|
expect(multiaddrs.length).to.at.least(2)
|
||||||
|
expect(multiaddrs[0].toString()).to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ws\/ipfs\/\w+$/)
|
||||||
|
node.stop(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -7,3 +7,4 @@ require('./peer-discovery.node')
|
|||||||
require('./peer-routing.node')
|
require('./peer-routing.node')
|
||||||
require('./content-routing.node')
|
require('./content-routing.node')
|
||||||
require('./circuit-relay.node')
|
require('./circuit-relay.node')
|
||||||
|
require('./multiaddr-trim')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user