Skip to content

Commit a41a15d

Browse files
committed
refactor: reverse MD5 check logic. Now by default it's disabled.
BREAKING CHANGE: Reverse MD5 check logic. Now by default it's disabled. `MONGOMS_SKIP_MD5_CHECK` was removed `MONGOMS_MD5_CHECK` was added. Should be `true` if you want to enable md5 check. But who cares this checks if we just need blazing fast tests? Related typegoose#131
1 parent 0f1dc61 commit a41a15d

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ const mongod = new MongoMemoryServer({
6262
platform?: string, // by default os.platform()
6363
arch?: string, // by default os.arch()
6464
debug?: boolean, // by default false
65-
skipMD5?: boolean, // by default false OR process.env.MONGOMS_SKIP_MD5_CHECK
65+
checkMD5?: boolean, // by default false OR process.env.MONGOMS_MD5_CHECK
6666
systemBinary?: string, // by default undefined or process.env.MONGOMS_SYSTEM_BINARY
6767
},
6868
debug?: boolean, // by default false
6969
autoStart?: boolean, // by default true
7070
});
7171
```
72+
7273
Also you can use the environment variables for configure installation process
73-
```
74+
75+
```txt
7476
MONGOMS_DOWNLOAD_DIR=/path/to/mongodb/binaries
7577
MONGOMS_PLATFORM=linux
7678
MONGOMS_ARCH=x64
@@ -79,11 +81,12 @@ MONGOMS_DEBUG=1 # also available case-insensitive values: "on" "yes" "true"
7981
MONGOMS_DOWNLOAD_MIRROR=url # your mirror url to download the mongodb binary
8082
MONGOMS_DISABLE_POSTINSTALL=1 # if you want to skip download binaries on `npm i` command
8183
MONGOMS_SYSTEM_BINARY=/usr/local/bin/mongod # if you want to use an existing binary already on your system.
82-
MONGOMS_SKIP_MD5_CHECK=1 # if you want to skip MD5 check of downloaded binary.
83-
# Passed constructor parameter `binary.skipMD5` has higher priority.
84+
MONGOMS_MD5_CHECK=1 # if you want to make MD5 check of downloaded binary.
85+
# Passed constructor parameter `binary.checkMD5` has higher priority.
8486
```
8587

8688
### Replica Set start:
89+
8790
```js
8891
import { MongoMemoryReplSet } from 'mongodb-memory-server';
8992

src/util/MongoBinaryDownload.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class MongoBinaryDownload {
1212
debug: DebugFn;
1313
dlProgress: DownloadProgressT;
1414

15+
checkMD5: boolean;
1516
downloadDir: string;
1617
arch: string;
1718
version: string;
@@ -20,7 +21,7 @@ export default class MongoBinaryDownload {
2021
constructor(opts: MongoBinaryDownloadOpts);
2122
getMongodPath(): Promise<string>;
2223
startDownload(): Promise<string>;
23-
checkMd5(mongoDBArchiveMd5: string, mongoDBArchive: string): Promise<void>;
24+
makeMd5check(mongoDBArchiveMd5: string, mongoDBArchive: string): Promise<void>;
2425
download(downloadUrl: string): Promise<string>;
2526
extract(mongoDBArchive: string): Promise<string>;
2627
httpDownload(

src/util/MongoBinaryDownload.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export type MongoBinaryDownloadOpts = {
1818
platform: string,
1919
arch: string,
2020
debug?: DebugPropT,
21-
skipMD5?: boolean,
21+
checkMD5?: boolean,
2222
};
2323

2424
export default class MongoBinaryDownload {
2525
debug: DebugFn;
2626
dlProgress: DownloadProgressT;
2727

28-
skipMD5: boolean;
28+
checkMD5: boolean;
2929
downloadDir: string;
3030
arch: string;
3131
version: string;
@@ -36,19 +36,19 @@ export default class MongoBinaryDownload {
3636
arch,
3737
downloadDir,
3838
version,
39-
skipMD5,
39+
checkMD5,
4040
debug,
4141
}: $Shape<MongoBinaryDownloadOpts>) {
4242
this.platform = platform || os.platform();
4343
this.arch = arch || os.arch();
4444
this.version = version || 'latest';
4545
this.downloadDir = path.resolve(downloadDir || 'mongodb-download');
46-
if (skipMD5 === undefined) {
47-
this.skipMD5 =
48-
typeof process.env.MONGOMS_SKIP_MD5_CHECK === 'string' &&
49-
['1', 'on', 'yes', 'true'].indexOf(process.env.MONGOMS_SKIP_MD5_CHECK.toLowerCase()) !== -1;
46+
if (checkMD5 === undefined) {
47+
this.checkMD5 =
48+
typeof process.env.MONGOMS_MD5_CHECK === 'string' &&
49+
['1', 'on', 'yes', 'true'].indexOf(process.env.MONGOMS_MD5_CHECK.toLowerCase()) !== -1;
5050
} else {
51-
this.skipMD5 = skipMD5;
51+
this.checkMD5 = checkMD5;
5252
}
5353
this.dlProgress = {
5454
current: 0,
@@ -100,13 +100,13 @@ export default class MongoBinaryDownload {
100100
const downloadUrl = await mbdUrl.getDownloadUrl();
101101
const mongoDBArchive = await this.download(downloadUrl);
102102

103-
await this.checkMD5(`${downloadUrl}.md5`, mongoDBArchive);
103+
await this.makeMD5check(`${downloadUrl}.md5`, mongoDBArchive);
104104

105105
return mongoDBArchive;
106106
}
107107

108-
async checkMD5(urlForReferenceMD5: string, mongoDBArchive: string): Promise<?boolean> {
109-
if (this.skipMD5) {
108+
async makeMD5check(urlForReferenceMD5: string, mongoDBArchive: string): Promise<?boolean> {
109+
if (!this.checkMD5) {
110110
return undefined;
111111
}
112112
const mongoDBArchiveMd5 = await this.download(urlForReferenceMD5);

src/util/__tests__/MongoBinaryDownload-test.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ describe('MongoBinaryDownload', () => {
1212
delete process.env.MONGOMS_SKIP_MD5_CHECK;
1313
});
1414

15-
it('skipMD5 attribute can be set via constructor parameter', () => {
16-
expect(new MongoBinaryDownload({ skipMD5: true }).skipMD5).toBe(true);
17-
expect(new MongoBinaryDownload({ skipMD5: false }).skipMD5).toBe(false);
15+
it('checkMD5 attribute can be set via constructor parameter', () => {
16+
expect(new MongoBinaryDownload({ checkMD5: true }).checkMD5).toBe(true);
17+
expect(new MongoBinaryDownload({ checkMD5: false }).checkMD5).toBe(false);
1818
});
1919

20-
it(`if skipMD5 input parameter is missing, then it checks
21-
MONGOMS_SKIP_MD5_CHECK environment variable`, () => {
22-
expect(new MongoBinaryDownload({}).skipMD5).toBe(false);
23-
process.env.MONGOMS_SKIP_MD5_CHECK = '1';
24-
expect(new MongoBinaryDownload({}).skipMD5).toBe(true);
20+
it(`if checkMD5 input parameter is missing, then it checks
21+
MONGOMS_MD5_CHECK environment variable`, () => {
22+
expect(new MongoBinaryDownload({}).checkMD5).toBe(false);
23+
process.env.MONGOMS_MD5_CHECK = '1';
24+
expect(new MongoBinaryDownload({}).checkMD5).toBe(true);
2525
});
2626

2727
it('should use direct download', async () => {
@@ -56,7 +56,7 @@ MONGOMS_SKIP_MD5_CHECK environment variable`, () => {
5656
expect(callArg1.agent.options.href).toBe('http://user:pass@proxy:8080/');
5757
});
5858

