rewriteframes.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { defineIntegration, convertIntegrationFnToClass } from '@sentry/core';
  2. import { relative, basename } from '@sentry/utils';
  3. const INTEGRATION_NAME = 'RewriteFrames';
  4. const _rewriteFramesIntegration = ((options = {}) => {
  5. const root = options.root;
  6. const prefix = options.prefix || 'app:///';
  7. const iteratee =
  8. options.iteratee ||
  9. ((frame) => {
  10. if (!frame.filename) {
  11. return frame;
  12. }
  13. // Determine if this is a Windows frame by checking for a Windows-style prefix such as `C:\`
  14. const isWindowsFrame =
  15. /^[a-zA-Z]:\\/.test(frame.filename) ||
  16. // or the presence of a backslash without a forward slash (which are not allowed on Windows)
  17. (frame.filename.includes('\\') && !frame.filename.includes('/'));
  18. // Check if the frame filename begins with `/`
  19. const startsWithSlash = /^\//.test(frame.filename);
  20. if (isWindowsFrame || startsWithSlash) {
  21. const filename = isWindowsFrame
  22. ? frame.filename
  23. .replace(/^[a-zA-Z]:/, '') // remove Windows-style prefix
  24. .replace(/\\/g, '/') // replace all `\\` instances with `/`
  25. : frame.filename;
  26. const base = root ? relative(root, filename) : basename(filename);
  27. frame.filename = `${prefix}${base}`;
  28. }
  29. return frame;
  30. });
  31. /** Process an exception event. */
  32. function _processExceptionsEvent(event) {
  33. try {
  34. return {
  35. ...event,
  36. exception: {
  37. ...event.exception,
  38. // The check for this is performed inside `process` call itself, safe to skip here
  39. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  40. values: event.exception.values.map(value => ({
  41. ...value,
  42. ...(value.stacktrace && { stacktrace: _processStacktrace(value.stacktrace) }),
  43. })),
  44. },
  45. };
  46. } catch (_oO) {
  47. return event;
  48. }
  49. }
  50. /** Process a stack trace. */
  51. function _processStacktrace(stacktrace) {
  52. return {
  53. ...stacktrace,
  54. frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => iteratee(f)),
  55. };
  56. }
  57. return {
  58. name: INTEGRATION_NAME,
  59. // TODO v8: Remove this
  60. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  61. processEvent(originalEvent) {
  62. let processedEvent = originalEvent;
  63. if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) {
  64. processedEvent = _processExceptionsEvent(processedEvent);
  65. }
  66. return processedEvent;
  67. },
  68. };
  69. }) ;
  70. const rewriteFramesIntegration = defineIntegration(_rewriteFramesIntegration);
  71. /**
  72. * Rewrite event frames paths.
  73. * @deprecated Use `rewriteFramesIntegration()` instead.
  74. */
  75. // eslint-disable-next-line deprecation/deprecation
  76. const RewriteFrames = convertIntegrationFnToClass(
  77. INTEGRATION_NAME,
  78. rewriteFramesIntegration,
  79. )
  80. ;
  81. export { RewriteFrames, rewriteFramesIntegration };
  82. //# sourceMappingURL=rewriteframes.js.map