From 8cf0a8eb5bbe406a38982dee56fc11aab4635034 Mon Sep 17 00:00:00 2001 From: Shinuwa Date: Sun, 29 Nov 2026 12:23:28 +0100 Subject: [PATCH] Stop listening when an events client disconnects; warn on partial OAuth setup - Detach the events stream from the queue on the response's close event, which fires when the client goes away whatever the Node version. - Log a warning when only some of the Discord login variables are set, instead of silently leaving the login disabled. Co-Authored-By: Claude Opus 5.5 --- src/config.js | 7 +++++++ src/web/routes/events.js | 3 ++- test/webServer.test.js | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 1cd1bfd..a31100f 100644 --- a/src/config.js +++ b/src/config.js @@ -49,6 +49,13 @@ const webPublicUrl = process.env.WEB_PUBLIC_URL?.trim().replace(/\/+$/, "") || n const sessionSecret = process.env.SESSION_SECRET?.trim() || null; const discordOAuthEnabled = Boolean(discordClientSecret && webPublicUrl && sessionSecret); +const oauthVariablesSet = [discordClientSecret, webPublicUrl, sessionSecret].filter(Boolean).length; +if (oauthVariablesSet > 0 && !discordOAuthEnabled) { + console.warn( + "Connexion avec Discord désactivée : DISCORD_CLIENT_SECRET, WEB_PUBLIC_URL et SESSION_SECRET doivent toutes être définies" + ); +} + if (discordOAuthEnabled) { let publicUrl; try { diff --git a/src/web/routes/events.js b/src/web/routes/events.js index c0579e3..4bd3fff 100644 --- a/src/web/routes/events.js +++ b/src/web/routes/events.js @@ -27,7 +27,8 @@ function eventsRouter({ queue }) { queue.on("libraryUpdate", onLibraryUpdate); const heartbeat = setInterval(() => res.write(": ping\n\n"), HEARTBEAT_INTERVAL); - req.on("close", () => { + // Fermeture de la connexion (onglet fermé, réseau coupé) : on arrête d'écouter la file. + res.on("close", () => { clearInterval(heartbeat); queue.off("queueUpdate", onQueueUpdate); queue.off("libraryUpdate", onLibraryUpdate); diff --git a/test/webServer.test.js b/test/webServer.test.js index 7384be5..d8eb218 100644 --- a/test/webServer.test.js +++ b/test/webServer.test.js @@ -178,6 +178,14 @@ test("the events stream sends the queue state, then every update", async () => { body: JSON.stringify({ enabled: "yes" }), }); assert.equal(invalid.status, 400); + + // Le client se déconnecte : le serveur cesse d'écouter la file. + assert.equal(queue.listenerCount("queueUpdate"), 1); + controller.abort(); + for (let attempt = 0; attempt < 50 && queue.listenerCount("queueUpdate") > 0; attempt++) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + assert.equal(queue.listenerCount("queueUpdate"), 0); } finally { controller.abort(); server.closeAllConnections();