Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@data-provider/browser-storage
Advanced tools
Data Provider addon providing localStorage and sessionStorage origins
It provides CRUD methods for objects stored in localStorage
or sessionStorage
.
Create a new provider using the LocalStorage
or SessionStorage
classes.
options
(Object): Apart of common data-provider options, this plugin also accept next options:
id
(String): Id of the provider, will be used also as the key
where the provider data is stored in localStorage
or sessionStorage
.storageFallback
(Boolean): Default true
. If there is an error trying to access to window.localStorage
or window.sessionStorage
, a mock will be used instead, and data will be persisted in memory. This could happen if localStorage
is disabled by the browser, for example. If you want to handle exceptions by yourself, you can disable this behavior setting this option to false
, and then all calls to read
, update
or delete
methods will be rejected with the correspondent error, which will be stored also in the error
property of the provider state.initialState
(Object): Same option than the one described in the data-provider API docs, except the data
property, which in this case has no effect, as the initial data set in the state will be the data retrieved synchronously from the real localStorage
or sessionStorage
.import { LocalStorage } from "@data-provider/browser-storage";
const userPreferences = new LocalStorage({
id: "user-preferences",
storageFallback: false
});
// userPreferences object will be stored in localStorage `user-preferences` key.
// If localStorage is disabled, no in-memory mock will be used instead
const sessionData = new SessionStorage({
id: "user-session"
});
// sessionData object will be stored in sessionStorate `user-session` key.
Read the Data Provider docs for further info about how to use addons.
When querying providers created with this addon, the query object can have one of the next properties:
prop
(String): Specific property of the object from localStorage or sessionStorage to be accessed.import { LocalStorage } from "@data-provider/browser-storage";
const userPreferences = new LocalStorage({
id: "user-preferences"
});
userPreferences.query({ prop: "cookiesAccepted" }).update(true);
userPreferences.query({ prop: "cookiesAccepted" }).read().then(result => {
console.log("Cookies accepted", result);
// true
});
Apart of the common Data Provider methods, next ones are available:
update(data)
Updates an specific property of the stored object when the provider is queried, or the full object when not. When the object is modified, it will automatically cleans the cache of the provider and also the cache of the parent provider when it is queried (as modifying a property also modifies the full object).
data
(Any): New data to be set. (Take into account that provided data will be stringified when saved to localStorage)A promise that will be resolved when the localStorage is updated, or will be rejected with an error in case the operation fails.
// modify an specific property
userPreferences.query({ prop: "cookiesAccepted" }).update(true)
.then(() => {
console.log("Local storage updated!");
})
.catch(error => {
console.log("Error updating local storage", error);
});
// Overwrite full object
userPreferences.update({
cookiesAccepted: true
});
delete()
Removes an specific property of the stored object when the provider is queried, or sets the full object as empty when not. When the object is modified, it will automatically cleans the cache of the provider and also the cache of the parent provider when it is queried (as deleting a property also modifies the full object).
A promise that will be resolved when the localStorage is updated, or will be rejected with an error in case the operation fails.
// removes an specific property
userPreferences.query({ prop: "cookiesAccepted" }).delete()
.then(() => {
console.log("Local storage updated");
})
.catch(error => {
console.log("Error updating local storage", error);
});
// Sets the full object as {}
userPreferences.delete();
Providers created with this addon will have automatically the browser-storage
tag, so you can select all of them together using the providers
methods as in:
import { providers } from "@data-provider/core";
providers.getByTag("browser-storage").cleanCache();
Apart of this common tag, each different type of browser-storage
origin also has next tags:
LocalStorage
: "local-storage"SessionStorage
: "session-storage"Contributors are welcome. Please read the contributing guidelines and code of conduct.
FAQs
Data Provider addon providing localStorage and sessionStorage origins
We found that @data-provider/browser-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.