Dough Git

commit

capes, capes and more capes

commit
2ff6cf688e298fd9fb92421946066bbbdf4dcd5c
author
Clove Twilight <[email protected]>
date
2026-07-13 01:35

diff --git a/assets/capes/1bd448116a6e4912ab6772849bc89018.png b/assets/capes/79ef438d69ea473c99cd6a5ec34c6736.png
similarity index 100%
rename from assets/capes/1bd448116a6e4912ab6772849bc89018.png
rename to assets/capes/79ef438d69ea473c99cd6a5ec34c6736.png
diff --git a/assets/capes/d20b556ae2cc452dab726ae082d439af.png b/assets/capes/d20b556ae2cc452dab726ae082d439af.png
new file mode 100644
index 0000000..e40d2d8
Binary files /dev/null and b/assets/capes/d20b556ae2cc452dab726ae082d439af.png differ
diff --git a/src/docs.ts b/src/docs.ts
index 0fddcd1..46d1b82 100644
--- a/src/docs.ts
+++ b/src/docs.ts
@@ -260,9 +260,9 @@ var GROUPS = [
         params: [["uuid", "Minecraft UUID — 32 hex characters, dashes optional."], ["?fresh / ?nocache / ?refresh", "Skip the cache and fetch fresh."]],
         example: 'GET /minecraft/general/853c80ef3c3749fdaa49938b674adae6  →  { name, uuid, skin_url, cape_url, skin_model, capes: [ { source, cape_url } ], render: { face, head, body, player, combo, skin } }' },
       { m: "GET", path: "/minecraft/capes", auth: "public",
-        desc: "The accumulated catalogue of vanilla (Mojang) capes the API has seen. It grows over time: every account looked up via /minecraft/general contributes its equipped vanilla cape once, keyed by texture hash. Handy for building up a list of all vanilla capes in circulation.",
+        desc: "The set of vanilla (Mojang) capes the API has seen, persisted to memory. It grows over time: every account looked up via /minecraft/general has its equipped vanilla cape remembered, deduped by texture hash. Third-party capes (OptiFine, LabyMod, etc.) are loaded fresh per request and are not persisted here.",
         params: [],
-        example: 'GET /minecraft/capes  →  { count, capes: [ { hash, url, first_seen } ] }' },
+        example: 'GET /minecraft/capes  →  { count, capes: [ { source: "minecraft", cape_url } ] }' },
       { m: "GET", path: "/minecraft/hypixel/:uuid", auth: "public",
         desc: "A player's Hypixel stats: the full player object (network level and every game's stats — Bedwars, SkyWars, Duels, and the rest) plus their SkyBlock profiles. Always returns 200; the 'source' field tells you whether each section loaded ('ok') or why it's null — 'not_found' if the player has never joined Hypixel, 'unavailable' if this server isn't serving Hypixel data.",
         params: [["uuid", "Minecraft UUID — 32 hex characters, dashes optional."], ["?fresh / ?nocache / ?refresh", "Skip the cache and fetch fresh."]],
diff --git a/src/minecraft.ts b/src/minecraft.ts
index d0205f9..e8408d1 100644
--- a/src/minecraft.ts
+++ b/src/minecraft.ts
@@ -136,7 +136,7 @@ export function isMinecraftUuid(uuid: string): boolean {
 
 const generalKey = (short: string) => `minecraft:general:${short}`;
 const hypixelKey = (short: string) => `minecraft:hypixel:${short}`;
-/** KV key holding the accumulated registry of vanilla cape textures we've seen. */
+/** KV key holding the persistent memory of vanilla cape textures we've seen. */
 const VANILLA_CAPES_KEY = "minecraft:capes:vanilla";
 
 interface MojangTexturePayload {
@@ -156,7 +156,10 @@ interface MojangProfileResponse {
 interface CapesDevEntry {
   type: string;
   exists: boolean;
+  /** capes.dev /get/<hash> — returns JSON metadata, NOT an image. */
   capeUrl?: string | null;
+  /** capes.dev /img/<hash> — the rendered cape PNG. This is what we expose. */
+  imageUrl?: string | null;
 }
 
 const DOUGH_BASE_URL = "https://doughmination.uk";
@@ -183,40 +186,46 @@ async function fetchDoughminationCape(env: Env, short: string): Promise<UnifiedC
 }
 
 /**
- * List every cape a player has via capes.dev, which aggregates all the cape
- * providers (Minecraft, OptiFine, MinecraftCapes, LabyMod, 5zig, TLauncher,
- * SkinMC). Returns only the providers where a cape actually exists. Best-effort:
- * any failure yields [] so the general endpoint still succeeds without capes.
+ * Load every cape a player has straight from capes.dev's /load endpoint, which
+ * re-checks all providers (Minecraft, OptiFine, MinecraftCapes, LabyMod, 5zig,
+ * TLauncher, SkinMC) fresh. Returns only providers where a cape exists.
+ *
+ * Returns null (not []) when capes.dev itself couldn't be reached or parsed, so
+ * the caller can tell "definitely no capes" ([]) apart from "load failed" (null)
+ * and avoid caching a transient failure.
  */
-async function fetchCapes(short: string): Promise<UnifiedCape[]> {
+async function fetchCapes(short: string): Promise<UnifiedCape[] | null> {
   try {
     const res = await fetch(`${CAPES_API}/${short}`, {
       headers: { Accept: "application/json", "User-Agent": USER_AGENT },
     });
-    if (!res.ok) return [];
+    if (!res.ok) return null;
     const body = (await res.json()) as Record<string, CapesDevEntry>;
     return Object.values(body)
       .filter((c) => c && c.exists)
       .map((c) => ({
         source: c.type,
-        cape_url: c.capeUrl ?? null,
+        // Expose the rendered PNG (/img), not /get which returns JSON.
+        cape_url: c.imageUrl ?? null,
       }));
   } catch {
-    return [];
+    return null;
   }
 }
 
+
 /** Pull the stable texture hash out of a textures.minecraft.net cape URL. */
 function capeTextureHash(url: string | null): string | null {
   return url?.match(/\/texture\/([0-9a-f]+)/i)?.[1]?.toLowerCase() ?? null;
 }
 
 /**
- * Add a vanilla (Mojang) cape to the shared registry the first time we see it,
- * so /v2/minecraft/capes builds up a catalogue of every vanilla cape that has
- * passed through the API. Keyed by texture hash. Best-effort and fire-and-forget:
- * it reads-modifies-writes a single KV entry, so under heavy concurrency an
- * occasional new cape could be missed — acceptable for a slowly-growing list.
+ * Persist a vanilla (Mojang) cape to memory the first time we see it, keyed by
+ * texture hash so we build up the set of vanilla capes in circulation. Unlike the
+ * third-party providers (loaded fresh each request), vanilla capes are stored
+ * permanently (no TTL). Best-effort and fire-and-forget: it reads-modifies-writes
+ * one KV entry, so under heavy concurrency an occasional new cape could be missed
+ * — acceptable for a slowly-growing list.
  */
 function recordVanillaCape(env: Env, capeUrl: string | null, ctx?: ExecutionContext): void {
   const hash = capeTextureHash(capeUrl);
@@ -225,8 +234,8 @@ function recordVanillaCape(env: Env, capeUrl: string | null, ctx?: ExecutionCont
     try {
       const reg =
         ((await env.PROFILE_CACHE.get(VANILLA_CAPES_KEY, "json")) as VanillaCapeRegistry | null) ?? {};
-      if (reg[hash]) return; // already catalogued
-      reg[hash] = { hash, url: capeUrl, first_seen: Date.now() };
+      if (reg[hash]) return; // already remembered
+      reg[hash] = { source: "minecraft", cape_url: capeUrl };
       await env.PROFILE_CACHE.put(VANILLA_CAPES_KEY, JSON.stringify(reg));
     } catch {
       /* registry write failed — non-fatal */
@@ -235,10 +244,10 @@ function recordVanillaCape(env: Env, capeUrl: string | null, ctx?: ExecutionCont
   if (ctx) ctx.waitUntil(work);
 }
 
-/** The accumulated vanilla-cape catalogue, newest first. */
+/** The persisted set of vanilla capes we've seen, as { source, cape_url }. */
 export async function getVanillaCapeList(env: Env): Promise<VanillaCapeList> {
   const reg = ((await env.PROFILE_CACHE.get(VANILLA_CAPES_KEY, "json")) as VanillaCapeRegistry | null) ?? {};
-  const capes = Object.values(reg).sort((a, b) => b.first_seen - a.first_seen);
+  const capes = Object.values(reg);
   return { count: capes.length, capes };
 }
 
@@ -294,12 +303,15 @@ export async function getMinecraftGeneral(
   // independent upstreams, so fire them together. fetchMojangProfile throws
   // MojangUpstreamError if every source is blocked/errored, and returns null
   // only on a definitive not-found.
-  const [data, providerCapes, doughCape] = await Promise.all([
+  // OptiFine/LabyMod/etc. capes are loaded fresh every time (bounded only by the
+  // 5-min /general cache). Null means capes.dev failed — degrade to no capes.
+  const [data, providerCapesResult, doughCape] = await Promise.all([
     fetchMojangProfile(short),
     fetchCapes(short),
     fetchDoughminationCape(env, short),
   ]);
   if (!data) return null;
+  const providerCapes = providerCapesResult ?? [];
 
   name = data.name ?? null;
   const texturesB64 = data.properties?.find((p) => p.name === "textures")?.value;
@@ -314,8 +326,8 @@ export async function getMinecraftGeneral(
     }
   }
 
-  // Every vanilla cape we see contributes to the shared registry, so the list at
-  // /v2/minecraft/capes fills in over time. Best-effort, off the response path.
+  // Persist the equipped vanilla (Mojang) cape to memory so /v2/minecraft/capes
+  // builds up the set of vanilla capes seen. Best-effort, off the response path.
   recordVanillaCape(env, cape_url, ctx);
 
   const capes = doughCape ? [...providerCapes, doughCape] : providerCapes;
diff --git a/src/types.ts b/src/types.ts
index 0f76fc4..13b6f3d 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -436,20 +436,19 @@ export interface UnifiedCape {
   cape_url: string | null;
 }
 
-/** One vanilla (Mojang) cape in the accumulated registry. */
+/** One vanilla (Mojang) cape we've persisted to memory. Same shape as an entry
+ *  in a player's `capes` array: just the provider and the cape image URL. */
 export interface VanillaCapeEntry {
-  /** textures.minecraft.net texture hash — the stable id for the cape. */
-  hash: string;
-  /** Full cape texture URL. */
-  url: string;
-  /** Epoch ms when this cape was first catalogued by the API. */
-  first_seen: number;
+  /** Always "minecraft" — these are vanilla Mojang capes. */
+  source: string;
+  /** Cape texture URL. */
+  cape_url: string;
 }
 
 /** Registry blob stored in KV: texture hash -> entry. */
 export type VanillaCapeRegistry = Record<string, VanillaCapeEntry>;
 
-/** Response for GET /v2/minecraft/capes — the built-up vanilla cape catalogue. */
+/** Response for GET /v2/minecraft/capes — the persisted vanilla capes. */
 export interface VanillaCapeList {
   count: number;
   capes: VanillaCapeEntry[];