Skip to content

Instantly share code, notes, and snippets.

@tomasdev
Created April 7, 2025 15:15
Show Gist options
  • Save tomasdev/7992d069336926c08cccb250b7ce96a9 to your computer and use it in GitHub Desktop.
Save tomasdev/7992d069336926c08cccb250b7ce96a9 to your computer and use it in GitHub Desktop.
Gzip compress using native browser libraries
export async function gzip(input: Uint8Array|string): Promise<Uint8Array> {
if (typeof input === 'string') {
const encoder = new TextEncoder();
input = encoder.encode(input);
}
const cs = new CompressionStream('gzip');
const buffer = new Response(cs.readable).arrayBuffer();
const writer = cs.writable.getWriter();
await writer.write(input);
await writer.close();
return new Uint8Array(await buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment