UNPKG

browser-cache-mock

Version:

a mock for the browser cache that can be used in a node environment

147 lines (146 loc) 5.92 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const utils = __importStar(require("./utils")); /** An in-memory implementation of the browser cache that can be used to test caching strategies in a node environment. */ class CacheMock { constructor() { this.cache = new Map(); } get({ url }) { for (const [key, value] of this.cache.entries()) { if (key.url === url) { return value; } } } getAll({ url }) { let matches = []; for (const [key, value] of this.cache.entries()) { const regex = new RegExp(url, 'i'); if (key.url.match(regex)) { matches.push(value); } } return matches; } set(request, response) { let relevantRequest = request; for (const [key] of this.cache.entries()) { if (key.url === request.url) { relevantRequest = key; break; } } this.cache = this.cache.set(relevantRequest, response); } /** * Takes a URL, retrieves it and adds the resulting response object to the given cache. This is functionally equivalent to calling fetch(), then using put() to add the results to the cache. * @param {RequestInfo} request The request to add to the Cache * @returns {Promise<void>} */ async add(request) { if (utils.isRequest(request) || utils.isString(request)) { const response = await utils.fitch(request); this.cache.set(new Request(request), response); } else { throw utils.invalidRequestParamError(); } } /** * Takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache. * @param {Array<RequestInfo>} requests An array of requests to add to the cache * @returns {Promise<void>} */ async addAll(requests) { for (const request of requests) { await this.add(request); } } /** * Finds the Cache entry whose key is the request, returning a Promise that resolves to true if a matching Cache entry is found and deleted. If no Cache entry is found, the promise resolves to false. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<boolean>} A boolean value indicating whether the a key was deleted from the cache */ async delete(request, options) { const requestUrl = this.getRequestUrl(request, options); const key = (await this.keys()).find(({ url }) => url === requestUrl); if (key) { return this.cache.delete(key); } return false; } getRequestUrl(request, options) { let requestUrl = utils.getRequestUrl(request); if (options && options.ignoreSearch) { requestUrl = utils.removeSearchParams(requestUrl); } return requestUrl; } /** * Returns a Promise that resolves to an array of Cache keys. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<ReadonlyArray<Request>>} A readonly array of matching cache keys */ async keys(request, options) { let keys = Array.from(this.cache.keys()); if (request || (request && options && options.ignoreSearch)) { const requestUrl = this.getRequestUrl(request, options); const regex = new RegExp(requestUrl, 'i'); return keys.filter(({ url }) => url.match(regex)); } return keys; } /** * Returns a Promise that resolves to the response associated with the first matching request in the Cache object. * @param {RequestInfo} request The request to search for * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<Response | undefined>} The match found in the cache, if anything has been found */ async match(request, options) { const req = this.validateRequest(request, options); return this.get(req); } validateRequest(request, options) { let req = utils.validateRequest(request); if (options && options.ignoreSearch) { req = new Request(utils.removeSearchParams(req.url)); } return req; } /** * Returns a Promise that resolves to an array of all matching requests in the Cache object. * @param {RequestInfo} request The request key to match against * @param {CacheQueryOptions} options Additional options to be condsidered when searching * @returns {Promise<ReadonlyArray<Response>>} A readonly array of matching cache responses */ async matchAll(request, options) { if (request) { const req = this.validateRequest(request, options); return this.getAll(req); } else { return Array.from(this.cache.values()); } } /** * Takes both a request and its response and adds it to the given cache. * @param {RequestInfo} request The request key to add to the cache * @param {Response} response The response value to add to the cache key * @returns {Promise<void>} */ async put(request, response) { const newRequest = utils.validateRequest(request); this.set(newRequest, response); } } exports.default = CacheMock;