Created
April 7, 2025 15:15
-
-
Save tomasdev/7992d069336926c08cccb250b7ce96a9 to your computer and use it in GitHub Desktop.
Gzip compress using native browser libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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