123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- var fs = require('fs');
- var path = require('path');
- var isAllowedResource = require('./is-allowed-resource');
- var hasProtocol = require('../utils/has-protocol');
- var isRemoteResource = require('../utils/is-remote-resource');
- function loadOriginalSources(context, callback) {
- var loadContext = {
- callback: callback,
- fetch: context.options.fetch,
- index: 0,
- inline: context.options.inline,
- inlineRequest: context.options.inlineRequest,
- inlineTimeout: context.options.inlineTimeout,
- localOnly: context.localOnly,
- rebaseTo: context.options.rebaseTo,
- sourcesContent: context.sourcesContent,
- uriToSource: uriToSourceMapping(context.inputSourceMapTracker.all()),
- warnings: context.warnings
- };
- return context.options.sourceMap && context.options.sourceMapInlineSources ?
- doLoadOriginalSources(loadContext) :
- callback();
- }
- function uriToSourceMapping(allSourceMapConsumers) {
- var mapping = {};
- var consumer;
- var uri;
- var source;
- var i, l;
- for (source in allSourceMapConsumers) {
- consumer = allSourceMapConsumers[source];
- for (i = 0, l = consumer.sources.length; i < l; i++) {
- uri = consumer.sources[i];
- source = consumer.sourceContentFor(uri, true);
- mapping[uri] = source;
- }
- }
- return mapping;
- }
- function doLoadOriginalSources(loadContext) {
- var uris = Object.keys(loadContext.uriToSource);
- var uri;
- var source;
- var total;
- for (total = uris.length; loadContext.index < total; loadContext.index++) {
- uri = uris[loadContext.index];
- source = loadContext.uriToSource[uri];
- if (source) {
- loadContext.sourcesContent[uri] = source;
- } else {
- return loadOriginalSource(uri, loadContext);
- }
- }
- return loadContext.callback();
- }
- function loadOriginalSource(uri, loadContext) {
- var content;
- if (isRemoteResource(uri)) {
- return loadOriginalSourceFromRemoteUri(uri, loadContext, function (content) {
- loadContext.index++;
- loadContext.sourcesContent[uri] = content;
- return doLoadOriginalSources(loadContext);
- });
- } else {
- content = loadOriginalSourceFromLocalUri(uri, loadContext);
- loadContext.index++;
- loadContext.sourcesContent[uri] = content;
- return doLoadOriginalSources(loadContext);
- }
- }
- function loadOriginalSourceFromRemoteUri(uri, loadContext, whenLoaded) {
- var isAllowed = isAllowedResource(uri, true, loadContext.inline);
- var isRuntimeResource = !hasProtocol(uri);
- if (loadContext.localOnly) {
- loadContext.warnings.push('Cannot fetch remote resource from "' + uri + '" as no callback given.');
- return whenLoaded(null);
- } else if (isRuntimeResource) {
- loadContext.warnings.push('Cannot fetch "' + uri + '" as no protocol given.');
- return whenLoaded(null);
- } else if (!isAllowed) {
- loadContext.warnings.push('Cannot fetch "' + uri + '" as resource is not allowed.');
- return whenLoaded(null);
- }
- loadContext.fetch(uri, loadContext.inlineRequest, loadContext.inlineTimeout, function (error, content) {
- if (error) {
- loadContext.warnings.push('Missing original source at "' + uri + '" - ' + error);
- }
- whenLoaded(content);
- });
- }
- function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
- var isAllowed = isAllowedResource(relativeUri, false, loadContext.inline);
- var absoluteUri = path.resolve(loadContext.rebaseTo, relativeUri);
- if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
- loadContext.warnings.push('Ignoring local source map at "' + absoluteUri + '" as resource is missing.');
- return null;
- } else if (!isAllowed) {
- loadContext.warnings.push('Cannot fetch "' + absoluteUri + '" as resource is not allowed.');
- return null;
- }
- return fs.readFileSync(absoluteUri, 'utf8');
- }
- module.exports = loadOriginalSources;
|