|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * Modifications copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * Copyright (c) 2016-2023 Isaac Z. Schlueter [email protected], James Talmage [email protected] (github.com/jamestalmage), and |
| 6 | + * Contributors |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 14 | + * Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +import * as url from 'url'; |
| 23 | + |
| 24 | +type StackData = { |
| 25 | + line?: number; |
| 26 | + column?: number; |
| 27 | + file?: string; |
| 28 | + isConstructor?: boolean; |
| 29 | + evalOrigin?: string; |
| 30 | + native?: boolean; |
| 31 | + function?: string; |
| 32 | + method?: string; |
| 33 | + evalLine?: number | undefined; |
| 34 | + evalColumn?: number | undefined; |
| 35 | + evalFile?: string | undefined; |
| 36 | +}; |
| 37 | + |
| 38 | +export class StackUtils { |
| 39 | + parseLine(line: string) { |
| 40 | + const match = line && line.match(re); |
| 41 | + if (!match) |
| 42 | + return null; |
| 43 | + |
| 44 | + const ctor = match[1] === 'new'; |
| 45 | + let fname = match[2]; |
| 46 | + const evalOrigin = match[3]; |
| 47 | + const evalFile = match[4]; |
| 48 | + const evalLine = Number(match[5]); |
| 49 | + const evalCol = Number(match[6]); |
| 50 | + let file = match[7]; |
| 51 | + const lnum = match[8]; |
| 52 | + const col = match[9]; |
| 53 | + const native = match[10] === 'native'; |
| 54 | + const closeParen = match[11] === ')'; |
| 55 | + let method; |
| 56 | + |
| 57 | + const res: StackData = {}; |
| 58 | + |
| 59 | + if (lnum) |
| 60 | + res.line = Number(lnum); |
| 61 | + |
| 62 | + if (col) |
| 63 | + res.column = Number(col); |
| 64 | + |
| 65 | + if (closeParen && file) { |
| 66 | + // make sure parens are balanced |
| 67 | + // if we have a file like "asdf) [as foo] (xyz.js", then odds are |
| 68 | + // that the fname should be += " (asdf) [as foo]" and the file |
| 69 | + // should be just "xyz.js" |
| 70 | + // walk backwards from the end to find the last unbalanced ( |
| 71 | + let closes = 0; |
| 72 | + for (let i = file.length - 1; i > 0; i--) { |
| 73 | + if (file.charAt(i) === ')') { |
| 74 | + closes++; |
| 75 | + } else if (file.charAt(i) === '(' && file.charAt(i - 1) === ' ') { |
| 76 | + closes--; |
| 77 | + if (closes === -1 && file.charAt(i - 1) === ' ') { |
| 78 | + const before = file.slice(0, i - 1); |
| 79 | + const after = file.slice(i + 1); |
| 80 | + file = after; |
| 81 | + fname += ` (${before}`; |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (fname) { |
| 89 | + const methodMatch = fname.match(methodRe); |
| 90 | + if (methodMatch) { |
| 91 | + fname = methodMatch[1]; |
| 92 | + method = methodMatch[2]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + setFile(res, file); |
| 97 | + |
| 98 | + if (ctor) |
| 99 | + res.isConstructor = true; |
| 100 | + |
| 101 | + if (evalOrigin) { |
| 102 | + res.evalOrigin = evalOrigin; |
| 103 | + res.evalLine = evalLine; |
| 104 | + res.evalColumn = evalCol; |
| 105 | + res.evalFile = evalFile && evalFile.replace(/\\/g, '/'); |
| 106 | + } |
| 107 | + |
| 108 | + if (native) |
| 109 | + res.native = true; |
| 110 | + if (fname) |
| 111 | + res.function = fname; |
| 112 | + if (method && fname !== method) |
| 113 | + res.method = method; |
| 114 | + return res; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function setFile(result: StackData, filename: string) { |
| 119 | + if (filename) { |
| 120 | + if (filename.startsWith('file://')) |
| 121 | + filename = url.fileURLToPath(filename); |
| 122 | + result.file = filename; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +const re = new RegExp('^' + |
| 127 | + // Sometimes we strip out the ' at' because it's noisy |
| 128 | + '(?:\\s*at )?' + |
| 129 | + // $1 = ctor if 'new' |
| 130 | + '(?:(new) )?' + |
| 131 | + // $2 = function name (can be literally anything) |
| 132 | + // May contain method at the end as [as xyz] |
| 133 | + '(?:(.*?) \\()?' + |
| 134 | + // (eval at <anonymous> (file.js:1:1), |
| 135 | + // $3 = eval origin |
| 136 | + // $4:$5:$6 are eval file/line/col, but not normally reported |
| 137 | + '(?:eval at ([^ ]+) \\((.+?):(\\d+):(\\d+)\\), )?' + |
| 138 | + // file:line:col |
| 139 | + // $7:$8:$9 |
| 140 | + // $10 = 'native' if native |
| 141 | + '(?:(.+?):(\\d+):(\\d+)|(native))' + |
| 142 | + // maybe close the paren, then end |
| 143 | + // if $11 is ), then we only allow balanced parens in the filename |
| 144 | + // any imbalance is placed on the fname. This is a heuristic, and |
| 145 | + // bound to be incorrect in some edge cases. The bet is that |
| 146 | + // having weird characters in method names is more common than |
| 147 | + // having weird characters in filenames, which seems reasonable. |
| 148 | + '(\\)?)$' |
| 149 | +); |
| 150 | + |
| 151 | +const methodRe = /^(.*?) \[as (.*?)\]$/; |
0 commit comments