Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary txt option. #10

Merged
merged 3 commits into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var util = require('util')
var EventEmitter = require('events').EventEmitter
var serviceName = require('multicast-dns-service-types')
var dnsEqual = require('dns-equal')
var txt = require('dns-txt')()
var dnsTxt = require('dns-txt')

var TLD = '.local'
var WILDCARD = '_services._dns-sd._udp' + TLD
Expand Down Expand Up @@ -35,14 +35,21 @@ function Browser (mdns, opts, onup) {
this._mdns = mdns
this._onresponse = null
this._serviceMap = {}
this._txt = null

if (!opts || !opts.type) {
this._name = WILDCARD
this._wildcard = true
this._txt = dnsTxt()
} else {
this._name = serviceName.stringify(opts.type, opts.protocol || 'tcp') + TLD
if (opts.name) this._name = opts.name + '.' + this._name
this._wildcard = false
if (!opts.txt || !opts.txt.binary) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the txt property is given, why not just pass it into the dnsTxt constructor as it is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisely!

this._txt = dnsTxt()
} else {
this._txt = dnsTxt({ binary: true })
}
}

this.services = []
Expand Down Expand Up @@ -75,7 +82,7 @@ Browser.prototype.start = function () {
Object.keys(nameMap).forEach(function (name) {
goodbyes(name, packet).forEach(self._removeService.bind(self))

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

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

function buildServicesFor (name, packet) {
function buildServicesFor (name, packet, txt) {
var records = packet.answers.concat(packet.additionals).filter(function (rr) {
return rr.ttl > 0 // ignore goodbye messages
})
Expand Down
16 changes: 16 additions & 0 deletions test/bonjour.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ test('bonjour.find', function (bonjour, t) {
bonjour.publish({ name: 'Baz', type: 'test', port: 3000, txt: { foo: 'bar' } }).on('up', next())
})

test('bonjour.find - binary txt', function (bonjour, t) {
var next = afterAll(function () {
var browser = bonjour.find({ type: 'test', txt: { binary: true } })

browser.on('up', function (s) {
t.equal(s.name, 'Foo')
t.deepEqual(s.txt, { bar: new Buffer('buz') })
t.deepEqual(s.rawTxt, new Buffer('076261723d62757a', 'hex'))
bonjour.destroy()
t.end()
})
})

bonjour.publish({ name: 'Foo', type: 'test', port: 3000, txt: { bar: new Buffer('buz') } }).on('up', next())
})

test('bonjour.find - down event', function (bonjour, t) {
var service = bonjour.publish({ name: 'Foo Bar', type: 'test', port: 3000 })

Expand Down