Skip to content

Commit

Permalink
comply with syntax and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucidar committed May 6, 2016
1 parent bf7935f commit 4240426
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
12 changes: 6 additions & 6 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Browser.prototype.start = function () {
var nameMap = {}
if (!this._wildcard) nameMap[this._name] = true

this._onresponse = function (packet,rinfo) {
this._onresponse = function (packet) {
if (self._wildcard) {
packet.answers.forEach(function (answer) {
if (answer.type !== 'PTR' || answer.name !== self._name || answer.name in nameMap) return
Expand All @@ -75,7 +75,7 @@ Browser.prototype.start = function () {
Object.keys(nameMap).forEach(function (name) {
goodbyes(name, packet).forEach(self._removeService.bind(self))

var matches = buildServicesFor(name, packet,rinfo)
var matches = buildServicesFor(name, packet)
if (matches.length === 0) return

matches.forEach(function (service) {
Expand Down Expand Up @@ -131,7 +131,7 @@ function goodbyes (name, packet) {
})
}

function buildServicesFor (name, packet,rinfo) {
function buildServicesFor (name, packet) {
var records = packet.answers.concat(packet.additionals).filter(function (rr) {
return rr.ttl > 0 // ignore goodbye messages
})
Expand All @@ -155,7 +155,6 @@ function buildServicesFor (name, packet,rinfo) {
var name = parts[0]
var types = serviceName.parse(parts.slice(1, -1).join('.'))
service.name = name
service.rinfo=rinfo;
service.fqdn = rr.name
service.host = rr.data.target
service.port = rr.data.port
Expand All @@ -175,9 +174,10 @@ function buildServicesFor (name, packet,rinfo) {
return (rr.type === 'A' || rr.type === 'AAAA') && dnsEqual(rr.name, service.host)
})
.forEach(function (rr) {
service.addresses.push(rr.data)
if (service.addresses.indexOf(rr.data) === -1) { // avoid duplicate
service.addresses.push(rr.data)
}
})

return service
})
.filter(function (rr) {
Expand Down
4 changes: 2 additions & 2 deletions lib/mdns-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Server.prototype._recordsFor = function (name, type) {
if (name) {
return self.registry[type].filter(function (record) {
var _name = ~name.indexOf('.') ? record.name : record.name.split('.')[0]
var host = (record.type==='A' || record.type==='AAAA');
return dnsEqual(_name, name) || host; // the A or AAAA name match the host name, not the service name
var host = (record.type === 'A' || record.type === 'AAAA')
return dnsEqual(_name, name) || host // the A or AAAA name match the host name, not the service name
})
} else {
return self.registry[type]
Expand Down
30 changes: 15 additions & 15 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ function Service (opts) {
}

Service.prototype._records = function () {
var records = [rr_ptr(this), rr_srv(this), rr_txt(this)];
var self = this;
var networks_list = os.networkInterfaces();
Object.keys(networks_list).forEach(function(itr_idx,index,arr){//itr_idx = interface name
networks_list[itr_idx].forEach(function(itr,index2,arr2){ // for each interface (itr)
if (itr.internal === false && itr.family === 'IPv4'){
records.push(rr_a(self,itr.address));
}else if(itr.internal === false && itr.family === 'IPv6'){
records.push(rr_aaaa(self,itr.address));
}
});
});
return records;
var records = [rr_ptr(this), rr_srv(this), rr_txt(this)]
var self = this
var networks_list = os.networkInterfaces()
Object.keys(networks_list).forEach(function (itr_idx, index, arr) { // itr_idx = interface name
networks_list[itr_idx].forEach(function (itr, index2, arr2) { // for each interface (itr)
if (itr.internal === false && itr.family === 'IPv4') {
records.push(rr_a(self, itr.address))
} else if (itr.internal === false && itr.family === 'IPv6') {
records.push(rr_aaaa(self, itr.address))
}
})
})
return records
}

function rr_ptr (service) {
Expand Down Expand Up @@ -76,7 +76,7 @@ function rr_txt (service) {
}
}

function rr_a (service,ip_address) {
function rr_a (service, ip_address) {
return {
name: service.host,
type: 'A',
Expand All @@ -85,7 +85,7 @@ function rr_a (service,ip_address) {
}
}

function rr_aaaa (service,ip_address) {
function rr_aaaa (service, ip_address) {
return {
name: service.host,
type: 'AAAA',
Expand Down
19 changes: 18 additions & 1 deletion test/bonjour.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ var afterAll = require('after-all')
var Service = require('../lib/service')
var Bonjour = require('../')

var get_addresses = function () {
var addresses = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
var addrs = itrs[i]
for (var j in addrs) {
if (addrs[j].internal === false) {
addresses.push(addrs[j].address)
}
}
}
return addresses
}

var port = function (cb) {
var s = dgram.createSocket('udp4')
s.bind(0, function () {
Expand Down Expand Up @@ -80,13 +94,14 @@ test('bonjour.find', function (bonjour, t) {
t.equal(s.type, 'test')
t.equal(s.protocol, 'tcp')
t.deepEqual(s.subtypes, [])
t.deepEqual(s.addresses, [])
t.deepEqual(s.addresses, get_addresses())

if (++ups === 2) {
// use timeout in an attempt to make sure the invalid record doesn't
// bubble up
setTimeout(function () {
bonjour.destroy()
browser.stop()
t.end()
}, 50)
}
Expand All @@ -111,6 +126,7 @@ test('bonjour.find - down event', function (bonjour, t) {

browser.on('down', function (s) {
t.equal(s.name, 'Foo Bar')
browser.stop()
bonjour.destroy()
t.end()
})
Expand All @@ -135,6 +151,7 @@ test('bonjour.findOne - emitter', function (bonjour, t) {
var browser = bonjour.findOne({ type: 'test' })
browser.on('up', function (s) {
t.equal(s.name, 'Emitter')
browser.stop()
bonjour.destroy()
t.end()
})
Expand Down
18 changes: 16 additions & 2 deletions test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ var os = require('os')
var test = require('tape')
var Service = require('../lib/service')

var get_addresses_records = function (host) {
var addresses_records = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
var addrs = itrs[i]
for (var j in addrs) {
if (addrs[j].internal === false) {
addresses_records.push({ data: addrs[j].address, name: host, ttl: 120, type: addrs[j].family === 'IPv4' ? 'A' : 'AAAA' })
}
}
}
return addresses_records
}

test('no name', function (t) {
t.throws(function () {
new Service({ type: 'http', port: 3000 }) // eslint-disable-line no-new
Expand Down Expand Up @@ -63,7 +77,7 @@ test('_records() - minimal', function (t) {
{ data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
{ data: { port: 3000, target: os.hostname() }, name: s.fqdn, ttl: 120, type: 'SRV' },
{ data: new Buffer('00', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
])
].concat(get_addresses_records(s.host)))
t.end()
})

Expand All @@ -73,6 +87,6 @@ test('_records() - everything', function (t) {
{ data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
{ data: { port: 3000, target: 'example.com' }, name: s.fqdn, ttl: 120, type: 'SRV' },
{ data: new Buffer('07666f6f3d626172', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
])
].concat(get_addresses_records(s.host)))
t.end()
})

0 comments on commit 4240426

Please sign in to comment.