askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
favicons.ts · 6.5 KB · 137 lines
| 1 | // Site icons (favicons), one per host, for the tabs, the address bar's suggestions and the bookmarks panel. |
| 2 | // |
| 3 | // A page names its icon; the browser fetches it once (through the page's own session, like any of its requests), shrinks |
| 4 | // it when it can, and keeps it here as a data: URL. The chrome UI only ever shows these data: URLs, so it never loads |
| 5 | // anything from a site itself. Icons are learned only from pages you open: typing in the address bar fetches nothing, so a |
| 6 | // page from your history that was never opened on this computer shows the row's usual symbol instead. |
| 7 | // The file is a list of sites you have opened, so it is readable only by you, and forgetting a site forgets its icon. |
| 8 | |
| 9 | export const MAX_HOSTS = 1500; |
| 10 | export const MAX_BYTES = 48 * 1024; // an icon bigger than this (after shrinking) is not kept |
| 11 | export const REFRESH_MS = 7 * 24 * 3600 * 1000; // look again for a site's icon after a week |
| 12 | const RETRY_MS = 24 * 3600 * 1000; // a site whose icon could not be fetched is not tried again for a day |
| 13 | |
| 14 | export interface Fetched { bytes: Uint8Array; type: string } |
| 15 | export interface FaviconDeps { |
| 16 | /** Fetch an icon's bytes, or null (network error, not an image, too big). */ |
| 17 | fetch(url: string): Promise<Fetched | null>; |
| 18 | /** Shrink to a 32×32 PNG data: URL when the format allows it (PNG, JPEG); null to keep the original. */ |
| 19 | shrink(f: Fetched): string | null; |
| 20 | load(): string | null; |
| 21 | save(text: string | null): void; |
| 22 | now(): number; |
| 23 | } |
| 24 | interface Entry { icon: string; at: number; seen: number } |
| 25 | |
| 26 | const IMAGE = /^image\/(png|x-icon|vnd\.microsoft\.icon|ico|svg\+xml|jpeg|gif|webp|bmp)$/i; |
| 27 | |
| 28 | /** The image type from the first bytes (servers often send icons as text/plain or octet-stream), or null. */ |
| 29 | export function sniff(b: Uint8Array): string | null { |
| 30 | const at = (i: number, ...xs: number[]) => xs.every((x, j) => b[i + j] === x); |
| 31 | if (at(0, 0x89, 0x50, 0x4e, 0x47)) return "image/png"; |
| 32 | if (at(0, 0x00, 0x00, 0x01, 0x00)) return "image/x-icon"; |
| 33 | if (at(0, 0xff, 0xd8, 0xff)) return "image/jpeg"; |
| 34 | if (at(0, 0x47, 0x49, 0x46, 0x38)) return "image/gif"; |
| 35 | if (at(0, 0x52, 0x49, 0x46, 0x46) && at(8, 0x57, 0x45, 0x42, 0x50)) return "image/webp"; |
| 36 | const head = Buffer.from(b.subarray(0, 512)).toString("utf8").trimStart().toLowerCase(); |
| 37 | if (/^(<\?xml[^>]*>\s*)?(<!--[\s\S]*?-->\s*)*<svg[\s>]/.test(head)) return "image/svg+xml"; |
| 38 | if (/^<(!doctype|html)/.test(head)) return "text/html"; // a "not found" page served as the icon |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | export function hostOf(url: string): string { |
| 43 | try { |
| 44 | const u = new URL(url); |
| 45 | return /^https?:$/.test(u.protocol) ? u.hostname.toLowerCase() : ""; |
| 46 | } catch { return ""; } |
| 47 | } |
| 48 | |
| 49 | export class FaviconStore { |
| 50 | private map = new Map<string, Entry>(); |
| 51 | private failed = new Map<string, number>(); // host -> when it last failed (not saved) |
| 52 | private busy = new Set<string>(); |
| 53 | private saveTimer: ReturnType<typeof setTimeout> | null = null; |
| 54 | |
| 55 | constructor(private deps: FaviconDeps) { |
| 56 | try { |
| 57 | const raw = deps.load(); |
| 58 | const obj = raw ? JSON.parse(raw) : null; |
| 59 | if (obj && obj.v === 1 && obj.icons && typeof obj.icons === "object") { |
| 60 | for (const [h, e] of Object.entries(obj.icons as Record<string, Entry>)) { |
| 61 | if (e && typeof e.icon === "string" && e.icon.startsWith("data:image/") && typeof e.at === "number") this.map.set(h, { icon: e.icon, at: e.at, seen: Number(e.seen) || e.at }); |
| 62 | } |
| 63 | } |
| 64 | } catch { /* a damaged file: start empty */ } |
| 65 | } |
| 66 | |
| 67 | get(host: string): string | null { return this.map.get(host)?.icon ?? null; } |
| 68 | all(): Record<string, string> { return Object.fromEntries([...this.map].map(([h, e]) => [h, e.icon])); } |
| 69 | size(): number { return this.map.size; } |
| 70 | |
| 71 | /** |
| 72 | * A page on `pageUrl` named these icons (best first). Fetches the first usable one unless the site's icon is fresh. |
| 73 | * Resolves to the new icon when it changed, else null. |
| 74 | */ |
| 75 | async learn(pageUrl: string, candidates: string[]): Promise<string | null> { |
| 76 | const host = hostOf(pageUrl); |
| 77 | if (!host) return null; |
| 78 | const now = this.deps.now(); |
| 79 | const had = this.map.get(host); |
| 80 | if (had) { had.seen = now; this.saveSoon(); } |
| 81 | if (had && now - had.at < REFRESH_MS) return null; |
| 82 | if ((this.failed.get(host) ?? -Infinity) > now - RETRY_MS || this.busy.has(host)) return null; |
| 83 | const url = candidates.find((c) => /^(https?:|data:image\/)/i.test(c)); |
| 84 | if (!url) return null; |
| 85 | this.busy.add(host); |
| 86 | try { |
| 87 | const icon = await this.toDataUrl(url); |
| 88 | if (!icon) { this.failed.set(host, now); if (had) had.at = now; return null; } |
| 89 | this.map.set(host, { icon, at: now, seen: now }); |
| 90 | this.trim(); |
| 91 | this.saveSoon(); |
| 92 | return had && had.icon === icon ? null : icon; |
| 93 | } finally { this.busy.delete(host); } |
| 94 | } |
| 95 | |
| 96 | /** Forget one site's icon (forgetting the site from history), or every icon. */ |
| 97 | forget(host?: string): void { |
| 98 | if (host === undefined) { this.map.clear(); this.failed.clear(); this.flush(); return; } |
| 99 | if (this.map.delete(host.toLowerCase())) this.flush(); |
| 100 | } |
| 101 | |
| 102 | flush(): void { |
| 103 | if (this.saveTimer) { clearTimeout(this.saveTimer); this.saveTimer = null; } |
| 104 | this.deps.save(this.map.size ? JSON.stringify({ v: 1, icons: Object.fromEntries(this.map) }) : null); |
| 105 | } |
| 106 | |
| 107 | private saveSoon(): void { |
| 108 | if (this.saveTimer) return; |
| 109 | this.saveTimer = setTimeout(() => { this.saveTimer = null; this.flush(); }, 2000); |
| 110 | (this.saveTimer as { unref?: () => void }).unref?.(); |
| 111 | } |
| 112 | |
| 113 | private trim(): void { |
| 114 | if (this.map.size <= MAX_HOSTS) return; |
| 115 | const oldest = [...this.map].sort((a, b) => a[1].seen - b[1].seen).slice(0, this.map.size - MAX_HOSTS); |
| 116 | for (const [h] of oldest) this.map.delete(h); |
| 117 | } |
| 118 | |
| 119 | private async toDataUrl(url: string): Promise<string | null> { |
| 120 | let f: Fetched | null; |
| 121 | if (/^data:/i.test(url)) { |
| 122 | const m = /^data:([^;,]+)(;base64)?,(.*)$/is.exec(url); |
| 123 | if (!m) return null; |
| 124 | const [, mime = "", b64, body = ""] = m; |
| 125 | let bytes: Uint8Array; |
| 126 | try { bytes = b64 ? Buffer.from(body, "base64") : Buffer.from(decodeURIComponent(body)); } catch { return null; } |
| 127 | f = { bytes, type: mime }; |
| 128 | } else f = await this.deps.fetch(url); |
| 129 | if (!f || !f.bytes.length) return null; |
| 130 | const type = sniff(f.bytes) ?? (f.type.split(";")[0] ?? "").trim().toLowerCase(); |
| 131 | if (!IMAGE.test(type)) return null; |
| 132 | f = { bytes: f.bytes, type }; |
| 133 | const small = /png|jpeg/.test(type) ? this.deps.shrink(f) : null; |
| 134 | const out = small ?? `data:${type};base64,${Buffer.from(f.bytes).toString("base64")}`; |
| 135 | return out.length <= MAX_BYTES * 1.4 ? out : null; |
| 136 | } |
| 137 | } |