/
SourceUtils.ts
147 lines (133 loc) · 5.21 KB
/
SourceUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import fs = require('fs');
import path = require('path');
import url = require('url');
import Specs = require('./Specs');
import Logger = require('./Logger');
import SourceMap = require("source-map");
var SourceMapResolve = require("source-map-resolve");
var errorStackParser = require('error-stack-parser');
type ErrorWithStack = {
skip?: number;
skipFunctions?: string;
stack?: string;
stacktrace?: string;
'opera#sourceloc'?: any;
};
class SourceUtils {
constructor(private basePath: string, private logger: Logger, private resolveFilePath: (fileName: string) => string) {
}
private sourceMapConsumers: { [filePath: string]: SourceMap.SourceMapConsumer } = {};
private getSourceMapConsumer(filePath: string): SourceMap.SourceMapConsumer {
if (filePath in this.sourceMapConsumers) {
return this.sourceMapConsumers[filePath];
}
try {
var content = fs.readFileSync(filePath).toString();
var sourceMap = SourceMapResolve.resolveSync(content, filePath, fs.readFileSync);
var consumer = sourceMap ? new SourceMap.SourceMapConsumer(sourceMap.map) : null;
if (consumer) {
consumer['resolvePath'] = (filePath: string) => path.resolve(path.dirname(sourceMap.sourcesRelativeTo), filePath);
}
this.sourceMapConsumers[filePath] = consumer;
return consumer;
} catch (e) {
this.sourceMapConsumers[filePath] = undefined;
}
}
resolveSource(source: Specs.Source): Specs.Source {
if (source && source.fileName) {
source.fileName = this.resolveFilePath(source.fileName);
var consumer = this.getSourceMapConsumer(source.fileName);
if (consumer) {
var position = {
line: Math.max(source.lineNumber || 1, 1),
column: Math.max(source.columnNumber || 1, 1) - 1,
bias: SourceMap.SourceMapConsumer.GREATEST_LOWER_BOUND
};
var orig = consumer.originalPositionFor(position);
if (!orig.source) {
position.bias = SourceMap.SourceMapConsumer.LEAST_UPPER_BOUND;
orig = consumer.originalPositionFor(position);
}
if (orig.source) {
source.source = this.resolveSource({
functionName: source.functionName,
fileName: consumer['resolvePath'](orig.source),
lineNumber: orig.line,
columnNumber: orig.column + 1
});
}
}
}
return source;
}
getRealSource(source: Specs.Source, relative: boolean): Specs.Source {
if (source) {
if (source.source) {
return this.getRealSource(source.source, relative);
}
if (relative && source.fileName) {
source.fileName = path.relative(this.basePath, source.fileName);
}
}
return source;
}
getSource(error: ErrorWithStack): Specs.Source {
if (!error) return;
var stack = this.parseStack(error, false);
if (!stack) return;
if (typeof error.skip === 'number' && error.skip > 0) {
stack = stack.slice(error.skip);
}
if (typeof error.skipFunctions === 'string' && error.skipFunctions) {
var re = new RegExp(error.skipFunctions);
while (stack.length > 0 && re.test(stack[0].functionName)) {
stack.shift();
}
}
return stack[0];
}
parseStack(error: ErrorWithStack, relative: boolean): Specs.Source[] {
var self = this;
try {
return errorStackParser.parse(error)
.map(frame => getSource(frame))
.map(frame => this.resolveSource(frame))
.map(frame => this.getRealSource(frame, relative));
} catch (e) {
this.logger.debug(e);
return;
}
function getSource(frame: any): Specs.Source {
return {
functionName: frame.functionName,
fileName: self.resolveFilePath(frame.fileName),
lineNumber: frame.lineNumber,
columnNumber: frame.columnNumber
};
}
}
normalizeStack(stack: string): string[] {
var relative = false;
var basePath = this.basePath;
var stackFrames = this.parseStack({ stack: stack }, relative);
if (stackFrames) {
return stackFrames
.filter(frame => !/\/require\.js$/.test(frame.fileName))
.map(frame => formatFrame(frame));
} else {
return stack.split(/\r\n|\n/g);
}
function formatFrame(frame: Specs.Source): string {
var result = 'at ';
result += frame.functionName || '<anonymous>';
result += ' in ';
result += frame.fileName;
if (typeof frame.lineNumber === 'number' && frame.lineNumber >= 0) {
result += ':line ' + frame.lineNumber.toString(10);
}
return result;
}
}
}
export = SourceUtils;