/* Socket Lib - Built with esbuild */
var _=Object.create;var y=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var A=(n,e)=>{for(var t in e)y(n,t,{get:e[t],enumerable:!0})},P=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of C(e))!T.call(n,r)&&r!==t&&y(n,r,{get:()=>e[r],enumerable:!(o=$(e,r))||o.enumerable});return n};var N=(n,e,t)=>(t=n!=null?_(I(n)):{},P(e||!n||!n.__esModule?y(t,"default",{value:n,enumerable:!0}):t,n)),F=n=>P(y({},"__esModule",{value:!0}),n);var G={};A(G,{dlxPackage:()=>B,downloadPackage:()=>j,executePackage:()=>v});module.exports=F(G);var d=N(require("node:path")),x=require("./constants/platform"),E=require("./constants/packages"),b=require("./dlx"),m=require("./fs"),g=require("./path"),D=require("./paths"),O=require("./process-lock"),S=require("./spawn");let k;function R(){return k===void 0&&(k=require("node:fs")),k}let h;function J(){return h===void 0&&(h=require("./external/npm-package-arg")),h}let w;function M(){return w===void 0&&(w=require("./external/pacote")),w}const K=/[~^><=xX* ]|\|\|/;function V(n){try{const t=J()(n),o=t.type==="tag"||t.type==="version"||t.type==="range"?t.fetchSpec:void 0;return{name:t.name||n,version:o}}catch{const e=n.lastIndexOf("@");return e===-1||n.startsWith("@")?{name:n,version:void 0}:{name:n.slice(0,e),version:n.slice(e+1)}}}async function q(n,e,t){const o=(0,b.generateCacheKey)(e),r=(0,g.normalizePath)(d.default.join((0,D.getSocketDlxDir)(),o)),p=(0,g.normalizePath)(d.default.join(r,"node_modules",n));try{await(0,m.safeMkdir)(r,{recursive:!0})}catch(a){const i=a.code;throw i==="EACCES"||i==="EPERM"?new Error(`Permission denied creating package directory: ${r}
Please check directory permissions or run with appropriate access.`,{cause:a}):i==="EROFS"?new Error(`Cannot create package directory on read-only filesystem: ${r}
Ensure the filesystem is writable or set SOCKET_DLX_DIR to a writable location.`,{cause:a}):new Error(`Failed to create package directory: ${r}`,{cause:a})}const c=d.default.join(r,"concurrency.lock");return await O.processLock.withLock(c,async()=>{const a=R();if(!t&&a.existsSync(p)){const s=d.default.join(p,"package.json");if(a.existsSync(s))return{installed:!1,packageDir:r}}const i=(0,E.getPacoteCachePath)();try{await M().extract(e,p,{cache:i||d.default.join(r,".cache")})}catch(s){const f=s.code;throw f==="E404"||f==="ETARGET"?new Error(`Package not found: ${e}
Verify the package exists on npm registry and check the version.
Visit https://www.npmjs.com/package/${n} to see available versions.`,{cause:s}):f==="ENOTFOUND"||f==="ETIMEDOUT"||f==="EAI_AGAIN"?new Error(`Network error installing ${e}
Check your internet connection and try again.`,{cause:s}):new Error(`Failed to install package: ${e}
Destination: ${p}
Check npm registry connectivity or package name.`,{cause:s})}return{installed:!0,packageDir:r}},{staleMs:5e3,touchIntervalMs:2e3})}function L(n,e,t){const o=(0,g.normalizePath)(d.default.join(n,"node_modules",e)),r=d.default.join(o,"package.json"),c=(0,m.readJsonSync)(r).bin;let a;if(typeof c=="string")a=c;else if(typeof c=="object"&&c!==null){const i=c,s=Object.keys(i);if(s.length===1)a=i[s[0]];else{const f=e.split("/").pop(),u=[t,f,e.replace(/^@[^/]+\//,"")].filter(Boolean);for(const l of u)if(l&&i[l]){a=i[l];break}!a&&s.length>0&&(a=i[s[0]])}}if(!a)throw new Error(`No binary found for package "${e}"`);return(0,g.normalizePath)(d.default.join(o,a))}async function B(n,e,t){const o=await j(e),r=v(o.binaryPath,n,e?.spawnOptions,t);return{...o,spawnPromise:r}}async function j(n){const e=R(),{binaryName:t,force:o,package:r}={__proto__:null,...n},{name:p,version:c}=V(r),a=c!==void 0&&K.test(c),i=o!==void 0?o:a,s=c?`${p}@${c}`:p,{installed:f,packageDir:u}=await q(p,s,i),l=L(u,p,t);if(!x.WIN32&&e.existsSync(l))try{e.chmodSync(l,493)}catch{}return{binaryPath:l,installed:f,packageDir:u}}function v(n,e,t,o){return(0,S.spawn)(n,e,t,o)}0&&(module.exports={dlxPackage,downloadPackage,executePackage});
//# sourceMappingURL=dlx-package.js.map
