| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const escapeStringRegexp = require('escape-string-regexp');
|
|---|
| 4 |
|
|---|
| 5 | const cwd = typeof process === 'object' && process && typeof process.cwd === 'function'
|
|---|
| 6 | ? process.cwd()
|
|---|
| 7 | : '.'
|
|---|
| 8 |
|
|---|
| 9 | const natives = [].concat(
|
|---|
| 10 | require('module').builtinModules,
|
|---|
| 11 | 'bootstrap_node',
|
|---|
| 12 | 'node',
|
|---|
| 13 | ).map(n => new RegExp(`(?:\\((?:node:)?${n}(?:\\.js)?:\\d+:\\d+\\)$|^\\s*at (?:node:)?${n}(?:\\.js)?:\\d+:\\d+$)`));
|
|---|
| 14 |
|
|---|
| 15 | natives.push(
|
|---|
| 16 | /\((?:node:)?internal\/[^:]+:\d+:\d+\)$/,
|
|---|
| 17 | /\s*at (?:node:)?internal\/[^:]+:\d+:\d+$/,
|
|---|
| 18 | /\/\.node-spawn-wrap-\w+-\w+\/node:\d+:\d+\)?$/
|
|---|
| 19 | );
|
|---|
| 20 |
|
|---|
| 21 | class StackUtils {
|
|---|
| 22 | constructor (opts) {
|
|---|
| 23 | opts = {
|
|---|
| 24 | ignoredPackages: [],
|
|---|
| 25 | ...opts
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | if ('internals' in opts === false) {
|
|---|
| 29 | opts.internals = StackUtils.nodeInternals();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | if ('cwd' in opts === false) {
|
|---|
| 33 | opts.cwd = cwd
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | this._cwd = opts.cwd.replace(/\\/g, '/');
|
|---|
| 37 | this._internals = [].concat(
|
|---|
| 38 | opts.internals,
|
|---|
| 39 | ignoredPackagesRegExp(opts.ignoredPackages)
|
|---|
| 40 | );
|
|---|
| 41 |
|
|---|
| 42 | this._wrapCallSite = opts.wrapCallSite || false;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | static nodeInternals () {
|
|---|
| 46 | return [...natives];
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | clean (stack, indent = 0) {
|
|---|
| 50 | indent = ' '.repeat(indent);
|
|---|
| 51 |
|
|---|
| 52 | if (!Array.isArray(stack)) {
|
|---|
| 53 | stack = stack.split('\n');
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (!(/^\s*at /.test(stack[0])) && (/^\s*at /.test(stack[1]))) {
|
|---|
| 57 | stack = stack.slice(1);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | let outdent = false;
|
|---|
| 61 | let lastNonAtLine = null;
|
|---|
| 62 | const result = [];
|
|---|
| 63 |
|
|---|
| 64 | stack.forEach(st => {
|
|---|
| 65 | st = st.replace(/\\/g, '/');
|
|---|
| 66 |
|
|---|
| 67 | if (this._internals.some(internal => internal.test(st))) {
|
|---|
| 68 | return;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | const isAtLine = /^\s*at /.test(st);
|
|---|
| 72 |
|
|---|
| 73 | if (outdent) {
|
|---|
| 74 | st = st.trimEnd().replace(/^(\s+)at /, '$1');
|
|---|
| 75 | } else {
|
|---|
| 76 | st = st.trim();
|
|---|
| 77 | if (isAtLine) {
|
|---|
| 78 | st = st.slice(3);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | st = st.replace(`${this._cwd}/`, '');
|
|---|
| 83 |
|
|---|
| 84 | if (st) {
|
|---|
| 85 | if (isAtLine) {
|
|---|
| 86 | if (lastNonAtLine) {
|
|---|
| 87 | result.push(lastNonAtLine);
|
|---|
| 88 | lastNonAtLine = null;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | result.push(st);
|
|---|
| 92 | } else {
|
|---|
| 93 | outdent = true;
|
|---|
| 94 | lastNonAtLine = st;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | });
|
|---|
| 98 |
|
|---|
| 99 | return result.map(line => `${indent}${line}\n`).join('');
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | captureString (limit, fn = this.captureString) {
|
|---|
| 103 | if (typeof limit === 'function') {
|
|---|
| 104 | fn = limit;
|
|---|
| 105 | limit = Infinity;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | const {stackTraceLimit} = Error;
|
|---|
| 109 | if (limit) {
|
|---|
| 110 | Error.stackTraceLimit = limit;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | const obj = {};
|
|---|
| 114 |
|
|---|
| 115 | Error.captureStackTrace(obj, fn);
|
|---|
| 116 | const {stack} = obj;
|
|---|
| 117 | Error.stackTraceLimit = stackTraceLimit;
|
|---|
| 118 |
|
|---|
| 119 | return this.clean(stack);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | capture (limit, fn = this.capture) {
|
|---|
| 123 | if (typeof limit === 'function') {
|
|---|
| 124 | fn = limit;
|
|---|
| 125 | limit = Infinity;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | const {prepareStackTrace, stackTraceLimit} = Error;
|
|---|
| 129 | Error.prepareStackTrace = (obj, site) => {
|
|---|
| 130 | if (this._wrapCallSite) {
|
|---|
| 131 | return site.map(this._wrapCallSite);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return site;
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | if (limit) {
|
|---|
| 138 | Error.stackTraceLimit = limit;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | const obj = {};
|
|---|
| 142 | Error.captureStackTrace(obj, fn);
|
|---|
| 143 | const { stack } = obj;
|
|---|
| 144 | Object.assign(Error, {prepareStackTrace, stackTraceLimit});
|
|---|
| 145 |
|
|---|
| 146 | return stack;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | at (fn = this.at) {
|
|---|
| 150 | const [site] = this.capture(1, fn);
|
|---|
| 151 |
|
|---|
| 152 | if (!site) {
|
|---|
| 153 | return {};
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | const res = {
|
|---|
| 157 | line: site.getLineNumber(),
|
|---|
| 158 | column: site.getColumnNumber()
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | setFile(res, site.getFileName(), this._cwd);
|
|---|
| 162 |
|
|---|
| 163 | if (site.isConstructor()) {
|
|---|
| 164 | Object.defineProperty(res, 'constructor', {
|
|---|
| 165 | value: true,
|
|---|
| 166 | configurable: true,
|
|---|
| 167 | });
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (site.isEval()) {
|
|---|
| 171 | res.evalOrigin = site.getEvalOrigin();
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | // Node v10 stopped with the isNative() on callsites, apparently
|
|---|
| 175 | /* istanbul ignore next */
|
|---|
| 176 | if (site.isNative()) {
|
|---|
| 177 | res.native = true;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | let typename;
|
|---|
| 181 | try {
|
|---|
| 182 | typename = site.getTypeName();
|
|---|
| 183 | } catch (_) {
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | if (typename && typename !== 'Object' && typename !== '[object Object]') {
|
|---|
| 187 | res.type = typename;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | const fname = site.getFunctionName();
|
|---|
| 191 | if (fname) {
|
|---|
| 192 | res.function = fname;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | const meth = site.getMethodName();
|
|---|
| 196 | if (meth && fname !== meth) {
|
|---|
| 197 | res.method = meth;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | return res;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | parseLine (line) {
|
|---|
| 204 | const match = line && line.match(re);
|
|---|
| 205 | if (!match) {
|
|---|
| 206 | return null;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | const ctor = match[1] === 'new';
|
|---|
| 210 | let fname = match[2];
|
|---|
| 211 | const evalOrigin = match[3];
|
|---|
| 212 | const evalFile = match[4];
|
|---|
| 213 | const evalLine = Number(match[5]);
|
|---|
| 214 | const evalCol = Number(match[6]);
|
|---|
| 215 | let file = match[7];
|
|---|
| 216 | const lnum = match[8];
|
|---|
| 217 | const col = match[9];
|
|---|
| 218 | const native = match[10] === 'native';
|
|---|
| 219 | const closeParen = match[11] === ')';
|
|---|
| 220 | let method;
|
|---|
| 221 |
|
|---|
| 222 | const res = {};
|
|---|
| 223 |
|
|---|
| 224 | if (lnum) {
|
|---|
| 225 | res.line = Number(lnum);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | if (col) {
|
|---|
| 229 | res.column = Number(col);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | if (closeParen && file) {
|
|---|
| 233 | // make sure parens are balanced
|
|---|
| 234 | // if we have a file like "asdf) [as foo] (xyz.js", then odds are
|
|---|
| 235 | // that the fname should be += " (asdf) [as foo]" and the file
|
|---|
| 236 | // should be just "xyz.js"
|
|---|
| 237 | // walk backwards from the end to find the last unbalanced (
|
|---|
| 238 | let closes = 0;
|
|---|
| 239 | for (let i = file.length - 1; i > 0; i--) {
|
|---|
| 240 | if (file.charAt(i) === ')') {
|
|---|
| 241 | closes++;
|
|---|
| 242 | } else if (file.charAt(i) === '(' && file.charAt(i - 1) === ' ') {
|
|---|
| 243 | closes--;
|
|---|
| 244 | if (closes === -1 && file.charAt(i - 1) === ' ') {
|
|---|
| 245 | const before = file.slice(0, i - 1);
|
|---|
| 246 | const after = file.slice(i + 1);
|
|---|
| 247 | file = after;
|
|---|
| 248 | fname += ` (${before}`;
|
|---|
| 249 | break;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (fname) {
|
|---|
| 256 | const methodMatch = fname.match(methodRe);
|
|---|
| 257 | if (methodMatch) {
|
|---|
| 258 | fname = methodMatch[1];
|
|---|
| 259 | method = methodMatch[2];
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | setFile(res, file, this._cwd);
|
|---|
| 264 |
|
|---|
| 265 | if (ctor) {
|
|---|
| 266 | Object.defineProperty(res, 'constructor', {
|
|---|
| 267 | value: true,
|
|---|
| 268 | configurable: true,
|
|---|
| 269 | });
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | if (evalOrigin) {
|
|---|
| 273 | res.evalOrigin = evalOrigin;
|
|---|
| 274 | res.evalLine = evalLine;
|
|---|
| 275 | res.evalColumn = evalCol;
|
|---|
| 276 | res.evalFile = evalFile && evalFile.replace(/\\/g, '/');
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | if (native) {
|
|---|
| 280 | res.native = true;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | if (fname) {
|
|---|
| 284 | res.function = fname;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | if (method && fname !== method) {
|
|---|
| 288 | res.method = method;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | return res;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | function setFile (result, filename, cwd) {
|
|---|
| 296 | if (filename) {
|
|---|
| 297 | filename = filename.replace(/\\/g, '/');
|
|---|
| 298 | if (filename.startsWith(`${cwd}/`)) {
|
|---|
| 299 | filename = filename.slice(cwd.length + 1);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | result.file = filename;
|
|---|
| 303 | }
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | function ignoredPackagesRegExp(ignoredPackages) {
|
|---|
| 307 | if (ignoredPackages.length === 0) {
|
|---|
| 308 | return [];
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | const packages = ignoredPackages.map(mod => escapeStringRegexp(mod));
|
|---|
| 312 |
|
|---|
| 313 | return new RegExp(`[\/\\\\]node_modules[\/\\\\](?:${packages.join('|')})[\/\\\\][^:]+:\\d+:\\d+`)
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | const re = new RegExp(
|
|---|
| 317 | '^' +
|
|---|
| 318 | // Sometimes we strip out the ' at' because it's noisy
|
|---|
| 319 | '(?:\\s*at )?' +
|
|---|
| 320 | // $1 = ctor if 'new'
|
|---|
| 321 | '(?:(new) )?' +
|
|---|
| 322 | // $2 = function name (can be literally anything)
|
|---|
| 323 | // May contain method at the end as [as xyz]
|
|---|
| 324 | '(?:(.*?) \\()?' +
|
|---|
| 325 | // (eval at <anonymous> (file.js:1:1),
|
|---|
| 326 | // $3 = eval origin
|
|---|
| 327 | // $4:$5:$6 are eval file/line/col, but not normally reported
|
|---|
| 328 | '(?:eval at ([^ ]+) \\((.+?):(\\d+):(\\d+)\\), )?' +
|
|---|
| 329 | // file:line:col
|
|---|
| 330 | // $7:$8:$9
|
|---|
| 331 | // $10 = 'native' if native
|
|---|
| 332 | '(?:(.+?):(\\d+):(\\d+)|(native))' +
|
|---|
| 333 | // maybe close the paren, then end
|
|---|
| 334 | // if $11 is ), then we only allow balanced parens in the filename
|
|---|
| 335 | // any imbalance is placed on the fname. This is a heuristic, and
|
|---|
| 336 | // bound to be incorrect in some edge cases. The bet is that
|
|---|
| 337 | // having weird characters in method names is more common than
|
|---|
| 338 | // having weird characters in filenames, which seems reasonable.
|
|---|
| 339 | '(\\)?)$'
|
|---|
| 340 | );
|
|---|
| 341 |
|
|---|
| 342 | const methodRe = /^(.*?) \[as (.*?)\]$/;
|
|---|
| 343 |
|
|---|
| 344 | module.exports = StackUtils;
|
|---|