// A site's hostname can itself be an atproto handle. When it is, the popup // can say so even on a page with no standard.site publication — which is the // question "is this site on the Atmosphere?" asked from the other direction. // // This is a popup-time lookup, not part of detection: it runs once when the // popup opens on a page detection found no publication on, and never in the // worker. A page with a publication already names the account behind it in the // owner card; a page without one costs one resolveHandle against the public // appview, cached by src/lib/atproto.ts. import { resolveHandleToDid } from './atproto' export interface SiteAccount { handle: string did: string } /** * Whether the page's own profile deserves a card. It does when the host is an * account and is not the publication's author: on a publication's own home * those are the same account, and one card already says everything. */ export function showsSiteProfile( site: SiteAccount | undefined, pubDid: string | undefined, ): boolean { return !!site && (!pubDid || site.did !== pubDid) } /** * Whether the publication's account needs an "Author" label. Only when a * separate site profile is on screen — the label exists to tell the two * accounts apart, not to describe the only one. */ export function showsAuthorRole( site: SiteAccount | undefined, pubDid: string | undefined, ): boolean { return !!site && !!pubDid && site.did !== pubDid } const IPV4 = /^\d{1,3}(\.\d{1,3}){3}$/ /** Reserved or local suffixes that are never registered atproto handles. */ const NOT_A_HANDLE = /\.(invalid|localhost|local|test|onion)$/i /** * The hostname worth asking about, or undefined for anything that cannot be a * handle by construction. The resolver has the final say; this only keeps IP * literals and local names off the network. */ export function handleCandidate(hostname: string): string | undefined { const host = hostname.trim().toLowerCase().replace(/\.$/, '') if (!host.includes('.') || IPV4.test(host)) return undefined // IPv6 literals arrive bracketed from URL.hostname; a bare colon cannot // appear in a handle either. if (host.includes(':') || host.includes('[')) return undefined if (NOT_A_HANDLE.test(host)) return undefined return host } /** * The account this site belongs to, if its hostname (or the apex behind a * `www.` prefix) resolves as a handle. Null means "not an account", which * includes every ordinary site and every failed lookup: this is an * informational answer, never an error. */ export async function resolveSiteAccount(url: string): Promise { let hostname: string try { hostname = new URL(url).hostname } catch { return null } const candidate = handleCandidate(hostname) if (!candidate) return null // Try the host itself first. `www.` is commonly a CNAME, not the account's // handle, so the apex is the fallback rather than the other way round. const handles = [candidate] if (candidate.startsWith('www.')) handles.push(candidate.slice('www.'.length)) for (const handle of handles) { try { return { handle, did: await resolveHandleToDid(handle) } } catch { // The resolver saying "no" is the answer for an ordinary site, not an // error worth logging on every page without a publication. } } return null }