From 20dfba8315ad952bf99bb774e2cd566019d24b0e Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Wed, 14 Dec 2022 01:17:17 +0100 Subject: [PATCH 1/4] Test out checking for fileexists in rmFile --- src/internal/utils.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/internal/utils.ts b/src/internal/utils.ts index cb5a922..2de06d6 100644 --- a/src/internal/utils.ts +++ b/src/internal/utils.ts @@ -1,9 +1,9 @@ import crypto from 'crypto' -import {promises as fs} from 'fs' -import {IncomingHttpHeaders, OutgoingHttpHeaders} from 'http' -import {debug, info, warning} from '@actions/core' -import {HttpCodes, HttpClient, HttpClientResponse} from '@actions/http-client' -import {BearerCredentialHandler} from '@actions/http-client/lib/auth' +import { promises as fs } from 'fs' +import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http' +import { debug, info, warning } from '@actions/core' +import { HttpCodes, HttpClient, HttpClientResponse } from '@actions/http-client' +import { BearerCredentialHandler } from '@actions/http-client/lib/auth' import { getRuntimeToken, getRuntimeUrl, @@ -270,6 +270,16 @@ 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) { + console.log("File does not exist."); + return; + } await fs.unlink(filePath) } From 98e1a306969be3a9872e39ec50bf687830ed0af3 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Wed, 14 Dec 2022 01:38:01 +0100 Subject: [PATCH 2/4] Fix linting --- src/internal/utils.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/internal/utils.ts b/src/internal/utils.ts index 2de06d6..e2a554f 100644 --- a/src/internal/utils.ts +++ b/src/internal/utils.ts @@ -1,9 +1,9 @@ import crypto from 'crypto' -import { promises as fs } from 'fs' -import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http' -import { debug, info, warning } from '@actions/core' -import { HttpCodes, HttpClient, HttpClientResponse } from '@actions/http-client' -import { BearerCredentialHandler } from '@actions/http-client/lib/auth' +import {promises as fs} from 'fs' +import {IncomingHttpHeaders, OutgoingHttpHeaders} from 'http' +import {debug, info, warning} from '@actions/core' +import {HttpCodes, HttpClient, HttpClientResponse} from '@actions/http-client' +import {BearerCredentialHandler} from '@actions/http-client/lib/auth' import { getRuntimeToken, getRuntimeUrl, @@ -275,10 +275,9 @@ export async function rmFile(filePath: string): Promise { // download-http-client.ts#L151 no longer creates a file and we fail here try { await fs.stat(filePath) - } - catch (e) { - console.log("File does not exist."); - return; + } catch (e) { + // File does not exist + return } await fs.unlink(filePath) } From d6c0a88eebfe53668c65c102682b48158f3fcdd9 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Wed, 14 Dec 2022 15:57:48 +0100 Subject: [PATCH 3/4] Await finish of filestream so file is created for node16 --- src/internal/download-http-client.ts | 12 ++++++++++-- src/internal/utils.ts | 9 --------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/internal/download-http-client.ts b/src/internal/download-http-client.ts index 2df4675..773c85b 100644 --- a/src/internal/download-http-client.ts +++ b/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/src/internal/utils.ts b/src/internal/utils.ts index e2a554f..cb5a922 100644 --- a/src/internal/utils.ts +++ b/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) } From bc3d6326f2b7da263831ee5e406bf189213bc2e9 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl Date: Wed, 14 Dec 2022 16:13:28 +0100 Subject: [PATCH 4/4] Fix linting --- src/internal/download-http-client.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/internal/download-http-client.ts b/src/internal/download-http-client.ts index 773c85b..e365f3a 100644 --- a/src/internal/download-http-client.ts +++ b/src/internal/download-http-client.ts @@ -219,14 +219,13 @@ export class DownloadHttpClient { fileDownloadPath: string ): Promise => { destinationStream.close() - // await until file is created at downloadpath - // wrap destinationStream's open event in promise + // await until file is created at downloadpath; node15 and up fs.createWriteStream had not created a file yet await new Promise(resolve => { destinationStream.on('close', resolve) if (destinationStream.writableFinished) { resolve() } - }); + }) await rmFile(fileDownloadPath) destinationStream = fs.createWriteStream(fileDownloadPath) } @@ -281,8 +280,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