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

billing-invariant

v0.1.0

Published

Asserts Stripe billing math (credit conservation, proration totals, invoice totals) is actually correct across a sequence of webhook events.

Readme

billing-invariant

CI npm version License: MIT

Checks that a sequence of Stripe webhook events actually adds up — before a silent billing-math bug reaches a customer's invoice.

Stripe's test clocks let you simulate subscription and proration scenarios, but nothing asserts the resulting math is correct. A real incident motivated this: a Stripe proration bug silently destroyed gift-subscription credit across 14 redemptions, and nothing caught it until a customer noticed. This package is the check that would have.

Quick start

npm install --save-dev billing-invariant
import { checkInvariants } from "billing-invariant";

const violations = checkInvariants(events); // events: your Stripe webhook log

console.log(violations);
[
  {
    rule: 'credit-conservation',
    message: "Customer cus_gift credit balance decreased from 14000 to 13000 (decrease of 1000) with no matching charge/debit event.",
    eventIds: [ 'evt_gift_bal_1' ],
    expected: 14000,
    actual: 13000
  }
]

Drop it straight into a test assertion:

expect(checkInvariants(events)).toHaveLength(0);

What it checks (v0.1)

  • Credit conservation — a customer's credit balance never decreases without a matching charge/debit event accounting for it. Directly matches the cited real incident.
  • Proration conservation — an invoice's proration-marked line items, grouped by their exact period.start/period.end window (real Stripe proration pairs share the same window, not adjacent ones), include both a credit (old-plan) and a charge (new-plan) side; a one-sided group is flagged.
  • Invoice total match — an invoice's stated total equals the sum of its own line items.

No live Stripe API calls, ever — this library only reads event objects you already have from a webhook log or a test-clock test run.

Module format

Ships as dual ESM + CJS with full TypeScript types for both:

import { checkInvariants } from "billing-invariant"; // ESM
const { checkInvariants } = require("billing-invariant"); // CJS

v0.1 scope

  • Stripe only. Three deterministic checks (above).
  • Not yet covered: orphaned-negative-balance-line-item detection (v0.2), any provider other than Stripe, live Stripe API calls.

See DETAILS.md for the full design and docs/USAGE.md for the complete API reference.

Examples

See examples/ for a sample event log and a CI test-assertion usage pattern.

License

MIT