diff --git a/package.json b/package.json index 8f9e242..6973ca3 100644 --- a/package.json +++ b/package.json @@ -30,18 +30,18 @@ }, "homepage": "https://github.com/libp2p/js-libp2p-mdns", "devDependencies": { - "aegir": "^13.0.6", - "async": "^2.6.0", + "aegir": "^14.0.0", + "async": "^2.6.1", "chai": "^4.1.2", "dirty-chai": "^2.0.1", "pre-commit": "^1.2.2" }, "dependencies": { "libp2p-tcp": "~0.12.0", - "multiaddr": "^4.0.0", + "multiaddr": "^5.0.0", "multicast-dns": "^7.0.0", "peer-id": "~0.10.7", - "peer-info": "~0.14.0" + "peer-info": "~0.14.1" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index 04f8b95..d2c3ef3 100644 --- a/src/index.js +++ b/src/index.js @@ -2,20 +2,21 @@ const multicastDNS = require('multicast-dns') const EventEmitter = require('events').EventEmitter +const assert = require('assert') const debug = require('debug') const log = debug('libp2p:mdns') const query = require('./query') class MulticastDNS extends EventEmitter { - constructor (peerInfo, options) { + constructor (options) { super() - options = options || {} + assert(options.peerInfo, 'needs a PeerInfo to work') this.broadcast = options.broadcast !== false this.interval = options.interval || (1e3 * 10) this.serviceTag = options.serviceTag || 'ipfs.local' this.port = options.port || 5353 - this.peerInfo = peerInfo + this.peerInfo = options.peerInfo this._queryInterval = null } @@ -56,7 +57,8 @@ class MulticastDNS extends EventEmitter { } } -module.exports = MulticastDNS +exports = module.exports = MulticastDNS +exports.tag = 'mdns' /* for reference diff --git a/test/multicast-dns.spec.js b/test/multicast-dns.spec.js index f7f1a7b..8958477 100644 --- a/test/multicast-dns.spec.js +++ b/test/multicast-dns.spec.js @@ -20,6 +20,7 @@ describe('MulticastDNS', () => { before(function (done) { this.timeout(80 * 1000) + parallel([ (cb) => { PeerInfo.create((err, peer) => { @@ -63,14 +64,16 @@ describe('MulticastDNS', () => { it('find another peer', function (done) { this.timeout(40 * 1000) - const options = { - port: 50001 // port must be the same - } - const mdnsA = new MulticastDNS(pA, { + const mdnsA = new MulticastDNS({ + peerInfo: pA, broadcast: false, // do not talk to ourself port: 50001 }) - const mdnsB = new MulticastDNS(pB, options) + + const mdnsB = new MulticastDNS({ + peerInfo: pB, + port: 50001 // port must be the same + }) parallel([ (cb) => mdnsA.start(cb), @@ -91,16 +94,19 @@ describe('MulticastDNS', () => { it('only announce TCP multiaddrs', function (done) { this.timeout(40 * 1000) - const options = { - port: 50003 // port must be the same - } - - const mdnsA = new MulticastDNS(pA, { + const mdnsA = new MulticastDNS({ + peerInfo: pA, broadcast: false, // do not talk to ourself port: 50003 }) - const mdnsC = new MulticastDNS(pC, options) - const mdnsD = new MulticastDNS(pD, options) + const mdnsC = new MulticastDNS({ + peerInfo: pC, + port: 50003 // port must be the same + }) + const mdnsD = new MulticastDNS({ + peerInfo: pD, + port: 50003 // port must be the same + }) parallel([ (cb) => mdnsA.start(cb), @@ -125,14 +131,16 @@ describe('MulticastDNS', () => { it('announces IP6 addresses', function (done) { this.timeout(40 * 1000) - const options = { - port: 50001 // port must be the same - } - const mdnsA = new MulticastDNS(pA, { + const mdnsA = new MulticastDNS({ + peerInfo: pA, broadcast: false, // do not talk to ourself port: 50001 }) - const mdnsB = new MulticastDNS(pB, options) + + const mdnsB = new MulticastDNS({ + peerInfo: pB, + port: 50001 + }) series([ (cb) => mdnsB.start(cb), @@ -154,11 +162,15 @@ describe('MulticastDNS', () => { it('doesn\'t emit peers after stop', function (done) { this.timeout(40 * 1000) - const options = { + const mdnsA = new MulticastDNS({ + peerInfo: pA, port: 50004 // port must be the same - } - const mdnsA = new MulticastDNS(pA, options) - const mdnsC = new MulticastDNS(pC, options) + }) + + const mdnsC = new MulticastDNS({ + peerInfo: pC, + port: 50004 + }) series([ (cb) => mdnsA.start(cb),