@@ -267,7 +267,7 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
267267 }
268268 } ;
269269
270- const response = await helper . nodeRequest ( 'POST' , 'api/v1/builds' , data , config , API_URL ) ;
270+ const response = await nodeRequest ( 'POST' , 'api/v1/builds' , data , config , API_URL ) ;
271271 exports . debug ( 'Build creation successfull!' ) ;
272272 process . env . BS_TESTOPS_BUILD_COMPLETED = true ;
273273 setEnvironmentVariablesForRemoteReporter ( response . data . jwt , response . data . build_hashed_id , response . data . allow_screenshots , data . observability_version . sdkVersion ) ;
@@ -303,6 +303,56 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
303303 }
304304}
305305
306+ const httpsScreenshotsKeepAliveAgent = new https . Agent ( {
307+ keepAlive : true ,
308+ timeout : 60000 ,
309+ maxSockets : 2 ,
310+ maxTotalSockets : 2
311+ } ) ;
312+
313+ exports . httpsKeepAliveAgent = new https . Agent ( {
314+ keepAlive : true ,
315+ timeout : 60000 ,
316+ maxSockets : 2 ,
317+ maxTotalSockets : 2
318+ } ) ;
319+
320+ const nodeRequest = ( type , url , data , config , api_url ) => {
321+ return new Promise ( async ( resolve , reject ) => {
322+ const options = { ...config , ...{
323+ method : type ,
324+ url : `${ api_url } /${ url } ` ,
325+ body : data ,
326+ json : config . headers [ 'Content-Type' ] === 'application/json' ,
327+ agent : this . httpsKeepAliveAgent
328+ } } ;
329+
330+ if ( url === exports . requestQueueHandler . screenshotEventUrl ) {
331+ options . agent = httpsScreenshotsKeepAliveAgent ;
332+ }
333+
334+ request ( options , function callback ( error , response , body ) {
335+ if ( error ) {
336+ reject ( error ) ;
337+ } else if ( response . statusCode != 200 ) {
338+ reject ( response && response . body ? response . body : `Received response from BrowserStack Server with status : ${ response . statusCode } ` ) ;
339+ } else {
340+ try {
341+ if ( typeof ( body ) !== 'object' ) body = JSON . parse ( body ) ;
342+ } catch ( e ) {
343+ if ( ! url . includes ( '/stop' ) ) {
344+ reject ( 'Not a JSON response from BrowserStack Server' ) ;
345+ }
346+ }
347+ resolve ( {
348+ data : body
349+ } ) ;
350+ }
351+ } ) ;
352+ } ) ;
353+ }
354+
355+
306356exports . getHookDetails = ( hookTitle ) => {
307357 if ( ! hookTitle || typeof ( hookTitle ) != 'string' ) return [ null , null ] ;
308358 if ( hookTitle . indexOf ( 'hook:' ) !== - 1 ) {
@@ -355,7 +405,7 @@ exports.batchAndPostEvents = async (eventUrl, kind, data) => {
355405 } ;
356406
357407 try {
358- const response = await helper . nodeRequest ( 'POST' , eventUrl , data , config , API_URL ) ;
408+ const response = await nodeRequest ( 'POST' , eventUrl , data , config , API_URL ) ;
359409 if ( response . data . error ) {
360410 throw ( { message : response . data . error } ) ;
361411 } else {
@@ -418,7 +468,7 @@ exports.uploadEventData = async (eventData, run=0) => {
418468 } ;
419469
420470 try {
421- const response = await helper . nodeRequest ( 'POST' , event_api_url , data , config , API_URL ) ;
471+ const response = await nodeRequest ( 'POST' , event_api_url , data , config , API_URL ) ;
422472 if ( response . data . error ) {
423473 throw ( { message : response . data . error } ) ;
424474 } else {
@@ -527,7 +577,7 @@ exports.stopBuildUpstream = async () => {
527577 } ;
528578
529579 try {
530- const response = await helper . nodeRequest ( 'PUT' , `api/v1/builds/${ process . env . BS_TESTOPS_BUILD_HASHED_ID } /stop` , data , config , API_URL ) ;
580+ const response = await nodeRequest ( 'PUT' , `api/v1/builds/${ process . env . BS_TESTOPS_BUILD_HASHED_ID } /stop` , data , config , API_URL ) ;
531581 if ( response . data && response . data . error ) {
532582 throw ( { message : response . data . error } ) ;
533583 } else {
0 commit comments