UnsafeCachePlugin.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Resolver")} Resolver */
  7. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  8. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  9. /** @typedef {import("./Resolver").ResolveContextYield} ResolveContextYield */
  10. /** @typedef {{[k: string]: ResolveRequest | ResolveRequest[] | undefined}} Cache */
  11. /**
  12. * @param {string} type type of cache
  13. * @param {ResolveRequest} request request
  14. * @param {boolean} withContext cache with context?
  15. * @returns {string} cache id
  16. */
  17. function getCacheId(type, request, withContext) {
  18. return JSON.stringify({
  19. type,
  20. context: withContext ? request.context : "",
  21. path: request.path,
  22. query: request.query,
  23. fragment: request.fragment,
  24. request: request.request
  25. });
  26. }
  27. module.exports = class UnsafeCachePlugin {
  28. /**
  29. * @param {string | ResolveStepHook} source source
  30. * @param {function(ResolveRequest): boolean} filterPredicate filterPredicate
  31. * @param {Cache} cache cache
  32. * @param {boolean} withContext withContext
  33. * @param {string | ResolveStepHook} target target
  34. */
  35. constructor(source, filterPredicate, cache, withContext, target) {
  36. this.source = source;
  37. this.filterPredicate = filterPredicate;
  38. this.withContext = withContext;
  39. this.cache = cache;
  40. this.target = target;
  41. }
  42. /**
  43. * @param {Resolver} resolver the resolver
  44. * @returns {void}
  45. */
  46. apply(resolver) {
  47. const target = resolver.ensureHook(this.target);
  48. resolver
  49. .getHook(this.source)
  50. .tapAsync("UnsafeCachePlugin", (request, resolveContext, callback) => {
  51. if (!this.filterPredicate(request)) return callback();
  52. const isYield = typeof resolveContext.yield === "function";
  53. const cacheId = getCacheId(
  54. isYield ? "yield" : "default",
  55. request,
  56. this.withContext
  57. );
  58. const cacheEntry = this.cache[cacheId];
  59. if (cacheEntry) {
  60. if (isYield) {
  61. const yield_ = /** @type {Function} */ (resolveContext.yield);
  62. if (Array.isArray(cacheEntry)) {
  63. for (const result of cacheEntry) yield_(result);
  64. } else {
  65. yield_(cacheEntry);
  66. }
  67. return callback(null, null);
  68. }
  69. return callback(null, /** @type {ResolveRequest} */ (cacheEntry));
  70. }
  71. /** @type {ResolveContextYield|undefined} */
  72. let yieldFn;
  73. /** @type {ResolveContextYield|undefined} */
  74. let yield_;
  75. /** @type {ResolveRequest[]} */
  76. const yieldResult = [];
  77. if (isYield) {
  78. yieldFn = resolveContext.yield;
  79. yield_ = result => {
  80. yieldResult.push(result);
  81. };
  82. }
  83. resolver.doResolve(
  84. target,
  85. request,
  86. null,
  87. yield_ ? { ...resolveContext, yield: yield_ } : resolveContext,
  88. (err, result) => {
  89. if (err) return callback(err);
  90. if (isYield) {
  91. if (result) yieldResult.push(result);
  92. for (const result of yieldResult) {
  93. /** @type {ResolveContextYield} */
  94. (yieldFn)(result);
  95. }
  96. this.cache[cacheId] = yieldResult;
  97. return callback(null, null);
  98. }
  99. if (result) return callback(null, (this.cache[cacheId] = result));
  100. callback();
  101. }
  102. );
  103. });
  104. }
  105. };