59-
it(`checkMD5 returns true if md5 of downloaded mongoDBArchive is
59+
it(`makeMD5check returns true if md5 of downloaded mongoDBArchive is
6060
the same as in the reference result`, () => {
6161
const someMd5 = 'md5';
6262
fs.readFileSync.mockImplementationOnce(() => `${someMd5} fileName`);
@@ -67,33 +67,35 @@ the same as in the reference result`, () => {
6767
// $FlowFixMe
6868
du.download = jest.fn(() => Promise.resolve(fileWithReferenceMd5));
6969
const urlToMongoDBArchivePath = 'some-url';
70-
return du.checkMD5(urlToMongoDBArchivePath, mongoDBArchivePath).then(res => {
70+
du.checkMD5 = true;
71+
return du.makeMD5check(urlToMongoDBArchivePath, mongoDBArchivePath).then(res => {
7172
expect(res).toBe(true);
7273
expect(du.download).toBeCalledWith(urlToMongoDBArchivePath);
7374
expect(fs.readFileSync).toBeCalledWith(fileWithReferenceMd5);
7475
expect(md5file.sync).toBeCalledWith(mongoDBArchivePath);
7576
});
7677
});
7778

78-
it(`checkMD5 throws an error if md5 of downloaded mongoDBArchive is NOT
79+
it(`makeMD5check throws an error if md5 of downloaded mongoDBArchive is NOT
7980
the same as in the reference result`, () => {
8081
fs.readFileSync.mockImplementationOnce(() => 'someMd5 fileName');
8182
md5file.sync.mockImplementationOnce(() => 'anotherMd5');
8283
const du = new MongoBinaryDownload({});
84+
du.checkMD5 = true;
8385
// $FlowFixMe
8486
du.download = jest.fn(() => Promise.resolve(''));
85-
expect(du.checkMD5('', '')).rejects.toMatchInlineSnapshot(
87+
expect(du.makeMD5check('', '')).rejects.toMatchInlineSnapshot(
8688
`[Error: MongoBinaryDownload: md5 check is failed]`
8789
);
8890
});
8991

90-
it('true value of skipMD5 attribute disables checkMD5 validation', () => {
92+
it('false value of checkMD5 attribute disables makeMD5check validation', () => {
9193
expect.assertions(1);
9294
fs.readFileSync.mockImplementationOnce(() => 'someMd5 fileName');
9395
md5file.sync.mockImplementationOnce(() => 'anotherMd5');
9496
const du = new MongoBinaryDownload({});
95-
du.skipMD5 = true;
96-
return du.checkMD5('', '').then(res => {
97+
du.checkMD5 = false;
98+
return du.makeMD5check('', '').then(res => {
9799
expect(res).toBe(undefined);
98100
});
99101
});

0 commit comments

Comments
 (0)