| 171 | } |
| 172 | |
| 173 | export async function startEventSeen(params: DockerResolverParameters, labels: Record<string, string>, canceled: Promise<void>, output: Log, trace: boolean) { |
| 174 | if (params.cliVariant === CLIVariant.Wslc) { |
| 175 | return startEventSeenPolling(params, labels, canceled, output, trace); |
| 176 | } |
| 177 | const eventsProcess = await getEvents(params, { event: ['start'] }); |
| 178 | return { |
| 179 | started: new Promise<void>((resolve, reject) => { |
| 180 | canceled.catch(err => { |
| 181 | eventsProcess.terminate(); |
| 182 | reject(err); |
| 183 | }); |
| 184 | const decoder = new StringDecoder('utf8'); |
| 185 | let startPart = ''; |
| 186 | eventsProcess.stdout.on('data', async chunk => { |
| 187 | if (chunk) { |
| 188 | const part = decoder.write(chunk); |
| 189 | if (trace) { |
| 190 | output.write(`Log: startEventSeen#data ${part.trim().replace(/\r?\n/g, '\r\n')}\r\n`); |
| 191 | } |
| 192 | const lines = (startPart + part).split('\n'); |
| 193 | startPart = lines.pop()!; |
| 194 | for (const line of lines) { |
| 195 | if (line.trim()) { |
| 196 | try { |
| 197 | const info = JSON.parse(line); |
| 198 | // Docker uses 'status', Podman 'Status'. Docker v29.0.0 onwards use 'Action' as 'status' is deprecated. |
| 199 | if ((info.status || info.Status || info.Action) === 'start' && await hasLabels(params, info, labels)) { |
| 200 | eventsProcess.terminate(); |
| 201 | resolve(); |
| 202 | } |
| 203 | } catch (e) { |
| 204 | // Ignore invalid JSON. |
| 205 | console.error(e); |
| 206 | console.error(line); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | }); |
| 212 | }) |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | // Polling-based fallback for runtimes that don't support `events` (e.g., wslc). |
| 217 | function startEventSeenPolling(params: DockerResolverParameters, labels: Record<string, string>, canceled: Promise<void>, output: Log, trace: boolean) { |