npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gravitysoftware/dnssd

v0.5.3

Published

Bonjour/Avahi-like service discovery in pure JavaScript

Downloads

393

Readme

dnssd.js

Bonjour/Avahi-like service discovery in pure JavaScript

Build Status Coverage Status

dnssd lets you find (and advertise) services on your network like chromecasts, printers, and airplay speakers.

Features

  • [x] Compliant with standards (RFC 6762 & RFC 6763) ✔
  • [x] Won't interfere with existing Bonjour/Avahi installs
  • [x] Handles sleep/hibernate/wakes 💤 without flooding your network
  • [x] Dependency-less
npm install dnssd

Usage

const dnssd = require('dnssd');

// advertise a http server on port 4321
const ad = new dnssd.Advertisement(dnssd.tcp('http'), 4321);
ad.start();

// find all chromecasts
const browser = dnssd.Browser(dnssd.tcp('googlecast'))
  .on('serviceUp', service => console.log("Device up: ", service))
  .on('serviceDown', service => console.log("Device down: ", service))
  .start();

Documentation

dnssd aims to have a API compatible with the mdns package + some extras.

new dnssd.Advertisement(serviceType, port [, options])

// advertising a http server on port 4321:
const ad = new dnssd.Advertisement(dnssd.tcp('http'), 4321);
ad.start();

options.name - instance name
options.host - hostname to use
options.txt - TXT record
options.subtypes - subtypes to register
options.interface - interface name or address to use ('eth0' or '1.2.3.4')

.start()

Starts the advertisement.
If there is a conflict with the instance name it will automatically get renamed. (Name -> Name (2))

.stop([forceImmediately [, callback]])

Stops the advertisement.
Can do either a clean stop or a forced stop. A clean stop will send goodbye records out so others will know the service is going down. This takes ~1s. Forced goodbyes shut everything down immediately.

.on(event, listener)

error
stopped when the advertisement is stopped
instanceRenamed when the service instance has to be renamed
hostRenamed when the hostname has to be renamed

.updateTXT(txt)

Updates the advertisements TXT record

new dnssd.Browser(serviceType [, options])

// find all chromecasts
const browser = dnssd.Browser(dnssd.tcp('googlecast'))
  .on('serviceUp', service => console.log("Device up: ", service))
  .on('serviceDown', service => console.log("Device down: ", service))
  .start();

A resolved service looks like:

service = {
  fullname: 'InstanceName._googlecast._tcp.local.',
  name: 'InstanceName',
  type: { name: 'googlecast', protocol: 'tcp' },
  domain: 'local',
  host: 'Hostname.local.',
  port: 8009,
  addresses: ['192.168.1.15'],
  txt: { id: 'strings' },
  txtRaw: { id: <Buffer XX XX XX... >},
};

Browser search is a multi-step process. First it finds an instance name, then it resolves all the necessary properties of the service, like the address and the port. It keeps that data up to date by sending more queries out as needed. If you want less steps, there's some options:

options.maintain: Set to false if don't want to maintain a service's info. This will give you a 'serviceUp' event but no 'serviceDown' or 'serviceUpdated'

options.resolve: Set to false if you only want the instance name and nothing else.

options.interface: Sets the interface to use ('eth0' or '1.2.3.4')

.start()

Starts the browser.

.stop()

Stops the browser.

.on(event, listener)

error
serviceUp when a new service is found
serviceChanged when a service's data has changed
serviceDown when a service goes down

.list()

Lists all current services that have been found.

new dnssd.ServiceType(...args)

Used to turn some input into a reliable service type for advertisements and browsers. Name and protocol are always required, subtypes are optional. Multiple forms available:

String (single argument)

'_http._tcp'
'_http._tcp,mysubtype,anothersub'

Object (single argument)

{
  name:     '_http',
  protocol: '_tcp',
  subtypes: ['mysubtype', 'anothersub'],
}

Array (single argument)

['_http', '_tcp', ['mysubtype', 'anothersub']]
['_http', '_tcp', 'mysubtype', 'anothersub']

Strings (multiple arguments)

'_http', '_tcp'
'_http', '_tcp', 'mysubtype', 'anothersub'

dnssd.tcp(...args)

Creates a new ServiceType with tcp protocol

ServiceType.tcp('_http')
ServiceType.tcp('_http', 'sub1', 'sub2')
ServiceType.tcp(['_http', 'sub1', 'sub2'])

dnssd.udp(...args)

Creates a new ServiceType with udp protocol

new ServiceType('_services._dns-sd._udp');

dnssd.all()

// browse all the things
const browser = dnssd.Browser(dnssd.all())

dnssd.resolve(name, type [, options])

Async functions for resolving specific records / record types. Returns a promise with result.

dnssd.resolve(name, rrtype).then(function(result) {})
result = {
    answer: {}
    related: [{}, {}]
}

dnssd.resolveA(name [, options])

dnssd.resolveA('something.local.').then((address) => {
  address === '192.168.1.10'
});

dnssd.resolveAAAA(name [, options])

dnssd.resolveAAAA('computer.local.').then((address) => {
  address === '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
});

dnssd.resolveSRV(name [, options])

dnssd.resolveSRV(name).then((srv) => {
  srv === {
      target: 'machine.local.',
      port: 8000,
  }
});

dnssd.resolveTXT(name [, options])

dnssd.resolveTXT(name).then((txt) => {
  txt === { some: 'thing' }
});

dnssd.resolveService(name [, options])

dnssd.resolveService(name).then((service) => {
  service === like the browser results
});

Validations

Service type names and TXT records have some restrictions:

serviceNames:
* must start with an underscore _
* less than 16 chars including the leading _
* must start with a letter or digit
* only letters / digits / hyphens (but not consecutively: --)

TXT records
* Keys <= 9 chars
* Keys must be ascii and can't use '='
* Values must be a string, buffer, number, or boolean
* Each key/value pair must be < 255 bytes
* Total TXT object is < 1300 bytes

License

The MIT License (MIT)

Copyright (c) Sterling DeMille

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.