1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-07 06:25:21 +02:00

Added timeout for uploading favicon request and writeFileSync changed to writeFile

This commit is contained in:
slaveeks 2022-07-07 11:23:14 +03:00
parent f1fa35564c
commit 3985edd62c

View file

@ -14,6 +14,9 @@ interface FaviconData {
type: string;
}
// Initiate controller for aborting request
const controller = new AbortController();
/**
* Check if string is url
*
@ -48,16 +51,30 @@ export async function downloadFavicon(destination: string): Promise<FaviconData>
return { destination: destination,
type: `image/${format}` } as FaviconData;
}
// Create timeout to abort request
const timeoutId = setTimeout(() => {
controller.abort();
console.log('Favicon request has timed out.');
}, 5000);
// Make get request to url
const res = await fetch(destination);
const res = await fetch(destination, { signal: controller.signal });
// Get buffer data from response
const fileData = await res.buffer();
// Clear timeout, if data was got
clearTimeout(timeoutId);
// Get file path in temporary directory
const filePath = path.join(os.tmpdir(), `favicon.${format}`);
// Save file
fs.writeFileSync(filePath, fileData);
await fs.writeFile(filePath, fileData, (err) => {
if (err) {
console.log(err);
}
});
return { destination: `/favicon/favicon.${format}`,
type: `image/${format}` } as FaviconData;