Skip to content

Commit

Permalink
Merge branch 'add-binary-txt' of https://github.com/kkushimoto/bonjour
Browse files Browse the repository at this point in the history
…into kkushimoto-add-binary-txt
  • Loading branch information
watson committed May 6, 2016
2 parents 6f8e6be + 8293e61 commit fda2f38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 @@ -131,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 @@ -112,6 +112,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

0 comments on commit fda2f38

Please sign in to comment.