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

fetch-dns-lookup

v3.0.6

Published

An implementation to speed up the nodejs `dns.lookup` method by avoiding thread pool and using tangerine library

Downloads

15

Readme

lookup-dns-cache - DNS cache to replace NodeJS dns.lookup standard method

Super simple to use

import fetch from 'node-fetch';
import { lookup } from 'fetch-dns-lookup';

// With "node-fetch" module
const staticDnsAgent = () => new https.Agent({
    lookup: lookup,
    // keep alive defaults to true for node >=18, this setting is for 16
    keepAlive: true,
});

const response = await fetch(uri, {
    method,
    agent: staticDnsAgent,
});

// Direct usage

lookup('google.com', {}, (error, address, family) => {
    // ...
});

Table of contents


Motivation

The main idea behind this package is eliminate NodeJS event loop usage when you do network request. See NodeJS DNS implementation to understand the problem with dns.lookup.

Valuable reads:

  • https://httptoolkit.com/blog/configuring-nodejs-dns/
  • https://www.npmjs.com/package/tangerine

back to top


How to use

This module supports almost the same params as dns.lookup does. Concretely, you can pass options object as a second param, and set:

  • family to 4 or 6
  • all flag to true/false if you want/don't want get all IP addresses at once.

Because this implementation does not use getaddrinfo method, the hints param is not supported.

The verbatim param is not supported for now. If you will not specify any family you will get IPv4 addresses first and IPv6 addresses second.

The callback function works the same way as a standard method.

The error object would have all fields the standard implementation's error object has.

NodeJS dns.lookup:

> const dns = require('dns');
> dns.lookup('host-doesnot-support-ipv6', {family: 6}, console.log)

> { Error: getaddrinfo ENOTFOUND vss-cc-ha.hwtool.net
    at errnoException (dns.js:55:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'vss-cc-ha.hwtool.net' }

lookup-dns-cache

> const {lookup} = require('lookup-dns-cache');
> lookup('host-doesnot-support-ipv6', {family: 6}, console.log)

> { Error: queryAaaa ENOTFOUND vss-cc-ha.hwtool.net
      at makeNotFoundError (/path/lookup-dns-cache/src/Lookup.js:182:19)
      at ipv6AddressesTable.resolve (/path/lookup-dns-cache/src/Lookup.js:147:37)
      at Immediate.setImmediate [as _onImmediate] (/path/lookup-dns-cache/src/IpAddressesTable.js:70:48)
      at runCallback (timers.js:773:18)
      at tryOnImmediate (timers.js:734:5)
      at processImmediate [as _immediateCallback] (timers.js:711:5)
    hostname: 'vss-cc-ha.hwtool.net',
    syscall: 'queryAaaa',
    code: 'ENOTFOUND',
    errno: 'ENOTFOUND' }

If you are looking for IPv4 addresses only, explicitly specify param {family: 4}. In that case, you will avoid spending time on useless searching for IPv6. Apply the same technique if you are looking for IPv6 addresses only.

Under the hood, lookup method has Round-robin algorithm. It means that if particular hostname resolves to several addresses it will return new address every time you call that function. For example:

// hostname: example.com
// resolves to: [1.2.3.4, 5.6.7.8, 9.10.11.12]

lookup('example.com', {family: 4}, (error, address, family) => {
   // address === "1.2.3.4"
   // family === 4
});

lookup('example.com', {family: 4}, (error, address, family) => {
   // address === "5.6.7.8"
   // family === 4
});

lookup('example.com', {family: 4}, (error, address, family) => {
   // address === "9.10.11.12"
   // family === 4
});

lookup('example.com', {family: 4}, (error, address, family) => {
   // address === "1.2.3.4"
   // family === 4
});

back to top


Implementation

Under the hood, this package uses dns.resolve4 and dns.resolve6 methods with {ttl: true} param. It caches addresses for that particular hostname for DNS TTL time and returns one address if you specified {all: false} (default value) and array of addresses if {all: true}.

If you didn't specify family type ({family: 4} or {family: 6}) the method searches for addresses of {family: 4} and {family: 6} in parallel. After that, if you specified {all: true} it returns an array in form [[...IPv4],[...IPv6]], in other case it returns IPv4 or IPv6 address. (IPv4 has more priority).

back to top


Examples

lookup('hostname', {all: true}, (error, results) => {
   // results is an array that contains both IPv4 and IPv6 addresses (Ipv4 first).
   //
   // error - null
   // results - [ 
   //   { address: '1.2.3.4', family: 4 },
   //   { address: '5.6.7.8', family: 4 } 
   // ]
});
lookup('hostname', {all: false}, (error, address, family) => {
   // address and family of the first resolved IP (IPv4 or IPv6 if supported).
   // error - null
   // address - '1.2.3.4'
   // family - 4
});
lookup('hostname', {all: false, family: 4}, (error, address, family) => {
   // address and family of the first resolved IP (IPv4 only). 
});
lookup('hostname', {all: false, family: 6}, (error, address, family) => {
   // address and family of the first resolved IP (IPv6 only).
   // will return an error if IPv6 is not supported. See NodeJS dns.lookup doc.
});

back to top


Similar packages

Yahoo tried to solve this problem in own way https://github.com/yahoo/dnscache.

The big disadvantages if this package are:

  • monkey patching dns module
  • does not support DNS TTL
  • cache just one IP address and use it for every request (no advantage of round-robin if you have dns resolver that returns several addresses)

back to top