[79a0317] | 1 | var fs = require('fs');
|
---|
| 2 | var path = require('path');
|
---|
| 3 |
|
---|
| 4 | var isAllowedResource = require('./is-allowed-resource');
|
---|
| 5 |
|
---|
| 6 | var hasProtocol = require('../utils/has-protocol');
|
---|
| 7 | var isRemoteResource = require('../utils/is-remote-resource');
|
---|
| 8 |
|
---|
| 9 | function loadOriginalSources(context, callback) {
|
---|
| 10 | var loadContext = {
|
---|
| 11 | callback: callback,
|
---|
| 12 | fetch: context.options.fetch,
|
---|
| 13 | index: 0,
|
---|
| 14 | inline: context.options.inline,
|
---|
| 15 | inlineRequest: context.options.inlineRequest,
|
---|
| 16 | inlineTimeout: context.options.inlineTimeout,
|
---|
| 17 | localOnly: context.localOnly,
|
---|
| 18 | rebaseTo: context.options.rebaseTo,
|
---|
| 19 | sourcesContent: context.sourcesContent,
|
---|
| 20 | uriToSource: uriToSourceMapping(context.inputSourceMapTracker.all()),
|
---|
| 21 | warnings: context.warnings
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | return context.options.sourceMap && context.options.sourceMapInlineSources
|
---|
| 25 | ? doLoadOriginalSources(loadContext)
|
---|
| 26 | : callback();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | function uriToSourceMapping(allSourceMapConsumers) {
|
---|
| 30 | var mapping = {};
|
---|
| 31 | var consumer;
|
---|
| 32 | var uri;
|
---|
| 33 | var source;
|
---|
| 34 | var i, l;
|
---|
| 35 |
|
---|
| 36 | for (source in allSourceMapConsumers) {
|
---|
| 37 | consumer = allSourceMapConsumers[source];
|
---|
| 38 |
|
---|
| 39 | for (i = 0, l = consumer.sources.length; i < l; i++) {
|
---|
| 40 | uri = consumer.sources[i];
|
---|
| 41 | source = consumer.sourceContentFor(uri, true);
|
---|
| 42 |
|
---|
| 43 | mapping[uri] = source;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | return mapping;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | function doLoadOriginalSources(loadContext) {
|
---|
| 51 | var uris = Object.keys(loadContext.uriToSource);
|
---|
| 52 | var uri;
|
---|
| 53 | var source;
|
---|
| 54 | var total;
|
---|
| 55 |
|
---|
| 56 | for (total = uris.length; loadContext.index < total; loadContext.index++) {
|
---|
| 57 | uri = uris[loadContext.index];
|
---|
| 58 | source = loadContext.uriToSource[uri];
|
---|
| 59 |
|
---|
| 60 | if (source) {
|
---|
| 61 | loadContext.sourcesContent[uri] = source;
|
---|
| 62 | } else {
|
---|
| 63 | return loadOriginalSource(uri, loadContext);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return loadContext.callback();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | function loadOriginalSource(uri, loadContext) {
|
---|
| 71 | var content;
|
---|
| 72 |
|
---|
| 73 | if (isRemoteResource(uri)) {
|
---|
| 74 | return loadOriginalSourceFromRemoteUri(uri, loadContext, function(content) {
|
---|
| 75 | loadContext.index++;
|
---|
| 76 | loadContext.sourcesContent[uri] = content;
|
---|
| 77 | return doLoadOriginalSources(loadContext);
|
---|
| 78 | });
|
---|
| 79 | }
|
---|
| 80 | content = loadOriginalSourceFromLocalUri(uri, loadContext);
|
---|
| 81 | loadContext.index++;
|
---|
| 82 | loadContext.sourcesContent[uri] = content;
|
---|
| 83 | return doLoadOriginalSources(loadContext);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | function loadOriginalSourceFromRemoteUri(uri, loadContext, whenLoaded) {
|
---|
| 87 | var isAllowed = isAllowedResource(uri, true, loadContext.inline);
|
---|
| 88 | var isRuntimeResource = !hasProtocol(uri);
|
---|
| 89 |
|
---|
| 90 | if (loadContext.localOnly) {
|
---|
| 91 | loadContext.warnings.push('Cannot fetch remote resource from "' + uri + '" as no callback given.');
|
---|
| 92 | return whenLoaded(null);
|
---|
| 93 | } if (isRuntimeResource) {
|
---|
| 94 | loadContext.warnings.push('Cannot fetch "' + uri + '" as no protocol given.');
|
---|
| 95 | return whenLoaded(null);
|
---|
| 96 | } if (!isAllowed) {
|
---|
| 97 | loadContext.warnings.push('Cannot fetch "' + uri + '" as resource is not allowed.');
|
---|
| 98 | return whenLoaded(null);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | loadContext.fetch(uri, loadContext.inlineRequest, loadContext.inlineTimeout, function(error, content) {
|
---|
| 102 | if (error) {
|
---|
| 103 | loadContext.warnings.push('Missing original source at "' + uri + '" - ' + error);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | whenLoaded(content);
|
---|
| 107 | });
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
|
---|
| 111 | var isAllowed = isAllowedResource(relativeUri, false, loadContext.inline);
|
---|
| 112 | var absoluteUri = path.resolve(loadContext.rebaseTo, relativeUri);
|
---|
| 113 |
|
---|
| 114 | if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
|
---|
| 115 | loadContext.warnings.push('Ignoring local source map at "' + absoluteUri + '" as resource is missing.');
|
---|
| 116 | return null;
|
---|
| 117 | } if (!isAllowed) {
|
---|
| 118 | loadContext.warnings.push('Cannot fetch "' + absoluteUri + '" as resource is not allowed.');
|
---|
| 119 | return null;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | var result = fs.readFileSync(absoluteUri, 'utf8');
|
---|
| 123 | if (result.charCodeAt(0) === 65279) {
|
---|
| 124 | result = result.substring(1);
|
---|
| 125 | }
|
---|
| 126 | return result;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | module.exports = loadOriginalSources;
|
---|