Registry / storage / expired-storage

expired-storage

JSON →
library1.0.2jsnpmunverified

Micro JavaScript library (v1.0.2, last updated 2018) that wraps localStorage or sessionStorage to provide expiration times on stored items. Items are stored with an additional key tracking expiration; fetching expired items removes them and returns null. Offers basic set/get with TTL in seconds, plus extended methods like getTimeLeft, isExpired, peek, updateExpiration, clearExpired, and JSON helpers. Accepts custom storage backends that implement localStorage API. No dependencies, minimal footprint. Not actively maintained; use as-is for legacy projects.

npm install expired-storage
INSTALL
IMPORT
SIG · EXPIRED-STORAGE
E
expired-storage
storagejavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ExpiredStorage
import ExpiredStorage from 'expired-storage'
const { ExpiredStorage } = require('expired-storage');
Default export in ESM; CommonJS requires require('expired-storage').default. For script tags, window.ExpiredStorage is available.
ExpiredStorage
const ExpiredStorage = require('expired-storage').default;
const ExpiredStorage = require('expired-storage');
CJS default export is accessed via .default; direct require gives the module object.
ExpiredStorage
var storage = new ExpiredStorage();
var storage = new expiredStorage();
Class name is ExpiredStorage (capital E), not expiredStorage.

Shows basic usage of ExpiredStorage: create instance, set/get items with expiration, peek, JSON methods, and clearing expired items.

// Install: npm install expired-storage import ExpiredStorage from 'expired-storage'; // Create instance using localStorage (default) const storage = new ExpiredStorage(); // Set item with expiration of 60 seconds storage.setItem('myKey', 'myValue', 60); // Get item (removes if expired) const value = storage.getItem('myKey'); console.log(value); // 'myValue' if within 60 sec, else null // Set without expiration storage.setItem('permanent', 'forever'); // Check time left (seconds) const timeLeft = storage.getTimeLeft('myKey'); console.log(timeLeft); // number of seconds remaining, or -1 if expired/not found // Peek without deleting const peeked = storage.peek('myKey'); console.log(peeked); // { value: '...', timeLeft: ..., expired: false } // JSON helpers storage.setJson('user', { name: 'Alice' }, 60); const user = storage.getJson('user'); console.log(user); // { name: 'Alice' } // Clear all expired items const cleared = storage.clearExpired(); console.log(cleared); // list of expired keys removed
Debug
Known issues
gotchaExpiredStorage relies on prefix-based keys to track expiration. If you manually modify localStorage with keys that start with '__expired__', they may interfere with the library's internal storage.
fix
Avoid manually setting any localStorage keys that start with '__expired__'.
affects: >=1.0.0
brokenWhen using custom storage that does not have a `keys()` method or is not iterable, the `clearExpired()` method may fail or produce unexpected results.
fix
Provide a `keys()` function in your custom storage object that returns an array of all keys.
affects: >=1.0.0
gotchaThe `getTimeLeft()` method returns -1 for expired or non-existent keys, which can be ambiguous. It does not throw an error.
fix
Always check for both null and -1 when interpreting time left. Consider using `isExpired()` first.
affects: >=1.0.0
deprecatedThe library has not been updated since 2018. There are known alternatives like localforage or idb-keyval that offer more robust expiration and async operations.
fix
For new projects, consider using localforage with expiration plugin or similar.
affects: 1.0.2
Errors
Common errors & fixes
TypeError: Cannot read property 'setItem' of undefined
ExpiredStorage not properly instantiated or localStorage not available (e.g., in Node.js without a polyfill).
fix
Ensure you create an instance with `new ExpiredStorage()`. In Node.js, provide a custom storage object that implements setItem/getItem/removeItem/clear.
Uncaught ReferenceError: ExpiredStorage is not defined
Library not imported or loaded correctly (e.g., using ESM in a script tag or missing type="module").
fix
In browsers, include the script via <script src="dist/expired-storage.min.js"></script> and use window.ExpiredStorage. In Node, use require('expired-storage').default or import.
TypeError: storage.keys is not a function
Custom storage object does not have a `keys()` method.
fix
Add a `keys()` function to your custom storage that returns an array of keys. For localStorage, this works out of the box.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
24
OpenAI (training)
1
Resources
expired-storage — npm install expired-storage · libregistry