-
-
Notifications
You must be signed in to change notification settings - Fork 363
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
refactor: use native req/res methods #747
Conversation
Codecov Report
@@ Coverage Diff @@
## master #747 +/- ##
==========================================
+ Coverage 99.15% 99.16% +0.01%
==========================================
Files 10 10
Lines 236 239 +3
Branches 72 73 +1
==========================================
+ Hits 234 237 +3
Misses 2 2
Continue to review full report at Codecov.
|
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.
We need to setup tests for connect in this case, we can't just change code because something use it, it can breaking other compatibles
I can help you with it, should be not hard |
Note this is a revert of #726. |
Yes, so I ask to add tests and we should create |
I can wrap the entire What do you think of just swapping Since |
Bad idea, we should we should test on what it works on
yes, but we need to add it for all tests, otherwise we can't guarantee compatibility |
How would you like to add tests for both? (eg. should I wrap the entire middleware tests so they run for both express and connect?) Also, supporting connect is effectively supporting a vanilla Node.js HTTP server as well, if you're interested in adding tests for that as well. |
Ideally - yes
Yes 👍 |
I added an outer iteration in the middleware test so that they're executed for I couldn't find out what was causing this. Do you mind looking into this? Also looked into adding a vanilla HTTP server, but I may have over-spoke. Adding middleware support wasn't as easy as I imagined... the whole BTW I still think it's kinda pointless to test for express if we're testing with connect, and we're not using any express features. At most, I think just add a smaller express smoke test. Maybe you'll agree as you checkout the code. |
webpack-dev-server uses express for the framework. |
Thanks for chiming in @ylemkimon This PR removes dependency on express-only API so that it can be HTTP framework agnostic. Instead of using the express API, this middleware now only uses native Node.js HTTP API. Therefore, this middleware now works with any HTTP framework that expose native request/response instances because we're no longer interacting with any framework code. The only dependency now is the "HTTP middleware paradigm". You can confirm this by swapping
So, testing with To support this even further, just take a look at one of the most popular express middleware If we want first-class support for |
res.setHeader( | ||
'Content-Type', | ||
contentType || 'application/octet-stream' | ||
); |
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.
We should always use official middleware API, so for example we shoudl use express
API, for connect (no example API) we should use http module API, why? Because other middlewares/tools can add additional logic for built-in API, and we will not working properly with them
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.
So we need to create compatibility
file and put some utils there, like:
function responseGetHeader(response, header) {
// Comment there we describe what it function belong express API and some more interesting stuff
if (response.get(header)) {
// Logic
}
// Other logic
}
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.
I agree with your explanation, and that's why this middleware can't be used by Koa, even though they expose native request/response.
However, if we scope our support level to vanilla HTTP, express, connect, and other frameworks that don't add extra logic, then we don't need to do that. We can't possibly support all frameworks anyway.
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.
So let's take my suggestion, it is easy and will not confusing for future refactoring
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.
If the support level is strictly express and connect, I think it's unnecessary.
Going back to the case with compression
, I'm not convinced why a third-party middleware requires this abstraction when a first-party middleware doesn't.
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.
Can you clarify?
Thanks for this pr, look forward to the release. |
Following up with #747 (comment) I couldn't find out what was causing this. Do you mind looking into this? |
Okay, I am start to working on it, want to separate it on two commits - prepare tests + compatibility fixes to avoid dirty git history |
Fixed #760 @privatenumber big thanks for helping and investigating, I will do release tomorrow, the next target - |
Thanks for getting it in @evilebottnawi. Looking forward to being able to use it! Not a big deal because I primarily did this for the feature, but for future reference, I would appreciate it if my commits could've somehow been integrated into your PR and the repository. I put in a lot of hours into this but I won't be credited as a contributor. |
This PR contains a:
Motivation / Use-Case
The code was using express specific methods for very simple things (eg. getting/setting header).
I use connect to keep my server light-weight, and others may be using frameworks like koa.
This change refactors the express method calls to use native Node.js HTTP request/response methods instead.
Breaking Changes
Additional Info