From 043c9501b85172e09819d44ac8eb49c574b27bda Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Tue, 28 Jul 2020 10:59:11 +0200 Subject: [PATCH] Fix `dnsCache: true` having no effect Fixes #1372 --- source/core/index.ts | 4 +++- test/create.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index f49889235..b51a6ae52 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -28,6 +28,8 @@ import {DnsLookupIpVersion, isDnsLookupIpVersion, dnsLookupIpVersionToFamily} fr import deprecationWarning from '../utils/deprecation-warning'; import {PromiseOnly} from '../as-promise/types'; +const globalDnsCache = new CacheableLookup(); + type HttpRequestFunction = typeof httpRequest; type Error = NodeJS.ErrnoException; @@ -888,7 +890,7 @@ export default class Request extends Duplex implements RequestEvents { // `options.dnsCache` if (options.dnsCache === true) { - options.dnsCache = new CacheableLookup(); + options.dnsCache = globalDnsCache; } else if (!is.undefined(options.dnsCache) && !options.dnsCache.lookup) { throw new TypeError(`Parameter \`dnsCache\` must be a CacheableLookup instance or a boolean, got ${is(options.dnsCache)}`); } diff --git a/test/create.ts b/test/create.ts index d8589653f..75f47cdc1 100644 --- a/test/create.ts +++ b/test/create.ts @@ -312,3 +312,15 @@ test('async handlers can throw', async t => { await t.throwsAsync(instance('https://example.com'), {message}); }); + +test('setting dnsCache to true points to global cache', t => { + const a = got.extend({ + dnsCache: true + }); + + const b = got.extend({ + dnsCache: true + }); + + t.is(a.defaults.options.dnsCache, b.defaults.options.dnsCache); +});