From 9a9b8f1ee517814385544bdf3999a36d9bb79a38 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sun, 7 Jul 2024 18:20:37 +0200 Subject: [PATCH] add version number to audit --- index.ts | 18 ++++++++++++------ utility.ts | 18 +++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index bdfe779..cabceb5 100755 --- a/index.ts +++ b/index.ts @@ -7,7 +7,7 @@ import { processMessage, trackConnection, } from "./websocket-handler"; -import { writeStatNumber, writeStatString, writeSuccess } from "./utility"; +import { getVersionNumber, writeStatNumber, writeStatString, writeSuccess } from "./utility"; import Colors from "colors"; import IP from "ip"; @@ -16,8 +16,10 @@ import { ServerWebSocket } from "bun"; import { getConfig } from "./config-handler"; // CONFIG -const STATIC_DIR = Path.join(Path.dirname(import.meta.path), "static/dist"); -const DEFAULT_PAGE = "index.html"; +export const CODE_DIR = Path.dirname(import.meta.path); +export const STATIC_DIR = Path.join(CODE_DIR, "static/dist"); +export const PACKAGE_FILE = Path.join(CODE_DIR, "package.json"); +export const DEFAULT_PAGE = "index.html"; // TYPES export type WS = ServerWebSocket | WebSocket; @@ -26,6 +28,7 @@ export type WS = ServerWebSocket | WebSocket; async function main() { // CONFIG const config = await getConfig(); + const versionNumber = await getVersionNumber(); // SERVER const server = Bun.serve({ @@ -73,6 +76,7 @@ async function main() { return [ ["server url", server.url.toString()], ["server ip address", IP.address()], + ["server version", versionNumber] ]; } @@ -85,9 +89,11 @@ async function main() { function updateCLI() { console.clear(); - + console.log(Colors.bold.bgWhite("UNIVERSAL DECENTRALIZED NETWORK")); - console.log(Colors.yellow("relevant output is available in the 'logs' file")); + console.log( + Colors.yellow("relevant output is available in the 'logs' file") + ); console.log(); console.log(new Date().toLocaleString()); @@ -95,7 +101,7 @@ async function main() { getServerStats().forEach((entry) => writeStatString(...entry)); console.log(); - + getWebSocketStats().forEach((entry) => writeStatNumber(...entry)); console.log(); } diff --git a/utility.ts b/utility.ts index cbdbde7..f112d9b 100644 --- a/utility.ts +++ b/utility.ts @@ -1,19 +1,27 @@ import Colors from "colors"; import Fs from "fs/promises"; +import { PACKAGE_FILE } from "."; import { WebSocketMessage } from "./websocket-handler"; // CONFIG const STAT_INDENT = 25; -// SAFETY -export function doIfIsString( - data: any, - cb: (data: string) => void -): void { +// MISC +export function doIfIsString(data: any, cb: (data: string) => void): void { if (typeof data != "string" || data == "") return; cb(data); } +export async function getVersionNumber(): Promise { + try { + const packageString = await Bun.file(PACKAGE_FILE).text(); + const packageObject = JSON.parse(packageString); + return packageObject.version; + } catch { + return "unknown"; + } +} + // OUTPUT async function writeToLogFile(message: string): Promise { await Fs.appendFile("logs", `${message}\n`);