31 lines
773 B
TypeScript
31 lines
773 B
TypeScript
/**
|
|
* Recognises links to this site's own pages, which previews resolve from the
|
|
* database instead of fetching.
|
|
*
|
|
* @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);
|
|
}
|