// SPDX-License-Identifier: AGPL-3.0-only // Author: metamuffin import { Application, Router } from "https://deno.land/x/oak@v10.1.0/mod.ts"; const app = new Application() const router = new Router() type State = "pending" | "accepted" const ticket = new Map void, state: State }>() router.get("/", async c => { const id = c.request.url.searchParams.get("k") if (!id) return c.response.redirect(`/?k=${Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart("FFFFFF".length, "0")}`), console.log(`->\tassigning id`) console.log(`->\tID: ${id}`); const t = ticket.get(id) if (!t) { c.response.headers.set("Refresh", "0") c.response.type = "text/html" c.response.body = ` ` console.log(`\tcreating ticket: ${id}`); ticket.set(id, { set_state: () => { }, state: "pending" }) } else { const ok = await new Promise(r => { if (t.state == "accepted") r(false) const ct = setTimeout(() => { r(true) }, 1000) t.set_state = (s) => { if (s == "accepted") { clearTimeout(ct) r(false) } } }) console.log(`\tresult for ${id}: ${ok}`); c.response.body = `

Funny hack to check if js is supported *without* actually using js

Enabled: ${ok}

re-run test ` } }) router.get("/p", c => { const id = c.request.url.searchParams.get("k") if (!id) return c.response.redirect("/"), console.warn("p: no id"); const t = ticket.get(id) if (!t) return c.response.redirect("/"), console.warn("p: no ticket") console.log(`\tticket accepted: ${id}`); t.set_state("accepted") t.state = "accepted" c.response.status = 403 c.response.body = "" }) app.use(router.routes()) app.listen({ port: 8080, hostname: "127.0.0.1" })