feat: allow manually registering the attachFieldsToBody preValidation hook - #634
feat: allow manually registering the attachFieldsToBody preValidation hook#634Truta446 wants to merge 2 commits into
Conversation
climba03003
left a comment
There was a problem hiding this comment.
Verify signature means it needs to read the payload.
When the payload is consumed by the user, attachFieldsToBody can't do anything later.
|
You're absolutely right, verifying a signature means reading the payload, and once it's read attachFieldsToBody() has nothing left to parse. That use case isn't what this option solves; it belongs in a preParsing hook that tees the stream (which is what #620 enables). I've dropped the signature-verification framing from this PR: the README example now shows a correct use case (rejecting early, before the plugin buffers the upload, using headers/token rather than the body), plus an explicit note that attachFieldsToBody() consumes the stream and that payload-reading concerns like signature verification should tee the stream in preParsing instead. The test was updated to match. |
Summary
Fixes #599.
When
attachFieldsToBodyis enabled, the plugin registers its ownpreValidationhook at the instance level. Because instance-level hooksalways run before route-level ones (and in registration order relative to
other instance-level hooks in the same scope), users had no way to control
this hook's position relative to their own
preValidationhooks — e.g. ahook that verifies a signature on the raw body before any field parsing
happens.
This PR extracts the hook body into a method decorated on the request,
request.attachFieldsToBody(), and adds a newattachFieldsToBodyHookoption (default
true, matching current behavior). When set tofalse,the plugin skips automatic registration and the user calls
request.attachFieldsToBody()themselves, from whichever hook and inwhatever order they choose:
No behavior change when
attachFieldsToBodyHookis left unset.Test plan
test/multipart-attach-body.test.jsreproducing theissue's scenario: a custom
preValidationhook that must run beforefield parsing, using
attachFieldsToBodyHook: falseto control ordering.npm run test:unit— 91/91 passing, 100% coverage.npm run test:typescript— passing.npm run lint— clean.