From ceab25dfab4e72c3ff892e551b2cf9930ab8e6c9 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 20 Jan 2021 18:26:15 -0800 Subject: [PATCH] fix: skip checksum for versions before it was implemented (#182) --- package.json | 2 + src/index.ts | 21 ++++++-- test/fixtures/electron-v1.0.0-darwin-x64.zip | 1 + test/fixtures/electron-v1.0.0-linux-x64.zip | 1 + test/fixtures/electron-v1.0.0-win32-x64.zip | 1 + .../electron-v1.3.3-darwin-x64 copy.zip | 1 + test/fixtures/electron-v1.3.3-darwin-x64.zip | 1 + .../electron-v1.3.3-linux-x64 copy.zip | 1 + test/fixtures/electron-v1.3.3-linux-x64.zip | 1 + .../electron-v1.3.3-win32-x64 copy.zip | 1 + test/fixtures/electron-v1.3.3-win32-x64.zip | 1 + test/fixtures/electron-v1.3.5-darwin-x64.zip | 1 + test/fixtures/electron-v1.3.5-linux-x64.zip | 1 + test/fixtures/electron-v1.3.5-win32-x64.zip | 1 + test/index.spec.ts | 51 +++++++++++++++++++ yarn.lock | 15 +++--- 16 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/electron-v1.0.0-darwin-x64.zip create mode 100644 test/fixtures/electron-v1.0.0-linux-x64.zip create mode 100644 test/fixtures/electron-v1.0.0-win32-x64.zip create mode 100644 test/fixtures/electron-v1.3.3-darwin-x64 copy.zip create mode 100644 test/fixtures/electron-v1.3.3-darwin-x64.zip create mode 100644 test/fixtures/electron-v1.3.3-linux-x64 copy.zip create mode 100644 test/fixtures/electron-v1.3.3-linux-x64.zip create mode 100644 test/fixtures/electron-v1.3.3-win32-x64 copy.zip create mode 100644 test/fixtures/electron-v1.3.3-win32-x64.zip create mode 100644 test/fixtures/electron-v1.3.5-darwin-x64.zip create mode 100644 test/fixtures/electron-v1.3.5-linux-x64.zip create mode 100644 test/fixtures/electron-v1.3.5-win32-x64.zip diff --git a/package.json b/package.json index dbdbaa523..f24723398 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/index.ts b/src/index.ts index 405706462..0d8edbe2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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, @@ -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); diff --git a/test/fixtures/electron-v1.0.0-darwin-x64.zip b/test/fixtures/electron-v1.0.0-darwin-x64.zip new file mode 100644 index 000000000..82fe3529b --- /dev/null +++ b/test/fixtures/electron-v1.0.0-darwin-x64.zip @@ -0,0 +1 @@ +electron-v1.0.0-darwin-x64.zip diff --git a/test/fixtures/electron-v1.0.0-linux-x64.zip b/test/fixtures/electron-v1.0.0-linux-x64.zip new file mode 100644 index 000000000..c408a73ce --- /dev/null +++ b/test/fixtures/electron-v1.0.0-linux-x64.zip @@ -0,0 +1 @@ +electron-v1.0.0-linux-x64.zip diff --git a/test/fixtures/electron-v1.0.0-win32-x64.zip b/test/fixtures/electron-v1.0.0-win32-x64.zip new file mode 100644 index 000000000..9ddfdbd41 --- /dev/null +++ b/test/fixtures/electron-v1.0.0-win32-x64.zip @@ -0,0 +1 @@ +electron-v1.0.0-win32-x64.zip diff --git a/test/fixtures/electron-v1.3.3-darwin-x64 copy.zip b/test/fixtures/electron-v1.3.3-darwin-x64 copy.zip new file mode 100644 index 000000000..6fb87da15 --- /dev/null +++ b/test/fixtures/electron-v1.3.3-darwin-x64 copy.zip @@ -0,0 +1 @@ +electron-v1.3.3-darwin-x64.zip diff --git a/test/fixtures/electron-v1.3.3-darwin-x64.zip b/test/fixtures/electron-v1.3.3-darwin-x64.zip new file mode 100644 index 000000000..393592448 --- /dev/null +++ b/test/fixtures/electron-v1.3.3-darwin-x64.zip @@ -0,0 +1 @@ +electron-v1.3.2-darwin-x64.zip diff --git a/test/fixtures/electron-v1.3.3-linux-x64 copy.zip b/test/fixtures/electron-v1.3.3-linux-x64 copy.zip new file mode 100644 index 000000000..47fb53e6a --- /dev/null +++ b/test/fixtures/electron-v1.3.3-linux-x64 copy.zip @@ -0,0 +1 @@ +electron-v1.3.3-linux-x64.zip diff --git a/test/fixtures/electron-v1.3.3-linux-x64.zip b/test/fixtures/electron-v1.3.3-linux-x64.zip new file mode 100644 index 000000000..47fb53e6a --- /dev/null +++ b/test/fixtures/electron-v1.3.3-linux-x64.zip @@ -0,0 +1 @@ +electron-v1.3.3-linux-x64.zip diff --git a/test/fixtures/electron-v1.3.3-win32-x64 copy.zip b/test/fixtures/electron-v1.3.3-win32-x64 copy.zip new file mode 100644 index 000000000..7671c271a --- /dev/null +++ b/test/fixtures/electron-v1.3.3-win32-x64 copy.zip @@ -0,0 +1 @@ +electron-v1.3.3-win32-x64.zip diff --git a/test/fixtures/electron-v1.3.3-win32-x64.zip b/test/fixtures/electron-v1.3.3-win32-x64.zip new file mode 100644 index 000000000..7671c271a --- /dev/null +++ b/test/fixtures/electron-v1.3.3-win32-x64.zip @@ -0,0 +1 @@ +electron-v1.3.3-win32-x64.zip diff --git a/test/fixtures/electron-v1.3.5-darwin-x64.zip b/test/fixtures/electron-v1.3.5-darwin-x64.zip new file mode 100644 index 000000000..72a7d0c37 --- /dev/null +++ b/test/fixtures/electron-v1.3.5-darwin-x64.zip @@ -0,0 +1 @@ +electron-v1.3.5-darwin-x64.zip diff --git a/test/fixtures/electron-v1.3.5-linux-x64.zip b/test/fixtures/electron-v1.3.5-linux-x64.zip new file mode 100644 index 000000000..7211827cf --- /dev/null +++ b/test/fixtures/electron-v1.3.5-linux-x64.zip @@ -0,0 +1 @@ +electron-v1.3.5-linux-x64.zip diff --git a/test/fixtures/electron-v1.3.5-win32-x64.zip b/test/fixtures/electron-v1.3.5-win32-x64.zip new file mode 100644 index 000000000..34e7fa80f --- /dev/null +++ b/test/fixtures/electron-v1.3.5-win32-x64.zip @@ -0,0 +1 @@ +electron-v1.3.5-win32-x64.zip diff --git a/test/index.spec.ts b/test/index.spec.ts index 9413cc18b..6866f3315 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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(); @@ -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(); + }); + }); }); }); diff --git a/yarn.lock b/yarn.lock index 78c9f771e..c08053b9b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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==