You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.7 KiB

4 years ago
  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. } else {
  65. content = loadOriginalSourceFromLocalUri(uri, loadContext);
  66. loadContext.index++;
  67. loadContext.sourcesContent[uri] = content;
  68. return doLoadOriginalSources(loadContext);
  69. }
  70. }
  71. function loadOriginalSourceFromRemoteUri(uri, loadContext, whenLoaded) {
  72. var isAllowed = isAllowedResource(uri, true, loadContext.inline);
  73. var isRuntimeResource = !hasProtocol(uri);
  74. if (loadContext.localOnly) {
  75. loadContext.warnings.push('Cannot fetch remote resource from "' + uri + '" as no callback given.');
  76. return whenLoaded(null);
  77. } else if (isRuntimeResource) {
  78. loadContext.warnings.push('Cannot fetch "' + uri + '" as no protocol given.');
  79. return whenLoaded(null);
  80. } else if (!isAllowed) {
  81. loadContext.warnings.push('Cannot fetch "' + uri + '" as resource is not allowed.');
  82. return whenLoaded(null);
  83. }
  84. loadContext.fetch(uri, loadContext.inlineRequest, loadContext.inlineTimeout, function (error, content) {
  85. if (error) {
  86. loadContext.warnings.push('Missing original source at "' + uri + '" - ' + error);
  87. }
  88. whenLoaded(content);
  89. });
  90. }
  91. function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
  92. var isAllowed = isAllowedResource(relativeUri, false, loadContext.inline);
  93. var absoluteUri = path.resolve(loadContext.rebaseTo, relativeUri);
  94. if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
  95. loadContext.warnings.push('Ignoring local source map at "' + absoluteUri + '" as resource is missing.');
  96. return null;
  97. } else if (!isAllowed) {
  98. loadContext.warnings.push('Cannot fetch "' + absoluteUri + '" as resource is not allowed.');
  99. return null;
  100. }
  101. return fs.readFileSync(absoluteUri, 'utf8');
  102. }
  103. module.exports = loadOriginalSources;