load-original-sources.js 3.8 KB

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