/* Socket Lib - Built with esbuild */
var T=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var $=(n,r)=>{for(var t in r)T(n,t,{get:r[t],enumerable:!0})},B=(n,r,t,d)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of k(r))!C.call(n,i)&&i!==t&&T(n,i,{get:()=>r[i],enumerable:!(d=P(r,i))||d.enumerable});return n};var S=n=>B(T({},"__esModule",{value:!0}),n);var z={};$(z,{httpDownload:()=>M,httpGetJson:()=>G,httpGetText:()=>A,httpRequest:()=>b});module.exports=S(z);var x=require("node:fs");let H,E;function _(){return H===void 0&&(H=require("node:http")),H}function q(){return E===void 0&&(E=require("node:https")),E}async function b(n,r){const{body:t,followRedirects:d=!0,headers:i={},maxRedirects:l=5,method:w="GET",retries:a=0,retryDelay:u=1e3,timeout:m=3e4}={__proto__:null,...r};let f;for(let h=0;h<=a;h++)try{return await O(n,{body:t,followRedirects:d,headers:i,maxRedirects:l,method:w,timeout:m})}catch(c){if(f=c,h===a)break;const y=u*2**h;await new Promise(p=>setTimeout(p,y))}throw f||new Error("Request failed after retries")}async function O(n,r){const{body:t,followRedirects:d=!0,headers:i={},maxRedirects:l=5,method:w="GET",timeout:a=3e4}={__proto__:null,...r};return await new Promise((u,m)=>{const f=new URL(n),c=f.protocol==="https:"?q():_(),y={headers:{"User-Agent":"socket-registry/1.0",...i},hostname:f.hostname,method:w,path:f.pathname+f.search,port:f.port,timeout:a},p=c.request(y,e=>{if(d&&e.statusCode&&e.statusCode>=300&&e.statusCode<400&&e.headers.location){if(l<=0){m(new Error(`Too many redirects (exceeded maximum: ${l})`));return}const s=e.headers.location.startsWith("http")?e.headers.location:new URL(e.headers.location,n).toString();u(O(s,{body:t,followRedirects:d,headers:i,maxRedirects:l-1,method:w,timeout:a}));return}const o=[];e.on("data",s=>{o.push(s)}),e.on("end",()=>{const s=Buffer.concat(o),R=e.statusCode!==void 0&&e.statusCode>=200&&e.statusCode<300,g={arrayBuffer(){return s.buffer.slice(s.byteOffset,s.byteOffset+s.byteLength)},body:s,headers:e.headers,json(){return JSON.parse(s.toString("utf8"))},ok:R,status:e.statusCode||0,statusText:e.statusMessage||"",text(){return s.toString("utf8")}};u(g)})});p.on("error",e=>{const o=new Error(`HTTP request failed: ${e.message}`,{cause:e});m(o)}),p.on("timeout",()=>{p.destroy(),m(new Error(`Request timed out after ${a}ms`))}),t&&p.write(t),p.end()})}async function M(n,r,t){const{headers:d={},onProgress:i,retries:l=0,retryDelay:w=1e3,timeout:a=12e4}={__proto__:null,...t};let u;for(let m=0;m<=l;m++)try{return await U(n,r,{headers:d,onProgress:i,timeout:a})}catch(f){if(u=f,m===l)break;const h=w*2**m;await new Promise(c=>setTimeout(c,h))}throw u||new Error("Download failed after retries")}async function U(n,r,t){const{headers:d={},onProgress:i,timeout:l=12e4}={__proto__:null,...t};return await new Promise((w,a)=>{const u=new URL(n),f=u.protocol==="https:"?q():_(),h={headers:{"User-Agent":"socket-registry/1.0",...d},hostname:u.hostname,method:"GET",path:u.pathname+u.search,port:u.port,timeout:l};let c,y=!1;const p=()=>{!y&&c&&(y=!0,c.close())},e=f.request(h,o=>{if(!o.statusCode||o.statusCode<200||o.statusCode>=300){p(),a(new Error(`Download failed: HTTP ${o.statusCode} ${o.statusMessage}`));return}const s=Number.parseInt(o.headers["content-length"]||"0",10);let R=0;c=(0,x.createWriteStream)(r),c.on("error",g=>{p();const D=new Error(`Failed to write file: ${g.message}`,{cause:g});a(D)}),o.on("data",g=>{R+=g.length,i&&s>0&&i(R,s)}),o.on("end",()=>{c?.close(()=>{y=!0,w({path:r,size:R})})}),o.on("error",g=>{p(),a(g)}),o.pipe(c)});e.on("error",o=>{p();const s=new Error(`HTTP download failed: ${o.message}`,{cause:o});a(s)}),e.on("timeout",()=>{e.destroy(),p(),a(new Error(`Download timed out after ${l}ms`))}),e.end()})}async function G(n,r){const t=await b(n,{...r,method:"GET"});if(!t.ok)throw new Error(`HTTP ${t.status}: ${t.statusText}`);try{return t.json()}catch(d){throw new Error("Failed to parse JSON response",{cause:d})}}async function A(n,r){const t=await b(n,{...r,method:"GET"});if(!t.ok)throw new Error(`HTTP ${t.status}: ${t.statusText}`);return t.text()}0&&(module.exports={httpDownload,httpGetJson,httpGetText,httpRequest});
//# sourceMappingURL=http-request.js.map
