bv-ui-core
Advanced tools
Comparing version 2.9.6 to 2.9.7
@@ -13,14 +13,52 @@ /** | ||
this.fetchPromises = new Map(); | ||
this.cachedUrls = new Set(); | ||
this.cachedRequests = new Set(); | ||
/** | ||
* Generates a unique cache key for the given URL and options. | ||
* @param {string} url - The URL of the API endpoint. | ||
* @param {Object} options - Optional request options. | ||
* @returns {string} The generated cache key. | ||
* Checks if a request is present in a set of cached URLs. | ||
* | ||
* @param {Set} cachedRequests - A set of cached request objects. | ||
* @param {Object} cacheKey - The request object to check for in the cachedRequests set. | ||
* @param {string} cacheKey.url - The URL of the request. | ||
* @param {Headers} cacheKey.headers - The headers of the request. | ||
* @returns {boolean} - Returns true if the request is found in the cachedRequests set, otherwise false. | ||
*/ | ||
function isRequestInSet (cachedRequests, cacheKey) { | ||
try { | ||
// Convert the Set to an array and check if any request matches | ||
return [...cachedRequests].some((cachedRequest) => { | ||
// Compare URLs | ||
if (cachedRequest.url !== cacheKey.url) { | ||
return false; | ||
} | ||
// Compare headers | ||
const cachedHeaders = [...cachedRequest.headers.entries()]; | ||
const keyHeaders = [...cacheKey.headers.entries()]; | ||
if (cachedHeaders.length !== keyHeaders.length) { | ||
return false; // Different number of headers | ||
} | ||
return cachedHeaders.every(([key, value]) => { | ||
return cacheKey.headers.get(key) === value; | ||
}); | ||
}); | ||
} | ||
catch (error) { | ||
console.warn('Error checking in if request is in cache: ', error); | ||
return false; | ||
} | ||
} | ||
/** | ||
* Creates a new Request object with the given URL and options. | ||
* | ||
* @param {string} url - The URL to which the request is sent. | ||
* @param {Object} options - The options to apply to the request. | ||
* @returns {Request} The created Request object. | ||
*/ | ||
this.generateCacheKey = (url, options) => { | ||
const optionsString = (Object.keys(options).length > 0) ? JSON.stringify(options) : ''; | ||
const key = url + optionsString; | ||
const key = new Request(url, options); | ||
return key; | ||
@@ -30,7 +68,7 @@ }; | ||
/** | ||
* Retrieves cached URLs from the cache storage associated with the provided cache name. | ||
* Retrieves cached Requests from the cache storage associated with the provided cache name. | ||
* @returns {void} | ||
*/ | ||
this.retrieveCachedUrls = () => { | ||
this.retrievecachedRequests = () => { | ||
// Open the Cache Storage | ||
@@ -41,11 +79,17 @@ caches.open(this.cacheName).then(cache => { | ||
keys.forEach(request => { | ||
this.cachedUrls.add(request.url); | ||
const headers = {}; | ||
request.headers.forEach((value, key) => { | ||
headers[key] = value; | ||
}); | ||
// Generate the cache key with headers and URL | ||
const cacheKey = this.generateCacheKey(request.url, { headers }); | ||
this.cachedRequests.add(cacheKey); // Add to the set | ||
}); | ||
}); | ||
}); | ||
} | ||
//callretrieveCachedUrls function to set the cache URL set with the cached URLS | ||
this.retrieveCachedUrls(); | ||
//callretrievecachedRequests function to set the cache URL set with the cached URLS | ||
this.retrievecachedRequests(); | ||
@@ -112,4 +156,4 @@ /** | ||
cache.put(cacheKey, newResponse); | ||
//add key to cachedUrls set | ||
this.cachedUrls.add(cacheKey); | ||
//add key to cachedRequests set | ||
this.cachedRequests.add(cacheKey); | ||
}); | ||
@@ -129,4 +173,4 @@ }); | ||
this.fetchFromCache = (cacheKey) => { | ||
// Check if the URL is in the set of cached URLs | ||
if (!this.cachedUrls.has(cacheKey)) { | ||
// Check if the URL is in the set of cached requests set | ||
if (!isRequestInSet(this.cachedRequests, cacheKey)) { | ||
return Promise.resolve(null); | ||
@@ -141,3 +185,3 @@ } | ||
if (!cachedResponse) { | ||
this.cachedUrls.delete(cacheKey) | ||
this.cachedRequests.delete(cacheKey) | ||
return Promise.resolve(null); | ||
@@ -227,3 +271,3 @@ } | ||
cache.delete(key); | ||
this.cachedUrls.delete(key); | ||
this.cachedRequests.delete(key); | ||
} | ||
@@ -276,3 +320,3 @@ }); | ||
cache.delete(entry.key); | ||
this.cachedUrls.delete(entry.key); | ||
this.cachedRequests.delete(entry.key); | ||
currentSize -= entry.size; | ||
@@ -279,0 +323,0 @@ } |
@@ -24,9 +24,9 @@ # BvFetch | ||
## generateCacheKey Return Value: | ||
`string:` The generated cache key. | ||
`Request:` The generated cache key. | ||
## retrieveCachedUrls Method | ||
Retrieves cached URLs from the cache storage associated with the provided cache name. | ||
## retrieveCachedUrls Parameters | ||
## retrievecachedRequests Method | ||
Retrieves cached Requests from the cache storage associated with the provided cache name. | ||
## retrievecachedRequests Parameters | ||
This method takes no parameters. | ||
## retrieveCachedUrls Return Value | ||
## retrievecachedRequests Return Value | ||
`void:` This method does not return anything. | ||
@@ -33,0 +33,0 @@ |
{ | ||
"name": "bv-ui-core", | ||
"version": "2.9.6", | ||
"version": "2.9.7", | ||
"license": "Apache 2.0", | ||
@@ -5,0 +5,0 @@ "description": "Bazaarvoice UI-related JavaScript", |
@@ -41,6 +41,12 @@ //Imports | ||
const url = 'https://jsonplaceholder.typicode.com/todos'; | ||
const options = {}; | ||
const expectedKey = 'https://jsonplaceholder.typicode.com/todos'; | ||
const options = { | ||
method: 'GET', | ||
headers: { | ||
'X-Header-1': 'Value 1', | ||
} | ||
}; | ||
const expectedKey = new Request(url, options) | ||
const generatedKey = bvFetchInstance.generateCacheKey(url, options); | ||
expect(generatedKey).to.equal(expectedKey); | ||
expect(generatedKey.url).to.equal(expectedKey.url); | ||
expect(generatedKey.headers).to.deep.equal(expectedKey.headers); | ||
}); | ||
@@ -51,3 +57,8 @@ | ||
const url = 'https://jsonplaceholder.typicode.com/todos'; | ||
const options = {}; | ||
const options = { | ||
method: 'GET', | ||
headers: { | ||
'X-Header-1': 'Value 1', | ||
} | ||
}; | ||
@@ -69,3 +80,4 @@ // Mocking cache response | ||
match: (key) => { | ||
expect(key).to.equal(cacheKey); | ||
expect(key.url).to.equal(cacheKey.url); | ||
expect(key.headers).to.deep.equal(cacheKey.headers); | ||
return Promise.resolve(mockResponse) | ||
@@ -80,3 +92,3 @@ }, | ||
// Simulate that the response is cached | ||
bvFetchInstance.cachedUrls.add(cacheKey); | ||
bvFetchInstance.cachedRequests.add(cacheKey); | ||
@@ -83,0 +95,0 @@ // Call the function under test |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
265895
6199
7