Skip to content

Instantly share code, notes, and snippets.

@juzi5201314
Created August 28, 2020 08:40
Show Gist options
  • Select an option

  • Save juzi5201314/cf8299977ad6dace9662325d8a37c858 to your computer and use it in GitHub Desktop.

Select an option

Save juzi5201314/cf8299977ad6dace9662325d8a37c858 to your computer and use it in GitHub Desktop.
crates.io mirror on cloudflare workers
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const failback_source = "crates.io"
/**
* @param {Request} request
*/
async function handleRequest(request) {
const url = new URL(request.url);
const source = url.searchParams.get("source") ?? failback_source
if (url.protocol === 'http:') {
url.protocol = 'https:'
return Response.redirect(url.href);
}
url.host = source
const resp = await fetch(url.href, {
method: request.method,
headers: request.headers
})
return resp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment