/* Socket Lib - Built with esbuild */
var l=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var x=(e,t)=>{for(var s in t)l(e,s,{get:t[s],enumerable:!0})},O=(e,t,s,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of R(t))!k.call(e,n)&&n!==s&&l(e,n,{get:()=>t[n],enumerable:!(i=A(t,n))||i.enumerable});return e};var S=e=>O(l({},"__esModule",{value:!0}),e);var F={};x(F,{cacheFetchGhsa:()=>U,clearRefCache:()=>E,fetchGhsaDetails:()=>_,fetchGitHub:()=>o,getGhsaUrl:()=>I,getGitHubToken:()=>m,getGitHubTokenFromGitConfig:()=>H,getGitHubTokenWithFallback:()=>P,resolveRefToSha:()=>D});module.exports=S(F);var y=require("./cache-with-ttl"),g=require("#env/github"),w=require("#env/socket-cli"),G=require("./http-request"),v=require("./spawn");const f="https://api.github.com",C=300*1e3;let c;function T(){return c===void 0&&(c=(0,y.createTtlCache)({memoize:!0,prefix:"github-refs",ttl:C})),c}function m(){return(0,g.getGithubToken)()||(0,g.getGhToken)()||(0,w.getSocketCliGithubToken)()||void 0}async function o(e,t){const s={__proto__:null,...t},i=s.token||m(),n={Accept:"application/vnd.github.v3+json","User-Agent":"socket-registry-github-client",...s.headers};i&&(n.Authorization=`Bearer ${i}`);const r=await(0,G.httpRequest)(e,{headers:n});if(!r.ok){if(r.status===403){const a=r.headers["x-ratelimit-remaining"];if((typeof a=="string"?a:a?.[0])==="0"){const u=r.headers["x-ratelimit-reset"],p=typeof u=="string"?u:u?.[0],h=p?new Date(Number(p)*1e3):void 0,d=new Error(`GitHub API rate limit exceeded${h?`. Resets at ${h.toLocaleString()}`:""}. Use GITHUB_TOKEN environment variable to increase rate limit.`);throw d.status=403,d.resetTime=h,d}}throw new Error(`GitHub API error ${r.status}: ${r.statusText}`)}return JSON.parse(r.body.toString("utf8"))}async function D(e,t,s,i){const n={__proto__:null,...i},r=`${e}/${t}@${s}`;return process.env.DISABLE_GITHUB_CACHE?await b(e,t,s,n):await T().getOrFetch(r,async()=>await b(e,t,s,n))}async function b(e,t,s,i){const n={token:i.token};try{const r=`${f}/repos/${e}/${t}/git/refs/tags/${s}`,a=await o(r,n);return a.object.type==="tag"?(await o(a.object.url,n)).object.sha:a.object.sha}catch{try{const r=`${f}/repos/${e}/${t}/git/refs/heads/${s}`;return(await o(r,n)).object.sha}catch{try{const r=`${f}/repos/${e}/${t}/commits/${s}`;return(await o(r,n)).sha}catch(r){throw new Error(`failed to resolve ref "${s}" for ${e}/${t}: ${r instanceof Error?r.message:String(r)}`)}}}}async function E(){c&&await c.clear({memoOnly:!0})}async function H(e){try{const t=await(0,v.spawn)("git",["config","github.token"],{...e,stdio:"pipe"});if(t.code===0&&t.stdout)return t.stdout.toString().trim()}catch{}}async function P(){return m()||await H()}function I(e){return`https://github.com/advisories/${e}`}async function _(e,t){const s=`https://api.github.com/advisories/${e}`,i=await o(s,t);return{ghsaId:i.ghsa_id,summary:i.summary,details:i.details,severity:i.severity,aliases:i.aliases||[],publishedAt:i.published_at,updatedAt:i.updated_at,withdrawnAt:i.withdrawn_at,references:i.references||[],vulnerabilities:i.vulnerabilities||[],cvss:i.cvss,cwes:i.cwes||[]}}async function U(e,t){const s=T(),i=`ghsa:${e}`;if(!process.env.DISABLE_GITHUB_CACHE){const r=await s.get(i);if(r)return JSON.parse(r)}const n=await _(e,t);return await s.set(i,JSON.stringify(n)),n}0&&(module.exports={cacheFetchGhsa,clearRefCache,fetchGhsaDetails,fetchGitHub,getGhsaUrl,getGitHubToken,getGitHubTokenFromGitConfig,getGitHubTokenWithFallback,resolveRefToSha});
//# sourceMappingURL=github.js.map
