Skip to content

Instantly share code, notes, and snippets.

@jimmywarting
Created February 18, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmywarting/5085682dc310ba34a42af19a4d735bfb to your computer and use it in GitHub Desktop.
Save jimmywarting/5085682dc310ba34a42af19a4d735bfb to your computer and use it in GitHub Desktop.
native (de)compress in browser
import { compress, decompress } from 'zlib.js'
compr = await compress('hhhhheeeej'.repeat(200)).blob()
blob = await decompress(compr).blob()
const r = d => new Response(d)
// accepts all kind of data response can handle
// and returns a new Response from witch you can call blob, arraybuffer, json, text and .body (stream)
// optional type is 'deflate'
export default {
compress: (d, t = 'gzip') => r(r(d).body.pipeThrough(new CompressionStream(t))),
decompress: (d, t = 'gzip') => r(r(d).body.pipeThrough(new DecompressionStream(t)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment