Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood
The 'store' npm package is a simple and lightweight library for managing key-value pairs in the browser's local storage. It provides an easy-to-use API for setting, getting, and removing data from local storage, making it useful for persisting user preferences, session data, and other client-side information.
Set a value
This feature allows you to store a key-value pair in the local storage. The 'set' method takes two arguments: the key and the value you want to store.
store.set('key', 'value');
Get a value
This feature allows you to retrieve a value from the local storage using its key. The 'get' method takes one argument: the key of the value you want to retrieve.
store.get('key');
Remove a value
This feature allows you to remove a key-value pair from the local storage. The 'remove' method takes one argument: the key of the value you want to remove.
store.remove('key');
Clear all values
This feature allows you to clear all key-value pairs from the local storage. The 'clear' method does not take any arguments.
store.clear();
LocalForage is a fast and simple storage library for JavaScript. It improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API. Compared to 'store', LocalForage supports larger data storage and asynchronous operations.
Store2 is a versatile and powerful library for managing local storage, session storage, and cookies. It provides a unified API for all three storage types, making it more flexible than 'store'. Store2 also supports namespaces and advanced data manipulation methods.
IDB-Keyval is a small library that provides a simple key-value store backed by IndexedDB. It offers a promise-based API for asynchronous operations, making it suitable for storing larger amounts of data compared to 'store'. IDB-Keyval is ideal for applications that require more robust storage solutions.
Store.js has been around since 2010 (first commit! HN discussion!), and is live on tens of thousands of websites - like cnn.com!
For many years v1.x provided basic cross-browser persistent storage, and over time more and more people started asking for additional functionality.
Store.js version 2 is a full revamp with pluggable storage (it will automatically fall back to one that works in every scenario by default), pluggable extra functionality (like expirations, default values, common array/object operations, etc), and fully cross-browser automatic testing using saucelabs.com.
All you need to know to get started:
store.js exposes a simple API for cross-browser local storage:
// Store current user
store.set('user', { name:'Marcus' })
// Get current user
store.get('user')
// Remove current user
store.remove('user')
// Clear all keys
store.clearAll()
// Loop over all stored values
store.each(function(value, key) {
console.log(key, '==', value)
})
Using npm:
// Example store.js usage with npm
var store = require('store')
store.set('user', { name:'Marcus' })
store.get('user').name == 'Marcus'
Using script tag (first download one of the builds):
<!-- Example store.js usage with script tag -->
<script src="path/to/my/store.legacy.min.js"></script>
<script>
var store = require('store')
store.set('user', { name:'Marcus' })
store.get('user').name == 'Marcus'
</script>
All of them, pretty much :)
To support all browsers (including IE 6, IE 7, Firefox 4, etc.), use require('store')
(alias for require('store/dist/store.legacy')
) or store.legacy.min.js.
To save some kilobytes but still support all modern browsers, use require('store/dist/store.modern')
or store.modern.min.js instead.
Plugins provide additional common functionality that some users might need:
With npm:
// Example plugin usage:
var expirePlugin = require('store/plugins/expire')
store.addPlugin(expirePlugin)
If you're using script tags, you can either use store.everything.min.js (which
has all plugins built-in), or clone this repo to add or modify a build and run make build
.
A store.js plugin is a function that returns an object that gets added to the store. If any of the plugin functions overrides existing functions, the plugin function can still call the original function using the first argument (super_fn).
// Example plugin that stores a version history of every value
var versionHistoryPlugin = function() {
var historyStore = this.namespace('history')
return {
set: function(super_fn, key, value) {
var history = historyStore.get(key) || []
history.push(value)
historyStore.set(key, history)
return super_fn()
},
getHistory: function(key) {
return historyStore.get(key)
}
}
}
store.addPlugin(versionHistoryPlugin)
store.set('foo', 'bar 1')
store.set('foo', 'bar 2')
store.getHistory('foo') == ['bar 1', 'bar 2']
Let me know if you need more info on writing plugins. For the moment I recommend taking a look at the current plugins. Good example plugins are plugins/defaults, plugins/expire and plugins/events.
Choose which build is right for you!
If you're using npm you can create your own build:
// Example custom build usage:
var engine = require('store/src/store-engine')
var storages = [
require('store/storages/localStorage'),
require('store/storages/cookieStorage')
]
var plugins = [
require('store/plugins/defaults'),
require('store/plugins/expire')
]
var store = engine.createStore(storages, plugins)
store.set('foo', 'bar', new Date().getTime() + 3000) // Using expire plugin to expire in 3 seconds
Store.js will pick the best available storage, and automatically falls back to the first available storage that works:
Each storage has different limits, restrictions and overflow behavior on different browser. For example, Android has has a 4.57M localStorage limit in 4.0, a 2.49M limit in 4.1, and a 4.98M limit in 4.2... Yeah.
To simplify things we provide these recommendations to ensure cross browser behavior:
Storage | Targets | Recommendations | More info |
---|---|---|---|
all | All browsers | Store < 1 million characters | (Except Safari Private mode) |
all | All & Private mode | Store < 32 thousand characters | (Including Safari Private mode) |
localStorage | Modern browsers | Max 2mb (~1M chars) | limits, android |
sessionStorage | Modern browsers | Max 5mb (~2M chars) | limits |
cookieStorage | Safari Private mode | Max 4kb (~2K chars) | limits |
userDataStorage | IE5, IE6 & IE7 | Max 64kb (~32K chars) | limits |
globalStorage | Firefox 2-5 | Max 5mb (~2M chars) | limits |
memoryStorage | All browsers, fallback | Does not persist across pages! |
Chances are you won't ever need another storage. But if you do...
See storages/ for examples. Two good examples are memoryStorage and localStorage.
Basically, you just need an object that looks like this:
// Example custom storage
var storage = {
name: 'myStorage',
read: function(key) { ... },
write: function(key, value) { ... },
each: function(fn) { ... },
remove: function(key) { ... },
clearAll: function() { ... }
}
var store = require('store').createStore(storage)
FAQs
A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood
The npm package store receives a total of 204,686 weekly downloads. As such, store popularity was classified as popular.
We found that store 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.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.