// THE CONTENT ROUTE'S ADDRESS (architecture, "Screens", "A page at a World's place"): at a World's place, what a page
// reaches on another of the pack's hosts that a content screen serves is `/@/`. This reads that
// address as a browser would have written it: a doubled slash collapsed, `%40` for `@`, the host's case, a trailing dot
// and a default port (80, 443) normalised away. What it reads is a candidate only: the route is taken when the host is
// one a built content screen names (pack-fetch.ts), and a request naming no such host is general routing's, as before.
/** A `/@/` address: the host normalised, the path under it (slashes collapsed), and why the spelling cannot
* be the host's content (`refused`: a port other than the default), when it cannot. */
export type ContentAddress = { host: string; path: string; refused?: string };
export function contentAddressOf(pathname: string): ContentAddress | undefined {
const collapsed = pathname.replace(/\/{2,}/g, '/');
const named = /^\/(?:@|%40)([^/]+)(\/.*)?$/i.exec(collapsed);
if (!named) return undefined;
let token: string;
try { token = decodeURIComponent(named[1]!).toLowerCase(); } catch { return undefined; }
const at = /^([^:]*)(?::(\d*))?$/.exec(token);
if (!at) return undefined;
const host = at[1]!.replace(/\.$/, '');
if (!/^[a-z0-9_-]+(?:\.[a-z0-9_-]+)+$/.test(host)) return undefined;
const port = at[2];
const refused = port && port !== '80' && port !== '443' ? `port ${port} is not the host's` : undefined;
return { host, path: named[2] ?? '/', ...(refused ? { refused } : {}) };
}