move to biomejs

This commit is contained in:
creations 2025-04-07 04:36:07 -04:00
parent 7d78a74a25
commit d91e832eab
Signed by: creations
GPG key ID: 8F553AA4320FC711
19 changed files with 99 additions and 259 deletions

View file

@ -1,6 +1,6 @@
export function timestampToReadable(timestamp?: number): string {
const date: Date =
timestamp && !isNaN(timestamp) ? new Date(timestamp) : new Date();
if (isNaN(date.getTime())) return "Invalid Date";
timestamp && !Number.isNaN(timestamp) ? new Date(timestamp) : new Date();
if (Number.isNaN(date.getTime())) return "Invalid Date";
return date.toISOString().replace("T", " ").replace("Z", "");
}

View file

@ -1,5 +1,5 @@
import { resolve } from "node:path";
import { renderFile } from "ejs";
import { resolve } from "path";
export async function renderEjsTemplate(
viewName: string | string[],

View file

@ -76,7 +76,7 @@ export async function handleReadMe(data: LanyardData): Promise<string | null> {
return null;
if (res.headers.has("content-length")) {
const size: number = parseInt(
const size: number = Number.parseInt(
res.headers.get("content-length") || "0",
10,
);

View file

@ -1,15 +1,15 @@
import { environment } from "@config/environment";
import { timestampToReadable } from "@helpers/char";
import type { Stats } from "fs";
import type { Stats } from "node:fs";
import {
type WriteStream,
createWriteStream,
existsSync,
mkdirSync,
statSync,
WriteStream,
} from "fs";
import { EOL } from "os";
import { basename, join } from "path";
} from "node:fs";
import { EOL } from "node:os";
import { basename, join } from "node:path";
import { environment } from "@config/environment";
import { timestampToReadable } from "@helpers/char";
class Logger {
private static instance: Logger;
@ -37,7 +37,7 @@ class Logger {
mkdirSync(logDir, { recursive: true });
}
let addSeparator: boolean = false;
let addSeparator = false;
if (existsSync(logFile)) {
const fileStats: Stats = statSync(logFile);
@ -66,9 +66,9 @@ class Logger {
private extractFileName(stack: string): string {
const stackLines: string[] = stack.split("\n");
let callerFile: string = "";
let callerFile = "";
for (let i: number = 2; i < stackLines.length; i++) {
for (let i = 2; i < stackLines.length; i++) {
const line: string = stackLines[i].trim();
if (line && !line.includes("Logger.") && line.includes("(")) {
callerFile = line.split("(")[1]?.split(")")[0] || "";
@ -91,7 +91,7 @@ class Logger {
return { filename, timestamp: readableTimestamp };
}
public info(message: string | string[], breakLine: boolean = false): void {
public info(message: string | string[], breakLine = false): void {
const stack: string = new Error().stack || "";
const { filename, timestamp } = this.getCallerInfo(stack);
@ -110,7 +110,7 @@ class Logger {
this.writeConsoleMessageColored(logMessageParts, breakLine);
}
public warn(message: string | string[], breakLine: boolean = false): void {
public warn(message: string | string[], breakLine = false): void {
const stack: string = new Error().stack || "";
const { filename, timestamp } = this.getCallerInfo(stack);
@ -131,7 +131,7 @@ class Logger {
public error(
message: string | Error | (string | Error)[],
breakLine: boolean = false,
breakLine = false,
): void {
const stack: string = new Error().stack || "";
const { filename, timestamp } = this.getCallerInfo(stack);
@ -161,7 +161,7 @@ class Logger {
bracketMessage2: string,
message: string | string[],
color: string,
breakLine: boolean = false,
breakLine = false,
): void {
const stack: string = new Error().stack || "";
const { timestamp } = this.getCallerInfo(stack);
@ -189,7 +189,7 @@ class Logger {
private writeConsoleMessageColored(
logMessageParts: ILogMessageParts,
breakLine: boolean = false,
breakLine = false,
): void {
const logMessage: string = Object.keys(logMessageParts)
.map((key: string) => {

View file

@ -3,11 +3,7 @@ import { logger } from "@helpers/logger";
import { serverHandler } from "@/server";
async function main(): Promise<void> {
try {
serverHandler.initialize();
} catch (error) {
throw error;
}
serverHandler.initialize();
}
main().catch((error: Error) => {

View file

@ -29,8 +29,7 @@ async function handler(request: ExtendedRequest): Promise<Response> {
}
const presence: LanyardData = data.data;
const readme: string | Promise<string> | null =
await handleReadMe(presence);
const readme: string | Promise<string> | null = await handleReadMe(presence);
const ejsTemplateData: EjsTemplateData = {
title: `${presence.discord_user.username || "Unknown"}`,

View file

@ -24,10 +24,7 @@ async function handler(request: ExtendedRequest): Promise<Response> {
try {
res = await fetch(url);
} catch {
return Response.json(
{ error: "Failed to fetch image" },
{ status: 500 },
);
return Response.json({ error: "Failed to fetch image" }, { status: 500 });
}
if (!res.ok) {

View file

@ -28,12 +28,10 @@ async function handler(): Promise<Response> {
}
const presence: LanyardData = data.data;
const readme: string | Promise<string> | null =
await handleReadMe(presence);
const readme: string | Promise<string> | null = await handleReadMe(presence);
const ejsTemplateData: EjsTemplateData = {
title:
presence.discord_user.global_name || presence.discord_user.username,
title: presence.discord_user.global_name || presence.discord_user.username,
username:
presence.discord_user.global_name || presence.discord_user.username,
status: presence.discord_status,

View file

@ -1,3 +1,4 @@
import { resolve } from "node:path";
import { environment } from "@config/environment";
import { logger } from "@helpers/logger";
import {
@ -6,7 +7,6 @@ import {
type MatchedRoute,
type Serve,
} from "bun";
import { resolve } from "path";
import { webSocketHandler } from "@/websocket";
@ -77,21 +77,16 @@ class ServerHandler {
if (await file.exists()) {
const fileContent: ArrayBuffer = await file.arrayBuffer();
const contentType: string =
file.type || "application/octet-stream";
const contentType: string = file.type || "application/octet-stream";
return new Response(fileContent, {
headers: { "Content-Type": contentType },
});
} else {
logger.warn(`File not found: ${filePath}`);
return new Response("Not Found", { status: 404 });
}
logger.warn(`File not found: ${filePath}`);
return new Response("Not Found", { status: 404 });
} catch (error) {
logger.error([
`Error serving static file: ${pathname}`,
error as Error,
]);
logger.error([`Error serving static file: ${pathname}`, error as Error]);
return new Response("Internal Server Error", { status: 500 });
}
}
@ -117,8 +112,7 @@ class ServerHandler {
try {
const routeModule: RouteModule = await import(filePath);
const contentType: string | null =
request.headers.get("Content-Type");
const contentType: string | null = request.headers.get("Content-Type");
const actualContentType: string | null = contentType
? contentType.split(";")[0].trim()
: null;
@ -145,9 +139,7 @@ class ServerHandler {
if (
(Array.isArray(routeModule.routeDef.method) &&
!routeModule.routeDef.method.includes(
request.method,
)) ||
!routeModule.routeDef.method.includes(request.method)) ||
(!Array.isArray(routeModule.routeDef.method) &&
routeModule.routeDef.method !== request.method)
) {
@ -172,9 +164,7 @@ class ServerHandler {
if (Array.isArray(expectedContentType)) {
matchesAccepts =
expectedContentType.includes("*/*") ||
expectedContentType.includes(
actualContentType || "",
);
expectedContentType.includes(actualContentType || "");
} else {
matchesAccepts =
expectedContentType === "*/*" ||
@ -213,10 +203,7 @@ class ServerHandler {
}
}
} catch (error: unknown) {
logger.error([
`Error handling route ${request.url}:`,
error as Error,
]);
logger.error([`Error handling route ${request.url}:`, error as Error]);
response = Response.json(
{

View file

@ -1,5 +1,5 @@
import { logger } from "@helpers/logger";
import { type ServerWebSocket } from "bun";
import type { ServerWebSocket } from "bun";
class WebSocketHandler {
public handleMessage(ws: ServerWebSocket, message: string): void {
@ -20,11 +20,7 @@ class WebSocketHandler {
}
}
public handleClose(
ws: ServerWebSocket,
code: number,
reason: string,
): void {
public handleClose(ws: ServerWebSocket, code: number, reason: string): void {
logger.warn(`WebSocket closed with code ${code}, reason: ${reason}`);
}
}