Last active
May 30, 2025 21:06
-
-
Save tomlarkworthy/cf1d4ceabeabdb6d1628575ab3a83acf to your computer and use it in GitHub Desktop.
isomorphic git proxy 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', e => e.respondWith(handle(e.request))) | |
const ok = (req,u)=>{ | |
const q=u.searchParams | |
return req.method==='OPTIONS'|| | |
(req.method==='GET' && u.pathname.endsWith('/info/refs') && | |
['git-upload-pack','git-receive-pack'].includes(q.get('service')))|| | |
(req.method==='POST'&&u.pathname.endsWith('git-upload-pack') && | |
req.headers.get('content-type')==='application/x-git-upload-pack-request')|| | |
(req.method==='POST'&&u.pathname.endsWith('git-receive-pack') && | |
req.headers.get('content-type')==='application/x-git-receive-pack-request') | |
} | |
async function handle(req){ | |
const src=new URL(req.url) | |
if(!ok(req,src)) return new Response('Forbidden',{status:403}) | |
const target='https://'+src.pathname.slice(1)+src.search // drop leading “/” | |
if(req.method==='OPTIONS') | |
return new Response(null,{status:200,headers:cors(req)}) | |
const resp=await fetch(target,{ | |
method:req.method, | |
headers:strip(req.headers), | |
body:req.body, | |
redirect:'follow' | |
}) | |
return new Response(resp.body,{ | |
status:resp.status, | |
headers:merge(resp.headers,cors(req)) | |
}) | |
} | |
const cors=req=>{ | |
const hdr=req.headers | |
return { | |
'Access-Control-Allow-Origin': hdr.get('Origin')||'*', | |
'Access-Control-Allow-Methods':'GET,POST,OPTIONS', | |
'Access-Control-Allow-Headers': hdr.get('Access-Control-Request-Headers')||'*', | |
'Vary':'Origin' | |
} | |
} | |
const strip=h=>{ | |
const out=new Headers(h) | |
;['host','origin','referer','content-length'].forEach(k=>out.delete(k)) | |
return out | |
} | |
const merge=(h,x)=>{const o=new Headers(h);for(const[k,v]of Object.entries(x))o.set(k,v);return o} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
proxy for isomorphic git which is MUCH FASTER than the default one, and using it avoid sending your git credentials to a third party. CloudFlare workers are scale to zere and globally distributed, making it a great choice for a low traffic but demanding workload.
Used for
https://observablehq.com/@tomlarkworthy/jumpgate
I just pasted the code in the Cloudflare Workers console, ultra quick and simple to setup.
short video walkthrough https://www.youtube.com/watch?v=Nrc4xHZx80E (1m20)