Bulletproof persistent browser storage, works with disabled Cookies and/or localStorage
npm install ClientStorage

![]()
- 👷 __100% Tests coverage__;
- 📦 No external dependencies;
- 💪 Bulletproof persistent Client storage;
- ㊗️ With Unicode support for values and keys;
- 👨💻 With String, Array, Object, and Boolean support as values;
- ♿ Works with disabled localStorage and cookies;
- Available via 📦 NPM and ☄️ Atmosphere.
!ClientStorage NPM library logo
``shell`
npm install --save ClientStorage
`shell`Via Atmosphere
meteor add ostrio:cstorage
`shell`Via NPM
meteor npm install --save ClientStorage
`js`
var ClientStorage = require('ClientStorage').ClientStorage;
var clientStorage = new ClientStorage();
`js`
import { ClientStorage } from 'ClientStorage';
const clientStorage = new ClientStorage();
`js`
import { ClientStorage } from 'meteor/ostrio:cstorage';
const clientStorage = new ClientStorage();
- clientStorage.get('key') - Read a record. If the key doesn't exist a undefined value will be returned;key
- - {String} - Record's key;clientStorage.set('key', value[, ttl])
- - Create/overwrite a value in storage;key
- - {String} - Record's key;value
- - {String|[mix]|Boolean|Object} - Record's value (content);ttl
- - {Number} — [Optional] Record's TTL in seconds;clientStorage.remove('key')
- - Remove a record;key
- - {String} - Record's key;clientStorage.has('key')
- - Check whether a record exists, returns a boolean value;key
- - {String} - Record's key;clientStorage.keys()
- - Returns an array of all storage keys;clientStorage.empty()
- - Empty storage (remove all key/value pairs). __Use with caution! (May remove cookies which weren't set by you)__.
By default ClientStorage package handle selecting storage driver in the next order (descending priority):
1. localStoragecookies
2. js
3. (JS Object driven storage)
To alter priority pass "preferred driver" to new ClientStorage(driverName) constructor.
Pass cookies as an argument to new instance of ClientStorage:
`js`
const { clientStorage } = require('ClientStorage');
var cookiesStorage = new ClientStorage('cookies');
cookiesStorage.has('locale'); // false
cookiesStorage.set('locale', 'en_US'); // true
Pass localStorage as an argument to new instance of ClientStorage:
`js`
const { clientStorage } = require('ClientStorage');
var locStorage = new ClientStorage('localStorage');
locStorage.has('locale'); // false
locStorage.set('locale', 'en_US'); // true
Pass js (in-memory js object) as an argument to new instance of ClientStorage:
`js`
const { clientStorage } = require('ClientStorage');
var jsStorage = new ClientStorage('js');
jsStorage.has('locale'); // false
jsStorage.set('locale', 'en_US'); // true
__Note:__ All instances are sharing same cookie and localStorage records!
Persistent ReactiveVar implementation:
`js
import { ReactiveVar } from 'meteor/reactive-var';
import { ClientStorage } from 'meteor/ostrio:cstorage';
const clientStorage = new ClientStorage();
const persistentReactive = (name, initial = undefined) => {
let reactive;
if (clientStorage.has(name)) {
reactive = new ReactiveVar(clientStorage.get(name));
} else {
clientStorage.set(name, initial);
reactive = new ReactiveVar(initial);
}
reactive.set = function (newValue) {
let oldValue = reactive.curValue;
if ((reactive.equalsFunc || ReactiveVar._isEqual)(oldValue, newValue)) {
return;
}
reactive.curValue = newValue;
clientStorage.set(name, newValue);
reactive.dep.changed();
};
return reactive;
};
const layout = persistentReactive('ui-layout', 'two-columns');
layout.get(); // two-columns
layout.set('single-column');
`
`js
const clientStorage = new (require('ClientStorage').ClientStorage);
clientStorage.set('locale', 'en'); // true
clientStorage.set('country', 'usa'); // true
clientStorage.set('gender', 'male'); // true
clientStorage.get('gender'); // male
clientStorage.has('locale'); // true
clientStorage.has('city'); // false
clientStorage.keys(); // ['locale', 'country', 'gender']
clientStorage.remove('locale'); // true
clientStorage.get('locale'); // undefined
clientStorage.keys(); // ['country', 'gender']
clientStorage.empty(); // true
clientStorage.keys(); // []
clientStorage.empty(); // false
`
1. Clone this package
2. In Terminal (Console) go to directory where package is cloned
3. Then run:
`shellDefault
meteor test-packages ./
- Sponsor via GitHub
- Support via PayPal
- Use ostr.io — Monitoring, Analytics, WebSec, Web-CRON and Pre-rendering for a website