diff --git a/src/index.js b/src/index.js index 3484175..160eab8 100644 --- a/src/index.js +++ b/src/index.js @@ -31,10 +31,11 @@ const REQ_TIMEOUT_REACHABLE = REQ_TIMEOUT * 0.25 // Puppeteer doesn't resolve redirection well. // We need to ensure we have the right url. const getUrl = mem( - async targetUrl => { + async (targetUrl, { dnsCache }) => { try { const res = await reachableUrl(targetUrl, { - timeout: REQ_TIMEOUT_REACHABLE + timeout: REQ_TIMEOUT_REACHABLE, + dnsCache }) return res } catch (err) { @@ -73,17 +74,14 @@ const fetch = (url, { toEncode, reflect = false, ...opts }) => } }) -const prerender = async ( - url, - { getBrowserless, gotOptions, toEncode, ...opts } -) => { +const prerender = async (url, { getBrowserless, gotOptions, toEncode, dnsCache, ...opts }) => { let fetchReq let fetchDataProps = {} let isFetchRejected = false let html = '' try { - fetchReq = fetch(url, { reflect: true, toEncode, ...gotOptions }) + fetchReq = fetch(url, { reflect: true, toEncode, dnsCache, ...gotOptions }) const browserless = await getBrowserless() html = await browserless.html(url, { timeout: REQ_TIMEOUT, ...opts }) await fetchReq.cancel() @@ -125,16 +123,8 @@ const baseHtml = ({ url, headers, head, body }) => { ${path.basename(url)} - ${ - date - ? `` - : '' -} - ${ - expires - ? `` - : '' -} + ${date ? `` : ''} + ${expires ? `` : ''} ${head} @@ -183,7 +173,7 @@ const getAudioHtml = (url, headers) => { } const getContent = async (encodedUrl, mode, opts) => { - const { url, headers } = await getUrl(encodedUrl) + const { url, headers } = await getUrl(encodedUrl, opts) debug(`getUrl ${encodedUrl === url ? url : `${encodedUrl} → ${url}`}`) const contentType = headers['content-type'] if (isMime(contentType, 'image')) return getImageHtml(url, headers) @@ -200,7 +190,8 @@ module.exports = async ( getMode = determinateMode, gotOptions, prerender = 'auto', - puppeteerOpts + puppeteerOpts, + dnsCache = new Map() } = {} ) => { const { href: encodedUrl } = new URL(targetUrl) @@ -209,8 +200,8 @@ module.exports = async ( const opts = reqMode === 'fetch' - ? { toEncode, ...gotOptions } - : { toEncode, getBrowserless, gotOptions, ...puppeteerOpts } + ? { dnsCache, toEncode, ...gotOptions } + : { dnsCache, toEncode, getBrowserless, gotOptions, ...puppeteerOpts } const time = timeSpan() const { url, html, mode } = await getContent(encodedUrl, reqMode, opts) diff --git a/test/index.js b/test/index.js index 7bae9ce..3104eff 100644 --- a/test/index.js +++ b/test/index.js @@ -57,8 +57,7 @@ test('decode base64 entities', async t => { }) test('unencoded URL', async t => { - const url = - 'https://medium.com/@Acegikmo/the-ever-so-lovely-bézier-curve-eb27514da3bf' + const url = 'https://medium.com/@Acegikmo/the-ever-so-lovely-bézier-curve-eb27514da3bf' t.is( await wait(getHTML(url, { prerender: false }), 'url'), 'https://medium.com/@Acegikmo/the-ever-so-lovely-b%C3%A9zier-curve-eb27514da3bf' @@ -97,3 +96,11 @@ test('get html from video url', async t => { t.is(stats.mode, 'fetch') t.is(url, urlDetected) }) + +test('cache dns lookup', async t => { + const url = 'https://kikobeats.com' + const dnsCache = new Map() + + await getHTML(url, { prerender: false }) + t.is(dnsCache.size, 1) +})