Every write path normalises a bundle's texture to WebP Lossless, so the catalog icon was being read back with a PNG-only decoder. decodePng throws on anything that is not a PNG, the callers caught that and returned null, and the user-visible result was "no icon (not in source or bundle)" for every furniture whose source does not serve a standalone icon. Measured against the production asset tree, all 18,505 bundles were WebP; icon extraction succeeded on 0 of them. src/lib/services/imager/ decode-texture.ts keeps PNG on the dependency-free decoder and routes WebP through sharp, which is already a dependency and already encodes these textures. extractFurniIconPng and getPetIconPng become async; the five call sites (upload, clone import, furni import, icon repair and both icon routes) already awaited their surrounding work. Same root cause, second bug: the spritesheet frame key. Converters disagree on packing — some keep a trailing ".png", and some lowercase the whole key while leaving the bundle name mixed-case, so "LTD_fashionistaf" looks up frame "LTD_fashionistaf_LTD_fashionistaf_icon_a" and never finds "ltd_fashionistaf_ltd_fashionistaf_icon_a". Any mixed-case classname could therefore never match, which is most of the catalogue. findFrame tries the two exact spellings, then falls back to one case-insensitive pass. Extraction now succeeds on 18,483 of 18,505 bundles (99.88%); the 22 remainder are data, not code — 9 bundles ship no icon asset, 11 do not parse. Third: three catalogue icons exist only as .gif while catalogueIconUrl hardcoded .png, so the picker offered icons that could only ever 404, and 291 icons that ship as both formats were listed twice. The API now dedupes per id and the two renderers retry with .gif before falling back to the placeholder, matching what catalog-image-picker already did. Verified live: /gamedata/.../icon_1542.png returns 404 while icon_1542.gif returns 200. Separately, close the last hole in the memory cap. Every script in package.json routes through scripts/with-memory-cap.sh, but invoking the builder directly — from a terminal, an IDE or an agent — skipped the wrapper and ran unbounded, on a host with no swap where the OOM killer picks its victim across the whole machine. next.config.ts now refuses a production build that the wrapper has not marked, before anything allocates. next dev and next start are deliberately unaffected. README gains a Memory-capped commands section covering the per-script ceilings, the backends and the ulimit -v trap, and its stale version and script tables are corrected.
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { apiError } from "@/lib/api";
|
|
import { withAdmin } from "@/lib/api-handler";
|
|
import { PERMS } from "@/lib/permissions";
|
|
import { extractFurniIconPng } from "@/lib/services/clone-icon";
|
|
import { getSource } from "@/lib/services/clone-sources";
|
|
|
|
// In-process cache of extracted icons, keyed by source+classname.
|
|
// null = known-missing (don't refetch the bundle every render).
|
|
const iconCache = new Map<string, Buffer | null>();
|
|
const MAX_ICON_CACHE_SIZE = 500;
|
|
|
|
function pruneIconCache() {
|
|
while (iconCache.size > MAX_ICON_CACHE_SIZE) {
|
|
const oldest = iconCache.keys().next().value;
|
|
if (oldest === undefined) break;
|
|
iconCache.delete(oldest);
|
|
}
|
|
}
|
|
|
|
// GET ?source=<id>&classname=<cn>
|
|
// Fetches the source's .nitro bundle and returns the embedded furni icon as a
|
|
// PNG. Used by the clone preview grid for sources that don't serve standalone
|
|
// {classname}_icon.png files (icons live inside the .nitro).
|
|
export const GET = withAdmin(
|
|
{ permission: PERMS.ASSETS_IMPORT },
|
|
async (request) => {
|
|
const sp = request.nextUrl.searchParams;
|
|
const sourceId = sp.get("source");
|
|
const classname = sp.get("classname");
|
|
if (!sourceId || !classname)
|
|
return apiError("Missing source or classname", 400);
|
|
if (!/^[\w\-.*]+$/.test(classname))
|
|
return apiError("Invalid classname", 400);
|
|
|
|
const cacheKey = `${sourceId}:${classname}`;
|
|
let icon = iconCache.get(cacheKey);
|
|
|
|
if (icon === undefined) {
|
|
const source = await getSource(sourceId);
|
|
if (!source) return apiError("Source not found", 404);
|
|
try {
|
|
const res = await fetch(`${source.nitroBaseUrl}/${classname}.nitro`, {
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) {
|
|
iconCache.set(cacheKey, null);
|
|
return apiError("Bundle not found", 404);
|
|
}
|
|
icon = await extractFurniIconPng(Buffer.from(await res.arrayBuffer()));
|
|
iconCache.set(cacheKey, icon);
|
|
pruneIconCache();
|
|
} catch {
|
|
iconCache.set(cacheKey, null);
|
|
return apiError("Failed to fetch bundle", 502);
|
|
}
|
|
}
|
|
|
|
if (!icon) return apiError("No icon in bundle", 404);
|
|
|
|
return new Response(new Uint8Array(icon), {
|
|
headers: {
|
|
"Content-Type": "image/png",
|
|
"Cache-Control": "public, max-age=86400",
|
|
},
|
|
});
|
|
},
|
|
);
|