grix-connector 4.11.0 → 4.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
Binary file
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
|
-
"bridgeVersion": "4.11.
|
|
4
|
-
"tarball": "grix-dsh-bridge-4.11.
|
|
5
|
-
"size":
|
|
3
|
+
"bridgeVersion": "4.11.2",
|
|
4
|
+
"tarball": "grix-dsh-bridge-4.11.2.tgz",
|
|
5
|
+
"size": 23388,
|
|
6
6
|
"unpackedSize": 94036,
|
|
7
|
-
"shasum": "
|
|
8
|
-
"integrity": "sha512-
|
|
7
|
+
"shasum": "dc1108d970fa2c9b645bee715d1754656e5005a7",
|
|
8
|
+
"integrity": "sha512-qrq9qWyRhAmeTbHajVDu4YRrf2JDTi/c9i2ZI1bpJD8CxpoYO5cod2ZG/alse5hHx1cCl3G1Fi31zAtJKxz6wQ=="
|
|
9
9
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{execFile as yt,execFileSync as R}from"node:child_process";import{randomUUID as Q}from"node:crypto";import{promisify as Et}from"node:util";import{uptime as kt}from"node:os";import{closeSync as It,copyFileSync as Pt,cpSync as vt,existsSync as d,linkSync as xt,mkdirSync as E,mkdtempSync as tt,openSync as $t,readFileSync as P,renameSync as h,rmSync as g,statfsSync as Dt,symlinkSync as Nt,unlinkSync as H,writeFileSync as x}from"node:fs";import{basename as v,dirname as w,join as l,relative as et,resolve as u,sep as b}from"node:path";import{GRIX_PATHS as $}from"../log/index.js";import{appendRotatingFileSync as At}from"../log/rotation.js";import{getMachineName as Rt}from"../util/host-info.js";import{resolveInstallId as bt}from"../util/install-id.js";import{NPM_REGISTRIES as Tt,npmInstallWithMirror as rt,probeRegistries as Ut,resolveNpmCacheDir as nt,resolveNpmInvocation as M}from"../installer/npm-registry.js";import{resolveWindowsServiceRuntimeDir as it}from"../../service/service-paths.js";const ot=Et(yt);class f extends Error{code;constructor(e,r){super(r),this.name="UpgradeError",this.code=e}}const Lt=/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;function O(t){const e=String(t??"").trim().replace(/^v(?=\d)/,"");if(!Lt.test(e))throw new f("INVALID_VERSION",`Invalid connector upgrade version: ${String(t)}`);return e}const Y=".grix-upgrade-backup",Ft=".grix-upgrade-failed",at=".grix-upgrade-backup",ct=".grix-upgrade-next",Mt="upgrade-wrapper.pause.json",Ot="upgrade-rollback.intent.json",Ct=".grix-connector-upgrade.lock",st=[250,750],C=[100,250,750,1500];function ut(){return l($.log,"upgrade.log")}function I(){return l($.data,"upgrade-pending.json")}function pe(){let t=null;try{t=_()}catch{return{in_progress:!1}}if(!t)return{in_progress:!1};const e=t.phase??"staged";return{in_progress:!(e==="failed"||e==="rolled_back"),phase:e,from:t.from_version,target:t.target_version}}function ge(){return d(I())}function _e(t,e="failed"){const r=new Date().toISOString().replace(/[:.]/g,""),n=l($.data,`upgrade-pending.${e}-${r}.json`);try{return x(n,JSON.stringify(t,null,2),"utf-8"),n}catch{return null}}function _(){const t=I();if(!d(t))return null;try{return JSON.parse(P(t,"utf-8"))}catch{return null}}function me(t,e,r){const n={from_version:t,target_version:e,upgraded_at:new Date().toISOString(),crash_count:0,transaction_id:Q(),...r?{phase:r}:{}},i=I();E(w(i),{recursive:!0});let a;try{a=$t(i,"wx",384)}catch(o){throw o?.code==="EEXIST"?new f("UPGRADE_IN_PROGRESS","Another connector upgrade transaction is already active"):o}try{x(a,JSON.stringify(n),"utf-8")}catch(o){try{H(i)}catch{}throw o}finally{It(a)}return n}function he(t){const e=t.package_dir;if(!e)return null;const r=u(e);if(!r.includes(`${b}node_modules${b}`))return null;try{const n=JSON.parse(P(l(r,"package.json"),"utf-8"));return n.name!=="grix-connector"||typeof n.version!="string"?null:n.version.trim().replace(/^v/,"")||null}catch{return null}}function Gt(t,e){E(w(t),{recursive:!0});const r=`${t}.tmp-${process.pid}`;x(r,JSON.stringify(e),"utf-8"),h(r,t)}function D(t,e){const r=_();if(!r)throw new f("PENDING_MISSING","Upgrade transaction is missing");if(e&&r.transaction_id!==e)throw new f("UPGRADE_TRANSACTION_CHANGED","Upgrade transaction ownership changed");const n={...r,...t};return Gt(I(),n),n}function j(){return l($.data,Mt)}function Vt(){return l($.data,Ot)}function we(t,e){const r=_();if(!r?.transaction_id||r.phase!=="staged"||e&&r.transaction_id!==e)throw new f("INVALID_UPGRADE_STATE","Windows upgrade is not staged for activation");const n=j();E(w(n),{recursive:!0});const i=`${n}.tmp-${process.pid}`;return x(i,JSON.stringify({transaction_id:r.transaction_id}),"utf-8"),h(i,n),D({phase:"handoff_ready",old_pid:t,old_command_line:Wt(t)??void 0,old_boot_time:Date.now()-kt()*1e3,activation_started_at:new Date().toISOString(),wrapper_pause_file:n},r.transaction_id)}function Wt(t){if(process.platform!=="win32"||!Number.isInteger(t)||t<=0)return null;try{const e=`${process.env.SystemRoot||"C:\\Windows"}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;return R(e,["-NoProfile","-NonInteractive","-WindowStyle","Hidden","-Command",`(Get-CimInstance Win32_Process -Filter 'ProcessId=${t}').CommandLine`],{encoding:"utf-8",timeout:3e3,windowsHide:!0}).trim()||null}catch{return null}}function Se(t,e){const r=_();if(!r?.transaction_id||r.transaction_id!==t||r.phase!=="verifying")throw new f("INVALID_UPGRADE_STATE","Windows upgrade is not awaiting verification");const n=Vt();E(w(n),{recursive:!0});const i=`${n}.tmp-${process.pid}`;x(i,JSON.stringify({transaction_id:t,message:e}),"utf-8"),h(i,n);const a=j();E(w(a),{recursive:!0});const o=`${a}.tmp-${process.pid}`;return x(o,JSON.stringify({transaction_id:t}),"utf-8"),h(o,a),D({phase:"rolling_back",guardian_pid:void 0,error_code:"STARTUP_CRASH",error_msg:e,wrapper_pause_file:a},t)}function ye(){try{g(j(),{force:!0})}catch{}}function Bt(t=_()){if(z(t)){try{g(u(t.staging_prefix),{recursive:!0,force:!0})}catch{}if(ft(t))try{g(u(t.runtime_next_dir),{recursive:!0,force:!0})}catch{}}}function ft(t){if(!t?.runtime_dir||!t.runtime_backup_dir||!t.runtime_next_dir)return!1;const e=u(t.runtime_dir),r=it($.base),n=l(w(w(r)),"runtime"),i=e===u(n),a=e===u(r)&&/^GrixConnectorDaemon-[0-9a-f]{12}$/.test(v(e))&&v(w(e))==="runtimes"&&v(w(w(e)))==="grix-connector";return(i||a)&&u(t.runtime_backup_dir)===`${e}${at}`&&u(t.runtime_next_dir)===`${e}${ct}`}function J(t){return l(u(t),Ct)}function K(t){try{const e=d(t)&&!d(l(t,"owner.json"))?t:l(t,"owner.json"),r=JSON.parse(P(e,"utf-8"));return typeof r.transaction_id=="string"&&typeof r.pending_file=="string"?r:null}catch{return null}}function lt(t){if(!t||!d(t.pending_file))return!1;try{return JSON.parse(P(t.pending_file,"utf-8")).transaction_id===t.transaction_id}catch{return!0}}function Ht(t){const e=_();if(!e?.transaction_id)throw new f("PENDING_MISSING","Upgrade transaction disappeared before npm staging");const r=J(t),n={transaction_id:e.transaction_id,pending_file:I()};D({npm_prefix:u(t),npm_lock_file:r},e.transaction_id);const i=`${r}.candidate-${e.transaction_id}-${Q()}`;try{x(i,JSON.stringify(n),{encoding:"utf-8",flag:"wx"}),xt(i,r),g(i,{force:!0})}catch(a){try{g(i,{force:!0})}catch{}if(!d(r))throw a;const o=K(r);if(o?.transaction_id===e.transaction_id&&u(o.pending_file)===u(I()))return;throw new f(lt(o)?"UPGRADE_IN_PROGRESS":"UPGRADE_LOCK_RECOVERY_REQUIRED",lt(o)?`Another Windows service root is upgrading the shared npm prefix ${u(t)}`:`A stale or incomplete Windows upgrade lock requires recovery at ${r}`)}}function Yt(t=_(),e={}){if(!t?.transaction_id||!t.npm_prefix||!t.npm_lock_file)return!0;const r=J(t.npm_prefix);if(u(t.npm_lock_file)!==r)return!1;if(!d(r))return!0;const n=K(r);if(!n)return!1;if(n.transaction_id!==t.transaction_id||u(n.pending_file)!==u(I()))return!0;const i=e.remove??g,a=e.sleep??(o=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,o)});for(let o=0;;o+=1)try{return i(r,{recursive:!0,force:!0}),!d(r)}catch(c){const s=c?.code;if((s==="EBUSY"||s==="EPERM"||s==="EACCES")&&o<C.length){a(C[o]);continue}return m(`shared npm-prefix lock release failed at ${r}: ${c instanceof Error?c.message:String(c)}`),!1}}function G(t,e){const r=u(t);return u(e)===`${r}${Y}`&&r.includes(`${b}node_modules${b}`)&&v(r)==="grix-connector"}function jt(t,e){const r=et(u(t),u(e));return r!==""&&r!==".."&&!r.startsWith(`..${b}`)}function z(t){if(!t?.npm_prefix||!t.staging_prefix||!t.staged_package_dir)return!1;const e=u(t.npm_prefix),r=u(t.staging_prefix),n=u(t.staged_package_dir);return w(r)===e&&v(r).startsWith(".grix-upgrade-stage-")&&jt(r,n)&&v(w(n))==="node_modules"&&v(n)==="grix-connector"}function Jt(t,e){const r=_();!r||!G(t,e)||D({package_dir:u(t),backup_dir:u(e)},r.transaction_id)}function Kt(t){if(!G(t.packageDir,t.backupDir))throw new f("UNSAFE_UPGRADE_PATH","Refusing to record an unsafe global package path");if(!z({npm_prefix:t.npmPrefix,staging_prefix:t.stagingPrefix,staged_package_dir:t.stagedPackageDir}))throw new f("UNSAFE_UPGRADE_PATH","Refusing to record an unsafe npm staging path");const e=_();if(!e)throw new f("PENDING_MISSING","Upgrade transaction disappeared during staging");let r;try{const n=JSON.parse(P(l(t.packageDir,"package.json"),"utf-8"));if(n.name!=="grix-connector"||typeof n.version!="string")throw new Error("invalid metadata");r=O(n.version)}catch(n){throw new f("VERSION_VERIFY_FAILED",`Unable to identify the active global package before staging: ${n instanceof Error?n.message:String(n)}`)}D({phase:"staged",activation_strategy:t.activationStrategy,package_from_version:r,npm_prefix:u(t.npmPrefix),package_dir:u(t.packageDir),backup_dir:u(t.backupDir),staged_package_dir:u(t.stagedPackageDir),staging_prefix:u(t.stagingPrefix)},e.transaction_id)}function zt(t,e,r={}){const n=_();if(!n?.transaction_id)throw new f("PENDING_MISSING","Upgrade transaction disappeared before runtime staging");const i=it($.base),a=`${i}${at}`,o=`${i}${ct}`;D({runtime_dir:i,runtime_backup_dir:a,runtime_next_dir:o},n.transaction_id);const c=r.copyTree??vt,s=r.copyFile??Pt,p=r.sleep??(k=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,k)});for(let k=0;;k+=1)try{E(w(i),{recursive:!0}),g(o,{recursive:!0,force:!0}),E(o,{recursive:!0}),c(l(t.stagedPackageDir,"dist"),l(o,"dist"),{recursive:!0,force:!0}),s(l(t.stagedPackageDir,"package.json"),l(o,"package.json"));const S=l(t.stagedPackageDir,"node_modules");d(S)&&c(S,l(o,"node_modules"),{recursive:!0,force:!0});const y=U(o,"grix-connector",e);L(o,y);return}catch(S){try{g(o,{recursive:!0,force:!0})}catch{}const y=S?.code;if(y==="ENOSPC")throw new f("DISK_FULL",`Insufficient space for the isolated Windows runtime: ${S.message}`);if((y==="EBUSY"||y==="EPERM"||y==="EACCES"||y==="ENOTEMPTY")&&k<st.length){p(st[k]);continue}throw new f("RUNTIME_STAGE_FAILED",`Could not prepare the isolated Windows runtime before shutdown: ${S instanceof Error?S.message:String(S)}`)}}function V(t,e){if(!d(t))return!0;const r=e.remove??g,n=e.sleep??(i=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,i)});for(let i=0;;i+=1)try{if(r(t,{recursive:!0,force:!0}),!d(t))return!0;throw Object.assign(new Error(`path still exists after removal: ${t}`),{code:"EBUSY"})}catch(a){const o=a?.code;if((o==="EBUSY"||o==="EPERM"||o==="EACCES"||o==="ENOTEMPTY")&&i<C.length){n(C[i]);continue}return m(`upgrade artifact cleanup failed at ${t}: ${a instanceof Error?a.message:String(a)}`),!1}}function Ee(t=_(),e={}){if(t?.transaction_id&&_()?.transaction_id!==t.transaction_id)return!1;let r=!0,n=process.platform!=="win32";if(t?.npm_lock_file||t?.npm_prefix){if(!t.transaction_id||!t.npm_lock_file||!t.npm_prefix)return!1;const o=J(t.npm_prefix);if(u(t.npm_lock_file)!==o)return!1;if(d(o)){const c=K(o);if(c?.transaction_id!==t.transaction_id||u(c.pending_file)!==u(I()))return!1;n=!0}else{if(t.backup_dir&&d(t.backup_dir))return!1;n=!1}}const i=t?.package_dir,a=t?.backup_dir;return n&&i&&a&&G(i,a)&&(r=V(a,e)&&r),z(t)&&(r=V(u(t.staging_prefix),e)&&r),ft(t)&&(r=V(u(t.runtime_backup_dir),e)&&r,r=V(u(t.runtime_next_dir),e)&&r),r}function ke(t=_()){const e=t?.package_dir,r=t?.backup_dir;if(!e||!r||!G(e,r)||!d(r))return!1;const n=`${e}${Ft}`;try{return g(n,{recursive:!0,force:!0}),d(e)&&h(e,n),h(r,e),g(n,{recursive:!0,force:!0}),m(`atomic rollback restored ${e}`),!0}catch(i){try{!d(e)&&d(n)&&h(n,e)}catch{}return m(`atomic rollback failed: ${i instanceof Error?i.message:String(i)}`),!1}}function dt(t,e={}){const r=I();if(t){const n=_();if(!n)return!0;if(n.transaction_id!==t)return!1}if(d(r))try{(e.unlink??H)(r)}catch{return!1}return!d(r)}function Ie(t=_(),e={}){if(!t?.transaction_id)return dt(void 0,{unlink:e.unlinkPending});const r=_();if(r?.transaction_id!==t.transaction_id)return!1;const n=r.phase==="finalizing"?r:D({phase:"finalizing"},t.transaction_id);return Yt(n,{remove:e.removeLock,sleep:e.sleep})?dt(t.transaction_id,{unlink:e.unlinkPending}):!1}function m(t){const e=`[${new Date().toISOString()}] ${t}
|
|
2
|
-
`;try{At(ut(),e)}catch{}}function Pe(t=4096){const e=ut();if(!d(e))return"";try{const r=P(e,"utf-8");return r.length<=t?r:r.slice(-t)}catch{return""}}function pt(){try{const t=M(["--version"]);return R(t.command,t.args,{encoding:"utf-8",timeout:1e4,windowsHide:!0}).trim()}catch{throw new f("NPM_NOT_FOUND","npm is not available or timed out")}}const T=1e6;function gt(){let t;try{t=X(["prefix","-g"])}catch{return T}try{const e=Dt(t),r=Math.floor(Number(e.bavail)*Number(e.bsize)/(1024*1024));return!Number.isFinite(r)||r<0?T:Math.min(r,T)}catch{return T}}function ve(){let t;try{t=pt()}catch(r){return{ok:!1,errorCode:"NPM_NOT_FOUND",errorMsg:r instanceof Error?r.message:"npm not available"}}const e=gt();return e<100?{ok:!1,errorCode:"DISK_FULL",errorMsg:`Only ${e}MB free disk space (need >= 100MB)`,npmVersion:t,diskFreeMb:e}:{ok:!0,npmVersion:t,diskFreeMb:e}}function xe(){let t="";try{t=pt()}catch{}let e=T;try{e=gt()}catch{}return{npm_version:t,node_version:process.version,disk_free_mb:e,platform:process.platform,arch:process.arch,host_name:Rt(),install_id:bt()}}function U(t,e,r){const n=l(t,"package.json");let i;try{i=JSON.parse(P(n,"utf-8"))}catch{throw new f("VERSION_VERIFY_FAILED",`Unable to read staged package metadata: ${n}`)}if(i.name!==e||i.version?.replace(/^v(?=\d)/,"")!==r)throw new f("VERSION_MISMATCH",`Staged package ${String(i.name)}@${String(i.version)} does not match ${e}@${r}`);return i}function Xt(t){return typeof t.bin=="string"?[[v(t.name),t.bin]]:!t.bin||typeof t.bin!="object"?[]:Object.entries(t.bin).filter(e=>typeof e[1]=="string")}function L(t,e){const r=Xt(e);if(r.length===0)throw new f("VERSION_VERIFY_FAILED",`Staged package ${e.name} has no executable bin`);for(const[,n]of r){const i=l(t,n);if(!d(i))throw new f("VERSION_VERIFY_FAILED",`Staged package entrypoint is missing: ${i}`)}return r}function qt(t,e,r){if(process.platform==="win32")return;const n=l(t,"bin");E(n,{recursive:!0});for(const[i,a]of r){const o=l(n,i);if(d(o))continue;const c=`${o}.grix-upgrade-${process.pid}`;try{H(c)}catch{}Nt(et(n,l(e,a)),c),h(c,o)}}function Zt(t){const e=u(t.activePackageDir),r=u(t.stagedPackageDir),n=`${e}${Y}`,i=U(r,t.packageName,t.version),a=L(r,i);d(n)&&(d(e)?g(n,{recursive:!0,force:!0}):h(n,e)),Jt(e,n);let o=!1;try{d(e)&&(h(e,n),o=!0),h(r,e);const c=U(e,t.packageName,t.version),s=L(e,c);return qt(t.npmPrefix,e,s),{packageDir:e,backupDir:n}}catch(c){try{d(e)&&g(e,{recursive:!0,force:!0}),o&&d(n)&&h(n,e)}catch{}throw c}}function X(t,e=1e4){try{const r=M(t);return R(r.command,r.args,{encoding:"utf-8",timeout:e,windowsHide:!0}).trim()}catch{throw new f("NPM_NOT_FOUND",`Unable to resolve npm ${t.join(" ")}`)}}function W(t){return X(t)}const _t=3e5,mt=Tt.find(t=>t.id==="official")?.url??"https://registry.npmjs.org";async function Qt(t,e,r){const n=["view",`${t}@${e}`,"dist.integrity","--registry",r,"--no-audit","--no-fund"],i=nt();i&&n.push("--cache",i);const a=M(n),{stdout:o}=await ot(a.command,a.args,{timeout:1e4,windowsHide:!0});return o.trim()}async function ht(t,e,r,n={}){if(!r)return;let i;try{i=new URL(r).origin}catch{return}if(i===new URL(mt).origin)return;const a=n.fetchIntegrity??Qt;let o;try{o=await a(t,e,mt)}catch(s){m(`integrity cross-check skipped: official registry unreachable (${s instanceof Error?s.message:String(s)})`);return}if(!o){m("integrity cross-check skipped: official registry returned no dist.integrity");return}let c;try{c=await a(t,e,r)}catch(s){m(`integrity cross-check skipped: mirror metadata unreadable (${s instanceof Error?s.message:String(s)})`);return}if(!c){m("integrity cross-check skipped: mirror returned no dist.integrity");return}if(c!==o)throw new f("INTEGRITY_MISMATCH",`Mirror ${r} reports dist.integrity ${c} but official npm reports ${o} for ${t}@${e}`);m(`integrity cross-check passed for ${t}@${e} via ${r}`)}async function wt(t,e,r,n={},i=process.platform==="win32"?"staged":"atomic"){const a=`${t}@${e}`,o=W(["prefix","-g"]),c=W(["root","-g"]);E(o,{recursive:!0}),i==="staged"&&Ht(o);const s=tt(l(o,".grix-upgrade-stage-"));try{m(`staging npm install ${a} under ${s}`);const p=n.installWithMirror??rt,{registry:k,registryUrl:S}=await p(a,r,10*1024*1024,{prefix:s,...i==="staged"?{env:{...process.env,GRIX_UPGRADE_STAGING:"1"}}:{}});await(n.verifyIntegrityConsensus??ht)(t,e,S);const N=W(["root","-g","--prefix",s]),A=l(N,t),B=U(A,t,e),F=L(A,B),St=l(A,F[0][1]),q=R(process.execPath,[St,"--version"],{encoding:"utf-8",timeout:15e3,maxBuffer:2*1024*1024,windowsHide:!0}).trim().replace(/^v(?=\d)/,"");if(q!==e)throw new f("VERSION_MISMATCH",`Staged CLI reported ${q||"<empty>"}, expected ${e}`);const Z=l(c,t);return{npmPrefix:o,packageDir:Z,backupDir:`${u(Z)}${Y}`,stagedPackageDir:A,stagingPrefix:s,registry:k}}catch(p){try{g(s,{recursive:!0,force:!0})}catch{}throw p}}async function te(t,e,r,n){const i=await wt(t,e,r,n,"atomic");try{const a=Zt({packageName:t,version:e,npmPrefix:i.npmPrefix,activePackageDir:i.packageDir,stagedPackageDir:i.stagedPackageDir});m(`atomic package switch succeeded via ${i.registry}; backup=${a.backupDir}`)}finally{try{g(i.stagingPrefix,{recursive:!0,force:!0})}catch{}}}async function $e(t,e,r=_t,n={}){e=O(e);const i=_()?.transaction_id,a=`${t}@${e}`,o=n.activationMode??(process.platform==="win32"?"staged":"atomic");m(`npm install -g ${a} starting (${o} staging with mirror fallback)`);try{if(o==="staged"){const c=await wt(t,e,r,n,o);try{Kt({...c,activationStrategy:process.platform==="win32"?"wrapper-runtime":"service-start"}),process.platform==="win32"&&zt(c,e)}catch(s){try{g(c.stagingPrefix,{recursive:!0,force:!0})}catch{}throw s}return m(`staged package verified via ${c.registry}; waiting for guardian activation`),{activationRequired:!0,packageDir:c.packageDir,backupDir:c.backupDir,stagedPackageDir:c.stagedPackageDir,stagingPrefix:c.stagingPrefix}}else return await te(t,e,r,n),{activationRequired:!1}}catch(c){if(o==="staged"){const p=_();i&&p?.transaction_id===i&&Bt(p)}if(c instanceof f)throw c;const s=c instanceof Error?c.message:String(c);throw m(`npm install failed: ${s}`),/timed out|ETIMEDOUT/i.test(s)?new f("NPM_TIMEOUT",`npm install timed out after ${r/1e3}s (tried all mirrors): ${s}`):s.includes("EACCES")||s.includes("permission denied")?new f("NPM_INSTALL_FAILED",`Permission denied: ${s}`):s.includes("ENOSPC")||s.includes("no space left")?new f("DISK_FULL",`Disk full: ${s}`):s.includes("404")||s.includes("not found")?new f("NPM_INSTALL_FAILED",`Package not found: ${s}`):new f("NPM_INSTALL_FAILED",s)}}function De(t){let e;try{e=X(["root","-g"])}catch{throw new f("VERSION_VERIFY_FAILED","Unable to resolve the global npm package directory")}let r;try{const n=l(e,"grix-connector","package.json"),i=JSON.parse(P(n,"utf-8"));if(i.name!=="grix-connector"||typeof i.version!="string"||!i.version.trim())throw new Error("invalid package metadata");r=i.version.trim().replace(/^v(?=\d)/,"")}catch{throw new f("VERSION_VERIFY_FAILED","Unable to read the installed grix-connector version")}if(r!==t)throw new f("VERSION_MISMATCH",`Installed version ${r} does not match expected ${t}`);return r}async function Ne(t,e){e=O(e);let r=!1;for(const n of await Ut())try{const i=["view",`${t}@${e}`,"version","--registry",n.url,"--no-audit","--no-fund"],a=nt();a&&i.push("--cache",a);const o=M(i),{stdout:c}=await ot(o.command,o.args,{timeout:8e3,windowsHide:!0});if(c.trim())return!0}catch(i){const a=i,o=a.stderr||a.message||String(i);/ETARGET|No matching version|notarget|E404/i.test(o)&&(r=!0)}return!r}async function Ae(t,e,r,n=_t,i={}){r=O(r);const a=`${e}@${r}`,o=l(t,"versions",r);E(l(t,"versions"),{recursive:!0});const c=tt(l(t,"versions",`.stage-${r}-`));m(`installing ${a} into version store under ${c}`);try{const s=i.installWithMirror??rt,{registry:p,registryUrl:k}=await s(a,n,10*1024*1024,{prefix:c,env:{...process.env,GRIX_UPGRADE_STAGING:"1"},ignoreScripts:!0});await(i.verifyIntegrityConsensus??ht)(e,r,k);const y=W(["root","-g","--prefix",c]),N=l(y,e),A=U(N,e,r),B=L(N,A),F=R(process.execPath,[l(N,B[0][1]),"--version"],{encoding:"utf-8",timeout:15e3,maxBuffer:2*1024*1024,windowsHide:!0}).trim().replace(/^v(?=\d)/,"");if(F!==r)throw new f("VERSION_MISMATCH",`Installed CLI reported ${F||"<empty>"}, expected ${r}`);return g(o,{recursive:!0,force:!0}),h(N,o),m(`${a} verified via ${p}; placed at ${o}`),{registry:p,versionDir:o}}catch(s){if(s instanceof f)throw s;const p=s instanceof Error?s.message:String(s);throw m(`version store install failed: ${p}`),/timed out|ETIMEDOUT/i.test(p)?new f("NPM_TIMEOUT",`npm install timed out after ${n/1e3}s (tried all mirrors): ${p}`):p.includes("ENOSPC")||p.includes("no space left")?new f("DISK_FULL",`Disk full: ${p}`):new f("NPM_INSTALL_FAILED",p)}finally{try{g(c,{recursive:!0,force:!0})}catch{}}}export{_t as NPM_INSTALL_TIMEOUT_MS,f as UpgradeError,Ht as acquireNpmPrefixUpgradeLock,_e as archivePendingJournal,we as armWindowsUpgradeHandoff,Se as armWindowsUpgradeRollback,gt as checkDiskSpace,pt as checkNpmAvailable,Ee as cleanupPendingBackup,xe as collectEnvInfo,Zt as commitStagedGlobalPackage,Bt as discardPendingStaging,Ie as finalizePendingTransaction,Pe as getUpgradeLogTail,Ae as installVersionIntoStore,Ne as isVersionAvailable,O as normalizeUpgradeVersion,$e as npmInstall,ge as pendingExists,ve as preflightCheck,zt as prepareStagedWindowsRuntime,_ as readPending,he as readPendingPackageVersion,Yt as releasePendingUpgradeLock,dt as removePending,ye as removeWindowsHandoffFiles,ke as rollbackPendingInstall,D as updatePending,m as upgradeLog,pe as upgradeStatusSnapshot,De as verifyInstalledVersion,ht as verifyMirrorIntegrityConsensus,Vt as windowsRollbackIntentPath,j as windowsWrapperPausePath,me as writePending,Gt as writePendingData};
|
|
1
|
+
import{execFile as Et,execFileSync as M}from"node:child_process";import{randomUUID as Q}from"node:crypto";import{promisify as It}from"node:util";import{uptime as kt}from"node:os";import{closeSync as vt,copyFileSync as Pt,cpSync as xt,existsSync as d,linkSync as $t,mkdirSync as E,mkdtempSync as tt,openSync as Dt,readFileSync as v,renameSync as h,rmSync as _,statfsSync as Nt,symlinkSync as At,unlinkSync as Y,writeFileSync as x}from"node:fs";import{basename as P,dirname as w,join as l,relative as et,resolve as f,sep as R}from"node:path";import{GRIX_PATHS as $}from"../log/index.js";import{appendRotatingFileSync as Rt}from"../log/rotation.js";import{getMachineName as bt}from"../util/host-info.js";import{resolveInstallId as Tt}from"../util/install-id.js";import{NPM_REGISTRIES as Ut,npmInstallWithMirror as rt,probeRegistries as Mt,resolveNpmCacheDir as nt,resolveNpmInvocation as O}from"../installer/npm-registry.js";import{resolveWindowsServiceRuntimeDir as it}from"../../service/service-paths.js";const ot=It(Et);class u extends Error{code;constructor(e,r){super(r),this.name="UpgradeError",this.code=e}}const Ot=/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;function F(t){const e=String(t??"").trim().replace(/^v(?=\d)/,"");if(!Ot.test(e))throw new u("INVALID_VERSION",`Invalid connector upgrade version: ${String(t)}`);return e}const B=".grix-upgrade-backup",Ft=".grix-upgrade-failed",at=".grix-upgrade-backup",ct=".grix-upgrade-next",Lt="upgrade-wrapper.pause.json",Ct="upgrade-rollback.intent.json",Vt=".grix-connector-upgrade.lock",st=[250,750],L=[100,250,750,1500];function ut(){return l($.log,"upgrade.log")}function k(){return l($.data,"upgrade-pending.json")}function me(){let t=null;try{t=m()}catch{return{in_progress:!1}}if(!t)return{in_progress:!1};const e=t.phase??"staged";return{in_progress:!(e==="failed"||e==="rolled_back"),phase:e,from:t.from_version,target:t.target_version}}function he(){return d(k())}function we(t,e="failed"){const r=new Date().toISOString().replace(/[:.]/g,""),n=l($.data,`upgrade-pending.${e}-${r}.json`);try{return x(n,JSON.stringify(t,null,2),"utf-8"),n}catch{return null}}function m(){const t=k();if(!d(t))return null;try{return JSON.parse(v(t,"utf-8"))}catch{return null}}function ye(t,e,r){const n={from_version:t,target_version:e,upgraded_at:new Date().toISOString(),crash_count:0,transaction_id:Q(),...r?{phase:r}:{}},i=k();E(w(i),{recursive:!0});let c;try{c=Dt(i,"wx",384)}catch(o){throw o?.code==="EEXIST"?new u("UPGRADE_IN_PROGRESS","Another connector upgrade transaction is already active"):o}try{x(c,JSON.stringify(n),"utf-8")}catch(o){try{Y(i)}catch{}throw o}finally{vt(c)}return n}function Se(t){const e=t.package_dir;if(!e)return null;const r=f(e);if(!r.includes(`${R}node_modules${R}`))return null;try{const n=JSON.parse(v(l(r,"package.json"),"utf-8"));return n.name!=="grix-connector"||typeof n.version!="string"?null:n.version.trim().replace(/^v/,"")||null}catch{return null}}function Gt(t,e){E(w(t),{recursive:!0});const r=`${t}.tmp-${process.pid}`;x(r,JSON.stringify(e),"utf-8"),h(r,t)}function D(t,e){const r=m();if(!r)throw new u("PENDING_MISSING","Upgrade transaction is missing");if(e&&r.transaction_id!==e)throw new u("UPGRADE_TRANSACTION_CHANGED","Upgrade transaction ownership changed");const n={...r,...t};return Gt(k(),n),n}function H(){return l($.data,Lt)}function Wt(){return l($.data,Ct)}function Ee(t,e){const r=m();if(!r?.transaction_id||r.phase!=="staged"||e&&r.transaction_id!==e)throw new u("INVALID_UPGRADE_STATE","Windows upgrade is not staged for activation");const n=H();E(w(n),{recursive:!0});const i=`${n}.tmp-${process.pid}`;return x(i,JSON.stringify({transaction_id:r.transaction_id}),"utf-8"),h(i,n),D({phase:"handoff_ready",old_pid:t,old_command_line:Yt(t)??void 0,old_boot_time:Date.now()-kt()*1e3,activation_started_at:new Date().toISOString(),wrapper_pause_file:n},r.transaction_id)}function Yt(t){if(process.platform!=="win32"||!Number.isInteger(t)||t<=0)return null;try{const e=`${process.env.SystemRoot||"C:\\Windows"}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;return M(e,["-NoProfile","-NonInteractive","-WindowStyle","Hidden","-Command",`(Get-CimInstance Win32_Process -Filter 'ProcessId=${t}').CommandLine`],{encoding:"utf-8",timeout:3e3,windowsHide:!0}).trim()||null}catch{return null}}function Ie(t,e){const r=m();if(!r?.transaction_id||r.transaction_id!==t||r.phase!=="verifying")throw new u("INVALID_UPGRADE_STATE","Windows upgrade is not awaiting verification");const n=Wt();E(w(n),{recursive:!0});const i=`${n}.tmp-${process.pid}`;x(i,JSON.stringify({transaction_id:t,message:e}),"utf-8"),h(i,n);const c=H();E(w(c),{recursive:!0});const o=`${c}.tmp-${process.pid}`;return x(o,JSON.stringify({transaction_id:t}),"utf-8"),h(o,c),D({phase:"rolling_back",guardian_pid:void 0,error_code:"STARTUP_CRASH",error_msg:e,wrapper_pause_file:c},t)}function ke(){try{_(H(),{force:!0})}catch{}}function Bt(t=m()){if(K(t)){try{_(f(t.staging_prefix),{recursive:!0,force:!0})}catch{}if(ft(t))try{_(f(t.runtime_next_dir),{recursive:!0,force:!0})}catch{}}}function ft(t){if(!t?.runtime_dir||!t.runtime_backup_dir||!t.runtime_next_dir)return!1;const e=f(t.runtime_dir),r=it($.base),n=l(w(w(r)),"runtime"),i=e===f(n),c=e===f(r)&&/^GrixConnectorDaemon-[0-9a-f]{12}$/.test(P(e))&&P(w(e))==="runtimes"&&P(w(w(e)))==="grix-connector";return(i||c)&&f(t.runtime_backup_dir)===`${e}${at}`&&f(t.runtime_next_dir)===`${e}${ct}`}function j(t){return l(f(t),Vt)}function J(t){try{const e=d(t)&&!d(l(t,"owner.json"))?t:l(t,"owner.json"),r=JSON.parse(v(e,"utf-8"));return typeof r.transaction_id=="string"&&typeof r.pending_file=="string"?r:null}catch{return null}}function lt(t){if(!t||!d(t.pending_file))return!1;try{return JSON.parse(v(t.pending_file,"utf-8")).transaction_id===t.transaction_id}catch{return!0}}function Ht(t){const e=m();if(!e?.transaction_id)throw new u("PENDING_MISSING","Upgrade transaction disappeared before npm staging");const r=j(t),n={transaction_id:e.transaction_id,pending_file:k()};D({npm_prefix:f(t),npm_lock_file:r},e.transaction_id);const i=`${r}.candidate-${e.transaction_id}-${Q()}`;try{x(i,JSON.stringify(n),{encoding:"utf-8",flag:"wx"}),$t(i,r),_(i,{force:!0})}catch(c){try{_(i,{force:!0})}catch{}if(!d(r))throw c;const o=J(r);if(o?.transaction_id===e.transaction_id&&f(o.pending_file)===f(k()))return;throw new u(lt(o)?"UPGRADE_IN_PROGRESS":"UPGRADE_LOCK_RECOVERY_REQUIRED",lt(o)?`Another Windows service root is upgrading the shared npm prefix ${f(t)}`:`A stale or incomplete Windows upgrade lock requires recovery at ${r}`)}}function jt(t=m(),e={}){if(!t?.transaction_id||!t.npm_prefix||!t.npm_lock_file)return!0;const r=j(t.npm_prefix);if(f(t.npm_lock_file)!==r)return!1;if(!d(r))return!0;const n=J(r);if(!n)return!1;if(n.transaction_id!==t.transaction_id||f(n.pending_file)!==f(k()))return!0;const i=e.remove??_,c=e.sleep??(o=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,o)});for(let o=0;;o+=1)try{return i(r,{recursive:!0,force:!0}),!d(r)}catch(a){const s=a?.code;if((s==="EBUSY"||s==="EPERM"||s==="EACCES")&&o<L.length){c(L[o]);continue}return p(`shared npm-prefix lock release failed at ${r}: ${a instanceof Error?a.message:String(a)}`),!1}}function C(t,e){const r=f(t);return f(e)===`${r}${B}`&&r.includes(`${R}node_modules${R}`)&&P(r)==="grix-connector"}function Jt(t,e){const r=et(f(t),f(e));return r!==""&&r!==".."&&!r.startsWith(`..${R}`)}function K(t){if(!t?.npm_prefix||!t.staging_prefix||!t.staged_package_dir)return!1;const e=f(t.npm_prefix),r=f(t.staging_prefix),n=f(t.staged_package_dir);return w(r)===e&&P(r).startsWith(".grix-upgrade-stage-")&&Jt(r,n)&&P(w(n))==="node_modules"&&P(n)==="grix-connector"}function Kt(t,e){const r=m();!r||!C(t,e)||D({package_dir:f(t),backup_dir:f(e)},r.transaction_id)}function zt(t){if(!C(t.packageDir,t.backupDir))throw new u("UNSAFE_UPGRADE_PATH","Refusing to record an unsafe global package path");if(!K({npm_prefix:t.npmPrefix,staging_prefix:t.stagingPrefix,staged_package_dir:t.stagedPackageDir}))throw new u("UNSAFE_UPGRADE_PATH","Refusing to record an unsafe npm staging path");const e=m();if(!e)throw new u("PENDING_MISSING","Upgrade transaction disappeared during staging");let r;try{const n=JSON.parse(v(l(t.packageDir,"package.json"),"utf-8"));if(n.name!=="grix-connector"||typeof n.version!="string")throw new Error("invalid metadata");r=F(n.version)}catch(n){throw new u("VERSION_VERIFY_FAILED",`Unable to identify the active global package before staging: ${n instanceof Error?n.message:String(n)}`)}D({phase:"staged",activation_strategy:t.activationStrategy,package_from_version:r,npm_prefix:f(t.npmPrefix),package_dir:f(t.packageDir),backup_dir:f(t.backupDir),staged_package_dir:f(t.stagedPackageDir),staging_prefix:f(t.stagingPrefix)},e.transaction_id)}function Xt(t,e,r={}){const n=m();if(!n?.transaction_id)throw new u("PENDING_MISSING","Upgrade transaction disappeared before runtime staging");const i=it($.base),c=`${i}${at}`,o=`${i}${ct}`;D({runtime_dir:i,runtime_backup_dir:c,runtime_next_dir:o},n.transaction_id);const a=r.copyTree??xt,s=r.copyFile??Pt,g=r.sleep??(I=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,I)});for(let I=0;;I+=1)try{E(w(i),{recursive:!0}),_(o,{recursive:!0,force:!0}),E(o,{recursive:!0}),a(l(t.stagedPackageDir,"dist"),l(o,"dist"),{recursive:!0,force:!0}),s(l(t.stagedPackageDir,"package.json"),l(o,"package.json"));const y=l(t.stagedPackageDir,"node_modules");d(y)&&a(y,l(o,"node_modules"),{recursive:!0,force:!0});const S=T(o,"grix-connector",e);U(o,S);return}catch(y){try{_(o,{recursive:!0,force:!0})}catch{}const S=y?.code;if(S==="ENOSPC")throw new u("DISK_FULL",`Insufficient space for the isolated Windows runtime: ${y.message}`);if((S==="EBUSY"||S==="EPERM"||S==="EACCES"||S==="ENOTEMPTY")&&I<st.length){g(st[I]);continue}throw new u("RUNTIME_STAGE_FAILED",`Could not prepare the isolated Windows runtime before shutdown: ${y instanceof Error?y.message:String(y)}`)}}function V(t,e){if(!d(t))return!0;const r=e.remove??_,n=e.sleep??(i=>{Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,i)});for(let i=0;;i+=1)try{if(r(t,{recursive:!0,force:!0}),!d(t))return!0;throw Object.assign(new Error(`path still exists after removal: ${t}`),{code:"EBUSY"})}catch(c){const o=c?.code;if((o==="EBUSY"||o==="EPERM"||o==="EACCES"||o==="ENOTEMPTY")&&i<L.length){n(L[i]);continue}return p(`upgrade artifact cleanup failed at ${t}: ${c instanceof Error?c.message:String(c)}`),!1}}function ve(t=m(),e={}){if(t?.transaction_id&&m()?.transaction_id!==t.transaction_id)return!1;let r=!0,n=process.platform!=="win32";if(t?.npm_lock_file||t?.npm_prefix){if(!t.transaction_id||!t.npm_lock_file||!t.npm_prefix)return!1;const o=j(t.npm_prefix);if(f(t.npm_lock_file)!==o)return!1;if(d(o)){const a=J(o);if(a?.transaction_id!==t.transaction_id||f(a.pending_file)!==f(k()))return!1;n=!0}else{if(t.backup_dir&&d(t.backup_dir))return!1;n=!1}}const i=t?.package_dir,c=t?.backup_dir;return n&&i&&c&&C(i,c)&&(r=V(c,e)&&r),K(t)&&(r=V(f(t.staging_prefix),e)&&r),ft(t)&&(r=V(f(t.runtime_backup_dir),e)&&r,r=V(f(t.runtime_next_dir),e)&&r),r}function Pe(t=m()){const e=t?.package_dir,r=t?.backup_dir;if(!e||!r||!C(e,r)||!d(r))return!1;const n=`${e}${Ft}`;try{return _(n,{recursive:!0,force:!0}),d(e)&&h(e,n),h(r,e),_(n,{recursive:!0,force:!0}),p(`atomic rollback restored ${e}`),!0}catch(i){try{!d(e)&&d(n)&&h(n,e)}catch{}return p(`atomic rollback failed: ${i instanceof Error?i.message:String(i)}`),!1}}function dt(t,e={}){const r=k();if(t){const n=m();if(!n)return!0;if(n.transaction_id!==t)return!1}if(d(r))try{(e.unlink??Y)(r)}catch{return!1}return!d(r)}function xe(t=m(),e={}){if(!t?.transaction_id)return dt(void 0,{unlink:e.unlinkPending});const r=m();if(r?.transaction_id!==t.transaction_id)return!1;const n=r.phase==="finalizing"?r:D({phase:"finalizing"},t.transaction_id);return jt(n,{remove:e.removeLock,sleep:e.sleep})?dt(t.transaction_id,{unlink:e.unlinkPending}):!1}function p(t){const e=`[${new Date().toISOString()}] ${t}
|
|
2
|
+
`;try{Rt(ut(),e)}catch{}}function $e(t=4096){const e=ut();if(!d(e))return"";try{const r=v(e,"utf-8");return r.length<=t?r:r.slice(-t)}catch{return""}}const z=1e4;function gt(){let t;try{t=O(["--version"])}catch(n){throw new u("NPM_NOT_FOUND",`npm is not available: ${n instanceof Error?n.message:String(n)}`)}let e;for(let n=1;n<=2;n++)try{return M(t.command,t.args,{encoding:"utf-8",timeout:z,windowsHide:!0}).trim()}catch(i){const c=i instanceof Error?i.message:String(i);if(!/timed out|ETIMEDOUT/i.test(c))throw new u("NPM_NOT_FOUND",`npm is not available: ${c}`);e=i,n===1&&p(`npm availability check timed out after ${z/1e3}s (cold start, likely AV scan); retrying once`)}const r=e instanceof Error?e.message:String(e);throw new u("NPM_CHECK_TIMEOUT",`npm availability check ("npm --version") timed out twice in a row (${z/1e3}s each); npm may well be installed \u2014 this looks like a slow cold start (e.g. antivirus scanning), not a missing npm: ${r}`)}const b=1e6;function pt(){let t;try{t=X(["prefix","-g"])}catch{return b}try{const e=Nt(t),r=Math.floor(Number(e.bavail)*Number(e.bsize)/(1024*1024));return!Number.isFinite(r)||r<0?b:Math.min(r,b)}catch{return b}}function De(){let t;try{t=gt()}catch(r){return{ok:!1,errorCode:r instanceof u?r.code:"NPM_NOT_FOUND",errorMsg:r instanceof Error?r.message:"npm not available"}}const e=pt();return e<100?{ok:!1,errorCode:"DISK_FULL",errorMsg:`Only ${e}MB free disk space (need >= 100MB)`,npmVersion:t,diskFreeMb:e}:{ok:!0,npmVersion:t,diskFreeMb:e}}function Ne(){let t="";try{t=gt()}catch{}let e=b;try{e=pt()}catch{}return{npm_version:t,node_version:process.version,disk_free_mb:e,platform:process.platform,arch:process.arch,host_name:bt(),install_id:Tt()}}function T(t,e,r){const n=l(t,"package.json");let i;try{i=JSON.parse(v(n,"utf-8"))}catch{throw new u("VERSION_VERIFY_FAILED",`Unable to read staged package metadata: ${n}`)}if(i.name!==e||i.version?.replace(/^v(?=\d)/,"")!==r)throw new u("VERSION_MISMATCH",`Staged package ${String(i.name)}@${String(i.version)} does not match ${e}@${r}`);return i}function qt(t){return typeof t.bin=="string"?[[P(t.name),t.bin]]:!t.bin||typeof t.bin!="object"?[]:Object.entries(t.bin).filter(e=>typeof e[1]=="string")}function U(t,e){const r=qt(e);if(r.length===0)throw new u("VERSION_VERIFY_FAILED",`Staged package ${e.name} has no executable bin`);for(const[,n]of r){const i=l(t,n);if(!d(i))throw new u("VERSION_VERIFY_FAILED",`Staged package entrypoint is missing: ${i}`)}return r}function Zt(t,e,r){if(process.platform==="win32")return;const n=l(t,"bin");E(n,{recursive:!0});for(const[i,c]of r){const o=l(n,i);if(d(o))continue;const a=`${o}.grix-upgrade-${process.pid}`;try{Y(a)}catch{}At(et(n,l(e,c)),a),h(a,o)}}function Qt(t){const e=f(t.activePackageDir),r=f(t.stagedPackageDir),n=`${e}${B}`,i=T(r,t.packageName,t.version),c=U(r,i);d(n)&&(d(e)?_(n,{recursive:!0,force:!0}):h(n,e)),Kt(e,n);let o=!1;try{d(e)&&(h(e,n),o=!0),h(r,e);const a=T(e,t.packageName,t.version),s=U(e,a);return Zt(t.npmPrefix,e,s),{packageDir:e,backupDir:n}}catch(a){try{d(e)&&_(e,{recursive:!0,force:!0}),o&&d(n)&&h(n,e)}catch{}throw a}}function X(t,e=1e4){try{const r=O(t);return M(r.command,r.args,{encoding:"utf-8",timeout:e,windowsHide:!0}).trim()}catch{throw new u("NPM_NOT_FOUND",`Unable to resolve npm ${t.join(" ")}`)}}function G(t){return X(t)}const _t=3e5,mt=Ut.find(t=>t.id==="official")?.url??"https://registry.npmjs.org";async function te(t,e,r){const n=["view",`${t}@${e}`,"dist.integrity","--registry",r,"--no-audit","--no-fund"],i=nt();i&&n.push("--cache",i);const c=O(n),{stdout:o}=await ot(c.command,c.args,{timeout:1e4,windowsHide:!0});return o.trim()}async function ht(t,e,r,n={}){if(!r)return;let i;try{i=new URL(r).origin}catch{return}if(i===new URL(mt).origin)return;const c=n.fetchIntegrity??te;let o;try{o=await c(t,e,mt)}catch(s){p(`integrity cross-check skipped: official registry unreachable (${s instanceof Error?s.message:String(s)})`);return}if(!o){p("integrity cross-check skipped: official registry returned no dist.integrity");return}let a;try{a=await c(t,e,r)}catch(s){p(`integrity cross-check skipped: mirror metadata unreadable (${s instanceof Error?s.message:String(s)})`);return}if(!a){p("integrity cross-check skipped: mirror returned no dist.integrity");return}if(a!==o)throw new u("INTEGRITY_MISMATCH",`Mirror ${r} reports dist.integrity ${a} but official npm reports ${o} for ${t}@${e}`);p(`integrity cross-check passed for ${t}@${e} via ${r}`)}const ee=15e3;function wt(t,e,r=ee){const n=()=>M(process.execPath,[t,"--version"],{encoding:"utf-8",timeout:r,maxBuffer:2097152,windowsHide:!0}).trim().replace(/^v(?=\d)/,"");let i;for(let o=1;o<=2;o++)try{const a=n();if(a!==e)throw new u("VERSION_MISMATCH",`Staged CLI reported ${a||"<empty>"}, expected ${e}`);return a}catch(a){if(a instanceof u)throw a;const s=a instanceof Error?a.message:String(a);if(!/timed out|ETIMEDOUT/i.test(s))throw new u("VERSION_VERIFY_FAILED",`Post-install version check failed to run "${t} --version": ${s}`);i=a,o===1&&p(`staged CLI version check timed out after ${r/1e3}s (cold start, likely AV scan of a new executable); retrying once`)}const c=i instanceof Error?i.message:String(i);throw new u("VERSION_VERIFY_TIMEOUT",`Post-install version check ("${t} --version") timed out twice in a row (${r/1e3}s each); the npm install itself already succeeded. This looks like a slow cold start (e.g. antivirus scanning a newly written executable), not a network or npm-install problem: ${c}`)}async function yt(t,e,r,n={},i=process.platform==="win32"?"staged":"atomic"){const c=`${t}@${e}`,o=G(["prefix","-g"]),a=G(["root","-g"]);E(o,{recursive:!0}),i==="staged"&&Ht(o);const s=tt(l(o,".grix-upgrade-stage-"));try{p(`staging npm install ${c} under ${s}`);const g=n.installWithMirror??rt,{registry:I,registryUrl:y}=await g(c,r,10*1024*1024,{prefix:s,...i==="staged"?{env:{...process.env,GRIX_UPGRADE_STAGING:"1"}}:{}});await(n.verifyIntegrityConsensus??ht)(t,e,y);const N=G(["root","-g","--prefix",s]),A=l(N,t),W=T(A,t,e),q=U(A,W),St=l(A,q[0][1]);(n.verifyStagedCliVersion??wt)(St,e);const Z=l(a,t);return{npmPrefix:o,packageDir:Z,backupDir:`${f(Z)}${B}`,stagedPackageDir:A,stagingPrefix:s,registry:I}}catch(g){try{_(s,{recursive:!0,force:!0})}catch{}throw g}}async function re(t,e,r,n){const i=await yt(t,e,r,n,"atomic");try{const c=Qt({packageName:t,version:e,npmPrefix:i.npmPrefix,activePackageDir:i.packageDir,stagedPackageDir:i.stagedPackageDir});p(`atomic package switch succeeded via ${i.registry}; backup=${c.backupDir}`)}finally{try{_(i.stagingPrefix,{recursive:!0,force:!0})}catch{}}}async function Ae(t,e,r=_t,n={}){e=F(e);const i=m()?.transaction_id,c=`${t}@${e}`,o=n.activationMode??(process.platform==="win32"?"staged":"atomic");p(`npm install -g ${c} starting (${o} staging with mirror fallback)`);try{if(o==="staged"){const a=await yt(t,e,r,n,o);try{zt({...a,activationStrategy:process.platform==="win32"?"wrapper-runtime":"service-start"}),process.platform==="win32"&&Xt(a,e)}catch(s){try{_(a.stagingPrefix,{recursive:!0,force:!0})}catch{}throw s}return p(`staged package verified via ${a.registry}; waiting for guardian activation`),{activationRequired:!0,packageDir:a.packageDir,backupDir:a.backupDir,stagedPackageDir:a.stagedPackageDir,stagingPrefix:a.stagingPrefix}}else return await re(t,e,r,n),{activationRequired:!1}}catch(a){if(o==="staged"){const g=m();i&&g?.transaction_id===i&&Bt(g)}if(a instanceof u)throw a;const s=a instanceof Error?a.message:String(a);throw p(`npm install failed: ${s}`),/timed out|ETIMEDOUT/i.test(s)?new u("NPM_TIMEOUT",`npm install timed out after ${r/1e3}s (tried all mirrors): ${s}`):s.includes("EACCES")||s.includes("permission denied")?new u("NPM_INSTALL_FAILED",`Permission denied: ${s}`):s.includes("ENOSPC")||s.includes("no space left")?new u("DISK_FULL",`Disk full: ${s}`):s.includes("404")||s.includes("not found")?new u("NPM_INSTALL_FAILED",`Package not found: ${s}`):new u("NPM_INSTALL_FAILED",s)}}function Re(t){let e;try{e=X(["root","-g"])}catch{throw new u("VERSION_VERIFY_FAILED","Unable to resolve the global npm package directory")}let r;try{const n=l(e,"grix-connector","package.json"),i=JSON.parse(v(n,"utf-8"));if(i.name!=="grix-connector"||typeof i.version!="string"||!i.version.trim())throw new Error("invalid package metadata");r=i.version.trim().replace(/^v(?=\d)/,"")}catch{throw new u("VERSION_VERIFY_FAILED","Unable to read the installed grix-connector version")}if(r!==t)throw new u("VERSION_MISMATCH",`Installed version ${r} does not match expected ${t}`);return r}async function be(t,e){e=F(e);let r=!1;for(const n of await Mt())try{const i=["view",`${t}@${e}`,"version","--registry",n.url,"--no-audit","--no-fund"],c=nt();c&&i.push("--cache",c);const o=O(i),{stdout:a}=await ot(o.command,o.args,{timeout:8e3,windowsHide:!0});if(a.trim())return!0}catch(i){const c=i,o=c.stderr||c.message||String(i);/ETARGET|No matching version|notarget|E404/i.test(o)&&(r=!0)}return!r}async function Te(t,e,r,n=_t,i={}){r=F(r);const c=`${e}@${r}`,o=l(t,"versions",r);E(l(t,"versions"),{recursive:!0});const a=tt(l(t,"versions",`.stage-${r}-`));p(`installing ${c} into version store under ${a}`);try{const s=i.installWithMirror??rt,{registry:g,registryUrl:I}=await s(c,n,10*1024*1024,{prefix:a,env:{...process.env,GRIX_UPGRADE_STAGING:"1"},ignoreScripts:!0});await(i.verifyIntegrityConsensus??ht)(e,r,I);const S=G(["root","-g","--prefix",a]),N=l(S,e),A=T(N,e,r),W=U(N,A);return(i.verifyStagedCliVersion??wt)(l(N,W[0][1]),r),_(o,{recursive:!0,force:!0}),h(N,o),p(`${c} verified via ${g}; placed at ${o}`),{registry:g,versionDir:o}}catch(s){if(s instanceof u)throw s;const g=s instanceof Error?s.message:String(s);throw p(`version store install failed: ${g}`),/timed out|ETIMEDOUT/i.test(g)?new u("NPM_TIMEOUT",`npm install timed out after ${n/1e3}s (tried all mirrors): ${g}`):g.includes("ENOSPC")||g.includes("no space left")?new u("DISK_FULL",`Disk full: ${g}`):new u("NPM_INSTALL_FAILED",g)}finally{try{_(a,{recursive:!0,force:!0})}catch{}}}export{_t as NPM_INSTALL_TIMEOUT_MS,u as UpgradeError,Ht as acquireNpmPrefixUpgradeLock,we as archivePendingJournal,Ee as armWindowsUpgradeHandoff,Ie as armWindowsUpgradeRollback,pt as checkDiskSpace,gt as checkNpmAvailable,ve as cleanupPendingBackup,Ne as collectEnvInfo,Qt as commitStagedGlobalPackage,Bt as discardPendingStaging,xe as finalizePendingTransaction,$e as getUpgradeLogTail,Te as installVersionIntoStore,be as isVersionAvailable,F as normalizeUpgradeVersion,Ae as npmInstall,he as pendingExists,De as preflightCheck,Xt as prepareStagedWindowsRuntime,m as readPending,Se as readPendingPackageVersion,jt as releasePendingUpgradeLock,dt as removePending,ke as removeWindowsHandoffFiles,Pe as rollbackPendingInstall,D as updatePending,p as upgradeLog,me as upgradeStatusSnapshot,Re as verifyInstalledVersion,ht as verifyMirrorIntegrityConsensus,wt as verifyStagedCliVersion,Wt as windowsRollbackIntentPath,H as windowsWrapperPausePath,ye as writePending,Gt as writePendingData};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "grix-connector",
|
|
3
|
-
"version": "4.11.
|
|
3
|
+
"version": "4.11.2",
|
|
4
4
|
"description": "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.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|