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

@pegma/billing-core

v0.1.1

Published

Provider-agnostic subscription ledger: event watermark, lifecycle-rank arbitration, declared invariants, checkout reservation, and snapshot reconciliation.

Readme

@pegma/billing-core

Provider-agnostic subscription ledger for Pegma hosts: a per-account watermark, an effective-watermark guard, lifecycle-rank tie-breaking at the equal second, declared write-path invariants, a single-opportunity checkout reservation, and snapshot reconciliation.

[!IMPORTANT] Pegma is in early 0.x development. This package's public API is unstable.

import { createBillingLedger, firstWins, sticky } from "@pegma/billing-core";
import { createMemoryStore } from "@pegma/storage-core";
import { fixedClock } from "@pegma/spine";

const ledger = createBillingLedger({
  store: createMemoryStore(),
  clock: fixedClock("2026-08-15T16:00:00.000Z"),
  fields: {
    foundingMember: sticky(),
    consentAt: firstWins("consent"),
    consentVersion: firstWins("consent"),
  },
});

await ledger.apply("acct_123", {
  eventId: "evt_founding",
  eventAt: "2026-08-15T16:00:00.000Z",
  status: "active",
  providerCustomerId: "cus_123",
  providerSubscriptionId: "sub_123",
  providerPriceId: "price_founding",
  plan: "founding",
  periodStartAt: "2026-08-01T00:00:00.000Z",
  periodEndAt: "2026-09-01T00:00:00.000Z",
  trialStartAt: null,
  trialEndAt: null,
  cancelAtPeriodEnd: false,
  fields: {
    foundingMember: true,
    consentAt: "2026-08-15T16:00:00.000Z",
    consentVersion: "v1",
  },
});

const reserved = await ledger.reserve("acct_456");
if (reserved.reserved) {
  await ledger.release("acct_456", reserved.reservationId);
}

await ledger.reconcile("acct_123", async (observed) => ({
  status: observed.status,
  providerCustomerId: observed.providerCustomerId,
  providerSubscriptionId: observed.providerSubscriptionId,
  providerPriceId: observed.providerPriceId,
  plan: observed.plan,
  periodStartAt: observed.periodStartAt,
  periodEndAt: observed.periodEndAt,
  trialStartAt: observed.trialStartAt,
  trialEndAt: observed.trialEndAt,
  cancelAtPeriodEnd: observed.cancelAtPeriodEnd,
}));

Hosts inject a @pegma/storage-core Store and, optionally, a Spine Clock and Logger. Reservation TTL and snapshot freshness (snapshotAt) are taken from the injected clock — never from Date.now(). This package never creates a network client and never stores card data, raw payloads, line items, or amounts.

Arrival order proves nothing. Every apply goes through the effective-watermark guard and lifecycle rank. Exact redelivery is an idempotent no-op.

sticky and firstWins are declared on the host's ledger definition and enforced inside the update decider, so they re-evaluate against fresh state on every conflict. A reservation id is minted inside the decider and read back from storage; the caller's id is whatever the stored record says.

Snapshot reconciliation observes the domain CAS token (eventAt, eventId) and the current snapshotAt / snapshotGeneration, samples the freshness bound from the injected clock, then fetches provider truth. The decider re-checks the watermark and the observed generation against fresh state. An intervening event or a newer reconciliation drops the snapshot. A reservation-only row is skipped so a founding webhook is not gated by an invented bound. A token match always writes — even when no field changed — and increments snapshotGeneration so overlapping same-bound sweeps cannot clobber fresher fields. snapshotAt never jumps into a later Unix second than the sampled bound, so same-second webhooks still get lifecycle-rank arbitration. The watermark identity is never touched.

Phase 4 keeps this package provider-agnostic. Import Stripe translation from @pegma/billing-stripe, not from here.