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