|
| 1 | +// Pre-compiled RegExp patterns for ignored files |
| 2 | +const IGNORED_FILES = [ |
| 3 | + /^StackTrace\.js$/, |
| 4 | + /^TSLCore\.js$/, |
| 5 | + /^.*Node\.js$/, |
| 6 | + /^three\.webgpu.*\.js$/ |
| 7 | +]; |
| 8 | + |
| 9 | +/** |
| 10 | + * Parses the stack trace and filters out ignored files. |
| 11 | + * Returns an array with function name, file, line, and column. |
| 12 | + */ |
| 13 | +function getFilteredStack( stack ) { |
| 14 | + |
| 15 | + // Pattern to extract function name, file, line, and column from different browsers |
| 16 | + // Chrome: "at functionName (file.js:1:2)" or "at file.js:1:2" |
| 17 | + // Firefox: "[email protected]:1:2" |
| 18 | + const regex = /(?:at\s+(.+?)\s+\()?(?:(.+?)@)?([^@\s()]+):(\d+):(\d+)/; |
| 19 | + |
| 20 | + return stack.split( '\n' ) |
| 21 | + .map( line => { |
| 22 | + |
| 23 | + const match = line.match( regex ); |
| 24 | + if ( ! match ) return null; // Skip if line format is invalid |
| 25 | + |
| 26 | + // Chrome: match[1], Firefox: match[2] |
| 27 | + const fn = match[ 1 ] || match[ 2 ] || ''; |
| 28 | + const file = match[ 3 ].split( '?' )[ 0 ]; // Clean file name (Vite/HMR) |
| 29 | + const lineNum = parseInt( match[ 4 ], 10 ); |
| 30 | + const column = parseInt( match[ 5 ], 10 ); |
| 31 | + |
| 32 | + // Extract only the filename from full path |
| 33 | + const fileName = file.split( '/' ).pop(); |
| 34 | + |
| 35 | + return { |
| 36 | + fn: fn, |
| 37 | + file: fileName, |
| 38 | + line: lineNum, |
| 39 | + column: column |
| 40 | + }; |
| 41 | + |
| 42 | + } ) |
| 43 | + .filter( frame => { |
| 44 | + |
| 45 | + // Only keep frames that are valid and not in the ignore list |
| 46 | + return frame && ! IGNORED_FILES.some( regex => regex.test( frame.file ) ); |
| 47 | + |
| 48 | + } ); |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Class representing a stack trace for debugging purposes. |
| 54 | + */ |
| 55 | +class StackTrace { |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a StackTrace instance by capturing and filtering the current stack trace. |
| 59 | + * |
| 60 | + * @param {Error|string|null} stackMessage - An optional stack trace to use instead of capturing a new one. |
| 61 | + */ |
| 62 | + constructor( stackMessage = null ) { |
| 63 | + |
| 64 | + /** |
| 65 | + * This flag can be used for type testing. |
| 66 | + * |
| 67 | + * @type {boolean} |
| 68 | + * @readonly |
| 69 | + * @default true |
| 70 | + */ |
| 71 | + this.isStackTrace = true; |
| 72 | + |
| 73 | + /** |
| 74 | + * The stack trace. |
| 75 | + * |
| 76 | + * @type {Array<{fn: string, file: string, line: number, column: number}>} |
| 77 | + */ |
| 78 | + this.stack = getFilteredStack( stackMessage ? stackMessage : new Error().stack ); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns a formatted location string of the top stack frame. |
| 84 | + * |
| 85 | + * @returns {string} The formatted stack trace message. |
| 86 | + */ |
| 87 | + getLocation() { |
| 88 | + |
| 89 | + if ( this.stack.length === 0 ) { |
| 90 | + |
| 91 | + return '[Unknown location]'; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + const mainStack = this.stack[ 0 ]; |
| 96 | + |
| 97 | + const fn = mainStack.fn; |
| 98 | + const fnName = fn ? `"${ fn }()" at ` : ''; |
| 99 | + |
| 100 | + return `${fnName}"${mainStack.file}:${mainStack.line}"`; // :${mainStack.column} |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the full error message including the stack trace. |
| 106 | + * |
| 107 | + * @param {string} message - The error message. |
| 108 | + * @returns {string} The full error message with stack trace. |
| 109 | + */ |
| 110 | + getError( message ) { |
| 111 | + |
| 112 | + if ( this.stack.length === 0 ) { |
| 113 | + |
| 114 | + return message; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + // Output: "Error: message\n at functionName (file.js:line:column)" |
| 119 | + const stackString = this.stack.map( frame => { |
| 120 | + |
| 121 | + const location = `${ frame.file }:${ frame.line }:${ frame.column }`; |
| 122 | + |
| 123 | + if ( frame.fn ) { |
| 124 | + |
| 125 | + return ` at ${ frame.fn } (${ location })`; |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + return ` at ${ location }`; |
| 130 | + |
| 131 | + } ).join( '\n' ); |
| 132 | + |
| 133 | + return `${ message }\n${ stackString }`; |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +export default StackTrace; |
0 commit comments