Intelligence, Advanced Charting & Trading Platform

1 /**
2 * Project a provider-verified cached market catalog to a lightweight read-only
3 * snapshot. It never synthesizes prices, converts units or fabricates OHLC.
4 *
5 * Self-contained by design: this function is embedded unchanged into the
6 * Cloudflare Worker by the source-gated deployment patcher.
7 */
8 export function marketCatalogSummary(catalog, nowMs = Date.now()) {
9 const unavailable = {ok:false,error:'market_data_not_collected'};
10 if (!catalog || catalog.ok!==true || catalog.source!=='gheymat.online' ||
11 !Array.isArray(catalog.markets) || catalog.markets.length<1 ||
12 catalog.markets.length>200 || !Number.isFinite(nowMs)) return unavailable;
13 const ms=Date.parse(catalog.fetchedAt);
14 if (!Number.isFinite(ms) || ms>nowMs+300000) return unavailable;
15 const seen=new Set(), rows=[];
16 for(const m of catalog.markets) {
17 if(!m || typeof m.symbol!=='string' ||
18 !/^[A-Z0-9]{1,20}-[A-Z0-9]{2,10}$/.test(m.symbol) ||
19 seen.has(m.symbol) || typeof m.name!=='string' ||
20 !m.name.trim() || m.name.length>160 ||
21 typeof m.base!=='string' || m.base!==m.symbol.split('-')[0] ||
22 typeof m.quote!=='string' || m.quote!==m.symbol.split('-')[1] ||
23 typeof m.category!=='string' || !/^[A-Z_]{3,30}$/.test(m.category) ||
24 !Number.isFinite(m.sellPrice) || m.sellPrice<=0 ||
25 !(m.buyPrice===null || m.buyPrice===undefined || (Number.isFinite(m.buyPrice)&&m.buyPrice>0)) ||
26 !(m.change===null || m.change===undefined || Number.isFinite(m.change)) ||
27 typeof m.updatedAt!=='string' || !Number.isFinite(Date.parse(m.updatedAt)) ||
28 Date.parse(m.updatedAt)>nowMs+300000 ||
29 typeof m.isUpToDate!=='boolean' || !Array.isArray(m.history) ||
30 m.history.length>168 || !m.history.every(v=>Number.isFinite(v)&&v>0))return unavailable;
31 seen.add(m.symbol);
32 // Explicit allowlist: do not leak 168-point arrays to the initial screen.
33 rows.push({
34 symbol:m.symbol,name:m.name,base:m.base,quote:m.quote,category:m.category,
35 sellPrice:m.sellPrice,buyPrice:m.buyPrice??null,change:m.change??null,
36 updatedAt:m.updatedAt,isUpToDate:m.isUpToDate,
37 historyCount:m.history.length,
38 historyKind:'provider_price_points_not_ohlc'
39 });
40 }
41 return {
42 ok:true,source:catalog.source,fetchedAt:catalog.fetchedAt,
43 stale:nowMs-ms>45*60000,
44 quoteSide:'sell',
45 dataKind:'provider_market_snapshots_not_executable_quotes',
46 markets:rows
47 };
48 }