Spaces:
Sleeping
Sleeping
Use logger instead of console.log
Browse files- src/index.ts +1 -1
- src/logger/logger.ts +26 -1
- src/mods/mods.ts +13 -12
src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { LauncherManager } from "./launcher/launchers";
|
|
| 7 |
async function main(): Promise<void> {
|
| 8 |
const logger: Logger = new Logger();
|
| 9 |
const server: Server = new Server(logger);
|
| 10 |
-
const modManager: ModManager = new ModManager(server);
|
| 11 |
const launcherManager: LauncherManager = new LauncherManager(server);
|
| 12 |
|
| 13 |
server.get("/", (_req, res) => {
|
|
|
|
| 7 |
async function main(): Promise<void> {
|
| 8 |
const logger: Logger = new Logger();
|
| 9 |
const server: Server = new Server(logger);
|
| 10 |
+
const modManager: ModManager = new ModManager(server, logger);
|
| 11 |
const launcherManager: LauncherManager = new LauncherManager(server);
|
| 12 |
|
| 13 |
server.get("/", (_req, res) => {
|
src/logger/logger.ts
CHANGED
|
@@ -9,6 +9,8 @@ enum Color {
|
|
| 9 |
}
|
| 10 |
|
| 11 |
export class Logger {
|
|
|
|
|
|
|
| 12 |
constructor(private readonly context?: string) {}
|
| 13 |
|
| 14 |
debug(message: string, ...meta: unknown[]): void {
|
|
@@ -27,7 +29,30 @@ export class Logger {
|
|
| 27 |
this.log("ERROR", Color.Red, message, meta);
|
| 28 |
}
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
const timestamp = new Date().toISOString();
|
| 32 |
const context = this.context ? ` [${this.context}]` : "";
|
| 33 |
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
export class Logger {
|
| 12 |
+
private timers = new Map<string, number>();
|
| 13 |
+
|
| 14 |
constructor(private readonly context?: string) {}
|
| 15 |
|
| 16 |
debug(message: string, ...meta: unknown[]): void {
|
|
|
|
| 29 |
this.log("ERROR", Color.Red, message, meta);
|
| 30 |
}
|
| 31 |
|
| 32 |
+
time(label: string = "default"): void {
|
| 33 |
+
this.timers.set(label, performance.now());
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
timeEnd(label: string = "default"): void {
|
| 37 |
+
const start = this.timers.get(label);
|
| 38 |
+
|
| 39 |
+
if (start === undefined) {
|
| 40 |
+
this.warn(`Timer '${label}' does not exist`);
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const duration = performance.now() - start;
|
| 45 |
+
this.timers.delete(label);
|
| 46 |
+
|
| 47 |
+
this.info(`${label}: ${duration.toFixed(3)}ms`);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
private log(
|
| 51 |
+
level: string,
|
| 52 |
+
color: Color,
|
| 53 |
+
message: string,
|
| 54 |
+
meta: unknown[]
|
| 55 |
+
): void {
|
| 56 |
const timestamp = new Date().toISOString();
|
| 57 |
const context = this.context ? ` [${this.context}]` : "";
|
| 58 |
|
src/mods/mods.ts
CHANGED
|
@@ -4,9 +4,11 @@ import { listFiles, downloadFile } from "@huggingface/hub";
|
|
| 4 |
import { sha256 } from "../utils/sha256";
|
| 5 |
import { Mod } from "./mod";
|
| 6 |
import { Server } from "../server/server";
|
|
|
|
| 7 |
|
| 8 |
export class ModManager {
|
| 9 |
private server: Server;
|
|
|
|
| 10 |
|
| 11 |
private bucketId = "buckets/NSMP/NSMP-Server-Storage";
|
| 12 |
private bucketFolder = "mods";
|
|
@@ -14,8 +16,9 @@ export class ModManager {
|
|
| 14 |
private modCache: Mod[] = [];
|
| 15 |
private refreshIntervalMs = 60_000; // 1 minute
|
| 16 |
|
| 17 |
-
constructor(server: Server) {
|
| 18 |
this.server = server;
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
public async createModEndpoints(): Promise<void> {
|
|
@@ -23,7 +26,7 @@ export class ModManager {
|
|
| 23 |
|
| 24 |
setInterval(() => {
|
| 25 |
this.refreshModCache().catch(err => {
|
| 26 |
-
|
| 27 |
});
|
| 28 |
}, this.refreshIntervalMs);
|
| 29 |
|
|
@@ -31,7 +34,7 @@ export class ModManager {
|
|
| 31 |
try {
|
| 32 |
res.send(JSON.stringify(this.modCache));
|
| 33 |
} catch (err) {
|
| 34 |
-
|
| 35 |
res.status(500).send("Failed to list mods");
|
| 36 |
}
|
| 37 |
});
|
|
@@ -49,7 +52,7 @@ export class ModManager {
|
|
| 49 |
return;
|
| 50 |
}
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
const blob = await downloadFile({
|
| 55 |
repo: this.bucketId,
|
|
@@ -57,7 +60,7 @@ export class ModManager {
|
|
| 57 |
accessToken: process.env.HF_TOKEN
|
| 58 |
});
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
if (!blob) {
|
| 63 |
res.status(404).send("Mod not found");
|
|
@@ -80,14 +83,14 @@ export class ModManager {
|
|
| 80 |
|
| 81 |
res.send(buffer);
|
| 82 |
} catch (err) {
|
| 83 |
-
|
| 84 |
res.status(500).send("Failed to fetch mod from bucket");
|
| 85 |
}
|
| 86 |
});
|
| 87 |
}
|
| 88 |
|
| 89 |
private async refreshModCache(): Promise<void> {
|
| 90 |
-
|
| 91 |
|
| 92 |
const existing = new Map(
|
| 93 |
this.modCache.map(mod => [mod.path, mod])
|
|
@@ -115,7 +118,7 @@ export class ModManager {
|
|
| 115 |
continue;
|
| 116 |
}
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
const jarBlob = await downloadFile({
|
| 121 |
repo: this.bucketId,
|
|
@@ -146,10 +149,8 @@ export class ModManager {
|
|
| 146 |
|
| 147 |
this.modCache = updated;
|
| 148 |
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
-
`Cached ${this.modCache.length} mods`
|
| 153 |
-
);
|
| 154 |
}
|
| 155 |
}
|
|
|
|
| 4 |
import { sha256 } from "../utils/sha256";
|
| 5 |
import { Mod } from "./mod";
|
| 6 |
import { Server } from "../server/server";
|
| 7 |
+
import { Logger } from "../logger/logger";
|
| 8 |
|
| 9 |
export class ModManager {
|
| 10 |
private server: Server;
|
| 11 |
+
private logger: Logger;
|
| 12 |
|
| 13 |
private bucketId = "buckets/NSMP/NSMP-Server-Storage";
|
| 14 |
private bucketFolder = "mods";
|
|
|
|
| 16 |
private modCache: Mod[] = [];
|
| 17 |
private refreshIntervalMs = 60_000; // 1 minute
|
| 18 |
|
| 19 |
+
constructor(server: Server, logger: Logger) {
|
| 20 |
this.server = server;
|
| 21 |
+
this.logger = logger;
|
| 22 |
}
|
| 23 |
|
| 24 |
public async createModEndpoints(): Promise<void> {
|
|
|
|
| 26 |
|
| 27 |
setInterval(() => {
|
| 28 |
this.refreshModCache().catch(err => {
|
| 29 |
+
this.logger.error("Failed to refresh mod cache:", err);
|
| 30 |
});
|
| 31 |
}, this.refreshIntervalMs);
|
| 32 |
|
|
|
|
| 34 |
try {
|
| 35 |
res.send(JSON.stringify(this.modCache));
|
| 36 |
} catch (err) {
|
| 37 |
+
this.logger.error("Failed to list mods:", err);
|
| 38 |
res.status(500).send("Failed to list mods");
|
| 39 |
}
|
| 40 |
});
|
|
|
|
| 52 |
return;
|
| 53 |
}
|
| 54 |
|
| 55 |
+
this.logger.time(`download:${filename}`);
|
| 56 |
|
| 57 |
const blob = await downloadFile({
|
| 58 |
repo: this.bucketId,
|
|
|
|
| 60 |
accessToken: process.env.HF_TOKEN
|
| 61 |
});
|
| 62 |
|
| 63 |
+
this.logger.timeEnd(`download:${filename}`);
|
| 64 |
|
| 65 |
if (!blob) {
|
| 66 |
res.status(404).send("Mod not found");
|
|
|
|
| 83 |
|
| 84 |
res.send(buffer);
|
| 85 |
} catch (err) {
|
| 86 |
+
this.logger.error(err as string);
|
| 87 |
res.status(500).send("Failed to fetch mod from bucket");
|
| 88 |
}
|
| 89 |
});
|
| 90 |
}
|
| 91 |
|
| 92 |
private async refreshModCache(): Promise<void> {
|
| 93 |
+
this.logger.time("refreshModCache");
|
| 94 |
|
| 95 |
const existing = new Map(
|
| 96 |
this.modCache.map(mod => [mod.path, mod])
|
|
|
|
| 118 |
continue;
|
| 119 |
}
|
| 120 |
|
| 121 |
+
this.logger.info(`New mod detected: ${item.path}`);
|
| 122 |
|
| 123 |
const jarBlob = await downloadFile({
|
| 124 |
repo: this.bucketId,
|
|
|
|
| 149 |
|
| 150 |
this.modCache = updated;
|
| 151 |
|
| 152 |
+
this.logger.timeEnd("refreshModCache");
|
| 153 |
|
| 154 |
+
this.logger.info(`Cached ${this.modCache.length} mods`);
|
|
|
|
|
|
|
| 155 |
}
|
| 156 |
}
|