← Files Sulekha EventsARCHIVED FILE

source/src/data/event-images.js

2.82 KB · Nov 30, 2026 · 07:53 UTC

↓ Download file

// Enrich sparse search results using only public Sulekha event-page metadata.
const cache = new Map();
const inFlight = new Map();
const MAX_CACHE = 500;

function eventPage(value) {
  try {
    const url = new URL(value);
    if (url.origin === 'https://events.sulekha.com' && !url.username && !url.password && /_event-in_/.test(url.pathname)) return url.href;
  } catch {}
}

export function posterFromHtml(html) {
  for (const tag of html.match(/<meta\b[^>]*>/gi) ?? []) {
    const attributes = {};
    for (const match of tag.matchAll(/([\w:-]+)\s*=\s*(["'])(.*?)\2/gs)) attributes[match[1].toLowerCase()] = match[3];
    if (!['og:image', 'og:image:secure_url', 'twitter:image'].includes((attributes.property ?? attributes.name ?? '').toLowerCase())) continue;
    try {
      const url = new URL((attributes.content ?? '').replace(/&amp;/gi, '&'));
      if (url.origin === 'https://usimg.sulekha.io' && !url.username && !url.password) return url.href;
    } catch {}
  }
}

// Posters occur in the page head. Stop reading as soon as metadata is found,
// instead of downloading the entire event page for every result.
async function readPoster(response) {
  if (!response.body?.getReader) return posterFromHtml(await response.text());
  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  let html = '';
  let bytes = 0;
  try {
    while (bytes < 262144) {
      const { done, value } = await reader.read();
      if (done) break;
      bytes += value.byteLength;
      html += decoder.decode(value, { stream: true });
      const image = posterFromHtml(html);
      if (image) return image;
      if (/<\/head\s*>/i.test(html)) break;
    }
  } finally { await reader.cancel().catch(() => {}); }
}

async function loadPoster(url, fetchImpl) {
  const cached = cache.get(url);
  if (cached && cached.expires > Date.now()) return cached.image;
  if (inFlight.has(url)) return inFlight.get(url);
  const request = (async () => {
    let image;
    try {
      const response = await fetchImpl(url, { redirect: 'error', signal: AbortSignal.timeout(2500), headers: { accept: 'text/html' } });
      if (response.ok) image = await readPoster(response);
    } catch { /* Missing posters must not prevent event discovery. */ }
    if (cache.size >= MAX_CACHE) cache.delete(cache.keys().next().value);
    cache.set(url, { image, expires: Date.now() + (image ? 3600000 : 60000) });
    return image;
  })();
  inFlight.set(url, request);
  try { return await request; } finally { inFlight.delete(url); }
}

export async function enrichEventImages(events, { fetchImpl = fetch } = {}) {
  return Promise.all(events.map(async event => {
    if (event.imageUrl) return event;
    const url = eventPage(event.url);
    if (!url) return event;
    const imageUrl = await loadPoster(url, fetchImpl);
    return imageUrl ? { ...event, imageUrl } : event;
  }));
}

SHA-256: 32abcaf906aeefe2ef8a8fab0f9bb573c880820deb47c6506e6f985ce30aa224