Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resolve): allow configurable dnsServers #341

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type MultiaddrInput = string | Multiaddr | Uint8Array | null
* A Resolver is a function that takes a {@link Multiaddr} and resolves it into one
* or more string representations of that {@link Multiaddr}.
*/
export interface Resolver { (addr: Multiaddr, options?: AbortOptions): Promise<string[]> }
export interface Resolver { (addr: Multiaddr, options?: AbortOptions & ResolverOptions): Promise<string[]> }

/**
* A code/value pair
Expand All @@ -87,6 +87,20 @@ export interface AbortOptions {
signal?: AbortSignal
}

/**
* Options for DNS resolvers.
* dns-over-http-resolver in the browser and 'node:dns' in Node.js
*/
export interface ResolverOptions {
/**
* DNS servers to use for resolution.
*
* In the browser, this is passed to dns-over-http-resolver, so the servers should be a DoH compatible url such as https://cloudflare-dns.com/dns-query
* In Node.js, this is passed to dns.resolve, so the servers should be an IP address or hostname (with optional port) such as '9.9.9.9'
Comment on lines +98 to +99
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* In the browser, this is passed to dns-over-http-resolver, so the servers should be a DoH compatible url such as https://cloudflare-dns.com/dns-query
* In Node.js, this is passed to dns.resolve, so the servers should be an IP address or hostname (with optional port) such as '9.9.9.9'
* In the browser, this is passed to [dns-over-http-resolver](https://github.com/vasco-santos/dns-over-http-resolver#setserversservers), so the servers should be a DoH compatible url such as https://cloudflare-dns.com/dns-query
* In Node.js, this is passed to [dns.setServers](https://nodejs.org/api/dns.html#dnspromisessetserversservers), so the servers should be an IP address or hostname (with optional port) such as '9.9.9.9'

*/
dnsServers?: string[]
}

/**
* All configured {@link Resolver}s
*/
Expand Down Expand Up @@ -351,7 +365,7 @@ export interface Multiaddr {
* // ]
* ```
*/
resolve: (options?: AbortOptions) => Promise<Multiaddr[]>
resolve: (options?: AbortOptions & ResolverOptions) => Promise<Multiaddr[]>

/**
* Gets a Multiaddrs node-friendly address object. Note that protocol information
Expand Down Expand Up @@ -678,7 +692,7 @@ class DefaultMultiaddr implements Multiaddr {
return uint8ArrayEquals(this.bytes, addr.bytes)
}

async resolve (options?: AbortOptions): Promise<Multiaddr[]> {
async resolve (options?: AbortOptions & ResolverOptions): Promise<Multiaddr[]> {
const resolvableProto = this.protos().find((p) => p.resolvable)

// Multiaddr is not resolvable?
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/dns.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Resolver } from 'dns/promises'
import { Resolver } from 'node:dns/promises'

export default Resolver
7 changes: 5 additions & 2 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { getProtocol } from '../protocols-table.js'
import Resolver from './dns.js'
import type { AbortOptions, Multiaddr } from '../index.js'
import type { AbortOptions, Multiaddr, ResolverOptions } from '../index.js'

const { code: dnsaddrCode } = getProtocol('dnsaddr')

Expand All @@ -31,8 +31,11 @@ const { code: dnsaddrCode } = getProtocol('dnsaddr')
* //]
* ```
*/
export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions = {}): Promise<string[]> {
export async function dnsaddrResolver (addr: Multiaddr, options: AbortOptions & ResolverOptions = {}): Promise<string[]> {
const resolver = new Resolver()
if (options.dnsServers != null) {
resolver.setServers(options.dnsServers)
}

if (options.signal != null) {
options.signal.addEventListener('abort', () => {
Expand Down