From 21db4b974888e9e91eb80f9cc88fc044a84e55b2 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Sun, 2 Mar 2025 14:34:55 +0900 Subject: [PATCH 1/2] Refactoring the download process for deploy clients using the Streams Promises API --- src/core/download-binary-helper.ts | 54 +++++++++++++----------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/src/core/download-binary-helper.ts b/src/core/download-binary-helper.ts index c76f8c824..0fe9300e7 100644 --- a/src/core/download-binary-helper.ts +++ b/src/core/download-binary-helper.ts @@ -1,6 +1,7 @@ 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"; @@ -41,44 +42,35 @@ export async function downloadAndValidateBinary( 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 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 writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined }); - writableStream.on("end", () => { - bodyStream?.end(); - }); + await stp.pipeline(bodyStream, writableStream); - 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.`); - } + 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.`); + } - spinner.fail(); - reject(new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`)); - } else { - spinner.succeed(); - - 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; } /** From fdc0b8f7b8367fdf3ee3a5019baba0278f8fe669 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Sun, 2 Mar 2025 15:04:53 +0900 Subject: [PATCH 2/2] Remove PassThrough pipe --- src/core/download-binary-helper.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/core/download-binary-helper.ts b/src/core/download-binary-helper.ts index 0fe9300e7..7cf839fce 100644 --- a/src/core/download-binary-helper.ts +++ b/src/core/download-binary-helper.ts @@ -5,7 +5,6 @@ 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"; @@ -38,8 +37,6 @@ 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); const isPosix = platform === "linux-x64" || platform === "osx-x64"; @@ -47,7 +44,7 @@ export async function downloadAndValidateBinary( const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined }); - await stp.pipeline(bodyStream, writableStream); + await stp.pipeline(response.body, writableStream); const computedHash = computeChecksumfromFile(outputFile).toLowerCase(); const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase();