UNPKG

h3

Version:

Minimal H(TTP) framework built for high performance and portability.

43 lines (42 loc) 2.03 kB
import { normalizeRoute as normalizeRoute$1 } from "./response.mjs"; const DOT_SEGMENT_SRC = String.raw`(?:^|/)(?:\.|%(?:25)*2e){1,2}(?:/|$)`; const ENCODED_SEP_SRC = String.raw`%(?:25)*(?:2f|5c)`; const ENCODED_SEP_RE_G = /* @__PURE__ */ new RegExp(ENCODED_SEP_SRC, "gi"); const TRIGGER_RES = /* @__PURE__ */ (() => { const base = String.raw`\\|` + DOT_SEGMENT_SRC; return [ new RegExp(base, "i"), new RegExp(`${base}|${ENCODED_SEP_SRC}`, "i"), new RegExp(`${base}|//`, "i"), new RegExp(`${base}|${ENCODED_SEP_SRC}|//`, "i") ]; })(); const ENCODED_DOT_RE_G = /%(?:25)*2e/gi; function resolveDotSegments(path, opts) { if (path[0] !== "/" || path[1] === "/" || path[1] === "\\") path = "/" + path.replace(/^[/\\]+/, ""); if (isCanonicalPath(path, opts)) return path; const decodeSlashes = opts?.decodeSlashes; const mergeSlashes = opts?.mergeSlashes; let normalized = path.includes("\\") ? path.replaceAll("\\", "/") : path; if (decodeSlashes) normalized = normalized.replace(ENCODED_SEP_RE_G, "/"); const segments = normalized.split("/"); const lastIndex = segments.length - 1; const resolved = []; for (let i = 0; i <= lastIndex; i++) { const segment = segments[i]; const normalizedSegment = segment.includes("%") ? segment.replace(ENCODED_DOT_RE_G, ".") : segment; const isDotSegment = normalizedSegment === "." || normalizedSegment === ".."; if (normalizedSegment === "..") { if (resolved.length > 1) resolved.pop(); } else if (mergeSlashes && normalizedSegment === "" && i > 0 && i < lastIndex) {} else if (!isDotSegment) resolved.push(segment); if (isDotSegment && i === lastIndex) resolved.push(""); } return (resolved.join("/") || "/").replace(/^\/+/, "/"); } function isCanonicalPath(path, opts) { return path[0] === "/" && path[1] !== "/" && path[1] !== "\\" && !TRIGGER_RES[(opts?.decodeSlashes ? 1 : 0) | (opts?.mergeSlashes ? 2 : 0)].test(path); } function normalizeRoute(route) { return normalizeRoute$1(route); } export { isCanonicalPath, normalizeRoute, resolveDotSegments };