Skip to content

Commit

Permalink
fix: remove usage of "md5-file"
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed May 8, 2023
1 parent 41b2551 commit 8096884
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Expand Up @@ -2,7 +2,6 @@ import os from 'os';
import { URL } from 'url';
import path from 'path';
import { promises as fspromises, createWriteStream, createReadStream, constants } from 'fs';
import md5File from 'md5-file';
import https from 'https';
import { createUnzip } from 'zlib';
import tar from 'tar-stream';
Expand All @@ -11,7 +10,7 @@ import MongoBinaryDownloadUrl from './MongoBinaryDownloadUrl';
import { HttpsProxyAgent } from 'https-proxy-agent';
import resolveConfig, { envToBool, ResolveConfigVariables } from './resolveConfig';
import debug from 'debug';
import { assertion, mkdir, pathExists } from './utils';
import { assertion, mkdir, pathExists, md5 } from './utils';
import { DryMongoBinary } from './DryMongoBinary';
import { MongoBinaryOpts } from './MongoBinary';
import { clearLine } from 'readline';
Expand Down Expand Up @@ -200,7 +199,7 @@ export class MongoBinaryDownload {
const signatureContent = (await fspromises.readFile(archiveMD5Path)).toString('utf-8');
const regexMatch = signatureContent.match(/^\s*([\w\d]+)\s*/i);
const md5SigRemote = regexMatch ? regexMatch[1] : null;
const md5SigLocal = md5File.sync(mongoDBArchive);
const md5SigLocal = md5(mongoDBArchive);
log(`makeMD5check: Local MD5: ${md5SigLocal}, Remote MD5: ${md5SigRemote}`);

if (md5SigRemote !== md5SigLocal) {
Expand Down
@@ -1,5 +1,4 @@
import { createWriteStream, promises as fspromises } from 'fs';
import md5file from 'md5-file';
import { assertIsError } from '../../__tests__/testUtils/test_utils';
import { DryMongoBinary } from '../DryMongoBinary';
import { Md5CheckFailedError } from '../errors';
Expand All @@ -13,8 +12,6 @@ import * as yazl from 'yazl';
import { pack } from 'tar-stream';
import { createGzip } from 'zlib';

jest.mock('md5-file');

describe('MongoBinaryDownload', () => {
afterEach(() => {
delete process.env[envName(ResolveConfigVariables.MD5_CHECK)];
Expand Down Expand Up @@ -129,7 +126,7 @@ describe('MongoBinaryDownload', () => {
const fileWithReferenceMd5 = '/another/path';

jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`${someMd5} fileName`);
jest.spyOn(md5file, 'sync').mockImplementationOnce(() => someMd5);
jest.spyOn(utils, 'md5').mockImplementationOnce(() => someMd5);
jest.spyOn(fspromises, 'unlink').mockResolvedValue(void 0);

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true });
Expand All @@ -141,12 +138,12 @@ describe('MongoBinaryDownload', () => {
expect(du.download).toBeCalledWith(urlToMongoDBArchivePath);
expect(fspromises.readFile).toBeCalledWith(fileWithReferenceMd5);
expect(fspromises.unlink).toBeCalledTimes(1);
expect(md5file.sync).toBeCalledWith(mongoDBArchivePath);
expect(utils.md5).toBeCalledWith(mongoDBArchivePath);
});

it('makeMD5check throws an error if md5 of downloaded mongoDBArchive is NOT the same as in the reference result', async () => {
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`);
jest.spyOn(md5file, 'sync').mockImplementationOnce(() => 'anotherMD5');
jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5');

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true });
jest.spyOn(du, 'download').mockResolvedValue('');
Expand All @@ -162,7 +159,7 @@ describe('MongoBinaryDownload', () => {

it('false value of checkMD5 attribute disables makeMD5check validation', async () => {
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`);
jest.spyOn(md5file, 'sync').mockImplementationOnce(() => 'anotherMD5');
jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5');

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: false });
const result = await du.makeMD5check('', '');
Expand Down
11 changes: 10 additions & 1 deletion packages/mongodb-memory-server-core/src/util/utils.ts
Expand Up @@ -10,7 +10,7 @@ import {
} from './errors';
import { tmpdir } from 'os';
import * as path from 'path';
import { randomUUID } from 'crypto';
import { BinaryLike, createHash, randomUUID } from 'crypto';

const log = debug('MongoMS:utils');

Expand Down Expand Up @@ -365,3 +365,12 @@ export async function removeDir(dirPath: string): Promise<void> {
export function uuidv4(): string {
return randomUUID();
}

/**
* Helper function to have md5 generation and definition in one place
* @param content the content to checksum
* @returns a md5 of the input
*/
export function md5(content: BinaryLike): string {
return createHash('md5').update(content).digest('hex');
}

0 comments on commit 8096884

Please sign in to comment.