Created
June 22, 2023 03:34
-
-
Save NV/6222a34e5c09d7ce2d7c8e09b8e61fc4 to your computer and use it in GitHub Desktop.
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
(async function() { | |
let compressed_array_buffer = await compress("Hello"); | |
console.info("compressed_array_buffer", compressed_array_buffer); | |
let text_decoder = new TextDecoder("utf-8", {fatal: true}); | |
// TypeError: Failed to execute 'decode' on 'TextDecoder': The encoded data was not valid. | |
let compressed_text = text_decoder.decode(compressed_array_buffer); | |
console.info("GZIP", compressed_text); | |
// Save compressed_text... | |
let encoded_chunks = new TextEncoder("utf-8", {fatal: true}).encode(compressed_text); | |
let uncompressed = await decompress(encoded_chunks.buffer); | |
console.info("uncompressed", uncompressed); | |
})(); | |
function compress(string) { | |
const byteArray = new TextEncoder("utf-8", {fatal: true}).encode(string); | |
const cs = new CompressionStream("gzip"); | |
const writer = cs.writable.getWriter(); | |
writer.write(byteArray); | |
writer.close(); | |
return new Response(cs.readable).arrayBuffer(); | |
} | |
function decompress(byteArray) { | |
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("utf-8", {fatal: true}).decode(arrayBuffer); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment