Skip to content

Instantly share code, notes, and snippets.

addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function timeout (ms) {
return new Promise(accept => {
setTimeout(() => {
accept('timeout')
}, ms)
})

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'
@shagamemnon
shagamemnon / cloudflare-enable-logs.sh
Created January 21, 2020 19:30
Enable log file retention for a list of Cloudflare (Enterprise) zones.
#!/bin/sh
# Enable log retention for a list of Cloudflare zones
AUTH_EMAIL="xxxx@example.com" # the account you use to
AUTH_KEY="xxxxxxxxxxxx" # https://dash.cloudflare.com/profile/api-tokens --> "Global API Key"
ZONE_NAMES="franktaylor.io cfiq.io valleynet.io" # space-separated list of zone names
for ZONE in $ZONE_NAMES
do
@shagamemnon
shagamemnon / getZones.sh
Last active January 17, 2020 10:26 — forked from cjus/jsonval.sh
Extract a JSON value from a BASH script
#!/bin/bash
function jsonval {
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
}
json=`curl -s -X GET https://api.cloudflare.com/client/v4/zones?account.name=$ACCT_NAME&page=1&per_page=1000&order=status&direction=desc&match=all`
prop='profile_image_url'
picurl=`jsonval`
@shagamemnon
shagamemnon / stick-session.js
Last active September 16, 2019 05:17
A Cloudflare Worker for session persistence
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
event.passThroughOnException()
})
const ORIGINS = [
'storage.googleapis.com',
'frank-io.herokuapp.com'
]
addEventListener('fetch', e => e.respondWith(handleRequest(e.request)))
async function handleRequest(request) {
let url = new URL(request.url)
let params = new URLSearchParams(url.search.slice(1))
let features = {
minify: ['html', 'css', 'js'],
polish: ['lossy', 'lossless', 'off']
}
addEventListener('fetch', event => event.respondWith(handleRequest(event)))
async function cacheOnRequest (requestOrUrl, { headers, options }) {
let request = new Request(requestOrUrl)
/* You can pass a JSON object or a new Headers instance - both are accepted by
* the Service Workers spec
*/
if (headers && headers.req) {
request.headers = new Headers(request.headers)
@shagamemnon
shagamemnon / install_node.sh
Last active July 13, 2018 03:05 — forked from carlosvillu/install_node.sh
Configure NodeJs + NPM + NGINX
#!/bin/bash
NODE_VERSION=0.4.10
NPM_VERSION=1.0.22
sudo apt-get update
sudo apt-get install -y build-essential git-core nginx libssl-dev pkg-config curl
# Install node
mkdir -p $HOME/local/node
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

Keybase proof

I hereby claim:

  • I am shagamemnon on github.
  • I am shagamemnon (https://keybase.io/shagamemnon) on keybase.
  • I have a public key ASDQzBR4cnFTxuE3KHiuTyfTRkepkGL4daWVylWV578q-go

To claim this, I am signing this object: