import { checkOperations } from '../server/operations-check.ts'; const args=process.argv.slice(2); if(args.length!==2||args[0]!=='--port'||!/^\d{1,5}$/.test(args[1])||Number(args[1])<1||Number(args[1])>65535)throw new Error('Explicit --port 1..65535 required; loopback diagnostics only.'); const response=await fetch(`http://127.0.0.1:${Number(args[1])}/status`,{redirect:'error',signal:AbortSignal.timeout(5000)}); if(!response.ok||!response.body||!/^application\/json(?:;|$)/i.test(response.headers.get('content-type')??''))throw new Error('Local diagnostics unavailable'); const reader=response.body.getReader(),chunks:Uint8Array[]=[];let bytes=0; try{for(;;){const part=await reader.read();if(part.done)break;bytes+=part.value.byteLength;if(bytes>65536)throw new Error('Diagnostics too large');chunks.push(part.value);}} finally{await reader.cancel();reader.releaseLock();} const joined=new Uint8Array(bytes);let offset=0;for(const chunk of chunks){joined.set(chunk,offset);offset+=chunk.length;} const report=checkOperations(JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(joined))); console.log(JSON.stringify(report,null,2));if(!report.healthy)process.exitCode=1;