From 0b0fd8412cfa56cb14e6fb2825ceb2cf9b2dabd8 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Sat, 8 Nov 2025 19:17:55 -0700 Subject: [PATCH] Fix db not closing --- src/lib/util.ts | 4 ++-- src/routes/launcher/latest.ts | 4 +++- src/routes/launcher/loader/update-data.ts | 8 +++++++- src/routes/launcher/versions.ts | 8 +++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/lib/util.ts b/src/lib/util.ts index 55e0d74..d7fafdc 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -1,6 +1,5 @@ import mysql from 'mysql2' import { drizzle } from 'drizzle-orm/mysql2' -import { Connection } from 'mysql2/typings/mysql/lib/Connection' export function jsonResponse (data: any, status = 200) { return new Response(JSON.stringify(data, null, 2), { @@ -17,6 +16,7 @@ export function getDatabaseConnection () { password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? '' }) + const db = drizzle(connection) - return drizzle(connection) + return { connection, db } } diff --git a/src/routes/launcher/latest.ts b/src/routes/launcher/latest.ts index 840de6c..dd3c24d 100644 --- a/src/routes/launcher/latest.ts +++ b/src/routes/launcher/latest.ts @@ -3,7 +3,7 @@ import { desc, eq } from 'drizzle-orm' import { getDatabaseConnection } from '../../lib/util' export async function handler () { - const db = getDatabaseConnection() + const { connection, db } = getDatabaseConnection() const version = await db .select({ @@ -14,5 +14,7 @@ export async function handler () { .orderBy(desc(launcherUpdates.place)) .limit(1) + connection.end() + return version[0].id } diff --git a/src/routes/launcher/loader/update-data.ts b/src/routes/launcher/loader/update-data.ts index 57fd672..e45de5f 100644 --- a/src/routes/launcher/loader/update-data.ts +++ b/src/routes/launcher/loader/update-data.ts @@ -4,7 +4,7 @@ import { getDatabaseConnection, jsonResponse } from '../../../lib/util' import { Context } from 'elysia' export async function handler (context: Context) { - const db = getDatabaseConnection() + const { connection, db } = getDatabaseConnection() const platform = context.query.platform as string | undefined const arch = context.query.arch as string | undefined @@ -20,6 +20,7 @@ export async function handler (context: Context) { if (arch == 'x86_64') platString = 'windows' else if (arch == 'aarch64') platString = 'windows-arm64' else { + connection.end() return jsonResponse( { error: 'Unsupported architecture for Windows' }, 400 @@ -28,6 +29,7 @@ export async function handler (context: Context) { } else if (platform == 'linux') { if (arch == 'x86_64') platString = 'linux' else { + connection.end() return jsonResponse( { error: 'Unsupported architecture for Linux' }, 400 @@ -37,12 +39,14 @@ export async function handler (context: Context) { if (arch == 'x86_64') platString = 'macos-intel' else if (arch == 'aarch64') platString = 'macos-silicon' else { + connection.end() return jsonResponse( { error: 'Unsupported architecture for macOS' }, 400 ) } } else { + connection.end() return jsonResponse({ error: 'Unsupported platform' }, 400) } } @@ -88,5 +92,7 @@ export async function handler (context: Context) { return false }) + connection.end() + return jsonResponse(versions[0]) } diff --git a/src/routes/launcher/versions.ts b/src/routes/launcher/versions.ts index 5d86167..6c1578e 100644 --- a/src/routes/launcher/versions.ts +++ b/src/routes/launcher/versions.ts @@ -4,7 +4,7 @@ import { getDatabaseConnection, jsonResponse } from '../../lib/util' import { Context } from 'elysia' export async function handler (context: Context) { - const db = getDatabaseConnection() + const { connection, db } = getDatabaseConnection() const platform = context.query.platform as string | undefined const arch = context.query.arch as string | undefined @@ -20,6 +20,7 @@ export async function handler (context: Context) { if (arch == 'x86_64') platString = 'windows' else if (arch == 'aarch64') platString = 'windows-arm64' else { + connection.end() return jsonResponse( { message: 'Unsupported architecture for Windows', @@ -32,6 +33,7 @@ export async function handler (context: Context) { } else if (platform == 'linux') { if (arch == 'x86_64') platString = 'linux' else { + connection.end() return jsonResponse( { message: 'Unsupported architecture for Linux', @@ -44,6 +46,7 @@ export async function handler (context: Context) { } else if (platform == 'macos') { if (arch == 'x86_64' || arch == 'aarch64') platString = 'macos' else { + connection.end() return jsonResponse( { message: 'Unsupported architecture for macOS', @@ -56,6 +59,7 @@ export async function handler (context: Context) { } else if (platform == 'android') platString = 'android' else if (platform == 'ios') platString = 'ios' else { + connection.end() return jsonResponse( { message: 'Unsupported platform', versions: null, games: null }, 400 @@ -119,5 +123,7 @@ export async function handler (context: Context) { const games = await db.select().from(launcherGames).execute() + connection.end() + return jsonResponse({ versions, games }) }