Socket
Socket
Sign inDemoInstall

@algolia/cache-browser-local-storage

Package Overview
Dependencies
Maintainers
3
Versions
75
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.17.2 to 4.18.0

31

dist/cache-browser-local-storage.cjs.js

@@ -18,2 +18,21 @@ 'use strict';

};
const setNamespace = (namespace) => {
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
};
const removeOutdatedCacheItems = () => {
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
const namespace = getNamespace();
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
return cacheItem.timestamp !== undefined;
}));
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);
};
return {

@@ -25,6 +44,9 @@ get(key, defaultValue, events = {

.then(() => {
removeOutdatedCacheItems();
const keyAsString = JSON.stringify(key);
const value = getNamespace()[keyAsString];
return Promise.all([value || defaultValue(), value !== undefined]);
return getNamespace()[keyAsString];
})
.then(value => {
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
})
.then(([value, exists]) => {

@@ -39,3 +61,6 @@ return Promise.all([value, exists || events.miss(value)]);

// eslint-disable-next-line functional/immutable-data
namespace[JSON.stringify(key)] = value;
namespace[JSON.stringify(key)] = {
timestamp: new Date().getTime(),
value,
};
getStorage().setItem(namespaceKey, JSON.stringify(namespace));

@@ -42,0 +67,0 @@ return value;

import { Cache } from '@algolia/cache-common';
export declare type BrowserLocalStorageCacheItem = {
/**
* The cache item creation timestamp.
*/
readonly timestamp: number;
/**
* The cache item value
*/
readonly value: any;
};
export declare type BrowserLocalStorageOptions = {

@@ -9,2 +20,6 @@ /**

/**
* The time to live for each cached item in seconds.
*/
readonly timeToLive?: number;
/**
* The native local storage implementation.

@@ -11,0 +26,0 @@ */

@@ -14,2 +14,21 @@ function createBrowserLocalStorageCache(options) {

};
const setNamespace = (namespace) => {
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
};
const removeOutdatedCacheItems = () => {
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
const namespace = getNamespace();
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
return cacheItem.timestamp !== undefined;
}));
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);
};
return {

@@ -21,6 +40,9 @@ get(key, defaultValue, events = {

.then(() => {
removeOutdatedCacheItems();
const keyAsString = JSON.stringify(key);
const value = getNamespace()[keyAsString];
return Promise.all([value || defaultValue(), value !== undefined]);
return getNamespace()[keyAsString];
})
.then(value => {
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
})
.then(([value, exists]) => {

@@ -35,3 +57,6 @@ return Promise.all([value, exists || events.miss(value)]);

// eslint-disable-next-line functional/immutable-data
namespace[JSON.stringify(key)] = value;
namespace[JSON.stringify(key)] = {
timestamp: new Date().getTime(),
value,
};
getStorage().setItem(namespaceKey, JSON.stringify(namespace));

@@ -38,0 +63,0 @@ return value;

4

package.json
{
"name": "@algolia/cache-browser-local-storage",
"version": "4.17.2",
"version": "4.18.0",
"private": false,

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

"dependencies": {
"@algolia/cache-common": "4.17.2"
"@algolia/cache-common": "4.18.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc