1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| export default { async fetch(request, env, ctx) { const TMDB_KEY = env.TMDB_KEY;
if (!TMDB_KEY) { return new Response(JSON.stringify({ error: "Configuration Error", message: "TMDB_KEY is not configured. Please set it in Worker environment variables.", docs: "https://developers.cloudflare.com/workers/configuration/secrets/" }), { status: 500, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } }); }
if (request.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", "Access-Control-Allow-Headers": "*", "Access-Control-Max-Age": "86400", }, }); }
const url = new URL(request.url); const isImage = url.pathname.startsWith("/t/p/"); const targetHost = isImage ? "image.tmdb.org" : "api.themoviedb.org";
const targetUrl = new URL(request.url); targetUrl.hostname = targetHost; targetUrl.protocol = "https:";
if (!isImage && !targetUrl.searchParams.has("api_key")) { targetUrl.searchParams.set("api_key", TMDB_KEY); }
const headers = new Headers(request.headers); headers.set("User-Agent", "Cloudflare-TMDB-Proxy/1.0"); headers.set("Accept-Encoding", "identity");
const newRequest = new Request(targetUrl.toString(), { method: request.method, headers, body: request.method !== "GET" && request.method !== "HEAD" ? request.clone().body : null, redirect: "follow", });
async function fetchWithRetry(req, retries = 2) { try { const response = await fetch(req); if ((response.status >= 500 || response.status === 429) && retries > 0) { await new Promise(resolve => setTimeout(resolve, 1000)); return await fetchWithRetry(req, retries - 1); } return response; } catch (e) { if (retries > 0) { await new Promise(resolve => setTimeout(resolve, 1000)); return await fetchWithRetry(req, retries - 1); } return new Response("TMDB Proxy Error: " + e.message, { status: 502 }); } }
if (isImage) { const cacheUrl = new URL(request.url); const cacheKey = new Request(cacheUrl.toString(), { method: "GET", headers: new Headers({ "Accept": "image/*" }) });
let cached = await caches.default.match(cacheKey); if (cached) { const headers = new Headers(cached.headers); headers.set("X-Cache", "HIT"); headers.set("X-Cache-Source", "Cloudflare-Workers"); return new Response(cached.body, { status: cached.status, headers: headers }); }
const resp = await fetchWithRetry(newRequest);
if (!resp.ok) { const errorHeaders = new Headers(resp.headers); errorHeaders.set("Access-Control-Allow-Origin", "*"); return new Response(resp.body, { status: resp.status, headers: errorHeaders }); }
const respBuffer = await resp.arrayBuffer(); const contentType = resp.headers.get("Content-Type") || "image/jpeg";
const respHeaders = new Headers({ "Content-Type": contentType, "Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=604800, immutable", "X-Content-Type-Options": "nosniff", "X-Cache": "MISS", "X-Cache-Source": "TMDB-Origin", });
const finalResp = new Response(respBuffer, { status: resp.status, headers: respHeaders });
ctx.waitUntil(caches.default.put(cacheKey, finalResp.clone()));
return finalResp; }
const apiResp = await fetchWithRetry(newRequest);
const apiHeaders = new Headers(apiResp.headers); apiHeaders.set("Access-Control-Allow-Origin", "*"); apiHeaders.set("Access-Control-Allow-Headers", "*"); apiHeaders.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); apiHeaders.set("X-Cache", "BYPASS"); apiHeaders.set("X-Cache-Source", "TMDB-API"); if (apiResp.ok) { apiHeaders.set("Cache-Control", "public, max-age=3600"); } else { apiHeaders.set("Cache-Control", "no-cache"); }
return new Response(apiResp.body, { status: apiResp.status, headers: apiHeaders }); }, };
|