/* Socket Lib - Built with esbuild */
var d=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var y=(o,t)=>{for(var n in t)d(o,n,{get:t[n],enumerable:!0})},b=(o,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of L(t))!h.call(o,r)&&r!==n&&d(o,r,{get:()=>t[r],enumerable:!(i=T(t,r))||i.enumerable});return o};var I=o=>b(d({},"__esModule",{value:!0}),o);var E={};y(E,{downloadWithLock:()=>z});module.exports=I(E);var m=require("node:fs"),e=require("node:fs/promises"),l=require("node:path"),D=require("./http-request");function O(o,t){const n=t||`${(0,l.dirname)(o)}/.locks`,i=`${o.replace(/[^\w.-]/g,"_")}.lock`;return(0,l.join)(n,i)}function x(o,t){if(Date.now()-o.startTime>t)return!0;try{return process.kill(o.pid,0),!1}catch{return!0}}async function _(o,t,n){const{lockTimeout:i,locksDir:r,pollInterval:u,staleTimeout:f}=n,a=O(o,r),w=(0,l.dirname)(a);await(0,e.mkdir)(w,{recursive:!0});const c=Date.now();for(;;)try{if((0,m.existsSync)(a)){const p=await(0,e.readFile)(a,"utf8"),k=JSON.parse(p);if(x(k,f))await(0,e.rm)(a,{force:!0});else{if(Date.now()-c>i)throw new Error(`Lock acquisition timed out after ${i}ms (held by PID ${k.pid})`);await new Promise(g=>setTimeout(g,u));continue}}const s={pid:process.pid,startTime:Date.now(),url:t};return await(0,e.writeFile)(a,JSON.stringify(s,null,2),{flag:"wx"}),a}catch(s){if(s.code==="EEXIST"){if(Date.now()-c>i)throw new Error(`Lock acquisition timed out after ${i}ms`);await new Promise(p=>setTimeout(p,u));continue}throw s}}async function v(o){try{await(0,e.rm)(o,{force:!0})}catch{}}async function z(o,t,n){const{lockTimeout:i=6e4,locksDir:r,pollInterval:u=1e3,staleTimeout:f=1e4,...a}={__proto__:null,...n};if((0,m.existsSync)(t)){const c=await(0,e.stat)(t).catch(()=>null);if(c&&c.size>0)return{path:t,size:c.size}}const w=await _(t,o,{lockTimeout:i,locksDir:r,pollInterval:u,staleTimeout:f});try{if((0,m.existsSync)(t)){const s=await(0,e.stat)(t).catch(()=>null);if(s&&s.size>0)return{path:t,size:s.size}}return await(0,D.httpDownload)(o,t,a)}finally{await v(w)}}0&&(module.exports={downloadWithLock});
//# sourceMappingURL=download-lock.js.map
