|
|
@ -19,7 +19,7 @@ export const STATUS_PAGE_ALL_DOWN = 0;
|
|
|
|
export const STATUS_PAGE_ALL_UP = 1;
|
|
|
|
export const STATUS_PAGE_ALL_UP = 1;
|
|
|
|
export const STATUS_PAGE_PARTIAL_DOWN = 2;
|
|
|
|
export const STATUS_PAGE_PARTIAL_DOWN = 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Flip the status of s */
|
|
|
|
export function flipStatus(s: number) {
|
|
|
|
export function flipStatus(s: number) {
|
|
|
|
if (s === UP) {
|
|
|
|
if (s === UP) {
|
|
|
|
return DOWN;
|
|
|
|
return DOWN;
|
|
|
@ -32,6 +32,10 @@ export function flipStatus(s: number) {
|
|
|
|
return s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Delays for specified number of seconds
|
|
|
|
|
|
|
|
* @param ms Number of milliseconds to sleep for
|
|
|
|
|
|
|
|
*/
|
|
|
|
export function sleep(ms: number) {
|
|
|
|
export function sleep(ms: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -94,6 +98,12 @@ class Logger {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Write a message to the log
|
|
|
|
|
|
|
|
* @param module The module the log comes from
|
|
|
|
|
|
|
|
* @param msg Message to write
|
|
|
|
|
|
|
|
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
|
|
|
|
|
|
|
|
*/
|
|
|
|
log(module: string, msg: any, level: string) {
|
|
|
|
log(module: string, msg: any, level: string) {
|
|
|
|
if (this.hideLog[level] && this.hideLog[level].includes(module)) {
|
|
|
|
if (this.hideLog[level] && this.hideLog[level].includes(module)) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -120,22 +130,48 @@ class Logger {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Log an INFO message
|
|
|
|
|
|
|
|
* @param module Module log comes from
|
|
|
|
|
|
|
|
* @param msg Message to write
|
|
|
|
|
|
|
|
*/
|
|
|
|
info(module: string, msg: any) {
|
|
|
|
info(module: string, msg: any) {
|
|
|
|
this.log(module, msg, "info");
|
|
|
|
this.log(module, msg, "info");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Log a WARN message
|
|
|
|
|
|
|
|
* @param module Module log comes from
|
|
|
|
|
|
|
|
* @param msg Message to write
|
|
|
|
|
|
|
|
*/
|
|
|
|
warn(module: string, msg: any) {
|
|
|
|
warn(module: string, msg: any) {
|
|
|
|
this.log(module, msg, "warn");
|
|
|
|
this.log(module, msg, "warn");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Log an ERROR message
|
|
|
|
|
|
|
|
* @param module Module log comes from
|
|
|
|
|
|
|
|
* @param msg Message to write
|
|
|
|
|
|
|
|
*/
|
|
|
|
error(module: string, msg: any) {
|
|
|
|
error(module: string, msg: any) {
|
|
|
|
this.log(module, msg, "error");
|
|
|
|
this.log(module, msg, "error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Log a DEBUG message
|
|
|
|
|
|
|
|
* @param module Module log comes from
|
|
|
|
|
|
|
|
* @param msg Message to write
|
|
|
|
|
|
|
|
*/
|
|
|
|
debug(module: string, msg: any) {
|
|
|
|
debug(module: string, msg: any) {
|
|
|
|
this.log(module, msg, "debug");
|
|
|
|
this.log(module, msg, "debug");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Log an exeption as an ERROR
|
|
|
|
|
|
|
|
* @param module Module log comes from
|
|
|
|
|
|
|
|
* @param exception The exeption to include
|
|
|
|
|
|
|
|
* @param msg The message to write
|
|
|
|
|
|
|
|
*/
|
|
|
|
exception(module: string, exception: any, msg: any) {
|
|
|
|
exception(module: string, exception: any, msg: any) {
|
|
|
|
let finalMessage = exception
|
|
|
|
let finalMessage = exception
|
|
|
|
|
|
|
|
|
|
|
@ -151,13 +187,13 @@ export const log = new Logger();
|
|
|
|
|
|
|
|
|
|
|
|
declare global { interface String { replaceAll(str: string, newStr: string): string; } }
|
|
|
|
declare global { interface String { replaceAll(str: string, newStr: string): string; } }
|
|
|
|
|
|
|
|
|
|
|
|
export function polyfill() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* String.prototype.replaceAll() polyfill
|
|
|
|
* String.prototype.replaceAll() polyfill
|
|
|
|
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
|
|
|
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
|
|
|
* @author Chris Ferdinandi
|
|
|
|
* @author Chris Ferdinandi
|
|
|
|
* @license MIT
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function polyfill() {
|
|
|
|
if (!String.prototype.replaceAll) {
|
|
|
|
if (!String.prototype.replaceAll) {
|
|
|
|
String.prototype.replaceAll = function (str: string, newStr: string) {
|
|
|
|
String.prototype.replaceAll = function (str: string, newStr: string) {
|
|
|
|
// If a regex pattern
|
|
|
|
// If a regex pattern
|
|
|
@ -177,7 +213,10 @@ export class TimeLogger {
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
|
this.startTime = dayjs().valueOf();
|
|
|
|
this.startTime = dayjs().valueOf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Output time since start of monitor
|
|
|
|
|
|
|
|
* @param name Name of monitor
|
|
|
|
|
|
|
|
*/
|
|
|
|
print(name: string) {
|
|
|
|
print(name: string) {
|
|
|
|
if (isDev && process.env.TIMELOGGER === "1") {
|
|
|
|
if (isDev && process.env.TIMELOGGER === "1") {
|
|
|
|
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms")
|
|
|
|
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms")
|
|
|
@ -231,6 +270,13 @@ let getRandomBytes = (
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)();
|
|
|
|
)();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Get a random integer suitable for use in cryptography between upper
|
|
|
|
|
|
|
|
* and lower bounds.
|
|
|
|
|
|
|
|
* @param min Minimum value of integer
|
|
|
|
|
|
|
|
* @param max Maximum value of integer
|
|
|
|
|
|
|
|
* @returns Cryptographically suitable random integer
|
|
|
|
|
|
|
|
*/
|
|
|
|
export function getCryptoRandomInt(min: number, max: number):number {
|
|
|
|
export function getCryptoRandomInt(min: number, max: number):number {
|
|
|
|
|
|
|
|
|
|
|
|
// synchronous version of: https://github.com/joepie91/node-random-number-csprng
|
|
|
|
// synchronous version of: https://github.com/joepie91/node-random-number-csprng
|
|
|
@ -267,6 +313,11 @@ export function getCryptoRandomInt(min: number, max: number):number {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Generate a secret
|
|
|
|
|
|
|
|
* @param length Lenght of secret to generate
|
|
|
|
|
|
|
|
* @returns
|
|
|
|
|
|
|
|
*/
|
|
|
|
export function genSecret(length = 64) {
|
|
|
|
export function genSecret(length = 64) {
|
|
|
|
let secret = "";
|
|
|
|
let secret = "";
|
|
|
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
@ -277,6 +328,11 @@ export function genSecret(length = 64) {
|
|
|
|
return secret;
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Get the path of a monitor
|
|
|
|
|
|
|
|
* @param id ID of monitor
|
|
|
|
|
|
|
|
* @returns Formatted relative path
|
|
|
|
|
|
|
|
*/
|
|
|
|
export function getMonitorRelativeURL(id: string) {
|
|
|
|
export function getMonitorRelativeURL(id: string) {
|
|
|
|
return "/dashboard/" + id;
|
|
|
|
return "/dashboard/" + id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|