@@ -33,6 +33,7 @@ import {
3333 SAFE_CHARS ,
3434 UNSAFE_CHARS ,
3535} from './constants.js' ;
36+ import { handlerResultToWebResponse } from './converters.js' ;
3637
3738export function getPathString ( path : Path ) : string {
3839 return isString ( path ) ? path : path . source . replaceAll ( / \\ \/ / g, '/' ) ;
@@ -230,6 +231,11 @@ export const isExtendedAPIGatewayProxyResult = (
230231 * follows the onion model where middleware executes in order before `next()` and in
231232 * reverse order after `next()`.
232233 *
234+ * When a middleware returns a value(short-circuits), that result becomes the response
235+ * and the `res` object in the `RequestContext` is mutated with that result converted
236+ * to a Web Response preserving any existing headers while applying the status code
237+ * from the middleware result.
238+ *
233239 * @param middleware - Array of middleware functions to compose
234240 * @returns A single middleware function that executes all provided middleware in sequence
235241 *
@@ -294,8 +300,13 @@ export const composeMiddleware = (middleware: Middleware[]): Middleware => {
294300 ) ;
295301 }
296302
303+ // middleware result takes precedence to allow short-circuiting
297304 if ( middlewareResult !== undefined ) {
298305 result = middlewareResult ;
306+ reqCtx . res = handlerResultToWebResponse ( middlewareResult , {
307+ statusCode : getStatusCode ( middlewareResult ) ,
308+ resHeaders : reqCtx . res . headers ,
309+ } ) ;
299310 }
300311 } ;
301312
0 commit comments