Skip to content
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

feat: disable semicolon separator by default #336

Merged
merged 3 commits into from
Dec 26, 2023

Conversation

levensta
Copy link
Contributor

This PR comes from the discussion in the issue (fastify/fastify#5050). I've added a new parameter that allows the ; character to be used to separate path and query as before, but its default value is false to be fully compliant with the RFC

Copy link
Collaborator

@ivan-tymoshenko ivan-tymoshenko left a comment

Choose a reason for hiding this comment

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

This is really weird, but it looks like it's correct. Can you provide a bench results?
@mcollina This is a major for find-my-way.

@@ -88,6 +88,7 @@ function Router (opts) {
this.maxParamLength = opts.maxParamLength || 100
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
this.constrainer = new Constrainer(opts.constraints)
this.useSemicolonDelimiter = opts.useSemicolonDelimiter || false
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to set it true in the Fastify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally this option should be disabled in Fastify as well, but I don't know the best way to do it: leave the default false here and open issue/PR to update dependencies in Fastify, or make it true here and send PR to Fastify with the option disabled

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would leave the false here, because it's a correct behavior and we can release a new major of fmw at any moment. In current version of Fastify it should be set to true by default, because otherwise it's a breaking change.

About changing it to false in the fastify in a future release. It's better to open an issue about this in fastify, but IMHO it never would be set to true by defauld in the Fastify, because it will break too many apps. It just doesn't worth it even if it's true according to the spec.

// Thus, we need to split on `;` as well as `?` and `#`.
} else if (charCode === 63 || charCode === 59 || charCode === 35) {
// Thus, we need to split on `;` as well as `?` and `#` if the useSemicolonDelimiter option is enabled.
} else if (charCode === 63 || charCode === 35 || (useSemicolonDelimiter && charCode === 59)) {
querystring = path.slice(i + 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there is any perf difference (I doubt), you can move the useSemicolonDelimiteradditional check under the charCode checking if. That would remove it from a hot path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, given that the ; symbol is rare, that's correct. I fixed

@levensta
Copy link
Contributor Author

Can you provide a bench results?

I made a simple example in the sandbox
https://codesandbox.io/p/sandbox/find-my-way-semicolon-g29hw7

btw, it works the same way for the # character, but I also don't understand for which situation this is necessary. At the same time in Fastify the hash from request does not get into query object

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Collaborator

@levensta did you run the benchmarks by any chance?

@levensta
Copy link
Contributor Author

@levensta did you run the benchmarks by any chance?

I run bench:cmp and got these results 🤔

screenshot

Снимок экрана 2023-09-26 в 17 57 18

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you document the new option?

@levensta
Copy link
Contributor Author

levensta commented Oct 6, 2023

@mcollina I've updated the README. But I am still confused by the fact that there is still a # character, which also separates path and query. It is not a problem in Fastify, but is it needed here? Maybe I should remove it too?

@mcollina
Copy link
Collaborator

mcollina commented Oct 7, 2023

I'm lost. can you make an example?

@levensta
Copy link
Contributor Author

levensta commented Oct 9, 2023

I'm lost. can you make an example?

In this example, you can see that both ; and # work as delimiters
https://codesandbox.io/p/sandbox/find-my-way-semicolon-g29hw7?file=%2Findex.js%3A7%2C1

@mcollina
Copy link
Collaborator

That does not look correct, what does the rfc say?