Skip to content

Commit

Permalink
Move hashFile function to common place
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Sep 21, 2022
1 parent aef8ea4 commit c24005f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 12 additions & 0 deletions common/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs';
import { keccak256 } from 'js-sha3';

/**
* Returns true if and only if the value is null or undefined.
*
Expand All @@ -18,3 +21,12 @@ export function isObject (value: any): boolean {
// to confirm it's just an object.
return typeof value === 'object' && !Array.isArray(value);
}

/**
* Returns the keccak256 hash of a file.
*
* @param path The path to the file to be hashed.
*/
export function hashFile (path: string): string {
return '0x' + keccak256(fs.readFileSync(path, { encoding: 'binary' }));
}
4 changes: 2 additions & 2 deletions downloader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import { https } from 'follow-redirects';
import MemoryStream from 'memorystream';
import { keccak256 } from 'js-sha3';
import { hashFile } from './common/helpers';

function getVersionList (host: string): Promise<string> {
console.log('Retrieving available version list...');
Expand Down Expand Up @@ -44,7 +44,7 @@ function downloadBinary (host: string, outputName: string, releaseFile: string,
response.pipe(file);
file.on('finish', function () {
file.close();
const hash = '0x' + keccak256(fs.readFileSync(outputName, { encoding: 'binary' }));
const hash = hashFile(outputName);
if (expectedHash !== hash) {
reject(new Error('Hash mismatch: expected ' + expectedHash + ' but got ' + hash));
} else {
Expand Down
9 changes: 3 additions & 6 deletions test/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import tape from 'tape';
import nock from 'nock';
import fs from 'fs';
import path from 'path';
import { keccak256 } from 'js-sha3';
import { https } from 'follow-redirects';
import downloader from '../downloader';
import { keccak256 } from 'js-sha3';
import { hashFile } from '../common/helpers';

const assets = path.resolve(__dirname, 'resources/assets');

Expand All @@ -15,10 +16,6 @@ tape.onFinish(() => {
}
});

function hash (filePath: string): string {
return '0x' + keccak256(fs.readFileSync(filePath, { encoding: 'binary' }));
}

function generateTestFile (t: tape.Test, content: string): tmp.FileResult {
// As the `keep` option is set to true the removeCallback must be called by the caller
// to cleanup the files after the test.
Expand Down Expand Up @@ -126,7 +123,7 @@ tape('Download binary', async function (t) {
server.origin,
targetFilename,
file.name,
hash(file.name)
hashFile(file.name)
);

if (!fs.existsSync(targetFilename)) {
Expand Down

0 comments on commit c24005f

Please sign in to comment.