Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
feat: Add custom HTTP timeout (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Sep 5, 2019
1 parent 8ef0a62 commit a187352
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ Allowed version argument inputs:
- "all"
Options:
-d, --debug Enable debug logging
-f, --force Force downloading the latest release file
-l, --limit <number> Limit output of releases
-r, --raw Output raw JSON
-s, --source <url> Use a custom releases source URL or path
--no-colors Don't use colors for displaying
--no-prereleases Don't include Electron prereleases
-v, --version output the version number
-h, --help output usage information
-d, --debug Enable debug logging
-f, --force Force downloading the latest release file
-l, --limit <number> Limit output of releases
-r, --raw Output raw JSON
-s, --source <url> Use a custom releases source URL or path
-t, --timeout <number> Use a custom HTTP timeout
--no-colors Don't use colors for displaying
--no-prereleases Don't include Electron prereleases
-v, --version output the version number
-h, --help output usage information
Commands:
electron|e [version] Get informations about an Electron release
chrome|c [version] Get informations about a chrome release
modules|m [version] Get informations about a modules release
node|n [version] Get informations about a node release
openssl|o [version] Get informations about an openssl release
uv|u [version] Get informations about an uv release
v8|v [version] Get informations about a v8 release
zlib|z [version] Get informations about a zlib release
all Get informations about all releases
electron|e [version] Get informations about an Electron release
chrome|c [version] Get informations about a chrome release
modules|m [version] Get informations about a modules release
node|n [version] Get informations about a node release
openssl|o [version] Get informations about an openssl release
uv|u [version] Get informations about an uv release
v8|v [version] Get informations about a v8 release
zlib|z [version] Get informations about a zlib release
all|a Get informations about all releases
```

### Examples
Expand Down
2 changes: 1 addition & 1 deletion spec/ElectronInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as nock from 'nock';
import * as path from 'path';
import * as uuid from 'uuid';

import {ElectronInfo, RawReleaseInfo} from '../src';
import {ElectronInfo, RawReleaseInfo} from '../src/ElectronInfo';

const tempDir = path.resolve(__dirname, '.temp');
const tempDirDownload = path.resolve(__dirname, '.temp/download');
Expand Down
33 changes: 33 additions & 0 deletions spec/FileService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as nock from 'nock';

import {FileService} from '../src/FileService';

const mockUrl = 'http://example.com';

describe('FileService', () => {
const fileService = new FileService({
debug: false,
electronPrereleases: false,
forceUpdate: false,
limit: 0,
releasesUrl: mockUrl,
tempDirectory: '',
timeout: 500,
});

beforeEach(() =>
nock(mockUrl)
.get('/')
.delayBody(5000)
.reply(200, [{data: 'invalid'}])
);

afterEach(() => nock.cleanAll());

describe('downloadReleasesFile', () => {
it('honors a custom timeout', async () => {
const result = await fileService['downloadReleasesFile'](mockUrl, '');
expect(result).toEqual([]);
});
});
});
3 changes: 3 additions & 0 deletions src/ElectronInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface Options {
* Use a custom temporary directory. If not defined, the system's temporary directory will be used.
*/
tempDirectory?: string;
/** Use a custom HTTP timeout in milliseconds. Default is `2000`. */
timeout?: number;
}

const {bold} = Chalk;
Expand All @@ -55,6 +57,7 @@ const defaultOptions: Required<Options> = {
limit: 0,
releasesUrl: 'https://unpkg.com/electron-releases@latest/lite.json',
tempDirectory: '',
timeout: 2000,
};

export const SupportedDependencies: RawDeps = {
Expand Down
11 changes: 10 additions & 1 deletion src/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ export class FileService {

private async downloadReleasesFile(downloadUrl: string, targetFile: string): Promise<RawReleaseInfo[]> {
this.logger.log('Downloading releases file:', {downloadUrl, targetFile});
const {data: releases} = await axios.get<RawReleaseInfo[]>(downloadUrl);

let releases = [];

try {
const response = await axios.get<RawReleaseInfo[]>(downloadUrl, {timeout: this.options.timeout});
releases = response.data;
} catch (error) {
this.logger.warn(`Request failed: "${error.message}"`);
return [];
}

this.logger.info(
'Received data from server:',
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Allowed version argument inputs:
.option('-l, --limit <number>', 'Limit output of releases')
.option('-r, --raw', 'Output raw JSON')
.option('-s, --source <url>', 'Use a custom releases source URL or path')
.option('-t, --timeout <number>', 'Use a custom HTTP timeout')
.option('--no-colors', `Don't use colors for displaying`)
.option('--no-prereleases', `Don't include Electron prereleases`)
.version(version, '-v, --version');
Expand All @@ -48,6 +49,7 @@ program
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
...(parent.timeout && {timeout: parseInt(parent.timeout, 10)}),
});

const releases = parent.raw
Expand Down Expand Up @@ -80,6 +82,7 @@ for (const dependency in SupportedDependencies) {
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
...(parent.timeout && {timeout: parent.timeout}),
});

const releases = parent.raw
Expand All @@ -106,11 +109,13 @@ program
...(parent.limit && {limit: parseInt(parent.limit, 10)}),
...(parent.prereleases && {electronPrereleases: parent.prereleases}),
...(parent.source && {releasesUrl: parent.source}),
...(parent.timeout && {timeout: parent.timeout}),
});

const releases = parent.raw
? await electronInfo.getAllReleases()
: await electronInfo.getAllReleases(true, parent.colors);

console.log(releases);
} catch (error) {
console.error(error);
Expand Down

0 comments on commit a187352

Please sign in to comment.