Skip to content

feat: align the rest of the ContentTypeParser methods with .add#5345

Closed
gurgunday wants to merge 14 commits intofastify:nextfrom
gurgunday:align-methods
Closed

feat: align the rest of the ContentTypeParser methods with .add#5345
gurgunday wants to merge 14 commits intofastify:nextfrom
gurgunday:align-methods

Conversation

@gurgunday
Copy link
Member

@gurgunday gurgunday commented Mar 2, 2024

Per @climba03003's comment, #4477 (comment)

We now trim and lowercase the values of user-added ContentTypeParsers so that weird scenarios like this are not possible

The same behavior should be exhibited by .remove and .hasParser for consistency

After the merge, follow-up PRs:

@gurgunday gurgunday requested a review from a team March 2, 2024 11:48
}

ContentTypeParser.prototype.getParser = function (contentType) {
contentType = contentType.trim()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel this will slow us down

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, .trim should place way lower place and it should be another string.
So, it would cache the original header and utilize the cache.

Copy link
Member Author

@gurgunday gurgunday Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal method to match the content type and seems like Content-Type is matched correctly even if there are surrounding spaces - so trimming already happens somewhere

As a result, I removed any behavioral changes from it, focusing on the public API

@gurgunday gurgunday added benchmark Label to run benchmark against PR and main branch and removed benchmark Label to run benchmark against PR and main branch labels Mar 3, 2024
@gurgunday
Copy link
Member Author

gurgunday commented Mar 4, 2024

Ok everything is much better/simpler now - note that getParser always takes a string parameter

Please take a look

@gurgunday gurgunday requested a review from a team March 4, 2024 10:08
@Uzlopak Uzlopak added the benchmark Label to run benchmark against PR and main branch label Mar 5, 2024
@gurgunday gurgunday removed the benchmark Label to run benchmark against PR and main branch label Mar 5, 2024
@gurgunday
Copy link
Member Author

Benchmark isn't working on next I believe

const result = parser.fn(request, request[kRequestPayloadStream], done)

if (result && typeof result.then === 'function') {
if (typeof result?.then === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think that this is faster.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not slower either, it just reads better imo, do you disagree?

There is no string comparison as I mentioned previously

const contentLength = request.headers['content-length'] === undefined
? NaN
: Number(request.headers['content-length'])
const contentLength = Number(request.headers['content-length'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please explain

Copy link
Member Author

@gurgunday gurgunday Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isNaN(Number(undefined)) === true

That check does nothing

@gurgunday gurgunday requested a review from mcollina March 6, 2024 07:36
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd prefer to wait for @climba03003's review

@climba03003
Copy link
Member

I need to test it when I have time.
Just want to figure out where did the .trim happen before .getParser.

@gurgunday
Copy link
Member Author

Just want to figure out where did the .trim happen before .getParser.

Please do let me know when you find it 😁

@gurgunday
Copy link
Member Author

gurgunday commented Mar 22, 2024

@climba03003 I think node does it since this returns the trimmed version here:

  fastify.post('/', (req, reply) => {
    console.log(req.raw.header['content-type'])
    reply.send(req.body)
  })

@gurgunday
Copy link
Member Author

I've found a bug with the contentType matching, so it would be appreciated if we can get this merged

I will be concentrating on v5 for a few days

@gurgunday gurgunday requested a review from metcoder95 March 24, 2024 18:05
@gurgunday
Copy link
Member Author

Merged at #5372

@gurgunday gurgunday closed this Mar 25, 2024
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants