-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a94288
commit be9b919
Showing
4 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { SourceMapConsumer } from 'source-map' | ||
|
||
// FUN@FILE:LINE:COLUMN | ||
const STACK_TRACE_LINE_REGEX = /^([A-Za-z0-9]*)@(.*):([0-9]+):([0-9]+)/ | ||
|
||
/** | ||
* Decode a minified stack trace using source maps | ||
* @param {string} stack - the minified stack trace | ||
* @param {Object<string, SourceMapConsumer>} [sourceMaps] - an object used to cache source maps | ||
* @returns {Promise<string>} Decoded stack trace | ||
*/ | ||
export async function decodeMinifiedStackTrace (stack, sourceMaps = {}) { | ||
let decodedStack = '' | ||
for (const line of stack.split('\n')) { | ||
try { | ||
const stackLine = line.trim() | ||
const stackLineParts = stackLine?.match(STACK_TRACE_LINE_REGEX) | ||
if (stackLineParts) { | ||
const [stackFile, stackLine, stackColumn] = stackLineParts.slice(2) | ||
if (!stackFile || !stackLine || !stackColumn) throw new Error('Unsupported stack line ' + JSON.stringify(stackLineParts)) | ||
if ( | ||
( | ||
!stackFile.startsWith(process.env.NEXT_PUBLIC_ASSET_PREFIX) && | ||
!stackFile.startsWith(process.env.NEXT_PUBLIC_URL) | ||
) || | ||
!stackFile.endsWith('.js') | ||
) throw new Error('Unsupported file url ' + stackFile) | ||
const sourceMapUrl = stackFile + '.map' | ||
if (!sourceMaps[sourceMapUrl]) { | ||
sourceMaps[sourceMapUrl] = await new SourceMapConsumer(await fetch(sourceMapUrl).then(res => res.text())) | ||
} | ||
const sourceMapper = sourceMaps[sourceMapUrl] | ||
const map = sourceMapper.originalPositionFor({ | ||
line: parseInt(stackLine), | ||
column: parseInt(stackColumn) | ||
}) | ||
const { source, name, line, column } = map | ||
decodedStack += `${name || ''}@${source}:${line}:${column}\n` | ||
continue | ||
} | ||
} catch (e) { | ||
console.error('Cannot decode stack line', e) | ||
} | ||
decodedStack += `${line}\n` | ||
} | ||
return decodedStack | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters