/* Socket Lib - Built with esbuild */
var v=Object.create;var b=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var F=(t,n)=>{for(var e in n)b(t,e,{get:n[e],enumerable:!0})},O=(t,n,e,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of H(n))!z.call(t,r)&&r!==e&&b(t,r,{get:()=>n[r],enumerable:!(i=E(n,r))||i.enumerable});return t};var T=(t,n,e)=>(e=t!=null?v(L(t)):{},O(n||!t||!t.__esModule?b(e,"default",{value:t,enumerable:!0}):e,t)),W=t=>O(b({},"__esModule",{value:!0}),t);var G={};F(G,{cleanDlxCache:()=>M,dlxBinary:()=>V,getDlxCachePath:()=>D,listDlxCache:()=>X});module.exports=W(G);var j=require("node:crypto"),s=require("node:fs"),x=T(require("node:os")),p=T(require("node:path")),A=require("#constants/platform"),_=require("./download-lock"),c=require("./fs"),C=require("./objects"),S=require("./path"),B=require("./paths"),R=require("./spawn");function Y(t,n){return(0,j.createHash)("sha256").update(`${t}:${n}`).digest("hex")}function g(t){return p.default.join(t,".dlx-metadata.json")}async function q(t,n){try{const e=g(t);if(!(0,s.existsSync)(e))return!1;const i=await(0,c.readJson)(e,{throws:!1});if(!(0,C.isObjectObject)(i))return!1;const r=Date.now(),a=i.timestamp;return typeof a!="number"||a<=0?!1:r-a<n}catch{return!1}}async function J(t,n,e){await(0,_.downloadWithLock)(t,n,{staleTimeout:1e4,lockTimeout:12e4});const i=await s.promises.readFile(n),r=(0,j.createHash)("sha256");r.update(i);const a=r.digest("hex");if(e&&a!==e)throw await(0,c.safeDelete)(n),new Error(`Checksum mismatch: expected ${e}, got ${a}`);return A.WIN32||await s.promises.chmod(n,493),a}async function K(t,n,e){const i=g(t),r={arch:x.default.arch(),checksum:e,platform:x.default.platform(),timestamp:Date.now(),url:n,version:"1.0.0"};await s.promises.writeFile(i,JSON.stringify(r,null,2))}async function M(t=require("#constants/time").DLX_BINARY_CACHE_TTL){const n=D();if(!(0,s.existsSync)(n))return 0;let e=0;const i=Date.now(),r=await s.promises.readdir(n);for(const a of r){const m=p.default.join(n,a),u=g(m);try{if(!await(0,c.isDir)(m))continue;const f=await(0,c.readJson)(u,{throws:!1});if(!f||typeof f!="object"||Array.isArray(f))continue;const l=f.timestamp;(typeof l=="number"&&l>0?i-l:Number.POSITIVE_INFINITY)>t&&(await(0,c.safeDelete)(m,{force:!0,recursive:!0}),e+=1)}catch{try{(await s.promises.readdir(m)).length||(await(0,c.safeDelete)(m),e+=1)}catch{}}}return e}async function V(t,n,e){const{cacheTtl:i=require("#constants/time").DLX_BINARY_CACHE_TTL,checksum:r,force:a=!1,name:m,spawnOptions:u,url:f}={__proto__:null,...n},l=D(),d=m||`binary-${process.platform}-${x.default.arch()}`,P=Y(f,d),o=p.default.join(l,P),y=(0,S.normalizePath)(p.default.join(o,d));let h=!1,k=r;if(!a&&(0,s.existsSync)(o)&&await q(o,i))try{const N=g(o),w=await(0,c.readJson)(N,{throws:!1});w&&typeof w=="object"&&!Array.isArray(w)&&typeof w.checksum=="string"?k=w.checksum:h=!0}catch{h=!0}else h=!0;h&&(await s.promises.mkdir(o,{recursive:!0}),k=await J(f,y,r),await K(o,f,k||""));const $=A.WIN32&&/\.(?:bat|cmd|ps1)$/i.test(y)?{...u,env:{...u?.env,PATH:`${o}${p.default.delimiter}${process.env.PATH||""}`},shell:!0}:u,I=(0,R.spawn)(y,t,$,e);return{binaryPath:y,downloaded:h,spawnPromise:I}}function D(){return(0,B.getSocketDlxDir)()}async function X(){const t=D();if(!(0,s.existsSync)(t))return[];const n=[],e=Date.now(),i=await s.promises.readdir(t);for(const r of i){const a=p.default.join(t,r);try{if(!await(0,c.isDir)(a))continue;const m=g(a),u=await(0,c.readJson)(m,{throws:!1});if(!u||typeof u!="object"||Array.isArray(u))continue;const l=(await s.promises.readdir(a)).find(d=>!d.startsWith("."));if(l){const d=p.default.join(a,l),P=await s.promises.stat(d),o=u;n.push({age:e-(o.timestamp||0),arch:o.arch||"unknown",checksum:o.checksum||"",name:l,platform:o.platform||"unknown",size:P.size,url:o.url||""})}}catch{}}return n}0&&(module.exports={cleanDlxCache,dlxBinary,getDlxCachePath,listDlxCache});
//# sourceMappingURL=dlx-binary.js.map
