-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
source-map added to detect api calls in stacktraces Refs: #7
- Loading branch information
1 parent
a0b51d0
commit 6644cad
Showing
11 changed files
with
164 additions
and
18 deletions.
There are no files selected for viewing
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
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
Binary file not shown.
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,15 @@ | ||
// @ts-ignore | ||
import mappingsUrl from './mappings.wasm?url'; | ||
|
||
export default async function readWasm() { | ||
if (mappingsUrl.startsWith('data:')) { | ||
const [,base64] = mappingsUrl.split(','); | ||
return Buffer.from(base64, 'base64').buffer; | ||
} | ||
const response = await fetch(mappingsUrl); | ||
return await response.arrayBuffer(); | ||
}; | ||
|
||
export const initialize = () => { | ||
// nothing to do | ||
}; |
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,74 @@ | ||
// @ts-ignore | ||
import { SourceMapConsumer } from 'source-map/lib/source-map-consumer'; | ||
|
||
const regex = /^ +at.+\((.*):([0-9]+):([0-9]+)/; | ||
|
||
class StacktraceSourcemap { | ||
|
||
_mapForUri = new Map<string, SourceMapConsumer | undefined>(); | ||
|
||
async register(uri: string) { | ||
if (this._mapForUri.has(uri)) return; | ||
this._mapForUri.set(uri, undefined); | ||
await new Promise<void>(async resolve => { | ||
try { | ||
const result = await fetch(`${uri}.map`); | ||
const sourceMap = await result.json(); | ||
const sourceMapConsumer = result.ok ? await new SourceMapConsumer(sourceMap) : undefined; | ||
this._mapForUri.set(uri, sourceMapConsumer); | ||
} catch (e) { | ||
console.error(e); | ||
} finally { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
|
||
processSourceMaps([firstLine, ...stack]: string[]): string[] { | ||
const result = [firstLine]; | ||
|
||
for (const line of stack) { | ||
const [_, uri, lineNumberStr, columnStr] = line.match(regex) ?? []; | ||
if (uri) { | ||
const lineNumber = parseInt(lineNumberStr, 10); | ||
const column = parseInt(columnStr, 10); | ||
const map = this._mapForUri.get(uri); | ||
|
||
if (map) { | ||
// we think we have a map for that uri. call source-map library | ||
var origPos = map.originalPositionFor({ line: lineNumber, column }); | ||
if (origPos.source && origPos.line && origPos.column) { | ||
result.push(this._formatOriginalPosition( | ||
origPos.source, | ||
origPos.line, | ||
origPos.column, | ||
origPos.name || this._origName(line) | ||
)); | ||
} | ||
} else { | ||
// we can't find a map for that url, but we parsed the row. | ||
// reformat unchanged line for consistency with the sourcemapped | ||
// lines. | ||
result.push(this._formatOriginalPosition(uri, lineNumber, column, this._origName(line))); | ||
} | ||
} else { | ||
// we weren't able to parse the row, push back what we were given | ||
result.push(line); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
_origName(origLine: string) { | ||
const [, name] = / +at +([^ ]*).*/.exec(origLine) ?? []; | ||
return name; | ||
} | ||
|
||
_formatOriginalPosition(source: string, line: number, column: number, name?: string) { | ||
// mimic chrome's format | ||
return ` at ${name ?? "(unknown)"} (${source}:${line}:${column})`; | ||
}; | ||
} | ||
|
||
export const stacktraceSourcemap = new StacktraceSourcemap(); |
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
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,3 @@ | ||
import './background.js'; | ||
|
||
self._stacktraceSourcemap.register(chrome.runtime.getURL('background.js')).catch(() => {}); |
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