From b9de68a590daf37c6747e38d3cb4f1dd2cfb791c Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Wed, 14 Dec 2022 15:57:48 +0100 Subject: [PATCH] Await finish of filestream so file is created for node16 --- .../artifact/src/internal/download-http-client.ts | 12 ++++++++++-- packages/artifact/src/internal/utils.ts | 9 --------- 2 files changed, 10 insertions(+), 11 deletions(-) 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) }