Ware Middleware Composer
raw JSON →Ware is a lightweight JavaScript library designed to easily create and manage a middleware layer for applications. It provides a simple API for composing synchronous, asynchronous, and generator functions into a sequential processing pipeline. The package's current stable version is 1.3.0, released in May 2015. Due to its age, development is no longer active, and the library is considered abandoned. It predates modern `async/await` syntax, relying instead on Node.js's older generator-based asynchronous patterns and explicit `next()` calls for flow control. Its primary differentiator was its simplicity and ability to handle various function types for request/response processing, similar to frameworks like Express but without the opinionated structure, allowing for flexible custom middleware stacks.
Common errors
error TypeError: ware is not a function ↓
const ware = require('ware'); as it is a CommonJS module. error Error: next() called multiple times ↓
next() is called exactly once. This often happens in conditional logic or error handling where next() might be called in multiple branches. error Application hangs or does not proceed past a certain middleware ↓
next() is called for synchronous operations, or that a Promise is returned and resolved for asynchronous operations. Warnings
breaking The package has not been updated since 2015 (version 1.3.0) and uses older Node.js patterns like generator functions. It does not natively support modern `async/await` syntax, which might lead to unexpected behavior or require manual Promise wrapping for asynchronous middleware. ↓
gotcha Middleware functions are expected to call `next()` to pass control to the next function in the chain. Failing to call `next()` (or resolve a Promise in an async function) will halt the middleware execution prematurely, potentially leaving requests hanging or state unupdated. ↓
breaking Ware is a CommonJS module and does not natively support ES Modules (`import`/`export`). Attempting to `import ware from 'ware'` in an ESM context will result in a runtime error. ↓
Install
npm install ware yarn add ware pnpm add ware Imports
- ware wrong
import ware from 'ware'correctconst ware = require('ware') - WareInstance wrong
import { Ware } from 'ware'correctconst middleware = ware()
Quickstart
const ware = require('ware');
const myMiddleware = ware()
.use(function (ctx, next) {
ctx.log = [];
ctx.log.push('Middleware 1: Before next()');
next();
ctx.log.push('Middleware 1: After next()');
})
.use(function (ctx, next) {
return new Promise(resolve => {
setTimeout(() => {
ctx.log.push('Middleware 2: Async operation');
next();
resolve();
}, 50);
});
})
.use(function (ctx) {
ctx.log.push('Middleware 3: Final step');
ctx.result = 'Processed';
});
const context = {};
myMiddleware.run(context, function (err, finalContext) {
if (err) {
console.error('Error in middleware chain:', err);
} else {
console.log('Final Context:', finalContext);
console.log('Execution Log:', finalContext.log);
// Expected output for log:
// [
// 'Middleware 1: Before next()',
// 'Middleware 2: Async operation',
// 'Middleware 3: Final step',
// 'Middleware 1: After next()'
// ]
}
});