What is dom-storage?
The dom-storage npm package is a simple implementation of the W3C DOM Storage API, which provides a way to store key-value pairs in a web browser. It is useful for server-side JavaScript environments like Node.js where the browser's localStorage and sessionStorage are not available.
What are dom-storage's main functionalities?
localStorage
This feature allows you to use localStorage in a Node.js environment. You can set, get, remove, and clear items just like you would in a browser.
const { LocalStorage } = require('dom-storage');
const localStorage = new LocalStorage('./localStorage');
// Set an item
localStorage.setItem('key', 'value');
// Get an item
const value = localStorage.getItem('key');
console.log(value); // Outputs: 'value'
// Remove an item
localStorage.removeItem('key');
// Clear all items
localStorage.clear();
sessionStorage
This feature allows you to use sessionStorage in a Node.js environment. You can set, get, remove, and clear items just like you would in a browser.
const { SessionStorage } = require('dom-storage');
const sessionStorage = new SessionStorage();
// Set an item
sessionStorage.setItem('key', 'value');
// Get an item
const value = sessionStorage.getItem('key');
console.log(value); // Outputs: 'value'
// Remove an item
sessionStorage.removeItem('key');
// Clear all items
sessionStorage.clear();
Other packages similar to dom-storage
node-localstorage
node-localstorage is another package that provides a localStorage API for Node.js. It is similar to dom-storage but focuses solely on localStorage functionality. It also supports persistence by writing data to disk.
localstorage-memory
localstorage-memory is an in-memory implementation of the localStorage API for Node.js. Unlike dom-storage, it does not persist data to disk, making it suitable for testing and temporary storage.
node-persist
node-persist is a more general-purpose storage library for Node.js that supports both key-value storage and JSON object storage. It offers more flexibility compared to dom-storage but may be overkill if you only need localStorage or sessionStorage functionality.
sessionStorage & localStorage for NodeJS
An inefficient, but as W3C-compliant as possible using only pure JavaScript, DOMStorage
implementation.
Purpose
This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.
Usage
var Storage = require('dom-storage')
// in-file, doesn't call `String(val)` on values (default)
, localStorage = new Storage('./db.json', { strict: false })
// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
, sessionStorage = new Storage(null, { strict: true })
, myValue = { foo: 'bar', baz: 'quux' }
;
localStorage.setItem('myKey', myValue);
myValue = localStorage.getItem('myKey');
// use JSON to stringify / parse when using strict w3c compliance
sessionStorage.setItem('myKey', JSON.stringify(myValue));
myValue = JSON.parse(localStorage.getItem('myKey'));
API
- getItem(key)
- setItem(key, value)
- removeItem(key)
- clear()
- key(n)
- length
Tests
0 === localStorage.length;
null === localStorage.getItem('doesn't exist');
undefined === localStorage['doesn't exist'];
localStorage.setItem('myItem');
"undefined" === localStorage.getItem('myItem');
1 === localStorage.length;
localStorage.setItem('myItem', 0);
"0" === localStorage.getItem('myItem');
localStorage.removeItem('myItem', 0);
0 === localStorage.length;
localStorage.clear();
0 === localStorage.length;
Notes
- db is read in synchronously
- No callback when db is saved
- Doesn't not emit
Storage
events (not sure how to do)