This commit is contained in:
NoLogicAlan committed 2026-09-10 02:13:17 +03:00
1 parent a188fa0b62
commit 774d188757
2 files changed
-189

No files matched your search

-82
View File
@@ -1,82 +0,0 @@
/**
* @module dashboard/DatabaseManager
* @description MySQL connection pool wrapper for the dashboard. Provides parameterised
* query execution and bcrypt hashing utilities for login code management.
*/
import { compare, genSalt, hash } from "bcryptjs";
import { createPool } from "mysql2/promise";
import { logger } from "../constants/Logger.mjs";
/**
* @class
* @description Wraps a mysql2 connection pool with convenience methods for
* parameterised queries and bcrypt hash operations.
*/
export class DatabaseManager {
/**
* Create a new DatabaseManager.
* @param {object} config - mysql2 pool configuration (host, user, password, database, etc.).
*/
constructor(config) {
this.db = createPool({
connectionLimit: 15,
...config,
});
this.db.on("error", (err) => {
logger.error("[DashboardDB] MySQL pool error:", err.code ?? err.message);
});
}
/**
* Execute a raw SQL query (no parameter binding).
* @async
* @param {string} query - The SQL query string.
* @returns {Promise<Array>} The query result rows and fields.
*/
async query(query) {
return this.db.query(query);
}
/**
* Execute a parameterised SQL query.
* @async
* @param {string} query - The SQL query with `?` placeholders.
* @param {Array} data - The values to bind.
* @returns {Promise<Array>} The result rows.
*/
async execute(query, data) {
const [res, _fields] = await this.db.execute(query, data);
return res;
}
/**
* Generate a bcrypt hash of the given plaintext.
* @async
* @param {string} plain - The plaintext string.
* @returns {Promise<string>} The hashed string.
*/
async hash(plain) {
const salt = await genSalt(10);
return hash(plain, salt);
}
/**
* Compare a plaintext string against a bcrypt hash.
* @async
* @param {string} plain - The plaintext string.
* @param {string} hashed - The bcrypt hash.
* @returns {Promise<boolean>} True if the plaintext matches the hash.
*/
async compareHash(plain, hashed) {
return await compare(plain, hashed);
}
/**
* Close the underlying MySQL connection pool.
* @returns {Promise<void>}
*/
close() {
return this.db.end();
}
}
-107
View File
@@ -1,107 +0,0 @@
/** @module constants/audio/Tuna */
import pkg from "follow-redirects";
const { https } = pkg;
/**
* Client for the Tuna (Voicemod) sound-effect API.
* @class
*/
class Tuna {
/** @private */ apiKey = null;
/**
* @param {{ key: string }} auth - Object containing the API key.
*/
constructor(auth) {
this.apiKey = auth.key;
}
/**
* Perform an authenticated GET request to the Tuna API.
* @private
* @async
* @param {string} path - API path (e.g. `"/v1/sounds/search"`).
* @param {Object<string, *>} [params={}] - Query-string parameters.
* @returns {Promise<object>} Parsed JSON response.
*/
get(path, params = {}) {
return new Promise((resolve, reject) => {
const qs = new URLSearchParams(params).toString();
const query = qs ? "?" + qs : "";
const options = {
method: "GET",
hostname: "tuna-api.voicemod.net",
path: path + query,
headers: { "x-api-key": this.apiKey },
maxRedirects: 20
};
const req = https.request(options, (res) => {
const chunks = [];
res.on("data", (chunk) => { chunks.push(chunk); });
res.on("end", () => {
try {
resolve(JSON.parse(Buffer.concat(chunks).toString()));
} catch (e) {
reject(new Error(`Tuna API JSON parse error: ${e.message}`));
}
});
});
req.on("error", (error) => { reject(error); });
req.setTimeout(15_000, () => {
req.destroy();
reject(new Error("Tuna API request timeout"));
});
req.end();
});
}
/**
* Search for sound effects. Each result item gains a `download()` method
* that returns the OGG audio stream.
* @async
* @param {string} query - Search term.
* @param {number} [page=1]
* @param {number} [size=10]
* @returns {Promise<object>} Search results with enriched `items` array.
*/
search(query, page = 1, size = 10) {
return this.get("/v1/sounds/search", { size, page, search: query }).then(results => {
results.items = results.items.map(s => {
const oggPath = s.oggPath;
s.download = () => {
return new Promise((resolve, reject) => {
const req = https.get(oggPath, { maxRedirects: 20 }, (r) => { resolve(r); });
req.on("error", (err) => { reject(err); });
req.setTimeout(15_000, () => { req.destroy(); reject(new Error("Tuna download timeout")); });
});
};
return s;
});
return results;
});
}
/**
* Fetch a single sound effect by ID. The returned object gains a `download()`
* method that returns the OGG audio stream.
* @async
* @param {string} id - Sound ID.
* @returns {Promise<object>} Sound object with `download()` method.
*/
getSound(id) {
return this.get("/v1/sounds/" + id).then(sound => {
const oggPath = sound.oggPath;
sound.download = () => {
return new Promise((resolve, reject) => {
const req = https.get(oggPath, { maxRedirects: 20 }, (result) => { resolve(result); });
req.on("error", (err) => { reject(err); });
req.setTimeout(15_000, () => { req.destroy(); reject(new Error("Tuna download timeout")); });
});
};
return sound;
});
}
}
export default Tuna;