How ashwini-chaturvedi/codebox works
Purpose and Process Entry Points
CodeBox is a self-hosted, Judge0-compatible code-execution service for untrusted programming submissions. The API process starts at src/index.js and serves Express from src/api/server.js; worker processing is separated in src/queue/worker.js. Local and production service topologies are defined in docker-compose.yml and docker-compose.prod.yml.
HTTP API Boundary
Submission, language, system, and metrics endpoints are routed through src/api/routes/index.js, with execution requests centered in src/api/routes/submissions.js. API middleware applies token authentication, request validation, rate limiting, and centralized error handling before requests enter the queue:
• src/api/middleware/auth.js
• src/api/middleware/validation.js
• src/api/middleware/rateLimit.js
The public contract supports asynchronous token-based submission retrieval, optional synchronous waiting, and bounded batch submission.
Queue-Based Execution Pipeline
The API publishes execution work to Redis via src/queue/producer.js. BullMQ workers consume jobs in src/queue/worker.js and dispatch the execution job implementation in src/queue/jobs/executeCode.js. This boundary isolates HTTP responsiveness from slow or resource-intensive compilation and execution, and allows worker concurrency to scale independently.
Language Abstraction and Runtime Images
Language IDs map source code to compiler/interpreter commands, filenames, and execution behavior in src/languages/index.js, with concrete definitions such as src/languages/python.js and src/languages/java.js. Execution images package language toolchains independently:
• docker/images/python/Dockerfile
• docker/images/gcc/Dockerfile
• docker/images/java/Dockerfile
This makes language additions primarily a language-definition plus runtime-image concern.
Executor Selection and Sandbox Boundary
src/executor/ExecutorFactory.js selects Docker, Firecracker, or automatic fallback. Docker execution is implemented in src/executor/DockerExecutor.js; Firecracker microVM execution is in src/executor/FirecrackerExecutor.js. src/executor/ContainerPool.js manages reusable container capacity, while src/executor/ResultParser.js normalizes sandbox output into Judge0-style results.
Untrusted code crosses the principal trust boundary only at these executors, which enforce isolated execution, disabled network access, and CPU, memory, process, and time constraints. Firecracker requires host/kernel support; Docker is the broader compatibility path.
Security, Results, and Observability
Before execution, src/security/codeAnalyzer.js provides a code-analysis security stage. Results flow from the chosen executor through parsing back to the queue/job state, then are retrieved by submission token through the API.
Prometheus-facing instrumentation is implemented at src/api/metrics.js and exposed through src/api/routes/metrics.js; scrape configuration is prometheus.yml. Redis is the durable coordination dependency; Docker is required for runtime isolation and image execution.
Deployment Edge
Production places Caddy in front of the API for TLS termination and auto-SSL, configured by Caddyfile and docker-compose.prod.yml. API and worker images are built separately in docker/api/Dockerfile and docker/worker/Dockerfile, reinforcing independent scaling and failure domains. Production setup and Firecracker capability detection are orchestrated by scripts/setup-production.sh.
Main entry points: routes/index.js API Routes · submissions.js Submission API · languages/index.js Language Registry · ExecutorFactory.js Executor Factory
Components 22
API Service
- API Entry
src/index.js - Express Server
src/api/server.js - API Routes · Submission, language, system, and metrics endpoints.
src/api/routes/index.js - Submission API · Supports async retrieval, sync wait, and bounded batches.
src/api/routes/submissions.js - API Middleware · Token auth, validation, rate limits, and error handling.
src/api/middleware/auth.js - Prometheus Metrics
src/api/metrics.js
Execution Pipeline
- Queue Producer
src/queue/producer.js - Redis · Stores queue and submission job state.
- Queue Worker · Scales independently from HTTP API.
src/queue/worker.js - Execute Code Job
src/queue/jobs/executeCode.js
Sandbox Runtime
- Code Analyzer · Analyzes code before sandbox execution.
src/security/codeAnalyzer.js - Language Registry · Maps language IDs to commands, files, and behavior.
src/languages/index.js - Executor Factory · Chooses Docker, Firecracker, or automatic fallback.
src/executor/ExecutorFactory.js - Docker Executor · Compatibility execution path with constrained, network-disabled containers.
src/executor/DockerExecutor.js - Firecracker Executor · Requires compatible host and kernel support.
src/executor/FirecrackerExecutor.js - Container Pool · Manages reusable Docker container capacity.
src/executor/ContainerPool.js - Result Parser · Converts sandbox output to Judge0-style results.
src/executor/ResultParser.js - Language Runtime Images · Independent Python, GCC, and Java toolchain images.
docker/images/python/Dockerfile
Deployment & Observability
- Caddy TLS Proxy · Terminates TLS in production.
Caddyfile - Prometheus
prometheus.yml - Production Topology · Builds separate API and worker images behind Caddy.
docker-compose.prod.yml
Other components
- Judge0 Clients
Connections 25
- Judge0 Clients → Caddy TLS Proxy: HTTPS
- Caddy TLS Proxy → API Entry: proxies requests
- API Entry → Express Server: starts
- Express Server → API Middleware: applies
- API Middleware → API Routes: permits requests
- API Routes → Submission API: dispatches
- API Routes → Prometheus Metrics: exposes
- Submission API → Queue Producer: publishes execution
- Queue Producer → Redis: enqueues jobs
- Queue Worker → Redis: consumes jobs
- Queue Worker → Execute Code Job: dispatches
- Execute Code Job → Code Analyzer: analyzes source
- Code Analyzer → Language Registry: validated code
- Language Registry → Executor Factory: execution specification
- Executor Factory → Docker Executor: Docker path
- Executor Factory → Firecracker Executor: microVM path
- Docker Executor → Container Pool: uses capacity
- Language Registry → Language Runtime Images: selects toolchain
- Docker Executor → Result Parser: sandbox output
- Firecracker Executor → Result Parser: sandbox output
- Result Parser → Redis: stores result state
- Submission API → Redis: retrieves by token
- Prometheus → Prometheus Metrics: scrapes
- Production Topology → API Entry: deploys API image
- Production Topology → Queue Worker: deploys worker image