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