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