How to get page with worker in gzip format?

Can somebody help?
7 Replies
rdutton
rdutton6d ago
If you mean fetch a URL and return the result as a gzipped file you could do something like: const uri = "https://example.com"; const filename = "example.com.html.gz"; const response = await fetch(uri); return new Response(response.body.pipeThrough(new CompressionStream("gzip")), { headers: { "Content-Type": "application/x-gzip", "Content-Encoding": "gzip", "Content-Disposition": attachment; filename="+encodeURIComponent(filename)+", }, }, response);
Vladimir_dev
Vladimir_devOP4d ago
Thanks, but I need fetch gzip from site, do some work and send a https
rdutton
rdutton4d ago
Then you could do the reverse, capture the gzip resource as a stream and then have access to the 'ungzipped' data piping it through a DecompressionStream. You would need to elaborate on 'do some work' before I could suggest any actual code. If it's gzipped text, then it could be fairly simple.
Vladimir_dev
Vladimir_devOP4d ago
The site gives json in gzip format. I need to fetch the gzip from the site, then I modify json inside worker and then send to another service. I just don't know if it is possible to automatically unzip the code
rdutton
rdutton4d ago
This should do what you described: try { // fetch the gzipped json file const response = await fetch('https://somerandomdomain.com/test.json.gz'); // decompress the response and obtain the data const stream = response.body.pipeThrough(new DecompressionStream('gzip')); const data = await new Response(stream).json(); // do some work with the data const new_data = {"abc": "def", "original": data}; return new Response(JSON.stringify(new_data), { status: 200 }); } catch (error) { return new Response(error.message, { status: 500 }); }
Vladimir_dev
Vladimir_devOP4d ago
Hi. Thank you so much. I've already figured it out myself. Reqwest crate handles gzip in wasm fine.
rdutton
rdutton4d ago
Cool
Want results from more Discord servers?
Add your server