mware Middleware Stack Creator

raw JSON →
1.0.1 verified Thu Apr 23 auth: no javascript abandoned

mware is a lightweight utility designed to create and manage sequential middleware stacks, inspired by patterns found in frameworks like Connect. It provides a simple API (`use` and `run`) to define a series of functions that process a shared context and can either proceed to the next middleware, halt the stack with a success signal, or terminate it by reporting an error. The current stable version is 1.0.1. However, the project's copyright date of 2016 and the nature of its last fix suggest it is largely unmaintained. Its key differentiator is its minimalism, offering a core middleware pattern without the overhead or specific request/response abstractions typical of larger web frameworks, making it suitable for generic processing pipelines in both Node.js and browser environments.

error TypeError: Cannot read properties of undefined (reading 'use')
cause Attempting to call `.use()` or `.run()` directly on the `mware` import instead of an `mware` instance.
fix
You must first call the mware function to get an instance. Correct: const { use, run } = mware();
error Error: next is not a function
cause Middleware function arguments are incorrectly destructured or not provided, leading to `next` being undefined.
fix
Ensure your middleware function correctly accepts (ctx, next) as arguments, or however you've chosen to name your context and next callback.
error Middleware stack stopped unexpectedly / Stack never completes
cause A middleware function failed to call `next()` (or `return next()`), causing the stack to hang or stop without reporting completion to the `done` callback.
fix
Every middleware function must eventually call next() (or return next(null, result)/return next(error)) to ensure control flow proceeds or terminates correctly.
gotcha The `mware` project appears to be unmaintained. The latest copyright date in the README is 2016, and version 1.0.1 primarily includes a bug fix, indicating a lack of recent active development or new features. Users should be aware there might not be future updates or security patches.
fix Evaluate for alternatives if long-term maintenance, security, or feature development is a concern, or consider forking for internal maintenance.
gotcha Error handling in `mware` middleware functions is done by calling `next(new Error())` rather than `throw new Error()`. Throwing an error within a middleware function will bypass the `done` callback and might lead to unhandled promise rejections or uncaught exceptions, depending on the surrounding execution environment.
fix Always pass errors to the `next` function (e.g., `return next(new Error('...'))`) to ensure they are properly caught by the `done` callback.
npm install mware
yarn add mware
pnpm add mware

This example demonstrates how to initialize `mware`, add multiple asynchronous middleware functions, and run the stack with a context. It shows how middleware can modify the context, stop the stack early with a result, or terminate with an error.

import mware from 'mware';

const { use, run } = mware();

// Add middleware functions
use((ctx, next) => {
    console.log('Middleware 1: Processing context', ctx);
    // Modify context or perform async operations
    setTimeout(() => next(), 100);
});

use((ctx, next) => {
    console.log('Middleware 2: Further processing', ctx);
    if (ctx.shouldStop) {
        console.log('Middleware 2: Stopping stack early.');
        return next(null, 'Stopped Early'); // Stop the stack with a result
    }
    return next(); // Proceed to the next middleware
});

use((ctx, next) => {
    console.log('Middleware 3: This might not run if stopped early.');
    // Simulate an error condition
    if (ctx.fail) {
        return next(new Error('Simulated error in middleware 3')); // Stop and report error
    }
    next();
});

// Run the stack with an initial context
const context = { value: 42, shouldStop: false, fail: false };
run([context], (err, result) => {
    if (err) {
        console.error('Stack completed with error:', err.message);
    } else {
        console.log('Stack completed successfully. Result:', result || 'No explicit result');
    }
    console.log('Final context state:', context);
});