Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dns-over-http-resolver

vasco-santos398.3kApache-2.0 OR MIT3.0.16TypeScript support: included

DNS over HTTP resolver

dns, doh, http

readme

dns-over-http-resolver

codecov CI

DNS over HTTP resolver

Table of contents

Install

$ npm i dns-over-http-resolver

Browser <script> tag

Loading this module through a script tag will make it's exports available as DnsOverHttpResolver in the global namespace.

<script src="https://unpkg.com/dns-over-http-resolver/dist/index.min.js"></script>

Isomorphic DNS over HTTP resolver using fetch.

API based on Node.js' dns promises API, allowing the native dns module to be used if available when relying on this API.

Usage

const DnsOverHttpResolver = require('dns-over-http-resolver')

const dohResolver = new DnsOverHttpResolver(options)

Cloudflare and Google DNS servers are used by default. They can be replaced via the API.

You can also use require('dns').promises in Node.js in lieu of this module.

options

You can provide the following options for the DnsOverHttpResolver:

Name Type Description Default
maxCache number maximum number of cached dns records 100

API

resolve(hostname, rrType)

Uses the DNS protocol to resolve the given host name into a DNS record.

Parameters

Name Type Description
hostname string host name to resolve
[rrType] string resource record type (default: 'A')

Returns

Type Description
Promise<Array<string>> returns a Promise resolving a DNS record according to its type

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'
const recordType = 'TXT'

const dnsRecord = await resolver.resolve(hostname, recordType)

resolve4(hostname)

Uses the DNS protocol to resolve the given host name into IPv4 addresses.

Parameters

Name Type Description
hostname string host name to resolve

Returns

Type Description
Promise<Array<string>> returns a Promise resolving IPv4 addresses

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolve4(hostname) // ['216.58.212.142']

resolve6(hostname)

Uses the DNS protocol to resolve the given host name into IPv6 addresses.

Parameters

Name Type Description
hostname string host name to resolve

Returns

Type Description
Promise<Array<string>> returns a Promise resolving IPv6 addresses

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolve6(hostname) // ['2a00:1450:4001:801::200e']

resolveTxt(hostname)

Uses the DNS protocol to resolve the given host name into a Text Record.

Parameters

Name Type Description
hostname string host name to resolve

Returns

Type Description
Promise<Array<Array<string>>> returns a Promise resolving a Text Record

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')
const resolver = new DnsOverHttpResolver()

const hostname = 'google.com'

const address = await resolver.resolveTxt(hostname) // [['v=spf1 -all']]

getServers()

Get an array of the IP addresses currently configured for DNS resolution. These addresses are formatted according to RFC 5952. It can include a custom port.

Returns

Type Description
Array<string> returns array of DNS servers used

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')

const resolver = new DnsOverHttpResolver()
const servers = resolver.getServers()

setServers(servers)

Sets the IP address and port of servers to be used when performing DNS resolution. Note that the servers order will be randomized on each request for load distribution.

Parameters

Name Type Description
servers Array<string> Array of RFC 5952 formatted addresses.

Example

const DnsOverHttpResolver = require('dns-over-http-resolver')

const resolver = new DnsOverHttpResolver()
resolver.setServers(['https://cloudflare-dns.com/dns-query'])

Contribute

Feel free to dive in! Open an issue or submit PRs.

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

changelog

3.0.16 (2025-06-17)

Dependencies

  • dev: bump sinon from 20.0.0 to 21.0.0 (#142) (d2ac1ac)

3.0.15 (2025-05-14)

Dependencies

  • dev: bump aegir from 46.0.0 to 46.0.2 (#136) (27acef1)

3.0.14 (2025-05-08)

Dependencies

  • dev: bump aegir from 45.2.1 to 46.0.0 (#134) (ff3426a)

3.0.13 (2025-04-30)

Dependencies

3.0.12 (2025-04-29)

Dependencies

  • dev: bump aegir from 45.1.4 to 45.2.1 (#133) (c7643f3)

3.0.11 (2025-04-28)

Dependencies

  • dev: bump sinon from 19.0.4 to 20.0.0 (#130) (991a774)

3.0.10 (2025-03-24)

Dependencies

  • dev: bump sinon from 19.0.2 to 19.0.4 (#129) (4a66a44)

3.0.9 (2025-03-20)

Dependencies

  • dev: bump aegir from 45.1.3 to 45.1.4 (#128) (be36c2f)

3.0.8 (2025-03-11)

Dependencies

  • dev: bump aegir from 45.1.2 to 45.1.3 (#127) (a5d1eb4)

3.0.7 (2025-02-28)

Dependencies

  • dev: bump aegir from 45.0.9 to 45.1.2 (#126) (34cfb0a)

3.0.6 (2025-02-24)

Dependencies

  • dev: bump @types/sinon from 17.0.3 to 17.0.4 (#125) (3a089a2)

3.0.5 (2025-02-17)

Dependencies

  • dev: bump aegir from 44.1.4 to 45.0.9 (#124) (ae88784)

3.0.4 (2025-02-12)

Dependencies

  • dev: bump sinon from 18.0.1 to 19.0.2 (#118) (a19379f)

3.0.3 (2024-08-02)

Bug Fixes

Dependencies

  • dev: bump aegir from 42.2.11 to 43.0.1 (#114) (bf396fe)
  • dev: bump aegir from 43.0.3 to 44.0.0 (#115) (ff0a161)
  • dev: bump sinon from 17.0.2 to 18.0.0 (#112) (e2da10b)

3.0.2 (2024-01-12)

Dependencies

  • dev: bump aegir from 41.3.5 to 42.1.1 (#111) (8d8d5c6)

3.0.1 (2024-01-10)

Dependencies

  • dev: bump @types/sinon from 10.0.20 to 17.0.3 (#110) (94b4014)

3.0.0 (2023-11-06)

⚠ BREAKING CHANGES

  • requires node 18+

Bug Fixes

2.1.3 (2023-10-26)

Bug Fixes

  • specify correct return type from resolve (#101) (6dd2bd8)

Dependencies

  • dev: bump sinon from 14.0.2 to 17.0.0 (#100) (f946dcb)

2.1.2 (2023-08-14)

Trivial Changes

  • add or force update .github/workflows/js-test-and-release.yml (#98) (7147501)
  • delete templates [skip ci] (#97) (f3f9a24)

2.1.1 (2022-11-23)

Bug Fixes

  • add undici to satisfy peer dep requirements (#78) (cf7a941), closes #44

2.1.0 (2022-07-07)

Features

Trivial Changes

  • deps-dev: bump sinon from 13.0.2 to 14.0.0 (#64) (cb45791)

2.0.2 (2022-07-07)

Trivial Changes

2.0.1 (2021-12-17)

Bug Fixes

2.0.0 (2021-12-14)

Features

BREAKING CHANGES

  • ESM only publishing

1.2.3 (2021-05-11)

1.2.2 (2021-02-22)

1.2.1 (2021-01-18)

1.2.0 (2020-11-09)

1.1.0 (2020-11-05)

Features

1.0.0 (2020-10-13)

Features