UNPKG

@sentry/browser

Version:
57 lines (56 loc) • 2.23 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { WINDOW } from "../helpers.js"; import { _INTERNAL_normalizeTunnelBasePath, debug, getTunnelPath, isURLObjectRelative, makeDsn, parseStringToURLObject } from "@sentry/core"; // src/utils/managedTunnel.ts /** * Resolves `tunnel: true` or {@link BrowserTunnelOptions} to the managed tunnel's same-origin path. A string is left as * written, so a tunnel the app serves itself keeps its URL. */ function resolveBrowserTunnelOption(options) { const { tunnel, dsn, transportOptions } = options; if (!tunnel) return { tunnel, transportOptions }; if (typeof tunnel === "string") return { tunnel, transportOptions: isSameOrigin(tunnel) ? withEnvelopeContentType(transportOptions) : transportOptions }; const tunnelOptions = tunnel === true ? {} : tunnel; const dsnComponents = dsn && makeDsn(dsn); const tunnelPath = tunnelOptions.path ?? (dsnComponents && getTunnelPath(dsnComponents)); if (!tunnelPath) return { tunnel: void 0, transportOptions }; const resolvedTunnel = `${_INTERNAL_normalizeTunnelBasePath(tunnelOptions.basePath)}${tunnelPath}${tunnelOptions.trailingSlash ? "/" : ""}`; DEBUG_BUILD && debug.log(`Tunneling events to "${resolvedTunnel}"`); return { tunnel: resolvedTunnel, transportOptions: withEnvelopeContentType(transportOptions) }; } /** * Sends envelopes as `application/x-sentry-envelope`. CSRF protection (SvelteKit, Astro) rejects the default * `text/plain` POST as a form submission, and the Next.js managed tunnel route on Vercel matches on this header. * Cross-origin it would trigger a CORS preflight, so only same-origin tunnels get it. */ function withEnvelopeContentType(transportOptions) { return { ...transportOptions, headers: { "Content-Type": "application/x-sentry-envelope", ...transportOptions?.headers } }; } /** * Whether the tunnel URL is on the page's origin. Unknown counts as cross-origin. */ function isSameOrigin(url) { const urlObject = parseStringToURLObject(url, WINDOW.location?.href); if (!urlObject) return false; return isURLObjectRelative(urlObject) || urlObject.origin === WINDOW.location?.origin; } export { resolveBrowserTunnelOption }; //# sourceMappingURL=managedTunnel.js.map