Skip to content

Commit 21db4b9

Browse files
committed
Refactoring the download process for deploy clients using the Streams Promises API
1 parent 062fb28 commit 21db4b9

1 file changed

Lines changed: 23 additions & 31 deletions

File tree

src/core/download-binary-helper.ts

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chalk from "chalk";
22
import crypto from "node:crypto";
33
import fs from "node:fs";
4+
import stp from "node:stream/promises";
45
import { fetchWithProxy as fetch } from "./utils/fetch-proxy.js";
56
import ora from "ora";
67
import path from "node:path";
@@ -41,44 +42,35 @@ export async function downloadAndValidateBinary(
4142

4243
createBinaryDirectoryIfNotExists(id, outputFolder);
4344

44-
return await new Promise<string>((resolve, reject) => {
45-
const isPosix = platform === "linux-x64" || platform === "osx-x64";
46-
const outputFile = path.join(outputFolder, id, downloadFilename);
45+
const isPosix = platform === "linux-x64" || platform === "osx-x64";
46+
const outputFile = path.join(outputFolder, id, downloadFilename);
4747

48-
const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined });
49-
bodyStream?.pipe(writableStream);
48+
const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined });
5049

51-
writableStream.on("end", () => {
52-
bodyStream?.end();
53-
});
50+
await stp.pipeline(bodyStream, writableStream);
5451

55-
writableStream.on("finish", async () => {
56-
const computedHash = computeChecksumfromFile(outputFile).toLowerCase();
57-
const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase();
58-
if (computedHash !== releaseChecksum) {
59-
try {
60-
// in case of a failure, we remove the file
61-
fs.unlinkSync(outputFile);
62-
} catch {
63-
logger.silly(`Not able to delete ${downloadFilename}, please delete manually.`);
64-
}
52+
const computedHash = computeChecksumfromFile(outputFile).toLowerCase();
53+
const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase();
54+
if (computedHash !== releaseChecksum) {
55+
try {
56+
// in case of a failure, we remove the file
57+
fs.unlinkSync(outputFile);
58+
} catch {
59+
logger.silly(`Not able to delete ${downloadFilename}, please delete manually.`);
60+
}
6561

66-
spinner.fail();
67-
reject(new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`));
68-
} else {
69-
spinner.succeed();
70-
71-
logger.silly(`Checksum match: ${computedHash}`);
72-
logger.silly(`Saved binary to ${outputFile}`);
62+
spinner.fail();
63+
throw new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`);
64+
} else {
65+
spinner.succeed();
7366

74-
saveMetadata(releaseMetadata, outputFile, computedHash, binaryName);
67+
logger.silly(`Checksum match: ${computedHash}`);
68+
logger.silly(`Saved binary to ${outputFile}`);
7569

76-
resolve(outputFile);
77-
}
78-
});
70+
saveMetadata(releaseMetadata, outputFile, computedHash, binaryName);
71+
}
7972

80-
// writableStream.close();
81-
});
73+
return outputFile;
8274
}
8375

8476
/**

0 commit comments

Comments
 (0)