UNPKG

astro

Version:

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

38 lines (37 loc) • 1.24 kB
import { collapseDuplicateSlashes } from "@astrojs/internal-helpers/path"; import { validateAndDecodePathname } from "./pathname.js"; /** * Creates a normalized URL from a request URL string. * Decodes and validates the pathname, collapses duplicate slashes. */ export function createNormalizedUrl(requestUrl) { return normalizeUrl(new URL(requestUrl)); } /** * Assigns `url.pathname` only when the value differs. * The setter re-parses the whole URL, so a no-op write is still expensive. */ export function setPathname(url, pathname) { if (url.pathname !== pathname) { url.pathname = pathname; } } /** * Normalizes an already-parsed URL in place: decodes and validates the * pathname, collapses duplicate slashes. Returns the same URL object. * * Collapse runs after the decode is written back: the pathname setter * rewrites `\` to `/`, so a decoded backslash only becomes `//` once assigned. */ export function normalizeUrl(url) { try { setPathname(url, validateAndDecodePathname(url.pathname)); } catch { // For decoding failures (truly malformed URLs), fall back gracefully. try { setPathname(url, decodeURI(url.pathname)); } catch {} } setPathname(url, collapseDuplicateSlashes(url.pathname)); return url; }