Created
November 1, 2021 18:51
-
-
Save Explosion-Scratch/357c2eebd8254f8ea5548b0e6ac7a61b to your computer and use it in GitHub Desktop.
Compress string using gzip and native browser APIs
This file contains 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
function compress(string, encoding) { | |
const byteArray = new TextEncoder().encode(string); | |
const cs = new CompressionStream(encoding); | |
const writer = cs.writable.getWriter(); | |
writer.write(byteArray); | |
writer.close(); | |
return new Response(cs.readable).arrayBuffer(); | |
} | |
function decompress(byteArray, encoding) { | |
const cs = new DecompressionStream(encoding); | |
const writer = cs.writable.getWriter(); | |
writer.write(byteArray); | |
writer.close(); | |
return new Response(cs.readable).arrayBuffer().then(function (arrayBuffer) { | |
return new TextDecoder().decode(arrayBuffer); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, works:
Thank you for the help and Explosion-Scratch for writing it.