chadtmiller.com/pds.js
An atproto PDS in JavaScript, for Cloudflare Workers and Node
0
0
// @pdsjs/sites - hostname → site-name resolution
//
// A site's record key is a DNS label, and the label is the subdomain it
// serves on: `blog` serves at `blog.<siteDomain>`. The apex serves the site
// named by `apexName`. Hostnames outside `siteDomain` are not ours at all —
// the caller falls through to normal PDS dispatch for them.
/**
* One DNS label: 1-63 chars, lowercase alphanumeric with interior hyphens.
* Site rkeys must match this because they become subdomains.
*/
const LABEL = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/;
/**
* Resolve a request hostname to the site name it serves.
*
* @param {string} hostname - request hostname, may carry mixed case
* @param {string} siteDomain - apex domain sites serve under
* @param {string} apexName - site name bound to the apex itself
* @returns {{site: string}|{notFound: true}|null} The site to serve; a
* claimed-but-unservable hostname (deeper subdomains, malformed labels)
* that should get the sites 404 page; or null when the hostname is not
* under the site domain and the request is not ours.
*/
export function hostnameToSite(hostname, siteDomain, apexName) {
const host = hostname.toLowerCase().replace(/\.$/, '');
const domain = siteDomain.toLowerCase();
if (host === domain) return { site: apexName };
if (!host.endsWith(`.${domain}`)) return null;
const label = host.slice(0, -(domain.length + 1));
// Multi-label subdomains are claimed by the wildcard route but carry no
// site (and sit outside the one-level Universal SSL wildcard anyway).
if (label.includes('.') || !LABEL.test(label)) return { notFound: true };
return { site: label };
}
/**
* Whether a site name is usable as a subdomain label.
* @param {string} name
* @returns {boolean}
*/
export function isValidSiteName(name) {
return LABEL.test(name);
}