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-timed-storage

v1.4.3

Published

Secure timed storage library for encrypting and managing data in localStorage with expiry

Downloads

57

Readme

Secure Timed Storage

Secure Timed Storage is a TypeScript library for securely encrypting and managing data in the browser's localStorage with expiration functionality. It provides an easy-to-use API to store sensitive or temporary data securely.

Description

Local storage in browsers is a useful feature for storing data on the client side, but it has two major concerns:

  1. Data Security: By default, data stored in local storage is not encrypted, making it accessible to anyone who has access to the browser’s developer tools. This lack of security can be problematic when storing sensitive information such as authentication tokens, user details, or other confidential data.
  2. Expiry Management: Local storage does not natively support setting expiry times for stored items. Data stored remains until explicitly removed, which can lead to outdated or stale data persisting indefinitely.

Secure Timed Storage addresses these issues by:

  • Encrypting Data: Ensuring that all data stored in local storage is securely encrypted using AES encryption from the crypto-js library. This protects sensitive information from being easily accessed or tampered with.
  • Setting Expiry Times: Allowing developers to specify an expiration time for each stored item. The library automatically handles the removal of expired data, ensuring that local storage only contains relevant and up-to-date information.

Features

  • Optional Encryption: Encrypts data before storing it in localStorage using AES encryption from crypto-js if an encryption key is provided.
  • Decryption: Decrypts data when retrieving it from localStorage.
  • Expiry: Supports setting an expiry time for stored data, automatically removing it after expiration.
  • TypeScript Support: Fully written in TypeScript for type safety and enhanced developer experience.
  • Modern JavaScript: Utilizes ES Modules for modern JavaScript environments.
  • Developer Friendly: Built with ease of integration and simplicity in mind.

Installation

To install the library, you can use npm or yarn:

npm install secure-timed-storage
# or
yarn add secure-timed-storage

Usage

Importing the Library

import { createSecureTimedStorage } from 'secure-timed-storage';

Initializing Secure Timed Storage

const secretKey = 'your_secret_key_here';
const storage = createSecureTimedStorage({ encryptionKey: secretKey });

Storing Data

# Store data with an optional expiry time in hours
storage.setItem('myKey', { name: 'John Doe' }, 1); # Expires in 1 hour

Retrieving Data

const data = storage.getItem<{ name: string }>('myKey');
console.log(data); # { name: 'John Doe' }

Removing Data

storage.removeItem('myKey');

Additional Methods

  • getRemainingStorage(): Retrieves information about remaining localStorage capacity.
  • clearStorage(): Clears all data from localStorage.
  • cleanUp(): Removes expired data from localStorage.
  • query(predicate: (item: T) => boolean): Queries and retrieves data based on a predicate function.

Example

Here's a simple example demonstrating basic usage:

import { createSecureTimedStorage } from 'secure-timed-storage';

const secretKey = 'your_secret_key_here';
const storage = createSecureTimedStorage({ encryptionKey: secretKey });

# Store data with an expiry of 1 hour
storage.setItem('userToken', { token: 'abc123' }, 1);

# Retrieve the stored data
const token = storage.getItem<{ token: string }>('userToken');
console.log(token); # { token: 'abc123' }

# Clean up expired data (optional)
storage.cleanUp();

Error Handling

The library includes error handling for encryption and decryption failures. For example, if the encryption key is invalid or the data in localStorage is tampered with, appropriate errors are thrown and logged.

try {
    storage.setItem('invalidKey', { foo: 'bar' }, 1);
} catch (error) {
    console.error('Failed to set item:', error.message);
}

const data = storage.getItem<{ foo: string }>('invalidKey');
if (data === null) {
    console.warn('Failed to retrieve item or item has expired.');
}

Contributing

Contributions are welcome! Please follow these guidelines before contributing:

  1. Fork the repository and clone it locally.
  2. Install dependencies with npm install or yarn install.
  3. Create a new branch for your feature or bug fix: git checkout -b feature/my-feature or git checkout -b bugfix/issue-fix.
  4. Make your changes and ensure they follow the code style (linting) and tests pass.
  5. Commit your changes following Conventional Commits.
  6. Push to your forked repository and submit a pull request against the main branch of the original repository.

Development Scripts

  • Build: npm run build - Compiles TypeScript files and builds the project.
  • Lint: npm run lint - Lints TypeScript files using ESLint.
  • Format: npm run format - Formats TypeScript files using Prettier.
  • Test: npm run test - Runs the test cases using Vitest.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Inspired by the need for secure data storage in browser environments.
  • Uses crypto-js library for encryption.