A secure and efficient storage solution with support for compression and encryption.
npm install glancy
Glancy is a powerful, flexible, and efficient client-side storage library for modern JavaScript applications. It provides a range of features including key-value storage, item expiration (TTL), encryption, compression, and the ability to handle multiple items at once. This project is designed to be simple to use while also providing advanced features for complex storage needs.
- ๐๏ธ Namespace Support: All items are stored under a unique namespace.
- โฑ๏ธ Time-to-Live (TTL): You can set an expiration time for stored items.
- ๐๏ธ Compression: Built-in gzip compression for optimizing storage space.
- ๐ Encryption: AES-256-GCM encryption with PBKDF2 key derivation for secure storage of sensitive data.
- โ ๏ธ Error Handling: Graceful error handling with custom error classes.
- ๐ฆ Batch Operations: Methods to get/set multiple items in a single operation.
- ๐ก๏ธ Storage Integrity: Validations and checks to ensure safe storage usage.
Install the package via npm:
``bash`
npm install glancy
Then, import the Glancy class:
`typescript
import { Glancy } from 'glancy';
const storage = new Glancy({ namespace: 'my_app' });
`
Easily set, get, and remove items:
`typescript
const storage = new Glancy({ namespace: 'my_app' });
interface MyData {
value: string;
}
// ๐ Set a value
await storage.set
// ๐ Get a value
const { data } = await storage.get
console.log(data?.value); // Outputs: Hello, world!
// ๐๏ธ Remove a value
storage.remove('my_key');
// ๐งน Clear all items in the namespace
storage.clear();
`
Optimize storage and secure your data with built-in compression and encryption:
`typescript
const storage = new Glancy({
namespace: 'my_app',
encryption: {
enabled: true, // ๐ Enable encryption
key: 'my-secret-key',
},
compress: true, // ๐๏ธ Enable compression
compressionLevel: 6, // Optional: Set compression level (0-9)
});
interface SecureData {
value: string;
}
// ๐ Set a value securely
await storage.set
'secure_key',
{ value: 'Sensitive Information' },
60000
);
// ๐ Retrieve the secure value
const { data } = await storage.get
console.log(data?.value); // Outputs: Sensitive Information
`
> ๐ง Tip: You can generate a secure key using openssl by running openssl rand -base64 32
Perform operations on multiple keys at once:
`typescript
interface MyData {
value: string;
}
// ๐ Set multiple items
await storage.setMany
key1: { value: 'value1' },
key2: { value: 'value2' },
});
// ๐ Get multiple items
const { data } = await storage.getMany
console.log(data['key1']); // Outputs: value1
`
Store items with a lifespan:
`typescript
await storage.set('temporary_data', { value: 'Expires Soon' }, 5000); // Expires in 5 seconds
setTimeout(async () => {
const data = await storage.get('temporary_data');
console.log(data); // Should be null after 5 seconds
}, 6000);
`
| Option | Description | Default |
| ------------------ | --------------------------------------------------- | ----------------------------- |
| namespace | A unique identifier for the storage namespace. | glancy |encryption
| | An object containing encryption configuration. | { enabled: false, key: '' } |defaultTTL
| | The default TTL (in milliseconds) for stored items. | null |compress
| | Whether to enable compression for stored items. | false |compressionLevel
| | The gzip compression level (0-9). | 6 |
#### get
Retrieves a value. Returns null if the key doesn't exist or has expired.
#### set(key: string, value: T, ttl?: number): Promise
Stores a value with an optional TTL.
#### getMany
Retrieves multiple values at once.
#### setMany
Stores multiple values at once.
#### remove(key: string): GlancyResponse
Deletes an item from storage.
#### clear(): GlancyResponse
Removes all items in the current namespace.
#### keys(): GlancyResponse
Lists all keys in the namespace.
#### has(key: string): Promise
Checks if a key exists and is not expired.
#### touch(key: string, ttl?: number): Promise
Updates the TTL for an existing item.
#### getTTL(key: string): Promise
Gets the remaining TTL for a key.
#### size(): GlancyResponse
Calculates the total size (in bytes) of all items in the namespace.
#### GlancyResponse
Represents the response from a storage operation.
| Property | Description |
| --------- | ----------------------------------------------- |
| success | Indicates whether the operation was successful. |message
| | A message associated with the response. |data
| | The data returned by the operation. |
`typescript`
interface GlancyResponse
success: boolean;
message: string;
data: T | null | undefined;
}
---
For more details, visit the Glancy Storage Library Documentation.
We โค๏ธ contributions! Here's how to get started:
1. ๐ด Fork the repository
2. ๐ฑ Create a new branch (git checkout -b feat/my-feature)git commit -am 'feat: add new feature'
3. ๐ป Commit your changes ()git push origin feat/my-feature`)
4. ๐ Push to your branch (
5. ๐ ๏ธ Create a Pull Request
This project is licensed under the Apache 2.0 License.
> ๐ง Pro Tip: If you encounter any issues or have feature requests, feel free to open an issue or submit a pull request. Let's make Glancy even better!