Compress Response

Isn't it cool that browsers can natively decompress stuff? There are a couple of supported compression algorithms: gzip (used below), compress, deflate, br.

Learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding

If you don't add the content-encoding header, the gzip result looks like:

���������
���.���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������YDA��"��

Which is incredibly short for a 500kb string! (Which shouldn't be surprising because it's just "hi" 250k times)

Readme
1
2
3
4
5
6
7
8
9
10
import { gzip } from "npm:pako";
export default async function(req: Request): Promise<Response> {
return new Response(await gzip(JSON.stringify("hi".repeat(250_000))), {
headers: {
"Content-Encoding": "gzip",
"Content-Type": "application/json",
},
});
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.