Skip to content

Commit

Permalink
Replaced dns-txt with first party handler and upgraded multicast-dns@…
Browse files Browse the repository at this point in the history
…7.2.3

This commit resolves the issue with multicast-dns@6.2.3
#2
  • Loading branch information
mdidon committed May 27, 2021
1 parent d8fbb24 commit c50a7a9
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 196 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"dependencies": {
"array-flatten": "^2.1.2",
"dns-equal": "^1.0.0",
"dns-txt": "^2.0.2",
"fast-deep-equal": "^3.1.3",
"multicast-dns": "^6.2.3"
"multicast-dns": "^7.2.3"
},
"devDependencies": {
"@types/node": "^14.14.35",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Bonjour {
* @param onup Callback when up event received
* @returns
*/
public find(opts: BrowserConfig, onup?: (...args: any[]) => void): Browser {
public find(opts: BrowserConfig | undefined = undefined, onup?: (...args: any[]) => void): Browser {
return new Browser(this.server.mdns, opts, onup)
}

Expand All @@ -53,7 +53,7 @@ export class Bonjour {
* @param callback Callback when device found
* @returns
*/
public findOne(opts: BrowserConfig, timeout = 10000, callback: CallableFunction): Browser {
public findOne(opts: BrowserConfig | undefined = undefined, timeout = 10000, callback: CallableFunction): Browser {
const browser: Browser = new Browser(this.server.mdns, opts)
var timer: any
browser.once('up', (service: Service) => {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/browser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import EventEmitter from 'events'
import Service, { ServiceRecord } from './service'
import { toString as ServiceToString, toType as ServiceToType } from './service-types'
import DnsTxt from './dns-txt'

const dnsEqual = require('dns-equal')
const dnsTxt = require('dns-txt')

const TLD = '.local'
const WILDCARD = '_services._dns-sd._udp' + TLD
Expand Down Expand Up @@ -51,7 +51,9 @@ export class Browser extends EventEmitter {
this.mdns = mdns

if(opts != null && opts.txt != null) {
this.txt = dnsTxt(opts.txt)
this.txt = new DnsTxt(opts.txt)
} else {
this.txt = new DnsTxt()
}

if (!opts || !opts.type) {
Expand Down Expand Up @@ -156,7 +158,6 @@ export class Browser extends EventEmitter {

private buildServicesFor(name: string, packet: any, txt: any, referer: any) {
var records = packet.answers.concat(packet.additionals).filter( (rr: ServiceRecord) => rr.ttl > 0) // ignore goodbye messages


return records
.filter((rr: ServiceRecord) => rr.type === 'PTR' && dnsEqual(rr.name, name))
Expand Down Expand Up @@ -184,7 +185,7 @@ export class Browser extends EventEmitter {
service.subtypes = types.subtypes
} else if (rr.type === 'TXT') {
service.rawTxt = rr.data
service.txt = dnsTxt().decode(rr.data)
service.txt = this.txt.decodeAll(rr.data)
}
})

Expand Down
65 changes: 65 additions & 0 deletions src/lib/dns-txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

type KeyValue = { [key: string]: any }

export class DnsTxt {

private binary: boolean

constructor(opts: KeyValue = {}) {
this.binary = opts ? opts.binary : false
}

/**
* Encode the KeyValue to buffer
* @param data
* @returns
*/
public encode(data: KeyValue = {}) {
return Object.entries(data)
.map(([key, value]) => {
let item: string = `${key}=${value}`
return Buffer.from(item)
})
}

/**
* Decode the buffer to KeyValue
* @param buffer
* @returns
*/
public decode(buffer: Buffer): KeyValue {
var data: KeyValue = {}
// Format buffer to KeyValue
try {
let format : string = buffer.toString()
let parts : Array<any> = format.split(/=(.+)/)
let key : string = parts[0]
let value : any = parts[1]
data[key] = value
} catch(_) {}
// Return data a KeyValue
return data
}

/**
* Decode all buffer items to KeyValye
* @param buffer
* @returns
*/
public decodeAll(buffer: Array<Buffer>) {
return buffer
.filter(i => i.length > 1) //i.toString() != ''
.map(i => this.decode(i))
.reduce((prev, curr) => {
var obj = prev
let [key] = Object.keys(curr)
let [value] = Object.values(curr)
obj[key] = value
return obj
}, {})
}

}

export default DnsTxt
9 changes: 6 additions & 3 deletions src/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os from 'os'
import { EventEmitter } from 'events'
import { toString as ServiceToString } from './service-types'

const txt = require('dns-txt')()
import DnsTxt from './dns-txt'

const TLD: string = '.local'

Expand Down Expand Up @@ -59,9 +58,13 @@ export class Service extends EventEmitter {
public start? : any
public stop? : any

private txtService : DnsTxt

constructor(config: ServiceConfig) {
super()

this.txtService = new DnsTxt()

this.name = config.name
this.protocol = config.protocol || 'tcp'
this.type = ServiceToString({ name: config.type, protocol: this.protocol })
Expand Down Expand Up @@ -138,7 +141,7 @@ export class Service extends EventEmitter {
name : service.fqdn,
type : 'TXT',
ttl : 4500,
data : txt.encode(service.txt)
data : this.txtService.encode(service.txt)
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var os = require('os')
var test = require('tape')
var Service = require('../dist/lib/service')
var { Service } = require('../dist/lib/service')

var getAddressesRecords = function (host) {
var records = []
Expand Down Expand Up @@ -73,7 +73,7 @@ test('txt', function (t) {

test('_records() - minimal', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000 })
t.deepEqual(s._records(), [
t.deepEqual(s.records(), [
{ 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: Buffer.from('00', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
Expand All @@ -83,7 +83,7 @@ test('_records() - minimal', function (t) {

test('_records() - everything', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000, host: 'example.com', txt: { foo: 'bar' } })
t.deepEqual(s._records(), [
t.deepEqual(s.records(), [
{ 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: Buffer.from('07666f6f3d626172', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
Expand Down
Loading

0 comments on commit c50a7a9

Please sign in to comment.