Skip to content

Commit

Permalink
Allow hashing with sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Dec 13, 2023
1 parent 704fd00 commit 5f41a87
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
8 changes: 4 additions & 4 deletions __tests__/lib/client-file-uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @format
*/

import { getFileMD5Hash, getFileMeta, getPartBoundaries } from '../../src/lib/client-file-uploader';
import { getFileHash, getFileMeta, getPartBoundaries } from '../../src/lib/client-file-uploader';

describe( 'client-file-uploader', () => {
describe( 'getFileMeta()', () => {
Expand All @@ -27,16 +27,16 @@ describe( 'client-file-uploader', () => {
} );
} );

describe( 'getFileMD5Hash()', () => {
describe( 'getFileHash()', () => {
it( 'should get hash from a 67mb sql file', async () => {
const fileName = '__fixtures__/client-file-uploader/db-dump-ipsum-67mb.sql';
const md5 = await getFileMD5Hash( fileName );
const md5 = await getFileHash( fileName );
expect( md5 ).toBe( '6a051288a7848e3fb3571af220fc455a' );
} );

it( 'should get hash from a 5+mb text file', async () => {
const fileName = '__fixtures__/client-file-uploader/numerical-test-file-5.24mb.txt';
const md5 = await getFileMD5Hash( fileName );
const md5 = await getFileHash( fileName );
expect( md5 ).toBe( '6f18fdff4f9f9926989e0816741aa2ba' );
} );
} );
Expand Down
7 changes: 4 additions & 3 deletions src/bin/vip-app-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,26 @@ Processing the file for deployment to your environment...
env: envInput,
fileMeta,
progressCallback,
hashType: 'sha256',
};
const startDeployVariables: StartDeployVariables = { input: {} };

try {
const {
fileMeta: { basename },
md5,
checksum,
result,
} = await uploadImportSqlFileToS3( uploadParams );

startDeployVariables.input = {
id: app.id,
environmentId: env.id,
basename: fileMeta.basename,
md5,
checksum,
deployMessage,
};

debug( { basename, md5, result, startDeployVariables } );
debug( { basename, checksum, result, startDeployVariables } );
debug( 'Upload complete. Initiating the deploy.' );
progressTracker.stepSuccess( 'upload' );
await track( 'deploy_app_upload_complete' );
Expand Down
2 changes: 1 addition & 1 deletion src/bin/vip-import-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ Processing the SQL import for your environment...
try {
const {
fileMeta: { basename },
md5,
checksum: md5,
result,
} = await uploadImportSqlFileToS3( {
app,
Expand Down
2 changes: 1 addition & 1 deletion src/graphqlTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ export type AppEnvironmentDeployInput = {
basename?: InputMaybe< Scalars[ 'String' ][ 'input' ] >;
environmentId?: InputMaybe< Scalars[ 'Int' ][ 'input' ] >;
id?: InputMaybe< Scalars[ 'Int' ][ 'input' ] >;
md5?: InputMaybe< Scalars[ 'String' ][ 'input' ] >;
checksum?: InputMaybe< Scalars[ 'String' ][ 'input' ] >;
deployMessage?: InputMaybe< Scalars[ 'String' ][ 'input' ] >;
};

Expand Down
17 changes: 11 additions & 6 deletions src/lib/client-file-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ export interface UploadArguments {
env: WithId;
fileMeta: FileMeta;
progressCallback?: ( percentage: string ) => unknown;
hashType?: string;
}

export const getFileMD5Hash = async ( fileName: string ): Promise< string > => {
export const getFileHash = async (
fileName: string,
hashType: 'md5' | 'sha256' = 'md5'
): Promise< string > => {
const src = createReadStream( fileName );
const dst = createHash( 'md5' );
const dst = createHash( hashType );
try {
await pipeline( src, dst );
return dst.digest().toString( 'hex' );
Expand Down Expand Up @@ -162,6 +166,7 @@ export async function uploadImportSqlFileToS3( {
env,
fileMeta,
progressCallback,
hashType = 'md5',
}: UploadArguments ) {
let tmpDir;
try {
Expand Down Expand Up @@ -205,9 +210,9 @@ export async function uploadImportSqlFileToS3( {
debug( `** Compression resulted in a ${ calculation } smaller file 📦 **\n` );
}

debug( 'Calculating file md5 checksum...' );
const md5 = await getFileMD5Hash( fileMeta.fileName );
debug( `Calculated file md5 checksum: ${ md5 }\n` );
debug( `Calculating file ${ hashType } checksum...` );
const checksum = await getFileHash( fileMeta.fileName );
debug( `Calculated file ${ hashType } checksum: ${ checksum }\n` );

const result =
fileMeta.fileSize < MULTIPART_THRESHOLD
Expand All @@ -216,7 +221,7 @@ export async function uploadImportSqlFileToS3( {

return {
fileMeta,
md5,
checksum,
result,
};
}
Expand Down

0 comments on commit 5f41a87

Please sign in to comment.