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

refactor: replaced _implicitHeader #908

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

lamweili
Copy link

@lamweili lamweili commented Aug 2, 2022

Fixes #888

Replaced the usage of undocumented HTTP API with its implementation instead (bringing about some* http2 support).
There is no change to the existing behavior. This is similar to expressjs/compression#170.

The changes do not require a semver major.

some* - This change may/may not provide full http2 support.

@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch 4 times, most recently from d2392b6 to 12eb09c Compare August 2, 2022 09:38
@import-brain import-brain added the pr label Aug 2, 2022
Copy link
Contributor

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

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

I'm not sure what the purpose of the added test code is in relation to this change, as (1) reverting the changes in index.js doesn't affect the outcome of the tests and (2) looking at the test code it is just a http/2 server returning "Hello, world!" and asserting that that is the case...

test/session.js Outdated Show resolved Hide resolved
@dougwilson
Copy link
Contributor

Sweet. Also, I am going to sleep now, but meant to update here that I think I was using a Node.js version without http/2 earlier when I tested; I needed to get back and double-check that.

@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch 2 times, most recently from cf9cc21 to 4d16065 Compare August 3, 2022 02:06
@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch from cf9cc21 to 92d05cc Compare August 3, 2022 02:10
@lamweili
Copy link
Author

lamweili commented Aug 3, 2022

Ok, I have split the initial commit (d021958) into two separate commits (92d05cc, df7163e) for clarity.

  1. 92d05cc - added test case which fails if res._implicitHeader() is used (e.g. in http2)
    I added tests that actually require this change, such that we don't accidentally regress the behavior.
    The existing codes are untouched. Checking out to it, we will get a test failure on existing codes.

  2. df7163e - replaced res._implicitHeader() to address test case
    In the next commit, which replaced the res._implicitHeader(), the test will now pass. As we are using the implementation of res._implicitHeader(), semver major is not required.

  3. 416ae6f - removed explicit http2 headers for less verbosity (using default values)

I ran the GH actions on my forked repository and here are the results for all the commits:
https://github.com/lamweili/session/actions

@lamweili
Copy link
Author

lamweili commented Aug 3, 2022

I'm not sure if this change will provide full http2 support.

IMHO, changing from an undocumented API to a documented API is a move in the right direction.
As a by-product, it helps to remove a blocker in the http2 scope, as http2 does not have res._implicitHeader().

@dougwilson, do you think we should add a repeat of all the tests for http2?
Or should we put that aside for now and simply treat this PR as a refactor of undocumented API to documented API?

@lamweili lamweili requested a review from dougwilson August 3, 2022 02:31
@dougwilson
Copy link
Contributor

Yep, makes sense. I assume the line right above that should be changed as well, right? res._header is also an undocumented HTTP API, which seems to be the purpose of the PR based on the description. We really should just only use public APIs for sure 👍 . A lot of this is just kruft, haha

@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch from 6770cbf to 416ae6f Compare August 3, 2022 02:37
@lamweili
Copy link
Author

lamweili commented Aug 3, 2022

Yup, agreed.
For clarity, PR only fixes ONE undocumented API to documented API, which is res._implicitHeader(). 🤗

@dougwilson
Copy link
Contributor

Gotcha. Why spread it across a bunch of PRs? We can just fix the undocumented API usage at once. It's better for making a release, as making a release can cause folks to break. It is sad but true. I usually err on this being a semver major so we should get them all fixed at once.

@dougwilson
Copy link
Contributor

I'm not sure what that means. It is used in the line right above the one you changed in the PR.

if (!res._header) {

@lamweili
Copy link
Author

lamweili commented Aug 3, 2022

Ah, I don't seem to find any res._header.
I guess we got lucky?

Ah, my bad, I found it. Bad search I had previously.

I will address the res._header as well then. Any others to take note of?

if (!res._header) {

@dougwilson
Copy link
Contributor

I'm on mobile, so not sure. I just saw that one right above the line you changed when looking at the diff. I can help take a look over the code to find them tomorrow 👍

@lamweili

This comment was marked as off-topic.

@dougwilson

This comment was marked as off-topic.

@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch 3 times, most recently from 647bdd1 to 9e240a5 Compare August 3, 2022 12:16
@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch from e7b53e3 to 35880d6 Compare August 3, 2022 16:00
@lamweili lamweili force-pushed the refactor/use_writeHead_instead_of_implicitHeader branch from 7b8de6b to 36618af Compare August 4, 2022 02:38
@lamweili
Copy link
Author

lamweili commented Aug 4, 2022

Continuation of #908 (comment):

  1. 35880d6 - added tests to check for res._header and res.headersSent equivalence
    Strangely, while res.headersSent is only available from Node.js v0.9.4, all test cases (including the added one) passed.
    Perhaps the documentation is incorrect?

  2. 36618af - replaced res._header with res.headersSent if available (prevented regression so no need for semver major)

I ran the GH actions on my forked repository and here are the results for all the commits:
https://github.com/lamweili/session/actions

@lamweili
Copy link
Author

lamweili commented Oct 7, 2022

@dougwilson, have you got the time to take a look at this PR?

@Martin-Luther
Copy link

Martin-Luther commented Dec 10, 2022

I had the same issue : http2 + express-session and the latest version of express-session 1.17.3 did not help.
express-session is not the only module impacted: I saw people having the same issue with express-compression.

NB.: I am not using ExpressJS, but my framework allows me to use modules/plugins from ExpressJS

Option 1)
You don't have the time to wait for a fix, you can intercept your response and add a test inside your script right before calling res.end(...)

if (!res._implicitHeader) {
    // Hack required until `express-<plugin>` get support for http2 (express-session)
    res._implicitHeader = function(){ return; }; // Don't need, don't care
}
res.end();

This worked for me.

Option 2)
For those brave enough to fixe this issue for people like me.
Open express-session/index.js:275

You should have something like:

if (!res._header) {
    res._implicitHeader()
}

Needs to be replaced by:

if (!res._header && res._implicitHeader) {
    res._implicitHeader()
}

This will skip the call for people using http2 or when res._implicitHeader not defined.

Let me know if it worked for you guys.

@lamweili
Copy link
Author

lamweili commented Apr 22, 2023

@dougwilson Not sure if you have the time to have a look at this PR again?

@dougwilson dougwilson force-pushed the master branch 2 times, most recently from 9d2e29b to 408229e Compare January 28, 2024 20:24
@@ -272,8 +272,8 @@ function session(options) {
return ret;
}

if (!res._header) {
res._implicitHeader()
if (typeof res.headersSent === 'boolean' ? !res.headersSent : !res._header) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's better to make it a function like in compression expressjs/compression#129

@@ -1371,6 +1381,25 @@ describe('session()', function(){
})
});

describe('res._header patch', function () {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this test makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No need to call undocumented nodejs function ServerResponse._implicitHeader
5 participants