-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
222 lines (182 loc) · 6.03 KB
/
index.js
File metadata and controls
222 lines (182 loc) · 6.03 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var path = require('path');
var fs = require('fs');
var url = require('url');
var StackTrace = require('node-stacktrace');
// Maps a file path to a string containing the file contents
var fileContentsCache = {};
// Maps a file path to a source map for that file
var sourceMapCache = {};
// Regex for detecting source maps
var reSourceMap = /^data:application\/json[^,]+base64,/;
var readFile;
function retrieveFile(path){
// Trim the path to make sure there is no extra whitespace.
path = path.trim();
if( path in fileContentsCache ){
return fileContentsCache[path];
}
var contents;
contents = readFile(path);
if( !contents ){
try{
contents = fs.readFileSync(path, 'utf8');
}
catch (e) {
contents = null;
}
}
return fileContentsCache[path] = contents;
}
function supportRelativeURL(file, base){
return file ? url.resolve(file, base) : base;
}
function retrieveSourceMapURL(source){
var fileData;
// Get the URL of the source map
fileData = retrieveFile(source);
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;
// Keep executing the search to find the *last* sourceMappingURL to avoid
// picking up sourceMappingURLs from comments, strings, etc.
var lastMatch, match;
while( match = re.exec(fileData) ){
lastMatch = match;
}
return lastMatch ? lastMatch[1] : null;
}
// Can be overridden by the retrieveSourceMap option to install. Takes a
// generated source filename; returns a {map, optional url} object, or null if
// there is no source map. The map field may be either a string or the parsed
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
// constructor).
function retrieveSourceMap(source){
var sourceMappingURL = retrieveSourceMapURL(source);
if( !sourceMappingURL ) return null;
// Read the contents of the source map
var sourceMapData;
if( reSourceMap.test(sourceMappingURL) ){
// Support source map URL as a data url
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
sourceMapData = new Buffer(rawData, "base64").toString();
sourceMappingURL = null;
}
else{
// Support source map URLs relative to the source URL
sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
sourceMapData = retrieveFile(sourceMappingURL);
}
if( !sourceMapData ){
return null;
}
return {
url: sourceMappingURL,
map: sourceMapData
};
}
function mapSourcePosition(position){
var sourceMap = sourceMapCache[position.source];
if( !sourceMap ){
// Call the (overrideable) retrieveSourceMap function to get the source map.
var urlAndMap = retrieveSourceMap(position.source);
if( urlAndMap ){
sourceMap = sourceMapCache[position.source] = {
url: urlAndMap.url,
map: new SourceMapConsumer(urlAndMap.map)
};
// Load all sources stored inline with the source map into the file cache
// to pretend like they are already loaded. They may not exist on disk.
if( sourceMap.map.sourcesContent ){
sourceMap.map.sources.forEach(function(source, i) {
var contents = sourceMap.map.sourcesContent[i];
if( contents ){
var url = supportRelativeURL(sourceMap.url, source);
fileContentsCache[url] = contents;
}
});
}
}
else{
sourceMap = sourceMapCache[position.source] = {
url: null,
map: null
};
}
}
// Resolve the source URL relative to the URL of the source map
if( sourceMap && sourceMap.map ){
var originalPosition = sourceMap.map.originalPositionFor(position);
// Only return the original position if a matching line was found. If no
// matching line is found then we return position instead, which will cause
// the stack trace to print the path and line for the compiled file. It is
// better to give a precise location in the compiled file than a vague
// location in the original file.
if( originalPosition.source !== null ){
originalPosition.source = supportRelativeURL(sourceMap.url || position.source, originalPosition.source);
return originalPosition;
}
}
return position;
}
function mapCallSite(callSite, index, callSites){
var source = callSite.getScriptNameOrSourceURL() || callSite.getFileName();
if( source ){
var line = callSite.getLineNumber();
var column = callSite.getColumnNumber() - 1;
// Fix position in Node where some (internal) code is prepended.
// See https://github.com/evanw/node-source-map-support/issues/36
var fromModule = typeof process !== 'undefined' && callSites.length && callSites[callSites.length - 1].getFileName() === 'module.js';
if( fromModule && line === 1 ) {
column-= 63;
}
var position = mapSourcePosition({
source: source,
line: line,
column: column
});
callSite.source = position.source;
callSite.lineNumber = position.line;
callSite.columnNumber = position.column + 1;
}
/*
if( callSite.isEval() ){
console.log('handling isEval calls');
var evalOrigin = callSite.getEvalOrigin();
var evalSsource = evalOrigin.getFileName() || evalOrigin.getScriptNameOrSourceURL();
var evalLine = evalOrigin.getLineNumber();
var evalColumn = evalOrigin.getColumnNumber() - 1;
var evalPosition = mapSourcePosition({
source: source,
line: evalSsource,
column: evalColumn
});
callSite.evalFileName = evalPosition.source;
callSite.evalLineNumber = evalPosition.line;
callSite.evalColumnNumber = evalPosition.column + 1;
}
*/
// Code called using eval() needs special handling
/*
if( callSite.isEval() ){
var evalOrigin = callSite.getEvalOrigin();
if( evalOrigin ){
mapCallSite(evalOrigin);
}
}
*/
//console.log('mapping', source, 'into', callSite.source);
}
var installed = false;
module.exports = {
StackTrace: StackTrace,
install: function(readSource){
if( installed ){
throw new Error('sourcemap already installed');
}
else{
installed = true;
readFile = readSource || function(){};
StackTrace.setTransformer(mapCallSite);
}
}
};