@@ -4,24 +4,9 @@ var fs = require('fs');
44import * as tl from "azure-pipelines-task-lib/task" ;
55
66import httpClient = require( "typed-rest-client/HttpClient" ) ;
7+ import restInterfaces = require( "typed-rest-client/Interfaces" ) ;
78import util = require( "util" ) ;
89
9-
10- let proxyUrl : string = tl . getVariable ( "agent.proxyurl" ) ;
11- var requestOptions : any = proxyUrl ? {
12- proxy : {
13- proxyUrl : proxyUrl ,
14- proxyUsername : tl . getVariable ( "agent.proxyusername" ) ,
15- proxyPassword : tl . getVariable ( "agent.proxypassword" ) ,
16- proxyBypassHosts : tl . getVariable ( "agent.proxybypasslist" ) ? JSON . parse ( tl . getVariable ( "agent.proxybypasslist" ) ) : null
17- }
18- } : { } ;
19-
20- let ignoreSslErrors : string = tl . getVariable ( "VSTS_ARM_REST_IGNORE_SSL_ERRORS" ) ;
21- requestOptions . ignoreSslError = ignoreSslErrors && ignoreSslErrors . toLowerCase ( ) == "true" ;
22-
23- var httpCallbackClient = new httpClient . HttpClient ( tl . getVariable ( "AZURE_HTTP_USER_AGENT" ) , null , requestOptions ) ;
24-
2510export class WebRequest {
2611 public method : string ;
2712 public uri : string ;
@@ -43,6 +28,13 @@ export class WebRequestOptions {
4328 public retryIntervalInSeconds : number ;
4429 public retriableStatusCodes : number [ ] ;
4530 public retryRequestTimedout : boolean ;
31+ public httpGlobalAgentOptions ?: IHttpGlobalAgentOptions ;
32+ public socketTimeout ?: number ;
33+ }
34+
35+ export interface IHttpGlobalAgentOptions {
36+ keepAlive ?: boolean ;
37+ timeout ?: number ;
4638}
4739
4840export async function sendRequest ( request : WebRequest , options ?: WebRequestOptions ) : Promise < WebResponse > {
@@ -52,9 +44,27 @@ export async function sendRequest(request: WebRequest, options?: WebRequestOptio
5244 let retriableErrorCodes = options && options . retriableErrorCodes ? options . retriableErrorCodes : [ "ETIMEDOUT" , "ECONNRESET" , "ENOTFOUND" , "ESOCKETTIMEDOUT" , "ECONNREFUSED" , "EHOSTUNREACH" , "EPIPE" , "EA_AGAIN" ] ;
5345 let retriableStatusCodes = options && options . retriableStatusCodes ? options . retriableStatusCodes : [ 408 , 409 , 500 , 502 , 503 , 504 ] ;
5446 let timeToWait : number = retryIntervalInSeconds ;
47+
48+ let proxyUrl : string = tl . getVariable ( "agent.proxyurl" ) ;
49+ const requestOptions : restInterfaces . IRequestOptions = proxyUrl ? {
50+ proxy : {
51+ proxyUrl : proxyUrl ,
52+ proxyUsername : tl . getVariable ( "agent.proxyusername" ) ,
53+ proxyPassword : tl . getVariable ( "agent.proxypassword" ) ,
54+ proxyBypassHosts : tl . getVariable ( "agent.proxybypasslist" ) ? JSON . parse ( tl . getVariable ( "agent.proxybypasslist" ) ) : null
55+ }
56+ } : { } ;
57+
58+ let ignoreSslErrors : string = tl . getVariable ( "VSTS_ARM_REST_IGNORE_SSL_ERRORS" ) ;
59+ requestOptions . ignoreSslError = ignoreSslErrors && ignoreSslErrors . toLowerCase ( ) == "true" ;
60+ requestOptions . globalAgentOptions = options . httpGlobalAgentOptions ;
61+ requestOptions . socketTimeout = options . socketTimeout ;
62+
63+ const httpCallbackClient = new httpClient . HttpClient ( tl . getVariable ( "AZURE_HTTP_USER_AGENT" ) , null , requestOptions ) ;
64+
5565 while ( true ) {
5666 try {
57- let response : WebResponse = await sendRequestInternal ( request ) ;
67+ let response : WebResponse = await sendRequestInternal ( request , httpCallbackClient ) ;
5868 if ( retriableStatusCodes . indexOf ( response . statusCode ) != - 1 && ++ i < retryCount ) {
5969 tl . debug ( util . format ( "Encountered a retriable status code: %s. Message: '%s'." , response . statusCode , response . statusMessage ) ) ;
6070 await sleepFor ( timeToWait ) ;
@@ -87,7 +97,7 @@ export function sleepFor(sleepDurationInSeconds): Promise<any> {
8797 } ) ;
8898}
8999
90- async function sendRequestInternal ( request : WebRequest ) : Promise < WebResponse > {
100+ async function sendRequestInternal ( request : WebRequest , httpCallbackClient : httpClient . HttpClient ) : Promise < WebResponse > {
91101 tl . debug ( util . format ( "[%s]%s" , request . method , request . uri ) ) ;
92102 var response : httpClient . HttpClientResponse = await httpCallbackClient . request ( request . method , request . uri , request . body , request . headers ) ;
93103 return await toWebResponse ( response ) ;
0 commit comments