a zip code crypto-currency system good for red ONLY

load-original-sources.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;