|
| 1 | +// Bounded integration-test client. Each child owns a process group; cleanup |
| 2 | +// runs after success, protocol failure, and request timeout. |
| 3 | +import {spawn} from 'node:child_process'; |
| 4 | +export class Client { |
| 5 | + constructor(executable,args=[],options={}){ |
| 6 | + this.next=1;this.pending=new Map();this.buffer=Buffer.alloc(0);this.stderr=''; |
| 7 | + this.child=spawn(executable,args,{...options,detached:true,stdio:['pipe','pipe','pipe']}); |
| 8 | + this.exited=new Promise(resolve=>{this.child.once('exit',(code,signal)=>{this.fail(Error(`Process exited: ${code}/${signal}: ${this.stderr}`));resolve();});this.child.once('error',error=>{this.fail(error);resolve();});}); |
| 9 | + this.child.stdin.on('error',error=>this.fail(error)); |
| 10 | + this.child.stderr.on('data',bytes=>{this.stderr=(this.stderr+bytes).slice(-65536);}); |
| 11 | + this.child.stdout.on('data',bytes=>{ |
| 12 | + try{ |
| 13 | + this.buffer=Buffer.concat([this.buffer,bytes]);if(this.buffer.length>16*1024*1024)throw Error('Protocol frame exceeds 16 MiB'); |
| 14 | + while(true){ |
| 15 | + const end=this.buffer.indexOf('\r\n\r\n');if(end<0)return; |
| 16 | + const length=Number(/Content-Length: (\d+)/i.exec(this.buffer.subarray(0,end).toString())?.[1]); |
| 17 | + if(!Number.isSafeInteger(length)||length>16*1024*1024)throw Error('Invalid protocol length'); |
| 18 | + if(this.buffer.length<end+4+length)return; |
| 19 | + const message=JSON.parse(this.buffer.subarray(end+4,end+4+length));this.buffer=this.buffer.subarray(end+4+length); |
| 20 | + if(message.method && message.id!==undefined){ |
| 21 | + const result=message.method==='workspace/configuration'?(message.params?.items??[]).map(()=>({})):null; |
| 22 | + this.send({id:message.id,result}); |
| 23 | + }else if(message.id!==undefined){const item=this.pending.get(message.id);if(item){this.pending.delete(message.id);clearTimeout(item.timer);message.error?item.reject(Error(JSON.stringify(message.error))):item.resolve(message.result);}} |
| 24 | + } |
| 25 | + }catch(error){this.fail(error);} |
| 26 | + }); |
| 27 | + } |
| 28 | + fail(error){this.failure=error;for(const item of this.pending.values()){clearTimeout(item.timer);item.reject(error);}this.pending.clear();} |
| 29 | + send(message){const body=JSON.stringify({jsonrpc:'2.0',...message});this.child.stdin.write(`Content-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`);} |
| 30 | + notify(method,params){this.send({method,params});} |
| 31 | + request(method,params,timeout=60000){ |
| 32 | + if(this.failure)return Promise.reject(this.failure); |
| 33 | + const id=this.next++;return new Promise((resolve,reject)=>{ |
| 34 | + const timer=setTimeout(()=>{this.pending.delete(id);reject(Error(`${method} timed out: ${this.stderr}`));},timeout); |
| 35 | + this.pending.set(id,{resolve,reject,timer});this.send({id,method,params}); |
| 36 | + }); |
| 37 | + } |
| 38 | + async close(){ |
| 39 | + this.fail(Error('Client closed')); |
| 40 | + if(this.child.pid){try{process.kill(-this.child.pid,'SIGKILL');}catch(error){if(error.code!=='ESRCH')throw error;}} |
| 41 | + let timer;try{await Promise.race([this.exited,new Promise((_,reject)=>{timer=setTimeout(()=>reject(Error('Process cleanup timed out')),5000);})]);}finally{clearTimeout(timer);} |
| 42 | + } |
| 43 | +} |
0 commit comments