-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(desktop): add favicons to the Browser panel #5644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrisdeeming
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
chrisdeeming:browser-favicons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,916
−277
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
|
macroscopeapp[bot] marked this conversation as resolved.
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| BROWSER_FAVICON_MAX_ENTRIES, | ||
| type BrowserFaviconEntry, | ||
| evictExcessFavicons, | ||
| faviconKey, | ||
| isStorableFaviconDataUrl, | ||
| migratePersistedBrowserFaviconState, | ||
| } from "./browserFaviconLogic"; | ||
|
|
||
| function entry(updatedAt = 0): BrowserFaviconEntry { | ||
| return { dataUrl: "data:image/png;base64,AAAA", updatedAt }; | ||
| } | ||
|
|
||
| describe("faviconKey", () => { | ||
| it("combines project key with the canonical origin", () => { | ||
| expect(faviconKey("proj-a", "http://myapp.test:3000/admin?x=1", null)).toBe( | ||
| "proj-a http://myapp.test:3000", | ||
| ); | ||
| }); | ||
|
|
||
| it("collapses loopback hosts and the environment host to the same key", () => { | ||
| const viaResolvedHost = faviconKey("proj-a", "http://192.168.64.2:3000/", "192.168.64.2"); | ||
| const viaLocalhost = faviconKey("proj-a", "http://localhost:3000/", "192.168.64.2"); | ||
| expect(viaResolvedHost).not.toBeNull(); | ||
| expect(viaResolvedHost).toBe(viaLocalhost); | ||
| }); | ||
|
|
||
| it("collapses loopback even when there is no connected environment", () => { | ||
| const a = faviconKey("proj-a", "http://localhost:3000/", null); | ||
| const b = faviconKey("proj-a", "http://127.0.0.1:3000/", null); | ||
| expect(a).not.toBeNull(); | ||
| expect(a).toBe(b); | ||
| }); | ||
|
|
||
| it("does not collapse an unrelated remote host or a different LAN device", () => { | ||
| const remote = faviconKey("proj-a", "http://example.com:3000/", "192.168.64.2"); | ||
| const otherLan = faviconKey("proj-a", "http://192.168.1.50:3000/", "192.168.64.2"); | ||
| const local = faviconKey("proj-a", "http://localhost:3000/", "192.168.64.2"); | ||
| expect(remote).not.toBeNull(); | ||
| expect(otherLan).not.toBeNull(); | ||
| expect(remote).not.toBe(otherLan); | ||
| expect(remote).not.toBe(local); | ||
| expect(otherLan).not.toBe(local); | ||
| }); | ||
|
|
||
| it("separates ports, schemes, and projects", () => { | ||
| expect(faviconKey("proj-a", "http://localhost:3000/", null)).not.toBe( | ||
| faviconKey("proj-a", "http://localhost:5173/", null), | ||
| ); | ||
| expect(faviconKey("proj-a", "http://myapp.test/", null)).not.toBe( | ||
| faviconKey("proj-a", "https://myapp.test/", null), | ||
| ); | ||
| expect(faviconKey("proj-a", "http://localhost:3000/", null)).not.toBe( | ||
| faviconKey("proj-b", "http://localhost:3000/", null), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects non-http(s) and unparseable urls", () => { | ||
| expect(faviconKey("proj-a", "ftp://example.com/", null)).toBeNull(); | ||
| expect(faviconKey("proj-a", "not a url", null)).toBeNull(); | ||
| expect(faviconKey("", "http://localhost:3000/", null)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isStorableFaviconDataUrl", () => { | ||
| it("accepts image data urls within the cap", () => { | ||
| expect(isStorableFaviconDataUrl("data:image/png;base64,AAAA")).toBe(true); | ||
| expect(isStorableFaviconDataUrl("data:image/svg+xml;base64,AAAA")).toBe(true); | ||
| expect(isStorableFaviconDataUrl("data:image/x-icon;base64,AAAA")).toBe(true); | ||
| }); | ||
|
|
||
| it("rejects non-image data urls, other schemes, and oversized values", () => { | ||
| expect(isStorableFaviconDataUrl("data:text/html;base64,AAAA")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/svg+xml,<svg></svg>")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("http://example.com/favicon.ico")).toBe(false); | ||
| expect(isStorableFaviconDataUrl(42)).toBe(false); | ||
| expect(isStorableFaviconDataUrl(`data:image/png;base64,${"A".repeat(8192)}`)).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects data urls with no payload", () => { | ||
| expect(isStorableFaviconDataUrl("data:image/x-icon;base64,")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/png;base64,")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/png;base64, ")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/png;base64,%%%%")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/png;base64,AAA\n")).toBe(false); | ||
| expect(isStorableFaviconDataUrl("data:image/png")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("evictExcessFavicons", () => { | ||
| it("keeps the most recently updated entries when over the cap", () => { | ||
| const byKey = Object.fromEntries( | ||
| Array.from({ length: BROWSER_FAVICON_MAX_ENTRIES + 2 }, (_, i) => [`k-${i}`, entry(i)]), | ||
| ); | ||
| const next = evictExcessFavicons(byKey); | ||
| expect(Object.keys(next)).toHaveLength(BROWSER_FAVICON_MAX_ENTRIES); | ||
| expect(next["k-0"]).toBeUndefined(); | ||
| expect(next["k-1"]).toBeUndefined(); | ||
| expect(next[`k-${BROWSER_FAVICON_MAX_ENTRIES + 1}`]).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("migratePersistedBrowserFaviconState", () => { | ||
| it("returns empty state for junk payloads", () => { | ||
| expect(migratePersistedBrowserFaviconState(null)).toEqual({ byKey: {} }); | ||
| expect(migratePersistedBrowserFaviconState("nope")).toEqual({ byKey: {} }); | ||
| expect(migratePersistedBrowserFaviconState({ byKey: 42 })).toEqual({ byKey: {} }); | ||
| }); | ||
|
|
||
| it("drops entries with invalid data urls or timestamps", () => { | ||
| const migrated = migratePersistedBrowserFaviconState({ | ||
| byKey: { | ||
| good: { dataUrl: "data:image/png;base64,AAAA", updatedAt: 10 }, | ||
| badScheme: { dataUrl: "http://example.com/i.ico", updatedAt: 10 }, | ||
| badTime: { dataUrl: "data:image/png;base64,AAAA", updatedAt: Number.NaN }, | ||
| notAnObject: "junk", | ||
| }, | ||
| }); | ||
| expect(migrated.byKey).toEqual({ | ||
| good: { dataUrl: "data:image/png;base64,AAAA", updatedAt: 10 }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { FAVICON_DATA_URL_MAX_LENGTH } from "@t3tools/contracts"; | ||
|
|
||
| import { isLocalLoopbackHost, normalizeHostname } from "./browser/browserTargetResolver"; | ||
|
|
||
| export type BrowserFaviconEntry = { dataUrl: string; updatedAt: number }; | ||
|
|
||
| export const BROWSER_FAVICON_MAX_DATA_URL_LENGTH = FAVICON_DATA_URL_MAX_LENGTH; | ||
| export const BROWSER_FAVICON_MAX_ENTRIES = 40; | ||
|
|
||
| export function faviconKey( | ||
| projectRefKey: string, | ||
| url: string, | ||
| environmentHostname: string | null, | ||
| ): string | null { | ||
| if (projectRefKey.length === 0) return null; | ||
| try { | ||
| const parsed = new URL(url); | ||
| if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; | ||
| const host = normalizeHostname(parsed.hostname); | ||
| const collapsesToLocal = | ||
| isLocalLoopbackHost(host) || | ||
| host === "0.0.0.0" || | ||
| (environmentHostname !== null && host === normalizeHostname(environmentHostname)); | ||
| const port = parsed.port || (parsed.protocol === "https:" ? "443" : "80"); | ||
| const canonicalHost = collapsesToLocal ? "local" : host; | ||
| return `${projectRefKey} ${parsed.protocol}//${canonicalHost}:${port}`; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export function isStorableFaviconDataUrl(value: unknown): value is string { | ||
| if ( | ||
| typeof value !== "string" || | ||
| !/^data:image\/[a-z0-9.+-]+;base64,/i.test(value) || | ||
| value.length > BROWSER_FAVICON_MAX_DATA_URL_LENGTH | ||
| ) { | ||
| return false; | ||
| } | ||
| const commaIndex = value.indexOf(","); | ||
| if (commaIndex === -1) return false; | ||
| const payload = value.slice(commaIndex + 1); | ||
| return ( | ||
| payload.length > 0 && | ||
| payload.length % 4 !== 1 && | ||
| !/[^a-z0-9+/=]/i.test(payload) && | ||
| /^[a-z0-9+/]*={0,2}$/i.test(payload) | ||
| ); | ||
| } | ||
|
|
||
| export function evictExcessFavicons( | ||
| byKey: Record<string, BrowserFaviconEntry>, | ||
| ): Record<string, BrowserFaviconEntry> { | ||
| const keys = Object.keys(byKey); | ||
| if (keys.length <= BROWSER_FAVICON_MAX_ENTRIES) return byKey; | ||
| const kept = keys | ||
| .toSorted((a, b) => (byKey[b]?.updatedAt ?? 0) - (byKey[a]?.updatedAt ?? 0)) | ||
| .slice(0, BROWSER_FAVICON_MAX_ENTRIES); | ||
| return Object.fromEntries(kept.map((key) => [key, byKey[key] as BrowserFaviconEntry])); | ||
| } | ||
|
|
||
| export function migratePersistedBrowserFaviconState(persistedState: unknown): { | ||
| byKey: Record<string, BrowserFaviconEntry>; | ||
| } { | ||
| if (!persistedState || typeof persistedState !== "object") return { byKey: {} }; | ||
| const raw = "byKey" in persistedState ? (persistedState as { byKey?: unknown }).byKey : null; | ||
| if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { byKey: {} }; | ||
| const byKey: Record<string, BrowserFaviconEntry> = {}; | ||
| for (const [key, value] of Object.entries(raw as Record<string, unknown>)) { | ||
| if (!value || typeof value !== "object") continue; | ||
| const { dataUrl, updatedAt } = value as Record<string, unknown>; | ||
| if (!isStorableFaviconDataUrl(dataUrl)) continue; | ||
| if (typeof updatedAt !== "number" || !Number.isFinite(updatedAt)) continue; | ||
| byKey[key] = { dataUrl, updatedAt }; | ||
| } | ||
| return { byKey: evictExcessFavicons(byKey) }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.