source: trip-planner-front/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
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 {{[k: string]: any}} Cache */
12
13function getCacheId(request, withContext) {
14 return JSON.stringify({
15 context: withContext ? request.context : "",
16 path: request.path,
17 query: request.query,
18 fragment: request.fragment,
19 request: request.request
20 });
21}
22
23module.exports = class UnsafeCachePlugin {
24 /**
25 * @param {string | ResolveStepHook} source source
26 * @param {function(ResolveRequest): boolean} filterPredicate filterPredicate
27 * @param {Cache} cache cache
28 * @param {boolean} withContext withContext
29 * @param {string | ResolveStepHook} target target
30 */
31 constructor(source, filterPredicate, cache, withContext, target) {
32 this.source = source;
33 this.filterPredicate = filterPredicate;
34 this.withContext = withContext;
35 this.cache = cache;
36 this.target = target;
37 }
38
39 /**
40 * @param {Resolver} resolver the resolver
41 * @returns {void}
42 */
43 apply(resolver) {
44 const target = resolver.ensureHook(this.target);
45 resolver
46 .getHook(this.source)
47 .tapAsync("UnsafeCachePlugin", (request, resolveContext, callback) => {
48 if (!this.filterPredicate(request)) return callback();
49 const cacheId = getCacheId(request, this.withContext);
50 const cacheEntry = this.cache[cacheId];
51 if (cacheEntry) {
52 return callback(null, cacheEntry);
53 }
54 resolver.doResolve(
55 target,
56 request,
57 null,
58 resolveContext,
59 (err, result) => {
60 if (err) return callback(err);
61 if (result) return callback(null, (this.cache[cacheId] = result));
62 callback();
63 }
64 );
65 });
66 }
67};
Note: See TracBrowser for help on using the repository browser.