Dough Git

clove/api

src/index.ts

back to src

/* =====================================================================
 * index.ts — Worker entry: v2 router + cron keepalive.
 *
 * Namespaces
 *   /v2/ws            — the realtime socket for presence and devices (SystemState DO).
 *                       Fronting and mental state live on the system site's own
 *                       socket, wss://doughmination.co.uk/api/plural/ws
 *   /v2/discord/*     — Discord users (profile + badges + presence + equipped
 *                       collectibles, one merged record), guilds, girls, status
 *                       (the old /v2/lanyard/* presence routes now live here)
 *   /v2/devices/*     — device state (battery, etc.) (SystemState DO)
 *   /v2/guestbook/*   — public guestbook            (SystemState DO)
 *   /v2/counter/*     — per-site visit counters    (SystemState DO)
 *   /abuse, /terms, /privacy, /.well-known/security.txt — contact & legal pages
 *   /docs, /openapi.json — API reference (HTML + OpenAPI 3.1)
 *   /v2/health        — liveness for uptime monitors
 *
 * Two Durable Objects:
 *   GATEWAY  — singleton holding the Discord gateway socket (presence). Relays
 *              presence updates to SYSTEM for fan-out; no browser sockets.
 *   SYSTEM   — singleton running devices, guestbook, counters + the single /v2/ws hub.
 * ===================================================================== */

import type {
  ApiEnvelope,
  Env,
  UnifiedPresence,
  UnifiedRecord,
  UnifiedGuildInvite,
  UnifiedGirlsRole,
  UnifiedGirlsMember,
  UnifiedMinecraftGeneral,
  VanillaCapeList,
  UnifiedGenshinRoster,
  UnifiedGenshinCharacterDetail,
  UnifiedGenshinCharacterItems,
  UnifiedGenshinCharacterConstellations,
} from "./types";
import { getProfile } from "./profile";
import { GatewayManager } from "./gateway";
import { getGuildInvite } from "./guild";
import { getMinecraftGeneral, getVanillaCapeList, normalizeMcUuid, MojangUpstreamError } from "./minecraft";
import { getContributions } from "./contribapi";
import { purgeAllCache } from "./cloudflare";
import {
  getGenshinRoster,
  getGenshinCharacterDetail,
  getGenshinCharacterItems,
  getGenshinCharacterConstellations,
  EnkaNotFoundError,
} from "./genshin";
import { DOCS_HTML } from "./docs";
import { ABUSE_HTML, ABUSE_CONTACT, securityTxt } from "./abuse";
import { LANDING_HTML, TERMS_HTML, PRIVACY_HTML, NOT_FOUND_HTML } from "./pages";
import { OPENAPI_JSON } from "./openapi";
import { SystemState } from "./system/do";

export { GatewayManager, SystemState };

const CORS = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "GET, OPTIONS",
  "Access-Control-Allow-Headers": "Content-Type",
};

// Security headers on every Worker response. HTML pages additionally get a
// CSP: they're fully self-contained (inline style + script), with the favicon
// served from this Worker's assets, so everything else is locked down.
const SECURITY_HEADERS = {
  "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
  "X-Content-Type-Options": "nosniff",
  "Referrer-Policy": "no-referrer",
};
const HTML_SECURITY_HEADERS = {
  ...SECURITY_HEADERS,
  "Content-Security-Policy":
    "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
  "X-Frame-Options": "DENY",
};

function json<T>(body: ApiEnvelope<T>, status = 200): Response {
  return new Response(JSON.stringify(body), {
    status,
    headers: { "Content-Type": "application/json", "Cache-Control": "no-store", ...CORS, ...SECURITY_HEADERS },
  });
}

function html(body: string, status = 200): Response {
  return new Response(body, {
    status,
    headers: {
      "Content-Type": "text/html; charset=utf-8",
      "Cache-Control": status === 200 ? "public, max-age=3600" : "no-store",
      ...CORS,
      ...HTML_SECURITY_HEADERS,
    },
  });
}

function gatewayStub(env: Env): DurableObjectStub {
  return env.GATEWAY.get(env.GATEWAY.idFromName("gateway"));
}

function systemStub(env: Env): DurableObjectStub {
  return env.SYSTEM.get(env.SYSTEM.idFromName("system"));
}

async function fetchPresence(env: Env, id: string): Promise<UnifiedPresence | null> {
  const res = await gatewayStub(env).fetch(`https://do/presence/${id}`);
  if (!res.ok) return null;
  const body = (await res.json()) as { monitored: boolean; presence: UnifiedPresence | null };
  return body.presence;
}

/** Merge REST profile + gateway presence into the unified record shape the
 *  website's presence cards consume (identical to the old /v1/users/:id). */
function buildRecord(
  profile: NonNullable<Awaited<ReturnType<typeof getProfile>>>,
  presence: UnifiedPresence | null,
): UnifiedRecord {
  return {
    user: profile.user,
    presence,
    badges: profile.badges,
    clientBadges: profile.clientBadges,
    connected_accounts: profile.connected_accounts,
    wishlist: profile.wishlist ?? null,
    collectibles: profile.collectibles ?? null,
    guild_memberships: profile.guild_memberships ?? null,
    pronoundb: profile.pronoundb ?? null,
    timezone: profile.timezone ?? null,
    reviews: profile.reviews ?? null,
    updated_at: Date.now(),
    source: { presence: presence ? "gateway" : "none", profile: profile.source },
  };
}

const ID_RE = /^\d{16,21}$/;

/** Paths owned by the SystemState Durable Object. */
function isSystemPath(path: string): boolean {
  return (
    path === "/v2/ws" ||
    path === "/v2/devices" ||
    path.startsWith("/v2/devices/") ||
    path === "/v2/guestbook" ||
    path.startsWith("/v2/guestbook/") ||
    path === "/v2/counter" ||
    path.startsWith("/v2/counter/")
  );
}

/** decodeURIComponent that returns the raw input instead of throwing on a
 *  malformed %-sequence, so a bad path param becomes a clean 400, not a 500. */
function safeDecode(s: string): string {
  try {
    return decodeURIComponent(s);
  } catch {
    return s;
  }
}

function wantsForce(url: URL): boolean {
  return (
    url.searchParams.has("fresh") ||
    url.searchParams.has("nocache") ||
    url.searchParams.has("refresh")
  );
}

export default {
  async fetch(req: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const url = new URL(req.url);

    // ---- Enforce HTTPS ---------------------------------------------------
    // Anything arriving over plain http is 301-redirected to the https URL
    // before any routing runs. Cloudflare terminates TLS, so the original
    // client scheme is read from the URL, x-forwarded-proto, or cf-visitor.
    if (isInsecure(req, url)) {
      url.protocol = "https:";
      return Response.redirect(url.toString(), 301);
    }

    const path = url.pathname.replace(/\/+$/, "") || "/";

    // ---- SystemState DO (devices / guestbook / counter / realtime) -------
    // Forwarded first, and untouched, so its Hono CORS + the single realtime
    // WebSocket upgrade (/v2/ws) work end-to-end.
    if (isSystemPath(path)) {
      return systemStub(env).fetch(req);
    }

    if (req.method === "OPTIONS") return new Response(null, { headers: CORS });

    // ---- API reference + OpenAPI spec ------------------------------------
    if (path === "/docs") return html(DOCS_HTML);
    if (path === "/openapi.json") {
      return new Response(OPENAPI_JSON, {
        headers: { "Content-Type": "application/json", "Cache-Control": "public, max-age=3600", ...CORS, ...SECURITY_HEADERS },
      });
    }

    // ---- Abuse / terms / privacy / security contact ----------------------
    if (path === "/abuse") return html(ABUSE_HTML);
    if (path === "/terms") return html(TERMS_HTML);
    if (path === "/privacy") return html(PRIVACY_HTML);
    if (path === "/.well-known/security.txt" || path === "/security.txt") {
      return new Response(securityTxt(url.origin), {
        headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=86400", ...CORS, ...SECURITY_HEADERS },
      });
    }

    // ---- /v2/health  (liveness for uptime monitors) ----------------------
    // Gateway DO status + SYSTEM DO reachability in parallel. 200 when both
    // are up and the Discord gateway socket is connected, 503 otherwise.
    if (path === "/v2/health") {
      const [gw, sys] = await Promise.all([
        gatewayStub(env)
          .fetch("https://do/status")
          .then((r) => (r.ok ? (r.json() as Promise<{ connected: boolean; tracked: number; connected_since: number | null }>) : null))
          .catch(() => null),
        systemStub(env)
          .fetch("https://do/v2/health-probe") // any response (even 404) proves the DO is up
          .then(() => true)
          .catch(() => false),
      ]);
      const ok = !!gw?.connected && sys;
      return json(
        {
          success: true,
          data: {
            status: ok ? "ok" : "degraded",
            gateway: gw ?? { connected: false },
            system: sys ? "ok" : "unreachable",
            timestamp: Date.now(),
          },
        } as never,
        ok ? 200 : 503,
      );
    }

    // ---- /v2/burst  (nuke ALL Cloudflare cache, everywhere) --------------
    // Lists every zone the CF_API_TOKEN can see (all accounts, including the
    // one that owns this Worker) and purges everything on each. Returns
    // {code:"success"} when every zone purged, else {code:"<error>"}.
    // WARNING: unauthenticated — anyone who loads this URL purges all cache.
    if (path === "/v2/burst") {
      let code: string;
      let ok = false;
      try {
        const result = await purgeAllCache(env);
        ok = result.ok;
        code = result.ok ? "success" : (result.error ?? "unknown error");
      } catch (err) {
        code = (err as Error).message || "unknown error";
      }
      return new Response(JSON.stringify({ code }), {
        status: ok ? 200 : 500,
        headers: { "Content-Type": "application/json", "Cache-Control": "no-store", ...CORS, ...SECURITY_HEADERS },
      });
    }

    // ---- Gateway status (debug) ------------------------------------------
    if (path === "/v2/discord/status") {
      const res = await gatewayStub(env).fetch("https://do/status");
      const body = await res.json();
      return json({ success: true, data: body as never });
    }

    // ---- Service info ----------------------------------------------------
    if (path === "/") {
      // Browsers and link-preview crawlers get the HTML landing card; API
      // clients keep the exact JSON they got before. Crawlers don't always
      // send Accept: text/html, so sniff the common bot user-agents too.
      const accept = req.headers.get("accept") || "";
      const userAgent = req.headers.get("user-agent") || "";
      const wantsHtml =
        accept.includes("text/html") ||
        /bot|crawler|embed|discord|twitter|slack|facebookexternalhit|whatsapp|telegram|preview/i.test(
          userAgent,
        );
      if (wantsHtml) return html(LANDING_HTML);

      return json({
        success: true,
        data: {
          service: "Doughmination API",
          description: "Universal API: live Discord presence + profiles + Genshin roster tracking.",
          licence: "DASL-1.0",
          repository_url: "https://github.com/doughmination/api",
          docs: "/docs",
          openapi: "/openapi.json",
          health: "/v2/health",
          abuse: "/abuse",
          abuse_contact: ABUSE_CONTACT,
          terms: "/terms",
          privacy: "/privacy",
          security_txt: "/.well-known/security.txt",
        },
      } as never);
    }

    // ---- /v2/contribapi  (merged git contribution heatmaps) --------------
    // Stateless: fetch + merge each configured forge's heatmap. Cached at the
    // edge for an hour since contribution graphs move at most daily.
    if (path === "/v2/contribapi") {
      const data = await getContributions(env);
      return new Response(JSON.stringify({ success: true, data }), {
        status: 200,
        headers: {
          "Content-Type": "application/json",
          "Cache-Control": "public, max-age=3600",
          ...CORS,
        },
      });
    }

    // ---- /v2/discord/guilds/:invite --------------------------------------
    const gm = path.match(/^\/v2\/discord\/guilds\/([\w-]+)$/);
    if (gm) {
      const invite = await getGuildInvite(env, gm[1], ctx, wantsForce(url));
      if (!invite) {
        return json(
          { success: false, error: { code: "not_found", message: "Invalid or expired invite." } },
          404,
        );
      }
      return json<UnifiedGuildInvite>({ success: true, data: invite });
    }
    // ---- /v2/minecraft/capes  (accumulated vanilla cape catalogue) -------
    // Grows as accounts are looked up via /general — every vanilla Mojang cape
    // seen is catalogued once, keyed by texture hash.
    if (path === "/v2/minecraft/capes") {
      const data = await getVanillaCapeList(env);
      return json<VanillaCapeList>({ success: true, data });
    }

    // ---- /v2/minecraft/general/:uuid  (Mojang profile + skin) ------------
    // Mojang identity + skin/cape textures + aggregated cape providers.
    const mcg = path.match(/^\/v2\/minecraft\/general\/(.+)$/);
    if (mcg) {
      const short = normalizeMcUuid(safeDecode(mcg[1]));
      if (!short) {
        return json(
          { success: false, error: { code: "invalid_uuid", message: "Not a Minecraft UUID (dashed, undashed, or NBT int-array form)." } },
          400,
        );
      }
      try {
        const data = await getMinecraftGeneral(env, short, ctx, wantsForce(url));
        if (!data) {
          return json({ success: false, error: { code: "not_found", message: "No Minecraft account with that UUID." } }, 404);
        }
        return json<UnifiedMinecraftGeneral>({ success: true, data });
      } catch (err) {
        if (err instanceof MojangUpstreamError) {
          return json(
            { success: false, error: { code: "upstream_error", message: err.message } },
            502,
          );
        }
        throw err;
      }
    }

    // ---- /v2/genshin/roster/:uid  (owned/not-owned + level, via Enka.Network) ----
    // Stateless passthrough + merge: Enka's live UID data joined against the
    // (separately, long-) cached character catalog. See ./genshin.ts for the
    // `partial` caveat (player must enable "Display all your characters").
    const gs = path.match(/^\/v2\/genshin\/roster\/(\d{1,12})$/);
    if (gs) {
      const uid = gs[1];
      try {
        const data = await getGenshinRoster(env, uid, ctx, wantsForce(url));
        return json<UnifiedGenshinRoster>({ success: true, data });
      } catch (err) {
        if (err instanceof EnkaNotFoundError) {
          return json({ success: false, error: { code: "not_found", message: err.message } }, 404);
        }
        return json(
          { success: false, error: { code: "upstream_error", message: (err as Error).message } },
          502,
        );
      }
    }

    // ---- /v2/genshin/roster/:uid/:heroId  (single-character detail) ------
    // Full detail for one character: level, constellation, friendship,
    // weapon, artifacts. Same cached Enka fetch as the roster route above —
    // this never costs an extra upstream call within the ttl window.
    const gsItems = path.match(/^\/v2\/genshin\/roster\/(\d{1,12})\/(\d{1,10})\/items$/);
    const gsCons = path.match(/^\/v2\/genshin\/roster\/(\d{1,12})\/(\d{1,10})\/constellations$/);
    const gsHero = path.match(/^\/v2\/genshin\/roster\/(\d{1,12})\/(\d{1,10})$/);
    if (gsItems || gsCons || gsHero) {
      const [, uid, heroId] = (gsItems ?? gsCons ?? gsHero)!;
      try {
        if (gsItems) {
          const data = await getGenshinCharacterItems(env, uid, heroId, ctx, wantsForce(url));
          if (!data) {
            return json(
              { success: false, error: { code: "not_found", message: `No playable character with id ${heroId}` } },
              404,
            );
          }
          return json<UnifiedGenshinCharacterItems>({ success: true, data });
        }
        if (gsCons) {
          const data = await getGenshinCharacterConstellations(env, uid, heroId, ctx, wantsForce(url));
          if (!data) {
            return json(
              { success: false, error: { code: "not_found", message: `No playable character with id ${heroId}` } },
              404,
            );
          }
          return json<UnifiedGenshinCharacterConstellations>({ success: true, data });
        }
        const data = await getGenshinCharacterDetail(env, uid, heroId, ctx, wantsForce(url));
        if (!data) {
          return json(
            { success: false, error: { code: "not_found", message: `No playable character with id ${heroId}` } },
            404,
          );
        }
        return json<UnifiedGenshinCharacterDetail>({ success: true, data });
      } catch (err) {
        if (err instanceof EnkaNotFoundError) {
          return json({ success: false, error: { code: "not_found", message: err.message } }, 404);
        }
        return json(
          { success: false, error: { code: "upstream_error", message: (err as Error).message } },
          502,
        );
      }
    }

    // ---- /v2/discord/users/:id  (merged profile + presence) --------------
    // Profile (REST) + presence (gateway) in parallel — the full user record
    // the website's presence cards render from.
    const dm = path.match(/^\/v2\/discord\/users\/(\d{1,32})$/);
    if (dm) {
      const id = dm[1];
      if (!ID_RE.test(id)) {
        return json({ success: false, error: { code: "invalid_id", message: "Not a Discord snowflake." } }, 400);
      }
      const [profile, presence] = await Promise.all([
        getProfile(env, id, ctx, wantsForce(url)),
        fetchPresence(env, id),
      ]);
      if (!profile) {
        return json({ success: false, error: { code: "not_found", message: "User not found." } }, 404);
      }
      return json<UnifiedRecord>({ success: true, data: buildRecord(profile, presence) });
    }

    // ---- 404 --------------------------------------------------------------
    // Browsers (Accept: text/html) get the friendly page; API clients keep
    // the JSON error envelope.
    if ((req.headers.get("accept") || "").includes("text/html")) {
      return html(NOT_FOUND_HTML, 404);
    }
    return json({ success: false, error: { code: "not_found", message: "Unknown route. See /docs." } }, 404);
  },

  // Cron keepalive — keep the gateway DO connected.
  async scheduled(_event: ScheduledController, env: Env, _ctx: ExecutionContext): Promise<void> {
    await gatewayStub(env).fetch("https://do/connect");
  },
};

/** True when the request reached us over plain http (so we should redirect to
 *  https). Cloudflare terminates TLS, so we check the URL scheme plus the
 *  proxy headers that carry the original client scheme. */
function isInsecure(req: Request, url: URL): boolean {
  // Local dev (wrangler / bun dev) has no TLS — never redirect localhost.
  if (url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]") return false;
  if (url.protocol === "http:") return true;
  const xfProto = req.headers.get("x-forwarded-proto");
  if (xfProto && xfProto.split(",")[0].trim().toLowerCase() === "http") return true;
  const visitor = req.headers.get("cf-visitor");
  if (visitor && /"scheme"\s*:\s*"http"/i.test(visitor)) return true;
  return false;
}