Update the browser, editor, settings, and installer
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { webUrl } from './policy.mjs';
|
||||
|
||||
const limit = 64 * 1024;
|
||||
const imageType = /^image\/(?:png|jpeg|gif|webp|x-icon|vnd\.microsoft\.icon|svg\+xml)$/;
|
||||
|
||||
export async function loadFavicon(urls, fetchImage, signal) {
|
||||
for (const url of [...new Set(urls)].slice(0, 8)) {
|
||||
if (signal.aborted) return null;
|
||||
try {
|
||||
if (typeof url !== 'string' || url.length > 90_000) continue;
|
||||
if (url.startsWith('data:image/')) {
|
||||
const comma = url.indexOf(',');
|
||||
const metadata = url.slice(5, comma).split(';');
|
||||
if (comma < 0 || !imageType.test(metadata[0])) continue;
|
||||
const bytes = Buffer.from(decodeURIComponent(url.slice(comma + 1)), metadata.includes('base64') ? 'base64' : 'utf8');
|
||||
if (bytes.length && bytes.length <= limit) return `data:${metadata[0]};base64,${bytes.toString('base64')}`;
|
||||
continue;
|
||||
}
|
||||
webUrl(url, { blank: false });
|
||||
const response = await fetchImage(url, { signal: AbortSignal.any([signal, AbortSignal.timeout(5000)]) });
|
||||
try {
|
||||
const type = response.headers.get('content-type')?.split(';')[0].trim().toLowerCase();
|
||||
if (!response.ok || !imageType.test(type || '') || Number(response.headers.get('content-length')) > limit) continue;
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of response.body) {
|
||||
size += chunk.length;
|
||||
if (size > limit) throw new Error('Icon exceeds the size limit.');
|
||||
chunks.push(chunk);
|
||||
}
|
||||
if (size) return `data:${type};base64,${Buffer.concat(chunks).toString('base64')}`;
|
||||
} finally {
|
||||
if (!response.body?.locked) await response.body?.cancel().catch(() => {});
|
||||
}
|
||||
} catch { /* Try the next icon when a request fails. */ }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user