REST API that accepts MP3 file uploads and returns the frame count. Built with Bun runtime and Elysia framework.
- Runtime: Bun
- Framework: Elysia
- Language: TypeScript (strict mode)
- Testing: Bun's native test runner
- Linting/Formatting: Biome
src/index.ts- API server,/file-uploadPOST endpointsrc/parser/- MP3 parsing modulemp3-parser.ts- Core parser class,countFrames()returnsnumberdirectlyconstants.ts- MPEG1 Layer3 bitrates, sample rates, sync patterns, bit maskstypes.ts- TypeScript interfaces (FrameHeader)errors.ts- Custom error classes
tests/- Test files (separate from source)index.test.ts- API integration testsparser/mp3-parser.test.ts- Parser unit tests
- Skip ID3v2 metadata tags (detected by "ID3" magic bytes)
- Scan for frame sync pattern (0xFF followed by 0xE0 mask)
- Parse 4-byte frame header to extract version, layer, bitrate, sample rate
- Only count MPEG Version 1 Layer 3 frames (by design requirement)
- Calculate frame size:
floor((144 * bitrate * 1000) / sampleRate) + padding - Skip Xing/Info VBR header frames (metadata, not audio)
bun run dev- Start dev server with hot reloadbun test- Run test suitebun run lint- Lint codebun run format- Format codebun run check- Lint + format check
- Endpoint: POST
/file-upload - Request: multipart/form-data with
filefield - Success Response:
{ "frameCount": number }(200) - Error Response:
{ "error": string }(400/500)