| 70 | // ─── JS ProcessRunner (Worker thread) ──────────────────────────────────────── |
| 71 | |
| 72 | export class ProcessRunner implements IProcessRunner { |
| 73 | private worker: Worker | null = null |
| 74 | private ready: boolean = false |
| 75 | private extDir: string |
| 76 | private entry: string |
| 77 | private workspaceDir: string |
| 78 | private tempDir: string |
| 79 | |
| 80 | constructor(extDir: string, entry: string, workspaceDir: string, tempDir: string) { |
| 81 | this.extDir = extDir |
| 82 | this.entry = entry |
| 83 | this.workspaceDir = workspaceDir |
| 84 | this.tempDir = tempDir |
| 85 | } |
| 86 | |
| 87 | private async ensureReady(): Promise<void> { |
| 88 | if (this.ready && this.worker) return |
| 89 | |
| 90 | return new Promise((resolve, reject) => { |
| 91 | const worker = new Worker(WORKER_CODE, { |
| 92 | eval: true, |
| 93 | workerData: { |
| 94 | extDir: this.extDir, |
| 95 | entry: this.entry, |
| 96 | workspaceDir: this.workspaceDir, |
| 97 | tempDir: this.tempDir, |
| 98 | }, |
| 99 | }) |
| 100 | |
| 101 | worker.once('message', (msg) => { |
| 102 | if (msg.type === 'ready') { |
| 103 | this.worker = worker |
| 104 | this.ready = true |
| 105 | resolve() |
| 106 | } else if (msg.type === 'error') { |
| 107 | worker.terminate() |
| 108 | reject(new Error(msg.message)) |
| 109 | } |
| 110 | }) |
| 111 | |
| 112 | worker.once('error', (err) => { |
| 113 | reject(err) |
| 114 | }) |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | async run( |
| 119 | input: ProcessInput, |
| 120 | params: Record<string, unknown>, |
| 121 | onProgress?: (percent: number, label: string) => void, |
| 122 | onLog?: (message: string) => void, |
| 123 | ): Promise<ProcessResult> { |
| 124 | await this.ensureReady() |
| 125 | const worker = this.worker! |
| 126 | |
| 127 | return new Promise((resolve, reject) => { |
| 128 | const handler = (msg: { type: string; result?: ProcessResult; message?: string; percent?: number; label?: string }) => { |
| 129 | if (msg.type === 'progress') { |
nothing calls this directly
no outgoing calls
no test coverage detected