npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

secure-cache-nest

v1.0.0

Published

A lightweight and secure JavaScript library for storing data in LocalStorage and IndexedDB.

Downloads

6

Readme

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