stack-parsers.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { createStackParser } from '@sentry/utils';
  2. // global reference to slice
  3. const UNKNOWN_FUNCTION = '?';
  4. const OPERA10_PRIORITY = 10;
  5. const OPERA11_PRIORITY = 20;
  6. const CHROME_PRIORITY = 30;
  7. const WINJS_PRIORITY = 40;
  8. const GECKO_PRIORITY = 50;
  9. function createFrame(filename, func, lineno, colno) {
  10. const frame = {
  11. filename,
  12. function: func,
  13. in_app: true, // All browser frames are considered in_app
  14. };
  15. if (lineno !== undefined) {
  16. frame.lineno = lineno;
  17. }
  18. if (colno !== undefined) {
  19. frame.colno = colno;
  20. }
  21. return frame;
  22. }
  23. // Chromium based browsers: Chrome, Brave, new Opera, new Edge
  24. const chromeRegex =
  25. /^\s*at (?:(.+?\)(?: \[.+\])?|.*?) ?\((?:address at )?)?(?:async )?((?:<anonymous>|[-a-z]+:|.*bundle|\/)?.*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i;
  26. const chromeEvalRegex = /\((\S*)(?::(\d+))(?::(\d+))\)/;
  27. const chrome = line => {
  28. const parts = chromeRegex.exec(line);
  29. if (parts) {
  30. const isEval = parts[2] && parts[2].indexOf('eval') === 0; // start of line
  31. if (isEval) {
  32. const subMatch = chromeEvalRegex.exec(parts[2]);
  33. if (subMatch) {
  34. // throw out eval line/column and use top-most line/column number
  35. parts[2] = subMatch[1]; // url
  36. parts[3] = subMatch[2]; // line
  37. parts[4] = subMatch[3]; // column
  38. }
  39. }
  40. // Kamil: One more hack won't hurt us right? Understanding and adding more rules on top of these regexps right now
  41. // would be way too time consuming. (TODO: Rewrite whole RegExp to be more readable)
  42. const [func, filename] = extractSafariExtensionDetails(parts[1] || UNKNOWN_FUNCTION, parts[2]);
  43. return createFrame(filename, func, parts[3] ? +parts[3] : undefined, parts[4] ? +parts[4] : undefined);
  44. }
  45. return;
  46. };
  47. const chromeStackLineParser = [CHROME_PRIORITY, chrome];
  48. // gecko regex: `(?:bundle|\d+\.js)`: `bundle` is for react native, `\d+\.js` also but specifically for ram bundles because it
  49. // generates filenames without a prefix like `file://` the filenames in the stacktrace are just 42.js
  50. // We need this specific case for now because we want no other regex to match.
  51. const geckoREgex =
  52. /^\s*(.*?)(?:\((.*?)\))?(?:^|@)?((?:[-a-z]+)?:\/.*?|\[native code\]|[^@]*(?:bundle|\d+\.js)|\/[\w\-. /=]+)(?::(\d+))?(?::(\d+))?\s*$/i;
  53. const geckoEvalRegex = /(\S+) line (\d+)(?: > eval line \d+)* > eval/i;
  54. const gecko = line => {
  55. const parts = geckoREgex.exec(line);
  56. if (parts) {
  57. const isEval = parts[3] && parts[3].indexOf(' > eval') > -1;
  58. if (isEval) {
  59. const subMatch = geckoEvalRegex.exec(parts[3]);
  60. if (subMatch) {
  61. // throw out eval line/column and use top-most line number
  62. parts[1] = parts[1] || 'eval';
  63. parts[3] = subMatch[1];
  64. parts[4] = subMatch[2];
  65. parts[5] = ''; // no column when eval
  66. }
  67. }
  68. let filename = parts[3];
  69. let func = parts[1] || UNKNOWN_FUNCTION;
  70. [func, filename] = extractSafariExtensionDetails(func, filename);
  71. return createFrame(filename, func, parts[4] ? +parts[4] : undefined, parts[5] ? +parts[5] : undefined);
  72. }
  73. return;
  74. };
  75. const geckoStackLineParser = [GECKO_PRIORITY, gecko];
  76. const winjsRegex = /^\s*at (?:((?:\[object object\])?.+) )?\(?((?:[-a-z]+):.*?):(\d+)(?::(\d+))?\)?\s*$/i;
  77. const winjs = line => {
  78. const parts = winjsRegex.exec(line);
  79. return parts
  80. ? createFrame(parts[2], parts[1] || UNKNOWN_FUNCTION, +parts[3], parts[4] ? +parts[4] : undefined)
  81. : undefined;
  82. };
  83. const winjsStackLineParser = [WINJS_PRIORITY, winjs];
  84. const opera10Regex = / line (\d+).*script (?:in )?(\S+)(?:: in function (\S+))?$/i;
  85. const opera10 = line => {
  86. const parts = opera10Regex.exec(line);
  87. return parts ? createFrame(parts[2], parts[3] || UNKNOWN_FUNCTION, +parts[1]) : undefined;
  88. };
  89. const opera10StackLineParser = [OPERA10_PRIORITY, opera10];
  90. const opera11Regex =
  91. / line (\d+), column (\d+)\s*(?:in (?:<anonymous function: ([^>]+)>|([^)]+))\(.*\))? in (.*):\s*$/i;
  92. const opera11 = line => {
  93. const parts = opera11Regex.exec(line);
  94. return parts ? createFrame(parts[5], parts[3] || parts[4] || UNKNOWN_FUNCTION, +parts[1], +parts[2]) : undefined;
  95. };
  96. const opera11StackLineParser = [OPERA11_PRIORITY, opera11];
  97. const defaultStackLineParsers = [chromeStackLineParser, geckoStackLineParser, winjsStackLineParser];
  98. const defaultStackParser = createStackParser(...defaultStackLineParsers);
  99. /**
  100. * Safari web extensions, starting version unknown, can produce "frames-only" stacktraces.
  101. * What it means, is that instead of format like:
  102. *
  103. * Error: wat
  104. * at function@url:row:col
  105. * at function@url:row:col
  106. * at function@url:row:col
  107. *
  108. * it produces something like:
  109. *
  110. * function@url:row:col
  111. * function@url:row:col
  112. * function@url:row:col
  113. *
  114. * Because of that, it won't be captured by `chrome` RegExp and will fall into `Gecko` branch.
  115. * This function is extracted so that we can use it in both places without duplicating the logic.
  116. * Unfortunately "just" changing RegExp is too complicated now and making it pass all tests
  117. * and fix this case seems like an impossible, or at least way too time-consuming task.
  118. */
  119. const extractSafariExtensionDetails = (func, filename) => {
  120. const isSafariExtension = func.indexOf('safari-extension') !== -1;
  121. const isSafariWebExtension = func.indexOf('safari-web-extension') !== -1;
  122. return isSafariExtension || isSafariWebExtension
  123. ? [
  124. func.indexOf('@') !== -1 ? func.split('@')[0] : UNKNOWN_FUNCTION,
  125. isSafariExtension ? `safari-extension:${filename}` : `safari-web-extension:${filename}`,
  126. ]
  127. : [func, filename];
  128. };
  129. export { chromeStackLineParser, defaultStackLineParsers, defaultStackParser, geckoStackLineParser, opera10StackLineParser, opera11StackLineParser, winjsStackLineParser };
  130. //# sourceMappingURL=stack-parsers.js.map