Stop listening when an events client disconnects; warn on partial OAuth setup
All checks were successful
Deploy Palico Bot / check (push) Successful in 10s
Deploy Palico Bot / deploy (push) Successful in 7s

- 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 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 12:23:28 +01:00
parent a269824e2a
commit 8cf0a8eb5b
3 changed files with 17 additions and 1 deletions

View file

@ -49,6 +49,13 @@ const webPublicUrl = process.env.WEB_PUBLIC_URL?.trim().replace(/\/+$/, "") || n
const sessionSecret = process.env.SESSION_SECRET?.trim() || null; const sessionSecret = process.env.SESSION_SECRET?.trim() || null;
const discordOAuthEnabled = Boolean(discordClientSecret && webPublicUrl && sessionSecret); 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) { if (discordOAuthEnabled) {
let publicUrl; let publicUrl;
try { try {

View file

@ -27,7 +27,8 @@ function eventsRouter({ queue }) {
queue.on("libraryUpdate", onLibraryUpdate); queue.on("libraryUpdate", onLibraryUpdate);
const heartbeat = setInterval(() => res.write(": ping\n\n"), HEARTBEAT_INTERVAL); 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); clearInterval(heartbeat);
queue.off("queueUpdate", onQueueUpdate); queue.off("queueUpdate", onQueueUpdate);
queue.off("libraryUpdate", onLibraryUpdate); queue.off("libraryUpdate", onLibraryUpdate);

View file

@ -178,6 +178,14 @@ test("the events stream sends the queue state, then every update", async () => {
body: JSON.stringify({ enabled: "yes" }), body: JSON.stringify({ enabled: "yes" }),
}); });
assert.equal(invalid.status, 400); 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 { } finally {
controller.abort(); controller.abort();
server.closeAllConnections(); server.closeAllConnections();