Skip to content

Instantly share code, notes, and snippets.

@tomlarkworthy
Last active January 3, 2026 19:01
Show Gist options
  • Select an option

  • Save tomlarkworthy/cf1d4ceabeabdb6d1628575ab3a83acf to your computer and use it in GitHub Desktop.

Select an option

Save tomlarkworthy/cf1d4ceabeabdb6d1628575ab3a83acf to your computer and use it in GitHub Desktop.
isomorphic git proxy on Cloudflare Workers
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}
@tomlarkworthy
Copy link
Copy Markdown
Author

tomlarkworthy commented May 17, 2025

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment