Skip to content

Commit

Permalink
fix: download electron checksum failure
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Aug 8, 2022
1 parent 5c81f9a commit eb92398
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 118 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"README.md"
],
"engines": {
"node": ">=8.6"
"node": ">=10.19.0"
},
"dependencies": {
"debug": "^4.1.1",
"env-paths": "^2.2.0",
"fs-extra": "^8.1.0",
"got": "^9.6.0",
"got": "^11.8.3",
"progress": "^2.0.3",
"semver": "^6.2.0",
"sumchecker": "^3.0.1"
Expand All @@ -40,7 +40,7 @@
"@types/fs-extra": "^8.0.0",
"@types/got": "^9.4.4",
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"@types/node": "^18.6.4",
"@types/progress": "^2.0.3",
"@types/semver": "^6.2.0",
"@typescript-eslint/eslint-plugin": "^2.34.0",
Expand Down
36 changes: 27 additions & 9 deletions src/GotDownloader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import * as fs from 'fs-extra';
import * as got from 'got';
import got, { Options, Progress, RequestError } from 'got';
import * as path from 'path';
import * as ProgressBar from 'progress';

import { Downloader } from './Downloader';

const PROGRESS_BAR_DELAY_IN_SECONDS = 30;

type GotStreamOptions = Options & {
isStream?: true;
};

/**
* See [`got#options`](https://github.com/sindresorhus/got#options) for possible keys/values.
* See [`got#options`](https://github.com/sindresorhus/got/tree/v11.8.5#options) for possible keys/values.
*/
export type GotDownloaderOptions = got.GotOptions<string | null> & {
export type GotDownloaderOptions = GotStreamOptions & {
/**
* if defined, triggers every time `got`'s `downloadProgress` event callback is triggered.
*/
getProgressCallback?: (progress: got.Progress) => Promise<void>;
getProgressCallback?: (progress: Progress) => Promise<void>;
/**
* if `true`, disables the console progress bar (setting the `ELECTRON_GET_NO_PROGRESS`
* environment variable to a non-empty value also does this).
Expand Down Expand Up @@ -56,8 +60,25 @@ export class GotDownloader implements Downloader<GotDownloaderOptions> {
}
}, PROGRESS_BAR_DELAY_IN_SECONDS * 1000);
}
await new Promise((resolve, reject) => {
const downloadStream = got.stream(url, gotOptions);
await new Promise<void>((resolve, reject) => {
const downloadStream = got.stream(url, {
hooks: {
beforeError: [
(error): RequestError => {
const { response } = error;

if (response) {
if (error.name === 'HTTPError' && response.statusCode === 404) {
error.message += ` for ${response.url}`;
}
}

return error;
},
],
},
...gotOptions,
});
downloadStream.on('downloadProgress', async progress => {
progressPercent = progress.percent;
if (bar) {
Expand All @@ -68,9 +89,6 @@ export class GotDownloader implements Downloader<GotDownloaderOptions> {
}
});
downloadStream.on('error', error => {
if (error.name === 'HTTPError' && error.statusCode === 404) {
error.message += ` for ${error.url}`;
}
if (writeStream.destroy) {
writeStream.destroy(error);
}
Expand Down
1 change: 0 additions & 1 deletion test/checksums.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as crypto from 'crypto';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
Expand Down
25 changes: 18 additions & 7 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ describe('utils', () => {
Object.defineProperty(process, 'arch', {
value: savedArch,
});
process.config.variables = savedVariables;
Object.defineProperty(process.config, 'variables', {
value: savedVariables,
});
});

it('should return process.arch on x64', () => {
Expand All @@ -101,8 +103,9 @@ describe('utils', () => {
Object.defineProperty(process, 'arch', {
value: 'arm',
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
process.config.variables = {} as any;
Object.defineProperty(process.config, 'variables', {
value: {},
});
expect(getHostArch()).toEqual('armv7l');
});

Expand All @@ -111,8 +114,12 @@ describe('utils', () => {
Object.defineProperty(process, 'arch', {
value: 'arm',
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/camelcase
process.config.variables = { arm_version: '6' } as any;
Object.defineProperty(process.config, 'variables', {
value: {
// eslint-disable-next-line @typescript-eslint/camelcase
arm_version: '6',
},
});
expect(getHostArch()).toEqual(uname());
});
}
Expand All @@ -121,8 +128,12 @@ describe('utils', () => {
Object.defineProperty(process, 'arch', {
value: 'arm',
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/camelcase
process.config.variables = { arm_version: '7' } as any;
Object.defineProperty(process.config, 'variables', {
value: {
// eslint-disable-next-line @typescript-eslint/camelcase
arm_version: '7',
},
});
expect(getHostArch()).toEqual('armv7l');
});
});
Expand Down
Loading

0 comments on commit eb92398

Please sign in to comment.