Skip to content

Commit bde135d

Browse files
authored
fix: Refactoring the download process for StaticSiteClient (#936)
* Refactoring the download process for deploy clients using the Streams Promises API * Remove PassThrough pipe
1 parent 062fb28 commit bde135d

1 file changed

Lines changed: 23 additions & 34 deletions

File tree

src/core/download-binary-helper.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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";
7-
import { PassThrough } from "node:stream";
88
import { DATA_API_BUILDER_BINARY_NAME, DATA_API_BUILDER_FOLDER, DEPLOY_BINARY_NAME, DEPLOY_FOLDER } from "./constants.js";
99
import { logger } from "./utils/logger.js";
1010

@@ -37,48 +37,37 @@ export async function downloadAndValidateBinary(
3737
throw new Error(`Failed to download ${binaryName} binary from url ${url}. File not found (${response.status})`);
3838
}
3939

40-
const bodyStream = response?.body?.pipe(new PassThrough());
41-
4240
createBinaryDirectoryIfNotExists(id, outputFolder);
4341

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);
47-
48-
const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined });
49-
bodyStream?.pipe(writableStream);
42+
const isPosix = platform === "linux-x64" || platform === "osx-x64";
43+
const outputFile = path.join(outputFolder, id, downloadFilename);
5044

51-
writableStream.on("end", () => {
52-
bodyStream?.end();
53-
});
45+
const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined });
5446

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-
}
47+
await stp.pipeline(response.body, writableStream);
6548

66-
spinner.fail();
67-
reject(new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`));
68-
} else {
69-
spinner.succeed();
49+
const computedHash = computeChecksumfromFile(outputFile).toLowerCase();
50+
const releaseChecksum = releaseMetadata.files[platform].sha.toLowerCase();
51+
if (computedHash !== releaseChecksum) {
52+
try {
53+
// in case of a failure, we remove the file
54+
fs.unlinkSync(outputFile);
55+
} catch {
56+
logger.silly(`Not able to delete ${downloadFilename}, please delete manually.`);
57+
}
7058

71-
logger.silly(`Checksum match: ${computedHash}`);
72-
logger.silly(`Saved binary to ${outputFile}`);
59+
spinner.fail();
60+
throw new Error(`Checksum mismatch for ${binaryName}! Expected ${releaseChecksum}, got ${computedHash}.`);
61+
} else {
62+
spinner.succeed();
7363

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

76-
resolve(outputFile);
77-
}
78-
});
67+
saveMetadata(releaseMetadata, outputFile, computedHash, binaryName);
68+
}
7969

80-
// writableStream.close();
81-
});
70+
return outputFile;
8271
}
8372

8473
/**

0 commit comments

Comments
 (0)