11'use strict' ;
22
33const WebSocket = require ( 'ws' ) ;
4- const { config, kCleanup, kAddNewContext, HTTPLOG } = require ( '../config/constants' ) ;
4+ const {
5+ config,
6+ kCleanup,
7+ kAddNewContext,
8+ HTTPLOG ,
9+ customRequestEnabled,
10+ } = require ( '../config/constants' ) ;
511const logger = require ( '../util/loggerFactory' ) ;
612const Context = require ( './Context' ) ;
713const {
@@ -16,6 +22,12 @@ const { setMetrics } = require('../util/metrics');
1622const AlertManager = require ( '../util/AlertManager' ) ;
1723const Instrumentation = require ( '../util/Instrumentation' ) ;
1824const ErrorHandler = require ( '../util/ErrorHandler' ) ;
25+ const CustomRequestHandler = require ( './CustomRequestHandler' ) ;
26+ const responseHeaders = {
27+ 'content-type' : 'application/json; charset=utf-8' ,
28+ accept : 'application/json' ,
29+ 'WWW-Authenticate' : 'Basic realm="WS Reconnect Proxy"' ,
30+ }
1931
2032/**
2133 * Proxy is the entrypoint and instantiates the context among the socket connection.
@@ -64,9 +76,58 @@ class Proxy {
6476 headers : request . headers ,
6577 } ;
6678 logger . info ( `${ HTTPLOG } Received http request ${ options } ` ) ;
67- if ( request . url . indexOf ( '/status' ) > - 1 ) {
68- response . writeHead ( 200 , { 'content-type' : 'application/json; charset=utf-8' , 'accept' : 'application/json' , 'WWW-Authenticate' : 'Basic realm="WS Reconnect Proxy"' } ) ;
69- response . end ( JSON . stringify ( { "status" : "Running" } ) ) ;
79+ if ( request . url . indexOf ( '/status' ) > - 1 ) {
80+ response . writeHead ( 200 , responseHeaders ) ;
81+ response . end ( JSON . stringify ( { status : 'Running' } ) ) ;
82+ return ;
83+ } else if (
84+ customRequestEnabled &&
85+ request . url . indexOf ( '/customRequest' ) > - 1 &&
86+ request . method == 'POST'
87+ ) {
88+ try {
89+ logger . info ( `Handling request to execute custom command in server` ) ;
90+ let body = '' ;
91+
92+ // Read data from the request
93+ request . on ( 'data' , ( chunk ) => {
94+ body += chunk . toString ( ) ; // Convert Buffer to string
95+ } ) ;
96+
97+ // When the request ends, process the received data
98+ request . on ( 'end' , ( ) => {
99+ body = JSON . parse ( body ) ;
100+ const command = body . command ;
101+ const commandId = body . command . id ;
102+ //Create singleton object and map the command id with pending promise
103+ const customReqInstance = CustomRequestHandler . getInstance ( ) ;
104+ customReqInstance . addCustomRequest ( commandId ) ;
105+
106+ //Send to playwright server
107+ const sessionId = [ ...this . contexts . keys ( ) ] [ 0 ] ;
108+ const sessionContext = this . contexts . get ( sessionId ) ;
109+ sessionContext . outgoingSocket . send ( JSON . stringify ( command ) ) ;
110+
111+ //Get the resolved promise and returning it to end user
112+ customReqInstance . customRequestList [ commandId ] . promise
113+ . then ( ( result ) => {
114+ delete customReqInstance . customRequestList [ commandId ] ;
115+ response . writeHead ( 200 , responseHeaders ) ;
116+ response . end (
117+ JSON . stringify ( { status : 'success' , value : result } )
118+ ) ;
119+ } )
120+ . catch ( ( err ) => {
121+ delete customReqInstance . customRequestList [ commandId ] ;
122+ response . writeHead ( 500 , responseHeaders ) ;
123+ response . end ( JSON . stringify ( { status : 'failure' , value : err } ) ) ;
124+ } ) ;
125+ } ) ;
126+ } catch ( err ) {
127+ logger . error ( `Error while handling custom request ${ err } ` ) ;
128+ response . writeHead ( 500 , responseHeaders ) ;
129+ response . end ( JSON . stringify ( { status : 'failure' , value : err . message } ) ) ;
130+ }
70131 return ;
71132 }
72133 const proxyReq = http . request ( options , ( proxyResponse ) => {
@@ -75,13 +136,14 @@ class Proxy {
75136 end : true ,
76137 } ) ;
77138 } ) ;
78- proxyReq . on ( 'error' , ( error ) => {
139+
140+ proxyReq . on ( 'error' , ( error ) => {
79141 logger . error ( `${ request . url } received error ${ error } ` ) ;
80142 } ) ;
81- proxyReq . on ( 'timeout' , ( ) => {
143+ proxyReq . on ( 'timeout' , ( ) => {
82144 logger . info ( `${ request . url } timed out` ) ;
83145 } ) ;
84- proxyReq . on ( 'drain' , ( ) => {
146+ proxyReq . on ( 'drain' , ( ) => {
85147 logger . info ( `${ request . url } drained out` ) ;
86148 } ) ;
87149 request . pipe ( proxyReq , {
0 commit comments