From bbe6b15cdc9109da0ef7e37eb84afa54e29daa51 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 15 Jul 2024 10:02:37 +0200 Subject: [PATCH] chore(logo-favicon): add resolveFaviconUrl --- packages/metascraper-logo-favicon/README.md | 6 ++ .../metascraper-logo-favicon/src/index.js | 55 +++++++++++++------ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/packages/metascraper-logo-favicon/README.md b/packages/metascraper-logo-favicon/README.md index 8dab902cf..69b0a9919 100644 --- a/packages/metascraper-logo-favicon/README.md +++ b/packages/metascraper-logo-favicon/README.md @@ -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` diff --git a/packages/metascraper-logo-favicon/src/index.js b/packages/metascraper-logo-favicon/src/index.js index 64186c407..e9ba83a5a 100644 --- a/packages/metascraper-logo-favicon/src/index.js +++ b/packages/metascraper-logo-favicon/src/index.js @@ -113,7 +113,9 @@ const firstReachable = async (domNodeSizes, gotOpts) => { contentTypes && (!isValidContenType(contentType, contentTypes[1]) || response.body.toString()[0] === '<') - ) { continue } + ) { + continue + } return response.url } @@ -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 } } @@ -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)