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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@flipflag/persist

v1.1.0

Published

Persistence plugin for FlipFlag SDK - offline resilience with pluggable storage adapters

Downloads

117

Readme

@flipflag/persist

Persistence plugin for FlipFlag SDK - offline resilience with pluggable storage adapters.

Features

  • Offline resilience - Flag values persist and restore when SDK is unavailable
  • Multiple storage backends - localStorage, sessionStorage, cookies, or in-memory
  • TTL support - Automatic expiration of cached values
  • Zero config - Sensible defaults, just wrap your FlipFlag instance
  • TypeScript - Full type definitions included
  • Tree-shakeable - Import only what you need

Installation

npm install @flipflag/persist

Peer dependency: Requires @flipflag/sdk >= 1.2.0

Quick Start

import { FlipFlag } from "@flipflag/sdk";
import { withLocalStorage } from "@flipflag/persist";

const flipFlag = new FlipFlag({ apiKey: "your-api-key" });
const persistedFlipFlag = withLocalStorage(flipFlag);

// Flag values are now automatically persisted
const isEnabled = persistedFlipFlag.isEnabled("my-feature");

Storage Adapters

localStorage (recommended for web)

import { withLocalStorage } from "@flipflag/persist";

const persistedFlipFlag = withLocalStorage(flipFlag, {
  prefix: "ff:", // Storage key prefix (default: "flipflag:")
  ttlMs: 3600000, // TTL in ms (default: 24 hours)
});

sessionStorage

import { withSessionStorage } from "@flipflag/persist";

const persistedFlipFlag = withSessionStorage(flipFlag, {
  prefix: "ff:",
  ttlMs: 3600000,
});

Cookies

import { withCookies } from "@flipflag/persist";

const persistedFlipFlag = withCookies(flipFlag, {
  prefix: "ff:",
  ttlMs: 3600000,
  // Cookie-specific options:
  path: "/",
  domain: "example.com",
  secure: true,
  sameSite: "lax", // "strict" | "lax" | "none"
});

Custom Adapter

import { withPersistence, StorageAdapter } from "@flipflag/persist";

const customAdapter: StorageAdapter = {
  get(key) {
    // Return PersistedFlagEntry or undefined
  },
  set(key, entry) {
    // Store the entry
  },
  remove(key) {
    // Remove the entry
  },
  isAvailable() {
    return true;
  },
};

const persistedFlipFlag = withPersistence(flipFlag, {
  adapter: customAdapter,
  prefix: "ff:",
  ttlMs: 3600000,
});

API Reference

withPersistence(flipFlag, options)

Wrap a FlipFlag instance with persistence capabilities.

Options:

| Option | Type | Default | Description | |--------|------|---------|-------------| | adapter | StorageAdapter | required | Storage adapter to use | | prefix | string | "flipflag:" | Key prefix for storage | | ttlMs | number | 86400000 | Time-to-live in milliseconds (24h) | | onRestore | (flagName, value) => void | - | Called when a flag is restored from cache | | onPersist | (flagName, value) => void | - | Called when a flag is persisted | | onError | (error) => void | - | Called on persistence errors |

withLocalStorage(flipFlag, options?)

Convenience wrapper using localStorage.

withSessionStorage(flipFlag, options?)

Convenience wrapper using sessionStorage.

withCookies(flipFlag, options?)

Convenience wrapper using cookies.

Additional cookie options:

| Option | Type | Default | Description | |--------|------|---------|-------------| | path | string | "/" | Cookie path | | domain | string | - | Cookie domain | | secure | boolean | true in production | Secure flag | | sameSite | "strict" \| "lax" \| "none" | "lax" | SameSite attribute |

Built-in Adapters

import {
  localStorageAdapter,
  sessionStorageAdapter,
  cookieAdapter,
  memoryAdapter,
} from "@flipflag/persist";
  • localStorageAdapter() - Browser localStorage
  • sessionStorageAdapter() - Browser sessionStorage
  • cookieAdapter(options?) - Browser cookies
  • memoryAdapter() - In-memory storage (useful for SSR/testing)

Types

interface PersistedFlagEntry {
  value: boolean;
  persistedAt: number;
  expiresAt?: number;
}

interface StorageAdapter {
  get(key: string): PersistedFlagEntry | undefined | Promise<PersistedFlagEntry | undefined>;
  set(key: string, entry: PersistedFlagEntry): void | Promise<void>;
  remove(key: string): void | Promise<void>;
  isAvailable(): boolean;
}

interface PersistenceOptions {
  adapter: StorageAdapter;
  prefix?: string;
  ttlMs?: number;
  onRestore?: (flagName: string, value: boolean) => void;
  onPersist?: (flagName: string, value: boolean) => void;
  onError?: (error: Error) => void;
}

interface CookieAdapterOptions {
  path?: string;
  domain?: string;
  secure?: boolean;
  sameSite?: "strict" | "lax" | "none";
}

How It Works

The plugin wraps your FlipFlag instance with a Proxy that intercepts isEnabled() calls:

  1. When you call isEnabled(flagName):

    • If SDK succeeds: Value is returned and persisted to storage
    • If SDK fails: Value is restored from cache (if not expired)
  2. Each flag is stored individually with its own TTL

  3. Storage keys follow the pattern: {prefix}{flagName}

License

MIT