Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Find content-length of gzip'ed fetch() response

I'm doing a large fetch() and I want to have a progress bar. When I return uncompressed content from the webserver, the following code works fine.

When I enable gzip on the webserver, the content-length reflects the compressed size (which is correct given the HTTP spec) and the stream that I'm reading gives me uncompressed data (which is correct according to the Fetch spec)... however the combination of these two correct behaviours means that my progress bar is useless (it reports "300% complete: loaded 1200KB [of uncompressed data] from a total of 400KB [of compressed data]")

Is there any way to either measure "how much compressed data has been downloaded so far" (to compare against the content-length header) or "how long is the uncompressed data expected to be" (to compare against the uncompressed stream I'm reading)?

    fetch("http://example.com/bigdata.json")
        .then((response) => {
            if (!response.body) return;
            const reader = response.body.getReader();
            let download_done = 0;
            let download_size = parseInt(response.headers.get("content-length"));

            return new ReadableStream({
                start(controller) {
                    function push() {
                        reader.read().then(({ done, value }) => {
                            if (done) {
                                controller.close();
                                return;
                            }
                            if (value) {
                                download_done += value.byteLength;
                                report_progress(download_done, download_size);
                            }
                            controller.enqueue(value);
                            push();
                        });
                    }
                    push();
                },
            });
        })
        .then((stream) => {
            return new Response(stream, {
                headers: { "Content-Type": "text/json" },
            }).json();
        })
        .then(function (result) {
            console.groupCollapsed("api_request(bigdata.json)");
            console.log(result);
            console.groupEnd();
        })
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!