/* Socket Lib - Built with esbuild */
var q=Object.create;var _=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var U=(t,e)=>{for(var n in e)_(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Y(e))!W.call(t,r)&&r!==n&&_(t,r,{get:()=>e[r],enumerable:!(s=J(e,r))||s.enumerable});return t};var v=(t,e,n)=>(n=t!=null?q(V(t)):{},B(e||!t||!t.__esModule?_(n,"default",{value:t,enumerable:!0}):n,t)),G=t=>B(_({},"__esModule",{value:!0}),t);var re={};U(re,{cleanDlxCache:()=>Q,dlxBinary:()=>Z,downloadBinary:()=>ee,executeBinary:()=>te,getDlxCachePath:()=>S,listDlxCache:()=>ne});module.exports=G(re);var R=require("node:crypto"),D=v(require("node:os")),p=v(require("node:path")),P=require("#constants/platform"),A=require("./dlx"),F=require("./http-request"),l=require("./fs"),I=require("./objects"),T=require("./path"),N=require("./paths"),L=require("./process-lock"),j=require("./spawn");let O;function b(){return O===void 0&&(O=require("node:fs")),O}function k(t){return p.default.join(t,".dlx-metadata.json")}async function z(t,e){const n=b();try{const s=k(t);if(!n.existsSync(s))return!1;const r=await(0,l.readJson)(s,{throws:!1});if(!(0,I.isObjectObject)(r))return!1;const a=Date.now(),o=r.timestamp;return typeof o!="number"||o<=0?!1:a-o<e}catch{return!1}}async function H(t,e,n){const s=p.default.dirname(e),r=p.default.join(s,"concurrency.lock");return await L.processLock.withLock(r,async()=>{const a=b();if(a.existsSync(e)&&(await a.promises.stat(e)).size>0){const m=await a.promises.readFile(e),d=(0,R.createHash)("sha256");return d.update(m),d.digest("hex")}try{await(0,F.httpDownload)(t,e)}catch(i){throw new Error(`Failed to download binary from ${t}
Destination: ${e}
Check your internet connection or verify the URL is accessible.`,{cause:i})}const o=await a.promises.readFile(e),c=(0,R.createHash)("sha256");c.update(o);const f=c.digest("hex");if(n&&f!==n)throw await(0,l.safeDelete)(e),new Error(`Checksum mismatch: expected ${n}, got ${f}`);return P.WIN32||await a.promises.chmod(e,493),f},{staleMs:5e3,touchIntervalMs:2e3})}async function M(t,e,n,s,r){const a=k(t),o={version:"1.0.0",cache_key:e,timestamp:Date.now(),checksum:s,checksum_algorithm:"sha256",platform:D.default.platform(),arch:D.default.arch(),size:r,source:{type:"download",url:n}};await b().promises.writeFile(a,JSON.stringify(o,null,2))}async function Q(t=require("#constants/time").DLX_BINARY_CACHE_TTL){const e=S(),n=b();if(!n.existsSync(e))return 0;let s=0;const r=Date.now(),a=await n.promises.readdir(e);for(const o of a){const c=p.default.join(e,o),f=k(c);try{if(!await(0,l.isDir)(c))continue;const i=await(0,l.readJson)(f,{throws:!1});if(!i||typeof i!="object"||Array.isArray(i))continue;const m=i.timestamp;(typeof m=="number"&&m>0?r-m:Number.POSITIVE_INFINITY)>t&&(await(0,l.safeDelete)(c,{force:!0,recursive:!0}),s+=1)}catch{try{(await n.promises.readdir(c)).length||(await(0,l.safeDelete)(c),s+=1)}catch{}}}return s}async function Z(t,e,n){const{cacheTtl:s=require("#constants/time").DLX_BINARY_CACHE_TTL,checksum:r,force:a=!1,name:o,spawnOptions:c,url:f}={__proto__:null,...e},i=S(),m=o||`binary-${process.platform}-${D.default.arch()}`,d=`${f}:${m}`,x=(0,A.generateCacheKey)(d),u=p.default.join(i,x),h=(0,T.normalizePath)(p.default.join(u,m)),E=b();let y=!1,g=r;if(!a&&E.existsSync(u)&&await z(u,s))try{const $=k(u),w=await(0,l.readJson)($,{throws:!1});w&&typeof w=="object"&&!Array.isArray(w)&&typeof w.checksum=="string"?g=w.checksum:y=!0}catch{y=!0}else y=!0;if(y){try{await(0,l.safeMkdir)(u,{recursive:!0})}catch(w){const C=w.code;throw C==="EACCES"||C==="EPERM"?new Error(`Permission denied creating binary cache directory: ${u}
Please check directory permissions or run with appropriate access.`,{cause:w}):C==="EROFS"?new Error(`Cannot create binary cache directory on read-only filesystem: ${u}
Ensure the filesystem is writable or set SOCKET_DLX_DIR to a writable location.`,{cause:w}):new Error(`Failed to create binary cache directory: ${u}`,{cause:w})}g=await H(f,h,r);const $=await E.promises.stat(h);await M(u,x,f,g||"",$.size)}const K=P.WIN32&&/\.(?:bat|cmd|ps1)$/i.test(h)?{...c,env:{...c?.env,PATH:`${u}${p.default.delimiter}${process.env.PATH||""}`},shell:!0}:c,X=(0,j.spawn)(h,t,K,n);return{binaryPath:h,downloaded:y,spawnPromise:X}}async function ee(t){const{cacheTtl:e=require("#constants/time").DLX_BINARY_CACHE_TTL,checksum:n,force:s=!1,name:r,url:a}={__proto__:null,...t},o=S(),c=r||`binary-${process.platform}-${D.default.arch()}`,f=`${a}:${c}`,i=(0,A.generateCacheKey)(f),m=p.default.join(o,i),d=(0,T.normalizePath)(p.default.join(m,c)),x=b();let u=!1;if(!s&&x.existsSync(m)&&await z(m,e))u=!1;else{try{await(0,l.safeMkdir)(m,{recursive:!0})}catch(y){const g=y.code;throw g==="EACCES"||g==="EPERM"?new Error(`Permission denied creating binary cache directory: ${m}
Please check directory permissions or run with appropriate access.`,{cause:y}):g==="EROFS"?new Error(`Cannot create binary cache directory on read-only filesystem: ${m}
Ensure the filesystem is writable or set SOCKET_DLX_DIR to a writable location.`,{cause:y}):new Error(`Failed to create binary cache directory: ${m}`,{cause:y})}const h=await H(a,d,n),E=await x.promises.stat(d);await M(m,i,a,h||"",E.size),u=!0}return{binaryPath:d,downloaded:u}}function te(t,e,n,s){const r=P.WIN32&&/\.(?:bat|cmd|ps1)$/i.test(t),a=p.default.dirname(t),o=r?{...n,env:{...n?.env,PATH:`${a}${p.default.delimiter}${process.env.PATH||""}`},shell:!0}:n;return(0,j.spawn)(t,e,o,s)}function S(){return(0,N.getSocketDlxDir)()}async function ne(){const t=S(),e=b();if(!e.existsSync(t))return[];const n=[],s=Date.now(),r=await e.promises.readdir(t);for(const a of r){const o=p.default.join(t,a);try{if(!await(0,l.isDir)(o))continue;const c=k(o),f=await(0,l.readJson)(c,{throws:!1});if(!f||typeof f!="object"||Array.isArray(f))continue;const i=f,d=i.source?.url||i.url||"",u=(await e.promises.readdir(o)).find(h=>!h.startsWith("."));if(u){const h=p.default.join(o,u),E=await e.promises.stat(h);n.push({age:s-(i.timestamp||0),arch:i.arch||"unknown",checksum:i.checksum||"",name:u,platform:i.platform||"unknown",size:E.size,url:d})}}catch{}}return n}0&&(module.exports={cleanDlxCache,dlxBinary,downloadBinary,executeBinary,getDlxCachePath,listDlxCache});
//# sourceMappingURL=dlx-binary.js.map
