prjct-cli 3.80.1 → 3.82.0

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.
@@ -13,22 +13,22 @@ function isSafeRetry(e){const c=e&&e.code||"",m=e&&e.message||"";return c==="ECO
13
13
  // response raw. Hooks must never disturb the host session, so ANY failure
14
14
  // (connect error, timeout, closed socket) degrades to the empty no-op {} and
15
15
  // exit 0 — the same fail-soft contract the in-process hook runner honors.
16
- let hookDone=false;
16
+ const hookCompletion=new AbortController();
17
17
  function sendHook(sub,data){
18
- if(hookDone)return;hookDone=true;
18
+ if(hookCompletion.signal.aborted)return;hookCompletion.abort();
19
19
  const msg=JSON.stringify({id:randomUUID(),command:"hook",args:sub?[sub]:[],options:{},cwd:process.cwd(),stdin:data})+"\n";
20
- const sock=connect(sockPath);let buf="",done=false;
21
- const soft=()=>{if(!done){done=true;clearTimeout(t);sock.destroy();process.stdout.write("{}\n");process.exit(0)}};
20
+ const sock=connect(sockPath);const chunks=[],completion=new AbortController();
21
+ const soft=()=>{if(!completion.signal.aborted){completion.abort();clearTimeout(t);sock.destroy();process.stdout.write("{}\n");process.exit(0)}};
22
22
  const t=setTimeout(soft,5000);
23
23
  sock.on("connect",()=>sock.write(msg));
24
- sock.on("data",c=>{buf+=c.toString();const n=buf.indexOf("\n");if(n!==-1){const r=JSON.parse(buf.slice(0,n));if(r.retry){soft();return}done=true;clearTimeout(t);sock.end();if(r.stdout)process.stdout.write(r.stdout);process.exit(r.exitCode!=null?r.exitCode:0)}});
24
+ sock.on("data",c=>{chunks.push(c.toString());const buf=chunks.join("");const n=buf.indexOf("\n");if(n!==-1){const r=JSON.parse(buf.slice(0,n));if(r.retry){soft();return}completion.abort();clearTimeout(t);sock.end();if(r.stdout)process.stdout.write(r.stdout);process.exit(r.exitCode!=null?r.exitCode:0)}});
25
25
  sock.on("error",soft);
26
26
  sock.on("close",soft);
27
27
  }
28
28
  if(cmd==="hook"){
29
29
  if(process.env.PRJCT_NO_DAEMON!=="1"&&hasEndpoint()){
30
30
  const sub=args[1];
31
- if(process.stdin.isTTY){sendHook(sub,"")}else{let d="";process.stdin.on("data",c=>d+=c);process.stdin.on("end",()=>sendHook(sub,d));process.stdin.on("error",()=>sendHook(sub,d));setTimeout(()=>sendHook(sub,d),1000)}
31
+ if(process.stdin.isTTY){sendHook(sub,"")}else{const chunks=[];process.stdin.on("data",c=>chunks.push(c));const send=()=>sendHook(sub,Buffer.concat(chunks).toString("utf8"));process.stdin.on("end",send);process.stdin.on("error",send);setTimeout(send,1000)}
32
32
  }else{
33
33
  // Cold path (daemon disabled/unreachable): run the hook from the dedicated
34
34
  // hooks bundle, NOT the full core. cold-entry emits host JSON then detaches
@@ -37,16 +37,16 @@ if(cmd==="hook"){
37
37
  }
38
38
  }else if(cmd&&!skip.has(cmd)&&process.env.PRJCT_NO_DAEMON!=="1"&&hasEndpoint()){
39
39
  const cArgs=[],cOpts={};
40
- for(let i=0;i<args.length;i++){const a=args[i];if(a.startsWith("--")){const r=a.slice(2);if(r.includes("=")){const e=r.indexOf("=");cOpts[r.slice(0,e)]=r.slice(e+1)}else if(i+1<args.length&&!args[i+1].startsWith("--")){cOpts[r]=args[++i]}else{cOpts[r]=true}}else if(a.startsWith("-")&&a.length===2){cOpts[a.slice(1)]=true}else if(i>0){cArgs.push(a)}}
40
+ const consumed=new Set();for(const [i,a] of args.entries()){if(consumed.has(i))continue;if(a.startsWith("--")){const r=a.slice(2);if(r.includes("=")){const e=r.indexOf("=");cOpts[r.slice(0,e)]=r.slice(e+1)}else if(i+1<args.length&&!args[i+1].startsWith("--")){cOpts[r]=args[i+1];consumed.add(i+1)}else{cOpts[r]=true}}else if(a.startsWith("-")&&a.length===2){cOpts[a.slice(1)]=true}else if(i>0){cArgs.push(a)}}
41
41
  const msg=JSON.stringify({id:randomUUID(),command:cmd,args:cArgs,options:cOpts,cwd:process.cwd()})+"\n";
42
- const sock=connect(sockPath);let buf="",done=false;
42
+ const sock=connect(sockPath);const chunks=[],completion=new AbortController();
43
43
  // Long verbs (ship/sync/…) need 10min; everything else stays at 30s.
44
44
  const LONG=new Set(["ship","sync","dream","update","upgrade","analyze","init","cloud"]);
45
45
  const waitMs=LONG.has(cmd)?600000:30000;
46
- const t=setTimeout(()=>{if(!done){done=true;sock.destroy();refuse("timed out")}},waitMs);
46
+ const t=setTimeout(()=>{if(!completion.signal.aborted){completion.abort();sock.destroy();refuse("timed out")}},waitMs);
47
47
  sock.on("connect",()=>sock.write(msg));
48
- sock.on("data",c=>{buf+=c.toString();const n=buf.indexOf("\n");if(n!==-1){const r=JSON.parse(buf.slice(0,n));done=true;clearTimeout(t);sock.end();if(r.retry){process.env.PRJCT_NO_DAEMON="1";fallback();return}if(r.stdout)console.log(r.stdout);if(r.stderr)console.error(r.stderr);process.exit(r.exitCode)}});
49
- sock.on("error",e=>{if(!done){done=true;clearTimeout(t);if(isSafeRetry(e))fallback();else refuse(e&&e.message||String(e))}});
50
- sock.on("close",()=>{if(!done){done=true;clearTimeout(t);refuse("Connection closed before response")}});
48
+ sock.on("data",c=>{chunks.push(c.toString());const buf=chunks.join("");const n=buf.indexOf("\n");if(n!==-1){const r=JSON.parse(buf.slice(0,n));completion.abort();clearTimeout(t);sock.end();if(r.retry){process.env.PRJCT_NO_DAEMON="1";fallback();return}if(r.stdout)console.log(r.stdout);if(r.stderr)console.error(r.stderr);process.exit(r.exitCode)}});
49
+ sock.on("error",e=>{if(!completion.signal.aborted){completion.abort();clearTimeout(t);if(isSafeRetry(e))fallback();else refuse(e&&e.message||String(e))}});
50
+ sock.on("close",()=>{if(!completion.signal.aborted){completion.abort();clearTimeout(t);refuse("Connection closed before response")}});
51
51
  }else{fallback()}
52
52
  async function fallback(){await import("./prjct-core.mjs")}