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

@daisugi/kintsugi

v0.5.2

Published

Kintsugi is a set of utilities to help build a fault tolerant services.

Downloads

118

Readme

@daisugi/kintsugi

version npm downloads bundlephobia

This project is part of the @daisugi monorepo.

Zero dependencies and small size. | Used in production.

Kintsugi is a set of utilities to help build a fault tolerant services.

Usage

import {
  reusePromise,
  waitFor,
  withCache,
  withCircuitBreaker,
  withRetry,
} from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

async function fn() {
  await waitFor(1000);

  return Result.success('Hi Benadryl Cumberbatch.');
}

const rockSolidFn = withCache(
  withRetry(withCircuitBreaker(reusePromise(fn))),
);

Table of contents

Install

Using npm:

npm install @daisugi/kintsugi

Using yarn:

yarn add @daisugi/kintsugi

:top: back to top

API

Notice the helpers provided by this library are expecting that your functions are returning result instance as responses.

withCache

Cache serializable function calls results.

Usage

import { withCache } from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

function fnToBeCached() {
  return Result.success('Hi Benadryl Cumberbatch.');
}

const fnWithCache = withCache(fnToBeCached);

fnWithCache();

API

withCache(fn: Function, opts: Object = {}) => Function;
  • fn Function to be cached.

  • opts Is an object that can contain any of the following properties:

    • cacheStore An instance of the cache store, implementing CacheStore interface (default: SimpleMemoryStore).

    • version Version string used to build the cache key. Useful to manually invalidate cache key (default: v1).

    • maxAgeMs also known as TTL (default: 14400000) 4h.

    • buildCacheKey The function used to generate cache key, it receives a hash of the source code of the function itself (fnHash), needed to automatically invalidate cache when function code is changed, also receives version, and the last parameter are arguments provided to the original function (args). Default:

      function buildCacheKey(fnHash, version, args) {
        return `${fnHash}:${version}:${stringify(args)}`;
      }
    • calculateCacheMaxAgeMs Used to calculate max age in ms, uses jitter, based on provided maxAgeMs property, default:

      function calculateCacheMaxAgeMs(maxAgeMs) {
        return randomBetween(maxAgeMs * 0.75, maxAgeMs);
      }
    • shouldCache Determines when and when not cache the returned value. By default caches NotFound code. default:

      function shouldCache(response) {
        if (response.isSuccess) {
          return true;
        }
      
        if (
          response.isFailure &&
          response.error.code === Code.NotFound
        ) {
          return true;
        }
      
        return false;
      }
    • shouldInvalidateCache Useful to determine when you need to invalidate the cache key. For example provide refresh parameter to the function. default:

      function shouldInvalidateCache(args) {
        return false;
      }

    buildCacheKey, calculateCacheMaxAgeMs, shouldCache and shouldInvalidateCache are also exported, useful for the customizations.

Examples

Some examples to see how to use withCache with custom stores in your applications.

:top: back to top

withRetry

Retry function calls with an exponential backoff and custom retry strategies for failed operations. Retry is useful to avoid intermittent network hiccups. Retry may produce a burst number of requests upon dependent services is why it need to be used in combination with other patterns.

Usage

import { withRetry } from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

function fn() {
  return Result.success('Hi Benadryl Cumberbatch.');
}

const fnWithRetry = withRetry(fn);

fnWithRetry();

API

withRetry(fn: Function, opts: Object = {}) => Function;
  • fn Function to wrap with retry strategy.

  • opts Is an object that can contain any of the following properties:

    • firstDelayMs Used to calculate retry delay (default: 200).

    • maxDelayMs Time limit for the retry delay (default: 600).

    • timeFactor Used to calculate exponential backoff retry delay (default: 2).

    • maxRetries Limit of retry attempts (default: 3).

    • calculateRetryDelayMs Function used to calculate delay between retry calls. By default calculates exponential backoff with full jitter. Default:

      function calculateRetryDelayMs(
        firstDelayMs,
        maxDelayMs,
        timeFactor,
        retryNumber,
      ) {
        const delayMs = Math.min(
          maxDelayMs,
          firstDelayMs * timeFactor ** retryNumber,
        );
      
        const delayWithJitterMs = randomBetween(0, delayMs);
      
        return delayWithJitterMs;
      }
    • shouldRetry Determines when retry is needed. By default takes in account the max number of attempts, and if was block by circuit breaker. Default:

      function shouldRetry(
        response,
        retryNumber,
        maxRetries,
      ) {
        if (response.isFailure) {
          if (response.error.code === Code.CircuitSuspended) {
            return false;
          }
      
          if (retryNumber < maxRetries) {
            return true;
          }
        }
      
        return false;
      }

    calculateRetryDelayMs and shouldRetry are also exported, useful for the customizations.

:top: back to top

withTimeout

Wait for the response of the function, if it exceeds the maximum time, it returns a result with timeout. Useful to time limit in not mandatory content.

Usage

import {
  withTimeout,
  waitFor,
} from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

async function fn() {
  await waitFor(8000);

  return Result.success('Hi Benadryl Cumberbatch.');
}

const fnWithTimeout = withTimeout(fn);

fnWithTimeout();

