-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
preParsing hook payload manipulation and consistent Content Type Parser API #2286
preParsing hook payload manipulation and consistent Content Type Parser API #2286
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The migration guide should be updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@jsumners Ok, will update this tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
types lgtm
Migration guide and test symbols updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work!
Co-authored-by: James Sumners <[email protected]>
Co-authored-by: James Sumners <[email protected]>
Co-authored-by: James Sumners <[email protected]>
Co-authored-by: James Sumners <[email protected]>
* Remove deprecation FSTDEP001 * Remove deprecation FSTDEP002 * Remove deprecation FSTDEP003 * fixup! preParsing hook payload manipulation and consistent Content Type Parser API (#2286) * Remove deprecation FSTDEP004
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi all!
This PR focuses on two main things: preParsing hook payload manipulation hook and consistent Content Type Parser API.
The description are below.
This PR solves #1986 and it is needed for fastify/fastify-compress#105 (via fastify/fastify-compress#110).
This PR supersedes #2278.
Hope this helps!
Paolo
preParsing
hook payload manipulationThis PR add supports for a third parameter for the the preParsing hook before the callback, in order to allow manipulation of request body stream before attempting to parse.
The new hook signature is
fn(request,reply,payload,[done])
. Payload is the current request body stream. The done function argument (or the async return value) must be a stream itself.The rationale for this is to being able to handle compressed or encrypted request payload.
At the moment this can be achieved only inside the handler, which forces the developer to give up on fastify's serialization or validation and reimplement them inside the handler itself.
Example of gzipped request payload:
The hook should also ideally set the
receivedEncodedLength
property on the returned stream. The property is used to correctly match the request payload with theContent-Length
header value. Ideally, but completely optional, this property should be updated on each received chunk in order to allow earlier termination for large request payloads.Consistent Content Type Parser API
The PR deprecates the
fn(req, done)
andasync fn(req)
style Content Type Parser in favor offn(request, payload, done)
andasync fn(request, payload)
.The rationale for this change is to have a consistent callback styles API across all Fastify API. This was the last one.
The newer forms are used in all cases:
parseAs
is not passed toaddContentTypeParser
, then payload is a stream.parseAs
is passed toaddContentTypeParser
, then payload is a string or a buffer accordingly.Note that
request
, unlike in the past is a Fastify request object, not anIncomingMessage
.The older forms are still supported but emit a warning when used.
TypeScript notes
For both changes, I did not keep existing types.
The reason for this are the following:
Checklist
npm run test
andnpm run benchmark