mware Middleware Stack Creator
raw JSON →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.
Common errors
error TypeError: Cannot read properties of undefined (reading 'use') ↓
mware function to get an instance. Correct: const { use, run } = mware(); error Error: next is not a function ↓
(ctx, next) as arguments, or however you've chosen to name your context and next callback. error Middleware stack stopped unexpectedly / Stack never completes ↓
next() (or return next(null, result)/return next(error)) to ensure control flow proceeds or terminates correctly. Warnings
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. ↓
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. ↓
Install
npm install mware yarn add mware pnpm add mware Imports
- mware wrong
import { mware } from 'mware';correctimport mware from 'mware'; - mware instance methods wrong
import { use, run } from 'mware';correctconst { use, run } = mware(); - mware (CommonJS) wrong
const { mware } = require('mware');correctconst mware = require('mware');
Quickstart
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);
});