cache.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Filesystem Cache
  3. *
  4. * Given a file and a transform function, cache the result into files
  5. * or retrieve the previously cached files if the given file is already known.
  6. *
  7. * @see https://github.com/babel/babel-loader/issues/34
  8. * @see https://github.com/babel/babel-loader/pull/41
  9. */
  10. const os = require("os");
  11. const path = require("path");
  12. const zlib = require("zlib");
  13. const crypto = require("crypto");
  14. const {
  15. promisify
  16. } = require("util");
  17. const {
  18. readFile,
  19. writeFile,
  20. mkdir
  21. } = require("fs/promises");
  22. const findCacheDirP = import("find-cache-dir");
  23. const transform = require("./transform");
  24. // Lazily instantiated when needed
  25. let defaultCacheDirectory = null;
  26. let hashType = "sha256";
  27. // use md5 hashing if sha256 is not available
  28. try {
  29. crypto.createHash(hashType);
  30. } catch {
  31. hashType = "md5";
  32. }
  33. const gunzip = promisify(zlib.gunzip);
  34. const gzip = promisify(zlib.gzip);
  35. /**
  36. * Read the contents from the compressed file.
  37. *
  38. * @async
  39. * @params {String} filename
  40. * @params {Boolean} compress
  41. */
  42. const read = async function (filename, compress) {
  43. const data = await readFile(filename + (compress ? ".gz" : ""));
  44. const content = compress ? await gunzip(data) : data;
  45. return JSON.parse(content.toString());
  46. };
  47. /**
  48. * Write contents into a compressed file.
  49. *
  50. * @async
  51. * @params {String} filename
  52. * @params {Boolean} compress
  53. * @params {String} result
  54. */
  55. const write = async function (filename, compress, result) {
  56. const content = JSON.stringify(result);
  57. const data = compress ? await gzip(content) : content;
  58. return await writeFile(filename + (compress ? ".gz" : ""), data);
  59. };
  60. /**
  61. * Build the filename for the cached file
  62. *
  63. * @params {String} source File source code
  64. * @params {Object} options Options used
  65. *
  66. * @return {String}
  67. */
  68. const filename = function (source, identifier, options) {
  69. const hash = crypto.createHash(hashType);
  70. const contents = JSON.stringify({
  71. source,
  72. options,
  73. identifier
  74. });
  75. hash.update(contents);
  76. return hash.digest("hex") + ".json";
  77. };
  78. /**
  79. * Handle the cache
  80. *
  81. * @params {String} directory
  82. * @params {Object} params
  83. */
  84. const handleCache = async function (directory, params) {
  85. const {
  86. source,
  87. options = {},
  88. cacheIdentifier,
  89. cacheDirectory,
  90. cacheCompression,
  91. logger
  92. } = params;
  93. const file = path.join(directory, filename(source, cacheIdentifier, options));
  94. try {
  95. // No errors mean that the file was previously cached
  96. // we just need to return it
  97. logger.debug(`reading cache file '${file}'`);
  98. return await read(file, cacheCompression);
  99. } catch {
  100. // conitnue if cache can't be read
  101. logger.debug(`discarded cache as it can not be read`);
  102. }
  103. const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
  104. // Make sure the directory exists.
  105. try {
  106. // overwrite directory if exists
  107. logger.debug(`creating cache folder '${directory}'`);
  108. await mkdir(directory, {
  109. recursive: true
  110. });
  111. } catch (err) {
  112. if (fallback) {
  113. return handleCache(os.tmpdir(), params);
  114. }
  115. throw err;
  116. }
  117. // Otherwise just transform the file
  118. // return it to the user asap and write it in cache
  119. logger.debug(`applying Babel transform`);
  120. const result = await transform(source, options);
  121. // Do not cache if there are external dependencies,
  122. // since they might change and we cannot control it.
  123. if (!result.externalDependencies.length) {
  124. try {
  125. logger.debug(`writing result to cache file '${file}'`);
  126. await write(file, cacheCompression, result);
  127. } catch (err) {
  128. if (fallback) {
  129. // Fallback to tmpdir if node_modules folder not writable
  130. return handleCache(os.tmpdir(), params);
  131. }
  132. throw err;
  133. }
  134. }
  135. return result;
  136. };
  137. /**
  138. * Retrieve file from cache, or create a new one for future reads
  139. *
  140. * @async
  141. * @param {Object} params
  142. * @param {String} params.cacheDirectory Directory to store cached files
  143. * @param {String} params.cacheIdentifier Unique identifier to bust cache
  144. * @param {Boolean} params.cacheCompression Whether compressing cached files
  145. * @param {String} params.source Original contents of the file to be cached
  146. * @param {Object} params.options Options to be given to the transform fn
  147. *
  148. * @example
  149. *
  150. * const result = await cache({
  151. * cacheDirectory: '.tmp/cache',
  152. * cacheIdentifier: 'babel-loader-cachefile',
  153. * cacheCompression: false,
  154. * source: *source code from file*,
  155. * options: {
  156. * experimental: true,
  157. * runtime: true
  158. * },
  159. * });
  160. */
  161. module.exports = async function (params) {
  162. let directory;
  163. if (typeof params.cacheDirectory === "string") {
  164. directory = params.cacheDirectory;
  165. } else {
  166. if (defaultCacheDirectory === null) {
  167. const {
  168. default: findCacheDir
  169. } = await findCacheDirP;
  170. defaultCacheDirectory = findCacheDir({
  171. name: "babel-loader"
  172. }) || os.tmpdir();
  173. }
  174. directory = defaultCacheDirectory;
  175. }
  176. return await handleCache(directory, params);
  177. };