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 all 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
9 changes: 6 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,6 +35,7 @@ function Browser (mdns, opts, onup) {
this._mdns = mdns
this._onresponse = null
this._serviceMap = {}
this._txt = null

if (!opts || !opts.type) {
this._name = WILDCARD
Expand All @@ -45,6 +46,8 @@ function Browser (mdns, opts, onup) {
this._wildcard = false
}

this._txt = dnsTxt(opts.txt)

this.services = []

if (onup) this.on('up', onup)
Expand Down Expand Up @@ -75,7 +78,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 +130,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