diff --git a/src/core/download-binary-helper.ts b/src/core/download-binary-helper.ts index c76f8c824..7cf839fce 100644 --- a/src/core/download-binary-helper.ts +++ b/src/core/download-binary-helper.ts @@ -1,10 +1,10 @@ import chalk from "chalk"; import crypto from "node:crypto"; import fs from "node:fs"; +import stp from "node:stream/promises"; import { fetchWithProxy as fetch } from "./utils/fetch-proxy.js"; import ora from "ora"; import path from "node:path"; -import { PassThrough } from "node:stream"; import { DATA_API_BUILDER_BINARY_NAME, DATA_API_BUILDER_FOLDER, DEPLOY_BINARY_NAME, DEPLOY_FOLDER } from "./constants.js"; import { logger } from "./utils/logger.js"; @@ -37,48 +37,37 @@ export async function downloadAndValidateBinary( throw new Error(`Failed to download ${binaryName} binary from url ${url}. File not found (${response.status})`); } - const bodyStream = response?.body?.pipe(new PassThrough()); - createBinaryDirectoryIfNotExists(id, outputFolder); - return await new Promise((resolve, reject) => { - const isPosix = platform === "linux-x64" || platform === "osx-x64"; - const outputFile = path.join(outputFolder, id, downloadFilename); - - const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined }); - bodyStream?.pipe(writableStream); + const isPosix = platform === "linux-x64" || platform === "osx-x64"; + const outputFile = path.join(outputFolder, id, downloadFilename); - writableStream.on("end", () => { - bodyStream?.end(); - }); + const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined }); - writableStream.on("finish", async () => { - const computedHash = computeChecksumfromFile(outputFile).toLowerCase(); - const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase(); - if (computedHash !== releaseChecksum) { - try { - // in case of a failure, we remove the file - fs.unlinkSync(outputFile); - } catch { - logger.silly(`Not able to delete ${downloadFilename}, please delete manually.`); - } + await stp.pipeline(response.body, writableStream); - spinner.fail(); - reject(new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`)); - } else { - spinner.succeed(); + const computedHash = computeChecksumfromFile(outputFile).toLowerCase(); + const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase(); + if (computedHash !== releaseChecksum) { + try { + // in case of a failure, we remove the file + fs.unlinkSync(outputFile); + } catch { + logger.silly(`Not able to delete ${downloadFilename}, please delete manually.`); + } - logger.silly(`Checksum match: ${computedHash}`); - logger.silly(`Saved binary to ${outputFile}`); + spinner.fail(); + throw new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`); + } else { + spinner.succeed(); - saveMetadata(releaseMetadata, outputFile, computedHash, binaryName); + logger.silly(`Checksum match: ${computedHash}`); + logger.silly(`Saved binary to ${outputFile}`); - resolve(outputFile); - } - }); + saveMetadata(releaseMetadata, outputFile, computedHash, binaryName); + } - // writableStream.close(); - }); + return outputFile; } /**