pbc/libs/util.js

153 lines
4.1 KiB
JavaScript
Raw Normal View History

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) {
return new Promise(function (resolve, reject) {
if (!path.startsWith("/")) {
path = "/" + path;
}
2022-03-04 02:38:52 +01:00
const options = {
2022-02-07 15:41:27 +01:00
hostname: host,
port: port,
path: path,
method: method
2022-03-04 02:38:52 +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'
}
requestName += '?' + args;
2022-03-04 02:38:52 +01:00
}
requestName += '\'';
logger.debug('sending ' + requestName + '...');
2022-03-04 02:38:52 +01:00
const request = http.request(options, function (response) {
2022-02-07 15:41:27 +01:00
if (!response) {
return reject('no response from host for ' + requestName);
2022-02-07 15:41:27 +01:00
}
2022-03-14 22:37:42 +01:00
let responseData = "";
2022-02-07 15:41:27 +01:00
response.on('data', function (data) {
responseData += data;
});
response.on('end', function () {
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
});
});
request.on('error', function (err) {
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-14 22:37:42 +01:00
let hex = Number(value).toString(16);
2022-02-07 15:41:27 +01:00
return hex;
}
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;
}
let output = [
{
red: parseInt(validHEXInput[1], 16),
green: parseInt(validHEXInput[2], 16),
blue: parseInt(validHEXInput[3], 16),
},
];
return output;
}
2022-02-07 15:41:27 +01:00
function sortById(array) {
return array.sort(function (a, b) {
return a.id - b.id;
});
}
2022-02-20 22:04:32 +01:00
function fileExists(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not check the existence of an undefined file');
}
resolvePath(file)
.then((path) => {
stat(path)
.then((stats) => {
2022-03-04 02:38:52 +01:00
resolve({ path, stats });
2022-02-20 22:04:32 +01:00
})
})
.catch(reject);
});
}
function resolvePath(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not resolve a path to an undefined file');
}
realpath(file)
.then(resolve)
.catch((err) => {
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
});
});
}
2022-03-04 02:38:52 +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,
resolvePath
2022-02-07 15:41:27 +01:00
}