Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

6.3 KB
Raw Blame
// Downloads every source in src/sources.ts, renders it to a square
// transparent PNG in assets/ (plus a `-dark` one where configured) and
// rewrites src/index.ts. Run by hand, commit the result. Exits non-zero if
// any source failed so a broken logo is never silently dropped from the map.
// Existing PNGs are kept unless `--force` is passed, so a rerun only fetches
// what is missing.

import { access, mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import sharp from 'sharp';
import { LOGO_SOURCES } from '../src/sources';

const LOGO_SIZE = 64;
// Rasterize SVGs at a high density so a 64px downscale stays crisp.
const SVG_DENSITY = 300;
const FETCH_TIMEOUT_MS = 15_000;
// Wikimedia throttles bursts; space the downloads and back off on 429.
const PAUSE_BETWEEN_DOWNLOADS_MS = 4000;
const RETRY_DELAYS_MS = [20_000, 60_000, 120_000];
const HTTP_TOO_MANY_REQUESTS = 429;
// Wikimedia's policy wants a descriptive agent with a contact and throttles
// anonymous browser strings; some other hosts refuse anything but a browser.
const WIKIMEDIA_USER_AGENT =
  'OpenPanelLogoFetcher/1.0 (https://openpanel.dev; [email protected])';
const BROWSER_USER_AGENT =
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0 Safari/537.36';
const TRANSPARENT = { r: 0, g: 0, b: 0, alpha: 0 };

const packageRoot = fileURLToPath(new URL('..', import.meta.url));
const assetsDir = path.join(packageRoot, 'assets');
const indexFile = path.join(packageRoot, 'src', 'index.ts');

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

function userAgentFor(url: string): string {
  return new URL(url).hostname.endsWith('wikimedia.org')
    ? WIKIMEDIA_USER_AGENT
    : BROWSER_USER_AGENT;
}

async function download(url: string, attempt = 0): Promise<Buffer> {
  const response = await fetch(url, {
    headers: { 'user-agent': userAgentFor(url), accept: 'image/*,*/*;q=0.8' },
    signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
  });
  if (
    response.status === HTTP_TOO_MANY_REQUESTS &&
    attempt < RETRY_DELAYS_MS.length
  ) {
    await sleep(RETRY_DELAYS_MS[attempt]!);
    return download(url, attempt + 1);
  }
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }
  return Buffer.from(await response.arrayBuffer());
}

async function exists(file: string): Promise<boolean> {
  try {
    await access(file);
    return true;
  } catch {
    return false;
  }
}

async function render(source: Buffer): Promise<Buffer> {
  return sharp(source, { density: SVG_DENSITY })
    .resize(LOGO_SIZE, LOGO_SIZE, { fit: 'contain', background: TRANSPARENT })
    .png()
    .toBuffer();
}

function toIdentifier(slug: string): string {
  return slug.replace(/-([a-z0-9])/g, (_, char: string) => char.toUpperCase());
}

/** Negates the colour channels and keeps the alpha, so a black glyph turns white. */
function invert(png: Buffer): Promise<Buffer> {
  return sharp(png).negate({ alpha: false }).png().toBuffer();
}

async function renderDark(
  dark: string | 'invert',
  lightFile: string
): Promise<Buffer> {
  if (dark === 'invert') {
    return invert(await readFile(lightFile));
  }
  return render(await download(dark));
}

const PLAIN_KEY = /^[a-z_$][a-z0-9_$]*$/i;

// Biome wants quotes only where needed.
function key(name: string): string {
  return PLAIN_KEY.test(name) ? name : `'${name}'`;
}

function renderIndex(slugs: string[]): string {
  const imports = slugs
    .flatMap((slug) => {
      const id = toIdentifier(slug);
      const lines = [`import ${id} from '../assets/${slug}.png';`];
      if (LOGO_SOURCES[slug]!.dark) {
        lines.push(`import ${id}Dark from '../assets/${slug}-dark.png';`);
      }
      return lines;
    })
    .join('\n');
  const entries = slugs
    .flatMap((slug) => {
      const id = toIdentifier(slug);
      const value = LOGO_SOURCES[slug]!.dark
        ? `{ light: ${id}, dark: ${id}Dark }`
        : `{ light: ${id} }`;
      return [...LOGO_SOURCES[slug]!.names]
        .sort()
        .map((name) => `  ${key(name)}: ${value},`);
    })
    .join('\n');
  return `// Generated by scripts/generate.ts from src/sources.ts. Do not edit.

${imports}

export type LogoTheme = 'light' | 'dark';

export interface Logo {
  light: string;
  /** Only where the light file would vanish on a dark background. */
  dark?: string;
}

/** Lower-cased serie name (browser, OS, device, referrer) to its logo files. */
export const LOGOS: Readonly<Record<string, Logo>> = {
${entries}
};

export function getLogo(name: string): Logo | undefined {
  return LOGOS[name.toLowerCase()];
}

/** The file to show for the theme; falls back to the light one. */
export function getLogoUrl(
  name: string,
  theme: LogoTheme = 'light'
): string | undefined {
  const logo = getLogo(name);
  if (!logo) {
    return undefined;
  }
  return theme === 'dark' ? (logo.dark ?? logo.light) : logo.light;
}
`;
}

async function main(): Promise<void> {
  await mkdir(assetsDir, { recursive: true });
  const slugs = Object.keys(LOGO_SOURCES).sort();
  const force = process.argv.includes('--force');
  const failures: string[] = [];

  for (const slug of slugs) {
    const { url, dark } = LOGO_SOURCES[slug]!;
    const file = path.join(assetsDir, `${slug}.png`);
    const darkFile = path.join(assetsDir, `${slug}-dark.png`);
    try {
      if (force || !(await exists(file))) {
        const png = await render(await download(url));
        await writeFile(file, png);
        console.log(`ok    ${slug} (${png.length} bytes)`);
        await sleep(PAUSE_BETWEEN_DOWNLOADS_MS);
      } else {
        console.log(`keep  ${slug}`);
      }
      if (dark && (force || !(await exists(darkFile)))) {
        const png = await renderDark(dark, file);
        await writeFile(darkFile, png);
        console.log(`ok    ${slug}-dark (${png.length} bytes)`);
      }
    } catch (error) {
      failures.push(slug);
      console.error(`FAIL  ${slug}: ${(error as Error).message}  <- ${url}`);
    }
  }

  // The committed index stays as it is when a source fails, so a rerun with
  // `--force` cannot drop a logo whose PNG is still there.
  if (failures.length > 0) {
    console.error(
      `\n${failures.length} logo(s) failed: ${failures.join(', ')}`
    );
    process.exit(1);
  }
  await writeFile(indexFile, renderIndex(slugs));
}

await main();