Syntactic improvments, and linting ping.js

This commit is contained in:
Alicia Sykes 2021-06-19 13:46:02 +01:00
parent 77ca662f37
commit 106103a7df

View File

@ -1,66 +1,66 @@
/** /**
* This file contains the Node.js code, used for the optional status check feature * This file contains the Node.js code, used for the optional status check feature
* It accepts a single url parameter, and will make an empty GET request to that * It accepts a single url parameter, and will make an empty GET request to that
* endpoint, and then resolve the response status code, time taken, and short message * endpoint, and then resolve the response status code, time taken, and short message
*/ */
const axios = require('axios').default; const axios = require('axios').default;
/* Determines if successful from the HTTP response code */ /* Determines if successful from the HTTP response code */
const getResponseType = (code) => { const getResponseType = (code) => {
if (Number.isNaN(code)) return false; if (Number.isNaN(code)) return false;
const numericCode = parseInt(code, 10); const numericCode = parseInt(code, 10);
return (numericCode >= 200 && numericCode <= 302); return (numericCode >= 200 && numericCode <= 302);
}; };
/* Makes human-readable response text for successful check */ /* Makes human-readable response text for successful check */
const makeMessageText = (data) => `${data.successStatus ? '✅' : '⚠️'} ` const makeMessageText = (data) => `${data.successStatus ? '✅' : '⚠️'} `
+ `${data.serverName || 'Server'} responded with ` + `${data.serverName || 'Server'} responded with `
+ `${data.statusCode} - ${data.statusText}. ` + `${data.statusCode} - ${data.statusText}. `
+ `\nTook ${data.timeTaken} ms`; + `\nTook ${data.timeTaken} ms`;
/* Makes human-readable response text for failed check */ /* Makes human-readable response text for failed check */
const makeErrorMessage = (data) => `❌ Service Unavailable: ${data.hostname || 'Server'} ` const makeErrorMessage = (data) => `❌ Service Unavailable: ${data.hostname || 'Server'} `
+ `resulted in ${data.code || 'a fatal error'} ${data.errno ? `(${data.errno})` : ''}`; + `resulted in ${data.code || 'a fatal error'} ${data.errno ? `(${data.errno})` : ''}`;
const makeErrorMessage2 = (data) => `❌ Service Error - ` const makeErrorMessage2 = (data) => '❌ Service Error - '
+ `${data.status} - ${data.statusText}`; + `${data.status} - ${data.statusText}`;
/* Kicks of a HTTP request, then formats and renders results */ /* Kicks of a HTTP request, then formats and renders results */
const makeRequest = (url, render) => { const makeRequest = (url, render) => {
const startTime = new Date(); const startTime = new Date();
axios.get(url) axios.get(url)
.then((response) => { .then((response) => {
const statusCode = response.status; const statusCode = response.status;
const { statusText } = response; const { statusText } = response;
const successStatus = getResponseType(statusCode); const successStatus = getResponseType(statusCode);
const serverName = response.request.socket.servername; const serverName = response.request.socket.servername;
const timeTaken = (new Date() - startTime); const timeTaken = (new Date() - startTime);
const results = { const results = {
statusCode, statusText, serverName, successStatus, timeTaken, statusCode, statusText, serverName, successStatus, timeTaken,
}; };
const messageText = makeMessageText(results); const messageText = makeMessageText(results);
results.message = messageText; results.message = messageText;
return results; return results;
}) })
.catch((error) => { .catch((error) => {
render(JSON.stringify({ render(JSON.stringify({
successStatus: false, successStatus: false,
message: error.response ? makeErrorMessage2(error.response) : makeErrorMessage(error), message: error.response ? makeErrorMessage2(error.response) : makeErrorMessage(error),
})); }));
}).then((results) => { }).then((results) => {
render(JSON.stringify(results)); render(JSON.stringify(results));
}); });
}; };
/* Main function, will check if a URL present, and call function */ /* Main function, will check if a URL present, and call function */
module.exports = (params, render) => { module.exports = (params, render) => {
if (!params || !params.includes('=')) { if (!params || !params.includes('=')) {
render(JSON.stringify({ render(JSON.stringify({
success: false, success: false,
message: '❌ Malformed URL', message: '❌ Malformed URL',
})); }));
} else { } else {
const url = params.split('=')[1]; const url = params.split('=')[1];
makeRequest(url, render); makeRequest(url, render);
} }
}; };