@sentry/remix
Version:
Official Sentry SDK for Remix
70 lines (67 loc) • 2.32 kB
JavaScript
import { debug } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build.js';
import { applyFormDataAttributes } from './formData.js';
import { matchServerRoutes } from './vendor/response.js';
async function storeFormDataKeys(args, span, formDataCapture) {
try {
const clonedRequest = args.request.clone();
const formData = await clonedRequest.formData();
applyFormDataAttributes(span, formData, formDataCapture);
} catch (e) {
DEBUG_BUILD && debug.warn("Failed to read FormData from request", e);
}
}
function convertRemixRouteIdToPath(routeId) {
const path = routeId.replace(/^routes\//, "");
if (path === "_index") {
return "/";
}
const segments = path.split(".");
const pathSegments = [];
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
if (!segment) {
continue;
}
if (segment.startsWith("_") && segment !== "_index") {
continue;
}
if (segment === "_index" && i === segments.length - 1) {
continue;
}
const pathSegment = segment.endsWith("_") ? segment.slice(0, -1) : segment;
if (pathSegment === "$") {
pathSegments.push(":*");
continue;
}
if (pathSegment.startsWith("$")) {
const paramName = pathSegment.substring(1);
pathSegments.push(`:${paramName}`);
} else {
pathSegments.push(pathSegment);
}
}
const routePath = pathSegments.length > 0 ? `/${pathSegments.join("/")}` : "/";
return routePath;
}
function isCloudflareEnv() {
return typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare");
}
function getTransactionName(routes, url) {
const matches = matchServerRoutes(routes, url.pathname);
const match = matches?.[matches.length - 1];
if (!match) {
return [url.pathname, "url"];
}
const routeId = match.route.id || "no-route-id";
const parameterizedPath = convertRemixRouteIdToPath(routeId);
return [parameterizedPath, "route"];
}
function createRoutes(manifest, parentId) {
return Object.entries(manifest).filter(([, route]) => route.parentId === parentId).map(([id, route]) => ({
...route,
children: createRoutes(manifest, id)
}));
}
export { convertRemixRouteIdToPath, createRoutes, getTransactionName, isCloudflareEnv, storeFormDataKeys };
//# sourceMappingURL=utils.js.map