Preserve the current Alpha source and artwork snapshot. See handoff/alpha-1.0.0.3 for actual deployed hashes, checks, staged follow-ups and the production pause. This checkpoint does not claim every source feature is deployed.
14 lines
4.8 KiB
JavaScript
14 lines
4.8 KiB
JavaScript
import {identity} from '../node/auth.mjs';import {accessFor} from '../node/permissions.mjs';
|
|
const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v),exact=(v,keys)=>v&&typeof v==='object'&&!Array.isArray(v)&&Object.keys(v).length===keys.length&&keys.every(k=>Object.hasOwn(v,k));
|
|
const reply=(body,status=200)=>Response.json(body,{status,headers:{'Cache-Control':'private, no-store',Vary:'Cookie'}});
|
|
export function createKitMarket(db,settings={},dependencies={}){const authenticate=dependencies.authenticate||(r=>identity(r,db)),access=dependencies.access||(u=>accessFor(db,u)),fetcher=dependencies.fetcher||fetch;return async request=>{
|
|
const url=new URL(request.url),path=url.pathname;if(url.search)return reply({message:'Identity and query overrides are not accepted.'},400);const user=await authenticate(request);if(!user)return reply({message:'Sign in first.'},401);if(!(await access(user)).nodes.includes('web.menu.inventory'))return reply({message:'Inventory access required.'},403);const link=await db.prepare('SELECT minecraft_uuid FROM site_game_links WHERE user_id=?').bind(user.id).first();
|
|
const reads={'/api/kits/catalogue':'/kits/catalogue','/api/kits/arc':'/kits/arc','/api/kits/orders':'/kits/orders'};let nativePath=reads[path],body;if(request.method==='GET'&&nativePath){if(path!=='/api/kits/catalogue'){if(!uuid(link?.minecraft_uuid))return reply({linked:false,message:'Link Minecraft to use this feature.'},409);nativePath+='?uuid='+encodeURIComponent(link.minecraft_uuid);}}
|
|
else if(request.method==='POST'&&['/api/kits/redeem','/api/kits/arc/claim','/api/kits/cart/checkout'].includes(path)){
|
|
if(request.headers.get('origin')!==new URL(settings.CP_ORIGIN||'https://cp.dunerealms.com').origin)return reply({message:'Origin rejected.'},403);if(!uuid(link?.minecraft_uuid))return reply({linked:false,message:'Link Minecraft first.'},409);let b;try{const raw=await request.text();if(Buffer.byteLength(raw)>20000)throw Error();b=JSON.parse(raw);}catch{return reply({message:'Invalid request.'},400);}
|
|
if(path==='/api/kits/redeem'){if(!exact(b,['code'])||!/^DRK-[A-Z2-7]{32}$/.test(b.code))return reply({message:'Use the full kit redeem code.'},400);nativePath='/kits/redeem';body={uuid:link.minecraft_uuid,code:b.code};}
|
|
else if(path==='/api/kits/arc/claim'){if(!exact(b,['index'])||!Number.isInteger(b.index)||b.index<0||b.index>4)return reply({message:'Invalid quest chapter.'},400);nativePath='/kits/arc/claim';body={uuid:link.minecraft_uuid,index:b.index};}
|
|
else{if(!exact(b,['cartId','lines'])||!uuid(b.cartId)||!Array.isArray(b.lines)||b.lines.length<1||b.lines.length>20||b.lines.some(l=>!exact(l,['listingId','orderLineId','listingVersion','blueprintVersion','priceCents'])||!uuid(l.listingId)||!uuid(l.orderLineId)||!['listingVersion','blueprintVersion','priceCents'].every(k=>Number.isSafeInteger(l[k])&&l[k]>0))||new Set(b.lines.map(l=>l.orderLineId)).size!==b.lines.length)return reply({message:'Invalid cart. Refresh its prices before paying.'},400);const gate=await db.prepare("SELECT setting_value FROM website_settings WHERE setting_key='kit-policy-v2-publish-enabled'").first();if(gate?.setting_value!=='true'){for(const line of b.lines){const listing=await db.prepare('SELECT b.commerce_kind FROM site_kit_listings l JOIN site_kit_blueprints b ON b.id=l.blueprint_id WHERE l.id=?').bind(line.listingId).first();if(listing?.commerce_kind!=='personal_sale')return reply({message:'This design is published on the website. In-game delivery and checkout open after the game update.'},409);}}nativePath='/kits/cart/checkout';body={buyerUuid:link.minecraft_uuid,cartId:b.cartId,lines:b.lines};}
|
|
}else return reply({message:'Method or route not available.'},405);
|
|
if((settings.SERVER_CONTROL_TOKEN||'').length<64)return reply({message:'The kit service is unavailable.'},503);try{const result=await fetcher('http://127.0.0.1:15433'+nativePath,{method:body?'POST':'GET',headers:{Authorization:'Bearer '+settings.SERVER_CONTROL_TOKEN,...(body?{'Content-Type':'application/json'}:{})},...(body?{body:JSON.stringify(body)}:{}),signal:AbortSignal.timeout(15000)});if(!result.ok)return reply({message:result.status===409?'This item, balance or chapter changed. Refresh and retry.':result.status===403?'This action is not available for your verified character.':'The kit service could not complete this request.'},[400,403,404,409].includes(result.status)?result.status:503);const d=await result.json();if(path!=='/api/kits/catalogue'&&request.method==='GET'&&d.playerUuid?.toLowerCase()!==link.minecraft_uuid.toLowerCase())throw Error('Identity mismatch');if(path==='/api/kits/catalogue'&&(!Array.isArray(d.listings)||d.currency!=='dunemarks'))throw Error('Invalid catalogue');return reply(d);}catch{return reply({message:'The kit service is temporarily unavailable. Existing receipts remain safe.'},503);}
|
|
};}
|

Staff