import { defineIntegration } from '@sentry/core';
const APP_PREFIX = 'app:///';
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export function normalizeUrls(event, origin) {
const pattern = new RegExp(`${escapeRegExp(origin)}(/|(?=[?#\\s])|$)`, 'g');
const normalize = (url) => url.replace(pattern, APP_PREFIX);
for (const exception of event.exception?.values || []) {
for (const frame of exception.stacktrace?.frames || []) {
if (frame.filename) {
frame.filename = normalize(frame.filename);
}
if (frame.abs_path) {
frame.abs_path = normalize(frame.abs_path);
}
}
}
for (const image of event.debug_meta?.images || []) {
if (image.type === 'sourcemap') {
image.code_file = normalize(image.code_file);
}
}
if (event.transaction) {
event.transaction = normalize(event.transaction);
}
if (event.request?.url) {
event.request.url = normalize(event.request.url);
}
for (const span of event.spans || []) {
if (span.description) {
span.description = normalize(span.description);
}
for (const key of ['url', 'url.full', 'http.url']) {
const value = span.data?.[key];
if (typeof value === 'string') {
span.data[key] = normalize(value);
}
}
}
const urls = event.urls;
if (Array.isArray(urls)) {
event.urls = urls.map((url) => (typeof url === 'string' ? normalize(url) : url));
}
return event;
}
const _normalizeUrlsIntegration = (() => {
return {
name: 'TauriNormalizeUrls',
processEvent(event) {
return normalizeUrls(event, window.location.origin);
},
};
});
export const normalizeUrlsIntegration = defineIntegration(_normalizeUrlsIntegration);