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

fix: specify correct return type from resolve #101

Merged
merged 3 commits into from
Oct 26, 2023
Merged
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
112 changes: 7 additions & 105 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,107 +1,9 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
node_modules
build
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
.docs
.coverage
node_modules
package-lock.json

docs/
yarn.lock
.vscode
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# dns-over-http-resolver <!-- omit in toc -->

[![codecov](https://img.shields.io/codecov/c/github/vasco-santos/dns-over-http-resolver.svg?style=flat-square)](https://codecov.io/gh/vasco-santos/dns-over-http-resolver)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/main?style=flat-square)](https://github.com/vasco-santos/dns-over-http-resolver/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/actions/workflow/status/vasco-santos/dns-over-http-resolver/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/vasco-santos/dns-over-http-resolver/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> DNS over HTTP resolver
## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Browser `<script>` tag](#browser-script-tag)
- [Usage](#usage)
- [options](#options)
- [API](#api)
Expand All @@ -34,6 +35,7 @@
- [Parameters](#parameters-4)
- [Example](#example-5)
- [Contribute](#contribute)
- [API Docs](#api-docs)
- [License](#license)
- [Contribution](#contribution)

Expand All @@ -43,6 +45,14 @@
$ 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.

```html
<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](https://nodejs.org/dist/latest-v14.x/docs/api/dns.html#dns_dns_promises_api), allowing the native `dns` module to be used if available when relying on this API.
Expand Down Expand Up @@ -223,6 +233,10 @@ resolver.setServers(['https://cloudflare-dns.com/dns-query'])

Feel free to dive in! [Open an issue](https://github.com/vasco-santos/dns-over-http-resolver/issues/new) or submit PRs.

## API Docs

- <https://vasco-santos.github.io/dns-over-http-resolver>

## License

Licensed under either of
Expand Down
23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
"doh",
"http"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist/src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
Expand All @@ -38,6 +34,7 @@
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"project": true,
"sourceType": "module"
}
},
Expand Down Expand Up @@ -68,15 +65,15 @@
"release": "patch"
},
{
"type": "chore",
"type": "docs",
"release": "patch"
},
{
"type": "docs",
"type": "test",
"release": "patch"
},
{
"type": "test",
"type": "deps",
"release": "patch"
},
{
Expand Down Expand Up @@ -134,7 +131,8 @@
"lint": "aegir lint",
"release": "aegir release",
"build": "aegir build",
"dep-check": "aegir dep-check"
"dep-check": "aegir dep-check",
"docs": "aegir docs"
},
"dependencies": {
"debug": "^4.3.1",
Expand All @@ -144,9 +142,8 @@
},
"devDependencies": {
"@types/debug": "^4.1.7",
"aegir": "^37.4.4",
"sinon": "^17.0.0",
"ts-sinon": "^2.0.2",
"util": "^0.12.3"
"@types/sinon": "^10.0.20",
"aegir": "^41.0.11",
"sinon": "^17.0.0"
}
}
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ class Resolver {
* Cancel all outstanding DNS queries made by this resolver. Any outstanding
* requests will be aborted and promises rejected.
*/
cancel () {
this._abortControllers.forEach(controller => controller.abort())
cancel (): void {
this._abortControllers.forEach(controller => { controller.abort() })
}

/**
* 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.
*/
getServers () {
getServers (): string[] {
return this._servers
}

/**
* Get a shuffled array of the IP addresses currently configured for DNS resolution.
* These addresses are formatted according to RFC 5952. It can include a custom port.
*/
_getShuffledServers () {
_getShuffledServers (): string[] {
const newServers = [...this._servers]

for (let i = newServers.length - 1; i > 0; i--) {
Expand All @@ -80,7 +80,7 @@ class Resolver {
*
* @param {string[]} servers - array of RFC 5952 formatted addresses.
*/
setServers (servers: string[]) {
setServers (servers: string[]): void {
this._servers = servers
}

Expand All @@ -90,14 +90,17 @@ class Resolver {
* @param {string} hostname - host name to resolve
* @param {string} [rrType = 'A'] - resource record type
*/
async resolve (hostname: string, rrType = 'A') {
async resolve (hostname: string, rrType: 'TXT'): Promise<string[][]>
async resolve (hostname: string, rrType: 'A' | 'AAAA'): Promise<string[]>
async resolve (hostname: string): Promise<string[]>
async resolve (hostname: string, rrType: string = 'A'): Promise<string[] | string[][]> {
switch (rrType) {
case 'A':
return await this.resolve4(hostname)
return this.resolve4(hostname)
case 'AAAA':
return await this.resolve6(hostname)
return this.resolve6(hostname)
case 'TXT':
return await this.resolveTxt(hostname)
return this.resolveTxt(hostname)
default:
throw new Error(`${rrType} is not supported`)
}
Expand All @@ -108,7 +111,7 @@ class Resolver {
*
* @param {string} hostname - host name to resolve
*/
async resolve4 (hostname: string) {
async resolve4 (hostname: string): Promise<string[]> {
const recordType = 'A'
const cached = this._cache.get(utils.getCacheKey(hostname, recordType))
if (cached != null) {
Expand Down Expand Up @@ -158,7 +161,7 @@ class Resolver {
*
* @param {string} hostname - host name to resolve
*/
async resolve6 (hostname: string) {
async resolve6 (hostname: string): Promise<string[]> {
const recordType = 'AAAA'
const cached = this._cache.get(utils.getCacheKey(hostname, recordType))
if (cached != null) {
Expand Down Expand Up @@ -208,7 +211,7 @@ class Resolver {
*
* @param {string} hostname - host name to resolve
*/
async resolveTxt (hostname: string) {
async resolveTxt (hostname: string): Promise<string[][]> {
const recordType = 'TXT'
const cached = this._TXTcache.get(utils.getCacheKey(hostname, recordType))
if (cached != null) {
Expand Down Expand Up @@ -253,7 +256,7 @@ class Resolver {
throw new Error(`Could not resolve ${hostname} record ${recordType}`)
}

clearCache () {
clearCache (): void {
this._cache.clear()
this._TXTcache.clear()
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fetch as nativeFetch, Headers } from 'native-fetch'
/**
* Build fetch resource for request
*/
export function buildResource (serverResolver: string, hostname: string, recordType: string) {
export function buildResource (serverResolver: string, hostname: string, recordType: string): string {
return `${serverResolver}?name=${hostname}&type=${recordType}`
}

Expand All @@ -27,7 +27,7 @@ interface Answer {
/**
* Use fetch to find the record
*/
export async function request (resource: string, signal: AbortSignal) {
export async function request (resource: string, signal: AbortSignal): Promise<DNSJSON> {
const req = await nativeFetch(resource, {
headers: new Headers({
accept: 'application/dns-json'
Expand All @@ -45,6 +45,6 @@ export async function request (resource: string, signal: AbortSignal) {
* @param {string} hostname
* @param {string} recordType
*/
export function getCacheKey (hostname: string, recordType: string) {
export function getCacheKey (hostname: string, recordType: string): string {
return `${recordType}_${hostname}`
}
11 changes: 4 additions & 7 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
// import * as utils from '../src/utils.js'
import sinon from 'sinon'
import DnsOverHttpResolver from '../src/index.js'
import type { DNSJSON } from '../src/utils.js'
import sinon from 'sinon'
import type { SinonStub } from 'sinon'

// const getFetchPair = () => [utils, 'fetch']
// const getFetch = () => utils.fetch

describe('dns-over-http-resolver', () => {
let resolver: DnsOverHttpResolver
let request: SinonStub<[string], Promise<DNSJSON>>
Expand Down Expand Up @@ -264,8 +261,8 @@ describe('dns-over-http-resolver', () => {
it('cancels an in-flight DNS request', async () => {
const hostname = 'example.com'

const request = async (host: string, signal: AbortSignal) => {
return await new Promise<DNSJSON>((resolve, reject) => {
const request = async (host: string, signal: AbortSignal): Promise<DNSJSON> => {
return new Promise<DNSJSON>((resolve, reject) => {
signal.addEventListener('abort', () => {
reject(new Error('aborted'))
})
Expand Down
5 changes: 5 additions & 0 deletions typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entryPoints": [
"./src/index.ts"
]
}