Skip to content
This repository was archived by the owner on Jul 4, 2026. It is now read-only.

Commit 7c3932a

Browse files
committed
fix: resolve server 500 errors by downgrading Express v5 and making Supabase optional
- Downgrade Express from 5.2.1 to 4.21.0 to fix middleware incompatibility - Make Supabase connection non-fatal so server starts without it - Add 5s timeout to Supabase requests to prevent hanging - Fix download route filename validation to accept _dkutils_ pattern Closes #28
1 parent dc2dcf0 commit 7c3932a

5 files changed

Lines changed: 174 additions & 140 deletions

File tree

backend/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SER
2626

2727
/**
2828
* Tests the connection to Supabase Storage by attempting to get the specified bucket.
29-
* Logs a success message if the bucket is found, or an error message if the connection fails.
30-
* @throws {Error} If the connection fails
29+
* Logs a success message if the bucket is found, or a warning if the connection fails.
30+
* Does NOT throw - Supabase is optional for server startup.
31+
* @returns {Promise<boolean>} true if connected, false otherwise
3132
*/
3233
const testSupabaseConnection = async () => {
3334
try {
3435
const { data: bucket, error: getBucketError } = await supabase.storage.getBucket("utilityhub");
3536
if (getBucketError) throw getBucketError;
3637
console.log(`Supabase Storage connected!\nBucket '${bucket.name}' found.`);
38+
return true;
3739
} catch (error) {
38-
console.error("Supabase Storage connection failed:", error.message);
39-
throw error;
40+
console.warn("⚠️ Supabase Storage unavailable (non-fatal):", error.message);
41+
console.warn("File storage features will not work until Supabase is reachable.");
42+
return false;
4043
}
4144
};
4245

@@ -151,7 +154,7 @@ app.use((err, req, res, next) => {
151154

152155
/**
153156
* Starts the Express.js server and connects to MongoDB and Supabase.
154-
* @throws {Error} If there is an error connecting to MongoDB or Supabase.
157+
* Supabase connection failure is non-fatal - server will start without it.
155158
*/
156159
const startServer = async () => {
157160
try {

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"cors": "^2.8.6",
4444
"docx": "catalog:",
4545
"dotenv": "^17.2.3",
46-
"express": "^5.2.1",
46+
"express": "^4.21.0",
4747
"express-rate-limit": "^8.2.1",
4848
"express-validator": "^7.3.1",
4949
"image-type": "^5.2.0",

backend/routes/imageConverter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ router.get("/download", async (req, res) => {
142142
"merged_dkutils_",
143143
];
144144

145-
const isAllowed = allowedPrefixes.some((prefix) => filename.startsWith(prefix));
145+
const isAllowed =
146+
allowedPrefixes.some((prefix) => filename.startsWith(prefix)) ||
147+
filename.includes("_dkutils_");
146148
if (!isAllowed || filename.includes("..")) {
147149
return res.status(403).json({ msg: "Access denied." });
148150
}

backend/utils/supabaseClient.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ if (!process.env.SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) {
88
throw new Error("Missing required Supabase environment variables");
99
}
1010

11-
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY);
11+
const SUPABASE_TIMEOUT_MS = 5000;
12+
13+
const fetchWithTimeout = async (url, options = {}) => {
14+
const controller = new AbortController();
15+
const timeoutId = setTimeout(() => controller.abort(), SUPABASE_TIMEOUT_MS);
16+
try {
17+
return await fetch(url, { ...options, signal: controller.signal });
18+
} finally {
19+
clearTimeout(timeoutId);
20+
}
21+
};
22+
23+
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY, {
24+
global: { fetch: fetchWithTimeout },
25+
});
1226

1327
module.exports = { supabase };

0 commit comments

Comments
 (0)