// =========================================================================== // Cloudflare Worker — hidden "blackbox" backend. // // The real implementations live ONLY here, on Cloudflare's edge. They are // never sent to the browser, so students cannot download or read them. // The frontend only sends inputs and receives outputs (black-box RPC). // // Deploy via the Cloudflare dashboard: Workers & Pages -> Create -> Worker // -> paste this file -> Deploy. Then copy the *.workers.dev URL. // =========================================================================== // ---- Hidden problem implementations (keep behaviour matching the originals) ---- const SOLUTIONS = { Q1: { grade(score) { if (score >= 85) return "HD"; if (score >= 75) return "DN"; if (score >= 65) return "CR"; if (score >= 50) return "PS"; return "FL"; }, clamp(x, lo, hi) { return Math.max(lo, Math.min(x, hi)); }, fizzbuzz(n) { if (n % 15 === 0) return "FizzBuzz"; if (n % 3 === 0) return "Fizz"; if (n % 5 === 0) return "Buzz"; return String(n); }, }, Q2: { is_palindrome(s) { const c = [...String(s).toLowerCase()].filter((ch) => /[a-z0-9]/.test(ch)).join(""); return c === [...c].reverse().join(""); }, count_vowels(s) { return [...String(s).toLowerCase()].filter((ch) => "aeiou".includes(ch)).length; }, reverse_words(s) { const parts = String(s).trim().split(/\s+/).filter(Boolean); return parts.reverse().join(" "); }, }, }; const CORS = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type", }; function json(obj, status = 200) { return new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json", ...CORS }, }); } export default { async fetch(request) { if (request.method === "OPTIONS") return new Response(null, { headers: CORS }); const url = new URL(request.url); const problem = url.searchParams.get("problem"); const func = url.searchParams.get("func"); // Health check / root if (!problem) { return json({ ok: true, problems: Object.keys(SOLUTIONS) }); } const impl = SOLUTIONS[problem]; if (!impl) return json({ error: `unknown problem: ${problem}` }, 404); // Metadata: list the callable function names for a problem (names are NOT secret) if (!func) { return json({ problem, functions: Object.keys(impl) }); } // A black-box call: run the hidden function with the given args const fn = impl[func]; if (typeof fn !== "function") { return json({ error: `unknown function: ${problem}.${func}` }, 404); } let args; try { args = JSON.parse(url.searchParams.get("args") || "[]"); if (!Array.isArray(args)) throw new Error("args must be a JSON array"); } catch (e) { return json({ error: "bad args: " + e.message }, 400); } try { const result = fn(...args); return json({ result }); } catch (e) { return json({ error: String(e && e.message || e) }, 400); } }, };