mirror of
https://github.com/expo/source-map-lookup.git
synced 2026-11-15 09:28:12 -10:00
Implement resolver utility class
This commit is contained in:
1 parent
3067f2729e
commit
57c44446a5
2 files changed
+73
-1
No files matched your search
@@ -0,0 +1,69 @@
|
||||
var request = require('request'),
|
||||
sourceMap,
|
||||
SourceMapConsumer,
|
||||
url = require('url');
|
||||
|
||||
try {
|
||||
sourceMap = require('source-map');
|
||||
SourceMapConsumer = sourceMap.SourceMapConsumer;
|
||||
} catch (err) {
|
||||
/* NOP */
|
||||
}
|
||||
|
||||
var cache = {};
|
||||
|
||||
function loadSourceMap(file, callback) {
|
||||
if (!SourceMapConsumer) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
request(file, function(err, res, body) {
|
||||
var match = /@ ?sourceMappingURL=(.*)\n/.exec(body || '');
|
||||
if (match) {
|
||||
request(url.resolve(file, match[1]), function(err, res, body) {
|
||||
if (!err && (res.statusCode === 200 || res.statusCode === 304) && body) {
|
||||
callback(undefined, new SourceMapConsumer(body));
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Look up the source map for arbitrary http endpoints
|
||||
*/
|
||||
exports.map = function(file, line, column, callback) {
|
||||
function bound() {
|
||||
exports.map(file, line, column, callback);
|
||||
}
|
||||
if (cache[file] !== undefined) {
|
||||
if (!cache[file]) {
|
||||
callback(undefined, file, line);
|
||||
} else if (cache[file].pending) {
|
||||
cache[file].pending.push(bound);
|
||||
} else {
|
||||
var original = cache[file].originalPositionFor({line: line, column: column});
|
||||
callback(undefined, original.source, original.line, original.column);
|
||||
}
|
||||
} else {
|
||||
cache[file] = {
|
||||
pending: [bound]
|
||||
};
|
||||
|
||||
loadSourceMap(file, function(err, map) {
|
||||
var pending = cache[file].pending;
|
||||
|
||||
cache[file] = map || false;
|
||||
pending.forEach(function(callback) {
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
exports.reset = function() {
|
||||
cache = {};
|
||||
};
|
||||
+4
-1
@@ -9,8 +9,11 @@
|
||||
"node": ">= 0.8"
|
||||
},
|
||||
"dependencies": {
|
||||
"optimist": "~0.3.5",
|
||||
"request": "~2.34.0",
|
||||
"source-map": "~0.1.8",
|
||||
"optimist": "~0.3.5"
|
||||
"split": "~0.3.0",
|
||||
"through": "~2.3.4"
|
||||
},
|
||||
"bin": {
|
||||
"source-map-lookup": "bin/source-map-lookup"
|
||||
|
||||
Reference in new issue
Block a user