Skip to content

Commit

Permalink
Clean up PR #12
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed May 6, 2016
1 parent 4240426 commit 8be9758
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function buildServicesFor (name, packet) {
service.addresses.push(rr.data)
}
})

return service
})
.filter(function (rr) {
Expand Down
25 changes: 14 additions & 11 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ 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))
var interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach(function (name) {
interfaces[name].forEach(function (addr) {
if (addr.internal) return
if (addr.family === 'IPv4') {
records.push(rr_a(self, addr.address))
} else {
records.push(rr_aaaa(self, addr.address))
}
})
})

return records
}

Expand Down Expand Up @@ -76,20 +79,20 @@ function rr_txt (service) {
}
}

function rr_a (service, ip_address) {
function rr_a (service, ip) {
return {
name: service.host,
type: 'A',
ttl: 120,
data: ip_address
data: ip
}
}

function rr_aaaa (service, ip_address) {
function rr_aaaa (service, ip) {
return {
name: service.host,
type: 'AAAA',
ttl: 120,
data: ip_address
data: ip
}
}
7 changes: 2 additions & 5 deletions test/bonjour.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var afterAll = require('after-all')
var Service = require('../lib/service')
var Bonjour = require('../')

var get_addresses = function () {
var getAddresses = function () {
var addresses = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
Expand Down Expand Up @@ -94,14 +94,13 @@ test('bonjour.find', function (bonjour, t) {
t.equal(s.type, 'test')
t.equal(s.protocol, 'tcp')
t.deepEqual(s.subtypes, [])
t.deepEqual(s.addresses, get_addresses())
t.deepEqual(s.addresses, getAddresses())

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 @@ -126,7 +125,6 @@ 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 @@ -151,7 +149,6 @@ 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
6 changes: 3 additions & 3 deletions test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var os = require('os')
var test = require('tape')
var Service = require('../lib/service')

var get_addresses_records = function (host) {
var getAddressesRecords = function (host) {
var addresses_records = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
Expand Down Expand Up @@ -77,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)))
].concat(getAddressesRecords(s.host)))
t.end()
})

Expand All @@ -87,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)))
].concat(getAddressesRecords(s.host)))
t.end()
})

0 comments on commit 8be9758

Please sign in to comment.