From 6ab5ca7d712c7c6cb08d62475df2e665177befc1 Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Sun, 10 Mar 2024 13:30:14 -0400 Subject: [PATCH 1/7] fix requester --- dist/src/@Entities/MoonlinkNode.js | 3 +- dist/src/@Managers/PlayerManager.js | 11 ++--- dist/src/@Utils/MoonlinkTrack.d.ts | 1 + dist/src/@Utils/MoonlinkTrack.js | 3 ++ src/@Entities/MoonlinkNode.ts | 3 +- src/@Managers/PlayerManager.ts | 63 +++++++++++++++-------------- src/@Utils/MoonlinkTrack.ts | 3 ++ 7 files changed, 50 insertions(+), 37 deletions(-) diff --git a/dist/src/@Entities/MoonlinkNode.js b/dist/src/@Entities/MoonlinkNode.js index 9349bc8d..d5729e3f 100644 --- a/dist/src/@Entities/MoonlinkNode.js +++ b/dist/src/@Entities/MoonlinkNode.js @@ -229,7 +229,8 @@ class MoonlinkNode { let player = this._manager.players.get(payload.guildId); switch (payload.type) { case "TrackStartEvent": { - player.current = new (index_1.Structure.get("MoonlinkTrack"))(payload.track); + if (!player.current) + player.current = new (index_1.Structure.get("MoonlinkTrack"))(payload.track); player.playing = true; player.paused = false; this._manager.emit("trackStart", player, player.current); diff --git a/dist/src/@Managers/PlayerManager.js b/dist/src/@Managers/PlayerManager.js index db68def9..9c67cd7b 100644 --- a/dist/src/@Managers/PlayerManager.js +++ b/dist/src/@Managers/PlayerManager.js @@ -43,11 +43,12 @@ class PlayerManager { }; } async attemptConnection(guildId) { - if (!this.cache[guildId] || - !this.voices[guildId] || - (!this.voices[guildId]?.token && - !this.voices[guildId]?.endpoint && - !this.voices[guildId]?.sessionId)) + if (!this.cache[guildId]) + return false; + if (this.voices[guildId] && + !this.voices[guildId]?.token && + !this.voices[guildId]?.endpoint && + !this.voices[guildId]?.sessionId) return false; if (this._manager.options?.balancingPlayersByRegion) { const voiceRegion = this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1]; diff --git a/dist/src/@Utils/MoonlinkTrack.d.ts b/dist/src/@Utils/MoonlinkTrack.d.ts index 1e41c262..5d5361d1 100644 --- a/dist/src/@Utils/MoonlinkTrack.d.ts +++ b/dist/src/@Utils/MoonlinkTrack.d.ts @@ -16,4 +16,5 @@ export declare class MoonlinkTrack { time?: number; constructor(data?: MoonlinkTrackOptions, requester?: string | any); get calculateRealTimePosition(): number; + setRequester(data: any): void; } diff --git a/dist/src/@Utils/MoonlinkTrack.js b/dist/src/@Utils/MoonlinkTrack.js index d5cbe625..ca200a0f 100644 --- a/dist/src/@Utils/MoonlinkTrack.js +++ b/dist/src/@Utils/MoonlinkTrack.js @@ -45,5 +45,8 @@ class MoonlinkTrack { } return this.position; } + setRequester(data) { + this.requester = data; + } } exports.MoonlinkTrack = MoonlinkTrack; diff --git a/src/@Entities/MoonlinkNode.ts b/src/@Entities/MoonlinkNode.ts index ceca051c..d3065126 100644 --- a/src/@Entities/MoonlinkNode.ts +++ b/src/@Entities/MoonlinkNode.ts @@ -355,9 +355,10 @@ export class MoonlinkNode { let player: MoonlinkPlayer = this._manager.players.get(payload.guildId); switch (payload.type) { case "TrackStartEvent": { - player.current = new (Structure.get("MoonlinkTrack"))( + if(!player.current) player.current = new (Structure.get("MoonlinkTrack"))( payload.track ); + player.playing = true; player.paused = false; this._manager.emit("trackStart", player, player.current); diff --git a/src/@Managers/PlayerManager.ts b/src/@Managers/PlayerManager.ts index b5e74045..ce5a8ca7 100644 --- a/src/@Managers/PlayerManager.ts +++ b/src/@Managers/PlayerManager.ts @@ -69,38 +69,40 @@ export class PlayerManager { }; } public async attemptConnection(guildId: string): Promise { + if (!this.cache[guildId]) return false; if ( - !this.cache[guildId] || - !this.voices[guildId] || - (!this.voices[guildId]?.token && - !this.voices[guildId]?.endpoint && - !this.voices[guildId]?.sessionId) - ) - return false; - - if (this._manager.options?.balancingPlayersByRegion) { - const voiceRegion = - this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1]; - - if (!this.cache[guildId].voiceRegion) { - const connectedNodes = [ - ...this._manager.nodes.map.values() - ].filter(node => node.state == "READY"); - const matchingNode = connectedNodes.find(node => - node.regions.includes(voiceRegion) - ); - - this.cache[guildId].voiceRegion = voiceRegion; - - if (matchingNode) { - this.cache[guildId].node = matchingNode; + this.voices[guildId] && + !this.voices[guildId]?.token && + !this.voices[guildId]?.endpoint && + !this.voices[guildId]?.sessionId + ) return false; + if (this._manager.options?.balancingPlayersByRegion) { + const voiceRegion = + this.voices[guildId]?.endpoint?.match( + /([a-zA-Z-]+)\d+/ + )?.[1]; + + if (!this.cache[guildId].voiceRegion) { + const connectedNodes = [ + ...this._manager.nodes.map.values() + ].filter(node => node.state == "READY"); + const matchingNode = connectedNodes.find(node => + node.regions.includes(voiceRegion) + ); + + this.cache[guildId].voiceRegion = voiceRegion; + + if (matchingNode) { + this.cache[guildId].node = matchingNode; + } } + } else if (!this.cache[guildId].voiceRegion) { + const voiceRegion = + this.voices[guildId]?.endpoint?.match( + /([a-zA-Z-]+)\d+/ + )?.[1]; + this.cache[guildId].voiceRegion = voiceRegion; } - } else if (!this.cache[guildId].voiceRegion) { - const voiceRegion = - this.voices[guildId]?.endpoint?.match(/([a-zA-Z-]+)\d+/)?.[1]; - this.cache[guildId].voiceRegion = voiceRegion; - } await this.cache[guildId].node.rest.update({ guildId, @@ -220,6 +222,7 @@ export class PlayerManager { } public delete(guildId): void { delete this.cache[guildId]; - if(Structure.db.get(`players.${guildId}`))Structure.db.delete(`players.${guildId}`); + if (Structure.db.get(`players.${guildId}`)) + Structure.db.delete(`players.${guildId}`); } } diff --git a/src/@Utils/MoonlinkTrack.ts b/src/@Utils/MoonlinkTrack.ts index f66a87a1..403d0faf 100644 --- a/src/@Utils/MoonlinkTrack.ts +++ b/src/@Utils/MoonlinkTrack.ts @@ -48,4 +48,7 @@ export class MoonlinkTrack { return this.position; } + setRequester(data: any): void { + this.requester = data; + } } From fa075d361ab4843d119c006863dc28e2ba2436be Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Mon, 11 Mar 2024 11:28:57 -0400 Subject: [PATCH 2/7] fix: database was not closing the files it opened --- dist/src/@Utils/MoonlinkDatabase.js | 10 +++++++--- src/@Utils/MoonlinkDatabase.ts | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/dist/src/@Utils/MoonlinkDatabase.js b/dist/src/@Utils/MoonlinkDatabase.js index f8635d4b..3863c50f 100644 --- a/dist/src/@Utils/MoonlinkDatabase.js +++ b/dist/src/@Utils/MoonlinkDatabase.js @@ -88,8 +88,10 @@ class MoonlinkDatabase { fs_1.default.mkdirSync(directory, { recursive: true }); } const filePath = this.getFilePath(); - const rawData = fs_1.default.readFileSync(filePath, "utf-8"); + const fileDescriptor = fs_1.default.openSync(filePath, "r"); + const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); this.data = JSON.parse(rawData) || {}; + fs_1.default.closeSync(fileDescriptor); } catch (err) { if (err.code === "ENOENT") { @@ -103,10 +105,12 @@ class MoonlinkDatabase { save() { try { const filePath = this.getFilePath(); - fs_1.default.writeFileSync(filePath, JSON.stringify(this.data, null, 2)); + const fileDescriptor = fs_1.default.openSync(filePath, "w"); + fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); + fs_1.default.closeSync(fileDescriptor); } catch (error) { - throw new Error("@Moonlink(Database) - Failed to save data"); + throw new Error("@Moonlink(Database) - Failed to save data, error: ", error); } } } diff --git a/src/@Utils/MoonlinkDatabase.ts b/src/@Utils/MoonlinkDatabase.ts index 2665777c..f507f851 100644 --- a/src/@Utils/MoonlinkDatabase.ts +++ b/src/@Utils/MoonlinkDatabase.ts @@ -4,7 +4,7 @@ import path from "path"; type Data = Record; export class MoonlinkDatabase { - public data: Data = {}; + public data: Data = {}; public id: string; constructor(clientId: string) { @@ -104,8 +104,10 @@ export class MoonlinkDatabase { const filePath = this.getFilePath(); - const rawData = fs.readFileSync(filePath, "utf-8"); + const fileDescriptor = fs.openSync(filePath, "r"); + const rawData = fs.readFileSync(fileDescriptor, "utf-8"); this.data = JSON.parse(rawData) || {}; + fs.closeSync(fileDescriptor); } catch (err) { if (err.code === "ENOENT") { this.data = {}; @@ -117,13 +119,14 @@ export class MoonlinkDatabase { } } } - private save() { - try { - const filePath = this.getFilePath(); - fs.writeFileSync(filePath, JSON.stringify(this.data, null, 2)); - } catch (error) { - throw new Error("@Moonlink(Database) - Failed to save data"); - } + try { + const filePath = this.getFilePath(); + const fileDescriptor = fs.openSync(filePath, "w"); + fs.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); + fs.closeSync(fileDescriptor); + } catch (error) { + throw new Error("@Moonlink(Database) - Failed to save data, error: ", error); } } +} From c23ab754ea8040eea48a3f1188c135c8e0ba7a90 Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Tue, 12 Mar 2024 11:50:03 -0400 Subject: [PATCH 3/7] Provision: option to disable the system from saving and retrieving information from the file --- dist/src/@Entities/MoonlinkNode.js | 2 ++ dist/src/@Typings/index.d.ts | 2 ++ dist/src/@Utils/MoonlinkDatabase.d.ts | 1 + dist/src/@Utils/MoonlinkDatabase.js | 7 +++++++ src/@Entities/MoonlinkNode.ts | 11 +++++----- src/@Typings/index.ts | 2 ++ src/@Utils/MoonlinkDatabase.ts | 30 ++++++++++++++++++--------- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/dist/src/@Entities/MoonlinkNode.js b/dist/src/@Entities/MoonlinkNode.js index d5729e3f..bb9b196a 100644 --- a/dist/src/@Entities/MoonlinkNode.js +++ b/dist/src/@Entities/MoonlinkNode.js @@ -39,6 +39,8 @@ class MoonlinkNode { this.regions = node.regions; this.http = `http${node.secure ? "s" : ""}://${this.address}/v4/`; this.rest = new (index_1.Structure.get("MoonlinkRestFul"))(this); + if (node.sessionId) + this.sessionId = node.sessionId; this.connect(); } get address() { diff --git a/dist/src/@Typings/index.d.ts b/dist/src/@Typings/index.d.ts index 052962d1..1c9edec2 100644 --- a/dist/src/@Typings/index.d.ts +++ b/dist/src/@Typings/index.d.ts @@ -95,6 +95,7 @@ export interface INode { regions?: string[]; retryAmount?: number; retryDelay?: number; + sessionId?: string; } export interface IOptions { clientId?: string; @@ -104,6 +105,7 @@ export interface IOptions { resume?: boolean; plugins?: Plugin[]; http2?: boolean; + doNotSaveToFiles?: boolean; switchPlayersAnotherNode?: boolean; destroyPlayersStopped?: boolean; balancingPlayersByRegion?: boolean; diff --git a/dist/src/@Utils/MoonlinkDatabase.d.ts b/dist/src/@Utils/MoonlinkDatabase.d.ts index 6cfaac37..384fdc8c 100644 --- a/dist/src/@Utils/MoonlinkDatabase.d.ts +++ b/dist/src/@Utils/MoonlinkDatabase.d.ts @@ -2,6 +2,7 @@ type Data = Record; export declare class MoonlinkDatabase { data: Data; id: string; + doNotSaveToFiles: boolean; constructor(clientId: string); set(key: string, value: T): void; get(key: string): T | undefined; diff --git a/dist/src/@Utils/MoonlinkDatabase.js b/dist/src/@Utils/MoonlinkDatabase.js index 3863c50f..604b7ba1 100644 --- a/dist/src/@Utils/MoonlinkDatabase.js +++ b/dist/src/@Utils/MoonlinkDatabase.js @@ -6,12 +6,15 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.MoonlinkDatabase = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); +const index_1 = require("../../index"); class MoonlinkDatabase { data = {}; id; + doNotSaveToFiles; constructor(clientId) { this.fetch(); this.id = clientId; + this.doNotSaveToFiles = index_1.Structure.manager.options?.doNotSaveToFiles; } set(key, value) { if (!key) @@ -82,6 +85,8 @@ class MoonlinkDatabase { return path_1.default.join(__dirname, "../@Datastore", `database-${this.id}.json`); } fetch() { + if (this.doNotSaveToFiles) + return; try { const directory = path_1.default.join(__dirname, "../@Datastore"); if (!fs_1.default.existsSync(directory)) { @@ -103,6 +108,8 @@ class MoonlinkDatabase { } } save() { + if (this.doNotSaveToFiles) + return; try { const filePath = this.getFilePath(); const fileDescriptor = fs_1.default.openSync(filePath, "w"); diff --git a/src/@Entities/MoonlinkNode.ts b/src/@Entities/MoonlinkNode.ts index d3065126..72591098 100644 --- a/src/@Entities/MoonlinkNode.ts +++ b/src/@Entities/MoonlinkNode.ts @@ -58,7 +58,7 @@ export class MoonlinkNode { this.regions = node.regions; this.http = `http${node.secure ? "s" : ""}://${this.address}/v4/`; this.rest = new (Structure.get("MoonlinkRestFul"))(this); - + if (node.sessionId) this.sessionId = node.sessionId; this.connect(); } @@ -355,10 +355,11 @@ export class MoonlinkNode { let player: MoonlinkPlayer = this._manager.players.get(payload.guildId); switch (payload.type) { case "TrackStartEvent": { - if(!player.current) player.current = new (Structure.get("MoonlinkTrack"))( - payload.track - ); - + if (!player.current) + player.current = new (Structure.get("MoonlinkTrack"))( + payload.track + ); + player.playing = true; player.paused = false; this._manager.emit("trackStart", player, player.current); diff --git a/src/@Typings/index.ts b/src/@Typings/index.ts index fcf96d91..51299678 100644 --- a/src/@Typings/index.ts +++ b/src/@Typings/index.ts @@ -128,6 +128,7 @@ export interface INode { regions?: string[]; retryAmount?: number; retryDelay?: number; + sessionId?: string; } export interface IOptions { @@ -138,6 +139,7 @@ export interface IOptions { resume?: boolean; plugins?: Plugin[]; http2?: boolean; + doNotSaveToFiles?: boolean; switchPlayersAnotherNode?: boolean; destroyPlayersStopped?: boolean; balancingPlayersByRegion?: boolean; diff --git a/src/@Utils/MoonlinkDatabase.ts b/src/@Utils/MoonlinkDatabase.ts index f507f851..02ec7384 100644 --- a/src/@Utils/MoonlinkDatabase.ts +++ b/src/@Utils/MoonlinkDatabase.ts @@ -1,15 +1,16 @@ import fs from "fs"; import path from "path"; - +import { Structure } from "../../index"; type Data = Record; export class MoonlinkDatabase { public data: Data = {}; public id: string; - + public doNotSaveToFiles: boolean; constructor(clientId: string) { this.fetch(); this.id = clientId; + this.doNotSaveToFiles = Structure.manager.options?.doNotSaveToFiles; } set(key: string, value: T): void { @@ -96,6 +97,8 @@ export class MoonlinkDatabase { } fetch() { + if (this.doNotSaveToFiles) return; + try { const directory = path.join(__dirname, "../@Datastore"); if (!fs.existsSync(directory)) { @@ -120,13 +123,20 @@ export class MoonlinkDatabase { } } private save() { - try { - const filePath = this.getFilePath(); - const fileDescriptor = fs.openSync(filePath, "w"); - fs.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); - fs.closeSync(fileDescriptor); - } catch (error) { - throw new Error("@Moonlink(Database) - Failed to save data, error: ", error); + if (this.doNotSaveToFiles) return; + try { + const filePath = this.getFilePath(); + const fileDescriptor = fs.openSync(filePath, "w"); + fs.writeFileSync( + fileDescriptor, + JSON.stringify(this.data, null, 2) + ); + fs.closeSync(fileDescriptor); + } catch (error) { + throw new Error( + "@Moonlink(Database) - Failed to save data, error: ", + error + ); + } } } -} From 952583d598aee082bb784bf953ce4fc1335315af Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Wed, 13 Mar 2024 09:25:50 -0400 Subject: [PATCH 4/7] Improvement: attempt to save again and retrieve data --- dist/src/@Utils/MoonlinkDatabase.js | 90 ++++++++++++++++++++--------- src/@Utils/MoonlinkDatabase.ts | 81 +++++++++++++++++--------- 2 files changed, 119 insertions(+), 52 deletions(-) diff --git a/dist/src/@Utils/MoonlinkDatabase.js b/dist/src/@Utils/MoonlinkDatabase.js index 604b7ba1..54633060 100644 --- a/dist/src/@Utils/MoonlinkDatabase.js +++ b/dist/src/@Utils/MoonlinkDatabase.js @@ -87,38 +87,76 @@ class MoonlinkDatabase { fetch() { if (this.doNotSaveToFiles) return; - try { - const directory = path_1.default.join(__dirname, "../@Datastore"); - if (!fs_1.default.existsSync(directory)) { - fs_1.default.mkdirSync(directory, { recursive: true }); - } - const filePath = this.getFilePath(); - const fileDescriptor = fs_1.default.openSync(filePath, "r"); - const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); - this.data = JSON.parse(rawData) || {}; - fs_1.default.closeSync(fileDescriptor); - } - catch (err) { - if (err.code === "ENOENT") { - this.data = {}; + const maxAttempts = 10; + let attempts = 0; + const fetchWithRetry = () => { + try { + const directory = path_1.default.join(__dirname, "../@Datastore"); + if (!fs_1.default.existsSync(directory)) { + fs_1.default.mkdirSync(directory, { recursive: true }); + } + const filePath = this.getFilePath(); + const fileDescriptor = fs_1.default.openSync(filePath, "r"); + try { + const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); + this.data = JSON.parse(rawData) || {}; + } + finally { + fs_1.default.closeSync(fileDescriptor); + } } - else { - throw new Error("@Moonlink(Database) - Failed to fetch data (Error):", err); + catch (err) { + if (attempts < maxAttempts) { + if (err.code === "EMFILE") { + attempts++; + setTimeout(fetchWithRetry, 1000); + } + else if (err.code === "ENOENT") { + this.data = {}; + } + else { + throw err; + } + } + else { + throw err; + } } - } + }; + fetchWithRetry(); } save() { if (this.doNotSaveToFiles) return; - try { - const filePath = this.getFilePath(); - const fileDescriptor = fs_1.default.openSync(filePath, "w"); - fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); - fs_1.default.closeSync(fileDescriptor); - } - catch (error) { - throw new Error("@Moonlink(Database) - Failed to save data, error: ", error); - } + const maxAttempts = 10; + let attempts = 0; + const saveWithRetry = () => { + try { + const filePath = this.getFilePath(); + const fileDescriptor = fs_1.default.openSync(filePath, "w"); + try { + fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); + } + finally { + fs_1.default.closeSync(fileDescriptor); + } + } + catch (err) { + if (attempts < maxAttempts) { + if (err.code === "EMFILE") { + attempts++; + setTimeout(saveWithRetry, 1000); + } + else { + throw err; + } + } + else { + throw err; + } + } + }; + saveWithRetry(); } } exports.MoonlinkDatabase = MoonlinkDatabase; diff --git a/src/@Utils/MoonlinkDatabase.ts b/src/@Utils/MoonlinkDatabase.ts index 02ec7384..d3abd23b 100644 --- a/src/@Utils/MoonlinkDatabase.ts +++ b/src/@Utils/MoonlinkDatabase.ts @@ -95,10 +95,12 @@ export class MoonlinkDatabase { `database-${this.id}.json` ); } +fetch() { + if (this.doNotSaveToFiles) return; + const maxAttempts = 10; + let attempts = 0; - fetch() { - if (this.doNotSaveToFiles) return; - + const fetchWithRetry = () => { try { const directory = path.join(__dirname, "../@Datastore"); if (!fs.existsSync(directory)) { @@ -108,35 +110,62 @@ export class MoonlinkDatabase { const filePath = this.getFilePath(); const fileDescriptor = fs.openSync(filePath, "r"); - const rawData = fs.readFileSync(fileDescriptor, "utf-8"); - this.data = JSON.parse(rawData) || {}; - fs.closeSync(fileDescriptor); + try { + const rawData = fs.readFileSync(fileDescriptor, "utf-8"); + this.data = JSON.parse(rawData) || {}; + } finally { + fs.closeSync(fileDescriptor); + } } catch (err) { - if (err.code === "ENOENT") { - this.data = {}; + if (attempts < maxAttempts) { + if (err.code === "EMFILE") { + attempts++; + setTimeout(fetchWithRetry, 1000); + } else if (err.code === "ENOENT") { + this.data = {}; + } else { + throw err; + } } else { - throw new Error( - "@Moonlink(Database) - Failed to fetch data (Error):", - err - ); + throw err; } } - } - private save() { - if (this.doNotSaveToFiles) return; + }; + + fetchWithRetry(); +} + +private save() { + if (this.doNotSaveToFiles) return; + const maxAttempts = 10; + let attempts = 0; + + const saveWithRetry = () => { try { const filePath = this.getFilePath(); const fileDescriptor = fs.openSync(filePath, "w"); - fs.writeFileSync( - fileDescriptor, - JSON.stringify(this.data, null, 2) - ); - fs.closeSync(fileDescriptor); - } catch (error) { - throw new Error( - "@Moonlink(Database) - Failed to save data, error: ", - error - ); + try { + fs.writeFileSync( + fileDescriptor, + JSON.stringify(this.data, null, 2) + ); + } finally { + fs.closeSync(fileDescriptor); + } + } catch (err) { + if (attempts < maxAttempts) { + if (err.code === "EMFILE") { + attempts++; + setTimeout(saveWithRetry, 1000); + } else { + throw err; + } + } else { + throw err; + } } - } + }; + + saveWithRetry(); +} } From e0f5e0ec5266bfaec086f7f51c42b7ee5fe60d5b Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Wed, 13 Mar 2024 21:06:52 -0400 Subject: [PATCH 5/7] improvement: database --- dist/src/@Utils/MoonlinkDatabase.js | 95 +++++++++------------------- src/@Utils/MoonlinkDatabase.ts | 98 +++++++++++------------------ 2 files changed, 64 insertions(+), 129 deletions(-) diff --git a/dist/src/@Utils/MoonlinkDatabase.js b/dist/src/@Utils/MoonlinkDatabase.js index 54633060..916f00dd 100644 --- a/dist/src/@Utils/MoonlinkDatabase.js +++ b/dist/src/@Utils/MoonlinkDatabase.js @@ -87,76 +87,39 @@ class MoonlinkDatabase { fetch() { if (this.doNotSaveToFiles) return; - const maxAttempts = 10; - let attempts = 0; - const fetchWithRetry = () => { - try { - const directory = path_1.default.join(__dirname, "../@Datastore"); - if (!fs_1.default.existsSync(directory)) { - fs_1.default.mkdirSync(directory, { recursive: true }); - } - const filePath = this.getFilePath(); - const fileDescriptor = fs_1.default.openSync(filePath, "r"); - try { - const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); - this.data = JSON.parse(rawData) || {}; - } - finally { - fs_1.default.closeSync(fileDescriptor); - } - } - catch (err) { - if (attempts < maxAttempts) { - if (err.code === "EMFILE") { - attempts++; - setTimeout(fetchWithRetry, 1000); - } - else if (err.code === "ENOENT") { - this.data = {}; - } - else { - throw err; - } - } - else { - throw err; - } - } - }; - fetchWithRetry(); + const directory = path_1.default.join(__dirname, "../@Datastore"); + if (!fs_1.default.existsSync(directory)) { + fs_1.default.mkdirSync(directory, { recursive: true }); + } + let fileDescriptor; + const filePath = this.getFilePath(); + try { + fileDescriptor = fs_1.default.openSync(filePath, "r"); + const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); + this.data = JSON.parse(rawData) || {}; + } + catch (err) { + index_1.Structure.manager.emit("debug", "Moonlink(Database) - Error: " + err); + } + finally { + fs_1.default.closeSync(fileDescriptor); + } } save() { if (this.doNotSaveToFiles) return; - const maxAttempts = 10; - let attempts = 0; - const saveWithRetry = () => { - try { - const filePath = this.getFilePath(); - const fileDescriptor = fs_1.default.openSync(filePath, "w"); - try { - fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); - } - finally { - fs_1.default.closeSync(fileDescriptor); - } - } - catch (err) { - if (attempts < maxAttempts) { - if (err.code === "EMFILE") { - attempts++; - setTimeout(saveWithRetry, 1000); - } - else { - throw err; - } - } - else { - throw err; - } - } - }; - saveWithRetry(); + let fileDescriptor; + try { + const filePath = this.getFilePath(); + fileDescriptor = fs_1.default.openSync(filePath, "w"); + fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); + } + catch (err) { + index_1.Structure.manager.emit("debug", "Moonlink(Database) - Error: " + err); + } + finally { + fs_1.default.closeSync(fileDescriptor); + } } } exports.MoonlinkDatabase = MoonlinkDatabase; diff --git a/src/@Utils/MoonlinkDatabase.ts b/src/@Utils/MoonlinkDatabase.ts index d3abd23b..c1077767 100644 --- a/src/@Utils/MoonlinkDatabase.ts +++ b/src/@Utils/MoonlinkDatabase.ts @@ -95,77 +95,49 @@ export class MoonlinkDatabase { `database-${this.id}.json` ); } -fetch() { - if (this.doNotSaveToFiles) return; - const maxAttempts = 10; - let attempts = 0; + fetch() { + if (this.doNotSaveToFiles) return; - const fetchWithRetry = () => { - try { - const directory = path.join(__dirname, "../@Datastore"); - if (!fs.existsSync(directory)) { - fs.mkdirSync(directory, { recursive: true }); - } + const directory = path.join(__dirname, "../@Datastore"); + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory, { recursive: true }); + } - const filePath = this.getFilePath(); + let fileDescriptor; + const filePath = this.getFilePath(); + try { + fileDescriptor = fs.openSync(filePath, "r"); - const fileDescriptor = fs.openSync(filePath, "r"); - try { - const rawData = fs.readFileSync(fileDescriptor, "utf-8"); - this.data = JSON.parse(rawData) || {}; - } finally { - fs.closeSync(fileDescriptor); - } + const rawData = fs.readFileSync(fileDescriptor, "utf-8"); + this.data = JSON.parse(rawData) || {}; } catch (err) { - if (attempts < maxAttempts) { - if (err.code === "EMFILE") { - attempts++; - setTimeout(fetchWithRetry, 1000); - } else if (err.code === "ENOENT") { - this.data = {}; - } else { - throw err; - } - } else { - throw err; - } + Structure.manager.emit( + "debug", + "Moonlink(Database) - Error: " + err + ); + } finally { + fs.closeSync(fileDescriptor); } - }; - - fetchWithRetry(); -} - -private save() { - if (this.doNotSaveToFiles) return; - const maxAttempts = 10; - let attempts = 0; + } - const saveWithRetry = () => { + private save() { + if (this.doNotSaveToFiles) return; + let fileDescriptor; try { const filePath = this.getFilePath(); - const fileDescriptor = fs.openSync(filePath, "w"); - try { - fs.writeFileSync( - fileDescriptor, - JSON.stringify(this.data, null, 2) - ); - } finally { - fs.closeSync(fileDescriptor); - } + fileDescriptor = fs.openSync(filePath, "w"); + + fs.writeFileSync( + fileDescriptor, + JSON.stringify(this.data, null, 2) + ); } catch (err) { - if (attempts < maxAttempts) { - if (err.code === "EMFILE") { - attempts++; - setTimeout(saveWithRetry, 1000); - } else { - throw err; - } - } else { - throw err; - } + Structure.manager.emit( + "debug", + "Moonlink(Database) - Error: " + err + ); + } finally { + fs.closeSync(fileDescriptor); } - }; - - saveWithRetry(); -} + } } From 8612d15bd94124fe83520c55e259449962a2da60 Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Fri, 15 Mar 2024 21:00:25 -0400 Subject: [PATCH 6/7] restore --- dist/index.mjs | 17 -------- dist/src/@Utils/MoonlinkDatabase.d.ts | 1 - dist/src/@Utils/MoonlinkDatabase.js | 42 +++++++------------ src/@Utils/MoonlinkDatabase.ts | 60 ++++++++++----------------- 4 files changed, 38 insertions(+), 82 deletions(-) delete mode 100644 dist/index.mjs diff --git a/dist/index.mjs b/dist/index.mjs deleted file mode 100644 index ce350811..00000000 --- a/dist/index.mjs +++ /dev/null @@ -1,17 +0,0 @@ -import mod from "./index.js"; - -export default mod; -export const MoonlinkDatabase = mod.MoonlinkDatabase; -export const MoonlinkFilters = mod.MoonlinkFilters; -export const MoonlinkManager = mod.MoonlinkManager; -export const MoonlinkNode = mod.MoonlinkNode; -export const MoonlinkPlayer = mod.MoonlinkPlayer; -export const MoonlinkQueue = mod.MoonlinkQueue; -export const MoonlinkRestFul = mod.MoonlinkRestFul; -export const MoonlinkTrack = mod.MoonlinkTrack; -export const NodeManager = mod.NodeManager; -export const PlayerManager = mod.PlayerManager; -export const Plugin = mod.Plugin; -export const Structure = mod.Structure; -export const makeRequest = mod.makeRequest; -export const version = mod.version; diff --git a/dist/src/@Utils/MoonlinkDatabase.d.ts b/dist/src/@Utils/MoonlinkDatabase.d.ts index 384fdc8c..6cfaac37 100644 --- a/dist/src/@Utils/MoonlinkDatabase.d.ts +++ b/dist/src/@Utils/MoonlinkDatabase.d.ts @@ -2,7 +2,6 @@ type Data = Record; export declare class MoonlinkDatabase { data: Data; id: string; - doNotSaveToFiles: boolean; constructor(clientId: string); set(key: string, value: T): void; get(key: string): T | undefined; diff --git a/dist/src/@Utils/MoonlinkDatabase.js b/dist/src/@Utils/MoonlinkDatabase.js index 916f00dd..f8635d4b 100644 --- a/dist/src/@Utils/MoonlinkDatabase.js +++ b/dist/src/@Utils/MoonlinkDatabase.js @@ -6,15 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.MoonlinkDatabase = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); -const index_1 = require("../../index"); class MoonlinkDatabase { data = {}; id; - doNotSaveToFiles; constructor(clientId) { this.fetch(); this.id = clientId; - this.doNotSaveToFiles = index_1.Structure.manager.options?.doNotSaveToFiles; } set(key, value) { if (!key) @@ -85,40 +82,31 @@ class MoonlinkDatabase { return path_1.default.join(__dirname, "../@Datastore", `database-${this.id}.json`); } fetch() { - if (this.doNotSaveToFiles) - return; - const directory = path_1.default.join(__dirname, "../@Datastore"); - if (!fs_1.default.existsSync(directory)) { - fs_1.default.mkdirSync(directory, { recursive: true }); - } - let fileDescriptor; - const filePath = this.getFilePath(); try { - fileDescriptor = fs_1.default.openSync(filePath, "r"); - const rawData = fs_1.default.readFileSync(fileDescriptor, "utf-8"); + const directory = path_1.default.join(__dirname, "../@Datastore"); + if (!fs_1.default.existsSync(directory)) { + fs_1.default.mkdirSync(directory, { recursive: true }); + } + const filePath = this.getFilePath(); + const rawData = fs_1.default.readFileSync(filePath, "utf-8"); this.data = JSON.parse(rawData) || {}; } catch (err) { - index_1.Structure.manager.emit("debug", "Moonlink(Database) - Error: " + err); - } - finally { - fs_1.default.closeSync(fileDescriptor); + if (err.code === "ENOENT") { + this.data = {}; + } + else { + throw new Error("@Moonlink(Database) - Failed to fetch data (Error):", err); + } } } save() { - if (this.doNotSaveToFiles) - return; - let fileDescriptor; try { const filePath = this.getFilePath(); - fileDescriptor = fs_1.default.openSync(filePath, "w"); - fs_1.default.writeFileSync(fileDescriptor, JSON.stringify(this.data, null, 2)); - } - catch (err) { - index_1.Structure.manager.emit("debug", "Moonlink(Database) - Error: " + err); + fs_1.default.writeFileSync(filePath, JSON.stringify(this.data, null, 2)); } - finally { - fs_1.default.closeSync(fileDescriptor); + catch (error) { + throw new Error("@Moonlink(Database) - Failed to save data"); } } } diff --git a/src/@Utils/MoonlinkDatabase.ts b/src/@Utils/MoonlinkDatabase.ts index c1077767..0b7974ad 100644 --- a/src/@Utils/MoonlinkDatabase.ts +++ b/src/@Utils/MoonlinkDatabase.ts @@ -1,16 +1,15 @@ import fs from "fs"; import path from "path"; -import { Structure } from "../../index"; + type Data = Record; export class MoonlinkDatabase { - public data: Data = {}; + public data: Data = {}; public id: string; - public doNotSaveToFiles: boolean; + constructor(clientId: string) { this.fetch(); this.id = clientId; - this.doNotSaveToFiles = Structure.manager.options?.doNotSaveToFiles; } set(key: string, value: T): void { @@ -95,49 +94,36 @@ export class MoonlinkDatabase { `database-${this.id}.json` ); } - fetch() { - if (this.doNotSaveToFiles) return; - - const directory = path.join(__dirname, "../@Datastore"); - if (!fs.existsSync(directory)) { - fs.mkdirSync(directory, { recursive: true }); - } - let fileDescriptor; - const filePath = this.getFilePath(); + fetch() { try { - fileDescriptor = fs.openSync(filePath, "r"); + const directory = path.join(__dirname, "../@Datastore"); + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory, { recursive: true }); + } + + const filePath = this.getFilePath(); - const rawData = fs.readFileSync(fileDescriptor, "utf-8"); + const rawData = fs.readFileSync(filePath, "utf-8"); this.data = JSON.parse(rawData) || {}; } catch (err) { - Structure.manager.emit( - "debug", - "Moonlink(Database) - Error: " + err - ); - } finally { - fs.closeSync(fileDescriptor); + if (err.code === "ENOENT") { + this.data = {}; + } else { + throw new Error( + "@Moonlink(Database) - Failed to fetch data (Error):", + err + ); + } } } private save() { - if (this.doNotSaveToFiles) return; - let fileDescriptor; try { const filePath = this.getFilePath(); - fileDescriptor = fs.openSync(filePath, "w"); - - fs.writeFileSync( - fileDescriptor, - JSON.stringify(this.data, null, 2) - ); - } catch (err) { - Structure.manager.emit( - "debug", - "Moonlink(Database) - Error: " + err - ); - } finally { - fs.closeSync(fileDescriptor); + fs.writeFileSync(filePath, JSON.stringify(this.data, null, 2)); + } catch (error) { + throw new Error("@Moonlink(Database) - Failed to save data"); } } -} +} \ No newline at end of file From 89aea1c4f7eda7c442b0ca770204aeed8d4fe238 Mon Sep 17 00:00:00 2001 From: 1Lucas1apk Date: Fri, 15 Mar 2024 21:01:07 -0400 Subject: [PATCH 7/7] index.mjs --- dist/index.mjs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 dist/index.mjs diff --git a/dist/index.mjs b/dist/index.mjs new file mode 100644 index 00000000..ce350811 --- /dev/null +++ b/dist/index.mjs @@ -0,0 +1,17 @@ +import mod from "./index.js"; + +export default mod; +export const MoonlinkDatabase = mod.MoonlinkDatabase; +export const MoonlinkFilters = mod.MoonlinkFilters; +export const MoonlinkManager = mod.MoonlinkManager; +export const MoonlinkNode = mod.MoonlinkNode; +export const MoonlinkPlayer = mod.MoonlinkPlayer; +export const MoonlinkQueue = mod.MoonlinkQueue; +export const MoonlinkRestFul = mod.MoonlinkRestFul; +export const MoonlinkTrack = mod.MoonlinkTrack; +export const NodeManager = mod.NodeManager; +export const PlayerManager = mod.PlayerManager; +export const Plugin = mod.Plugin; +export const Structure = mod.Structure; +export const makeRequest = mod.makeRequest; +export const version = mod.version;