|
1 | 1 | import chalk from "chalk"; |
2 | 2 | import crypto from "node:crypto"; |
3 | 3 | import fs from "node:fs"; |
| 4 | +import stp from "node:stream/promises"; |
4 | 5 | import { fetchWithProxy as fetch } from "./utils/fetch-proxy.js"; |
5 | 6 | import ora from "ora"; |
6 | 7 | import path from "node:path"; |
7 | | -import { PassThrough } from "node:stream"; |
8 | 8 | import { DATA_API_BUILDER_BINARY_NAME, DATA_API_BUILDER_FOLDER, DEPLOY_BINARY_NAME, DEPLOY_FOLDER } from "./constants.js"; |
9 | 9 | import { logger } from "./utils/logger.js"; |
10 | 10 |
|
@@ -37,48 +37,37 @@ export async function downloadAndValidateBinary( |
37 | 37 | throw new Error(`Failed to download ${binaryName} binary from url ${url}. File not found (${response.status})`); |
38 | 38 | } |
39 | 39 |
|
40 | | - const bodyStream = response?.body?.pipe(new PassThrough()); |
41 | | - |
42 | 40 | createBinaryDirectoryIfNotExists(id, outputFolder); |
43 | 41 |
|
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); |
50 | 44 |
|
51 | | - writableStream.on("end", () => { |
52 | | - bodyStream?.end(); |
53 | | - }); |
| 45 | + const writableStream = fs.createWriteStream(outputFile, { mode: isPosix ? 0o755 : undefined }); |
54 | 46 |
|
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); |
65 | 48 |
|
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 | + } |
70 | 58 |
|
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(); |
73 | 63 |
|
74 | | - saveMetadata(releaseMetadata, outputFile, computedHash, binaryName); |
| 64 | + logger.silly(`Checksum match: ${computedHash}`); |
| 65 | + logger.silly(`Saved binary to ${outputFile}`); |
75 | 66 |
|
76 | | - resolve(outputFile); |
77 | | - } |
78 | | - }); |
| 67 | + saveMetadata(releaseMetadata, outputFile, computedHash, binaryName); |
| 68 | + } |
79 | 69 |
|
80 | | - // writableStream.close(); |
81 | | - }); |
| 70 | + return outputFile; |
82 | 71 | } |
83 | 72 |
|
84 | 73 | /** |
|
0 commit comments