Timeout fixes and cleanup
parent
88b52d23cd
commit
76f951093f
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `website` ADD COLUMN `favicon` VARCHAR(500) NULL;
|
||||||
|
|
@ -85,6 +85,7 @@ model website {
|
||||||
domain String? @db.VarChar(500)
|
domain String? @db.VarChar(500)
|
||||||
share_id String? @unique() @db.VarChar(64)
|
share_id String? @unique() @db.VarChar(64)
|
||||||
created_at DateTime? @default(now()) @db.Timestamp(0)
|
created_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
|
favicon String? @db.VarChar(500)
|
||||||
account account @relation(fields: [user_id], references: [user_id])
|
account account @relation(fields: [user_id], references: [user_id])
|
||||||
event event[]
|
event event[]
|
||||||
pageview pageview[]
|
pageview pageview[]
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import AbortController from 'abort-controller';
|
|
||||||
import { parse } from 'node-html-parser';
|
import { parse } from 'node-html-parser';
|
||||||
|
import { TimeoutController } from 'timeout-abort-controller';
|
||||||
//Node.js >=14.17 only
|
|
||||||
//const AbortController = globalThis.AbortController || (await import('abort-controller'));
|
|
||||||
|
|
||||||
const filterLinks = links => {
|
const filterLinks = links => {
|
||||||
const attrs = ['rel', 'href', 'sizes'];
|
const attrs = ['rel', 'href', 'sizes'];
|
||||||
|
|
@ -14,7 +11,7 @@ const filterLinks = links => {
|
||||||
.map(filterAttrs);
|
.map(filterAttrs);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateAttrs = url => icons => {
|
const formatValues = (url, icons) => {
|
||||||
const getOrigin = url => new URL(url).origin;
|
const getOrigin = url => new URL(url).origin;
|
||||||
return icons.map(({ sizes, href, rel }) => ({
|
return icons.map(({ sizes, href, rel }) => ({
|
||||||
size: parseInt(sizes?.split('x')[0]) || undefined,
|
size: parseInt(sizes?.split('x')[0]) || undefined,
|
||||||
|
|
@ -23,28 +20,30 @@ const updateAttrs = url => icons => {
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchLinks = url => {
|
const fetchLinks = async url => {
|
||||||
const controller = new AbortController();
|
// Time out the favicon request if it doesn't respond in a reasonable time.
|
||||||
const timeout = setTimeout(() => {
|
const tc = new TimeoutController(3000);
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.log('Aborting!');
|
let links = [];
|
||||||
controller.abort();
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return fetch(url, { signal: controller.signal })
|
const html = await (await fetch(url, { signal: tc.signal })).text();
|
||||||
.then(res => res.text())
|
links = parse(html).querySelectorAll('head link');
|
||||||
.then(str => parse(str))
|
|
||||||
.then(html => html.querySelectorAll('head link'));
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`Could not fetch favicon for url ${url}`, e);
|
console.log(`Could not fetch favicon for url ${url}`, e);
|
||||||
} finally {
|
} 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 => {
|
export default async domain => {
|
||||||
const icons = await getIcons(`https://${domain}`);
|
const icons = await getIcons(`https://${domain}`);
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,7 @@
|
||||||
"request-ip": "^3.3.0",
|
"request-ip": "^3.3.0",
|
||||||
"semver": "^7.3.6",
|
"semver": "^7.3.6",
|
||||||
"thenby": "^1.3.4",
|
"thenby": "^1.3.4",
|
||||||
|
"timeout-abort-controller": "^3.0.0",
|
||||||
"timezone-support": "^2.0.2",
|
"timezone-support": "^2.0.2",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2",
|
||||||
"zustand": "^3.7.2"
|
"zustand": "^3.7.2"
|
||||||
|
|
|
||||||
12
yarn.lock
12
yarn.lock
|
|
@ -5904,6 +5904,11 @@ restore-cursor@^3.1.0:
|
||||||
onetime "^5.1.0"
|
onetime "^5.1.0"
|
||||||
signal-exit "^3.0.2"
|
signal-exit "^3.0.2"
|
||||||
|
|
||||||
|
retimer@^3.0.0:
|
||||||
|
version "3.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/retimer/-/retimer-3.0.0.tgz#98b751b1feaf1af13eb0228f8ea68b8f9da530df"
|
||||||
|
integrity sha512-WKE0j11Pa0ZJI5YIk0nflGI7SQsfl2ljihVy7ogh7DeQSeYAUi0ubZ/yEueGtDfUPk6GH5LRw1hBdLq4IwUBWA==
|
||||||
|
|
||||||
reusify@^1.0.4:
|
reusify@^1.0.4:
|
||||||
version "1.0.4"
|
version "1.0.4"
|
||||||
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
|
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
|
||||||
|
|
@ -6492,6 +6497,13 @@ through@2.3.8, "through@>=2.2.7 <3", through@^2.3.8:
|
||||||
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
|
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
|
||||||
integrity sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==
|
integrity sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==
|
||||||
|
|
||||||
|
timeout-abort-controller@^3.0.0:
|
||||||
|
version "3.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/timeout-abort-controller/-/timeout-abort-controller-3.0.0.tgz#dd57ffca041652c03769904f8d95afd93fb95595"
|
||||||
|
integrity sha512-O3e+2B8BKrQxU2YRyEjC/2yFdb33slI22WRdUaDx6rvysfi9anloNZyR2q0l6LnePo5qH7gSM7uZtvvwZbc2yA==
|
||||||
|
dependencies:
|
||||||
|
retimer "^3.0.0"
|
||||||
|
|
||||||
timezone-support@^2.0.2:
|
timezone-support@^2.0.2:
|
||||||
version "2.0.2"
|
version "2.0.2"
|
||||||
resolved "https://registry.npmjs.org/timezone-support/-/timezone-support-2.0.2.tgz"
|
resolved "https://registry.npmjs.org/timezone-support/-/timezone-support-2.0.2.tgz"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue