This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Xget is a high-performance, Cloudflare Workers-based acceleration engine for developer resources. It provides unified acceleration for code repositories (GitHub, GitLab, etc.), package registries (npm, PyPI, Maven, etc.), container registries (Docker Hub, GHCR, etc.), and AI inference APIs (OpenAI, Anthropic, etc.).
The project operates as a reverse proxy that transforms incoming requests to match various platform APIs while adding security headers, caching, retry logic, and performance monitoring.
# Start development server (Cloudflare Workers local environment)
npm run dev # Runs on http://localhost:8787
# Deploy to Cloudflare Workers production
npm run deploy
# Build and run tests
npm run test # Run tests in watch mode
npm run test:run # Run tests once
npm run test:coverage # Generate coverage report
npm run test:ui # Open Vitest UI
# Code quality
npm run lint # Check code quality
npm run lint:fix # Fix linting issues
npm run format # Format code with Prettier
npm run format:check # Check formatting without changes
npm run type-check # TypeScript type checking (no emit)
npm run commitlint # Validate the latest commit message- Use Conventional Commits for every commit
- Preferred format:
type(scope): description - Common types:
feat,fix,docs,refactor,perf,test,chore - The repository installs a
commit-msghook vianpm install; do not bypass it unless explicitly required
- Before every commit, run the local CI-equivalent checks from
.github/workflows/ci.yml - Required commands:
npm run lint,npm run format:check,npm run test:run, andnpm run type-check - If any required check fails, do not commit until the failure is resolved
- Apply this rule to every commit, including documentation-only changes, unless the user explicitly asks for a different workflow
- Tests use Vitest with
@cloudflare/vitest-pool-workersfor Workers-specific testing - Run
npm run test:runbefore committing to ensure all tests pass - Coverage reports are generated in
coverage/directory
- Entry Point:
src/index.js- Exports default Worker withfetch()handler - Validation:
src/utils/validation.js- Validates HTTP methods, path length, detects protocol types - Platform Detection: URL path is parsed to identify platform (e.g.,
/gh/→ GitHub) - Path Transformation:
src/routing/platform-transformers.js#transformPath()converts request paths to upstream URLs - Protocol Handling: Different handlers for Git, Docker, AI inference requests
- Upstream Fetch: Request forwarded with appropriate headers and retry logic
- Response Processing: URL rewriting for certain platforms (npm, PyPI), cache storage
- Security Headers: Added via
src/utils/security.jsbefore returning to client
-
index.js: Runtime configuration with environment variable overridesTIMEOUT_SECONDS: Request timeout (default: 30s)MAX_RETRIES: Retry attempts (default: 3)CACHE_DURATION: Cache TTL (default: 1800s = 30 minutes)SECURITY.ALLOWED_METHODS: HTTP methods (default: GET, HEAD)
-
platform-catalog.js: Platform base URL definitionsPLATFORM_CATALOG: Object mapping platform keys to base URLs
-
routing/platform-index.js: Pre-sorted keys for efficient matchingSORTED_PLATFORMS: Longest-prefix-first platform matching order
-
routing/platform-transformers.js: Platform-specific path rewritingtransformPath(): Converts request paths to platform-specific URLs- Special handling for crates.io (adds
/api/v1/cratesprefix) and Jenkins (adds/current/prefix)
-
git.js: Git protocol detection and header configuration- Detects Git operations via User-Agent, endpoints (
/info/refs,/git-upload-pack) - Handles Git LFS via
Accept: application/vnd.git-lfs+json
- Detects Git operations via User-Agent, endpoints (
-
docker.js: Container registry protocol (OCI/Docker)- Parses WWW-Authenticate headers for token authentication
- Handles Docker registry v2 API authentication flow
- Special redirect handling to prevent leaking auth tokens to blob storage
-
ai.js: AI inference API detection and header forwarding- Detects requests to
/ip/*platforms - Preserves all headers for AI API compatibility
- Detects requests to
-
validation.js: Request validation logicisDockerRequest(): Detects Docker/OCI operationsvalidateRequest(): Enforces security policies
-
security.js: Security headers and error responses- Adds HSTS, X-Frame-Options, CSP, X-XSS-Protection
createErrorResponse(): Generates standardized error responses
-
performance.js: Performance monitoringPerformanceMonitor: Tracks request timing- Adds
X-Performance-Metricsheader to responses
- Uses Cloudflare Cache API for GET requests (200 OK only)
- Cache TTL controlled by
CACHE_DURATIONconfig - Skips cache for: Git operations, Docker operations, AI inference requests
- Range requests: First checks for range-specific cache, falls back to full content cache
- Rewrites
https://registry.npmjs.org/URLs in JSON responses to point to Xget instance
- Rewrites
https://files.pythonhosted.orgURLs in HTML responses to point to Xget instance - Uses separate
pypi-filesplatform for file downloads
- Adds
/api/v1/cratesprefix to all API requests - Handles search endpoint (
/?q=) specially
- Adds
/current/prefix to update center paths - Preserves
/experimental/and/download/paths as-is
- Handles authentication via token service
- Uses manual redirect mode to strip Authorization headers before S3 redirects
- Auto-retries with public token on 401 responses
src/
├── index.js # Main Worker entry point
├── app/
│ ├── handle-request.js # Shared request pipeline
│ └── request-context.js # Protocol-aware request classification
├── config/
│ ├── index.js # Runtime configuration
│ ├── platform-catalog.js # Platform base URLs
│ └── platforms.js # Compatibility exports
├── protocols/
│ ├── git.js # Git protocol handler
│ ├── docker.js # Docker/OCI handler
│ └── ai.js # AI inference handler
├── response/
│ └── finalize-response.js # Response shaping and cache writes
├── routing/
│ ├── platform-index.js # Platform matching order
│ ├── platform-transformers.js
│ └── resolve-target.js # Upstream target resolution
├── upstream/
│ ├── cache.js # Cache read helpers
│ └── fetch-upstream.js # Upstream transport and retries
└── utils/
├── validation.js # Request validation
├── security.js # Security utilities
└── performance.js # Performance monitoring
test/
├── features/ # Feature tests
├── platforms/ # Platform-specific tests
├── unit/ # Unit tests
├── index.test.js # Core Worker tests
└── integration.test.js # Integration tests
- Check if Docker request (via
isDockerRequest()) - Check if Git request (via
isGitRequest()) - Check if Git LFS request (via
isGitLFSRequest()) - Check if AI request (via
isAIInferenceRequest()) - Default to standard file download
- Add platform entry to
PLATFORM_CATALOGinsrc/config/platform-catalog.js - If special path transformation needed, add a transformer in
src/routing/platform-transformers.js - Add platform tests in
test/platforms/ - Update README.md with platform documentation
- Retries up to
MAX_RETRIEStimes with linear backoff - Delay:
RETRY_DELAY_MS * attempts(default: 1000ms, 2000ms, 3000ms) - Retries on: Network errors, timeouts, 5xx errors
- Does NOT retry: 4xx errors (except Docker 401 which has special handling)
- All errors caught at top level in
handleRequest() - Errors converted to JSON responses via
createErrorResponse() - Performance metrics still added even on error paths
- Unit tests (
test/unit/): Test individual functions in isolation - Feature tests (
test/features/): Test specific features (auth, caching, Git, performance) - Platform tests (
test/platforms/): Test platform-specific transformations - Integration tests (
test/integration.test.js): End-to-end request flows
# Run specific test file
npm run test:run test/unit/platforms.test.js
# Run tests matching pattern
npm run test:run -- --testNamePattern "Docker"
# Run with coverage
npm run test:coverage// Mock request creation
const request = new Request('http://localhost/gh/microsoft/vscode', {
method: 'GET',
headers: { 'User-Agent': 'git/2.34.1' }
});
// Mock environment
const env = {};
const ctx = { waitUntil: () => {} };
// Test the worker
const response = await worker.fetch(request, env, ctx);
expect(response.status).toBe(200);- Primary deployment target
- Uses GitHub Actions for CI/CD (
.github/workflows/workers.yml) - Requires
CLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_IDsecrets
- Alternative deployment via adapter in
adapters/pages/ - Auto-synced from
mainbranch topagesbranch - Uses separate workflow (
.github/workflows/pages-cf.yml)
- Vercel/Netlify: Uses Functions adapter in
adapters/functions/ - Deno Deploy: Uses Functions adapter (compatible format)
- Docker: Multi-stage build using
workerdruntime
Configure in Cloudflare Workers dashboard or via wrangler.toml:
TIMEOUT_SECONDS: Override default timeoutMAX_RETRIES: Override retry countCACHE_DURATION: Override cache TTLALLOWED_METHODS: Override allowed HTTP methods (comma-separated)ALLOWED_ORIGINS: Override CORS origins (comma-separated)
- Never log or expose Authorization headers
- Docker authentication tokens are stripped before S3 redirects
- All responses include security headers (HSTS, CSP, X-Frame-Options, etc.)
- Path length limited to prevent URL-based attacks (default: 2048 chars)
- Use
ctx.waitUntil()for cache writes to avoid blocking response - Range requests leverage cache when possible
- Cloudflare edge caching (
cffetch options) for non-protocol requests - HTTP/3 enabled for supported clients
- Skip normal caching mechanisms
- Allow POST/PUT/PATCH methods
- Preserve all upstream headers
- No performance headers added (to maintain protocol compatibility)
- Only enabled for npm and PyPI platforms
- Rewrites responses to point to Xget instance instead of upstream
- Required for package managers to download dependencies through Xget
- Add to
PLATFORM_CATALOGinsrc/config/platform-catalog.js - If special transformation needed, update
src/routing/platform-transformers.js - Add test in
test/platforms/ - Update README.md documentation
- Test locally with
npm run dev
- Use
npm run devto start local server - Add
console.log()statements insrc/app/handle-request.jsor the relevant extracted pipeline module - Check Wrangler dev server output
- Inspect
X-Performance-Metricsheader in responses
- Run specific failing test:
npm run test:run test/path/to/test.js - Check mock setup matches actual request pattern
- Verify platform configuration in
src/config/platform-catalog.jsandsrc/routing/platform-transformers.js - Run all tests before committing:
npm run test:run