API

withTimeout(fn: Function, opts: Object = {}) => Function;
  • fn Function to be wrapped with timeout.

  • opts Is an object that can contain any of the following properties:

    • maxTimeMs Max time to wait the function response, in ms. (default: 600).

:top: back to top

withCircuitBreaker

An implementation of the Circuit-breaker pattern using sliding window. Useful to prevent cascading failures in distributed systems.

Usage

import { withCircuitBreaker } from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

function fn() {
  return Result.success('Hi Benadryl Cumberbatch.');
}

const fnWithCircuitBreaker = withCircuitBreaker(fn);

fnWithCircuitBreaker();

API

withCircuitBreaker(fn: Function, opts: Object = {}) => Function;
  • fn Function to wrap with circuit-breaker strategy.

  • opts Is an object that can contain any of the following properties:

    • windowDurationMs Duration of rolling window in milliseconds. (default: 30000).

    • totalBuckets Number of buckets to retain in a rolling window (default: 10).

    • failureThresholdRate Percentage of the failures at which the circuit should trip open and start short-circuiting requests (default: 50).

    • volumeThreshold Minimum number of requests in rolling window needed before tripping the circuit will occur. (default: 10).

    • returnToServiceAfterMs Is the period of the open state, after which the state becomes half-open. (default: 5000).

    • isFailureResponse Function used to detect failed requests. Default:

      function isFailureResponse(response) {
        if (response.isSuccess) {
          return false;
        }
      
        if (
          response.isFailure &&
          response.error.code === Code.NotFound
        ) {
          return false;
        }
      
        return true;
      }

    isFailureResponse is also exported, useful for the customizations.

:top: back to top

reusePromise

Prevent an async function to run more than once concurrently by temporarily caching the promise until it's resolved/rejected.

Usage

import {
  reusePromise,
  waitFor,
} from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

async function fnToBeReused() {
  await waitFor(1000);

  return Result.success('Hi Benadryl Cumberbatch.');
}

const fn = reusePromise(fnToBeReused);

fn(); // It runs the promise and waits the response.
fn(); // Waits the response of the running promise.

API

reusePromise(fn: Function) => Function;

:top: back to top

waitFor

Useful promisified timeout.

Usage

import { waitFor } from '@daisugi/kintsugi';

async function fn() {
  await waitFor(1000);

  return Result.success('Hi Benadryl Cumberbatch.');
}

fn();

API

waitFor(delayMs: Number) => Promise;

:top: back to top

SimpleMemoryStore

A simple CacheStore implementation, with get/set methods. It wraps the response into result.

Usage

import { SimpleMemoryStore } from '@daisugi/kintsugi';

const simpleMemoryStore = new SimpleMemoryStore();

simpleMemoryStore.set('key', 'Benadryl Cumberbatch.');

const response = simpleMemoryStore.get('key');

if (response.isSuccess) {
  return response.getValue();
  // 'Benadryl Cumberbatch.'
}

:top: back to top

Code

An enum of HTTP based, and custom status codes, more.

Usage

import { Code, result } from '@daisugi/kintsugi';
import { Result } from '@daisugi/anzen';

function response() {
  return Result.failure({
    message: 'response',
    code: Code.NotFound,
  });
}

:top: back to top

CustomError

Returns inherited Error object with the code property, among the rest of the Error properties.

Usage

import { CustomError } from '@daisugi/kintsugi';

const customError = new CustomError(
  'response',
  Code.NotFound,
);

throw customError;

// customError.toString() would return 'NotFound: response'.
// customError.code === Code.NotFound

API

CustomError(message: string, code: string) => Error;

:top: back to top

deferredPromise

The deferred pattern implementation on top of promise. Returns a deferred object with resolve and reject methods.

Usage

import { deferredPromise } from '@daisugi/kintsugi';

async function fn() {
  const whenIsStarted = deferredPromise();

  setTimeout(() => {
    whenIsStarted.resolve();
  }, 1000);

  return whenIsStarted.promise;
}

fn();

API

deferredPromise() => {
  resolve: (value: unknown) => void,
  reject: (reason?: any) => void,
  promise: Promise,
  isFulfilled: () => Boolean,
  isPending: () => Boolean,
  isRejected: () => Boolean,
};

:top: back to top

randomBetween

A function returns a random integer between given numbers.

Usage

import { randomBetween } from '@daisugi/kintsugi';

const randomNumber = randomBetween(100, 200);
// Random number between 100 and 200.

API

randomBetween(min: Number, max: Number) => Number;

:top: back to top

encToFNV1A

A non-cryptographic hash function.

Usage

import { encToFNV1A } from '@daisugi/kintsugi';

const hash = encToFNV1A(
  JSON.stringify({ name: 'Hi Benadryl Cumberbatch.' }),
);

API

encToFNV1A(input: String | Buffer) => String;

:top: back to top

Etymology

Kintsugi is the Japanese art of repairing a broken object by enhancing its scars with real gold powder, instead of trying to hide them.

More info: https://esprit-kintsugi.com/en/quest-ce-que-le-kintsugi/

:top: back to top

Other projects

Meet the ecosystem

:top: back to top

License

MIT