cacheable-lookup
Advanced tools
Comparing version 6.0.1 to 6.0.2
{ | ||
"name": "cacheable-lookup", | ||
"version": "6.0.1", | ||
"description": "A cacheable dns.lookup(…) that respects the TTL", | ||
"version": "6.0.2", | ||
"description": "A cacheable dns.lookup(…) that respects TTL", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=10.6.0" |
@@ -6,3 +6,3 @@ # cacheable-lookup | ||
[![Node CI](https://github.com/szmarczak/cacheable-lookup/workflows/Node%20CI/badge.svg)](https://github.com/szmarczak/cacheable-lookup/actions) | ||
[![Coverage Status](https://coveralls.io/repos/github/szmarczak/cacheable-lookup/badge.svg?branch=master)](https://coveralls.io/github/szmarczak/cacheable-lookup?branch=master) | ||
[![codecov](https://codecov.io/gh/szmarczak/cacheable-lookup/branch/master/graph/badge.svg)](https://codecov.io/gh/szmarczak/cacheable-lookup) | ||
[![npm](https://img.shields.io/npm/dm/cacheable-lookup.svg)](https://www.npmjs.com/package/cacheable-lookup) | ||
@@ -32,6 +32,9 @@ [![install size](https://packagephobia.now.sh/badge?p=cacheable-lookup)](https://packagephobia.now.sh/result?p=cacheable-lookup) | ||
const http = require('http'); | ||
const https = require('https'); | ||
const CacheableLookup = require('cacheable-lookup'); | ||
const cacheable = new CacheableLookup(); | ||
cacheable.install(http.globalAgent); | ||
cacheable.install(https.globalAgent); | ||
@@ -38,0 +41,0 @@ http.get('http://example.com', response => { |
@@ -66,4 +66,16 @@ 'use strict'; | ||
const ignoreNoResultErrors = dnsPromise => { | ||
return dnsPromise.catch(error => { | ||
if (error.code === 'ENODATA' || error.code === 'ENOTFOUND') { | ||
return []; | ||
} | ||
throw error; | ||
}); | ||
}; | ||
const ttl = {ttl: true}; | ||
const all = {all: true}; | ||
const all4 = {all: true, family: 4}; | ||
const all6 = {all: true, family: 6}; | ||
@@ -226,19 +238,7 @@ class CacheableLookup { | ||
async _resolve(hostname) { | ||
const wrap = async promise => { | ||
try { | ||
return await promise; | ||
} catch (error) { | ||
if (error.code === 'ENODATA' || error.code === 'ENOTFOUND') { | ||
return []; | ||
} | ||
throw error; | ||
} | ||
}; | ||
// ANY is unsafe as it doesn't trigger new queries in the underlying server. | ||
const [A, AAAA] = await Promise.all([ | ||
this._resolve4(hostname, ttl), | ||
this._resolve6(hostname, ttl) | ||
].map(promise => wrap(promise))); | ||
ignoreNoResultErrors(this._resolve4(hostname, ttl)), | ||
ignoreNoResultErrors(this._resolve6(hostname, ttl)) | ||
]); | ||
@@ -285,17 +285,16 @@ let aTtl = 0; | ||
async _lookup(hostname) { | ||
try { | ||
const entries = await this._dnsLookup(hostname, { | ||
all: true | ||
}); | ||
const [A, AAAA] = await Promise.all([ | ||
// Passing {all: true} doesn't return all IPv4 and IPv6 entries. | ||
// See https://github.com/szmarczak/cacheable-lookup/issues/42 | ||
ignoreNoResultErrors(this._dnsLookup(hostname, all4)), | ||
ignoreNoResultErrors(this._dnsLookup(hostname, all6)) | ||
]); | ||
return { | ||
entries, | ||
cacheTtl: 0 | ||
}; | ||
} catch (_) { | ||
return { | ||
entries: [], | ||
cacheTtl: 0 | ||
}; | ||
} | ||
return { | ||
entries: [ | ||
...A, | ||
...AAAA | ||
], | ||
cacheTtl: 0 | ||
}; | ||
} | ||
@@ -302,0 +301,0 @@ |
24318
244
475