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

feat: cache dns lookup #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -125,16 +123,8 @@ const baseHtml = ({ url, headers, head, body }) => {
<meta name="viewport" content="width=device-width, initial-scale=1.0" shrink-to-fit="no">
<title>${path.basename(url)}</title>
<meta property="og:site_name" content="${hostname}">
${
date
? `<meta property="article:published_time" content="${date}">`
: ''
}
${
expires
? `<meta property="article:expiration_time" content="${expires}">`
: ''
}
${date ? `<meta property="article:published_time" content="${date}">` : ''}
${expires ? `<meta property="article:expiration_time" content="${expires}">` : ''}
<meta property="og:locale" content="en">
<meta property="og:url" content="${url}">
${head}
Expand Down Expand Up @@ -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)
Expand All @@ -200,7 +190,8 @@ module.exports = async (
getMode = determinateMode,
gotOptions,
prerender = 'auto',
puppeteerOpts
puppeteerOpts,
dnsCache = new Map()
} = {}
) => {
const { href: encodedUrl } = new URL(targetUrl)
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
})