sourcemap-parse.js 994 Bytes
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
};