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 🙏

© 2025 – Pkg Stats / Ryan Hefner

breaker-box

v5.0.0

Published

A zero-dependency circuit breaker implementation for Node.js

Readme

Breaker Box

A zero-dependency circuit breaker implementation for Node.js.

Installation

npm install breaker-box

Usage

Basic Usage

import { createCircuitBreaker } from "breaker-box"

// Wrap an unreliable async function
async function unreliableApiCall(data: string) {
	const response = await fetch(`https://api.example.com/data/${data}`)
	if (!response.ok) throw new Error("API call failed")
	return response.json()
}

const protectedApiCall = createCircuitBreaker(unreliableApiCall, {
	errorThreshold: 0.5, // Open circuit when 50% of calls fail
	errorWindow: 10_000, // Track errors over 10 second window
	minimumCandidates: 6, // Need at least 6 calls before calculating error rate
	resetAfter: 30_000, // Try again after 30 seconds
})

try {
	const result = await protectedApiCall("user-123")
	console.log("Success:", result)
} catch (error) {
	console.error("Circuit breaker error:", error.message)
}

Retry Strategies

The circuit breaker doesn't retry - it only tracks failures and prevents calls when too many fail. Use withRetry to add retry logic:

import {
	createCircuitBreaker,
	withRetry,
	useExponentialBackoff,
	useFibonacciBackoff,
} from "breaker-box"

// Exponential backoff: 1s, 2s, 4s, 8s, up to 30s max
const protectedWithExponential = createCircuitBreaker(
	withRetry(unreliableApiCall, { retryDelay: useExponentialBackoff(30) }),
)

// Fibonacci backoff: 1s, 2s, 3s, 5s, 8s, up to 60s max
const protectedWithFibonacci = createCircuitBreaker(
	withRetry(unreliableApiCall, { retryDelay: useFibonacciBackoff(60) }),
)

Timeout Protection

Compose withTimeout, withRetry, and circuit breaker for complete fault tolerance:

import {
	createCircuitBreaker,
	withRetry,
	withTimeout,
	useExponentialBackoff,
} from "breaker-box"

// 1. Add timeout (innermost - fails fast)
const timedCall = withTimeout(unreliableApiCall, 5_000, "Request timed out")

// 2. Add retry logic (middle - retries on transient errors)
const retryCall = withRetry(timedCall, {
	maxAttempts: 3,
	retryDelay: useExponentialBackoff(10),
	shouldRetry: (error) => !error.message.includes("404"), // Don't retry 404s
})

// 3. Add circuit breaker (outermost - prevents cascading failures)
const protectedApiCall = createCircuitBreaker(retryCall, {
	errorThreshold: 0.5,
	minimumCandidates: 5,
})

Event Monitoring

const protectedFunction = createCircuitBreaker(unreliableApiCall, {
	onClose: () => {
		console.log("🟢 Circuit closed - normal operation resumed")
	},
	onHalfOpen: () => {
		console.log("🟡 Circuit half-opened - waiting for success")
	},
	onOpen: (cause) => {
		console.log("🔴 Circuit opened due to:", cause.message)
	},
})

// Check current state
console.log("Current state:", protectedFunction.getState())
// Possible states: 'closed', 'open', 'halfOpen'

Cleanup

// Clean up resources when shutting down
protectedFunction.dispose()

API

createCircuitBreaker(fn, options?)

Creates a circuit breaker around the provided async function.

Parameters

  • fn: The async function to protect
  • options: Configuration object (optional)
    • errorIsFailure: Function to determine if an error should not count toward circuit breaker metrics (default: () => false)
    • errorThreshold: Percentage (0-1) of errors that triggers circuit opening (default: 0)
    • errorWindow: Time window in ms for tracking errors (default: 10_000)
    • fallback: Function to call when circuit is open (default: undefined)
    • minimumCandidates: Minimum calls before calculating error rate (default: 6)
    • onClose: Function called when circuit closes (default: undefined)
    • onHalfOpen: Function called when circuit enters half-open state (default: undefined)
    • onOpen: Function called when circuit opens (default: undefined)
    • resetAfter: Milliseconds to wait before trying half-open (default: 30_000)

Returns

A function with the same signature as fn and additional methods:

  • .dispose(message?): Clean up resources and reject future calls
  • .getLatestError(): Returns the error which triggered the circuit breaker
  • .getState(): Returns current circuit state ('closed', 'open', 'halfOpen')

Helper Functions

withRetry(fn, options?)

Wraps a function with retry logic. Failures will be retried according to the provided options.

Options:

  • maxAttempts: Maximum number of attempts (default: 3)
  • retryDelay: Function returning promise for when to retry (default: immediate)
  • shouldRetry: Function to determine if error should be retried (default: () => true)

Example:

const retryCall = withRetry(apiCall, {
	maxAttempts: 5,
	retryDelay: useExponentialBackoff(30),
	shouldRetry: (error) => error.statusCode !== 404,
})

useExponentialBackoff(maxSeconds)

Returns a retry delay function that implements exponential backoff (2^n seconds, capped at maxSeconds).

useFibonacciBackoff(maxSeconds)

Returns a retry delay function that implements Fibonacci backoff (Fibonacci sequence in seconds, capped at maxSeconds).

withTimeout(fn, timeoutMs, message?)

Wraps a function with a timeout. Rejects with Error(message) if execution exceeds timeoutMs.

Development

Building the Project

npm run build

Running Tests

npm test # once

npm run dev # run and watch for file changes

Contributing

Contributions are welcome! Please open an issue or submit a pull request.