diff --git a/src/index.js b/src/index.js index 80c2471a80..2cd1121c7a 100644 --- a/src/index.js +++ b/src/index.js @@ -156,15 +156,15 @@ class Node extends EventEmitter { // so that we can have webrtc-star addrs without adding manually the id const maOld = [] const maNew = [] - this.peerInfo.multiaddrs.forEach((ma) => { + this.peerInfo.multiaddrs.toArray().forEach((ma) => { if (!ma.getPeerId()) { maOld.push(ma) maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())) } }) this.peerInfo.multiaddrs.replace(maOld, maNew) - const multiaddrs = this.peerInfo.multiaddrs.toArray() + const multiaddrs = this.peerInfo.multiaddrs.toArray() transports.forEach((transport) => { if (transport.filter(multiaddrs).length > 0) { this.swarm.transport.add( @@ -200,6 +200,19 @@ class Node extends EventEmitter { } 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) => { this.emit('start') cb() diff --git a/test/multiaddr-trim.js b/test/multiaddr-trim.js new file mode 100644 index 0000000000..9dc93715b2 --- /dev/null +++ b/test/multiaddr-trim.js @@ -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) + }) + }) +}) diff --git a/test/node.js b/test/node.js index 6bde025de1..ede2d7f8c4 100644 --- a/test/node.js +++ b/test/node.js @@ -7,3 +7,4 @@ require('./peer-discovery.node') require('./peer-routing.node') require('./content-routing.node') require('./circuit-relay.node') +require('./multiaddr-trim')