map-store.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const path = require('path');
  7. const fs = require('fs');
  8. const debug = require('debug')('istanbuljs');
  9. const SMC = require('source-map').SourceMapConsumer;
  10. const pathutils = require('./pathutils');
  11. const sourceStore = require('./source-store');
  12. const transformer = require('./transformer');
  13. /**
  14. * Tracks source maps for registered files
  15. */
  16. class MapStore {
  17. /**
  18. * @param {Object} opts [opts=undefined] options.
  19. * @param {Boolean} opts.verbose [opts.verbose=false] verbose mode
  20. * @param {String} opts.baseDir [opts.baseDir=null] alternate base directory
  21. * to resolve sourcemap files
  22. * @param {String} opts.sourceStore [opts.sourceStore='memory'] - store that tracks
  23. * embedded sources found in source maps, one of 'memory' or 'file'
  24. * @param {String} opts.tmpdir [opts.tmpdir=undefined] - temporary directory
  25. * to use for storing files.
  26. * @constructor
  27. */
  28. constructor(opts = {}) {
  29. this.baseDir = opts.baseDir || null;
  30. this.verbose = opts.verbose || false;
  31. this.sourceStore = sourceStore.create(opts.sourceStore, {
  32. tmpdir: opts.tmpdir
  33. });
  34. this.data = Object.create(null);
  35. }
  36. /**
  37. * Registers a source map URL with this store. It makes some input sanity checks
  38. * and silently fails on malformed input.
  39. * @param transformedFilePath - the file path for which the source map is valid.
  40. * This must *exactly* match the path stashed for the coverage object to be
  41. * useful.
  42. * @param sourceMapUrl - the source map URL, **not** a comment
  43. */
  44. registerURL(transformedFilePath, sourceMapUrl) {
  45. const d = 'data:';
  46. if (
  47. sourceMapUrl.length > d.length &&
  48. sourceMapUrl.substring(0, d.length) === d
  49. ) {
  50. const b64 = 'base64,';
  51. const pos = sourceMapUrl.indexOf(b64);
  52. if (pos > 0) {
  53. this.data[transformedFilePath] = {
  54. type: 'encoded',
  55. data: sourceMapUrl.substring(pos + b64.length)
  56. };
  57. } else {
  58. debug(`Unable to interpret source map URL: ${sourceMapUrl}`);
  59. }
  60. return;
  61. }
  62. const dir = path.dirname(path.resolve(transformedFilePath));
  63. const file = path.resolve(dir, sourceMapUrl);
  64. this.data[transformedFilePath] = { type: 'file', data: file };
  65. }
  66. /**
  67. * Registers a source map object with this store. Makes some basic sanity checks
  68. * and silently fails on malformed input.
  69. * @param transformedFilePath - the file path for which the source map is valid
  70. * @param sourceMap - the source map object
  71. */
  72. registerMap(transformedFilePath, sourceMap) {
  73. if (sourceMap && sourceMap.version) {
  74. this.data[transformedFilePath] = {
  75. type: 'object',
  76. data: sourceMap
  77. };
  78. } else {
  79. debug(
  80. 'Invalid source map object: ' +
  81. JSON.stringify(sourceMap, null, 2)
  82. );
  83. }
  84. }
  85. /**
  86. * Transforms the coverage map provided into one that refers to original
  87. * sources when valid mappings have been registered with this store.
  88. * @param {CoverageMap} coverageMap - the coverage map to transform
  89. * @returns {Object} an object with 2 properties. `map` for the transformed
  90. * coverage map and `sourceFinder` which is a function to return the source
  91. * text for a file.
  92. */
  93. transformCoverage(coverageMap) {
  94. const sourceFinder = filePath => {
  95. const content = this.sourceStore.getSource(filePath);
  96. if (content !== null) {
  97. return content;
  98. }
  99. if (path.isAbsolute(filePath)) {
  100. return fs.readFileSync(filePath, 'utf8');
  101. }
  102. return fs.readFileSync(
  103. pathutils.asAbsolute(filePath, this.baseDir)
  104. );
  105. };
  106. coverageMap.files().forEach(file => {
  107. const coverage = coverageMap.fileCoverageFor(file);
  108. if (coverage.data.inputSourceMap && !this.data[file]) {
  109. this.registerMap(file, coverage.data.inputSourceMap);
  110. }
  111. });
  112. if (Object.keys(this.data).length === 0) {
  113. return {
  114. map: coverageMap,
  115. sourceFinder
  116. };
  117. }
  118. const mappedCoverage = transformer
  119. .create(filePath => {
  120. try {
  121. if (!this.data[filePath]) {
  122. return null;
  123. }
  124. const d = this.data[filePath];
  125. let obj;
  126. if (d.type === 'file') {
  127. obj = JSON.parse(fs.readFileSync(d.data, 'utf8'));
  128. } else if (d.type === 'encoded') {
  129. obj = JSON.parse(
  130. Buffer.from(d.data, 'base64').toString()
  131. );
  132. } else {
  133. obj = d.data;
  134. }
  135. const smc = new SMC(obj);
  136. smc.sources.forEach(s => {
  137. const content = smc.sourceContentFor(s);
  138. if (content) {
  139. const sourceFilePath = pathutils.relativeTo(
  140. s,
  141. filePath
  142. );
  143. this.sourceStore.registerSource(
  144. sourceFilePath,
  145. content
  146. );
  147. }
  148. });
  149. return smc;
  150. } catch (error) {
  151. debug('Error returning source map for ' + filePath);
  152. debug(error.stack);
  153. return null;
  154. }
  155. })
  156. .transform(coverageMap);
  157. return {
  158. map: mappedCoverage,
  159. sourceFinder
  160. };
  161. }
  162. /**
  163. * Disposes temporary resources allocated by this map store
  164. */
  165. dispose() {
  166. this.sourceStore.dispose();
  167. }
  168. }
  169. module.exports = { MapStore };