Skip to content

Commit

Permalink
Add DNS cache (#731)
Browse files Browse the repository at this point in the history
Fixes #661
  • Loading branch information
szmarczak authored and sindresorhus committed Feb 21, 2019
1 parent ada5861 commit cd12351
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"@sindresorhus/is": "^0.15.0",
"@szmarczak/http-timer": "^1.1.2",
"@types/p-cancelable": "^1.0.0",
"cacheable-lookup": "^0.1.0",
"cacheable-request": "^6.0.0",
"debug": "^4.1.1",
"decompress-response": "^4.0.0",
Expand Down
11 changes: 9 additions & 2 deletions readme.md
Expand Up @@ -328,7 +328,14 @@ If this is disabled, a compressed response is returned as a `Buffer`. This may b
Type: `Object`<br>
Default: `false`

[Cache adapter instance](#cache-adapters) for storing cached data.
[Cache adapter instance](#cache-adapters) for storing cached response data.

###### dnsCache

Type: `Object`<br>
Default: `false`

[Cache adapter instance](#cache-adapters) for storing cached DNS data.

###### request

Expand Down Expand Up @@ -806,7 +813,7 @@ The promise returned by Got has a [`.cancel()`](https://github.com/sindresorhus/
<a name="cache-adapters"></a>
## Cache

Got implements [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request).
Got implements [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).

You can use the JavaScript `Map` type as an in-memory cache:

Expand Down
4 changes: 2 additions & 2 deletions source/as-stream.ts
Expand Up @@ -76,13 +76,13 @@ export default function asStream(options: MergedOptions) {
throw new Error('Failed to pipe. The response has been emitted already.');
}

const result = pipe(destination, options);
pipe(destination, options);

if (Reflect.has(destination, 'setHeader')) {
piped.add(destination);
}

return result;
return destination;
};

proxy.unpipe = stream => {
Expand Down
1 change: 1 addition & 0 deletions source/index.js
Expand Up @@ -48,6 +48,7 @@ const defaults = {
followRedirect: true,
stream: false,
cache: false,
dnsCache: false,
useElectronNet: false,
responseType: 'text',
resolveBodyOnly: false
Expand Down
7 changes: 7 additions & 0 deletions source/normalize-arguments.js
@@ -1,6 +1,7 @@
'use strict';
const {URL, URLSearchParams} = require('url'); // TODO: Use the `URL` global when targeting Node.js 10
const urlLib = require('url');
const CacheableLookup = require('cacheable-lookup');
const is = require('@sindresorhus/is');
const lowercaseKeys = require('lowercase-keys');
const urlToOptions = require('./utils/url-to-options').default;
Expand Down Expand Up @@ -93,6 +94,12 @@ const preNormalize = (options, defaults) => {
options.retry.errorCodes = new Set(options.retry.errorCodes);
}

if (options.dnsCache) {
const cacheableLookup = new CacheableLookup({cacheAdapter: options.dnsCache});
options.lookup = cacheableLookup.lookup;
delete options.dnsCache;
}

return options;
};

Expand Down
7 changes: 7 additions & 0 deletions test/cache.js
Expand Up @@ -143,3 +143,10 @@ test('doesn\'t cache response when received HTTP error', async t => {
t.is(statusCode, 200);
t.deepEqual(body, 'ok');
});

test('DNS cache works', async t => {
const map = new Map();
await t.notThrowsAsync(got('https://example.com', {dnsCache: map}));

t.is(map.size, 1);
});

0 comments on commit cd12351

Please sign in to comment.