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 default option #191

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

Conversation

bjohansebas
Copy link
Member

@bjohansebas bjohansebas commented Sep 27, 2024

A new option is added to determine the default behavior when Accept-Encoding is not sent by the client, defaulting to identity.

closes #83

@bjohansebas bjohansebas marked this pull request as ready for review September 28, 2024 00:30
@bjohansebas bjohansebas changed the title [WIP]: feat: add default option feat: add default option Sep 28, 2024
@bjohansebas
Copy link
Member Author

Hi @IamLizu, could you review this PR?

@IamLizu
Copy link
Member

IamLizu commented Oct 3, 2024

Hey @bjohansebas 👋

Sure, I will read the relevant issues and then review this PR 👍

index.js Show resolved Hide resolved
@bjohansebas bjohansebas self-assigned this Oct 15, 2024
@bjohansebas bjohansebas marked this pull request as draft November 3, 2024 19:58
@bjohansebas bjohansebas marked this pull request as ready for review November 3, 2024 20:05
@bjohansebas bjohansebas added this to the 1.8.0 milestone Nov 3, 2024
Copy link
Member

@IamLizu IamLizu left a comment

Choose a reason for hiding this comment

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

Hi @bjohansebas 👋

I'm generally 👍 of the concept of introducing an enforcement mechanism, as discussed in #83.

However, would you mind renaming the variable defaultEnconfig to something that doesn't conflict with zlib.createGzip or zlib.createDeflate? It seems this conflict may have led to the addition of the following block:

if (opts.defaultEncoding) {
    opts.defaultEncoding = undefined
}

I suggest enforceEncoding, more on this below. Renaming the variable would help keep the code clean, and it’s important that we avoid such conflicts.

Thanks so much for your understanding and effort!

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@bjohansebas
Copy link
Member Author

@IamLizu Good catch

Copy link
Member

@IamLizu IamLizu left a comment

Choose a reason for hiding this comment

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

LGTM 👍

There's something I should address. As we know, content negotiation is handled by negotiator here, and it is indeed negotiator's responsibility to choose the best method.

Maybe we are interfering with negotiator's responsibilities?

cc: @expressjs/express-tc

@bjohansebas
Copy link
Member Author

@UlisesGascon ping

index.js Outdated
@@ -177,6 +180,11 @@ function compression (options) {
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

// if no method is found, use the default encoding
if (encodingSupported.indexOf(enforceEncoding) !== -1 && !req.headers['accept-encoding']) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you change the order of this? We should not check if the option is valid unless we are going to use 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

@@ -177,6 +180,11 @@ function compression (options) {
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

// if no method is found, use the default encoding
if (encodingSupported.indexOf(enforceEncoding) !== -1 && !req.headers['accept-encoding']) {
method = enforceEncoding === '*' ? 'gzip' : enforceEncoding
Copy link
Member

Choose a reason for hiding this comment

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

I dont see * referenced anywhere but here. Can you describe why this is necessary when you could drop this check and just have folks pass gzip directly for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

If * is not checked and changed, the deflate encoding would be applied as it’s the last one in the check (

compression/index.js

Lines 196 to 198 in 66c0cb7

stream = method === 'gzip'
? zlib.createGzip(opts)
: zlib.createDeflate(opts)
), and deflate is not the encoding we want to be applied, it should be gzip when it’s *.

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

Successfully merging this pull request may close these issues.

Force compression to client don't work
3 participants