| 90 | } |
| 91 | |
| 92 | function startWorker() { |
| 93 | return new Promise<{worker: ChildProcess; url: string}>((resolve, reject) => { |
| 94 | const lines: string[] = []; |
| 95 | const child = fork(require.resolve('./worker'), [], { |
| 96 | execArgv: ['--expose-gc'], |
| 97 | stdio: ['pipe', 'pipe', process.stderr, 'ipc'], |
| 98 | }); |
| 99 | |
| 100 | child.once('error', reject); |
| 101 | |
| 102 | child.on('message', msg => { |
| 103 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 104 | const url = (msg as any).url; |
| 105 | debug('Worker setup done, url is', url); |
| 106 | resolve({worker: child, url}); |
| 107 | }); |
| 108 | |
| 109 | child.once('exit', (code, signal) => { |
| 110 | const msg = [ |
| 111 | `Child exited with code ${code} signal ${signal}.`, |
| 112 | ...lines, |
| 113 | ].join('\n'); |
| 114 | reject(new Error(msg)); |
| 115 | }); |
| 116 | |
| 117 | const reader = byline.createStream(child.stdout as Readable); |
| 118 | reader.on('data', line => { |
| 119 | const str = line.toString(); |
| 120 | debug('[worker] %s', str); |
| 121 | lines.push(str); |
| 122 | }); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | async function closeWorker(worker: ChildProcess) { |
| 127 | worker.kill(); |