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: add routeSchema and routeConfig + switching context handling #4216

Merged
merged 18 commits into from Sep 27, 2022

Conversation

metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Aug 23, 2022

Implements #4149

Description:

As stated in #4149, this PR introduces:

  1. Hide the Context object tight to the route through a Symbol
  2. Expose a public API to fetch pieces of Route config through the request object with the signature of request.routeContext.

Notes

  • This is an initial implementation and can be changed over time through different iterations until we're happy with the shape. This is the reason why is pointing to the next major version of Fastify
  • The current settings exposed right now are:
    • schema
    • config
    • logLevel (which is also configurable)
  • Testing is pending, will be added later within this PR

Open Questions

  • Should we make the exposed properties "read-only"?
  • What else should be exposed through the public API?

Checklist

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@metcoder95 metcoder95 changed the base branch from main to next August 23, 2022 21:48
@metcoder95 metcoder95 marked this pull request as ready for review September 5, 2022 16:21
Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Should we make the exposed properties "read-only"?

Yes: any changes will not be processed by fastify.

What else should be exposed through the public API?

I think we should start small instead of adding unwanted stuff.
The initial request was to access the schema only

lib/reply.js Show resolved Hide resolved
lib/context.js Outdated
return context.config
}
},
logLevel: {
Copy link
Member

Choose a reason for hiding this comment

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

Why this addition?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was playing a bit and might be beneficial to have some sort of single place where to look for specific settings of the route definition. I was thinking of a single read-only approach but wanted to get your thoughts on this, as might/might-not have sense. wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

I would not say it is non-sense, it is just something nobody asked for so I would work on it in a separate PR if necessary instead of adding too much code on this one

Copy link
Member Author

Choose a reason for hiding this comment

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

Has sense, let's keep it clean 👍
Done in 2c18837

lib/request.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

Is this regressing on perf?

@Eomm
Copy link
Member

Eomm commented Sep 13, 2022

One failing test

I think we could target main instead of next.
Is @fastify/core against it?

@climba03003
Copy link
Member

One failing test

The failing test is non-related to the PR changes.
It is related to #4260

It just need to rebase.

@metcoder95
Copy link
Member Author

Is this regressing on perf?

@mcollina
Thankfully no so far, these are the results:

feat/4149 -> next
image

main
image

Specs

    Hardware Overview:

      Model Name: MacBook Pro
      Model Identifier: MacBookPro18,1
      Chip: Apple M1 Pro
      Total Number of Cores: 10 (8 performance and 2 efficiency)
      Memory: 16 GB
      System Firmware Version: 7459.141.1
      OS Loader Version: 7459.141.1

There is even an extremely small improvement within this branch. But no regression so far. Maybe someone else can double-check this?

@metcoder95
Copy link
Member Author

The failing test is non-related to the PR changes.
It is related to #4260

It just need to rebase.

Can I have some help rebasing on next so I can do the proper? 🙂

# Conflicts:
#	fastify.js
#	package.json
@climba03003
Copy link
Member

The failing test is non-related to the PR changes.
It is related to #4260
It just need to rebase.

Can I have some help rebasing on next so I can do the proper? 🙂

I have merged the main branch into next.
Can you check if rebase works in your local branch?
If it does works, I believe it can be switched from next to main safely.

@metcoder95
Copy link
Member Author

Done @climba03003, thanks for the rebase on next 🙂

@Eomm Eomm changed the base branch from next to main September 14, 2022 16:08
docs/Reference/Request.md Show resolved Hide resolved
fastify.js Outdated Show resolved Hide resolved
@@ -130,7 +87,8 @@ test('Should honor maxParamLength option', t => {
})
})

