Cache.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
  7. const {
  8. makeWebpackError,
  9. makeWebpackErrorCallback
  10. } = require("./errors/HookWebpackError");
  11. /**
  12. * Cache validation token whose string representation identifies the build
  13. * inputs associated with a cached value.
  14. * @typedef {object} Etag
  15. * @property {() => string} toString
  16. */
  17. /**
  18. * Completion callback used by cache operations that either fail with a `Error` or resolve with a typed result.
  19. * @template T
  20. * @callback CallbackCache
  21. * @param {Error | null} err
  22. * @param {T=} result
  23. * @returns {void}
  24. */
  25. /** @typedef {EXPECTED_ANY} Data */
  26. /**
  27. * Handler invoked after a cache read succeeds so additional cache layers can
  28. * react to the retrieved value.
  29. * @template T
  30. * @callback GotHandler
  31. * @param {T} result
  32. * @param {() => void} callback
  33. * @returns {void}
  34. */
  35. /**
  36. * Creates a callback wrapper that waits for a fixed number of completions and
  37. * forwards the first error immediately.
  38. * @param {number} times times
  39. * @param {(err?: Error | null) => void} callback callback
  40. * @returns {(err?: Error | null) => void} callback
  41. */
  42. const needCalls = (times, callback) => (err) => {
  43. if (--times === 0) {
  44. return callback(err);
  45. }
  46. if (err && times > 0) {
  47. times = 0;
  48. return callback(err);
  49. }
  50. };
  51. /**
  52. * Abstract cache interface backed by tapable hooks for reading, writing, idle
  53. * transitions, and shutdown across webpack cache implementations.
  54. */
  55. class Cache {
  56. /**
  57. * Initializes the cache lifecycle hooks implemented by cache backends.
  58. */
  59. constructor() {
  60. this.hooks = {
  61. /** @type {AsyncSeriesBailHook<[string, Etag | null, GotHandler<EXPECTED_ANY>[]], Data>} */
  62. get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
  63. /** @type {AsyncParallelHook<[string, Etag | null, Data]>} */
  64. store: new AsyncParallelHook(["identifier", "etag", "data"]),
  65. /** @type {AsyncParallelHook<[Iterable<string>]>} */
  66. storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
  67. /** @type {SyncHook<[]>} */
  68. beginIdle: new SyncHook([]),
  69. /** @type {AsyncParallelHook<[]>} */
  70. endIdle: new AsyncParallelHook([]),
  71. /** @type {AsyncParallelHook<[]>} */
  72. shutdown: new AsyncParallelHook([])
  73. };
  74. }
  75. /**
  76. * Retrieves a cached value and lets registered `gotHandlers` observe the
  77. * result before the caller receives it.
  78. * @template T
  79. * @param {string} identifier the cache identifier
  80. * @param {Etag | null} etag the etag
  81. * @param {CallbackCache<T>} callback signals when the value is retrieved
  82. * @returns {void}
  83. */
  84. get(identifier, etag, callback) {
  85. /** @type {GotHandler<T>[]} */
  86. const gotHandlers = [];
  87. this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
  88. if (err) {
  89. callback(makeWebpackError(err, "Cache.hooks.get"));
  90. return;
  91. }
  92. if (result === null) {
  93. result = undefined;
  94. }
  95. if (gotHandlers.length > 1) {
  96. const innerCallback = needCalls(gotHandlers.length, () =>
  97. callback(null, result)
  98. );
  99. for (const gotHandler of gotHandlers) {
  100. gotHandler(result, innerCallback);
  101. }
  102. } else if (gotHandlers.length === 1) {
  103. gotHandlers[0](result, () => callback(null, result));
  104. } else {
  105. callback(null, result);
  106. }
  107. });
  108. }
  109. /**
  110. * Stores a cache entry for the identifier and etag through the registered
  111. * cache backend hooks.
  112. * @template T
  113. * @param {string} identifier the cache identifier
  114. * @param {Etag | null} etag the etag
  115. * @param {T} data the value to store
  116. * @param {CallbackCache<void>} callback signals when the value is stored
  117. * @returns {void}
  118. */
  119. store(identifier, etag, data, callback) {
  120. this.hooks.store.callAsync(
  121. identifier,
  122. etag,
  123. data,
  124. makeWebpackErrorCallback(callback, "Cache.hooks.store")
  125. );
  126. }
  127. /**
  128. * Persists the set of build dependencies required to determine whether the
  129. * cache can be restored in a future compilation.
  130. * @param {Iterable<string>} dependencies list of all build dependencies
  131. * @param {CallbackCache<void>} callback signals when the dependencies are stored
  132. * @returns {void}
  133. */
  134. storeBuildDependencies(dependencies, callback) {
  135. this.hooks.storeBuildDependencies.callAsync(
  136. dependencies,
  137. makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
  138. );
  139. }
  140. /**
  141. * Signals that webpack is entering an idle phase and cache backends may flush
  142. * or compact pending work.
  143. * @returns {void}
  144. */
  145. beginIdle() {
  146. this.hooks.beginIdle.call();
  147. }
  148. /**
  149. * Signals that webpack is leaving the idle phase and waits for cache
  150. * backends to finish any asynchronous resume work.
  151. * @param {CallbackCache<void>} callback signals when the call finishes
  152. * @returns {void}
  153. */
  154. endIdle(callback) {
  155. this.hooks.endIdle.callAsync(
  156. makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
  157. );
  158. }
  159. /**
  160. * Shuts down every registered cache backend and waits for cleanup to finish.
  161. * @param {CallbackCache<void>} callback signals when the call finishes
  162. * @returns {void}
  163. */
  164. shutdown(callback) {
  165. this.hooks.shutdown.callAsync(
  166. makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
  167. );
  168. }
  169. }
  170. Cache.STAGE_MEMORY = -10;
  171. Cache.STAGE_DEFAULT = 0;
  172. Cache.STAGE_DISK = 10;
  173. Cache.STAGE_NETWORK = 20;
  174. module.exports = Cache;