node-stack-trace.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. /**
  3. * Does this filename look like it's part of the app code?
  4. */
  5. function filenameIsInApp(filename, isNative = false) {
  6. const isInternal =
  7. isNative ||
  8. (filename &&
  9. // It's not internal if it's an absolute linux path
  10. !filename.startsWith('/') &&
  11. // It's not internal if it's an absolute windows path
  12. !filename.match(/^[A-Z]:/) &&
  13. // It's not internal if the path is starting with a dot
  14. !filename.startsWith('.') &&
  15. // It's not internal if the frame has a protocol. In node, this is usually the case if the file got pre-processed with a bundler like webpack
  16. !filename.match(/^[a-zA-Z]([a-zA-Z0-9.\-+])*:\/\//)); // Schema from: https://stackoverflow.com/a/3641782
  17. // in_app is all that's not an internal Node function or a module within node_modules
  18. // note that isNative appears to return true even for node core libraries
  19. // see https://github.com/getsentry/raven-node/issues/176
  20. return !isInternal && filename !== undefined && !filename.includes('node_modules/');
  21. }
  22. /** Node Stack line parser */
  23. // eslint-disable-next-line complexity
  24. function node(getModule) {
  25. const FILENAME_MATCH = /^\s*[-]{4,}$/;
  26. const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/;
  27. // eslint-disable-next-line complexity
  28. return (line) => {
  29. const lineMatch = line.match(FULL_MATCH);
  30. if (lineMatch) {
  31. let object;
  32. let method;
  33. let functionName;
  34. let typeName;
  35. let methodName;
  36. if (lineMatch[1]) {
  37. functionName = lineMatch[1];
  38. let methodStart = functionName.lastIndexOf('.');
  39. if (functionName[methodStart - 1] === '.') {
  40. methodStart--;
  41. }
  42. if (methodStart > 0) {
  43. object = functionName.slice(0, methodStart);
  44. method = functionName.slice(methodStart + 1);
  45. const objectEnd = object.indexOf('.Module');
  46. if (objectEnd > 0) {
  47. functionName = functionName.slice(objectEnd + 1);
  48. object = object.slice(0, objectEnd);
  49. }
  50. }
  51. typeName = undefined;
  52. }
  53. if (method) {
  54. typeName = object;
  55. methodName = method;
  56. }
  57. if (method === '<anonymous>') {
  58. methodName = undefined;
  59. functionName = undefined;
  60. }
  61. if (functionName === undefined) {
  62. methodName = methodName || '<anonymous>';
  63. functionName = typeName ? `${typeName}.${methodName}` : methodName;
  64. }
  65. let filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2];
  66. const isNative = lineMatch[5] === 'native';
  67. // If it's a Windows path, trim the leading slash so that `/C:/foo` becomes `C:/foo`
  68. if (filename && filename.match(/\/[A-Z]:/)) {
  69. filename = filename.slice(1);
  70. }
  71. if (!filename && lineMatch[5] && !isNative) {
  72. filename = lineMatch[5];
  73. }
  74. return {
  75. filename,
  76. module: getModule ? getModule(filename) : undefined,
  77. function: functionName,
  78. lineno: parseInt(lineMatch[3], 10) || undefined,
  79. colno: parseInt(lineMatch[4], 10) || undefined,
  80. in_app: filenameIsInApp(filename, isNative),
  81. };
  82. }
  83. if (line.match(FILENAME_MATCH)) {
  84. return {
  85. filename: line,
  86. };
  87. }
  88. return undefined;
  89. };
  90. }
  91. exports.filenameIsInApp = filenameIsInApp;
  92. exports.node = node;
  93. //# sourceMappingURL=node-stack-trace.js.map