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

@fortify-ts/middleware

v2.0.0

Published

Middleware composition chain for @fortify-ts resilience patterns

Readme

@fortify-ts/middleware

Middleware chain for composing resilience patterns in Fortify-TS.

Installation

npm install @fortify-ts/middleware
# or
pnpm add @fortify-ts/middleware

Features

  • Pattern Composition: Combine multiple patterns in a chain
  • Fluent API: Method chaining for easy configuration
  • Custom Middleware: Add your own middleware functions
  • Execution Order: Outer to inner (first added = outermost)

Usage

Basic Usage

import { Chain } from '@fortify-ts/middleware';
import { CircuitBreaker } from '@fortify-ts/circuit-breaker';
import { Retry } from '@fortify-ts/retry';
import { Timeout } from '@fortify-ts/timeout';

const circuitBreaker = new CircuitBreaker({ maxFailures: 5 });
const retry = new Retry({ maxAttempts: 3 });
const timeout = new Timeout({ defaultTimeout: 5000 });

const chain = new Chain<Response>()
  .withCircuitBreaker(circuitBreaker)
  .withRetry(retry)
  .withTimeout(timeout);

const result = await chain.execute(async (signal) => {
  return fetch('/api/data', { signal });
});

Full Pattern Stack

import { Chain } from '@fortify-ts/middleware';
import { Bulkhead } from '@fortify-ts/bulkhead';
import { RateLimiter } from '@fortify-ts/rate-limit';
import { Timeout } from '@fortify-ts/timeout';
import { CircuitBreaker } from '@fortify-ts/circuit-breaker';
import { Retry } from '@fortify-ts/retry';
import { Fallback } from '@fortify-ts/fallback';

const chain = new Chain<Response>()
  .withBulkhead(bulkhead)           // 1st: Limit concurrency
  .withRateLimit(rateLimiter, key)  // 2nd: Rate limiting
  .withTimeout(timeout, 5000)       // 3rd: Timeout
  .withCircuitBreaker(circuitBreaker) // 4th: Circuit breaker
  .withRetry(retry)                 // 5th: Retry
  .withFallback(fallback);          // 6th: Fallback (innermost)

const result = await chain.execute(operation);

Custom Middleware

import { Chain, type Middleware } from '@fortify-ts/middleware';

// Custom logging middleware
const loggingMiddleware: Middleware<Response> = (next) => async (signal) => {
  console.log('Starting operation');
  const start = Date.now();
  try {
    const result = await next(signal);
    console.log(`Completed in ${Date.now() - start}ms`);
    return result;
  } catch (error) {
    console.log(`Failed in ${Date.now() - start}ms`);
    throw error;
  }
};

const chain = new Chain<Response>()
  .use(loggingMiddleware)
  .withRetry(retry);

Execution Order

Middleware executes from first added (outermost) to last added (innermost):

Request Flow:
  Bulkhead → RateLimit → Timeout → CircuitBreaker → Retry → Fallback → Operation

Response Flow:
  Operation → Fallback → Retry → CircuitBreaker → Timeout → RateLimit → Bulkhead

API Reference

| Method | Description | |--------|-------------| | withCircuitBreaker(cb) | Add circuit breaker | | withRetry(retry) | Add retry | | withTimeout(timeout, duration?) | Add timeout | | withRateLimit(rl, key?) | Add rate limiting | | withBulkhead(bh) | Add bulkhead | | withFallback(fb) | Add fallback | | use(middleware) | Add custom middleware | | execute(operation, signal?) | Execute chain |

License

MIT