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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-breakers

v2.0.0

Published

Basic circuit breaker implementation

Downloads

23

Readme

npm version Build Status codecov

ts-breakers

This package aims to provide a simple, basic and efficient Circuit Breaker implementation.

The CircuitBreaker pattern is a stateful pattern, as it associates a state to function calls in order to decide if it should fail fast, give a try to the function execution, or execute it normally.

Usage

Some usage examples are available as tests in CircuitBreaker.test.ts file.

The most basic example is to create a CircuitBreaker instance (for holding the state) then associate it to your subject to failure function by wrapping it.

The wrapped function has the exact same interface than the function it wraps and two functions are offered for wrapping synchronous and asynchronous functions.

Synchronous interface

To wrap a synchronous function, you have to call circuitBreaker.wrapFunction.

const sum = (a: number, b: number): number => a + b;
const cb = new CircuitBreaker('mycb', 5, 2000);
const wrapped = cb.wrapFunction(sum);
const result = wrapped(2, 3);
expect(result).toBe(5);

You may also want to be notified on state changes for logging or collecting metric:

const sum = (a: number, b: number): number => a + b;
const cb = new CircuitBreaker('mycb', 5, 2000);
cb.addObserver((previousState: CircuitBreakerState, currentState: CircuitBreakerState) => {
    console.log(`${previousState} => ${currentState}`);
});

const wrapped = cb.wrapFunction(sum);
const result = wrapped(2, 3);
expect(result).toBe(5);

Asynchronous interface

To wrap an asynchronous function, you have to call circuitBreaker.wrapAsyncFunction.

Example:

const sum = async (a: number, b: number): Promise<number> => {
    return Promise.resolve(a + b);
}

const cb = new CircuitBreaker('mycb', 5, 2000);
const wrapped = cb.wrapAsyncFunction(sum);
const result = wrapped(2, 3);
await expect(result).resolves.toBe(5);

You may also want to be notified on state changes for logging or collecting metric:

const sum = async (a: number, b: number): Promise<number> => {
    return Promise.resolve(a + b);
}
const cb = new CircuitBreaker('mycb', 5, 2000);
cb.addObserver((previousState: CircuitBreakerState, currentState: CircuitBreakerState) => {
    console.log(`${previousState} => ${currentState}`);
});

const wrapped = cb.wrapAsyncFunction(sum);
const result = wrapped(2, 3);
await expect(result).resolves.toBe(5);

Testing

Tests are run:

  • locally by using npm test which runs jest test suite on the local branch
  • within the CI which runs jest tests suite on every pushed branch and collects the test report within CircleCI UI

The tests are meant to cover the state machine transitions. They are designed as parameterized tests and written to serve as usage example for the end user.

Publishing a new version

Use the standard npm version x.y.z to update the package version, tag the main branch and push the tags to the GitHub repository.

The CircleCI pipeline will run the tests and publish the new package to npm.