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

fix: correct brotli options format #165

Merged
merged 3 commits into from
Jan 26, 2023
Merged

Conversation

patrickhulce
Copy link
Contributor

3d205cd didn't actually fix the default compression issue described in #121 because the zlib format expects quality to be inside an option called params

Example Test Sever

const Koa = require('koa')
const {Stream} = require('stream')
const app = (module.exports = new Koa())
const compress = require('koa-compress')

app.use(async (ctx, next) => {
  await next()

  if (ctx.body instanceof Stream) {
    console.time('request')
    ctx.body.on('end', () => console.timeEnd('request'))
  }
})

app.use(compress())

app.use(async function(ctx) {
  ctx.compress = true
  ctx.body = require('crypto').randomBytes(10_000_000)
})

if (!module.parent) app.listen(3000, () => console.log('Server is up!'))

Before

$ node server &
Server is up!
$ curl http://localhost:3000 -H 'Accept-Encoding: br' > /dev/null
request: 8.398s
request: 8.354s

After

$ node server &
Server is up!
$ curl http://localhost:3000 -H 'Accept-Encoding: br' > /dev/null
request: 90.855ms
request: 71.504ms

3d205cd didn't actually fix the default compression issue described in koajs#121 because the zlib format expects quality to be inside [an option called `params`](https://nodejs.org/api/zlib.html#class-brotlioptions)
@codecov
Copy link

codecov bot commented Dec 16, 2021

Codecov Report

Merging #165 (fc8fca0) into master (5e04339) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #165   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines           88        88           
  Branches        32        32           
=========================================
  Hits            88        88           
Impacted Files Coverage Δ
lib/encodings.js 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e04339...fc8fca0. Read the comment docs.

@@ -89,7 +89,9 @@ Encodings.encodingMethodDefaultOptions = {
gzip: {},
deflate: {},
br: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
params: {

Choose a reason for hiding this comment

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

With this change,

compress/lib/index.js

Lines 41 to 42 in fc8fca0

...Encodings.encodingMethodDefaultOptions[encoding],
...(options[encoding] || {})
may need to change too.


As the PR is now, options['br'] will shallow merge, so if a consumer provides

koaCompress({
  br: {
    params: {
      [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT
    },
  },
});

I think the final object sent to zlib.createBrotliCompress ends up being

{
  params: {
    [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
    // the koa-compress default of [zlib.constants.BROTLI_PARAM_QUALITY]: 4 is no longer passed in
  },
}

It can go either way but users might expect the brotli options to deep merge here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the final object sent to zlib.createBrotliCompress ends up being

Correct.

users might expect the brotli options to deep merge here

True, I'll happily defer to maintainers here on what the koa community expects.

@patrickhulce
Copy link
Contributor Author

@jonathanong friendly ping on this :)

@titanism
Copy link

We should prob also set [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,

@thynson
Copy link

thynson commented Mar 18, 2023

When there'll be a new release ship this fix?

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.

4 participants