Skip to content

Curated website design inspiration gallery, open zero-auth MCP server for AI agents, and shareable moodboard lists.

4.3 KB
Raw Blame
import { readFileSync, realpathSync, statSync } from "node:fs";
import { resolve, sep } from "node:path";
import { createHash } from "node:crypto";
import sharp from "sharp";
export const CATEGORIES = [
  "AI tools",
  "Design tools",
  "Developer tools",
  "SaaS",
  "Product",
  "Portfolio",
  "Experimental",
  "404 pages",
];
export function safeWebsite(value) {
  let u;
  try {
    u = new URL(value);
  } catch {
    throw Error("Use a full HTTPS website URL.");
  }
  if (
    u.protocol !== "https:" ||
    u.username ||
    u.password ||
    u.port ||
    u.hostname === "localhost" ||
    u.hostname.endsWith(".local") ||
    !u.hostname.includes(".") ||
    /^\d+\.\d+\.\d+\.\d+$/.test(u.hostname) ||
    u.hostname.includes(":")
  )
    throw Error(
      "Use a public HTTPS URL without credentials, ports, or IP addresses.",
    );
  return u;
}
export async function validateSites(sites, root) {
  if (!Array.isArray(sites) || !sites.length)
    throw Error("The catalog must contain websites.");
  const ids = new Set(),
    urls = new Set();
  for (const s of sites) {
    if (!s || typeof s !== "object") throw Error("Invalid entry.");
    if (
      typeof s.id !== "string" ||
      !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(s.id) ||
      s.id.length > 80 ||
      ids.has(s.id)
    )
      throw Error(`Invalid or duplicate id: ${s.id}`);
    ids.add(s.id);
    for (const [key, max] of [
      ["name", 100],
      ["description", 240],
      ["observation", 1200],
      ["credit", 150],
    ])
      if (typeof s[key] !== "string" || !s[key].trim() || s[key].length > max)
        throw Error(`${s.id}: invalid ${key}`);
    const u = safeWebsite(s.url);
    const normalized =
      u.hostname.replace(/^www\./, "") +
      u.pathname.replace(/\/$/, "") +
      u.search;
    if (urls.has(normalized)) throw Error(`${s.id}: duplicate URL`);
    urls.add(normalized);
    if (!CATEGORIES.includes(s.category))
      throw Error(`${s.id}: unknown category`);
    if (
      !Array.isArray(s.tags) ||
      s.tags.length > 8 ||
      !s.tags.every(
        (t) => typeof t === "string" && t.length > 0 && t.length <= 40,
      )
    )
      throw Error(`${s.id}: invalid tags`);
    if (
      !/^\d{4}-\d{2}-\d{2}$/.test(s.capturedAt) ||
      !Number.isFinite(Date.parse(s.capturedAt)) ||
      new Date(s.capturedAt).toISOString().slice(0, 10) !== s.capturedAt
    )
      throw Error(`${s.id}: invalid capture date`);
    if (s.discoveredVia) {
      safeWebsite(s.discoveredVia.url);
      if (
        typeof s.discoveredVia.name !== "string" ||
        s.discoveredVia.name.length > 150
      )
        throw Error(`${s.id}: invalid discovery credit`);
    }
    if (!/^assets\/[a-z0-9-]+\.(jpg|jpeg|png|webp)$/.test(s.image))
      throw Error(`${s.id}: use a local raster screenshot`);
    const p = resolve(root, s.image),
      assets = realpathSync(resolve(root, "assets"));
    if (
      !realpathSync(p).startsWith(assets + sep) ||
      statSync(p).size > 3 * 1024 * 1024
    )
      throw Error(`${s.id}: screenshot exceeds bounds`);
    const data = readFileSync(p),
      m = await sharp(data, { limitInputPixels: 40000000 }).metadata();
    if (
      !["jpeg", "png", "webp"].includes(m.format) ||
      m.width < 1000 ||
      m.width > 2400 ||
      m.height < 600 ||
      m.height > 16000
    )
      throw Error(
        `${s.id}: screenshot dimensions must be 1000–2400 wide, 600–16000 high`,
      );
    if (
      s.imageDimensions?.width !== m.width ||
      s.imageDimensions?.height !== m.height
    )
      throw Error(`${s.id}: screenshot dimensions do not match`);
    if (createHash("sha256").update(data).digest("hex") !== s.captureSha256)
      throw Error(`${s.id}: screenshot hash does not match`);
    if (s.video !== undefined) {
      if (
        typeof s.video !== "string" ||
        !/^assets\/[a-z0-9-]+\.mp4$/.test(s.video)
      )
        throw Error(`${s.id}: use a local mp4 scroll preview`);
      const vp = resolve(root, s.video);
      if (
        !realpathSync(vp).startsWith(assets + sep) ||
        statSync(vp).size > 5 * 1024 * 1024
      )
        throw Error(`${s.id}: video preview exceeds bounds`);
      const vdata = readFileSync(vp);
      if (createHash("sha256").update(vdata).digest("hex") !== s.videoSha256)
        throw Error(`${s.id}: video hash does not match`);
    }
  }
  return sites;
}