Compresses string using gzip and native browser APIs

PHOTO EMBED

Thu Dec 16 2021 22:56:28 GMT+0000 (UTC)

Saved by @Explosion #javascript #compression

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);
  });
}
content_copyCOPY

https://gist.github.com/Explosion-Scratch/357c2eebd8254f8ea5548b0e6ac7a61b