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

srv-discovery

v0.0.1

Published

A small DNS SRV record based service-discovery helper for Node.js.

Downloads

59

Readme

srv-discovery

A small DNS SRV record based service-discovery helper for Node.js.

Most basic usage example:

const request = require('request-promise-native');
const { resolve } = require('srv-discovery');

// Find an hostname and port where the service is listening and then make a
// HTTP request to it:
resolve('_myservice._tcp.example.com')
	.then(addr => request(`http://${addr}/path/to/123`))
	.then(res => console.log(res));

Calling resolve() the first time performs a DNS query. After that answer is cached for as long as the TTL field allows. Note that while calling resolve() past TTL triggers a new DNS query resolve() might return a cache address without waiting for the DNS answer. This behavior can be adjusted using the maxStale option.

Advanced usage

It is possible to configure the behavior of the resolve() function by creating a custom Agent:

const { Agent } = require('srv-discovery');
const agent = new Agent({
	// Number of seconds, how long past TTL are the cached addresses still
	// good for.
	maxStale: 120,

	// Maximum number of seconds to wait after repeating a failed DNS
	// query.
	maxRetryWait: 30,

	// Maximum number of milliseconds to wait for a DNS answer before
	// timing out.
	resolveTimeoutMs: 1200,

	// List of custom name servers to use. By default the system's
	// nameservers are used.
	nameservers: ['8.8.8.8', '8.8.4.4:53'],
});

// Resolve addresses using the agent
agent.resolve(...);

It is also possible to give feedback when the resolved address turns out to be a bad one:

agent.resolveFor('_myservice._tcp.example.com', (addr) => {
	return request(`http://${addr}/path/to/123`);
}).then((res) => {
	console.log('response: ', res);
});

In the above example, if the request library rejects with an ECONNREFUSED error then the agent will see it and know not to return that address from future resolve() calls. The address will return back to circulation either when (a) new addresses are received via DNS, or (b) all addresses have been marked as "bad".

The behavior of resolveFor can be adjusted using various options:

const opts = {
	// Maximum number of times to repeat operations that failed because of
	a bad address. By default this is set to 0.
	maxRetries: 1,

	// The function accepts an error as its argument and return whether
	// that error was caused by a bad address.
	test: (err) => err.name === 'ECONNREFUSED',
};

agent.resolveFor('_myservice._tcp.example.com', opts, (addr) => {
	return request(`http://${addr}/path/to/123`);
});

Known issues / features:

  • The SRV "weight" and "priority" parameters are not considered.