/** * The file engine reads what bytes ARE: a gated "download" (a paywall, a captcha, an encrypted stream) * answers with a page or noise under the file's content-type, and stored as a song it crashes the * owner's player. sniff() names the real type from the header; looksLike() says whether it matches the claim. */ import { describe, it, expect } from 'vitest'; import { sniff, looksLike } from '../src/fileEngine.js'; const bytes = (...parts) => Buffer.concat(parts.map((p) => (typeof p === 'string' ? Buffer.from(p, 'latin1') : Buffer.from(p)))); const noise = (n) => { const b = Buffer.alloc(n); let x = 12345; for (let i = 0; i < n; i++) { x = (x * 1103515245 + 12345) & 0x7fffffff; b[i] = (x >> 8) & 0xff; } return b; }; describe('what the bytes are', () => { it('names real files by their headers', () => { expect(sniff(bytes([0x89], 'PNG\r\n\x1a\n', noise(20)))).toBe('image/png'); expect(sniff(bytes([0xff, 0xd8, 0xff, 0xe0], noise(20)))).toBe('image/jpeg'); expect(sniff(bytes('RIFF', [1, 2, 3, 4], 'WEBPVP8 '))).toBe('image/webp'); expect(sniff(bytes('RIFF', [1, 2, 3, 4], 'WAVEfmt '))).toBe('audio/wav'); expect(sniff(bytes([0, 0, 0, 0x20], 'ftypM4A ', noise(20)))).toBe('audio/mp4'); expect(sniff(bytes([0, 0, 0, 0x20], 'ftypisom', noise(20)))).toBe('video/mp4'); expect(sniff(bytes('ID3', [3, 0], noise(20)))).toBe('audio/mpeg'); expect(sniff(bytes([0xff, 0xfb, 0x90, 0x64], noise(20)))).toBe('audio/mpeg'); expect(sniff(bytes('%PDF-1.7\n', noise(20)))).toBe('application/pdf'); expect(sniff(bytes('PK\x03\x04', noise(20)))).toBe('application/zip'); expect(sniff(bytes([0x1a, 0x45, 0xdf, 0xa3], noise(20)))).toBe('video/webm'); expect(sniff(bytes('OggS', noise(20)))).toBe('audio/ogg'); }); it('names a web page, json and text — the shapes a gated download answers with', () => { expect(sniff(Buffer.from('