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

@lp-labs/token-bucket

v0.1.0

Published

Fractional local token accounting with cancellable burst reservations.

Readme

@lp-labs/token-bucket

Fractional local token accounting with cancellable burst reservations.

Install

npm install @lp-labs/token-bucket

Zero runtime, peer, or sibling dependencies. Node 18+; strict TypeScript declarations and independent ESM/CommonJS builds. Version 0.1.0, MIT.

Quickstart

import { TokenBucket } from '@lp-labs/token-bucket';
let now = 0;
const bucket = new TokenBucket({ capacity: 4, refillPerSecond: 2, clock: () => now });
bucket.tryTake(4);
now = 250;
console.log(bucket.available); // 0.5
const reservation = bucket.reserve(0.5);
reservation?.cancel();

API and contracts

Construct TokenBucket with positive finite capacity, nonnegative finite refillPerSecond, and clock. initialTokens defaults to capacity and must be in [0, capacity]. available refreshes accounting before returning spendable tokens. tryTake(amount = 1) returns false without spending when insufficient. reserve(amount = 1) returns a Reservation or undefined when insufficient; delayFor(amount = 1) predicts a wait. Amounts must be positive finite values no greater than capacity.

Clock values are monotonic milliseconds, supplied by the caller (for example performance.now). Backward/nonfinite clocks and invalid numeric options throw RangeError. Refill is fractional and capped; held reservations occupy capacity until committed or cancelled. Cancellation refunds exactly once; commit spends tokens and opens refill capacity. delayFor returns milliseconds or Infinity, not a scheduling guarantee. No timers, waiting queue, global server rate guarantee, distributed coordination, or adaptive/circuit-breaker policy. O(1) operations, O(active reservations) caller storage. Do not retain unused reservations indefinitely.

Arithmetic uses JavaScript floating-point precision, not fixed-point credit accounting.

Full declarations

The complete public declarations below are generated from the package's strict source. All runtime exports, interfaces, options, return types and readonly state are listed here.

export interface TokenBucketOptions {
  capacity: number;
  refillPerSecond: number;
  clock: () => number;
  initialTokens?: number;
}
export interface Reservation {
  readonly amount: number;
  readonly state: 'reserved' | 'committed' | 'cancelled';
  commit(): boolean;
  cancel(): boolean;
}
export declare class TokenBucket {
  readonly capacity: number;
  readonly refillPerSecond: number;
  private tokens;
  private last;
  private held;
  private readonly clock;
  constructor(options: TokenBucketOptions);
  private refresh;
  private check;
  get available(): number;
  tryTake(amount?: number): boolean;
  delayFor(amount?: number): number;
  reserve(amount?: number): Reservation | undefined;
}

Development

npm ci --offline
npm run build
npm run typecheck
npm test
npm run format:check

All tests use deterministic clocks, immediate injected sleeps, or explicit promise gates rather than timing guesses. Package exports select ESM for import and CommonJS for require, with separate declaration trees. No runtime configuration files or global hooks.

Support

If this library is useful to you, consider supporting its maintenance with a donation via PayPal.

Author

Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.