sourcemap-parse.js
994 Bytes
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
const sourceMap = require('source-map');
const lru = require('lru-cache');
const rp = require('request-promise');
const logger = global.yoho.logger;
const cache = lru({
max: 500
});
const loadFileSourceMap = (url) => {
const cacheItem = cache.get(url);
if (cacheItem) {
logger.info('cache');
return Promise.resolve(cacheItem);
} else {
return rp(`${url}.map`, {json: true}).then(res => {
cache.set(url, res);
return res;
}, () => {
logger.error(`${url}.map file request failure!`);
});
}
};
const parse = ({url, line, column}) => {
return loadFileSourceMap(url).then(source => {
if (!source) {
return void 0;
}
const consumer = new sourceMap.SourceMapConsumer(source);
const origin = consumer.originalPositionFor({
line: line,
column: column
});
return origin;
});
};
module.exports = {
parse
};