Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DNS cache #731

Merged
merged 2 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior is the same, this just fixes some some linting error :P


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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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);
});