From 29936e8365b39c0f7d12be1e1db59007580e5835 Mon Sep 17 00:00:00 2001 From: Omar Najjar Date: Wed, 30 Apr 2025 19:45:42 +1000 Subject: [PATCH] Revert "x" This reverts commit 9e6a2ba4fc358a2001e80deaeeed9a65ddf8d590. --- worker.js | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/worker.js b/worker.js index e473843..9e08e8c 100644 --- a/worker.js +++ b/worker.js @@ -31,12 +31,16 @@ function handleOptions(request) { // Helper to create standardized responses function createResponse(body, status = 200, cacheDuration = 60*60*24) { - // Convert body to string if it's an object - const bodyString = typeof body === 'object' ? JSON.stringify(body) : String(body); + // Don't allow null or undefined body + if (body === null || body === undefined) { + console.error('createResponse called with null or undefined body'); + body = { error: 'Empty response body' }; + status = 500; + } const headers = { 'Content-Type': 'application/json', - ...corsHeaders + ...corsHeaders // Add CORS headers }; if (cacheDuration > 0) { @@ -46,7 +50,12 @@ function createResponse(body, status = 200, cacheDuration = 60*60*24) { headers['Cache-Control'] = 'no-store'; } - return new Response(bodyString, { + // Log the response details for debugging + console.log(`Creating response: status=${status}, headers=${JSON.stringify(headers)}, body type=${typeof body}`); + + // Create a standard Response object + // This ensures properties like 'ok' are properly set based on status code + return new Response(JSON.stringify(body), { status, headers }); @@ -82,22 +91,17 @@ function getCacheControl(path) { export default { async fetch(request, env, ctx) { try { - // Always handle OPTIONS requests first for CORS - if (request.method === 'OPTIONS') { - return handleOptions(request); - } - const url = new URL(request.url); const path = url.pathname; - // Add proper CORS headers to all responses - const responseHeaders = new Headers({ - ...corsHeaders - }); console.log(`Received request for path: ${path}`); console.log(`Full URL: ${url.toString()}`); - + // Handle CORS preflight requests + if (request.method === 'OPTIONS') { + return handleOptions(); + } + // Set cache control headers based on path const cacheControl = getCacheControl(path); const headers = new Headers(); @@ -162,22 +166,14 @@ export default { // Return 404 for unknown routes return new Response('Not Found', { status: 404 }); - // Make sure error responses also include CORS headers - ctx.passThroughOnException(); - } catch (error) { console.error('Unhandled error:', error); - return new Response(JSON.stringify({ error: 'Internal Server Error' }), { - status: 500, - headers: { - 'Content-Type': 'application/json', - ...corsHeaders - } - }); + return new Response('Internal Server Error', { status: 500 }); } } }; +// ... existing code ... // Handle media files (memes and audio) async function serveMedia(request, env) { @@ -272,6 +268,7 @@ async function serveStaticAsset(request, env) { } } +// ... existing code ... async function serveMainPage(request, env) { try {