| 30 | } from './perf.js'; |
| 31 | |
| 32 | export class SidecarAnalyzer implements IAnalyzer { |
| 33 | private sidecarProcess: child_process.ChildProcess; |
| 34 | private compilerOptions: NgpCompilerOptions = {}; |
| 35 | private pendingRequests = new Map< |
| 36 | number, |
| 37 | {method: string; resolve: (val: any) => void; reject: (err: any) => void} |
| 38 | >(); |
| 39 | private requestId = 0; |
| 40 | private resultQueue: CompilationChunk[] = []; |
| 41 | private nextResolve: ((val: CompilationChunk | null) => void) | null = null; |
| 42 | private isAnalyzing = false; |
| 43 | private activeError: Error | null = null; |
| 44 | /** |
| 45 | * Why the sidecar can no longer answer requests, once it has exited or failed to start. Set at |
| 46 | * most once; from then on every request is rejected with it instead of being written to a pipe |
| 47 | * nobody reads. |
| 48 | */ |
| 49 | private terminationReason: string | null = null; |
| 50 | |
| 51 | constructor( |
| 52 | sidecarPath: string, |
| 53 | private tracer: Tracer = NOOP_TRACER, |
| 54 | ) { |
| 55 | this.sidecarProcess = tracer.slice(SpanKind.SpawnSidecar, () => |
| 56 | child_process.spawn(sidecarPath), |
| 57 | ); |
| 58 | |
| 59 | if (this.sidecarProcess.stdout) { |
| 60 | this.readMessages(this.sidecarProcess.stdout); |
| 61 | } |
| 62 | |
| 63 | this.sidecarProcess.stderr?.on('data', (data) => { |
| 64 | console.error(`Sidecar error: ${data}`); |
| 65 | }); |
| 66 | |
| 67 | // A write racing the child's exit fails with EPIPE; without a listener that is an uncaught |
| 68 | // exception in the host. The request it carried is settled when the process closes. |
| 69 | this.sidecarProcess.stdin?.on('error', (err) => { |
| 70 | console.error(`Sidecar stdin error: ${err.message}`); |
| 71 | }); |
| 72 | |
| 73 | this.sidecarProcess.on('error', (err) => { |
| 74 | console.error(`Sidecar process error: ${err.message}`); |
| 75 | // `error` is also emitted for a failed `kill()` of a live process, which can still answer. |
| 76 | // Only a process that never started is known to be unable to. |
| 77 | if (this.sidecarProcess.pid === undefined) { |
| 78 | this.terminate(`Sidecar process failed to start (${err.message})`); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | this.sidecarProcess.on('exit', (code) => { |
| 83 | // stderr, like the sibling handlers above: this module is library code that can run |
| 84 | // inside a stdio-transport language server, where stdout carries the LSP framing. |
| 85 | console.error(`Sidecar exited with code ${code}`); |
| 86 | if (this.onExit) this.onExit(); |
| 87 | }); |
| 88 | |
| 89 | // `close`, not `exit`: it fires only after stdout has ended, so every response the child wrote |
nothing calls this directly
no outgoing calls
no test coverage detected