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

persistore

v2.0.0

Published

Automatically applying persistent storage with fallback strategies

Downloads

3,998

Readme

GitHub license npm package Travis Coverage Status

persistore

The Persistore is primary designed to use a very easy API (get, set, remove) to store any string with fallback strategies that apply automatically to each client browser. So if localStorage is not accessible it falls back internally to cookies. And if this would be neither possible, it will just use local variables, which is as persistent as possible in this situation without any backend support.

Some aspects

  • TypeScript support included
  • coverage of 100% is mandatory
  • < 0.85 kB (gzipped) (see bundlephobia)
  • any issues will be fixed as soon as possible

Installation

With yarn
npm i --save persistore

Usage

import { Persistore } from 'persistore';

Persistore.set('my-key', '{"here": "comes your data"}');

console.log(Persistore.get('my-key')); // prints: '{"here": "comes your data"}'

Persistore.remove('my-key');

console.log(Persistore.get('my-key')); // prints: undefined

Same is possible for persistence restricted to session lifetime:

import { Persistore } from 'persistore';

Persistore.session.set('my-key', '{"here": "comes your data"}');

console.log(Persistore.session.get('my-key')); // prints: '{"here": "comes your data"}'

Persistore.session.remove('my-key');

console.log(Persistore.session.get('my-key')); // prints: undefined

And additional features to handle cookies is also included:

import { CookieUtil } from 'persistore';

CookieUtil.set('my-key', '{"here": "comes your data"}');

console.log(CookieUtil.get('my-key')); // prints: '{"here": "comes your data"}'

CookieUtil.remove('my-key');

console.log(CookieUtil.get('my-key')); // prints: undefined

// access all cookies
CookieUtil.getAll().forEach(([name, value]) => console.log(name, value)); // prints all cookies

HINT: Since most browsers have a strict cookie size limitation, the Persistore will throw an error if it would try to insert a cookie which exceeds the maximum size of 4093 bytes (iOS Safari reserves 3 additional bytes). The actual browser behaviour in that case would be to silently do nothing, what can lead to strange behaviour of your application. Therefore you should catch the possibly thrown error if you want to store large strings to handle this special case. Another useful information for you might be: localStorage and sessionStorage are not available on some browsers. The most common cases are iOS-Safari-Private-Mode users, where the Persistore will always fallback to cookies.

Configuration

It is also possible to make some configuration. For now it is only the key prefixing and an opt-out for secure cookies (useful for localhost development).

import { Persistore } from 'persistore';

Persistore.config({ prefix: 'myApp.', insecure: IS_LOCALHOST });