13
13
14
14
import { GenericResponse } from '../../api/src/model/response' ;
15
15
import { HttpRequester } from '../../api/src/client/request-factory/http-request-sender' ;
16
- import { IncomingMessage } from 'http' ;
16
+ import * as http from 'http' ;
17
17
import * as https from 'https' ;
18
18
import { ProxyAgent } from './proxy-settings' ;
19
19
20
20
/**
21
21
* NodeJs implementation of a Http Requester.
22
22
*/
23
23
export class NodeRequester implements HttpRequester {
24
- private readonly proxyAgent : any ;
25
24
26
- public constructor ( agent : ProxyAgent ) {
27
- this . proxyAgent = agent . proxyAgent ;
25
+ public constructor ( private readonly agent : ProxyAgent ) {
28
26
}
29
27
30
28
private static createResponseHandler ( resolve : ( value ?: ( PromiseLike < GenericResponse > | GenericResponse ) ) => void ,
31
29
reject : ( reason ?: any ) => void ) :
32
- ( response : IncomingMessage ) => void {
30
+ ( response : http . IncomingMessage ) => void {
33
31
return response => {
34
32
this . handleResponse ( resolve , reject , response ) ;
35
33
} ;
36
34
}
37
35
38
36
private static handleResponse ( resolve : ( value ?: ( PromiseLike < GenericResponse > | GenericResponse ) ) => void ,
39
37
reject : ( reason ?: any ) => void ,
40
- response : IncomingMessage ) : void {
38
+ response : http . IncomingMessage ) : void {
41
39
let data = '' ;
42
40
response . on ( 'data' , chunk => {
43
41
data += chunk ;
@@ -64,7 +62,11 @@ export class NodeRequester implements HttpRequester {
64
62
65
63
public async doRequest ( method : string , url : string , header : Map < string , string > , payload : string ) : Promise < GenericResponse > {
66
64
return new Promise < GenericResponse > ( ( ( resolve , reject ) => {
67
- const req = https . request ( this . buildRequest ( method , url , header , payload ) , NodeRequester . createResponseHandler ( resolve , reject ) ) ;
65
+ const parsedUrl = new URL ( url ) ;
66
+ const isSecureRequest = parsedUrl . protocol === 'https:' ;
67
+ const client = isSecureRequest ? https : http ;
68
+ const requestOptions = this . buildRequest ( method , parsedUrl , header , payload , isSecureRequest ) ;
69
+ const req = client . request ( requestOptions , NodeRequester . createResponseHandler ( resolve , reject ) ) ;
68
70
req . on ( 'error' , e => {
69
71
throw new Error ( String ( e ) ) ;
70
72
} ) ;
@@ -79,27 +81,31 @@ export class NodeRequester implements HttpRequester {
79
81
* Builds an options object to perform a Http request with.
80
82
*
81
83
* @param method - The type of action to perform.
82
- * @param url - The Url to send the request to.
84
+ * @param parsedUrl - The Url to send the request to.
83
85
* @param header - The headers of the request.
84
86
* @param body - The payload to send with the request.
87
+ * @param isSecureRequest - If the request is a secure HTTPS request.
85
88
* @return the builder.
86
89
*/
87
- private buildRequest ( method : string , url : string , header : Map < string , string > , body : string ) : object {
90
+ private buildRequest ( method : string , parsedUrl : URL , header : Map < string , string > , body : string ,
91
+ isSecureRequest : boolean ) : object {
88
92
if ( body !== undefined && body !== '' ) {
89
93
header . set ( 'Content-Length' , Buffer . byteLength ( body ) . toString ( ) ) ;
90
94
}
91
95
const headers : { [ key : string ] : any } = { } ;
92
96
header . forEach ( ( v , k ) => headers [ k ] = v ) ;
93
- const parsedUrl = new URL ( url ) ;
94
- const request : { [ key : string ] : any } = {
97
+ return {
95
98
method,
96
99
headers,
97
- host : parsedUrl . host ,
98
- path : parsedUrl . pathname
100
+ hostname : parsedUrl . hostname ,
101
+ port : parsedUrl . port ,
102
+ path : parsedUrl . pathname ,
103
+ agent : this . getAgentForRequestType ( isSecureRequest )
99
104
} ;
100
- if ( this . proxyAgent !== undefined && this . proxyAgent . options . path !== undefined ) {
101
- request [ 'agent' ] = this . proxyAgent ;
102
- }
103
- return request ;
104
105
}
106
+
107
+ private getAgentForRequestType ( isSecureRequest : boolean ) : http . Agent | undefined {
108
+ return isSecureRequest ? this . agent . proxyAgent : this . agent . httpProxyAgent ;
109
+ }
110
+
105
111
}
0 commit comments