2022-02-07 15:41:27 +01:00
|
|
|
const logger = require('./logger.js');
|
|
|
|
const http = require('http');
|
2022-02-07 23:50:16 +01:00
|
|
|
const { HTTP_GET, HTTP_POST } = require('./constants.js');
|
2022-02-07 15:41:27 +01:00
|
|
|
|
|
|
|
function timeDiff(startTime) {
|
|
|
|
if (startTime instanceof Date) {
|
|
|
|
return (new Date().getTime() - startTime.getTime());
|
|
|
|
}
|
|
|
|
return new Date().getTime - startTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clone(object) {
|
|
|
|
var clone = {};
|
|
|
|
for (key in object) {
|
|
|
|
clone[key] = object[key];
|
|
|
|
}
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
function httpGET(host, port, path, args) {
|
|
|
|
return httpRequest(host, port, path, HTTP_GET, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function httpPOST(host, port, path, args) {
|
|
|
|
return httpRequest(host, port, path, HTTP_POST, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function httpRequest(host, port, path, method, args) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (!path.startsWith("/")) {
|
|
|
|
path = "/" + path;
|
|
|
|
}
|
|
|
|
logger.debug('sending http \'' + method + '\' request to \'' + host + ':' + port + path + '\'...');
|
|
|
|
const request = http.request({
|
|
|
|
hostname: host,
|
|
|
|
port: port,
|
|
|
|
path: path,
|
|
|
|
method: method
|
|
|
|
}, function (response) {
|
|
|
|
if (!response) {
|
|
|
|
return reject('error: no response from host for http \'' + method + '\' request \'' + host + ':' + port + path + '\'');
|
|
|
|
}
|
|
|
|
var responseData = "";
|
|
|
|
response.on('data', function (data) {
|
|
|
|
responseData += data;
|
|
|
|
});
|
|
|
|
response.on('end', function () {
|
2022-02-08 11:11:49 +01:00
|
|
|
logger.debug('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned status code \'' + response.statusCode + '\' and data \'' + responseData + '\'');
|
|
|
|
var fn = resolve;
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
fn = reject;
|
|
|
|
}
|
2022-02-07 16:56:18 +01:00
|
|
|
if (!responseData) {
|
2022-02-08 11:11:49 +01:00
|
|
|
return fn();
|
2022-02-07 16:56:18 +01:00
|
|
|
}
|
2022-02-07 23:50:16 +01:00
|
|
|
try {
|
2022-02-08 11:11:49 +01:00
|
|
|
return fn(JSON.parse(responseData));
|
2022-02-07 23:50:16 +01:00
|
|
|
} catch (err) {
|
2022-02-08 11:11:49 +01:00
|
|
|
return fn(responseData);
|
2022-02-07 23:50:16 +01:00
|
|
|
}
|
2022-02-08 11:11:49 +01:00
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
request.on('error', function (err) {
|
|
|
|
return reject('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned an error >>> ' + err);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toHex(value) {
|
|
|
|
var hex = Number(value).toString(16);
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortById(array) {
|
|
|
|
return array.sort(function (a, b) {
|
|
|
|
return a.id - b.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
timeDiff,
|
|
|
|
clone,
|
|
|
|
httpGET,
|
|
|
|
httpPOST,
|
|
|
|
toHex,
|
|
|
|
sortById
|
|
|
|
}
|