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

forbear

v0.1.4

Published

Read the server's rate-limit instructions — turn a 429/503 Response into the number of milliseconds to wait, with correct fallback backoff.

Readme

forbear

npm MIT License

Read HTTP server rate-limit instructions from 429/503 responses and return milliseconds to wait, with correct fallback backoff.

The problem

Different APIs use incompatible rate-limit header formats. Some send Retry-After: 30 (seconds), others send X-RateLimit-Reset: 1700000030 (epoch seconds), others use HTTP dates. Naive parsers treat epoch-seconds as delta-seconds, causing absurd waits (e.g., 53 years instead of 30 seconds).

Forbear parses all major dialects correctly, handles the epoch-seconds vs milliseconds ambiguity, and provides exponential backoff when headers are missing.

Install

npm install forbear
# or
pnpm add forbear
# or
yarn add forbear

Use

import { forbear } from 'forbear';

const response = await fetch(url);
const waitMs = forbear(response);
await sleep(waitMs); // wait 30000ms if server sent Retry-After: 30

With retry logic:

import pRetry from 'p-retry';
import { forbear } from 'forbear';

const result = await pRetry(async () => {
  const response = await fetch(apiUrl);
  if (response.status === 429) {
    const waitMs = forbear(response, { attempt: pRetry.attemptNumber() });
    throw new pRetry.AbortError(`Rate limited. Wait ${waitMs}ms before retry.`);
  }
  return response.json();
}, { minTimeout: 1000 });

API

forbear(input, options?)

Returns milliseconds to wait, always in [0, cap].

function forbear(
  input: Response | Headers | Record<string, string>,
  options?: ForbearOptions
): number

Parameters: input (Response/Headers/object), options.attempt (default: 0), options.now (default: Date.now()), options.base (default: 500), options.cap (default: 60000), options.jitter (default: true), options.random (default: Math.random).

Behavior: Parses headers by priority: Retry-After → RateLimit → X-RateLimit-Reset → exponential backoff. Never returns NaN or negative values.

inspect(input, options?)

Returns detailed rate-limit information.

function inspect(
  input: Response | Headers | Record<string, string>,
  options?: ForbearOptions
): RateLimitInfo

Returns: waitMs (milliseconds), source (which rule: "retry-after" | "ratelimit" | "x-ratelimit" | "backoff"), header (raw value, undefined for backoff).

ForbearOptions, RateLimitInfo

Types for options and inspect return value.

Default export

import forbear from 'forbear'; // same as forbear()

Non-goals

Forbear only calculates wait times. It will never: perform retries, sleep, make requests, store state, or track budgets.

Example for retries:

import pRetry from 'p-retry';
import { forbear } from 'forbear';

const response = await pRetry(async () => {
  const res = await fetch(url);
  if (res.status === 429) {
    const wait = forbear(res, { attempt: pRetry.attemptNumber() });
    await new Promise(resolve => setTimeout(resolve, wait));
    throw new Error('Retry');
  }
  return res;
}, { retries: 3 });

Related Packages

Caching & Concurrency:

Text Processing:

  • @azghr/shorn — Truncate strings by byte budget without breaking graphemes
  • seriatim — Sequential processing utilities

HTTP & Network:

  • forestall — Delay execution until a condition is met
  • obviate — Render operations unnecessary through caching

System & Process:

  • quiesce — Ordered, timeboxed graceful shutdown for Node
  • sortition — Deterministic percentage rollouts and A/B bucketing
  • stanch — Stop flows or operations based on conditions

Utilities:

  • expunge — Remove or exclude items from collections
  • occlude — Hide or mask data and functionality
  • placemark — Geographic location and mapping utilities
  • specie — Currency and financial calculations

License

MIT