test('Should expose router options via getters on request and reply', t => {
// TODO: fix once agreed on the new router options object
test('Should expose router options via getters on request and reply', { todo: true }, t => {
Copy link
Member

Choose a reason for hiding this comment

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

revert

package.json Outdated Show resolved Hide resolved
docs/Reference/Reply.md Show resolved Hide resolved
Copy link
Contributor

@zrosenbauer zrosenbauer left a comment

Choose a reason for hiding this comment

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

Only question I have is there a number of "TODOS" added and usually I link those to issues if I can, especially things like "remove in v5".

(I'm also new to community here so wondering if it handled differently)

@metcoder95
Copy link
Member Author

Only question I have is there a number of "TODOS" added and usually I link those to issues if I can, especially things like "remove in v5".

(I'm also new to community here so wondering if it handled differently)

Hey! 🙂
Not so sure how to handle those as the release of v5 will happen over 2023. No so sure if we want to keep the issue hanging all that time. We can track it in some other way, but now that you noticed the TODO I actually need to remove them as it's not needed anymore

@metcoder95
Copy link
Member Author

Change has been done and pushed, @Eomm can you have a second look? 🙂

@zrosenbauer
Copy link
Contributor

Only question I have is there a number of "TODOS" added and usually I link those to issues if I can, especially things like "remove in v5".
(I'm also new to community here so wondering if it handled differently)

Hey! 🙂 Not so sure how to handle those as the release of v5 will happen over 2023. No so sure if we want to keep the issue hanging all that time. We can track it in some other way, but now that you noticed the TODO I actually need to remove them as it's not needed anymore

SWEET!

I personally don't love TODOS in code getting merged (w/o an issue reference) as they fall into the nether and are (sometimes) important. Don't know if core maintainers have opinions... and don't want to create a problem where there might not be one 😄

Copy link
Contributor

@zrosenbauer zrosenbauer left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Last chores and then LGTM 👍🏽

@@ -84,7 +84,6 @@ object that exposes the following functions and properties:
from Node core.
- `.log` - The logger instance of the incoming request.
- `.request` - The incoming request.
- `.context` - Access the [Request's context](./Request.md) property.
Copy link
Member

Choose a reason for hiding this comment

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

we should kepp it right now

Copy link
Member Author

Choose a reason for hiding this comment

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

Also done in f3be7b5

docs/Reference/Request.md Show resolved Hide resolved
Comment on lines 138 to 139
t.equal(reply.context.config.url, '/test/:id')
t.equal(reply.context.config.method, 'GET')
Copy link
Member

Choose a reason for hiding this comment

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

I would keep these two lines: they will fail when we will remove the context option but till then we need to test it

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, done in f3be7b5

@Eomm Eomm linked an issue Sep 24, 2022 that may be closed by this pull request
2 tasks
@Eomm Eomm changed the title feat!: Switch context handling feat: add routeSchema and routeConfig + switching context handling Sep 24, 2022
@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Sep 25, 2022
@mcollina
Copy link
Member

Can you please include a benchmark?

@RafaelGSS RafaelGSS added the benchmark Label to run benchmark against PR and main branch label Sep 26, 2022
@github-actions
Copy link

Node: 14
PR: [1] 1706k requests in 30.05s, 320 MB read
MAIN: [1] 1699k requests in 30.06s, 319 MB read


Node: 16
PR: [1] 914k requests in 30.09s, 172 MB read
MAIN: [1] 932k requests in 30.13s, 175 MB read


Node: 18
PR: [1] 850k requests in 30.06s, 160 MB read
MAIN: [1] 836k requests in 30.07s, 157 MB read

@github-actions github-actions bot removed the benchmark Label to run benchmark against PR and main branch label Sep 26, 2022
Copy link
Member

@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

@Eomm Eomm merged commit 7ffefaf into fastify:main Sep 27, 2022
@Eomm Eomm deleted the feat/4149 branch September 27, 2022 22:04
@SimenB SimenB mentioned this pull request Nov 25, 2022
2 tasks
@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 Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
semver-minor Issue or PR that should land as semver minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose schema in request object
6 participants