1.0.0 • Published 10 months ago

secure-cache-nest v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

SecureCacheNest

SecureCacheNest is a lightweight and secure JavaScript library for storing data in LocalStorage and IndexedDB. It simplifies the process of storing, encrypting, compressing, and synchronizing data on the client side.

Table of Contents

  1. Purpose
  2. Requirements
  3. Installation Instructions
  4. Usage Examples
  5. Configuration
  6. Conclusion

Purpose

The library provides:

  • A simple API for saving and retrieving data.
  • Support for data encryption for security.
  • Data compression for saving space.
  • Synchronization with Google Drive and support for IndexedDB.

Requirements

To use SecureCacheNest, you need the following libraries:

  • CryptoJS: For data encryption.
  • LZString: For data compression.

Installation Instructions

Via npm

Install the library and its dependencies using npm:

npm install secure-cache-nest crypto-js lz-string

Via CDN

To use via CDN, add these lines to your HTML file:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>
<script src="path/to/secure-cache-nest.js"></script>

Usage Examples

Full Example

Here's a complete example demonstrating all functions of the library:

// Initialize the library
const storage = SecureCacheNest;

// Data to be stored
const userData = { name: 'Alice', age: 28 };

// Saving data with encryption and compression
storage.setItem('user', userData, { 
    expiry: 60000,   // Expires in 60 seconds
    compress: true, 
    encrypt: true, 
    secret: 'mySecret' 
});

// Retrieving data
const retrievedUser = storage.getItem('user', { 
    decompress: true, 
    encrypt: true, 
    secret: 'mySecret' 
});
console.log(retrievedUser); // { name: 'Alice', age: 28 }

// Removing data
storage.removeItem('user');

// Checking if the data is removed
const deletedUser = storage.getItem('user');
console.log(deletedUser); // null

// Clearing all storage
storage.clearStorage();

// Synchronizing with Google Drive
// Replace 'yourAuthToken' and 'yourData' with actual values
// storage.syncWithGoogleDrive('yourAuthToken', yourData);

// Saving to IndexedDB
storage.setItemIndexedDB('userIndexedDB', userData);

// Retrieving from IndexedDB
storage.getItemIndexedDB('userIndexedDB').then(data => {
    console.log(data); // { name: 'Alice', age: 28 }
});

Saving Data

storage.setItem('key', { name: 'John', age: 30 }, { expiry: 60000, compress: true, encrypt: true, secret: 'mySecret' });

Retrieving Data

const user = storage.getItem('key', { decompress: true, encrypt: true, secret: 'mySecret' });
console.log(user); // { name: 'John', age: 30 }

Removing Data

storage.removeItem('key');

Clearing Storage

storage.clearStorage();

Synchronizing with Google Drive

storage.syncWithGoogleDrive('yourAuthToken', yourData);

Saving to IndexedDB

storage.getItemIndexedDB('key').then(data => {
    console.log(data); // { name: 'Jane', age: 25 }
});

Retrieving from IndexedDB

storage.getItemIndexedDB('key').then(data => {
    console.log(data); // { name: 'Jane', age: 25 }
});

Caching Data

storage.setItemWithCache('cacheKey', { name: 'Cached Data' });
const cachedData = storage.getItemWithCache('cacheKey');
console.log(cachedData); // { name: 'Cached Data' }

History Management

storage.setItemWithHistory('historyKey', { action: 'created' });
const history = storage.getHistory('historyKey');
console.log(history); // [{ action: 'created', timestamp: Date }]

Data Backup and Restore

// Backup data
storage.backupData('backup.json');

// Restore data from a file (assuming you have a file input to get the file)
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (event) => {
    const file = event.target.files[0];
    storage.restoreData(file);
});

Syncing Between Tabs

storage.setItemWithSync('tabKey', { data: 'This data syncs between tabs.' });

Configuration

Encryption

  • To encrypt data, use the encrypt parameter and pass a secret key via the secret parameter:
storage.setItem('key', data, { encrypt: true, secret: 'yourSecret' });

Compression

  • To compress data before saving, use the compress parameter:
storage.setItem('key', data, { compress: true });

Setting Expiration

  • You can set an expiration time for the data using the expiry parameter in milliseconds:
storage.setItem('key', data, { expiry: 300000 }); // 5 minutes