SPB Git

Repository Public

sniff.ts 5.4 KB 77 lines typescript

1/** File type from content, not from the name or the declared MIME type. Pure functions. */2export type Sniffed =3  | { kind: "image"; mime: "image/png" | "image/jpeg" | "image/webp" | "image/gif"; width: number | null; height: number | null }4  | { kind: "pdf"; mime: "application/pdf" }5  | { kind: "svg"; mime: "image/svg+xml" }6  | { kind: "json"; mime: "application/json" }7  | { kind: "csv"; mime: "text/csv" }8  | { kind: "text"; mime: "text/plain" };910function jpegSize(b: Buffer): { width: number; height: number } | null {11  let i = 2;12  while (i + 9 < b.length) {13    if (b[i] !== 0xff) return null;14    const marker = b[i + 1], len = b.readUInt16BE(i + 2);15    if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) return { height: b.readUInt16BE(i + 5), width: b.readUInt16BE(i + 7) };16    i += 2 + len;17  }18  return null;19}2021function isUtf8Text(b: Buffer): string | null {22  if (b.includes(0)) return null;23  try { return new TextDecoder("utf-8", { fatal: true }).decode(b); } catch { return null; }24}2526/** Returns null for anything that is not one of the accepted types. `filename` only disambiguates text formats. */27export function sniff(b: Buffer, filename: string): Sniffed | null {28  if (b.length >= 24 && b.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return { kind: "image", mime: "image/png", width: b.readUInt32BE(16), height: b.readUInt32BE(20) };29  if (b.length >= 4 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) { const s = jpegSize(b); return { kind: "image", mime: "image/jpeg", width: s?.width ?? null, height: s?.height ?? null }; }30  if (b.length >= 30 && b.toString("latin1", 0, 4) === "RIFF" && b.toString("latin1", 8, 12) === "WEBP") {31    const fmt = b.toString("latin1", 12, 16);32    let w: number | null = null, h: number | null = null;33    if (fmt === "VP8X") { w = 1 + b.readUIntLE(24, 3); h = 1 + b.readUIntLE(27, 3); }34    else if (fmt === "VP8 ") { w = b.readUInt16LE(26) & 0x3fff; h = b.readUInt16LE(28) & 0x3fff; }35    else if (fmt === "VP8L") { const v = b.readUInt32LE(21); w = 1 + (v & 0x3fff); h = 1 + ((v >> 14) & 0x3fff); }36    return { kind: "image", mime: "image/webp", width: w, height: h };37  }38  if (b.length >= 10 && (b.toString("latin1", 0, 6) === "GIF87a" || b.toString("latin1", 0, 6) === "GIF89a")) return { kind: "image", mime: "image/gif", width: b.readUInt16LE(6), height: b.readUInt16LE(8) };39  if (b.length >= 5 && b.toString("latin1", 0, 5) === "%PDF-") return { kind: "pdf", mime: "application/pdf" };40  const text = isUtf8Text(b);41  if (text === null) return null;42  const head = text.replace(/^/, "").trimStart();43  if (/^(<\?xml[^>]*\?>\s*)?(<!--[\s\S]*?-->\s*)*(<!DOCTYPE[^>]*>\s*)?<svg[\s>]/i.test(head.slice(0, 2000))) return { kind: "svg", mime: "image/svg+xml" };44  const ext = filename.toLowerCase().split(".").pop() ?? "";45  if (ext === "json" || ((head.startsWith("{") || head.startsWith("[")) && ext !== "csv" && ext !== "txt" && ext !== "md")) {46    try { JSON.parse(text); return { kind: "json", mime: "application/json" }; } catch { if (ext === "json") return null; }47  }48  if (ext === "csv" || ext === "tsv") return { kind: "csv", mime: "text/csv" };49  if (/<(script|html|body|iframe)\b/i.test(head.slice(0, 4000))) return null; // HTML is not an accepted upload50  return { kind: "text", mime: "text/plain" };51}5253/** RFC 4180-style parser (quoted fields, doubled quotes, CR/LF), separator auto-detected among , ; and tab. */54export function parseCsv(text: string, maxRows = 5000): { columns: { name: string; type: "number" | "string" }[]; rows: (string | number)[][]; truncated: boolean } {55  const src = text.replace(/^/, "");56  const firstLine = src.slice(0, src.search(/\r?\n/) === -1 ? src.length : src.search(/\r?\n/));57  const sep = [",", ";", "\t"].map((s) => [s, firstLine.split(s).length] as const).sort((a, b) => b[1] - a[1])[0][0];58  const records: string[][] = []; let row: string[] = [], field = "", quoted = false;59  for (let i = 0; i < src.length; i++) {60    const c = src[i];61    if (quoted) { if (c === '"') { if (src[i + 1] === '"') { field += '"'; i++; } else quoted = false; } else field += c; continue; }62    if (c === '"' && field === "") quoted = true;63    else if (c === sep) { row.push(field); field = ""; }64    else if (c === "\n" || c === "\r") { if (c === "\r" && src[i + 1] === "\n") i++; row.push(field); field = ""; if (row.some((f) => f !== "")) records.push(row); row = []; if (records.length > maxRows + 1) break; }65    else field += c;66  }67  if (field !== "" || row.length) { row.push(field); if (row.some((f) => f !== "")) records.push(row); }68  if (records.length < 2) throw new Error("A dataset needs a header line and at least one row");69  const header = records[0].map((h, i) => h.trim() || `col${i + 1}`);70  if (header.length > 60) throw new Error("Too many columns (60 at most)");71  const body = records.slice(1, maxRows + 1).map((r) => header.map((_, i) => (r[i] ?? "").trim()));72  const num = (s: string) => /^-?(\d+([.,]\d+)?|[.,]\d+)(e[-+]?\d+)?$/i.test(s);73  const columns = header.map((name, i) => ({ name: name.slice(0, 80), type: body.every((r) => r[i] === "" || num(r[i])) && body.some((r) => r[i] !== "") ? ("number" as const) : ("string" as const) }));74  const rows = body.map((r) => r.map((v, i) => (columns[i].type === "number" && v !== "" ? Number(v.replace(",", ".")) : v.slice(0, 400))));75  return { columns, rows, truncated: records.length - 1 > maxRows };76}77
Files main
Menu

Theme