{"version":3,"file":"patterns-D9iDO3j8.mjs","names":[],"sources":["../src/redirects/patterns.ts"],"sourcesContent":["/**\n * URL pattern matching for redirects.\n *\n * Uses Astro's route syntax: [param] for named segments, [...rest] for catch-all.\n * Compiles patterns to safe regexes -- no user-supplied regex, no ReDoS risk.\n *\n * @example\n * ```ts\n * const compiled = compilePattern(\"/old-blog/[...path]\");\n * const match = matchPattern(compiled, \"/old-blog/2024/01/post\");\n * // match = { path: \"2024/01/post\" }\n *\n * interpolateDestination(\"/blog/[...path]\", match);\n * // \"/blog/2024/01/post\"\n * ```\n */\n\n/** Matches [paramName] placeholders */\nconst PARAM_PATTERN = /\\[(\\w+)\\]/g;\n\n/** Matches [...splatName] placeholders */\nconst SPLAT_PATTERN = /\\[\\.\\.\\.(\\w+)\\]/g;\n\n/** Combined pattern for validation: matches both [param] and [...splat] */\nconst ANY_PLACEHOLDER = /\\[(?:\\.\\.\\.)?(\\w+)\\]/g;\n\n/** Nested brackets check: [foo[ */\nconst NESTED_BRACKETS = /\\[[^\\]]*\\[/;\n\n/** Empty brackets: [] */\nconst EMPTY_BRACKETS = /\\[\\]/;\n\n/** Count open brackets */\nconst OPEN_BRACKET = /\\[/g;\n\n/** Count close brackets */\nconst CLOSE_BRACKET = /\\]/g;\n\n/** Split on capture groups in compiled regex string */\nconst CAPTURE_GROUP_SPLIT = /(\\([^)]+\\))/;\n\n/** Escape regex-special characters in literal parts */\nconst REGEX_SPECIAL_CHARS = /[.*+?^${}|\\\\]/g;\n\nexport interface CompiledPattern {\n\tregex: RegExp;\n\tparamNames: string[];\n\tsource: string;\n}\n\n/**\n * Returns true if a source string contains [param] or [...splat] placeholders.\n */\nexport function isPattern(source: string): boolean {\n\t// Use match() instead of test() to avoid lastIndex issues with the global regex\n\treturn source.match(ANY_PLACEHOLDER) !== null;\n}\n\n/**\n * Validate that a pattern string is well-formed.\n * Returns null if valid, or an error message if invalid.\n */\nexport function validatePattern(source: string): string | null {\n\tif (!source.startsWith(\"/\")) {\n\t\treturn \"Pattern must start with /\";\n\t}\n\n\t// Check for nested brackets\n\tif (NESTED_BRACKETS.test(source)) {\n\t\treturn \"Nested brackets are not allowed\";\n\t}\n\n\t// Check for empty brackets\n\tif (EMPTY_BRACKETS.test(source)) {\n\t\treturn \"Empty brackets are not allowed\";\n\t}\n\n\t// Check for unmatched brackets\n\tconst openCount = (source.match(OPEN_BRACKET) ?? []).length;\n\tconst closeCount = (source.match(CLOSE_BRACKET) ?? []).length;\n\tif (openCount !== closeCount) {\n\t\treturn \"Unmatched brackets\";\n\t}\n\n\t// Check that [...splat] is only in the last segment\n\tconst segments = source.split(\"/\").filter(Boolean);\n\tfor (let i = 0; i < segments.length; i++) {\n\t\tconst segment = segments[i];\n\t\tif (SPLAT_PATTERN.test(segment) && i !== segments.length - 1) {\n\t\t\tSPLAT_PATTERN.lastIndex = 0;\n\t\t\treturn \"Catch-all [...param] must be in the last segment\";\n\t\t}\n\t\tSPLAT_PATTERN.lastIndex = 0;\n\t}\n\n\t// Check that a segment is either all literal or a single placeholder\n\tfor (const segment of segments) {\n\t\tconst placeholders = segment.match(ANY_PLACEHOLDER);\n\t\tif (placeholders && placeholders.length > 1) {\n\t\t\treturn \"Each segment can contain at most one placeholder\";\n\t\t}\n\t\tif (placeholders && placeholders[0] !== segment) {\n\t\t\treturn \"A placeholder must be the entire segment, not mixed with literal text\";\n\t\t}\n\t}\n\n\t// Check for duplicate param names\n\tconst names: string[] = [];\n\tfor (const m of source.matchAll(ANY_PLACEHOLDER)) {\n\t\tconst name = m[1];\n\t\tif (names.includes(name)) {\n\t\t\treturn `Duplicate parameter name: ${name}`;\n\t\t}\n\t\tnames.push(name);\n\t}\n\n\treturn null;\n}\n\n/**\n * Validate that all placeholders in a destination exist in the source.\n * Returns null if valid, or an error message if invalid.\n */\nexport function validateDestinationParams(source: string, destination: string): string | null {\n\tconst sourceNames = new Set<string>();\n\tfor (const m of source.matchAll(ANY_PLACEHOLDER)) {\n\t\tsourceNames.add(m[1]);\n\t}\n\n\tfor (const m of destination.matchAll(ANY_PLACEHOLDER)) {\n\t\tconst name = m[1];\n\t\tif (!sourceNames.has(name)) {\n\t\t\treturn `Destination references [${name}] which is not captured in the source pattern`;\n\t\t}\n\t}\n\n\treturn null;\n}\n\n/**\n * Compile a URL pattern into a regex for matching.\n *\n * - `[param]` matches a single path segment (`[^/]+`)\n * - `[...rest]` matches one or more remaining segments (`.+`)\n */\nexport function compilePattern(source: string): CompiledPattern {\n\tconst paramNames: string[] = [];\n\n\t// Replace [...splat] first (before [param]) since [...x] contains [x]\n\tlet regexStr = source.replace(SPLAT_PATTERN, (_match, name: string) => {\n\t\tparamNames.push(name);\n\t\treturn \"(.+)\";\n\t});\n\n\t// Then replace [param]\n\tregexStr = regexStr.replace(PARAM_PATTERN, (_match, name: string) => {\n\t\tparamNames.push(name);\n\t\treturn \"([^/]+)\";\n\t});\n\n\t// Escape any regex-special characters in the literal parts\n\t// We need to be careful: the replacement groups are already valid regex\n\t// Split on capture groups, escape literals, rejoin\n\tconst parts = regexStr.split(CAPTURE_GROUP_SPLIT);\n\tconst escaped = parts\n\t\t.map((part, i) => {\n\t\t\t// Odd indices are the capture groups -- leave them alone\n\t\t\tif (i % 2 === 1) return part;\n\t\t\t// Even indices are literal text -- escape special regex chars\n\t\t\treturn part.replace(REGEX_SPECIAL_CHARS, \"\\\\$&\");\n\t\t})\n\t\t.join(\"\");\n\n\treturn {\n\t\tregex: new RegExp(`^${escaped}$`),\n\t\tparamNames,\n\t\tsource,\n\t};\n}\n\n/**\n * Match a path against a compiled pattern.\n * Returns captured params or null if no match.\n */\nexport function matchPattern(\n\tcompiled: CompiledPattern,\n\tpath: string,\n): Record<string, string> | null {\n\tconst match = path.match(compiled.regex);\n\tif (!match) return null;\n\n\tconst params: Record<string, string> = {};\n\tfor (let i = 0; i < compiled.paramNames.length; i++) {\n\t\tconst value = match[i + 1];\n\t\tif (value !== undefined) {\n\t\t\tparams[compiled.paramNames[i]] = value;\n\t\t}\n\t}\n\treturn params;\n}\n\n/**\n * Interpolate captured params into a destination pattern.\n *\n * @example\n * interpolateDestination(\"/blog/[...path]\", { path: \"2024/01/post\" })\n * // \"/blog/2024/01/post\"\n */\nexport function interpolateDestination(\n\tdestination: string,\n\tparams: Record<string, string>,\n): string {\n\t// Replace [...splat] first\n\tlet result = destination.replace(SPLAT_PATTERN, (_match, name: string) => {\n\t\treturn params[name] ?? \"\";\n\t});\n\n\t// Then [param]\n\tresult = result.replace(PARAM_PATTERN, (_match, name: string) => {\n\t\treturn params[name] ?? \"\";\n\t});\n\n\treturn result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,MAAM,gBAAgB;;AAGtB,MAAM,gBAAgB;;AAGtB,MAAM,kBAAkB;;AAGxB,MAAM,kBAAkB;;AAGxB,MAAM,iBAAiB;;AAGvB,MAAM,eAAe;;AAGrB,MAAM,gBAAgB;;AAGtB,MAAM,sBAAsB;;AAG5B,MAAM,sBAAsB;;;;AAW5B,SAAgB,UAAU,QAAyB;AAElD,QAAO,OAAO,MAAM,gBAAgB,KAAK;;;;;;AAO1C,SAAgB,gBAAgB,QAA+B;AAC9D,KAAI,CAAC,OAAO,WAAW,IAAI,CAC1B,QAAO;AAIR,KAAI,gBAAgB,KAAK,OAAO,CAC/B,QAAO;AAIR,KAAI,eAAe,KAAK,OAAO,CAC9B,QAAO;AAMR,MAFmB,OAAO,MAAM,aAAa,IAAI,EAAE,EAAE,YACjC,OAAO,MAAM,cAAc,IAAI,EAAE,EAAE,OAEtD,QAAO;CAIR,MAAM,WAAW,OAAO,MAAM,IAAI,CAAC,OAAO,QAAQ;AAClD,MAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;EACzC,MAAM,UAAU,SAAS;AACzB,MAAI,cAAc,KAAK,QAAQ,IAAI,MAAM,SAAS,SAAS,GAAG;AAC7D,iBAAc,YAAY;AAC1B,UAAO;;AAER,gBAAc,YAAY;;AAI3B,MAAK,MAAM,WAAW,UAAU;EAC/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