Fix response size calculation

The `response.size` is almost always set to `0`. However, the
`Content-Length` header is returned by Azure and contains the size of
the file.
This commit is contained in:
Koen Vlaswinkel
2023-01-26 17:11:29 +01:00
parent c7bd9f9394
commit 94968f41e7
2 changed files with 11 additions and 2 deletions

View File

@@ -90,12 +90,18 @@ export class VariantAnalysisResultsManager extends DisposableObject {
const zipFilePath = join(resultDirectory, "results.zip");
const response = await fetch(repoTask.artifactUrl);
let responseSize = parseInt(response.headers.get("content-length") || "0");
if (responseSize === 0 && response.size > 0) {
responseSize = response.size;
}
let amountDownloaded = 0;
for await (const chunk of response.body) {
await appendFile(zipFilePath, Buffer.from(chunk));
amountDownloaded += chunk.length;
await onDownloadPercentageChanged(
Math.floor((amountDownloaded / response.size) * 100),
Math.floor((amountDownloaded / responseSize) * 100),
);
}

View File

@@ -169,7 +169,10 @@ describe(VariantAnalysisResultsManager.name, () => {
(url: RequestInfo, _init?: RequestInit) => {
if (url === dummyRepoTask.artifactUrl) {
const response = new Response(Readable.from(generateInParts()));
response.size = fileContents.length;
response.headers.set(
"Content-Length",
fileContents.length.toString(),
);
return Promise.resolve(response);
}
return Promise.reject(new Error("Unexpected artifact URL"));