Something went wrong. Try again.
A Tangled knot on CF Workers, backed by CF Artifacts
Something went wrong. Try again.
1.9 kB · 58 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import { GitTransport } from './git-transport';import { validatePublicSourceUrl, type LfsFetch } from './lfs-source';import { fail } from './types';
/** Public HTTPS read-only smart Git adapter. Never attaches Artifacts credentials. */export function publicGitSource( source: string, fetcher: LfsFetch = (input, init) => fetch(input, init),) { const store = { async forward( _artifact: string, path: string, request: Request, scope: 'read' | 'write', ) { if ( scope !== 'read' || !['/info/refs?service=git-upload-pack', '/git-upload-pack'].includes( path, ) ) fail(400, 'UnsafeGitSource', 'source adapter is read-only'); const base = await validatePublicSourceUrl(source, fetcher), url = new URL(base.href); url.pathname = url.pathname.replace(/\/$/, '') + path.split('?')[0]; url.search = path.includes('?') ? '?' + path.split('?')[1] : ''; const headers = new Headers({ 'git-protocol': 'version=0', 'accept-encoding': 'identity', }); if (request.method === 'POST') headers.set('content-type', 'application/x-git-upload-pack-request'); const response = await fetcher(url, { method: request.method, headers, body: request.method === 'POST' ? await request.arrayBuffer() : undefined, redirect: 'manual', signal: AbortSignal.timeout(120000), }); if ( (response.status >= 300 && response.status < 400) || (response.headers.has('content-encoding') && response.headers.get('content-encoding') !== 'identity') ) { await response.body?.cancel(); fail( 502, 'UnsafeGitSource', 'Git source redirect or compressed response is unsupported', ); } return response; }, }; return new GitTransport(store, 'public-source');}