Skip to content

Commit 4930ed1

Browse files
refactor
1 parent 406abf6 commit 4930ed1

File tree

13 files changed

+45
-46
lines changed

13 files changed

+45
-46
lines changed

config/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports.PORTS = {
1717
MIN: 1
1818
};
1919

20-
module.exports.REQUEST_TYPES = {
20+
module.exports.SERVER_TYPES = {
2121
PROXY: "proxy",
2222
REVERSE_PROXY: "reverse proxy"
2323
};

src/commandLine.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var CommandLineManager = {
100100
argv.splice(index, 1);
101101
}
102102
}
103-
103+
104104
// delay for retries in case of request failures
105105
index = argv.indexOf('--retry-delay');
106106
if (index !== -1) {
@@ -156,7 +156,7 @@ var CommandLineManager = {
156156
argv.splice(index, 1);
157157
}
158158
}
159-
159+
160160
// process proxy host
161161
index = argv.indexOf('--proxy-host');
162162
if (index !== -1) {
@@ -195,7 +195,7 @@ var CommandLineManager = {
195195
argv.splice(index, 1);
196196
}
197197
}
198-
198+
199199
// if proxy port value in invalid or doesn't exist and host exists, set the default value
200200
if (RdGlobalConfig.proxy && RdGlobalConfig.proxy.host && (invalidArgs.has('--proxy-port') || !RdGlobalConfig.proxy.port)) {
201201
console.log('\nSetting Default Proxy Port:', constants.DEFAULT_PROXY_PORT, '\n');
@@ -234,7 +234,7 @@ var CommandLineManager = {
234234
argv.splice(index, 1);
235235
}
236236
}
237-
237+
238238
// if proxy pass is invalid or doesn't exist and username exists, set the password as empty
239239
if (RdGlobalConfig.proxy && RdGlobalConfig.proxy.username && (invalidArgs.has('--proxy-pass') || !RdGlobalConfig.proxy.password)) {
240240
console.log('Setting Proxy Password as Empty\n');

src/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var LogManager = {
3636
newLogger.transports.file.json = false;
3737

3838
newLogger.info("************* LOGGER INITIALIZED **************\r\n");
39-
39+
4040
return {
4141
info: function (topic, message, stringify, data, uuid) {
4242
stringify = stringify || false;

src/proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var RdHandler = {
101101
furtherRequestOptions: furtherRequestOptions
102102
};
103103

104-
ReqLib.call(paramsForRequest, clientRequest, constants.REQUEST_TYPES.PROXY)
104+
ReqLib.call(paramsForRequest, clientRequest, constants.SERVER_TYPES.PROXY)
105105
.then(function (response) {
106106
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + clientRequest.url + ', Status Code: ' + response.statusCode,
107107
false, {

src/requestLib.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var RequestLib = {
2020
* @param {http.IncomingMessage} clientRequest
2121
* @param {Number} retries
2222
*/
23-
_makeRequest: function (schemeObj, params, clientRequest, requestType, retries) {
23+
_makeRequest: function (schemeObj, params, clientRequest, serverType, retries) {
2424
return new Promise(function (resolve, reject) {
2525
var requestOptions = Object.assign({}, params.furtherRequestOptions);
2626
requestOptions.agent = RdGlobalConfig.SCHEME === 'http' ? httpKeepAliveAgent : httpsKeepAliveAgent;
@@ -45,12 +45,12 @@ var RequestLib = {
4545
response.on('data', function (chunk) {
4646
responseToSend.data.push(chunk);
4747
});
48-
48+
4949
response.on('end', function () {
5050
responseToSend.data = Buffer.concat(responseToSend.data).toString();
5151
resolve(responseToSend);
5252
});
53-
53+
5454
response.on('error', function (err) {
5555
reject({
5656
message: err,
@@ -61,9 +61,9 @@ var RequestLib = {
6161

6262
// Log the request that will be initiated on behalf of the client
6363
request.on('finish', function () {
64-
var url = RdGlobalConfig.SCHEME + "://" + requestOptions.host + requestOptions.path;
64+
var requestUrl = RdGlobalConfig.SCHEME + "://" + requestOptions.host + requestOptions.path;
6565
RdGlobalConfig.reqLogger.info(constants.TOPICS.TOOL_REQUEST_WITH_RETRIES + retries,
66-
clientRequest.method + ' ' + url, false, Object.assign({}, params.furtherRequestOptions, {
66+
clientRequest.method + ' ' + requestUrl, false, Object.assign({}, params.furtherRequestOptions, {
6767
data: Buffer.concat(params.request.data).toString()
6868
}),
6969
clientRequest.id);
@@ -98,18 +98,18 @@ var RequestLib = {
9898
});
9999
}
100100
});
101-
101+
102102
clientRequest.on('error', function (err) {
103103
request.end();
104104
reject({
105105
message: err,
106106
customTopic: constants.TOPICS.CLIENT_REQUEST_WITH_RETRIES + retries
107107
});
108108
});
109-
109+
110110
clientRequest.on('end', function () {
111-
var url = requestType === constants.REQUEST_TYPES.PROXY ? clientRequest.url : "http://" + clientRequest.headers.host + clientRequest.url;
112-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_END, params.request.method + ' ' + url, false, {
111+
var requestUrl = serverType === constants.SERVER_TYPES.PROXY ? clientRequest.url : "http://" + clientRequest.headers.host + requestOptions.path;
112+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_END, params.request.method + ' ' + requestUrl, false, {
113113
data: Buffer.concat(params.request.data).toString()
114114
},
115115
clientRequest.id);
@@ -128,10 +128,10 @@ var RequestLib = {
128128
* @param {http.IncomingMessage} clientRequest
129129
* @param {Number} retries
130130
*/
131-
call: function (params, clientRequest, requestType, retries) {
131+
call: function (params, clientRequest, serverType, retries) {
132132
retries = (typeof retries === 'number') ? Math.min(constants.MAX_RETRIES, Math.max(retries, 0)) : constants.MAX_RETRIES;
133133
var schemeObj = RdGlobalConfig.SCHEME === "http" ? http : https;
134-
return RequestLib._makeRequest(schemeObj, params, clientRequest, requestType, retries)
134+
return RequestLib._makeRequest(schemeObj, params, clientRequest, serverType, retries)
135135
.catch(function (err) {
136136
var errTopic = err.customTopic || constants.TOPICS.UNEXPECTED_ERROR;
137137
// Collect Network & Connectivity Logs whenever a request fails
@@ -147,7 +147,7 @@ var RequestLib = {
147147

148148
return Utils.delay(RdGlobalConfig.RETRY_DELAY)
149149
.then(function () {
150-
return RequestLib.call(params, clientRequest, requestType, retries - 1, false);
150+
return RequestLib.call(params, clientRequest, serverType, retries - 1, false);
151151
});
152152
} else {
153153
throw err;

src/reverseProxy.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,29 @@ var RdHandler = {
8484
*/
8585
requestHandler: function (clientRequest, clientResponse) {
8686
clientRequest.id = ++RdHandler._requestCounter + '::' + uuidv4();
87-
var url = "http://" + clientRequest.headers.host + clientRequest.url;
87+
var parsedClientUrl = url.parse(clientRequest.url);
88+
var requestUrl = "http://" + clientRequest.headers.host + parsedClientUrl.path;
8889
var request = {
8990
method: clientRequest.method,
9091
url: clientRequest.url,
9192
headers: clientRequest.headers,
9293
data: []
9394
};
94-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, request.method + ' ' + url,
95+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_REQUEST_START, request.method + ' ' + requestUrl,
9596
false, {
9697
headers: request.headers
9798
},
9899
clientRequest.id);
99-
100+
100101
var furtherRequestOptions = RdHandler._generateRequestOptions(clientRequest);
101102

102103
var paramsForRequest = {
103104
request: request,
104105
furtherRequestOptions: furtherRequestOptions
105106
};
106-
ReqLib.call(paramsForRequest, clientRequest, constants.REQUEST_TYPES.REVERSE_PROXY)
107+
ReqLib.call(paramsForRequest, clientRequest, constants.SERVER_TYPES.REVERSE_PROXY)
107108
.then(function (response) {
108-
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + url + ', Status Code: ' + response.statusCode,
109+
RdGlobalConfig.reqLogger.info(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + requestUrl + ', Status Code: ' + response.statusCode,
109110
false, {
110111
data: response.data,
111112
headers: response.headers,
@@ -116,14 +117,14 @@ var RdHandler = {
116117
clientResponse.end(response.data);
117118
})
118119
.catch(function (err) {
119-
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + clientRequest.url,
120+
RdGlobalConfig.reqLogger.error(err.customTopic || constants.TOPICS.UNEXPECTED_ERROR, clientRequest.method + ' ' + requestUrl,
120121
false, {
121122
errorMessage: err.message.toString()
122123
},
123124
clientRequest.id);
124125

125126
var errorResponse = RdHandler._frameErrorResponse(furtherRequestOptions, err.message.toString());
126-
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + url + ', Status Code: ' + errorResponse.statusCode,
127+
RdGlobalConfig.reqLogger.error(constants.TOPICS.CLIENT_RESPONSE_END, clientRequest.method + ' ' + requestUrl + ', Status Code: ' + errorResponse.statusCode,
127128
false,
128129
errorResponse.data,
129130
clientRequest.id);

src/stats/linuxStats.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ LinuxStats.mem = function (callback) {
4545
memStats.free = parseInt(Utils.fetchPropertyValue(memStatLines, 'memfree'));
4646
memStats.free = memStats.free ? memStats.free * 1024 : os.freemem();
4747
memStats.used = memStats.total - memStats.free;
48-
4948
memStats.swapTotal = parseInt(Utils.fetchPropertyValue(memStatLines, 'swaptotal'));
5049
memStats.swapTotal = memStats.swapTotal ? memStats.swapTotal * 1024 : 0;
5150
memStats.swapFree = parseInt(Utils.fetchPropertyValue(memStatLines, 'swapfree'));

src/stats/macStats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ MacStats.mem = function (callback) {
4949
case 'total':
5050
memStats.swapTotal = parseFloat(statLines[index].split('=')[1].trim()) * 1024 * 1024;
5151
break;
52-
52+
5353
case 'used':
5454
memStats.swapUsed = parseFloat(statLines[index].split('=')[1].trim()) * 1024 * 1024;
5555
break;
56-
56+
5757
case 'free':
5858
memStats.swapFree = parseFloat(statLines[index].split('=')[1].trim()) * 1024 * 1024;
5959
break;

test/commandLine.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('CommandLineManager', function () {
99

1010
var argv;
1111
var proxy_host_actual_value = "http://host";
12-
12+
1313
before(function () {
1414
console.log("NOTE: 'console.log' will be stubbed. In case any test fails, try removing the stub to see the logs");
1515
});
@@ -39,7 +39,7 @@ describe('CommandLineManager', function () {
3939
expect(RdGlobalConfig.proxy.port).to.eql(9687);
4040
});
4141

42-
it('parse proxy-host with without protocol', function () {
42+
it('parse proxy-host inputted without protocol', function () {
4343
sinon.stub(console, 'log');
4444
argv = argv.concat(['--proxy-host', 'host']);
4545
CommandLineManager.processArgs(argv);
@@ -303,7 +303,7 @@ describe('CommandLineManager', function () {
303303
console.log.restore();
304304
sinon.assert.called(process.exit);
305305
});
306-
306+
307307
it('exits with invalid args if the reverse port arg is provided without any value', function () {
308308
argv = argv.concat(['--reverse-proxy-port']);
309309
sinon.stub(console, 'log');
@@ -352,7 +352,7 @@ describe('CommandLineManager', function () {
352352
CommandLineManager.processArgs(argv);
353353
expect(RdGlobalConfig.SCHEME).to.eql('https');
354354
});
355-
355+
356356
// --request-timeout
357357
it("sets the timeout for the request being fired from the tool using the arg --request-timeout", function () {
358358
argv = argv.concat(['--request-timeout', '200000']);

test/connectivity.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ describe('Connectivity Checker for BrowserStack Components', function () {
163163
testHelper.nockGetRequestWithError(constants.HUB_STATUS_URL, 'https');
164164
testHelper.nockGetRequestWithError(constants.RAILS_AUTOMATE, 'http');
165165
testHelper.nockGetRequestWithError(constants.RAILS_AUTOMATE, 'https');
166-
167166
});
168167

169168
afterEach(function () {

0 commit comments

Comments
 (0)