cache.js 4.6 KB

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