Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoSign in
Socket

@algolia/cache-browser-local-storage

Package Overview
Dependencies
Maintainers
4
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@algolia/cache-browser-local-storage - npm Package Compare versions

Comparing version
4.26.0
to
4.27.0
+34
-26
dist/cache-browser-local-storage.cjs.js

@@ -5,2 +5,14 @@ 'use strict';

function yieldToMain() {
// eslint-disable-next-line no-undef
const g = typeof globalThis !== 'undefined' ? globalThis : undefined;
if (g && g.scheduler && g.scheduler.yield) {
return g.scheduler.yield().catch((error) => {
// eslint-disable-next-line no-console
console.error('Failed to yield to main: ', error);
return new Promise(resolve => setTimeout(resolve, 0));
});
}
return new Promise(resolve => setTimeout(resolve, 0));
}
function createBrowserLocalStorageCache(options) {

@@ -22,17 +34,15 @@ const namespaceKey = `algoliasearch-client-js-${options.key}`;

};
const removeOutdatedCacheItems = () => {
const getFilteredNamespace = () => {
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
const namespace = getNamespace();
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
return cacheItem.timestamp !== undefined;
const currentTime = new Date().getTime();
return Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
if (!cacheItem || cacheItem.timestamp === undefined) {
return false;
}
if (!timeToLive) {
return true;
}
return cacheItem.timestamp + timeToLive >= currentTime;
}));
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems);
if (!timeToLive)
return;
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => {
const currentTimestamp = new Date().getTime();
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
return !isExpired;
}));
setNamespace(filteredNamespaceWithoutExpiredItems);
};

@@ -43,18 +53,16 @@ return {

}) {
return Promise.resolve()
.then(() => {
removeOutdatedCacheItems();
return yieldToMain().then(() => {
const namespace = getFilteredNamespace();
const keyAsString = JSON.stringify(key);
return getNamespace()[keyAsString];
})
.then(value => {
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
})
.then(([value, exists]) => {
return Promise.all([value, exists || events.miss(value)]);
})
.then(([value]) => value);
const cachedItem = namespace[keyAsString];
setNamespace(namespace);
if (cachedItem) {
return cachedItem.value;
}
// eslint-disable-next-line promise/no-nesting
return defaultValue().then((value) => events.miss(value).then(() => value));
});
},
set(key, value) {
return Promise.resolve().then(() => {
return yieldToMain().then(() => {
const namespace = getNamespace();

@@ -71,3 +79,3 @@ // eslint-disable-next-line functional/immutable-data

delete(key) {
return Promise.resolve().then(() => {
return yieldToMain().then(() => {
const namespace = getNamespace();

@@ -74,0 +82,0 @@ // eslint-disable-next-line functional/immutable-data

@@ -0,1 +1,13 @@

function yieldToMain() {
// eslint-disable-next-line no-undef
const g = typeof globalThis !== 'undefined' ? globalThis : undefined;
if (g && g.scheduler && g.scheduler.yield) {
return g.scheduler.yield().catch((error) => {
// eslint-disable-next-line no-console
console.error('Failed to yield to main: ', error);
return new Promise(resolve => setTimeout(resolve, 0));
});
}
return new Promise(resolve => setTimeout(resolve, 0));
}
function createBrowserLocalStorageCache(options) {

@@ -17,17 +29,15 @@ const namespaceKey = `algoliasearch-client-js-${options.key}`;

};
const removeOutdatedCacheItems = () => {
const getFilteredNamespace = () => {
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
const namespace = getNamespace();
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
return cacheItem.timestamp !== undefined;
const currentTime = new Date().getTime();
return Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
if (!cacheItem || cacheItem.timestamp === undefined) {
return false;
}
if (!timeToLive) {
return true;
}
return cacheItem.timestamp + timeToLive >= currentTime;
}));
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems);
if (!timeToLive)
return;
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => {
const currentTimestamp = new Date().getTime();
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
return !isExpired;
}));
setNamespace(filteredNamespaceWithoutExpiredItems);
};

@@ -38,18 +48,16 @@ return {

}) {
return Promise.resolve()
.then(() => {
removeOutdatedCacheItems();
return yieldToMain().then(() => {
const namespace = getFilteredNamespace();
const keyAsString = JSON.stringify(key);
return getNamespace()[keyAsString];
})
.then(value => {
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
})
.then(([value, exists]) => {
return Promise.all([value, exists || events.miss(value)]);
})
.then(([value]) => value);
const cachedItem = namespace[keyAsString];
setNamespace(namespace);
if (cachedItem) {
return cachedItem.value;
}
// eslint-disable-next-line promise/no-nesting
return defaultValue().then((value) => events.miss(value).then(() => value));
});
},
set(key, value) {
return Promise.resolve().then(() => {
return yieldToMain().then(() => {
const namespace = getNamespace();

@@ -66,3 +74,3 @@ // eslint-disable-next-line functional/immutable-data

delete(key) {
return Promise.resolve().then(() => {
return yieldToMain().then(() => {
const namespace = getNamespace();

@@ -69,0 +77,0 @@ // eslint-disable-next-line functional/immutable-data

{
"name": "@algolia/cache-browser-local-storage",
"version": "4.26.0",
"version": "4.27.0",
"private": false,

@@ -20,4 +20,4 @@ "description": "Promise-based cache library for browser using local storage.",

"dependencies": {
"@algolia/cache-common": "4.26.0"
"@algolia/cache-common": "4.27.0"
}
}