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.32 kB
JavaScript
import{execFile as f}from"node:child_process";import{promisify as h}from"node:util";import{log as p}from"../log/logger.js";import{probeUrls as b}from"./speed-test.js";const w=h(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 e=await b(c.map(r=>({url:r.url,label:r.label})),5e3),t=[];for(const r of e)if(r.reachable){const n=c.find(o=>o.url===r.url);n&&t.push(n)}for(const r of e)if(!r.reachable){const n=c.find(o=>o.url===r.url);n&&!t.includes(n)&&t.push(n)}for(const r of c)t.includes(r)||t.push(r);return t}async function U(){try{const e=process.platform==="win32",t=e?"cmd.exe":"npm",r=e?["/c","npm","config","get","registry"]:["config","get","registry"],{stdout:n}=await w(t,r,{timeout:x,encoding:"utf-8"});return n.trim()}catch{return"https://registry.npmjs.org"}}async function _(e,t,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 s of n)try{return p.info("installer",`npm install ${e} using ${s.label} (${s.url})`),{output:await I(e,s.url,t,r),registry:s.label}}catch(i){const l=i instanceof Error?i.message:String(i);if(o=l,p.info("installer",`${s.label} failed: ${l.slice(0,200)}`),!$(l))throw i}throw new Error(`npm install failed on all registries. Last error: ${o}`)}function $(e){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(e)}function I(e,t,r,n){return new Promise((o,s)=>{const i=["install","-g",e,"--registry",t,"--prefer-online","--no-audit","--no-fund"],l=process.platform==="win32",g=l?"cmd.exe":"npm",d=l?["/c","npm",...i]:i;f(g,d,{timeout:r,maxBuffer:n},(a,E,m)=>{if(a){const u=m?.trim()||a.message,y=a.killed===!0?`ETIMEDOUT: npm install exceeded ${r}ms (registry likely unreachable): ${u}`:u;s(new Error(y));return}o(`${E??""}
${m??""}`.trim())})})}function A(e){return c.map(t=>({label:t.label,command:`npm install -g ${e} --registry ${t.url}`}))}export{c as NPM_REGISTRIES,U as getCurrentRegistry,A as getMirrorInstallCommands,$ as isRetriableRegistryError,_ as npmInstallWithMirror,T as probeRegistries};