{"version":3,"file":"check-has-field.cjs","sources":["../../../src/router/utils/check-has-field.ts"],"sourcesContent":["import { applyPCREMatches, matchPCRE } from '../pcre';\nimport type { SourceRouteHasField } from '../types';\n\n/**\n * Gets the has field PCRE match results, and tries to apply any named capture groups to a\n * route destination.\n *\n * @param hasValue The has field value to match against.\n * @param foundValue The value found in the request.\n * @param routeDest Destination to apply match to.\n * @returns Whether the match is valid, and the destination with the match applied.\n */\nfunction getHasFieldPCREMatchResult(\n\thasValue: string,\n\tfoundValue: string | null,\n\trouteDest?: string,\n): { valid: boolean; newRouteDest?: string } {\n\tconst { match, captureGroupKeys } = matchPCRE(hasValue, foundValue);\n\n\tif (routeDest && match && captureGroupKeys.length) {\n\t\treturn {\n\t\t\tvalid: !!match,\n\t\t\tnewRouteDest: applyPCREMatches(routeDest, match, captureGroupKeys, {\n\t\t\t\tnamedOnly: true,\n\t\t\t}),\n\t\t};\n\t}\n\n\treturn { valid: !!match };\n}\n\ntype HasFieldRequestProperties = {\n\turl: URL;\n\tcookies: Record<string, string>;\n\theaders: Headers;\n\trouteDest?: string;\n};\n\n/**\n * Checks if a source route's `has` record conditions match a request, and whether the request\n * destination should be updated based on the `has` record.\n *\n * @param has The `has` record conditions to check against the request.\n * @param requestProperties The request properties to check against.\n * @returns Whether the request matches the `has` record conditions, and the new destination if it changed.\n */\nexport function checkHasField(\n\thas: SourceRouteHasField,\n\t{ url, cookies, headers, routeDest }: HasFieldRequestProperties,\n): { valid: boolean; newRouteDest?: string } {\n\t// eslint-disable-next-line default-case\n\tswitch (has.type) {\n\t\tcase 'host': {\n\t\t\treturn { valid: url.hostname === has.value };\n\t\t}\n\t\tcase 'header': {\n\t\t\tif (has.value !== undefined) {\n\t\t\t\treturn getHasFieldPCREMatchResult(has.value, headers.get(has.key), routeDest);\n\t\t\t}\n\n\t\t\treturn { valid: headers.has(has.key) };\n\t\t}\n\t\tcase 'cookie': {\n\t\t\tconst cookie = cookies[has.key];\n\n\t\t\tif (cookie && has.value !== undefined) {\n\t\t\t\treturn getHasFieldPCREMatchResult(has.value, cookie, routeDest);\n\t\t\t}\n\n\t\t\treturn { valid: cookie !== undefined };\n\t\t}\n\t\tcase 'query': {\n\t\t\tif (has.value !== undefined) {\n\t\t\t\treturn getHasFieldPCREMatchResult(has.value, url.searchParams.get(has.key), routeDest);\n\t\t\t}\n\n\t\t\treturn { valid: url.searchParams.has(has.key) };\n\t\t}\n\t}\n}\n"],"names":["getHasFieldPCREMatchResult","hasValue","foundValue","routeDest","match","captureGroupKeys","matchPCRE","applyPCREMatches","checkHasField","has","url","cookies","headers","cookie"],"mappings":"sKAYA,SAASA,EACRC,EACAC,EACAC,EAC4C,CAC5C,KAAM,CAAE,MAAAC,EAAO,iBAAAC,CAAA,EAAqBC,EAAAA,UAAUL,EAAUC,CAAU,EAE9D,OAAAC,GAAaC,GAASC,EAAiB,OACnC,CACN,MAAO,CAAC,CAACD,EACT,aAAcG,EAAA,iBAAiBJ,EAAWC,EAAOC,EAAkB,CAClE,UAAW,EAAA,CACX,CAAA,EAII,CAAE,MAAO,CAAC,CAACD,EACnB,CAiBO,SAASI,EACfC,EACA,CAAE,IAAAC,EAAK,QAAAC,EAAS,QAAAC,EAAS,UAAAT,GACmB,CAE5C,OAAQM,EAAI,KAAM,CACjB,IAAK,OACJ,MAAO,CAAE,MAAOC,EAAI,WAAaD,EAAI,KAAM,EAE5C,IAAK,SACA,OAAAA,EAAI,QAAU,OACVT,EAA2BS,EAAI,MAAOG,EAAQ,IAAIH,EAAI,GAAG,EAAGN,CAAS,EAGtE,CAAE,MAAOS,EAAQ,IAAIH,EAAI,GAAG,GAEpC,IAAK,SAAU,CACR,MAAAI,EAASF,EAAQF,EAAI,GAAG,EAE1B,OAAAI,GAAUJ,EAAI,QAAU,OACpBT,EAA2BS,EAAI,MAAOI,EAAQV,CAAS,EAGxD,CAAE,MAAOU,IAAW,MAAU,CACtC,CACA,IAAK,QACA,OAAAJ,EAAI,QAAU,OACVT,EAA2BS,EAAI,MAAOC,EAAI,aAAa,IAAID,EAAI,GAAG,EAAGN,CAAS,EAG/E,CAAE,MAAOO,EAAI,aAAa,IAAID,EAAI,GAAG,EAE9C,CACD"}