cache.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (err) {
  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. } = params;
  92. const file = path.join(directory, filename(source, cacheIdentifier, options));
  93. try {
  94. // No errors mean that the file was previously cached
  95. // we just need to return it
  96. return await read(file, cacheCompression);
  97. } catch (err) {}
  98. const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
  99. // Make sure the directory exists.
  100. try {
  101. // overwrite directory if exists
  102. await mkdir(directory, {
  103. recursive: true
  104. });
  105. } catch (err) {
  106. if (fallback) {
  107. return handleCache(os.tmpdir(), params);
  108. }
  109. throw err;
  110. }
  111. // Otherwise just transform the file
  112. // return it to the user asap and write it in cache
  113. const result = await transform(source, options);
  114. // Do not cache if there are external dependencies,
  115. // since they might change and we cannot control it.
  116. if (!result.externalDependencies.length) {
  117. try {
  118. await write(file, cacheCompression, result);
  119. } catch (err) {
  120. if (fallback) {
  121. // Fallback to tmpdir if node_modules folder not writable
  122. return handleCache(os.tmpdir(), params);
  123. }
  124. throw err;
  125. }
  126. }
  127. return result;
  128. };
  129. /**
  130. * Retrieve file from cache, or create a new one for future reads
  131. *
  132. * @async
  133. * @param {Object} params
  134. * @param {String} params.cacheDirectory Directory to store cached files
  135. * @param {String} params.cacheIdentifier Unique identifier to bust cache
  136. * @param {Boolean} params.cacheCompression Whether compressing cached files
  137. * @param {String} params.source Original contents of the file to be cached
  138. * @param {Object} params.options Options to be given to the transform fn
  139. *
  140. * @example
  141. *
  142. * const result = await cache({
  143. * cacheDirectory: '.tmp/cache',
  144. * cacheIdentifier: 'babel-loader-cachefile',
  145. * cacheCompression: false,
  146. * source: *source code from file*,
  147. * options: {
  148. * experimental: true,
  149. * runtime: true
  150. * },
  151. * });
  152. */
  153. module.exports = async function (params) {
  154. let directory;
  155. if (typeof params.cacheDirectory === "string") {
  156. directory = params.cacheDirectory;
  157. } else {
  158. if (defaultCacheDirectory === null) {
  159. const {
  160. default: findCacheDir
  161. } = await findCacheDirP;
  162. defaultCacheDirectory = findCacheDir({
  163. name: "babel-loader"
  164. }) || os.tmpdir();
  165. }
  166. directory = defaultCacheDirectory;
  167. }
  168. return await handleCache(directory, params);
  169. };