awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
7.73k stars 342 forks source link

feat: Implementation of decompression in fetch() #360

Closed nabetti1720 closed 1 month ago

nabetti1720 commented 1 month ago

Issue #355 (if available)

Description of changes

Execution Sample

content-encoding.js:

const main = async () => {
  try {
    const response = await fetch('https://www.meta.com/', {headers: {"accept-encoding": process.argv[2]}});
    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();
  1. accept-encoding: zstd
    shinya@MBA2022M2 llrt-test % ./llrt content-encoding.js zstd   
    zstd
    vvv
    <!DOCTYPE html><html id="facebook" class="_a4ch _aber" lang="ja" dir="ltr"><head><link data-default-
    ^^^
  2. accept-encoding: br
    shinya@MBA2022M2 llrt-test % ./llrt content-encoding.js br  
    br
    vvv
    <!DOCTYPE html><html id="facebook" class="_a4ch _aber" lang="ja" dir="ltr"><head><link data-default-
    ^^^
  3. accept-encoding: gzip
    shinya@MBA2022M2 llrt-test % ./llrt content-encoding.js gzip
    gzip
    vvv
    <!DOCTYPE html><html id="facebook" class="_a4ch _aber" lang="ja" dir="ltr"><head><link data-default-
    ^^^
  4. accept-encoding: deflate
    shinya@MBA2022M2 llrt-test % ./llrt content-encoding.js deflate
    undefined       // NOTE: This website does not support deflate.
    vvv
    <!DOCTYPE html><html id="facebook" class="_a4ch _aber" lang="ja" dir="ltr"><head><link data-default-
    ^^^
  5. accept-encoding: gzip, deflate, br, zstd
    shinya@MBA2022M2 llrt-test % ./llrt content-encoding.js "gzip, deflate, br, zstd"
    zstd            // NOTE: The encoding is determined on the server side.
    vvv
    <!DOCTYPE html><html id="facebook" class="_a4ch _aber" lang="ja" dir="ltr"><head><link data-default-
    ^^^

ToDo

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

nabetti1720 commented 1 month ago

I suggest to encode (or hardcode) gzip, deflate, br and zstd "hello world" bytes and read the response as plain texts. Ideally test in Rust and not JS so we can mock the http responses.

Yes, I added the test that way. One of the remaining ToDo, checking deflate decompression on the actual website, can be substituted for this test if it seems difficult.

The remaining issue is the default accept-encoding and pre-allocating vectors.

richarddavison commented 1 month ago

Yes, I added the test that way. One of the remaining ToDo, checking deflate decompression on the actual website, can be substituted for this test if it seems difficult.

It's not ideal to have CI depend on external resources. These tests should suffice.

nabetti1720 commented 1 month ago

It's not ideal to have CI depend on external resources. These tests should suffice.

Sorry for the misunderstanding. We did not intend to incorporate access to external resources into the CI test, we just wanted to see how it worked on a real website. :)

richarddavison commented 1 month ago

@nabetti1720 please rebase from main. I have a minor changed CI

nabetti1720 commented 1 month ago

@nabetti1720 please rebase from main. I have a minor changed CI

I ran it as soon as possible.

nabetti1720 commented 1 month ago

Hi @richarddavison, sorry for the delay. All issues were addressed.