1- import chalk from 'chalk' ;
21import * as Debug from 'debug' ;
32
43import * as superagentType from 'superagent' ;
@@ -10,121 +9,76 @@ import { CreateRequestOptions, HttpMethod } from '../../definitions';
109
1110const debug = Debug ( 'ionic:cli-utils:lib:utils:http' ) ;
1211
13- let _proxyInstalled = false ;
14-
15- let CAS : string [ ] | undefined ;
16- let CERTS : string [ ] | undefined ;
17- let KEYS : string [ ] | undefined ;
18-
1912export const PROXY_ENVIRONMENT_VARIABLES : ReadonlyArray < string > = [ 'IONIC_HTTP_PROXY' , 'HTTPS_PROXY' , 'HTTP_PROXY' , 'PROXY' , 'https_proxy' , 'http_proxy' , 'proxy' ] ;
2013
21- function getGlobalProxy ( ) : [ string , string ] | [ undefined , undefined ] {
14+ function getGlobalProxy ( ) : { envvar : string ; envval : string ; } | undefined {
2215 for ( const envvar of PROXY_ENVIRONMENT_VARIABLES ) {
23- if ( process . env [ envvar ] ) {
24- return [ process . env [ envvar ] , envvar ] ;
16+ const envval = process . env [ envvar ] ;
17+
18+ if ( envval ) {
19+ return { envval, envvar } ;
2520 }
2621 }
27-
28- return [ undefined , undefined ] ;
2922}
3023
31- export async function installProxy ( superagent : superagentType . SuperAgentStatic , proxy : string , proxyVar : string ) : Promise < boolean > {
32- if ( _proxyInstalled ) {
33- return true ;
34- }
24+ export async function createRequest ( method : HttpMethod , url : string , { proxy, ssl } : CreateRequestOptions ) : Promise < { req : superagentType . SuperAgentRequest ; } > {
25+ const superagent = await import ( 'superagent' ) ;
3526
36- debug ( `Detected ${ chalk . green ( proxyVar ) } in environment` ) ;
27+ if ( ! proxy ) {
28+ const gproxy = getGlobalProxy ( ) ;
3729
38- try {
39- const superagentProxy = await import ( 'superagent-proxy' ) ;
40- superagentProxy ( superagent ) ;
41- _proxyInstalled = true ;
42- debug ( `Proxy installed to ${ chalk . green ( proxy ) } ` ) ;
43- } catch ( e ) {
44- if ( e . code !== 'MODULE_NOT_FOUND' ) {
45- throw e ;
30+ if ( gproxy ) {
31+ proxy = gproxy . envval ;
4632 }
47-
48- debug ( `Missing proxy package: ${ chalk . bold ( 'superagent-proxy' ) } ` ) ;
49-
50- return false ;
5133 }
5234
53- return true ;
54- }
55-
56- export async function createRequest ( method : HttpMethod , url : string , opts ?: CreateRequestOptions ) : Promise < { req : superagentType . SuperAgentRequest ; } > {
57- const superagent = await import ( 'superagent' ) ;
58- const [ proxy , proxyVar ] = getGlobalProxy ( ) ;
59-
6035 const req = superagent ( method , url ) ;
6136
6237 req . redirects ( 25 ) ;
6338
64- if ( proxy && proxyVar ) {
65- await installProxy ( superagent , proxy , proxyVar ) ;
39+ if ( proxy ) {
40+ const superagentProxy = await import ( 'superagent-proxy' ) ;
41+ superagentProxy ( superagent ) ;
6642
6743 if ( req . proxy ) {
6844 req . proxy ( proxy ) ;
45+ } else {
46+ debug ( `Cannot install proxy--req.proxy not defined` ) ;
6947 }
7048 }
7149
72- if ( opts && opts . ssl ) {
73- if ( ! CAS ) {
74- CAS = await Promise . all ( conform ( opts . ssl . cafile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
75- }
76-
77- if ( ! CERTS ) {
78- CERTS = await Promise . all ( conform ( opts . ssl . certfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
79- }
80-
81- if ( ! KEYS ) {
82- KEYS = await Promise . all ( conform ( opts . ssl . keyfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ;
83- }
84-
85- if ( CAS . length > 0 ) {
86- req . ca ( CAS ) ;
87- }
88-
89- if ( CERTS . length > 0 ) {
90- req . cert ( CERTS ) ;
91- }
92-
93- if ( KEYS . length > 0 ) {
94- req . key ( KEYS ) ;
95- }
50+ if ( ssl ) {
51+ req . ca ( await Promise . all ( conform ( ssl . cafile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ) ;
52+ req . cert ( await Promise . all ( conform ( ssl . certfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ) ;
53+ req . key ( await Promise . all ( conform ( ssl . keyfile ) . map ( p => fsReadFile ( p , { encoding : 'utf8' } ) ) ) ) ;
9654 }
9755
9856 return { req } ;
9957}
10058
101- export async function download ( url : string , ws : NodeJS . WritableStream , opts ?: { progress ?: ( loaded : number , total : number ) => void ; } & CreateRequestOptions ) {
102- const { req } = await createRequest ( 'GET' , url , opts ) ;
103-
104- const progressFn = opts ? opts . progress : undefined ;
105-
59+ export async function download ( req : superagentType . SuperAgentRequest , ws : NodeJS . WritableStream , { progress } : { progress ?: ( loaded : number , total : number ) => void ; } ) : Promise < void > {
10660 return new Promise < void > ( ( resolve , reject ) => {
10761 req
10862 . on ( 'response' , res => {
10963 if ( res . statusCode !== 200 ) {
11064 reject ( new Error (
111- `Encountered bad status code (${ res . statusCode } ) for ${ url } \n` +
65+ `Encountered bad status code (${ res . statusCode } ) for ${ req . url } \n` +
11266 `This could mean the server is experiencing difficulties right now--please try again later.`
11367 ) ) ;
11468 }
11569
116- if ( progressFn ) {
70+ if ( progress ) {
11771 let loaded = 0 ;
11872 const total = Number ( res . headers [ 'content-length' ] ) ;
11973 res . on ( 'data' , chunk => {
12074 loaded += chunk . length ;
121- progressFn ( loaded , total ) ;
75+ progress ( loaded , total ) ;
12276 } ) ;
12377 }
12478 } )
12579 . on ( 'error' , err => {
12680 if ( err . code === 'ECONNABORTED' ) {
127- reject ( new Error ( `Timeout of ${ err . timeout } ms reached for ${ url } ` ) ) ;
81+ reject ( new Error ( `Timeout of ${ err . timeout } ms reached for ${ req . url } ` ) ) ;
12882 } else {
12983 reject ( err ) ;
13084 }
0 commit comments