Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
Last active March 1, 2020 08: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 shagamemnon/813df528c29b95e96f5e55e888fe2f35 to your computer and use it in GitHub Desktop.
Save shagamemnon/813df528c29b95e96f5e55e888fe2f35 to your computer and use it in GitHub Desktop.

You can unzip POST request bodies in Workers using the pako library.

Demo

# Download a gzipped file to send as the body of a POST request
# The Worker running on this route will decompress the file and
# echo the human-readable response. Sweet!
wget https://storage.franktaylor.io/d06cef5527f329e519553f649b3a76e219f2c9d6/admin.php.gz
curl "POST" "https://workers-unzip.cflr.workers.dev/" --data-binary @admin.php.gz -H 'Content-Type: application/gzip'

Worker

const pako = require('pako')

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest (request) {
  request = new Request(request)

  try {
    let clone = request.clone()
    clone = await clone.arrayBuffer()
    clone = pako.inflate(clone, { to: 'string' })
    return new Response(clone, {
      headers: { 'content-type': 'text/plain' }
    })
  } catch (err) {
    console.log(err)
    return new Response(err)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment