{"version":3,"file":"pathMatcher.mjs","names":[],"sources":["../src/pathMatcher.ts"],"sourcesContent":["import { pathToRegexp } from './pathToRegexp';\nimport type { Autocomplete } from './types';\n\n/**\n * @deprecated Prefer {@link WithPathSegmentWildcard}; `(.*)` also matches sibling routes\n * (e.g. `/dashboard(.*)` matches `/dashboardxyz`).\n */\nexport type WithPathPatternWildcard<T = string> = `${T & string}(.*)`;\n\ntype StripTrailingSlash<T extends string> = T extends '/'\n  ? T\n  : T extends `${infer Prefix}/`\n    ? StripTrailingSlash<Prefix>\n    : T;\n\n/**\n * Suggests the `:path*` subtree form (e.g. `/dashboard/:path*`), which matches on\n * path-segment boundaries. `/` is special-cased to `/:path*` to avoid a malformed `//:path*`.\n *\n * @deprecated Will be removed in the next major version together with {@link createPathMatcher}.\n */\nexport type WithPathSegmentWildcard<T = string> = T extends '/'\n  ? '/:path*'\n  : `${StripTrailingSlash<T & string>}/:path*`;\n/**\n * @deprecated Will be removed in the next major version together with {@link createPathMatcher}.\n */\nexport type PathPattern = Autocomplete<WithPathSegmentWildcard>;\n/**\n * @deprecated Will be removed in the next major version together with {@link createPathMatcher}.\n */\nexport type PathMatcherParam = Array<RegExp | PathPattern> | RegExp | PathPattern;\n\nexport class MalformedURLError extends Error {\n  public readonly statusCode = 400;\n\n  constructor(pathname: string, cause?: unknown) {\n    super(`Malformed encoding in URL path: ${pathname}`);\n    this.name = 'MalformedURLError';\n    this.cause = cause;\n  }\n}\n\n/**\n * String-based check for MalformedURLError that works across package bundles\n * where `instanceof` would fail due to duplicate class identities.\n */\nexport function isMalformedURLError(e: unknown): e is MalformedURLError {\n  return e instanceof Error && e.name === 'MalformedURLError';\n}\n\nconst precomputePathRegex = (patterns: Array<string | RegExp>) => {\n  return patterns.map(pattern => (pattern instanceof RegExp ? pattern : pathToRegexp(pattern)));\n};\n\n/**\n * Normalizes a URL path for safe route matching.\n *\n * 1. Decodes percent-encoded unreserved characters using decodeURI (not\n *    decodeURIComponent) so path-reserved delimiters like %2F, %3F, %23\n *    are preserved — matching how framework routers interpret paths.\n * 2. Collapses consecutive slashes (e.g. //api/admin → /api/admin) to\n *    prevent bypass via extra slashes.\n *\n * @throws {MalformedURLError} if the path contains invalid percent-encoding\n */\nexport const normalizePath = (pathname: string): string => {\n  try {\n    pathname = decodeURI(pathname);\n  } catch (e) {\n    throw new MalformedURLError(pathname, e);\n  }\n  return pathname.replace(/\\/\\/+/g, '/');\n};\n\n/**\n * Creates a function that matches paths against a set of patterns.\n *\n * @param patterns - A string, RegExp, or array of patterns to match against\n * @returns A function that takes a pathname and returns true if it matches any of the patterns\n *\n * @deprecated This function will be removed in the next major version. Pattern-based path matching\n * can diverge from how frameworks route requests; use your framework's native routing primitives\n * to decide which paths to protect instead.\n */\nexport const createPathMatcher = (patterns: PathMatcherParam) => {\n  const routePatterns = [patterns || ''].flat().filter(Boolean);\n  const matchers = precomputePathRegex(routePatterns);\n  return (pathname: string) => matchers.some(matcher => matcher.test(normalizePath(pathname)));\n};\n"],"mappings":";;;AAiCA,IAAa,oBAAb,cAAuC,MAAM;CAC3C,AAAgB,aAAa;CAE7B,YAAY,UAAkB,OAAiB;EAC7C,MAAM,mCAAmC,UAAU;EACnD,KAAK,OAAO;EACZ,KAAK,QAAQ;CACf;AACF;;;;;AAMA,SAAgB,oBAAoB,GAAoC;CACtE,OAAO,aAAa,SAAS,EAAE,SAAS;AAC1C;AAEA,MAAM,uBAAuB,aAAqC;CAChE,OAAO,SAAS,KAAI,YAAY,mBAAmB,SAAS,UAAU,aAAa,OAAO,CAAE;AAC9F;;;;;;;;;;;;AAaA,MAAa,iBAAiB,aAA6B;CACzD,IAAI;EACF,WAAW,UAAU,QAAQ;CAC/B,SAAS,GAAG;EACV,MAAM,IAAI,kBAAkB,UAAU,CAAC;CACzC;CACA,OAAO,SAAS,QAAQ,UAAU,GAAG;AACvC;;;;;;;;;;;AAYA,MAAa,qBAAqB,aAA+B;CAE/D,MAAM,WAAW,oBADK,CAAC,YAAY,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,OAAO,OACJ,CAAC;CAClD,QAAQ,aAAqB,SAAS,MAAK,YAAW,QAAQ,KAAK,cAAc,QAAQ,CAAC,CAAC;AAC7F"}