Edge Functions

Handling Compressed Requests

Handling Gzip compressed requests.


To decompress Gzip bodies, you can use gunzipSync from the node:zlib API to decompress and then read the body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { } from 'node:zlib'.(async () => { try { // Check if the request body is gzip compressed const = ..('content-encoding') if ( !== 'gzip') { return new ('Request body is not gzip compressed', { : 400, }) } // Read the compressed body const = await .() // Decompress the body const = (new ()) // Convert the decompressed body to a string const = new ().() const = .() // Process the decompressed body as needed .(`Received: ${.()}`) return new ('ok', { : { 'Content-Type': 'text/plain' }, }) } catch () { .('Error:', ) return new ('Error processing request', { : 500 }) }})