Skip to content

Commit 1d3971f

Browse files
authored
Merge pull request #114 from codex-team/improve-source-code-sending
imp(source-maps): do not send minified source cod frames
2 parents 0dacbde + 27d928f commit 1d3971f

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/modules/stackParser.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export default class StackParser {
4242
* @param {StackFrame} frame — information about backtrace item
4343
*/
4444
private async extractSourceCode(frame: StackFrame): Promise<SourceCodeLine[] | null> {
45+
const minifiedSourceCodeThreshold = 200;
46+
4547
try {
4648
if (!frame.fileName) {
4749
return null;
@@ -55,7 +57,7 @@ export default class StackParser {
5557
* If error occurred in large column number, the script probably minified
5658
* Skip minified bundles — they will be processed if user enabled source-maps tracking
5759
*/
58-
if (frame.columnNumber && frame.columnNumber > 200) {
60+
if (frame.columnNumber && frame.columnNumber > minifiedSourceCodeThreshold) {
5961
return null;
6062
}
6163

@@ -70,14 +72,28 @@ export default class StackParser {
7072
const linesCollectCount = 5;
7173
const lineFrom = Math.max(0, actualLineNumber - linesCollectCount);
7274
const lineTo = Math.min(lines.length - 1, actualLineNumber + linesCollectCount + 1);
73-
const linesToCollect = lines.slice(lineFrom, lineTo);
74-
75-
return linesToCollect.map((content, index) => {
76-
return {
77-
line: lineFrom + index + 1,
78-
content,
79-
};
80-
});
75+
76+
const sourceCodeLines: SourceCodeLine[] = [];
77+
let extractedLineIndex = 1;
78+
79+
/**
80+
* In some cases column number of the error stack trace frame would be less then 200, but source code is minified
81+
* For this cases we need to check, that all of the lines to collect have length less than 200 too
82+
*/
83+
for (const lineToCheck in lines.slice(lineFrom, lineTo)) {
84+
if (lineToCheck.length > minifiedSourceCodeThreshold) {
85+
return null;
86+
} else {
87+
sourceCodeLines.push({
88+
line: lineFrom + extractedLineIndex,
89+
content: lineToCheck,
90+
});
91+
92+
extractedLineIndex += 1;
93+
}
94+
}
95+
96+
return sourceCodeLines;
8197
} catch (e) {
8298
console.warn('Hawk JS SDK: Can not extract source code. Please, report this issue: https://github.com/codex-team/hawk.javascript/issues/new', e);
8399

0 commit comments

Comments
 (0)