2022-02-07 15:41:27 +01:00
|
|
|
const logger = require('./logger.js');
|
2022-02-20 22:04:32 +01:00
|
|
|
const realpath = require('fs/promises').realpath;
|
|
|
|
const stat = require('fs/promises').stat;
|
2022-02-07 15:41:27 +01:00
|
|
|
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) {
|
2022-03-14 22:37:42 +01:00
|
|
|
let clone = {};
|
2022-02-07 15:41:27 +01:00
|
|
|
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) {
|
2022-03-24 13:00:19 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-03-25 13:52:41 +01:00
|
|
|
if (!path.startsWith('/')) {
|
|
|
|
path = '/' + path;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-03-04 02:38:52 +01:00
|
|
|
const options = {
|
2022-02-07 15:41:27 +01:00
|
|
|
hostname: host,
|
|
|
|
port: port,
|
|
|
|
path: path,
|
2022-03-24 13:00:19 +01:00
|
|
|
method: method,
|
|
|
|
timeout: global.config?.requests?.timeout || 5000
|
2022-03-04 02:38:52 +01:00
|
|
|
};
|
2022-03-08 00:51:58 +01:00
|
|
|
let requestName = 'http \'' + method + '\' request > \'' + host + ':' + port + path;
|
2022-03-04 02:38:52 +01:00
|
|
|
if (args !== undefined) {
|
|
|
|
args = new URLSearchParams(args).toString();
|
|
|
|
options.headers = {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
requestName += '?' + args;
|
2022-03-04 02:38:52 +01:00
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
requestName += '\'';
|
|
|
|
logger.debug('sending ' + requestName + '...');
|
2022-03-24 13:00:19 +01:00
|
|
|
const request = http.request(options, (response) => {
|
2022-02-07 15:41:27 +01:00
|
|
|
if (!response) {
|
2022-03-08 00:51:58 +01:00
|
|
|
return reject('no response from host for ' + requestName);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
let responseData = '';
|
2022-03-24 13:00:19 +01:00
|
|
|
response.on('data', (data) => {
|
2022-02-07 15:41:27 +01:00
|
|
|
responseData += data;
|
|
|
|
});
|
2022-03-24 13:00:19 +01:00
|
|
|
response.on('end', () => {
|
2022-03-08 00:51:58 +01:00
|
|
|
logger.debug(requestName + ' returned status code \'' + response.statusCode + '\' and data \'' + responseData + '\'');
|
2022-03-14 22:37:42 +01:00
|
|
|
let fn = resolve;
|
2022-02-08 11:11:49 +01:00
|
|
|
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-03-04 02:38:52 +01:00
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
});
|
|
|
|
});
|
2022-03-24 13:00:19 +01:00
|
|
|
request.on('timeout', () => {
|
|
|
|
request.destroy();
|
|
|
|
return reject(requestName + ' timed out after ' + options.timeout + 'ms');
|
|
|
|
});
|
|
|
|
request.on('error', (err) => {
|
2022-03-08 00:51:58 +01:00
|
|
|
return reject(requestName + ' returned an error >>> ' + err.message);
|
2022-02-07 15:41:27 +01:00
|
|
|
});
|
2022-03-04 02:38:52 +01:00
|
|
|
if (args !== undefined) {
|
|
|
|
request.write(args);
|
|
|
|
}
|
2022-02-07 15:41:27 +01:00
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toHex(value) {
|
2022-03-25 13:52:41 +01:00
|
|
|
return Number(value).toString(16);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-14 22:37:42 +01:00
|
|
|
function hexToRGB(hex) {
|
|
|
|
let validHEXInput = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
|
|
if (!validHEXInput) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
return [
|
2022-03-14 22:37:42 +01:00
|
|
|
{
|
|
|
|
red: parseInt(validHEXInput[1], 16),
|
|
|
|
green: parseInt(validHEXInput[2], 16),
|
|
|
|
blue: parseInt(validHEXInput[3], 16),
|
2022-03-25 13:52:41 +01:00
|
|
|
}
|
2022-03-14 22:37:42 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
function sortById(array) {
|
2022-03-24 13:00:19 +01:00
|
|
|
return array.sort((a, b) => {
|
2022-02-07 15:41:27 +01:00
|
|
|
return a.id - b.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function fileExists(file) {
|
|
|
|
const path = await resolvePath(file);
|
|
|
|
const stats = await stat(path);
|
|
|
|
return {path, stats};
|
2022-02-20 22:04:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function resolvePath(file) {
|
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error('can not resolve a path to an undefined file');
|
|
|
|
}
|
|
|
|
return await realpath(file);
|
2022-02-20 22:04:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
module.exports = {
|
|
|
|
timeDiff,
|
|
|
|
clone,
|
|
|
|
httpGET,
|
|
|
|
httpPOST,
|
|
|
|
toHex,
|
2022-03-14 22:37:42 +01:00
|
|
|
hexToRGB,
|
2022-02-20 22:04:32 +01:00
|
|
|
sortById,
|
|
|
|
fileExists,
|
2022-03-08 00:51:58 +01:00
|
|
|
resolvePath
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|