Skip to content

Remove middlewares#2014

Merged
delvedor merged 16 commits intonextfrom
remove-middlewares
Mar 16, 2020
Merged

Remove middlewares#2014
delvedor merged 16 commits intonextfrom
remove-middlewares

Conversation

@delvedor
Copy link
Copy Markdown
Member

@delvedor delvedor commented Dec 28, 2019

Hello folks! As you know, we have decided to remove middlewares support from Fastify core and move it to an external plugin, which will be middie and fastify-express.
If you want to know more about this decision, take a look at #1503.

In this pr, we are removing any line of code that handled our middlewares, the request lifecycle has been updated, same for test and documentation.
@Ethan-Arrowood would you like to work on the type definitions?

All the tests that I have removed from Fastify core will be added to fastify-express and middie, so we can be sure that the behavior of the API will be the same.

Closes: #1503

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

- removed .use api
- removed middie
- removed middlewares from request lifecycle
- removed middlewares from encapsulation handling
- removed kMiddlewares symbol
@delvedor delvedor added semver-major Issue or PR that should land as semver major v3.x Issue or pr related to Fastify v3 labels Dec 28, 2019
@delvedor delvedor added this to the v3.0.0 milestone Dec 28, 2019
@delvedor delvedor requested a review from a team December 28, 2019 13:58
Copy link
Copy Markdown
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.

We need to prepare a fastify-middleware implementation (or maybe merge it in middie) and benchmark it. I would expect its overhead to be comparable with our current internal implementation, but it's better to verify.

Note that https://github.com/fastify/fastify-helmet will depend on it, and we cannot easily migrate out of it for some of the compatibility layer.

I think we should add a basic use() method that throw an exception that links to the middie or fastify-express.

Comment thread README.md
- By default .use will throw an error
- Decorators can override the default .use
- Added new error code
Copy link
Copy Markdown
Member Author

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

We need to prepare a fastify-middleware implementation (or maybe merge it in middie) and benchmark it. I would expect its overhead to be comparable with our current internal implementation, but it's better to verify.

I agree, I'll send a pr to middie and transform it into a plugin.

Note that fastify/fastify-helmet will depend on it, and we cannot easily migrate out of it for some of the compatibility layer.
I think we should add a basic use() method that throw an exception that links to the middie or fastify-express.

Good idea, I've added back the use method, which throws as soon as you use it. Furthermore, the decorator API will not complain if you try to override use.

@delvedor delvedor mentioned this pull request Dec 29, 2019
4 tasks
@Ethan-Arrowood
Copy link
Copy Markdown
Member

Yes I can update the type defs for this!

@Ethan-Arrowood Ethan-Arrowood self-assigned this Jan 5, 2020
@delvedor delvedor self-assigned this Jan 6, 2020
Copy link
Copy Markdown
Member Author

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

I did run some benchmarks, there is no noticeable difference between Fastify v2 and Fastify v3 + middie, which is what we were expecting.
Fastify v2 is slightly slower than Fastify v3 (without middlewares), but performances are not the reason why we are removing core middlewares support.

@Ethan-Arrowood
Copy link
Copy Markdown
Member

Ethan-Arrowood commented Jan 7, 2020

As far as I can tell the only TS modification needed was in the use method.

All of the current hook handlers types currently return an instance of FastifyMiddleware. This seems like an issue with the type system. I think a lot of this stuff was mainly rolled over from the original types so we can take this opportunity to clean it up. What should be returned from every route level hook?

Whatever the answer is to that question will help me change the type system to better reflect the intended behavior here.

@delvedor
Copy link
Copy Markdown
Member Author

@Ethan-Arrowood I think we should get rid of the FastifyMiddleware definitions, as middlewares are not a Fastify concept.
Thus, I think we can delete types/middleware.d.ts.

Furthermore, I'm not sure what it's the meaning of the Raw* definitions eg:

fastify/types/hooks.d.ts

Lines 21 to 25 in 047442b

export type preValidationHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>
> = FastifyMiddleware<RawServer, RawRequest, RawReply>

Every hook gets the Fastify request/reply objects, and you can access the Node.js's request object via request.raw, same for the Node.js's response object via reply.raw. I fear this might be confusing. Probably we should open a separate issue for discussing this.

@Ethan-Arrowood
Copy link
Copy Markdown
Member

Ethan-Arrowood commented Jan 13, 2020

Thus, I think we can delete types/middleware.d.ts.

Sounds good. I'll start a branch to fix this up.

Furthermore, I'm not sure what it's the meaning of the Raw* definitions eg:

These are all generic parameters the type system uses to enforce the same Server/Request/Reply types throughout the type defined server. It makes the type system slightly more confusing, but the user experience is way improved because the user only needs to specify what it is way at the beginning.

I'll have something started as soon as possible

@delvedor delvedor requested a review from mcollina January 13, 2020 15:50
@mcollina
Copy link
Copy Markdown
Member

Code is ok. I would wait for a solution to avoid the after call.

@Ethan-Arrowood
Copy link
Copy Markdown
Member

@delvedor do Route level hooks return anything? I know the application hooks return void -- is this true for the other hooks?

@Ethan-Arrowood
Copy link
Copy Markdown
Member

After reading the hooks.js code I believe the answer is all hooks return void -- I'm going to move forward with this; let me know if I'm wrong!

@Ethan-Arrowood
Copy link
Copy Markdown
Member

@delvedor wanted to let you know types have finished being updated. I linked a draft PR if we want to merge separately. Otherwise happy to push that work to your branch if you'd like.

@delvedor
Copy link
Copy Markdown
Member Author

@Ethan-Arrowood we can directly merge in next once this pr is approved :)

@stale
Copy link
Copy Markdown

stale Bot commented Feb 3, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale Issue or pr with more than 15 days of inactivity. label Feb 3, 2020
@delvedor delvedor removed the stale Issue or pr with more than 15 days of inactivity. label Feb 3, 2020
@mcollina mcollina added the discussion Issues or PRs with this label will never stale label Feb 3, 2020
@delvedor delvedor marked this pull request as ready for review February 16, 2020 13:33
@delvedor delvedor requested a review from a team February 16, 2020 13:33
Copy link
Copy Markdown
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

Comment thread docs/Middleware.md Outdated
Comment thread docs/Middleware.md
@delvedor
Copy link
Copy Markdown
Member Author

@Eomm could you fix the conflicts? :)

Copy link
Copy Markdown
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

Comment thread fastify.js
Copy link
Copy Markdown
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

@delvedor delvedor merged commit 5d158f3 into next Mar 16, 2020
@delvedor delvedor deleted the remove-middlewares branch March 16, 2020 09:09
This was referenced Jul 5, 2020
@github-actions
Copy link
Copy Markdown

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 Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

discussion Issues or PRs with this label will never stale semver-major Issue or PR that should land as semver major v3.x Issue or pr related to Fastify v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants