Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

3.0 KB
Raw Blame
// Release news sign-up.
//
// POST /api/notify {"email", "source"} keeps the address in the AUTH KV namespace under "n:<email>"
// (no expiry).

const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json", "cache-control": "no-store" } });
let minute = 0, notifyCount = 0;
const tick = () => { const m = Math.floor(Date.now() / 60000); if (m !== minute) { minute = m; notifyCount = 0; } };

export async function notify(request, env) {
  if (request.method !== "POST") return json({ error: "Use POST." }, 405);
  if (!env.AUTH) return json({ error: "Not available right now." }, 503);
  const site = request.headers.get("sec-fetch-site");
  if (site && site !== "same-origin") return json({ error: "Forbidden." }, 403);
  let input;
  const type = request.headers.get("content-type") || "";
  try { input = type.includes("json") ? await request.json() : Object.fromEntries(await request.formData()); } catch { return json({ error: "Send the form." }, 400); }
  if (input.website) return json({ ok: true }); // honeypot
  const email = String(input.email || "").trim().toLowerCase().slice(0, 200);
  if (!/^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]{2,}$/.test(email)) return json({ error: "That email doesn't look right." }, 400);
  tick();
  if (++notifyCount > 20) return json({ error: "Too many sign-ups right now. Try again in a minute." }, 429);
  const source = String(input.source || "site").replace(/[^a-z0-9-]/g, "").slice(0, 20) || "site";
  // KV writes share a daily quota with sign-in, so a repeated address is not written again.
  if (await env.AUTH.get("n:" + email)) return json({ ok: true });
  await env.AUTH.put("n:" + email, JSON.stringify({ at: new Date().toISOString(), source }));
  return json({ ok: true });
}

// POST /api/wishlist {"email", "game"}: someone wants to hear when a game that isn't on sale yet comes out.
// Kept in the AUTH KV namespace under "w:<game>:<email>"; one email per game at launch, nothing else.
export async function wishlist(request, env) {
  if (request.method !== "POST") return json({ error: "Use POST." }, 405);
  if (!env.AUTH) return json({ error: "Not available right now." }, 503);
  const site = request.headers.get("sec-fetch-site");
  if (site && site !== "same-origin") return json({ error: "Forbidden." }, 403);
  let input;
  try { input = await request.json(); } catch { return json({ error: "Send JSON." }, 400); }
  const email = String(input.email || "").trim().toLowerCase().slice(0, 200);
  if (!/^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]{2,}$/.test(email)) return json({ error: "That email doesn't look right." }, 400);
  const game = String(input.game || "");
  if (!/^[a-z0-9][a-z0-9-]{0,63}$/.test(game)) return json({ error: "Unknown game." }, 400);
  tick();
  if (++notifyCount > 20) return json({ error: "Too many sign-ups right now. Try again in a minute." }, 429);
  const key = "w:" + game + ":" + email;
  if (await env.AUTH.get(key)) return json({ ok: true });
  await env.AUTH.put(key, JSON.stringify({ at: new Date().toISOString() }));
  return json({ ok: true });
}