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

localstorage-nodejs

v3.1.5

Published

A implementation of the localStorage Web API in Node.js!

Downloads

23

Readme

localstorage-nodejs Documentation

A simple and complete implementation of the Web API, localStorage.

Overview

The localstorage-nodejs module provides a simple interface for local file-based storage in Node.js, mimicking the behavior of the web's localStorage. It supports both synchronous and asynchronous operations (to which the standard web api doesn't) and uses the file system to store key-value pairs.

Installation

You can install this module via npm or yarn:

npm install localstorage-nodejs
# or
yarn add localstorage-nodejs

Usage

To use this module, require it in your Node.js project and create an instance of the storage. You can choose between synchronous and asynchronous modes.

Creating an Instance

const storage = require('localstorage-nodejs');
// or for ESM
import storage from 'localstorage-nodejs'

// Synchronous
const syncStorage = storage('path/to/storage', false);

// Asynchronous
const asyncStorage = storage('path/to/storage', true);

API

Constructor

new Storage(store_prefix, isAsync)
  • store_prefix (string): The directory path where the storage files will be kept.
  • isAsync (boolean): Determines whether to use asynchronous file operations.

Methods

  • clear()

    • Description: Clears all files and directories within the storage directory.
    • Async Mode: Returns a promise.
    • Sync Mode: Executes synchronously.
  • removeItem(key)

    • Description: Removes the file associated with the specified key.
    • Async Mode: Returns a promise.
    • Sync Mode: Executes synchronously.
  • setItem(key, value)

    • Description: Stores the value as a file with the specified key.
    • Parameters:
      • key (string): The key under which the value will be stored.
      • value (string): The value to store.
    • Async Mode: Returns a promise.
    • Sync Mode: Executes synchronously.
  • getItem(key)

    • Description: Retrieves the value associated with the specified key.
    • Parameters:
      • key (string): The key for which the value is to be retrieved.
    • Returns: A promise that resolves to the value or null if not found (in async mode) or the value or null (in sync mode).
  • length

    • Description: Returns the number of files (items) stored.
    • Async Mode: Returns a promise.
    • Sync Mode: Returns the number directly.

Proxy API

The module also exposes a Proxy object which allows for dynamic interaction with storage:

  • Get

    • Retrieves the value associated with a key. If the key does not exist, it will return null.
  • Set

    • Sets the value associated with a key. If the key does not exist, it will create a new file.
  • Delete

    • Removes the value associated with a key. If the key does not exist, it will do nothing.

Example

const storage = require('localstorage-nodejs')('my_storage', true);

(async () => {
  await storage.setItem('testKey', 'testValue');
  const value = await storage.getItem('testKey');
  console.log(value); // Outputs: testValue

  const length = await storage.length;
  console.log(length); // Outputs: 1

  await storage.removeItem('testKey');
})();

Notes

  • Ensure that the directory specified in store_prefix is writable by the Node.js process.
  • The synchronous mode is suitable for simpler use cases where blocking operations are acceptable.