-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from AikidoSec/fix-hono-body-locked
Fix Hono response body locked error
- Loading branch information
Showing
4 changed files
with
178 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Context } from "hono"; | ||
import { getContext, updateContext } from "../../agent/Context"; | ||
import { createWrappedFunction, isWrapped } from "../../helpers/wrap"; | ||
|
||
// Wrap the request body parsing functions to update the context with the parsed body, if any of the functions are called. | ||
export function wrapRequestBodyParsing(req: Context["req"]) { | ||
req.parseBody = wrapBodyParsingFunction(req.parseBody); | ||
req.json = wrapBodyParsingFunction(req.json); | ||
req.text = wrapBodyParsingFunction(req.text); | ||
} | ||
|
||
function wrapBodyParsingFunction<T extends Function>(func: T) { | ||
if (isWrapped(func)) { | ||
return func; | ||
} | ||
|
||
return createWrappedFunction(func, function parse(parser) { | ||
return async function wrap() { | ||
// @ts-expect-error No type for arguments | ||
// eslint-disable-next-line prefer-rest-params | ||
const returnValue = await parser.apply(this, arguments); | ||
|
||
if (returnValue) { | ||
const context = getContext(); | ||
if (context) { | ||
updateContext(context, "body", returnValue); | ||
} | ||
} | ||
|
||
return returnValue; | ||
}; | ||
}) as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters