Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 34x 34x 52x 1x 51x 51x 31x 20x 20x 34x | /**
* Middleware composer (Koa-style)
* Core logic - no dependencies
*/
import type { Middleware } from '../routing/route-types.js';
import type { VitekContext } from '../context/create-context.js';
/**
* Composes middlewares into a single function
* Executes in order and allows each middleware to call next() to continue
*/
export function compose(middlewares: Middleware[]) {
return async function (context: VitekContext, handler: () => Promise<any>): Promise<any> {
let index = -1;
async function dispatch(i: number): Promise<void> {
if (i <= index) {
throw new Error('next() called multiple times');
}
index = i;
// If all middlewares have been executed, call the final handler
if (i === middlewares.length) {
return handler();
}
const middleware = middlewares[i];
// Execute the current middleware
await middleware(context, () => dispatch(i + 1));
}
return dispatch(0);
};
}
|