1
1
const { chmodSync, createWriteStream, existsSync } = require ( "fs" ) ;
2
- const { get : httpsGet } = require ( "https" ) ;
3
2
const { tmpdir } = require ( "os" ) ;
4
3
const { dirname, join } = require ( "path" ) ;
4
+
5
5
const { mkdir } = require ( "shelljs" ) ;
6
+ const { get } = require ( "request" ) ;
7
+ const { getProxySettings } = require ( "proxy-lib" ) ;
6
8
7
9
const CONSTANTS = {
8
10
SNAPSHOT_TMP_DIR : join ( tmpdir ( ) , "snapshot-tools" ) ,
@@ -12,49 +14,51 @@ const createDirectory = dir => mkdir('-p', dir);
12
14
13
15
const downloadFile = ( url , destinationFilePath ) =>
14
16
new Promise ( ( resolve , reject ) => {
15
- const request = httpsGet ( url , response => {
16
- switch ( response . statusCode ) {
17
- case 200 :
18
- const file = createWriteStream ( destinationFilePath , { autoClose : true } ) ;
19
- file . on ( 'error' , function ( error ) {
20
- return reject ( error ) ;
21
- } ) ;
22
- file . on ( "finish" , function ( ) {
17
+ getRequestOptions ( url )
18
+ . then ( options =>
19
+ get ( options )
20
+ . on ( "error" , reject )
21
+ . pipe ( createWriteStream ( destinationFilePath , { autoClose : true } ) )
22
+ . on ( "finish" , _ => {
23
23
chmodSync ( destinationFilePath , 0755 ) ;
24
24
return resolve ( destinationFilePath ) ;
25
- } ) ;
26
- response . pipe ( file ) ;
27
- break ;
28
- case 301 :
29
- case 302 :
30
- case 303 :
31
- const redirectUrl = response . headers . location ;
32
- return this . downloadExecFile ( redirectUrl , destinationFilePath ) ;
33
- default :
34
- return reject ( new Error ( "Unable to download file at " + url + ". Status code: " + response . statusCode ) ) ;
35
- }
36
- } ) ;
25
+ } )
26
+ ) . catch ( reject ) ;
27
+ } ) ;
37
28
38
- request . end ( ) ;
29
+ const getJsonFile = url =>
30
+ new Promise ( ( resolve , reject ) => {
31
+ getRequestOptions ( url )
32
+ . then ( options =>
33
+ get ( options , ( error , response , body ) => {
34
+ if ( error ) {
35
+ return reject ( error ) ;
36
+ }
37
+
38
+ if ( ! response || response . statusCode !== 200 ) {
39
+ return reject ( `Couldn't fetch ${ url } ! Response:\n${ response } ` ) ;
40
+ }
39
41
40
- request . on ( 'error' , function ( err ) {
41
- return reject ( err ) ;
42
- } ) ;
42
+ try {
43
+ const data = JSON . parse ( body ) ;
44
+ resolve ( data ) ;
45
+ } catch ( error ) {
46
+ reject ( `Couldn't parse json data! Original error:\n${ error } ` ) ;
47
+ }
48
+ } )
49
+ ) . catch ( reject ) ;
43
50
} ) ;
44
51
45
- const getJsonFile = url =>
52
+ const getRequestOptions = ( url ) =>
46
53
new Promise ( ( resolve , reject ) => {
47
- httpsGet ( url , res => {
48
- let body = "" ;
49
- res . on ( "data" , chunk => {
50
- body += chunk ;
54
+ const options = { url } ;
55
+ getProxySettings ( )
56
+ . then ( proxySettings => {
57
+ const allOptions = Object . assign ( options , proxySettings ) ;
58
+ resolve ( allOptions ) ;
51
59
} )
52
-
53
- res . on ( "end" , ( ) => {
54
- const data = JSON . parse ( body ) ;
55
- return resolve ( data ) ;
56
- } ) ;
57
- } ) . on ( "error" , reject ) ;
60
+ . catch ( error =>
61
+ reject ( `Couldn't get proxy settings! Original error:\n${ error } ` ) ) ;
58
62
} ) ;
59
63
60
64
module . exports = {
0 commit comments