@sentry/browser
Version:
Official Sentry SDK for browsers
57 lines (56 loc) • 2.34 kB
JavaScript
const require_debug_build = require("../debug-build.js");
const require_helpers = require("../helpers.js");
let _sentry_core = require("@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 && (0, _sentry_core.makeDsn)(dsn);
const tunnelPath = tunnelOptions.path ?? (dsnComponents && (0, _sentry_core.getTunnelPath)(dsnComponents));
if (!tunnelPath) return {
tunnel: void 0,
transportOptions
};
const resolvedTunnel = `${(0, _sentry_core._INTERNAL_normalizeTunnelBasePath)(tunnelOptions.basePath)}${tunnelPath}${tunnelOptions.trailingSlash ? "/" : ""}`;
require_debug_build.DEBUG_BUILD && _sentry_core.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 = (0, _sentry_core.parseStringToURLObject)(url, require_helpers.WINDOW.location?.href);
if (!urlObject) return false;
return (0, _sentry_core.isURLObjectRelative)(urlObject) || urlObject.origin === require_helpers.WINDOW.location?.origin;
}
exports.resolveBrowserTunnelOption = resolveBrowserTunnelOption;
//# sourceMappingURL=managedTunnel.js.map