diff --git a/packages/artifact/src/internal/download-http-client.ts b/packages/artifact/src/internal/download-http-client.ts index 2df4675f..773c85bb 100644 --- a/packages/artifact/src/internal/download-http-client.ts +++ b/packages/artifact/src/internal/download-http-client.ts @@ -219,6 +219,14 @@ export class DownloadHttpClient { fileDownloadPath: string ): Promise => { destinationStream.close() + // await until file is created at downloadpath + // wrap destinationStream's open event in promise + await new Promise(resolve => { + destinationStream.on('close', resolve) + if (destinationStream.writableFinished) { + resolve() + } + }); await rmFile(fileDownloadPath) destinationStream = fs.createWriteStream(fileDownloadPath) } @@ -273,8 +281,8 @@ export class DownloadHttpClient { // if a throttled status code is received, try to get the retryAfter header value, else differ to standard exponential backoff isThrottledStatusCode(response.message.statusCode) ? await backOff( - tryGetRetryAfterValueTimeInMilliseconds(response.message.headers) - ) + tryGetRetryAfterValueTimeInMilliseconds(response.message.headers) + ) : await backOff() } else { // Some unexpected response code, fail immediately and stop the download diff --git a/packages/artifact/src/internal/utils.ts b/packages/artifact/src/internal/utils.ts index e2a554fa..cb5a9224 100644 --- a/packages/artifact/src/internal/utils.ts +++ b/packages/artifact/src/internal/utils.ts @@ -270,15 +270,6 @@ export async function getFileSize(filePath: string): Promise { } export async function rmFile(filePath: string): Promise { - // TODO: find actual fix - // node 16 `CreateWriteStream` no longer creates a file - // download-http-client.ts#L151 no longer creates a file and we fail here - try { - await fs.stat(filePath) - } catch (e) { - // File does not exist - return - } await fs.unlink(filePath) }