just-git 1.2.9 → 1.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,7 +28,7 @@ var ce=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(e,t)=>(
28
28
  `:`unpack error
29
29
  `));for(let o of e)o.ok?s.push(P(`ok ${o.name}
30
30
  `)):s.push(P(`ng ${o.name} ${o.error??"failed"}
31
- `));s.push(U());let n=W(...s);if(t){let o=[];return o.push(Ue(1,n)),o.push(U()),W(...o)}return n}function Ue(r,e){let t=new Uint8Array(1+e.byteLength);return t[0]=r,t.set(e,1),P(t)}var ae=class{entries=new Map;currentBytes=0;maxBytes;hits=0;misses=0;constructor(e=256*1024*1024){this.maxBytes=e}static key(e,t,s){if(s.length>0)return null;let n=t.slice().sort();return`${e}\0${n.join(",")}`}get(e){let t=this.entries.get(e);return t?this.hits++:this.misses++,t}set(e,t){if(this.entries.has(e))return;let s=t.packData.byteLength;if(!(s>this.maxBytes)){for(;this.currentBytes+s>this.maxBytes&&this.entries.size>0;){let n=this.entries.keys().next().value;this.currentBytes-=this.entries.get(n).packData.byteLength,this.entries.delete(n)}this.entries.set(e,t),this.currentBytes+=s}}get stats(){return{entries:this.entries.size,bytes:this.currentBytes,hits:this.hits,misses:this.misses}}},Tr=["multi_ack_detailed","no-done","side-band-64k","ofs-delta","include-tag","allow-reachable-sha1-in-want","shallow"],Ur=["report-status","side-band-64k","ofs-delta","delete-refs"];async function Wt(r){let e=await r.refStore.listRefs("refs"),t=await r.refStore.readRef("HEAD"),s=[],n=null,o;if(t)if(t.type==="symbolic"){o=t.target;let i=await r.refStore.readRef(t.target);i?.type==="direct"&&(n=i.hash)}else n=t.hash;let a=e.slice().sort((i,f)=>i.name<f.name?-1:i.name>f.name?1:0);if(!n&&a.length>0){let i=Lr(a);i&&(n=i.hash,o=i.name)}n&&s.push({name:"HEAD",hash:n});for(let i of a)if(s.push({name:i.name,hash:i.hash}),i.name.startsWith("refs/tags/"))try{let f=await r.objectStore.read(i.hash);if(f.type==="tag"){let c=M(f.content);s.push({name:`${i.name}^{}`,hash:c.object})}}catch{}return{refs:s,headTarget:o}}var Cr=["refs/heads/main","refs/heads/master"];function Lr(r){for(let t of Cr){let s=r.find(n=>n.name===t);if(s)return s}return r.find(t=>t.name.startsWith("refs/heads/"))??null}function Mt(r,e,t){return Ht(r,e,e==="git-upload-pack"?Tr:Ur,t)}async function Gt(r,e,t){let{wants:s,haves:n,capabilities:o,clientShallows:a,depth:i,done:f}=Nt(e);if(s.length===0)return z(new Uint8Array(0),!1);let c=o.includes("multi_ack_detailed"),l=o.includes("side-band-64k"),d,p,u;if(a.length>0&&(u=new Set(a)),i!==void 0){let m=await kt(r,s,i,u??new Set);d=m,p=new Set(m.shallow)}if(d&&!f)return Bt(d);let h;if(c&&n.length>0){h=[];for(let m of n)await r.objectStore.exists(m)&&h.push(m);h.length===0&&(h=void 0)}let y=!p&&t?.cache&&t.cacheKey?ae.key(t.cacheKey,s,n):null;if(y&&t?.cache){let m=t.cache.get(y);if(m)return z(m.packData,l,h)}return t?.noDelta?Dr(r,s,n,o,l,h,d,p,u):Hr(r,s,n,o,l,h,t,y,d,p,u)}async function Dr(r,e,t,s,n,o,a,i,f){let{count:c,objects:l}=await Te(r,e,t,i,f);if(c===0){let{data:b}=await ue([]),x=z(b,n,o,a);return new ReadableStream({start(R){R.enqueue(x),R.close()}})}let d=[];for await(let b of l)d.push(b);let p=new Set(d.map(b=>b.hash)),u=[];if(s.includes("include-tag")){let b=await r.refStore.listRefs("refs/tags");for(let x of b)if(!p.has(x.hash))try{let R=await r.objectStore.read(x.hash);if(R.type==="tag"){let g=M(R.content);p.has(g.object)&&u.push({hash:x.hash,type:"tag",content:R.content})}}catch{}}let h=d.length+u.length;async function*y(){for(let b of d){let x=await r.objectStore.read(b.hash);yield{type:x.type,content:x.content}}for(let b of u)yield{type:b.type,content:b.content}}let m=wt(h,y()),O=_t(m,n,o,a);return new ReadableStream({async pull(b){let{value:x,done:R}=await O.next();R?b.close():b.enqueue(x)}})}async function Hr(r,e,t,s,n,o,a,i,f,c,l){let d=await St(r,e,t,c,l);if(d.count===0){let{data:b}=await ue([]);return z(b,n,o,f)}let p=await At(d),u=new Set(p.map(b=>b.hash));if(s.includes("include-tag")){let b=await r.refStore.listRefs("refs/tags");for(let x of b)if(!u.has(x.hash))try{let R=await r.objectStore.read(x.hash);if(R.type==="tag"){let g=M(R.content);u.has(g.object)&&(p.push({hash:x.hash,type:"tag",content:R.content}),u.add(x.hash))}}catch{}}let h=a?.deltaWindow?{window:a.deltaWindow}:void 0,y=It(p,h),m=y.map(b=>({hash:b.hash,type:b.type,content:b.content,delta:b.delta,deltaBaseHash:b.deltaBase})),{data:O}=await ue(m);if(i&&a?.cache){let b=y.filter(x=>x.delta).length;a.cache.set(i,{packData:O,objectCount:p.length,deltaCount:b})}return z(O,n,o,f)}async function Ft(r,e){let{commands:t,packData:s,capabilities:n,sawFlush:o}=$t(e),a=!0;if(s.byteLength>0)try{await r.objectStore.ingestPack(s)}catch{a=!1}let i=[];for(let f of t){let c=f.oldHash===me,l=f.newHash===me,d=!1;if(!c&&!l&&a)try{d=await Pt(r,f.oldHash,f.newHash)}catch{}i.push({ref:f.refName,oldHash:c?null:f.oldHash,newHash:f.newHash,isFF:d,isCreate:c,isDelete:l})}return{updates:i,unpackOk:a,capabilities:n,sawFlush:o}}function Nr(r){let{resolveRepo:e,hooks:t,basePath:s}=r,n=r.packCache===!1?void 0:new ae(r.packCache?.maxBytes),o=r.onError===!1?void 0:r.onError??(a=>{let i=a instanceof Error?a.message:String(a);console.error(`[server] Internal error: ${i}`)});return{async fetch(a){try{let i=new URL(a.url),f=decodeURIComponent(i.pathname);if(s){let c=s.replace(/\/+$/,"");if(!f.startsWith(c))return new Response("Not Found",{status:404});f=f.slice(c.length)}if(f.startsWith("/")||(f=`/${f}`),f.endsWith("/info/refs")&&a.method==="GET"){let c=i.searchParams.get("service");if(c!=="git-upload-pack"&&c!=="git-receive-pack")return new Response("Unsupported service",{status:403});let l=Ce(f,"/info/refs"),d=await e(l,a);if(d instanceof Response)return d;if(!d)return new Response("Not Found",{status:404});let p=d,{refs:u,headTarget:h}=await Wt(p),y=u;if(t?.advertiseRefs){let O=await t.advertiseRefs({repo:p,repoPath:l,refs:u,service:c,request:a});O&&(y=O)}let m=Mt(y,c,h);return new Response(m,{headers:{"Content-Type":`application/x-${c}-advertisement`,"Cache-Control":"no-cache"}})}if(f.endsWith("/git-upload-pack")&&a.method==="POST"){let c=Ce(f,"/git-upload-pack"),l=await e(c,a);if(l instanceof Response)return l;if(!l)return new Response("Not Found",{status:404});let d=l,p=await qt(a),u=await Gt(d,p,{cache:n,cacheKey:c,noDelta:r.packOptions?.noDelta,deltaWindow:r.packOptions?.deltaWindow});return new Response(u,{headers:{"Content-Type":"application/x-git-upload-pack-result"}})}if(f.endsWith("/git-receive-pack")&&a.method==="POST"){let c=Ce(f,"/git-receive-pack"),l=await e(c,a);if(l instanceof Response)return l;if(!l)return new Response("Not Found",{status:404});let d=l,p=await qt(a),{updates:u,unpackOk:h,capabilities:y,sawFlush:m}=await Ft(d,p);if(!m&&u.length===0)return new Response("Bad Request",{status:400});let O=y.includes("side-band-64k"),b=y.includes("report-status");if(!h){if(b){let g=u.map(w=>({name:w.ref,ok:!1,error:"unpack failed"}));return new Response(be(!1,g,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}if(t?.preReceive){let g=await t.preReceive({repo:d,repoPath:c,updates:u,request:a});if(Y(g)){if(b){let w=g.message??"pre-receive hook declined",j=u.map(v=>({name:v.ref,ok:!1,error:w}));return new Response(be(!0,j,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}}let x=[],R=[];for(let g of u){if(!g.isDelete&&!Rt(g.ref)){x.push({ref:g.ref,ok:!1,error:"invalid refname"});continue}if(t?.update){let w=await t.update({repo:d,repoPath:c,update:g,request:a});if(Y(w)){x.push({ref:g.ref,ok:!1,error:w.message??"update hook declined"});continue}}try{let w=g.isCreate?null:g.oldHash,j=g.isDelete?null:{type:"direct",hash:g.newHash};if(!await d.refStore.compareAndSwapRef(g.ref,w,j)){x.push({ref:g.ref,ok:!1,error:"failed to lock"});continue}x.push({ref:g.ref,ok:!0}),R.push(g)}catch(w){x.push({ref:g.ref,ok:!1,error:w instanceof Error?w.message:String(w)})}}if(t?.postReceive&&R.length>0)try{await t.postReceive({repo:d,repoPath:c,updates:R,request:a})}catch{}if(b){let g=x.map(w=>({name:w.ref,ok:w.ok,error:w.error}));return new Response(be(!0,g,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response("Not Found",{status:404})}catch(i){return o?.(i,a),new Response("Internal Server Error",{status:500})}}}}function Ce(r,e){let t=r.slice(0,-e.length);return t.startsWith("/")&&(t=t.slice(1)),t}async function qt(r){let e=new Uint8Array(await r.arrayBuffer()),t=r.headers.get("content-encoding");if(t==="gzip"||t==="x-gzip"){let s=new DecompressionStream("gzip"),n=s.writable.getWriter();return n.write(e),n.close(),new Uint8Array(await new Response(s.readable).arrayBuffer())}return e}function Br(r){return(e,t)=>{let s=[];e.on("data",n=>s.push(new Uint8Array(n))),e.on("error",()=>{t.writeHead(500),t.end("Internal Server Error")}),e.on("end",()=>{_r(r,e,s,t).catch(()=>{try{t.writeHead(500),t.end("Internal Server Error")}catch{}})})}}async function _r(r,e,t,s){let n=typeof e.headers.host=="string"?e.headers.host:"localhost",o=new URL(e.url??"/",`http://${n}`),a=new Headers;for(let[u,h]of Object.entries(e.headers))if(h!==void 0)if(Array.isArray(h))for(let y of h)a.append(u,y);else a.set(u,h);let i=e.method??"GET",f;if(i!=="GET"&&i!=="HEAD"){let u=0;for(let m of t)u+=m.byteLength;let h=new Uint8Array(u),y=0;for(let m of t)h.set(m,y),y+=m.byteLength;f=h}let c=new Request(o.href,{method:i,headers:a,body:f}),l=await r.fetch(c),d={};l.headers.forEach((u,h)=>{d[h]=u}),s.writeHead(l.status,d);let p=new Uint8Array(await l.arrayBuffer());p.byteLength>0&&s.write(p),s.end()}function $r(...r){let e=r.filter(i=>i!=null);if(e.length===0)return{};if(e.length===1)return e[0];let t={},s=e.filter(i=>i.preReceive).map(i=>i.preReceive);s.length>0&&(t.preReceive=async i=>{for(let f of s){let c=await f(i);if(Y(c))return c}});let n=e.filter(i=>i.update).map(i=>i.update);n.length>0&&(t.update=async i=>{for(let f of n){let c=await f(i);if(Y(c))return c}});let o=e.filter(i=>i.postReceive).map(i=>i.postReceive);o.length>0&&(t.postReceive=async i=>{for(let f of o)try{await f(i)}catch{}});let a=e.filter(i=>i.advertiseRefs).map(i=>i.advertiseRefs);return a.length>0&&(t.advertiseRefs=async i=>{let f=i.refs;for(let c of a){let l=await c({...i,refs:f});l&&(f=l)}return f}),t}function Wr(r,e){return async(t,s)=>{let n=await r(s);return n instanceof Response?n:n?e(t,s):new Response("Forbidden",{status:403})}}function Mr(r){let{protectedBranches:e=[],denyNonFastForward:t=!1,denyDeletes:s=!1,denyDeleteTags:n=!1,authorizePush:o,onPush:a}=r,i=new Set(e.map(c=>c.startsWith("refs/")?c:`refs/heads/${c}`)),f={};return(o||i.size>0)&&(f.preReceive=async c=>{if(o&&!await o(c.request))return{reject:!0,message:"unauthorized"};for(let l of c.updates)if(i.has(l.ref)){if(l.isDelete)return{reject:!0,message:`cannot delete protected branch ${l.ref}`};if(!l.isCreate&&!l.isFF)return{reject:!0,message:`non-fast-forward push to protected branch ${l.ref}`}}}),(t||s||n)&&(f.update=async c=>{if(s&&c.update.isDelete)return{reject:!0,message:"ref deletion denied"};if(n&&c.update.ref.startsWith("refs/tags/")){if(c.update.isDelete)return{reject:!0,message:"tag deletion denied"};if(!c.update.isCreate)return{reject:!0,message:"tag overwrite denied"}}if(t&&!c.update.isCreate&&!c.update.isDelete&&!c.update.isFF)return{reject:!0,message:"non-fast-forward"}}),a&&(f.postReceive=a),f}var Le=class{objects=new Map;refs=new Map;repo(e){return{objectStore:new De(this.getObjects(e)),refStore:new He(this.getRefs(e))}}async deleteRepo(e){this.objects.get(e)?.clear(),this.objects.delete(e),this.refs.get(e)?.clear(),this.refs.delete(e)}getObjects(e){let t=this.objects.get(e);return t||(t=new Map,this.objects.set(e,t)),t}getRefs(e){let t=this.refs.get(e);return t||(t=new Map,this.refs.set(e,t)),t}},De=class{constructor(e){this.store=e}async write(e,t){let s=await N(_(e,t));return this.store.has(s)||this.store.set(s,{type:e,content:new Uint8Array(t)}),s}async read(e){let t=this.store.get(e);if(!t)throw new Error(`object ${e} not found`);return{type:t.type,content:new Uint8Array(t.content)}}async exists(e){return this.store.has(e)}async ingestPack(e){if(e.byteLength<32||new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(8)===0)return 0;let n=this.store,o=await B(e,async a=>{let i=n.get(a);return i?{type:i.type,content:new Uint8Array(i.content)}:null});for(let a of o)n.has(a.hash)||n.set(a.hash,{type:a.type,content:a.content});return o.length}async findByPrefix(e){if(e.length<4)return[];let t=[];for(let s of this.store.keys())s.startsWith(e)&&t.push(s);return t}},He=class{constructor(e){this.store=e}async readRef(e){return this.store.get(e)??null}async writeRef(e,t){this.store.set(e,$(t))}async deleteRef(e){this.store.delete(e)}async compareAndSwapRef(e,t,s){let n=this.store.get(e)??null,o=null;if(n&&(n.type==="direct"?o=n.hash:n.type==="symbolic"&&(o=this.resolveChain(n.target))),t===null){if(n!==null)return!1}else if(o!==t)return!1;return s===null?this.store.delete(e):this.store.set(e,s),!0}async listRefs(e){let t=[];for(let[s,n]of this.store)if(!(e&&!s.startsWith(e))){if(n.type==="direct")t.push({name:s,hash:n.hash});else if(n.type==="symbolic"){let o=this.resolveChain(n.target);o&&t.push({name:s,hash:o})}}return t}resolveChain(e,t=0){if(t>10)return null;let s=this.store.get(e);return s?s.type==="direct"?s.hash:s.type==="symbolic"?this.resolveChain(s.target,t+1):null:null}};var Gr=`
31
+ `));s.push(U());let n=W(...s);if(t){let o=[];return o.push(Ue(1,n)),o.push(U()),W(...o)}return n}function Ue(r,e){let t=new Uint8Array(1+e.byteLength);return t[0]=r,t.set(e,1),P(t)}var ae=class{entries=new Map;currentBytes=0;maxBytes;hits=0;misses=0;constructor(e=256*1024*1024){this.maxBytes=e}static key(e,t,s){if(s.length>0)return null;let n=t.slice().sort();return`${e}\0${n.join(",")}`}get(e){let t=this.entries.get(e);return t?this.hits++:this.misses++,t}set(e,t){if(this.entries.has(e))return;let s=t.packData.byteLength;if(!(s>this.maxBytes)){for(;this.currentBytes+s>this.maxBytes&&this.entries.size>0;){let n=this.entries.keys().next().value;this.currentBytes-=this.entries.get(n).packData.byteLength,this.entries.delete(n)}this.entries.set(e,t),this.currentBytes+=s}}get stats(){return{entries:this.entries.size,bytes:this.currentBytes,hits:this.hits,misses:this.misses}}},Tr=["multi_ack_detailed","no-done","side-band-64k","ofs-delta","include-tag","allow-reachable-sha1-in-want","shallow"],Ur=["report-status","side-band-64k","ofs-delta","delete-refs"];async function Wt(r){let e=await r.refStore.listRefs("refs"),t=await r.refStore.readRef("HEAD"),s=[],n=null,o;if(t)if(t.type==="symbolic"){o=t.target;let i=await r.refStore.readRef(t.target);i?.type==="direct"&&(n=i.hash)}else n=t.hash;let a=e.slice().sort((i,f)=>i.name<f.name?-1:i.name>f.name?1:0);if(!n&&a.length>0){let i=Lr(a);i&&(n=i.hash,o=i.name)}n&&s.push({name:"HEAD",hash:n});for(let i of a)if(s.push({name:i.name,hash:i.hash}),i.name.startsWith("refs/tags/"))try{let f=await r.objectStore.read(i.hash);if(f.type==="tag"){let c=M(f.content);s.push({name:`${i.name}^{}`,hash:c.object})}}catch{}return{refs:s,headTarget:o}}var Cr=["refs/heads/main","refs/heads/master"];function Lr(r){for(let t of Cr){let s=r.find(n=>n.name===t);if(s)return s}return r.find(t=>t.name.startsWith("refs/heads/"))??null}function Mt(r,e,t){return Ht(r,e,e==="git-upload-pack"?Tr:Ur,t)}async function Gt(r,e,t){let{wants:s,haves:n,capabilities:o,clientShallows:a,depth:i,done:f}=Nt(e);if(s.length===0)return z(new Uint8Array(0),!1);let c=o.includes("multi_ack_detailed"),l=o.includes("side-band-64k"),d,p,u;if(a.length>0&&(u=new Set(a)),i!==void 0){let m=await kt(r,s,i,u??new Set);d=m,p=new Set(m.shallow)}if(d&&!f)return Bt(d);let h;if(c&&n.length>0){h=[];for(let m of n)await r.objectStore.exists(m)&&h.push(m);h.length===0&&(h=void 0)}let y=!p&&t?.cache&&t.cacheKey?ae.key(t.cacheKey,s,n):null;if(y&&t?.cache){let m=t.cache.get(y);if(m)return z(m.packData,l,h)}return t?.noDelta?Dr(r,s,n,o,l,h,d,p,u):Hr(r,s,n,o,l,h,t,y,d,p,u)}async function Dr(r,e,t,s,n,o,a,i,f){let{count:c,objects:l}=await Te(r,e,t,i,f);if(c===0){let{data:b}=await ue([]),x=z(b,n,o,a);return new ReadableStream({start(R){R.enqueue(x),R.close()}})}let d=[];for await(let b of l)d.push(b);let p=new Set(d.map(b=>b.hash)),u=[];if(s.includes("include-tag")){let b=await r.refStore.listRefs("refs/tags");for(let x of b)if(!p.has(x.hash))try{let R=await r.objectStore.read(x.hash);if(R.type==="tag"){let g=M(R.content);p.has(g.object)&&u.push({hash:x.hash,type:"tag",content:R.content})}}catch{}}let h=d.length+u.length;async function*y(){for(let b of d){let x=await r.objectStore.read(b.hash);yield{type:x.type,content:x.content}}for(let b of u)yield{type:b.type,content:b.content}}let m=wt(h,y()),O=_t(m,n,o,a);return new ReadableStream({async pull(b){let{value:x,done:R}=await O.next();R?b.close():b.enqueue(x)}})}async function Hr(r,e,t,s,n,o,a,i,f,c,l){let d=await St(r,e,t,c,l);if(d.count===0){let{data:b}=await ue([]);return z(b,n,o,f)}let p=await At(d),u=new Set(p.map(b=>b.hash));if(s.includes("include-tag")){let b=await r.refStore.listRefs("refs/tags");for(let x of b)if(!u.has(x.hash))try{let R=await r.objectStore.read(x.hash);if(R.type==="tag"){let g=M(R.content);u.has(g.object)&&(p.push({hash:x.hash,type:"tag",content:R.content}),u.add(x.hash))}}catch{}}let h=a?.deltaWindow?{window:a.deltaWindow}:void 0,y=It(p,h),m=y.map(b=>({hash:b.hash,type:b.type,content:b.content,delta:b.delta,deltaBaseHash:b.deltaBase})),{data:O}=await ue(m);if(i&&a?.cache){let b=y.filter(x=>x.delta).length;a.cache.set(i,{packData:O,objectCount:p.length,deltaCount:b})}return z(O,n,o,f)}async function Ft(r,e){let{commands:t,packData:s,capabilities:n,sawFlush:o}=$t(e),a=!0;if(s.byteLength>0)try{await r.objectStore.ingestPack(s)}catch{a=!1}let i=[];for(let f of t){let c=f.oldHash===me,l=f.newHash===me,d=!1;if(!c&&!l&&a)try{d=await Pt(r,f.oldHash,f.newHash)}catch{}i.push({ref:f.refName,oldHash:c?null:f.oldHash,newHash:f.newHash,isFF:d,isCreate:c,isDelete:l})}return{updates:i,unpackOk:a,capabilities:n,sawFlush:o}}function Nr(r){if(!r||typeof r.resolveRepo!="function")throw new TypeError("createGitServer: config.resolveRepo must be a function. Example: createGitServer({ resolveRepo: (path) => storage.repo(path) })");let{resolveRepo:e,hooks:t,basePath:s}=r,n=r.packCache===!1?void 0:new ae(r.packCache?.maxBytes),o=r.onError===!1?void 0:r.onError??(a=>{let i=a instanceof Error?a.message:String(a);console.error(`[server] Internal error: ${i}`)});return{async fetch(a){try{let i=new URL(a.url),f=decodeURIComponent(i.pathname);if(s){let c=s.replace(/\/+$/,"");if(!f.startsWith(c))return new Response("Not Found",{status:404});f=f.slice(c.length)}if(f.startsWith("/")||(f=`/${f}`),f.endsWith("/info/refs")&&a.method==="GET"){let c=i.searchParams.get("service");if(c!=="git-upload-pack"&&c!=="git-receive-pack")return new Response("Unsupported service",{status:403});let l=Ce(f,"/info/refs"),d=await e(l,a);if(d instanceof Response)return d;if(!d)return new Response("Not Found",{status:404});let p=d,{refs:u,headTarget:h}=await Wt(p),y=u;if(t?.advertiseRefs){let O=await t.advertiseRefs({repo:p,repoPath:l,refs:u,service:c,request:a});O&&(y=O)}let m=Mt(y,c,h);return new Response(m,{headers:{"Content-Type":`application/x-${c}-advertisement`,"Cache-Control":"no-cache"}})}if(f.endsWith("/git-upload-pack")&&a.method==="POST"){let c=Ce(f,"/git-upload-pack"),l=await e(c,a);if(l instanceof Response)return l;if(!l)return new Response("Not Found",{status:404});let d=l,p=await qt(a),u=await Gt(d,p,{cache:n,cacheKey:c,noDelta:r.packOptions?.noDelta,deltaWindow:r.packOptions?.deltaWindow});return new Response(u,{headers:{"Content-Type":"application/x-git-upload-pack-result"}})}if(f.endsWith("/git-receive-pack")&&a.method==="POST"){let c=Ce(f,"/git-receive-pack"),l=await e(c,a);if(l instanceof Response)return l;if(!l)return new Response("Not Found",{status:404});let d=l,p=await qt(a),{updates:u,unpackOk:h,capabilities:y,sawFlush:m}=await Ft(d,p);if(!m&&u.length===0)return new Response("Bad Request",{status:400});let O=y.includes("side-band-64k"),b=y.includes("report-status");if(!h){if(b){let g=u.map(w=>({name:w.ref,ok:!1,error:"unpack failed"}));return new Response(be(!1,g,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}if(t?.preReceive){let g=await t.preReceive({repo:d,repoPath:c,updates:u,request:a});if(Y(g)){if(b){let w=g.message??"pre-receive hook declined",j=u.map(v=>({name:v.ref,ok:!1,error:w}));return new Response(be(!0,j,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}}let x=[],R=[];for(let g of u){if(!g.isDelete&&!Rt(g.ref)){x.push({ref:g.ref,ok:!1,error:"invalid refname"});continue}if(t?.update){let w=await t.update({repo:d,repoPath:c,update:g,request:a});if(Y(w)){x.push({ref:g.ref,ok:!1,error:w.message??"update hook declined"});continue}}try{let w=g.isCreate?null:g.oldHash,j=g.isDelete?null:{type:"direct",hash:g.newHash};if(!await d.refStore.compareAndSwapRef(g.ref,w,j)){x.push({ref:g.ref,ok:!1,error:"failed to lock"});continue}x.push({ref:g.ref,ok:!0}),R.push(g)}catch(w){x.push({ref:g.ref,ok:!1,error:w instanceof Error?w.message:String(w)})}}if(t?.postReceive&&R.length>0)try{await t.postReceive({repo:d,repoPath:c,updates:R,request:a})}catch{}if(b){let g=x.map(w=>({name:w.ref,ok:w.ok,error:w.error}));return new Response(be(!0,g,O),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response(new Uint8Array(0),{headers:{"Content-Type":"application/x-git-receive-pack-result"}})}return new Response("Not Found",{status:404})}catch(i){return o?.(i,a),new Response("Internal Server Error",{status:500})}}}}function Ce(r,e){let t=r.slice(0,-e.length);return t.startsWith("/")&&(t=t.slice(1)),t}async function qt(r){let e=new Uint8Array(await r.arrayBuffer()),t=r.headers.get("content-encoding");if(t==="gzip"||t==="x-gzip"){let s=new DecompressionStream("gzip"),n=s.writable.getWriter();return n.write(e),n.close(),new Uint8Array(await new Response(s.readable).arrayBuffer())}return e}function Br(r){return(e,t)=>{let s=[];e.on("data",n=>s.push(new Uint8Array(n))),e.on("error",()=>{t.writeHead(500),t.end("Internal Server Error")}),e.on("end",()=>{_r(r,e,s,t).catch(()=>{try{t.writeHead(500),t.end("Internal Server Error")}catch{}})})}}async function _r(r,e,t,s){let n=typeof e.headers.host=="string"?e.headers.host:"localhost",o=new URL(e.url??"/",`http://${n}`),a=new Headers;for(let[u,h]of Object.entries(e.headers))if(h!==void 0)if(Array.isArray(h))for(let y of h)a.append(u,y);else a.set(u,h);let i=e.method??"GET",f;if(i!=="GET"&&i!=="HEAD"){let u=0;for(let m of t)u+=m.byteLength;let h=new Uint8Array(u),y=0;for(let m of t)h.set(m,y),y+=m.byteLength;f=h}let c=new Request(o.href,{method:i,headers:a,body:f}),l=await r.fetch(c),d={};l.headers.forEach((u,h)=>{d[h]=u}),s.writeHead(l.status,d);let p=new Uint8Array(await l.arrayBuffer());p.byteLength>0&&s.write(p),s.end()}function $r(...r){let e=r.filter(i=>i!=null);if(e.length===0)return{};if(e.length===1)return e[0];let t={},s=e.filter(i=>i.preReceive).map(i=>i.preReceive);s.length>0&&(t.preReceive=async i=>{for(let f of s){let c=await f(i);if(Y(c))return c}});let n=e.filter(i=>i.update).map(i=>i.update);n.length>0&&(t.update=async i=>{for(let f of n){let c=await f(i);if(Y(c))return c}});let o=e.filter(i=>i.postReceive).map(i=>i.postReceive);o.length>0&&(t.postReceive=async i=>{for(let f of o)try{await f(i)}catch{}});let a=e.filter(i=>i.advertiseRefs).map(i=>i.advertiseRefs);return a.length>0&&(t.advertiseRefs=async i=>{let f=i.refs;for(let c of a){let l=await c({...i,refs:f});l&&(f=l)}return f}),t}function Wr(r,e){return async(t,s)=>{let n=await r(s);return n instanceof Response?n:n?e(t,s):new Response("Forbidden",{status:403})}}function Mr(r){let{protectedBranches:e=[],denyNonFastForward:t=!1,denyDeletes:s=!1,denyDeleteTags:n=!1,authorizePush:o,onPush:a}=r,i=new Set(e.map(c=>c.startsWith("refs/")?c:`refs/heads/${c}`)),f={};return(o||i.size>0)&&(f.preReceive=async c=>{if(o&&!await o(c.request))return{reject:!0,message:"unauthorized"};for(let l of c.updates)if(i.has(l.ref)){if(l.isDelete)return{reject:!0,message:`cannot delete protected branch ${l.ref}`};if(!l.isCreate&&!l.isFF)return{reject:!0,message:`non-fast-forward push to protected branch ${l.ref}`}}}),(t||s||n)&&(f.update=async c=>{if(s&&c.update.isDelete)return{reject:!0,message:"ref deletion denied"};if(n&&c.update.ref.startsWith("refs/tags/")){if(c.update.isDelete)return{reject:!0,message:"tag deletion denied"};if(!c.update.isCreate)return{reject:!0,message:"tag overwrite denied"}}if(t&&!c.update.isCreate&&!c.update.isDelete&&!c.update.isFF)return{reject:!0,message:"non-fast-forward"}}),a&&(f.postReceive=a),f}var Le=class{objects=new Map;refs=new Map;repo(e){return{objectStore:new De(this.getObjects(e)),refStore:new He(this.getRefs(e))}}async deleteRepo(e){this.objects.get(e)?.clear(),this.objects.delete(e),this.refs.get(e)?.clear(),this.refs.delete(e)}getObjects(e){let t=this.objects.get(e);return t||(t=new Map,this.objects.set(e,t)),t}getRefs(e){let t=this.refs.get(e);return t||(t=new Map,this.refs.set(e,t)),t}},De=class{constructor(e){this.store=e}async write(e,t){let s=await N(_(e,t));return this.store.has(s)||this.store.set(s,{type:e,content:new Uint8Array(t)}),s}async read(e){let t=this.store.get(e);if(!t)throw new Error(`object ${e} not found`);return{type:t.type,content:new Uint8Array(t.content)}}async exists(e){return this.store.has(e)}async ingestPack(e){if(e.byteLength<32||new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(8)===0)return 0;let n=this.store,o=await B(e,async a=>{let i=n.get(a);return i?{type:i.type,content:new Uint8Array(i.content)}:null});for(let a of o)n.has(a.hash)||n.set(a.hash,{type:a.type,content:a.content});return o.length}async findByPrefix(e){if(e.length<4)return[];let t=[];for(let s of this.store.keys())s.startsWith(e)&&t.push(s);return t}},He=class{constructor(e){this.store=e}async readRef(e){return this.store.get(e)??null}async writeRef(e,t){this.store.set(e,$(t))}async deleteRef(e){this.store.delete(e)}async compareAndSwapRef(e,t,s){let n=this.store.get(e)??null,o=null;if(n&&(n.type==="direct"?o=n.hash:n.type==="symbolic"&&(o=this.resolveChain(n.target))),t===null){if(n!==null)return!1}else if(o!==t)return!1;return s===null?this.store.delete(e):this.store.set(e,s),!0}async listRefs(e){let t=[];for(let[s,n]of this.store)if(!(e&&!s.startsWith(e))){if(n.type==="direct")t.push({name:s,hash:n.hash});else if(n.type==="symbolic"){let o=this.resolveChain(n.target);o&&t.push({name:s,hash:o})}}return t}resolveChain(e,t=0){if(t>10)return null;let s=this.store.get(e);return s?s.type==="direct"?s.hash:s.type==="symbolic"?this.resolveChain(s.target,t+1):null:null}};var Gr=`
32
32
  CREATE TABLE IF NOT EXISTS git_objects (
33
33
  repo_id TEXT NOT NULL,
34
34
  hash TEXT NOT NULL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "just-git",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "Pure TypeScript git implementation: virtual filesystem client and embeddable server.",
5
5
  "keywords": [
6
6
  "agent",