grix-connector
Version:
Connect local AI coding agents (Claude, Codex, Gemini, Qwen, DeepSeek, Cursor, OpenCode, Pi, OpenHuman, Reasonix) to the Grix scheduling platform. Also serves as an OpenClaw plugin for Grix channel transport.
3 lines (2 loc) • 2.45 kB
JavaScript
import{execFile as f}from"node:child_process";import{promisify as y}from"node:util";import{log as p}from"../log/logger.js";import{probeUrls as h}from"./speed-test.js";const w=y(f),x=1e4,c=[{id:"official",label:"npm \u5B98\u65B9",url:"https://registry.npmjs.org"},{id:"npmmirror",label:"npmmirror (\u6DD8\u5B9D\u955C\u50CF)",url:"https://registry.npmmirror.com"}];async function T(){const t=await h(c.map(r=>({url:r.url,label:r.label})),5e3),e=[];for(const r of t)if(r.reachable){const n=c.find(o=>o.url===r.url);n&&e.push(n)}for(const r of t)if(!r.reachable){const n=c.find(o=>o.url===r.url);n&&!e.includes(n)&&e.push(n)}for(const r of c)e.includes(r)||e.push(r);return e}async function U(){try{const t=process.platform==="win32",e=t?"cmd.exe":"npm",r=t?["/c","npm","config","get","registry"]:["config","get","registry"],{stdout:n}=await w(e,r,{timeout:x,encoding:"utf-8"});return n.trim()}catch{return"https://registry.npmjs.org"}}async function M(t,e,r){const n=await T();if(n.length===0)throw new Error("All npm registries are unreachable. Check your network connection.");let o="";for(const i of n)try{return p.info("installer",`npm install ${t} using ${i.label} (${i.url})`),{output:await $(t,i.url,e,r),registry:i.label}}catch(s){const l=s instanceof Error?s.message:String(s);if(o=l,p.info("installer",`${i.label} failed: ${l.slice(0,200)}`),!I(l))throw s}throw new Error(`npm install failed on all registries. Last error: ${o}`)}function I(t){return/ECONNRESET|ETIMEDOUT|ECONNREFUSED|ENOTFOUND|EAI_AGAIN|ERR_SOCKET_TIMEOUT|getaddrinfo|socket hang up|network|fetch failed|timed out|tunneling socket|self.signed|404/i.test(t)}function S(t){return/\bEBUSY\b|resource busy or locked|EPERM[^\n]*\brename\b|\bELOCKED\b/i.test(t)}function $(t,e,r,n){return new Promise((o,i)=>{const s=["install","-g",t,"--registry",e,"--prefer-online","--no-audit","--no-fund"],l=process.platform==="win32",g=l?"cmd.exe":"npm",E=l?["/c","npm",...s]:s;f(g,E,{timeout:r,maxBuffer:n},(a,d,m)=>{if(a){const u=m?.trim()||a.message,b=a.killed===!0?`ETIMEDOUT: npm install exceeded ${r}ms (registry likely unreachable): ${u}`:u;i(new Error(b));return}o(`${d??""}
${m??""}`.trim())})})}function _(t){return c.map(e=>({label:e.label,command:`npm install -g ${t} --registry ${e.url}`}))}export{c as NPM_REGISTRIES,U as getCurrentRegistry,_ as getMirrorInstallCommands,I as isRetriableRegistryError,S as isTransientInstallError,M as npmInstallWithMirror,T as probeRegistries};