UNPKG

@sentry/core

Version:
73 lines (72 loc) • 2.27 kB
import { defineIntegration } from "../integration.js"; import { basename, relative } from "../utils/path.js"; import { GLOBAL_OBJ } from "../utils/worldwide.js"; // src/integrations/rewriteframes.ts const INTEGRATION_NAME = "RewriteFrames"; /** * Rewrite event frames paths. */ const rewriteFramesIntegration = defineIntegration((options = {}) => { const root = options.root; const prefix = options.prefix || "app:///"; const isBrowser = "window" in GLOBAL_OBJ && !!GLOBAL_OBJ.window; const iteratee = options.iteratee || generateIteratee({ isBrowser, root, prefix }); /** Process an exception event. */ function _processExceptionsEvent(event) { try { return { ...event, exception: { ...event.exception, values: event.exception.values.map((value) => ({ ...value, ...value.stacktrace && { stacktrace: _processStacktrace(value.stacktrace) } })) } }; } catch { return event; } } /** Process a stack trace. */ function _processStacktrace(stacktrace) { return { ...stacktrace, frames: stacktrace?.frames?.map((f) => iteratee(f)) }; } return { name: INTEGRATION_NAME, processEvent(originalEvent) { let processedEvent = originalEvent; if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) processedEvent = _processExceptionsEvent(processedEvent); return processedEvent; } }; }); /** * Exported only for tests. */ function generateIteratee({ isBrowser, root, prefix }) { return (frame) => { if (!frame.filename) return frame; const isWindowsFrame = /^[a-zA-Z]:\\/.test(frame.filename) || frame.filename.includes("\\") && !frame.filename.includes("/"); const startsWithSlash = /^\//.test(frame.filename); if (isBrowser) { if (root) { const oldFilename = frame.filename; if (oldFilename.indexOf(root) === 0) frame.filename = oldFilename.replace(root, prefix); } } else if (isWindowsFrame || startsWithSlash) { const filename = isWindowsFrame ? frame.filename.replace(/^[a-zA-Z]:/, "").replace(/\\/g, "/") : frame.filename; frame.filename = `${prefix}${root ? relative(root, filename) : basename(filename)}`; } return frame; }; } export { generateIteratee, rewriteFramesIntegration }; //# sourceMappingURL=rewriteframes.js.map