Created
August 28, 2020 08:40
-
-
Save juzi5201314/cf8299977ad6dace9662325d8a37c858 to your computer and use it in GitHub Desktop.
crates.io mirror on cloudflare workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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