koajs/compress

Not using the correct encoding

CareLuLu-Gabriel opened this issue · 2 comments

Expected Behavior

By sending Accept-Encoding: gzip, deflate the response should be encoded with either gzip or deflate.

Current Behavior

If there are any other previous requests that included other encodings, the response may be encoded with an unexpected algorithm.

Possible Solution

  1. Don't use a shared map encodingWeights by multiple requests.

Steps to Reproduce (for bugs)

  1. Send a request with Accept-Encoding: gzip, deflate, br
  2. The response will have Content-Encoding: br
  3. Send another request with Accept-Encoding: gzip, deflate
  4. The new response will also have Content-Encoding: br
const Koa = require('koa');
const compress = require('koa-compress');
const axios = require('axios');

const app = new Koa();
app.use(compress());

app.use((ctx) => {
  ctx.body = {...}; // long json
});

app.listen(3000);

async function test() {
  const res1 = await axios.post('http://localhost:3000', {}, {
    headers: { 'Accept-Encoding': 'gzip, deflate, br' },
  });
  console.log(res1.headers['content-encoding']); // br

  const res2 = await axios.post('http://localhost:3000', {}, {
    headers: { 'Accept-Encoding': 'gzip, deflate' },
  });
  console.log(res2.headers['content-encoding']); // br
}
setTimeout(test, 1000);

Context

If the encoding doesn't use one of the expected algorithms, it will break the clients

Your Environment

  • Server: Node 14.2.0, koa-compress 4.0.0, koa 2.11.0
  • Client: Node 14.2.0, axios 0.19.2

this should be fixed in 4.0.1. can you upgrade?

@jonathanong I upgraded and the issue was solved, thank you!