Timeout fixes and cleanup

This commit is contained in:
Stanislav Khromov 2022-10-09 18:23:30 +02:00
parent 88b52d23cd
commit 76f951093f
5 changed files with 33 additions and 18 deletions

View file

@ -1,9 +1,6 @@
import fetch from 'node-fetch';
import AbortController from 'abort-controller';
import { parse } from 'node-html-parser';
//Node.js >=14.17 only
//const AbortController = globalThis.AbortController || (await import('abort-controller'));
import { TimeoutController } from 'timeout-abort-controller';
const filterLinks = links => {
const attrs = ['rel', 'href', 'sizes'];
@ -14,7 +11,7 @@ const filterLinks = links => {
.map(filterAttrs);
};
const updateAttrs = url => icons => {
const formatValues = (url, icons) => {
const getOrigin = url => new URL(url).origin;
return icons.map(({ sizes, href, rel }) => ({
size: parseInt(sizes?.split('x')[0]) || undefined,
@ -23,28 +20,30 @@ const updateAttrs = url => icons => {
}));
};
const fetchLinks = url => {
const controller = new AbortController();
const timeout = setTimeout(() => {
// eslint-disable-next-line no-console
console.log('Aborting!');
controller.abort();
}, 1000);
const fetchLinks = async url => {
// Time out the favicon request if it doesn't respond in a reasonable time.
const tc = new TimeoutController(3000);
let links = [];
try {
return fetch(url, { signal: controller.signal })
.then(res => res.text())
.then(str => parse(str))
.then(html => html.querySelectorAll('head link'));
const html = await (await fetch(url, { signal: tc.signal })).text();
links = parse(html).querySelectorAll('head link');
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Could not fetch favicon for url ${url}`, e);
} finally {
clearTimeout(timeout);
tc.clear();
}
return links;
};
const getIcons = url => fetchLinks(url).then(filterLinks).then(updateAttrs(url));
const getIcons = async url => {
const links = await fetchLinks(url);
const icons = filterLinks(links);
return formatValues(url, icons);
};
export default async domain => {
const icons = await getIcons(`https://${domain}`);