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