load-original-sources.js
3.74 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;