Skip to content

Commit

Permalink
Merge pull request #718 from microlinkhq/next
Browse files Browse the repository at this point in the history
chore(logo-favicon): add resolveFaviconUrl
  • Loading branch information
Kikobeats authored Jul 15, 2024
2 parents 5fb73f2 + bbe6b15 commit 2fc0cc6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
6 changes: 6 additions & 0 deletions packages/metascraper-logo-favicon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const metascraper = require('metascraper')([

If you don't specific it, the favicon with the bigger size will be picked.

##### resolveFaviconUrl

Type: `function`

It will be used to determine if a favicon URL is valid.

##### gotOpts

Type: `object`
Expand Down
55 changes: 39 additions & 16 deletions packages/metascraper-logo-favicon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ const firstReachable = async (domNodeSizes, gotOpts) => {
contentTypes &&
(!isValidContenType(contentType, contentTypes[1]) ||
response.body.toString()[0] === '<')
) { continue }
) {
continue
}

return response.url
}
Expand All @@ -137,21 +139,36 @@ const pickBiggerSize = async (sizes, { gotOpts } = {}) => {
pickBiggerSize.sortBySize = collection =>
orderBy(collection, ['size.priority'], ['desc'])

const createFavicon = ([ext, contentTypes]) => {
return async (url, { gotOpts } = {}) => {
const faviconUrl = logo(`/favicon.${ext}`, { url })
if (!faviconUrl) return undefined
const response = await reachableUrl(faviconUrl, gotOpts)
if (!reachableUrl.isReachable(response)) return undefined
const contentType = response.headers['content-type']
const defaultResolveFaviconUrl = async (faviconUrl, contentTypes, gotOpts) => {
const response = await reachableUrl(faviconUrl, gotOpts)
if (!reachableUrl.isReachable(response)) return undefined

if (
contentTypes &&
(!isValidContenType(contentType, contentTypes) ||
response.body.toString()[0] === '<')
) { return undefined }
const contentType = response.headers['content-type']

return response.url
if (
contentTypes &&
(!isValidContenType(contentType, contentTypes) ||
response.body.toString()[0] === '<')
) {
return undefined
}

if (contentTypes && !isValidContenType(contentType, contentTypes)) {
return undefined
}

return response.url
}

const createFavicon = (
[ext, contentTypes],
resolveFaviconUrl = defaultResolveFaviconUrl
) => {
return async (url, { gotOpts } = {}) => {
const faviconUrl = logo(`/favicon.${ext}`, { url })
return faviconUrl
? resolveFaviconUrl(faviconUrl, contentTypes, gotOpts)
: undefined
}
}

Expand All @@ -163,10 +180,16 @@ const google = async (url, { gotOpts } = {}) => {
google.url = (url, size = 128) =>
`https://www.google.com/s2/favicons?domain_url=${url}&sz=${size}`

const createGetLogo = ({ withGoogle, withFavicon, gotOpts, keyvOpts }) => {
const createGetLogo = ({
gotOpts,
keyvOpts,
resolveFaviconUrl,
withFavicon,
withGoogle
}) => {
const getLogo = async url => {
const providers = ALLOWED_EXTENSION_CONTENT_TYPES.map(
ext => withFavicon && createFavicon(ext)
ext => withFavicon && createFavicon(ext, resolveFaviconUrl)
)
.concat(withGoogle && google)
.filter(Boolean)
Expand Down

0 comments on commit 2fc0cc6

Please sign in to comment.