(params: DownloadRemoteArtifactParams)
| 345 | }; |
| 346 | |
| 347 | export async function downloadRemoteArtifact(params: DownloadRemoteArtifactParams): Promise<void> { |
| 348 | const artifactUrl = new URL(buildDaemonArtifactUrl(params.baseUrl, params.artifactId)); |
| 349 | const transport = artifactUrl.protocol === 'https:' ? https : http; |
| 350 | await fs.promises.mkdir(path.dirname(params.destinationPath), { recursive: true }); |
| 351 | await new Promise<void>((resolve, reject) => { |
| 352 | let settled = false; |
| 353 | const timeoutMs = params.timeoutMs ?? REMOTE_ARTIFACT_DOWNLOAD_TIMEOUT_MS; |
| 354 | const settle = (error?: Error) => { |
| 355 | if (settled) return; |
| 356 | settled = true; |
| 357 | clearTimeout(timeoutHandle); |
| 358 | if (error) { |
| 359 | void fs.promises.rm(params.destinationPath, { force: true }).finally(() => reject(error)); |
| 360 | return; |
| 361 | } |
| 362 | resolve(); |
| 363 | }; |
| 364 | const request = transport.request( |
| 365 | { |
| 366 | protocol: artifactUrl.protocol, |
| 367 | host: artifactUrl.hostname, |
| 368 | port: artifactUrl.port, |
| 369 | method: 'GET', |
| 370 | path: artifactUrl.pathname + artifactUrl.search, |
| 371 | headers: buildDaemonHttpAuthHeaders(params.token), |
| 372 | }, |
| 373 | (res) => { |
| 374 | if ((res.statusCode ?? 500) >= 400) { |
| 375 | let body = ''; |
| 376 | res.setEncoding('utf8'); |
| 377 | res.on('data', (chunk) => { |
| 378 | body += chunk; |
| 379 | }); |
| 380 | res.on('end', () => { |
| 381 | settle( |
| 382 | new AppError('COMMAND_FAILED', 'Failed to download remote artifact', { |
| 383 | artifactId: params.artifactId, |
| 384 | statusCode: res.statusCode, |
| 385 | requestId: params.requestId, |
| 386 | body, |
| 387 | }), |
| 388 | ); |
| 389 | }); |
| 390 | return; |
| 391 | } |
| 392 | res.on('aborted', () => { |
| 393 | settle( |
| 394 | new AppError('COMMAND_FAILED', 'Remote artifact download was interrupted', { |
| 395 | artifactId: params.artifactId, |
| 396 | requestId: params.requestId, |
| 397 | }), |
| 398 | ); |
| 399 | }); |
| 400 | void pipeline(res, fs.createWriteStream(params.destinationPath)).then( |
| 401 | () => settle(), |
| 402 | (error: unknown) => settle(error instanceof Error ? error : new Error(String(error))), |
| 403 | ); |
| 404 | }, |
no test coverage detected
searching dependent graphs…