llrt icon indicating copy to clipboard operation
llrt copied to clipboard

response.text() does not support content-encoding

Open nabetti1720 opened this issue 1 year ago • 3 comments

When the following code is executed, the result of response.text() is garbled. The same result is obtained when accept-encoding is set to something else(such as br, deflate, zstd).

And although I have not tried it, I suspect that response.json() will produce the same result.

// reproduction.js
const main = async () => {
  try {
    const response = await fetch('https://google.com', {headers: {'accept-encoding': 'gzip'}});
    console.log(response.headers.get('content-encoding'));
    const responseText = await response.text();
    console.log('vvv');
    console.log(responseText.substring(0, 100));
    console.log('^^^');
  } catch(e) {
    console.log(e);
  }
}

main();

result of llrt:

shinya@MBA2022M2 llrt-test % ./llrt reproduction.js
gzip
vvv
// This part is actually garbled.
^^^

result of bun:

shinya@MBA2022M2 llrt-test % bun reproduction.js
gzip
vvv
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta content
^^^

nabetti1720 avatar May 02 '24 04:05 nabetti1720

This is a must have. We already heavily rely on zstd for reading LLRT bytecode format. Gzip and deflate (zip) decompression can be added using flate2 crate with zlib-ng backend. Compression & decompression are also requirements of WinterCG compression stream with said algorithms. For now (since streaming is not yet in place), decompression can be done when reading from response here: https://github.com/awslabs/llrt/blob/8fa91565bd7446eca010bb93aac336a0fe3ba0ac/llrt_core/src/modules/http/response.rs#L135

richarddavison avatar May 02 '24 05:05 richarddavison

I know that many sites return content-encoding as br when accept-encoding is "gzip, deflate, br, zstd", but do you plan to support brotli?

nabetti1720 avatar May 02 '24 07:05 nabetti1720

I know that many sites return content-encoding as br when accept-encoding is "gzip, deflate, br, zstd", but do you plan to support brotli?

Yes brotli as well

richarddavison avatar May 02 '24 07:05 richarddavison

After this implementation is complete, We also need to decide on the default accept-encoding.

For example,

  • In general: gzip, deflate, br, zstd
  • If you want to make a difference: zstd, br, gzip, deflate

Personally, I think that within the scope of WinterCG, challenging settings that lead to improved performance could be incorporated.

nabetti1720 avatar May 02 '24 22:05 nabetti1720

Hi @richarddavison, are you already working on this issue? If you haven't already, would you give me the opportunity to work on it?

nabetti1720 avatar May 04 '24 11:05 nabetti1720

Hi @richarddavison, are you already working on this issue? If you haven't already, would you give me the opportunity to work on it?

Hi @nabetti1720! I'm not, please go ahead 👌

richarddavison avatar May 04 '24 17:05 richarddavison

Landed in https://github.com/awslabs/llrt/pull/360

richarddavison avatar May 06 '24 19:05 richarddavison