Instantiate CacheableLookup only when needed#1529
Instantiate CacheableLookup only when needed#1529szmarczak merged 3 commits intosindresorhus:masterfrom TelluIoT:cacheablelookup-delayed-instantiation
Conversation
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 jestjs/jest#6423 Jest should be fixed but meanwhile this small improvement in got may also hide the Jest issue for many developers.
|
It looks like the tests succeed excepts with Node 12. But I'm not sure it's related to the changes in this pull request. The last commit on the master branch was also failing on Node 12 : https://github.com/sindresorhus/got/runs/1342216844 |
|
This will cause the |
|
Are you sure ? I think it will be generated only the first time. It's still a global instance, just not initialized before it's used. |
|
|
I'm sure. |
|
I understand you don't want to create one instance for each call, and this test is still successful with my suggested modification. https://travis-ci.com/github/sindresorhus/got/jobs/443599127 |
|
Ah, I see now. I didn't notice the |
|
No problems, I understand why you were suspicious. Many thanks for the merge! |
|
Thanks to you for the PR! Never underestimate your work :) |
|
Could we get a release cut of |
|
No, there is v11 already and v12 is pending. |
|
We decided to back port it: https://github.com/sindresorhus/got/releases/tag/v11.8.2 |

The
dnsCacheoption is disabled by default but aCacheableLookupinstance is always created. The proposed change is to instantiate CacheableLookup only when needed, which may makegota bit faster to load by default and when you do not use the DNS cache.Moreover, the CacheableLookup class instantiates the DNS
Resolverclass in NodeJS the standard library, and the test framework 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 jestjs/jest#6423Jest should be fixed but meanwhile this small improvement in got may also hide the Jest issue for many developers.
Checklist