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

posthog-flag-toolkit

v0.1.2

Published

Type-safe feature flag registry, sync, release tracking, and auto-rollback guardian for PostHog

Readme

posthog-flag-toolkit

CI npm License: MIT

Type-safe feature flag registry, one-way sync, release tracking, and auto-rollback guardian for PostHog. Framework-agnostic core with optional adapters for Inngest and Slack.

Install

npm install posthog-flag-toolkit

Quick Start

1. Define your registry

import { createRegistry, type FlagDefinition } from "posthog-flag-toolkit";

const { NAMING_REGEX, getLifecycle, getArea } = createRegistry({
  areas: ["studio", "ai", "billing", "auth"] as const,
});

const registry = {
  RELEASE_STUDIO_NEW_EDITOR: {
    key: "release_studio_new_editor",
    description: "Gates the new Tiptap-based editor in the studio.",
    owner: "@yourname",
    tags: ["studio"],
    guardian: true,
  },
} as const satisfies Record<string, FlagDefinition>;

2. Sync flags to PostHog

import { runFlagSync } from "posthog-flag-toolkit";

const result = await runFlagSync({
  posthog: { apiKey: "phx_...", projectId: "12345" },
  registry,
  namingRegex: NAMING_REGEX,
  callbacks: {
    onCreate: (def) => console.log(`Created ${def.key}`),
    onOrphan: (flag) => console.log(`Orphan: ${flag.key}`),
  },
});

3. Track releases

import { runReleaseTracker } from "posthog-flag-toolkit";

const result = await runReleaseTracker({
  posthog: { apiKey: "phx_...", projectId: "12345" },
  registry,
  namingRegex: NAMING_REGEX,
  callbacks: {
    onNewRelease: (flag, type) => console.log(`Released: ${flag.key} (${type})`),
    onStale: (flag, days) => console.log(`Stale: ${flag.key} (${days} days)`),
    onDigestReady: (digest) => sendEmail(digest),
  },
});

4. Auto-rollback with Guardian

import { runFlagGuardian } from "posthog-flag-toolkit";

const result = await runFlagGuardian({
  posthog: { apiKey: "phx_...", projectId: "12345" },
  thresholds: { errorRateRatioThreshold: 2.0 },
  callbacks: {
    onRegression: (evaluation) => alert(evaluation),
    onEnforced: (flag) => console.log(`Auto-disabled: ${flag.key}`),
  },
});

Inngest Adapter

For durable execution with Inngest, pass step as the StepRunner:

import { runFlagSync } from "posthog-flag-toolkit";

export const syncFn = inngest.createFunction(
  { id: "flag-sync" },
  { cron: "*/10 * * * *" },
  async ({ step }) => {
    return runFlagSync({ posthog: config, registry, namingRegex, step });
  },
);

Or use the withInngestCron helper:

import { withInngestCron } from "posthog-flag-toolkit/adapters/inngest";

export const syncFn = withInngestCron(inngest, {
  id: "flag-sync",
  name: "Sync flags",
  cron: "*/10 * * * *",
  run: (step) => runFlagSync({ posthog: config, registry, namingRegex, step }),
});

Slack Adapter

import { postGuardianAlert } from "posthog-flag-toolkit/adapters/slack";

await postGuardianAlert(webhookUrl, {
  severity: "critical",
  flagKey: "release_studio_new_editor",
  flagName: "New Editor",
  decision: "auto_disabled",
  enforced: true,
  reason: "error rate ratio 3.2x",
  metrics: { treatmentErrorRate: 0.15, controlErrorRate: 0.047 },
  posthogFlagUrl: "https://us.posthog.com/project/12345/feature_flags/42",
});

API

Core Functions

| Function | Description | |---|---| | runFlagSync(options) | One-way sync from registry to PostHog (create, reconcile, detect orphans) | | runReleaseTracker(options) | Detect 100% rollout releases, stale flags, experiments | | runFlagGuardian(options) | Compare treatment/control cohorts, auto-disable on regression | | createRegistry(config) | Factory for typed flag registry with naming regex |

PostHog API Helpers

| Function | Description | |---|---| | fetchAllFlags(config) | Paginated fetch of all flags | | fetchFlagsByTag(config, tag) | Fetch flags by tag (substring filter) | | createFlag(config, body) | Create a new flag | | patchFlag(config, flagId, patch) | Update a flag | | isFullyReleased(flag) | Check if flag is active + 100% rollout | | queryCohortMetrics(params) | HogQL query for treatment/control metrics |

Guardian Decision Logic

| Function | Description | |---|---| | detectRegression(metrics, thresholds) | Pure function: compare metrics against thresholds | | meetsSampleFloor(metrics, thresholds) | Check if both cohorts have enough data | | mergeThresholds(overrides?) | Merge custom thresholds with defaults |

All run* functions accept an optional step: StepRunner parameter. Inngest's step object satisfies the interface natively. Without it, a SimpleStepRunner is used (direct execution, no durability).

Configuration

PostHogClientConfig

{ apiKey: string; projectId: string; baseUrl?: string }

GuardianThresholds

| Option | Default | Description | |---|---|---| | windowMinutes | 20 | HogQL lookback window | | minSampleSize | 100 | Min events per cohort | | minUniqueUsers | 50 | Min distinct users per cohort | | errorRateRatioThreshold | 2.0 | Treatment/control error rate ratio | | publishSuccessDropThreshold | 0.15 | Max acceptable success rate drop (pp) | | cooldownMinutes | 30 | Skip recently-updated flags |

License

MIT