# @toisu/middleware-runner

> The core middleware runner for Toisu!

Latest version **5.0.0** (published 2025-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @toisu/middleware-runner
pnpm add @toisu/middleware-runner
yarn add @toisu/middleware-runner
bun add @toisu/middleware-runner
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2025-08-17 |
| First published | 2021-04-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 3.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Aura N. Everitt |
| Maintainers | qubyte |
| Keywords | toisu, middleware |

## Links

- npm: https://www.npmjs.com/package/@toisu/middleware-runner
- Repository: https://codeberg.org/qubyte/toisu-monorepo
- Homepage: https://codeberg.org/qubyte/toisu-monorepo/src/branch/main/packages/middleware-runner#readme
- npm.io page: https://npm.io/package/@toisu/middleware-runner

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 5.0.0 (latest) — 2025-08-17
- 4.0.3 — 2025-08-16
- 4.0.2 — 2025-08-16
- 4.0.1 — 2025-08-16
- 4.0.0 — 2023-10-15
- 3.0.6 — 2022-06-25
- 3.0.5 — 2021-10-06
- 3.0.4 — 2021-09-18
- 3.0.3 — 2021-04-28

## README

# Toisu! middleware runner

For Node.js versions less than 14 please use the 2.x line of releases.

Toisu! runs middlewares in sequence, waiting for promises from middlwares to
resolve (when promises are returned) before continuing to the next. This module
contains a function which does this sequential middleware execution. This logic
has been placed in its own module since both `toisu` and `toisu-router` use it.
Other modules intended to work with Toisu! might also find it useful.

## usage

Middleware may either be a function which runs synchronously, or one that
returns a promise. A mixture of synchronous and asynchronous functions may be
used. Note though, that the runner returns a promise and will always resolve
asynchronously.

With plain old promises:
```javascript
import runner from '@toisu/middleware-runner';

// context will be used as the `this` value for middlewares.
runner.call(context, req, res, middlewares)
  .then(responding => {
    // responding is true when either the response headers are sent, or the
    // response is no longer writable for some reason.
  })
  .catch(err => {
    // Run when a middleware threw.
  });
```

With async-await:
```javascript
import runner from '@toisu/middleware-runner';

async function example() {
  let responding;

  try {
    responding = await runner.call(context, req, res, middlwares);
  } catch (e) {
    // Handle errors.
    return;
  }

  // responding is true when either the response headers are sent, or the
  // response is no longer writable for some reason.
}
```

---
_Source: https://npm.io/package/@toisu/middleware-runner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
