/** * Recognises links to this site's own pages. The self provider previews them * from the database, and remote/fetch.ts accepts them as dump links even on a * private host such as localhost. * * @module */ import { FRONTEND_URL, PUBLIC_URL } from "../config.ts"; const SELF_HOSTS = new Set( [PUBLIC_URL, FRONTEND_URL].flatMap((u) => { try { return [new URL(u).host.toLowerCase()]; } catch { return []; } }), ); const SELF_PATH_RE = /^\/(dumps|users|playlists)\/[^/]+$/; /** * Checks whether `url` links to one of this site's own dump, user or playlist * pages. Matched on the host as well as the path: `/users/bob` on some other * site is someone else's page, not ours. */ export function isSelfUrl(url: URL): boolean { return SELF_HOSTS.has(url.host.toLowerCase()) && SELF_PATH_RE.test(url.pathname); }