A lightweight, type-safe storage library for browser and Node.js environments with support for namespacing, TTL, and basic obfuscation.
- 🌐 Works in both browser and Node.js/SSR environments
- 🔒 Optional basic obfuscation support (not encryption)
- ⏰ Time-To-Live (TTL) support
- 🏷️ Namespace support to prevent key collisions
- 💾 Automatic fallback to memory storage in non-browser environments
- 🧹 Automatic cleanup of expired items
- 📦 Zero dependencies
- 🔍 Type-safe with TypeScript support
# Using npm
npm install @m4dm4x/pocketstore
# Using yarn
yarn add @m4dm4x/pocketstore
# Using pnpm
pnpm add @m4dm4x/pocketstore
Perfect for storing user preferences, theme settings, or UI state that should persist across page reloads:
const settingsStore = createStore<{theme: string, fontSize: number}>('settings');
settingsStore.set('userPrefs', { theme: 'dark', fontSize: 14 });
Automatically save form data to prevent loss during accidental page refreshes:
const formStore = createStore('forms', { autoCleanup: true });
formStore.set('draft', formData, 3600); // Auto-expires after 1 hour
Cache API responses with automatic expiration:
const cacheStore = createStore('api-cache', { autoCleanup: true });
cacheStore.set('user-data', apiResponse, 300); // Cache for 5 minutes
Handle session data with automatic cleanup:
const sessionStore = createStore('session', {
storage: 'session',
autoCleanup: true
});
sessionStore.set('auth', { token: '...', user: '...' }, 1800); // 30 minutes
Store data that needs to be shared between browser tabs:
const sharedStore = createStore('shared');
sharedStore.set('notification', { message: 'New update!' });
-
Data Sensitivity
- Use
obfuscate: true
only for non-sensitive data - Do NOT store sensitive information (passwords, tokens) without proper encryption
- Consider using Web Crypto API for sensitive data
- Use
-
TTL & Cleanup
- Use TTL for temporary data (e.g., cache, sessions)
- Enable
autoCleanup
to automatically remove expired items - Manually call
clear()
when handling sensitive data
-
Storage Limitations
- Be aware of browser storage limits (usually 5-10MB)
- Handle storage quota exceeded errors
- Use compression for large data sets
-
Storage Type
- Use
localStorage
for persistent data - Use
sessionStorage
for temporary session data - Memory storage is automatically used in SSR
- Use
-
Automatic Cleanup
- Enable
autoCleanup
only when necessary - Large stores might benefit from manual cleanup
- Use appropriate TTL values to prevent storage bloat
- Enable
-
Data Size
- Store minimal necessary data
- Consider compressing large objects
- Use namespaces to organize and manage data
Feature | Chrome | Firefox | Safari | Edge | IE11 |
---|---|---|---|---|---|
Basic Storage | ✅ | ✅ | ✅ | ✅ | ✅ |
TTL Support | ✅ | ✅ | ✅ | ✅ | ✅ |
Obfuscation | ✅ | ✅ | ✅ | ✅ | ✅ |
TypeScript | ✅ | ✅ | ✅ | ✅ | ✅ |
SSR Support | ✅ | ✅ | ✅ | ✅ | ✅ |
Feature | Pocketstore | localforage | store.js | js-cookie |
---|---|---|---|---|
Size (min+gzip) | 2KB | 8KB | 2KB | 1KB |
API Type | Promise-free | Promise-based | Sync | Sync |
Storage Types | localStorage, sessionStorage, memory | IndexedDB, WebSQL, localStorage | localStorage, memory | Cookies |
TypeScript | ✅ Native | |||
SSR Support | ✅ Built-in | ❌ Requires setup | ✅ Built-in | ✅ Built-in |
Namespacing | ✅ Built-in | ❌ Manual | ❌ Manual | ✅ Built-in |
TTL Support | ✅ Built-in | ❌ Manual | ❌ Manual | ✅ Built-in |
Auto Cleanup | ✅ Built-in | ❌ No | ❌ No | ✅ Built-in |
Type Safety | ✅ Full | |||
Browser Support | Modern + IE11 | Modern only | Modern + IE8 | All browsers |
-
Simplicity First
- No dependencies
- Synchronous API (no Promises)
- Intuitive TypeScript support
-
Modern Features
- Built-in TTL support
- Automatic cleanup
- Type-safe by default
-
Universal Usage
- Works in browsers
- Works in Node.js/SSR
- Works in all modern frameworks
-
Developer Experience
- Full TypeScript support
- Comprehensive documentation
- Active maintenance
import { createStore } from '@m4dm4x/pocketstore';
// Create a store with a namespace
const store = createStore('myapp');
// Store a value
store.set('user', { name: 'John', age: 30 });
// Get a value
const user = store.get('user'); // { name: 'John', age: 30 }
// Remove a value
store.remove('user');
// Clear all values in the namespace
store.clear();
const store = createStore('myapp');
// Set and get values
store.set('theme', 'dark');
const theme = store.get('theme'); // 'dark'
// Remove values
store.remove('theme');
const removedTheme = store.get('theme'); // null
const store = createStore('myapp');
// Set a value that expires in 60 seconds
store.set('token', '123456', 60);
// Value is available until TTL expires
const token = store.get('token'); // '123456'
// After 60 seconds:
const expiredToken = store.get('token'); // null
const store = createStore('myapp', {
obfuscate: true,
secret: 'your-secret-key'
});
// Values are automatically obfuscated before storage
store.set('data', { apiKey: '12345' });
// Values are automatically deobfuscated when retrieved
const data = store.get('data'); // { apiKey: '12345' }
⚠️ Security Note: The obfuscation feature is NOT encryption and should NOT be used for sensitive data. It provides basic obfuscation only and is suitable for non-sensitive data that you want to make slightly harder to read in the browser's dev tools.
const store = createStore('myapp', {
autoCleanup: true // Enable automatic cleanup
});
// Set some values with TTL
store.set('temp1', 'value1', 1); // Expires in 1 second
store.set('temp2', 'value2', 60); // Expires in 60 seconds
// After 2 seconds:
store.getAll(); // Automatically cleans up expired items
// Returns: { temp2: 'value2' }
// Use localStorage (default)
const localStore = createStore('myapp');
// Use sessionStorage
const sessionStore = createStore('myapp', {
storage: 'session'
});
// Create stores with different namespaces
const userStore = createStore('users');
const settingsStore = createStore('settings');
// Values don't conflict even with the same key
userStore.set('data', { name: 'John' });
settingsStore.set('data', { theme: 'dark' });
userStore.get('data'); // { name: 'John' }
settingsStore.get('data'); // { theme: 'dark' }
const store = createStore('myapp');
// Check if a key exists
store.has('user'); // true/false
// Get all keys
const keys = store.keys(); // ['user', 'theme', ...]
// Get all values
const all = store.getAll(); // { user: {...}, theme: 'dark', ... }
// Initialize store (cleans up expired items if autoCleanup is enabled)
store.init();
Creates a new store instance.
-
namespace
: String to prefix all keys with -
options
: Configuration object (optional)-
storage
: Storage type ('local' | 'session') -
obfuscate
: Enable basic obfuscation (boolean) -
secret
: Obfuscation secret key (required if obfuscate is true) -
autoCleanup
: Enable automatic cleanup of expired items (boolean)
-
Object with methods:
set(key: string, value: T, ttlSeconds?: number): void
get(key: string): T | null
remove(key: string): void
clear(): void
has(key: string): boolean
keys(): string[]
getAll(): Record<string, T>
init(): void
The library includes TypeScript definitions out of the box:
import { createStore } from '@m4dm4x/pocketstore';
interface UserData {
name: string;
age: number;
}
const store = createStore<UserData>('myapp');
store.set('user', { name: 'John', age: 30 });
const user = store.get('user'); // Type is UserData | null
The library automatically falls back to in-memory storage when running in Node.js or SSR environments, making it safe to use in server-side rendered applications.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
Give a ⭐️ if this project helped you!