MC/website/worker/public-item-metadata.mjs
2026-11-27 23:43:05 +00:00

17 lines
1.8 KiB
JavaScript

/** The public gateway carries presentation fields only, never a serialized item. */
export function publicItemMetadata(row){
const out={},text=(v,n)=>typeof v==='string'?v.replace(/[\x00-\x1f\x7f]/g,' ').slice(0,n):'';
if(row.nativeMetadata===true)out.nativeMetadata=true;
if(['clan','company','politics'].includes(row.organisationGear?.kind)&&/^[A-Za-z0-9_-]{1,16}$/.test(row.organisationGear?.tag||''))out.organisationGear={kind:row.organisationGear.kind,tag:row.organisationGear.tag};
const name=text(row.displayName,96);if(name)out.displayName=name;
if(['common','uncommon','rare','very_rare','legendary','mythic','relic'].includes(row.quality))out.quality=row.quality;
if(Number.isInteger(row.equipmentMark)&&row.equipmentMark>=0&&row.equipmentMark<=14||out.quality==='relic'&&row.equipmentMark==='infinite')out.equipmentMark=row.equipmentMark;
if(Array.isArray(row.lore))out.lore=row.lore.slice(0,8).filter(x=>typeof x==='string').map(x=>text(x,160));
if(row.enchantments&&typeof row.enchantments==='object'&&!Array.isArray(row.enchantments))out.enchantments=Object.fromEntries(Object.entries(row.enchantments).filter(([id,n])=>/^(?:minecraft:)?[a-z_]{1,64}$/.test(id)&&Number.isInteger(n)&&n>0&&n<=255).slice(0,20));
if(Array.isArray(row.stats))out.stats=row.stats.slice(0,12).filter(x=>x&&typeof x.label==='string'&&typeof x.value==='string').map(x=>({label:text(x.label,48),value:text(x.value,64)}));
if(/^S[0-9]{1,4}$/.test(row.season||''))out.season=row.season;
if(/^\/assets\/minecraft-items\/[a-z0-9_]+\.png$/.test(row.image||''))out.image=row.image;
if(['arrakis','caladan','giedi_prime','kaitain','sigma_draconis_iv','lobby'].includes(row.creationWorld))out.creationWorld=row.creationWorld;
if(Number.isInteger(row.levyPercent)&&row.levyPercent>=0&&row.levyPercent<=5)out.levyPercent=row.levyPercent;
return out;
}