Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
6.3 KiB
Plaintext
1 line
6.3 KiB
Plaintext
{"version":3,"file":"urls.js","sources":["../../../../src/common/utils/urls.ts"],"sourcesContent":["import { getSanitizedUrlStringFromUrlObject, parseStringToURLObject } from '@sentry/core';\n\ntype ComponentRouteParams = Record<string, string> | undefined;\ntype HeadersDict = Record<string, string> | undefined;\n\nconst HeaderKeys = {\n FORWARDED_PROTO: 'x-forwarded-proto',\n FORWARDED_HOST: 'x-forwarded-host',\n HOST: 'host',\n REFERER: 'referer',\n} as const;\n\n/**\n * Replaces route parameters in a path template with their values\n * @param path - The path template containing parameters in [paramName] format\n * @param params - Optional route parameters to replace in the template\n * @returns The path with parameters replaced\n */\nexport function substituteRouteParams(path: string, params?: ComponentRouteParams): string {\n if (!params || typeof params !== 'object') return path;\n\n let resultPath = path;\n for (const [key, value] of Object.entries(params)) {\n resultPath = resultPath.split(`[${key}]`).join(encodeURIComponent(value));\n }\n return resultPath;\n}\n\n/**\n * Normalizes a path by removing route groups\n * @param path - The path to normalize\n * @returns The normalized path\n */\nexport function sanitizeRoutePath(path: string): string {\n const cleanedSegments = path\n .split('/')\n .filter(segment => segment && !(segment.startsWith('(') && segment.endsWith(')')));\n\n return cleanedSegments.length > 0 ? `/${cleanedSegments.join('/')}` : '/';\n}\n\n/**\n * Constructs a full URL from the component route, parameters, and headers.\n *\n * @param componentRoute - The route template to construct the URL from\n * @param params - Optional route parameters to replace in the template\n * @param headersDict - Optional headers containing protocol and host information\n * @param pathname - Optional pathname coming from parent span \"http.target\"\n * @returns A sanitized URL string\n */\nexport function buildUrlFromComponentRoute(\n componentRoute: string,\n params?: ComponentRouteParams,\n headersDict?: HeadersDict,\n pathname?: string,\n): string {\n const parameterizedPath = substituteRouteParams(componentRoute, params);\n // If available, the pathname from the http.target of the HTTP request server span takes precedence over the parameterized path.\n // Spans such as generateMetadata and Server Component rendering are typically direct children of that span.\n const path = pathname ?? sanitizeRoutePath(parameterizedPath);\n\n const protocol = headersDict?.[HeaderKeys.FORWARDED_PROTO];\n const host = headersDict?.[HeaderKeys.FORWARDED_HOST] || headersDict?.[HeaderKeys.HOST];\n\n if (!protocol || !host) {\n return path;\n }\n\n const fullUrl = `${protocol}://${host}${path}`;\n\n const urlObject = parseStringToURLObject(fullUrl);\n if (!urlObject) {\n return path;\n }\n\n return getSanitizedUrlStringFromUrlObject(urlObject);\n}\n\n/**\n * Returns a sanitized URL string from the referer header if it exists and is valid.\n *\n * @param headersDict - Optional headers containing the referer\n * @returns A sanitized URL string or undefined if referer is missing/invalid\n */\nexport function extractSanitizedUrlFromRefererHeader(headersDict?: HeadersDict): string | undefined {\n const referer = headersDict?.[HeaderKeys.REFERER];\n if (!referer) {\n return undefined;\n }\n\n try {\n const refererUrl = new URL(referer);\n return getSanitizedUrlStringFromUrlObject(refererUrl);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Returns a sanitized URL string using the referer header if available,\n * otherwise constructs the URL from the component route, params, and headers.\n *\n * @param componentRoute - The route template to construct the URL from\n * @param params - Optional route parameters to replace in the template\n * @param headersDict - Optional headers containing protocol, host, and referer\n * @param pathname - Optional pathname coming from root span \"http.target\"\n * @returns A sanitized URL string\n */\nexport function getSanitizedRequestUrl(\n componentRoute: string,\n params?: ComponentRouteParams,\n headersDict?: HeadersDict,\n pathname?: string,\n): string {\n const refererUrl = extractSanitizedUrlFromRefererHeader(headersDict);\n if (refererUrl) {\n return refererUrl;\n }\n\n return buildUrlFromComponentRoute(componentRoute, params, headersDict, pathname);\n}\n"],"names":["parseStringToURLObject","getSanitizedUrlStringFromUrlObject"],"mappings":";;;;AAKA,MAAM,aAA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