import * as plugins from '../plugins.js'; import type { S3Context } from './context.js'; export type Middleware = ( req: plugins.http.IncomingMessage, res: plugins.http.ServerResponse, ctx: S3Context, next: () => Promise ) => Promise; /** * Middleware stack for composing request handlers */ export class MiddlewareStack { private middlewares: Middleware[] = []; /** * Add middleware to the stack */ public use(middleware: Middleware): void { this.middlewares.push(middleware); } /** * Execute all middlewares in order */ public async execute( req: plugins.http.IncomingMessage, res: plugins.http.ServerResponse, ctx: S3Context ): Promise { let index = 0; const next = async (): Promise => { if (index < this.middlewares.length) { const middleware = this.middlewares[index++]; await middleware(req, res, ctx, next); } }; await next(); } }