Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bonjour-hap

Package Overview
Dependencies
Maintainers
8
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bonjour-hap - npm Package Compare versions

Comparing version 3.5.11 to 3.6.0

.idea/bonjour-hap.iml

8

index.js
'use strict'
var Registry = require('./lib/Registry.js')
var Server = require('./lib/Server.js')
var Browser = require('./lib/Browser.js')
const Registry = require('./lib/Registry.js')
const Server = require('./lib/Server.js')
const Browser = require('./lib/Browser.js')

@@ -28,3 +28,3 @@ function Bonjour (opts) {

findOne: function (opts, cb) {
var browser = new Browser(this._server.mdns, opts)
const browser = new Browser(this._server.mdns, opts)
browser.once('up', function (service) {

@@ -31,0 +31,0 @@ browser.stop()

'use strict'
var util = require('util')
var EventEmitter = require('events').EventEmitter
var serviceName = require('multicast-dns-service-types')
var dnsEqual = require('./utils/dnsEqual')
var dnsTxt = require('./utils/txtDecoder')
const util = require('util')
const EventEmitter = require('events').EventEmitter
const serviceName = require('multicast-dns-service-types')
const dnsEqual = require('./utils/dnsEqual')
const dnsTxt = require('./utils/txtDecoder')
var TLD = '.local'
var WILDCARD = '_services._dns-sd._udp' + TLD
const TLD = '.local'
const WILDCARD = '_services._dns-sd._udp' + TLD

@@ -59,3 +59,3 @@ module.exports = Browser

var self = this
const self = this

@@ -65,3 +65,3 @@ // List of names for the browser to listen for. In a normal search this will

// the names will be determined at runtime as responses come in.
var nameMap = {}
const nameMap = {}
if (!this._wildcard) nameMap[this._name] = true

@@ -83,3 +83,3 @@

// register all new services
var matches = buildServicesFor(name, packet, self._txt, rinfo)
const matches = buildServicesFor(name, packet, self._txt, rinfo)
if (matches.length === 0) return

@@ -116,3 +116,3 @@

Browser.prototype._removeService = function (fqdn) {
var service, index
let service, index
this.services.some(function (s, i) {

@@ -124,2 +124,3 @@ if (dnsEqual(s.fqdn, fqdn)) {

}
return false
})

@@ -152,3 +153,3 @@ if (!service) return

function buildServicesFor (name, packet, txt, referer) {
var records = packet.answers.concat(packet.additionals).filter(function (rr) {
const records = packet.answers.concat(packet.additionals).filter(function (rr) {
return rr.ttl > 0 // ignore goodbye messages

@@ -162,3 +163,3 @@ })

.map(function (ptr) {
var service = {
const service = {
addresses: []

@@ -173,5 +174,5 @@ }

if (rr.type === 'SRV') {
var parts = rr.name.split('.')
var name = parts[0]
var types = serviceName.parse(parts.slice(1, -1).join('.'))
const parts = rr.name.split('.')
const name = parts[0]
const types = serviceName.parse(parts.slice(1, -1).join('.'))
service.name = name

@@ -192,3 +193,3 @@ service.fqdn = rr.name

if (!service.name) return
if (!service.name) return undefined

@@ -195,0 +196,0 @@ records

@@ -14,3 +14,3 @@ const deepEqual = require('deep-equal')

unique: function () {
var set = []
const set = []
return function (obj) {

@@ -17,0 +17,0 @@ if (~set.indexOf(obj)) return false

'use strict'
var dnsEqual = require('./utils/dnsEqual')
const dnsEqual = require('./utils/dnsEqual')

@@ -18,3 +18,3 @@ /**

var Prober = function (mdns, service, cb) {
const Prober = function (mdns, service, cb) {
this.sent = false

@@ -21,0 +21,0 @@ this.retries = 0

'use strict'
var flatten = require('array-flatten')
var Service = require('./Service.js')
var Prober = require('./Prober.js')
const flatten = require('array-flatten')
const Service = require('./Service.js')
const Prober = require('./Prober.js')
var Registry = function (server) {
const Registry = function (server) {
this._server = server

@@ -16,3 +16,3 @@ this._services = []

opts = opts || {}
var service = new Service(opts)
const service = new Service(opts)
service.on('service-publish', this._onServicePublish.bind(this))

@@ -32,3 +32,3 @@ service.on('service-unpublish', this._onServiceUnpublish.bind(this))

destroy: function () {
for (var i = 0; i < this._services.length; i++) { this._services[i].destroy() }
for (let i = 0; i < this._services.length; i++) { this._services[i].destroy() }
},

@@ -50,5 +50,5 @@

var records = flatten.depth(services.map(function (service) {
const records = flatten.depth(services.map(function (service) {
service.deactivate()
var records = service._records()
const records = service._records(true)
records.forEach(function (record) {

@@ -68,3 +68,3 @@ record.ttl = 0 // prepare goodbye message

_onTearDownComplete: function (services, cb) {
for (var i = 0; i < services.length; i++) { services[i].published = false }
for (let i = 0; i < services.length; i++) { services[i].published = false }

@@ -86,9 +86,11 @@ if (cb) { cb.apply(null, Array.prototype.slice.call(arguments, 2)) }

*/
_onAnnounceRequest: function (packet, cb) {
_onAnnounceRequest: function (packet, silent, cb) {
this._server.register(packet)
this._server.mdns.respond(packet, cb)
if (!silent) {
this._server.mdns.respond(packet, cb)
}
},
_onServiceUnpublish: function (service, cb) {
var index = this._services.indexOf(service)
const index = this._services.indexOf(service)

@@ -95,0 +97,0 @@ this._tearDown(service, cb)

'use strict'
var multicastdns = require('multicast-dns')
var dnsEqual = require('./utils/dnsEqual')
var flatten = require('array-flatten')
var helpers = require('./helpers.js')
const multicastdns = require('multicast-dns')
const dnsEqual = require('./utils/dnsEqual')
const flatten = require('array-flatten')
const helpers = require('./helpers.js')
var Server = function (opts) {
const Server = function (opts) {
this.mdns = multicastdns(opts)

@@ -17,10 +17,10 @@ this.mdns.setMaxListeners(0)

_respondToQuery: function (query) {
for (var i = 0; i < query.questions.length; i++) {
var question = query.questions[i]
for (let i = 0; i < query.questions.length; i++) {
const question = query.questions[i]
var type = question.type
var name = question.name
const type = question.type
const name = question.name
// generate the answers section
var answers = type === 'ANY'
const answers = type === 'ANY'
? flatten.depth(Object.keys(this.registry).map(this._recordsFor.bind(this, name)), 1)

@@ -32,3 +32,3 @@ : this._recordsFor(name, type)

// generate the additionals section
var additionals = []
let additionals = []
if (type !== 'ANY') {

@@ -71,7 +71,11 @@ answers.forEach(answer => {

for (var i = 0; i < records.length; i++) {
var record = records[i]
var subRegistry = this.registry[record.type]
for (let i = 0; i < records.length; i++) {
const record = records[i]
let subRegistry = this.registry[record.type]
if (!subRegistry) { subRegistry = this.registry[record.type] = [] } else if (subRegistry.some(helpers.isDuplicateRecord(record))) { return }
if (!subRegistry) {
subRegistry = this.registry[record.type] = []
} else if (subRegistry.some(helpers.isDuplicateRecord(record))) {
continue
}

@@ -85,7 +89,7 @@ subRegistry.push(record)

for (var i = 0; i < records.length; i++) {
var record = records[i]
var type = record.type
for (let i = 0; i < records.length; i++) {
const record = records[i]
const type = record.type
if (!(type in this.registry)) { return }
if (!(type in this.registry)) { continue }

@@ -102,3 +106,3 @@ this.registry[type] = this.registry[type].filter(r => {

return this.registry[type].filter(record => {
var recordName = ~name.indexOf('.') ? record.name : record.name.split('.')[0]
const recordName = ~name.indexOf('.') ? record.name : record.name.split('.')[0]
return dnsEqual(recordName, name)

@@ -105,0 +109,0 @@ })

'use strict'
var os = require('os')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var serviceName = require('multicast-dns-service-types')
const os = require('os')
const util = require('util')
const net = require('net')
const assert = require('assert')
const EventEmitter = require('events').EventEmitter
const serviceName = require('multicast-dns-service-types')
const network = require('./utils/network')
var TLD = '.local'
var REANNOUNCE_MAX_MS = 60 * 60 * 1000
var REANNOUNCE_FACTOR = 3
const TLD = '.local'
const REANNOUNCE_MAX_MS = 60 * 60 * 1000
const REANNOUNCE_FACTOR = 3
var Service = function (opts) {
const Service = function (opts) {
if (!opts.name) throw new Error('Required name not given')

@@ -28,2 +31,40 @@ if (!opts.type) throw new Error('Required type not given')

// adds the meta query to the records array
// this option can only be turned on if only one service is advertised on the responder
// otherwise it will break when one service is removed from the network
this.addUnsafeServiceEnumerationRecord = opts.addUnsafeServiceEnumerationRecord || false
this.restrictedAddresses = undefined
if (opts.restrictedAddresses) {
assert(opts.restrictedAddresses.length, "The service property 'restrictedAddresses' cannot be an empty array!")
this.restrictedAddresses = new Map()
for (const entry of opts.restrictedAddresses) {
if (net.isIP(entry)) {
if (entry === '0.0.0.0' || entry === '::') {
throw new Error(`[${this.fqdn}] Unspecified ip address (${entry}) cannot be used to restrict on to!`)
}
const interfaceName = network.resolveInterface(entry)
if (!interfaceName) {
throw new Error(`[${this.fqdn}] Could not restrict service to address ${entry} as we could not resolve it to an interface name!`)
}
const current = this.restrictedAddresses.get(interfaceName)
if (current) {
// empty interface signals "catch all" was already configured for this
if (current.length && !current.includes(entry)) {
current.push(entry)
}
} else {
this.restrictedAddresses.set(interfaceName, [entry])
}
} else {
this.restrictedAddresses.set(entry, []) // empty array signals "use all addresses for interface"
}
}
}
this.disabledIpv6 = opts.disabledIpv6 || false
this._activated = false // indicates intent - true: starting/started, false: stopping/stopped

@@ -34,3 +75,3 @@ }

var proto = {
const proto = {

@@ -51,3 +92,3 @@ start: function () {

updateTxt: function (txt) {
updateTxt: function (txt, silent) {
if (this.packet) { this.emit('service-packet-change', this.packet, this.onAnnounceComplete.bind(this)) }

@@ -60,6 +101,6 @@ this.packet = null

this._unpublish()
this.announce()
this.announce(silent)
},
announce: function () {
announce: function (silent) {
if (this._destroyed) { return }

@@ -72,3 +113,3 @@

this.delay = 1000
this.emit('service-announce-request', this.packet, this.onAnnounceComplete.bind(this))
this.emit('service-announce-request', this.packet, silent || false, this.onAnnounceComplete.bind(this))
},

@@ -109,7 +150,11 @@

_records: function () {
var records = [this._rrPtr(), this._rrSrv(), this._rrTxt()]
_records: function (teardown) {
const records = [this._rrPtr(), this._rrSrv(), this._rrTxt()]
records.push(...this._addressRecords())
if (!teardown && this.addUnsafeServiceEnumerationRecord) {
records.push(this._rrMetaPtr())
}
return records

@@ -120,5 +165,14 @@ },

const records = []
const addresses = []
const addresses = []
Object.values(os.networkInterfaces()).forEach(interfaces => {
Object.entries(os.networkInterfaces()).forEach(([name, interfaces]) => {
let restrictedAddresses = this.restrictedAddresses ? this.restrictedAddresses.get(name) : undefined
if (this.restrictedAddresses && !restrictedAddresses) {
return
}
if (restrictedAddresses && restrictedAddresses.length === 0) {
restrictedAddresses = undefined
}
interfaces.forEach(iface => {

@@ -129,8 +183,12 @@ if (iface.internal || addresses.includes(iface.address)) {

addresses.push(iface.address)
if (restrictedAddresses && restrictedAddresses.includes(iface.address)) {
return
}
if (iface.family === 'IPv4') {
records.push(this._rrA(iface.address))
} else {
addresses.push(iface.address)
} else if (!this.disabledIpv6) {
records.push(this._rrAaaa(iface.address))
addresses.push(iface.address)
}

@@ -143,2 +201,11 @@ })

_rrMetaPtr: function () {
return {
name: '_services._dns-sd._udp.local',
type: 'PTR',
ttl: 4500,
data: this.type + TLD
}
},
_rrPtr: function () {

@@ -148,3 +215,3 @@ return {

type: 'PTR',
ttl: 28800,
ttl: 4500,
data: this.fqdn

@@ -159,2 +226,3 @@ }

ttl: 120,
flush: true,
data: {

@@ -168,8 +236,8 @@ port: this.port,

_rrTxt: function () {
var data = []
const data = []
if (this.txt) {
var txtRecords = this.txt
var keys = Object.keys(txtRecords)
const txtRecords = this.txt
const keys = Object.keys(txtRecords)
keys.forEach((key) => {
var val = txtRecords[key]
const val = txtRecords[key]
data.push(key + '=' + val)

@@ -181,3 +249,4 @@ })

type: 'TXT',
ttl: 120,
ttl: 4500,
flush: true,
data: data

@@ -192,2 +261,3 @@ }

ttl: 120,
flush: true,
data: ip

@@ -202,2 +272,3 @@ }

ttl: 120,
flush: true,
data: ip

@@ -209,4 +280,4 @@ }

for (var x in proto) { Service.prototype[x] = proto[x] }
for (const x in proto) { Service.prototype[x] = proto[x] }
module.exports = Service
{
"name": "bonjour-hap",
"version": "3.5.11",
"version": "3.6.0",
"description": "A Bonjour/Zeroconf implementation in pure JavaScript (for HAP)",

@@ -37,3 +37,3 @@ "main": "index.js",

"array-flatten": "^2.1.2",
"deep-equal": "^2.0.2",
"deep-equal": "^2.0.5",
"multicast-dns": "^7.2.2",

@@ -45,7 +45,7 @@ "multicast-dns-service-types": "^1.1.0",

"after-all": "^2.0.2",
"standard": "^14.3.3",
"tape": "^4.5.1",
"nyc": "^15.0.1",
"standard": "^16.0.3",
"tape": "^4.13.3",
"nyc": "^15.1.0",
"rimraf": "^3.0.2"
}
}

@@ -12,3 +12,3 @@ # bonjour-hap

##Notice
## Notice

@@ -15,0 +15,0 @@ The `bonjour-hap` library was used in [HAP-NodeJS](https://github.com/homebridge/HAP-NodeJS) in versions

'use strict'
var os = require('os')
var dgram = require('dgram')
var tape = require('tape')
var afterAll = require('after-all')
var Service = require('../lib/Service.js')
var Bonjour = require('../')
const os = require('os')
const dgram = require('dgram')
const tape = require('tape')
const afterAll = require('after-all')
const Service = require('../lib/Service.js')
const Bonjour = require('../')
var getAddresses = function () {
const getAddresses = function () {
const addresses = []

@@ -26,6 +26,6 @@

var port = function (cb) {
var s = dgram.createSocket('udp4')
const port = function (cb) {
const s = dgram.createSocket('udp4')
s.bind(0, function () {
var port = s.address().port
const port = s.address().port
s.on('close', function () {

@@ -38,3 +38,3 @@ cb(port)

var test = function (name, fn) {
const test = function (name, fn) {
tape(name, function (t) {

@@ -48,3 +48,3 @@ port(function (p) {

test('bonjour.publish', function (bonjour, t) {
var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
const service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
t.ok(service instanceof Service)

@@ -61,3 +61,3 @@ t.equal(service.published, false)

t.test('published services', function (t) {
var service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
const service = bonjour.publish({ name: 'foo', type: 'bar', port: 3000 })
service.on('up', function () {

@@ -82,5 +82,5 @@ bonjour.unpublishAll(function (err) {

test('bonjour.find', function (bonjour, t) {
var next = afterAll(function () {
var browser = bonjour.find({ type: 'test' })
var ups = 0
const next = afterAll(function () {
const browser = bonjour.find({ type: 'test' })
let ups = 0

@@ -127,5 +127,5 @@ browser.on('up', function (s) {

test('bonjour.change', function (bonjour, t) {
var data = { init: true, found: false, timer: null }
var service = bonjour.publish({ name: 'Baz', type: 'test', port: 3000, txt: { foo: 'bar' } }).on('up', function () {
var browser = bonjour.find({ type: 'test' })
const data = { init: true, found: false, timer: null }
const service = bonjour.publish({ name: 'Baz', type: 'test', port: 3000, txt: { foo: 'bar' } }).on('up', function () {
const browser = bonjour.find({ type: 'test' })
browser.on('up', function (s) {

@@ -157,4 +157,4 @@ data.browserData = s

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

@@ -174,6 +174,6 @@ browser.on('up', function (s) {

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

@@ -194,3 +194,3 @@ browser.on('up', function (s) {

test('bonjour.findOne - callback', function (bonjour, t) {
var next = afterAll(function () {
const next = afterAll(function () {
bonjour.findOne({ type: 'test' }, function (s) {

@@ -208,4 +208,4 @@ t.equal(s.name, 'Callback')

test('bonjour.findOne - emitter', function (bonjour, t) {
var next = afterAll(function () {
var browser = bonjour.findOne({ type: 'test' })
const next = afterAll(function () {
const browser = bonjour.findOne({ type: 'test' })
browser.on('up', function (s) {

@@ -212,0 +212,0 @@ t.equal(s.name, 'Emitter')

'use strict'
var os = require('os')
const os = require('os')
const ip = require('ip')
var test = require('tape')
var Service = require('../lib/Service.js')
const test = require('tape')
const Service = require('../lib/Service.js')
var getAddressesRecords = function (host) {
const getAddressesRecords = function (host) {
const records = []

@@ -23,3 +23,3 @@

addresses.forEach(address => {
records.push({ data: address, name: host, ttl: 120, type: ip.isV4Format(address) ? 'A' : 'AAAA' })
records.push({ data: address, name: host, ttl: 120, flush: true, type: ip.isV4Format(address) ? 'A' : 'AAAA' })
})

@@ -52,3 +52,3 @@

test('minimal', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000 })
const s = new Service({ name: 'Foo Bar', type: 'http', port: 3000 })
t.equal(s.name, 'Foo Bar')

@@ -67,3 +67,3 @@ t.equal(s.protocol, 'tcp')

test('protocol', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, protocol: 'udp' })
const s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, protocol: 'udp' })
t.deepEqual(s.protocol, 'udp')

@@ -74,3 +74,3 @@ t.end()

test('host', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, host: 'example.com' })
const s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, host: 'example.com' })
t.deepEqual(s.host, 'example.com')

@@ -81,3 +81,3 @@ t.end()

test('txt', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, txt: { foo: 'bar' } })
const s = new Service({ name: 'Foo Bar', type: 'http', port: 3000, txt: { foo: 'bar' } })
t.deepEqual(s.txt, { foo: 'bar' })

@@ -88,7 +88,7 @@ t.end()

test('_records() - minimal', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000 })
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: [], name: s.fqdn, ttl: 120, type: 'TXT' }
const s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000 })
t.deepEqual(s._records(true), [
{ data: s.fqdn, name: '_http._tcp.local', ttl: 4500, type: 'PTR' },
{ data: { port: 3000, target: os.hostname() }, name: s.fqdn, ttl: 120, flush: true, type: 'SRV' },
{ data: [], name: s.fqdn, ttl: 4500, flush: true, type: 'TXT' }
].concat(getAddressesRecords(s.host)))

@@ -99,9 +99,11 @@ t.end()

test('_records() - everything', function (t) {
var s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000, host: 'example.com', txt: { foo: 'bar' } })
const s = new Service({ name: 'Foo Bar', type: 'http', protocol: 'tcp', port: 3000, host: 'example.com', txt: { foo: 'bar' }, addUnsafeServiceEnumerationRecord: true })
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: ['foo=bar'], name: s.fqdn, ttl: 120, type: 'TXT' }
].concat(getAddressesRecords(s.host)))
{ data: s.fqdn, name: '_http._tcp.local', ttl: 4500, type: 'PTR' },
{ data: { port: 3000, target: 'example.com' }, name: s.fqdn, ttl: 120, flush: true, type: 'SRV' },
{ data: ['foo=bar'], name: s.fqdn, ttl: 4500, flush: true, type: 'TXT' }
].concat(getAddressesRecords(s.host)).concat([
{ data: '_http._tcp.local', ttl: 4500, type: 'PTR', name: '_services._dns-sd._udp.local' }
]))
t.end()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc