Skip to content

Instantly share code, notes, and snippets.

@longern
Created June 10, 2022 05:41
Show Gist options
  • Save longern/2582924a676079c33b1077c8c2d97c0c to your computer and use it in GitHub Desktop.
Save longern/2582924a676079c33b1077c8c2d97c0c to your computer and use it in GitHub Desktop.
IPFS Cloudflare worker proxy
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