From 1f453b8a1f614b97083c4c2fe7b55164429ea8f6 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Sun, 18 Oct 2026 15:57:17 +0200 Subject: [PATCH] Add CHOAM read-only web market component --- website/worker/exchange-api.mjs | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 website/worker/exchange-api.mjs diff --git a/website/worker/exchange-api.mjs b/website/worker/exchange-api.mjs new file mode 100644 index 00000000..87ec6f86 --- /dev/null +++ b/website/worker/exchange-api.mjs @@ -0,0 +1,2 @@ +/** Read-only public market gateway; the private bridge must expose only sanitized listings. */ +export async function handleExchange(request,env){if(request.method!=='GET')return Response.json({error:'method_not_allowed'},{status:405});const url=new URL(request.url);if(url.pathname!=='/api/exchange/listings')return Response.json({error:'not_found'},{status:404});if(!env.EXCHANGE_LISTINGS_URL||!env.EXCHANGE_READ_TOKEN)return Response.json({error:'market_not_configured'},{status:503,headers:{'Cache-Control':'no-store'}});try{const upstream=new URL(env.EXCHANGE_LISTINGS_URL);if(upstream.protocol!=='https:')throw Error('HTTPS required');const response=await fetch(upstream,{headers:{Authorization:'Bearer '+env.EXCHANGE_READ_TOKEN},signal:AbortSignal.timeout(7000)});if(!response.ok)throw Error('upstream_error');const body=await response.json();if(!Array.isArray(body.listings))throw Error('invalid_data');const listings=body.listings.slice(0,500).map(x=>({id:Number(x.id),material:String(x.material||'').slice(0,80),category:String(x.category||'').slice(0,24),origin:String(x.origin||'').slice(0,48),seller:String(x.seller||'').slice(0,32),quantity:Number(x.quantity),price:Number(x.price)})).filter(x=>Number.isSafeInteger(x.id)&&x.id>0&&Number.isSafeInteger(x.quantity)&&x.quantity>0&&Number.isFinite(x.price)&&x.price>0);return Response.json({listings},{headers:{'Cache-Control':'public, max-age=30','X-Content-Type-Options':'nosniff'}});}catch{return Response.json({error:'market_unavailable'},{status:502,headers:{'Cache-Control':'no-store'}});}}