Skip to content

Commit

Permalink
fix: skip checksum for versions before it was implemented (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao authored Jan 21, 2021
1 parent 8aaa411 commit ceab25d
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"fs-extra": "^8.1.0",
"got": "^9.6.0",
"progress": "^2.0.3",
"semver": "^6.2.0",
"sumchecker": "^3.0.1"
},
"devDependencies": {
Expand All @@ -40,6 +41,7 @@
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"@types/progress": "^2.0.3",
"@types/semver": "^6.2.0",
"husky": "^2.3.0",
"jest": "^24.8.0",
"lint-staged": "^8.1.7",
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debug from 'debug';
import * as path from 'path';
import * as semver from 'semver';
import * as sumchecker from 'sumchecker';

import { getArtifactFileName, getArtifactRemoteURL } from './artifact-utils';
Expand Down Expand Up @@ -122,9 +123,11 @@ export async function downloadArtifact(
await downloader.download(url, tempDownloadPath, artifactDetails.downloadOptions);

// Don't try to verify the hash of the hash file itself
// and for older versions that don't have a SHASUMS256.txt
if (
!artifactDetails.artifactName.startsWith('SHASUMS256') &&
!artifactDetails.unsafelyDisableChecksums
!artifactDetails.unsafelyDisableChecksums &&
semver.gte(artifactDetails.version, '1.3.2')
) {
const shasumPath = await downloadArtifact({
isGeneric: true,
Expand All @@ -136,9 +139,19 @@ export async function downloadArtifact(
downloader: artifactDetails.downloader,
mirrorOptions: artifactDetails.mirrorOptions,
});
await sumchecker('sha256', shasumPath, path.dirname(tempDownloadPath), [
path.basename(tempDownloadPath),
]);

// For versions 1.3.2 - 1.3.4, need to overwrite the `defaultTextEncoding` option:
// https://github.com/electron/electron/pull/6676#discussion_r75332120
if (semver.satisfies(artifactDetails.version, '1.3.2 - 1.3.4')) {
const validatorOptions: sumchecker.ChecksumOptions = {};
validatorOptions.defaultTextEncoding = 'binary';
const checker = new sumchecker.ChecksumValidator('sha256', shasumPath, validatorOptions);
await checker.validate(path.dirname(tempDownloadPath), path.basename(tempDownloadPath));
} else {
await sumchecker('sha256', shasumPath, path.dirname(tempDownloadPath), [
path.basename(tempDownloadPath),
]);
}
}

return await cache.putFileInCache(url, tempDownloadPath, fileName);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.0.0-darwin-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.0.0-darwin-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.0.0-linux-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.0.0-linux-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.0.0-win32-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.0.0-win32-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-darwin-x64 copy.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.3-darwin-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-darwin-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.2-darwin-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-linux-x64 copy.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.3-linux-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-linux-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.3-linux-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-win32-x64 copy.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.3-win32-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.3-win32-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.3-win32-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.5-darwin-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.5-darwin-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.5-linux-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.5-linux-x64.zip
1 change: 1 addition & 0 deletions test/fixtures/electron-v1.3.5-win32-x64.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electron-v1.3.5-win32-x64.zip
51 changes: 51 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import * as path from 'path';

import { FixtureDownloader } from './FixtureDownloader';
import { download, downloadArtifact } from '../src';
import * as sumchecker from 'sumchecker';

jest.mock('sumchecker');

describe('Public API', () => {
const downloader = new FixtureDownloader();
Expand Down Expand Up @@ -209,5 +212,53 @@ describe('Public API', () => {
expect(path.extname(zipPath)).toEqual('.zip');
process.env.ELECTRON_CUSTOM_VERSION = '';
});

describe('sumchecker', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should use the default constructor for versions from v1.3.5 onward', async () => {
await downloadArtifact({
artifactName: 'electron',
downloader,
force: true,
version: 'v1.3.5',
});
await downloadArtifact({
artifactName: 'electron',
downloader,
force: true,
version: 'v2.0.3',
});

expect(sumchecker).toHaveBeenCalledTimes(2);
expect(sumchecker.ChecksumValidator).not.toHaveBeenCalled();
});

it('should use ChecksumValidator for v1.3.2 - v.1.3.4', async () => {
await downloadArtifact({
artifactName: 'electron',
downloader,
force: true,
version: 'v1.3.3',
});

expect(sumchecker).not.toHaveBeenCalled();
expect(sumchecker.ChecksumValidator).toHaveBeenCalledTimes(1);
});

it('should not be called for versions prior to v1.3.2', async () => {
await downloadArtifact({
artifactName: 'electron',
downloader,
force: true,
version: 'v1.0.0',
});

expect(sumchecker).not.toHaveBeenCalled();
expect(sumchecker.ChecksumValidator).not.toHaveBeenCalled();
});
});
});
});
15 changes: 6 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@
dependencies:
"@types/node" "*"

"@types/semver@^6.2.0":
version "6.2.2"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.2.2.tgz#5c27df09ca39e3c9beb4fae6b95f4d71426df0a9"
integrity sha512-RxAwYt4rGwK5GyoRwuP0jT6ZHAVTdz2EqgsHmX0PYNjGsko+OeT4WFXXTs/lM3teJUJodM+SNtAL5/pXIJ61IQ==

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down Expand Up @@ -5008,7 +5013,6 @@ npm@^6.8.0:
cmd-shim "^3.0.3"
columnify "~1.5.4"
config-chain "^1.1.12"
debuglog "*"
detect-indent "~5.0.0"
detect-newline "^2.1.0"
dezalgo "~1.0.3"
Expand All @@ -5023,7 +5027,6 @@ npm@^6.8.0:
has-unicode "~2.0.1"
hosted-git-info "^2.8.8"
iferr "^1.0.2"
imurmurhash "*"
infer-owner "^1.0.4"
inflight "~1.0.6"
inherits "^2.0.4"
Expand All @@ -5042,14 +5045,8 @@ npm@^6.8.0:
libnpx "^10.2.2"
lock-verify "^2.1.0"
lockfile "^1.0.4"
lodash._baseindexof "*"
lodash._baseuniq "~4.6.0"
lodash._bindcallback "*"
lodash._cacheindexof "*"
lodash._createcache "*"
lodash._getnative "*"
lodash.clonedeep "~4.5.0"
lodash.restparam "*"
lodash.union "~4.6.0"
lodash.uniq "~4.5.0"
lodash.without "~4.4.0"
Expand Down Expand Up @@ -6293,7 +6290,7 @@ semver-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==

semver@^6.0.0, semver@^6.3.0:
semver@^6.0.0, semver@^6.2.0, semver@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
Expand Down

0 comments on commit ceab25d

Please sign in to comment.