Skip to content

Commit

Permalink
fix: checksum comparison failed [HEAD-202] (#4476)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Mar 21, 2023
1 parent 92eb747 commit d3f72b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
39 changes: 13 additions & 26 deletions ts-binary-wrapper/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function runWrapper(executable: string, cliArguments: string[]): number {
const debug = debugEnabled(cliArguments);

if (debug) {
console.error('Executing: ' + executable + ' ' + cliArguments.join(' '));
console.debug('Executing: ' + executable + ' ' + cliArguments.join(' '));
}

const res = spawnSync(executable, cliArguments, {
Expand All @@ -184,12 +184,12 @@ export function runWrapper(executable: string, cliArguments: string[]): number {

if (res.status !== null) {
if (debug) {
console.error(res);
console.debug(res);
}

return res.status;
} else {
console.error(res);
console.debug(res);
if (!formatErrorMessage((res.error as SpawnError).code)) {
console.error('Failed to spawn child process. (' + executable + ')');
}
Expand Down Expand Up @@ -245,7 +245,7 @@ export function downloadExecutable(
const options = new URL(downloadUrl);
const temp = path.join(__dirname, Date.now().toString());
const fileStream = fs.createWriteStream(temp);
const shasum = createHash('sha256');
const shasum = createHash('sha256').setEncoding('hex');

const cleanupAfterError = (error: Error) => {
try {
Expand All @@ -259,16 +259,9 @@ export function downloadExecutable(

// shasum events
shasum.on('error', cleanupAfterError);

// filestream events
fileStream.on('error', (e) => {
cleanupAfterError(e);
});

fileStream.on('finish', () => {
// compare shasums
const actualShasum = shasum.digest('hex');

fileStream.on('error', cleanupAfterError).on('finish', () => {
const actualShasum = shasum.read();
const debugMessage =
'Shasums:\n- actual: ' +
actualShasum +
Expand All @@ -278,25 +271,24 @@ export function downloadExecutable(
if (filenameShasum && actualShasum != filenameShasum) {
cleanupAfterError(Error('Shasum comparison failed!\n' + debugMessage));
} else {
console.error(debugMessage);
console.debug(debugMessage);

// finally rename the file and change permissions
fs.renameSync(temp, filename);
fs.chmodSync(filename, 0o755);
console.error('Downloaded successfull! ');
console.debug('Downloaded successfull! ');
}

resolve(undefined);
});

console.error(
console.debug(
"Downloading from '" + downloadUrl + "' to '" + filename + "'",
);

const req = https.request(options, (res) => {
const req = https.get(options, (res) => {
// response events
res.on('error', cleanupAfterError);
res.on('end', () => {
res.on('error', cleanupAfterError).on('end', () => {
shasum.end();
fileStream.end();
});
Expand All @@ -306,11 +298,7 @@ export function downloadExecutable(
res.pipe(shasum);
});

req.on('error', (e) => {
cleanupAfterError(e);
});

req.on('response', (incoming) => {
req.on('error', cleanupAfterError).on('response', (incoming) => {
if (
incoming.statusCode &&
!(200 <= incoming.statusCode && incoming.statusCode < 300)
Expand Down Expand Up @@ -357,8 +345,7 @@ export async function logError(

// finally log the error to the console as well
if (printToConsole) {
console.error('\n');
console.error(err);
console.error('\n' + err);
formatErrorMessage(err.message);
}
}
Expand Down
10 changes: 2 additions & 8 deletions ts-binary-wrapper/test/acceptance/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ describe('Basic acceptance test', () => {

expect(resultIndex.status).toEqual(0);
expect(
resultIndex.stdout
.toString()
.trim()
.startsWith(cliVersionForTesting),
resultIndex.stdout.toString().includes(cliVersionForTesting),
).toBeTruthy();

fs.unlinkSync(executable);
Expand Down Expand Up @@ -98,10 +95,7 @@ describe('Basic acceptance test', () => {
expect(fs.existsSync(executable)).toBeTruthy();
expect(resultIndex.status).toEqual(0);
expect(
resultIndex.stdout
.toString()
.trim()
.startsWith(cliVersionForTesting),
resultIndex.stdout.toString().includes(cliVersionForTesting),
).toBeTruthy();

fs.unlinkSync(executable);
Expand Down

0 comments on commit d3f72b2

Please sign in to comment.