frame.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {t} from 'sentry/locale';
  2. import {WeightedNode} from './weightedNode';
  3. export class Frame extends WeightedNode {
  4. readonly key: string | number;
  5. readonly name: string;
  6. readonly file?: string;
  7. readonly line?: number;
  8. readonly column?: number;
  9. readonly is_application: boolean;
  10. readonly path?: string;
  11. readonly image?: string;
  12. readonly resource?: string;
  13. readonly threadId?: number;
  14. readonly inline?: boolean;
  15. static Root = new Frame(
  16. {
  17. key: 'sentry root',
  18. name: 'sentry root',
  19. is_application: false,
  20. },
  21. 'mobile'
  22. );
  23. constructor(frameInfo: Profiling.FrameInfo, type?: 'mobile' | 'web' | 'node') {
  24. super();
  25. this.key = frameInfo.key;
  26. this.file = frameInfo.file;
  27. this.name = frameInfo.name;
  28. this.resource = frameInfo.resource;
  29. this.line = frameInfo.line;
  30. this.column = frameInfo.column;
  31. this.is_application = !!frameInfo.is_application;
  32. this.image = frameInfo.image;
  33. this.threadId = frameInfo.threadId;
  34. this.path = frameInfo.path;
  35. // We are remapping some of the keys as they differ between platforms.
  36. // This is a temporary solution until we adopt a unified format.
  37. if (frameInfo.columnNumber && this.column === undefined) {
  38. this.column = frameInfo.columnNumber;
  39. }
  40. if (frameInfo.lineNumber && this.column === undefined) {
  41. this.line = frameInfo.lineNumber;
  42. }
  43. if (frameInfo.scriptName && this.column === undefined) {
  44. this.resource = frameInfo.scriptName;
  45. }
  46. // If the frame is a web frame and there is no name associated to it, then it was likely invoked as an iife or anonymous callback as
  47. // most modern browser engines properly show anonymous functions when they are assigned to references (e.g. `let foo = function() {};`)
  48. if (type === 'web' || type === 'node') {
  49. if (this.name === '(garbage collector)' || this.name === '(root)') {
  50. this.is_application = false;
  51. }
  52. if (!this.name || this.name.startsWith('unknown ')) {
  53. this.name = t('<anonymous>');
  54. }
  55. // If the frame had no line or column, it was part of the native code, (e.g. calling String.fromCharCode)
  56. if (this.line === undefined && this.column === undefined) {
  57. this.name += ` ${t('[native code]')}`;
  58. this.is_application = false;
  59. }
  60. // Doing this on the frontend while we figure out how to do this on the backend/client properly
  61. // @TODO Our old node.js incorrectly sends file instead of path (fixed in SDK, but not all SDK's are upgraded :rip:)
  62. const pathOrFile = this.path || this.file;
  63. if (pathOrFile) {
  64. if (pathOrFile.startsWith('node:internal')) {
  65. this.is_application = false;
  66. }
  67. if (this.image === undefined && pathOrFile) {
  68. const match =
  69. /node_modules(\/|\\)(?<maybeScopeOrPackage>.*?)(\/|\\)((?<maybePackage>.*)((\/|\\)))?/.exec(
  70. pathOrFile
  71. );
  72. if (match?.groups) {
  73. const {maybeScopeOrPackage, maybePackage} = match.groups;
  74. if (maybeScopeOrPackage.startsWith('@')) {
  75. this.image = `${maybeScopeOrPackage}/${maybePackage}`;
  76. } else {
  77. this.image = match.groups.maybeScopeOrPackage;
  78. }
  79. this.is_application = false;
  80. }
  81. }
  82. // Extract the first component of node:namespace/pkg if there is one
  83. // else return just the node:namespace
  84. if (pathOrFile?.substring(0, 5) === 'node:') {
  85. let image = '';
  86. const l = pathOrFile.length;
  87. for (let i = 0; i < l; i++) {
  88. if (pathOrFile.charAt(i) === '/') {
  89. image += '/';
  90. i++;
  91. while (i < l && pathOrFile.charAt(i) !== '/') {
  92. image += pathOrFile.charAt(i);
  93. i++;
  94. }
  95. break;
  96. }
  97. image += pathOrFile.charAt(i);
  98. }
  99. if (image) {
  100. this.image = image;
  101. }
  102. }
  103. }
  104. }
  105. if (!this.name) {
  106. this.name = t('<unknown>');
  107. }
  108. }
  109. isRoot(): boolean {
  110. return this.name === Frame.Root.name;
  111. }
  112. }