From f5fb0cf6637e572474613c8fa32702f1375d5494 Mon Sep 17 00:00:00 2001 From: Antoine Pultier <45740+fungiboletus@users.noreply.github.com> Date: Thu, 19 Nov 2020 14:46:47 +0100 Subject: [PATCH 1/3] Instantiate CacheableLookup only when needed The `dnsCache` option is disabled by default but a `CacheableLookup` instance is always created. The proposed change is to instantiate CacheableLookup only when needed, which may make `got` a bit faster to load by default and when you do not use the DNS cache. Moreover, the CacheableLookup class instantiates the DNS [`Resolver` class in NodeJS the standard library](https://nodejs.org/api/dns.html#dns_class_dnspromises_resolver), and the test framework [Jest](https://github.com/facebook/jest/) has a problem with it. It wrongly detects that something keeps running after the tests are finished, while it's just an instance of the DNS Resolver class. See https://github.com/facebook/jest/issues/6423 Jest should be fixed but meanwhile this small improvement in got may also hide the Jest issue for many developers. --- source/core/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index f3b08fb4d..448e12844 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -31,7 +31,7 @@ import normalizePromiseArguments from '../as-promise/normalize-arguments'; import {PromiseOnly} from '../as-promise/types'; import calculateRetryDelay from './calculate-retry-delay'; -const globalDnsCache = new CacheableLookup(); +let globalDnsCache; type HttpRequestFunction = typeof httpRequest; type Error = NodeJS.ErrnoException; @@ -1758,6 +1758,9 @@ export default class Request extends Duplex implements RequestEvents { // `options.dnsCache` if (options.dnsCache === true) { + if (!globalDnsCache) { + globalDnsCache = 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)}`); From e7e20770dbc425fd46010c691f24e22650e7f961 Mon Sep 17 00:00:00 2001 From: Antoine Pultier <45740+fungiboletus@users.noreply.github.com> Date: Thu, 19 Nov 2020 15:42:51 +0100 Subject: [PATCH 2/3] Explicit type of the globalDnsCache variable. --- source/core/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 448e12844..47f9ed20e 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -31,7 +31,7 @@ import normalizePromiseArguments from '../as-promise/normalize-arguments'; import {PromiseOnly} from '../as-promise/types'; import calculateRetryDelay from './calculate-retry-delay'; -let globalDnsCache; +let globalDnsCache : CacheableLookup; type HttpRequestFunction = typeof httpRequest; type Error = NodeJS.ErrnoException; From b4440edac096b3d655eb70620f3271ddf7e463ba Mon Sep 17 00:00:00 2001 From: Antoine Pultier <45740+fungiboletus@users.noreply.github.com> Date: Thu, 19 Nov 2020 15:47:44 +0100 Subject: [PATCH 3/3] Fix code style --- source/core/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 47f9ed20e..ae1015fa6 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -31,7 +31,7 @@ import normalizePromiseArguments from '../as-promise/normalize-arguments'; import {PromiseOnly} from '../as-promise/types'; import calculateRetryDelay from './calculate-retry-delay'; -let globalDnsCache : CacheableLookup; +let globalDnsCache: CacheableLookup; type HttpRequestFunction = typeof httpRequest; type Error = NodeJS.ErrnoException; @@ -1761,6 +1761,7 @@ export default class Request extends Duplex implements RequestEvents { if (!globalDnsCache) { globalDnsCache = 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)}`);