From 95edd54643b46b23fe78813794000f7fec0a3a63 Mon Sep 17 00:00:00 2001 From: Szymon Marczak Date: Tue, 19 Feb 2019 21:02:19 +0100 Subject: [PATCH 1/2] DNS cache --- package.json | 1 + readme.md | 11 +++++++++-- source/as-stream.ts | 4 ++-- source/index.js | 1 + source/normalize-arguments.js | 7 +++++++ test/cache.js | 7 +++++++ 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 44e4f6142..b1cf4cac0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index d3405b256..0a113a357 100644 --- a/readme.md +++ b/readme.md @@ -328,7 +328,14 @@ If this is disabled, a compressed response is returned as a `Buffer`. This may b Type: `Object`
Default: `false` -[Cache adapter instance](#cache-adapters) for storing cached data. +[Cache adapter instance](#cache-adapters) for storing cached response data. + +###### dnsCache + +Type: `Object`
+Default: `false` + +[Cache adapter instance](#cache-adapters) for storing cached DNS data. ###### request @@ -806,7 +813,7 @@ The promise returned by Got has a [`.cancel()`](https://github.com/sindresorhus/ ## 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: diff --git a/source/as-stream.ts b/source/as-stream.ts index 654740dfe..4d5226eeb 100644 --- a/source/as-stream.ts +++ b/source/as-stream.ts @@ -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 => { diff --git a/source/index.js b/source/index.js index f13d87440..0fe6bd7b4 100644 --- a/source/index.js +++ b/source/index.js @@ -48,6 +48,7 @@ const defaults = { followRedirect: true, stream: false, cache: false, + dnsCache: false, useElectronNet: false, responseType: 'text', resolveBodyOnly: false diff --git a/source/normalize-arguments.js b/source/normalize-arguments.js index 1d6a6d13a..8e363d135 100644 --- a/source/normalize-arguments.js +++ b/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; @@ -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; }; diff --git a/test/cache.js b/test/cache.js index 7fd961bc8..1fb72eba8 100644 --- a/test/cache.js +++ b/test/cache.js @@ -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); +}); From af2316ea2c3131790d1482af716e4ed7a55bdbc0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 21 Feb 2019 14:01:40 +0700 Subject: [PATCH 2/2] Update cache.js --- test/cache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cache.js b/test/cache.js index 1fb72eba8..09f6aca3a 100644 --- a/test/cache.js +++ b/test/cache.js @@ -144,7 +144,7 @@ test('doesn\'t cache response when received HTTP error', async t => { t.deepEqual(body, 'ok'); }); -test('dns cache works', async t => { +test('DNS cache works', async t => { const map = new Map(); await t.notThrowsAsync(got('https://example.com', {dnsCache: map}));