Created
June 10, 2022 05:41
-
-
Save longern/2582924a676079c33b1077c8c2d97c0c to your computer and use it in GitHub Desktop.
IPFS Cloudflare worker proxy
This file contains 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', function (event) { | |
event.respondWith(handleRequest(event.request)); | |
}); | |
async function handleRequest(request) { | |
// Only GET requests work with this proxy. | |
if (request.method !== 'GET') | |
return new Response(`Method ${request.method} not allowed.`, { | |
status: 405, | |
headers: { Allow: 'GET' }, | |
}); | |
const url = new URL(request.url); | |
const pathname = url.pathname.replace(/^\//, "") | |
if (!pathname) | |
return Response.redirect(UPLOAD_REDIRECT) | |
if (pathname.startsWith("k5")) | |
return fetch(`https://${IPFS_GATEWAY}/ipns/${pathname}`); | |
if (pathname.startsWith("Qm") && pathname.length === 46) | |
return fetch(`https://${IPFS_GATEWAY}/ipfs/${pathname}`); | |
return new Response("Object not found", { status: 404 }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment