|
| 1 | +import{resolve}from'node:path'; |
| 2 | +import{spawn}from'node:child_process'; |
| 3 | + |
| 4 | +exportdefaultclassQuicTestServer{ |
| 5 | + #pathToServer; |
| 6 | + #runningProcess; |
| 7 | + |
| 8 | +constructor(){ |
| 9 | +this.#pathToServer =resolve(process.execPath,'../ngtcp2_test_server'); |
| 10 | +console.log(this.#pathToServer); |
| 11 | +} |
| 12 | + |
| 13 | +help(options={stdio: 'inherit'}){ |
| 14 | +const{ promise, resolve, reject }=Promise.withResolvers(); |
| 15 | +constproc=spawn(this.#pathToServer,['--help'],options); |
| 16 | +proc.on('error',reject); |
| 17 | +proc.on('exit',(code,signal)=>{ |
| 18 | +if(code===0){ |
| 19 | +resolve(); |
| 20 | +}else{ |
| 21 | +reject(newError(`Process exited with code ${code} and signal ${signal}`)); |
| 22 | +} |
| 23 | +}); |
| 24 | +returnpromise; |
| 25 | +} |
| 26 | + |
| 27 | +run(address,port,keyFile,certFile,options={stdio: 'inherit'}){ |
| 28 | +const{ promise, resolve, reject }=Promise.withResolvers(); |
| 29 | +if(this.#runningProcess){ |
| 30 | +reject(newError('Server is already running')); |
| 31 | +returnpromise; |
| 32 | +} |
| 33 | +constargs=[ |
| 34 | +address, |
| 35 | +port, |
| 36 | +keyFile, |
| 37 | +certFile, |
| 38 | +]; |
| 39 | +this.#runningProcess =spawn(this.#pathToServer,args,options); |
| 40 | +this.#runningProcess.on('error',(err)=>{ |
| 41 | +this.#runningProcess =undefined; |
| 42 | +reject(err); |
| 43 | +}); |
| 44 | +this.#runningProcess.on('exit',(code,signal)=>{ |
| 45 | +this.#runningProcess =undefined; |
| 46 | +if(code===0){ |
| 47 | +resolve(); |
| 48 | +}else{ |
| 49 | +if(code===null&&signal==='SIGTERM'){ |
| 50 | +// Normal termination due to stop() being called. |
| 51 | +resolve(); |
| 52 | +return; |
| 53 | +} |
| 54 | +reject(newError(`Process exited with code ${code} and signal ${signal}`)); |
| 55 | +} |
| 56 | +}); |
| 57 | +returnpromise; |
| 58 | +} |
| 59 | + |
| 60 | +stop(){ |
| 61 | +if(this.#runningProcess){ |
| 62 | +this.#runningProcess.kill(); |
| 63 | +this.#runningProcess =undefined; |
| 64 | +} |
| 65 | +} |
| 66 | +}; |
0 commit comments