Overview
Cloudflare Workers provide:Edge Computing
Run code at the edge, close to users
Low Latency
Sub-millisecond response times
Serverless
No infrastructure management required
Scalability
Automatic scaling to handle traffic
Project Structure
Workers are located in thecloudflare-workers/ directory:
Wrangler Configuration
Thewrangler.jsonc file configures worker deployment:
wrangler.jsonc
Worker name displayed in Cloudflare dashboard
Entry point file for the worker code
Cloudflare Workers runtime version date
Enable observability features (logs, traces, metrics)
Public environment variables available to the worker
Backblaze B2 Proxy Worker
The Backblaze proxy worker provides secure, authenticated access to S3-compatible object storage (Backblaze B2) for downloading CS2 demo files.Worker Architecture
cloudflare-workers/backblaze-proxy/index.ts
Features
Usage
Request a file from the proxy worker:- Extracts the
fileparameter - Signs the request with S3 credentials
- Fetches from Backblaze B2
- Returns with download headers
Environment Variables
The worker requires these secrets (not inwrangler.jsonc):
Backblaze B2 application key ID
Backblaze B2 application key secret
Backblaze B2 S3 endpoint (e.g.,
s3.us-west-004.backblazeb2.com)S3 bucket name (defined in
wrangler.jsonc)Development
Prerequisites
Install Wrangler CLI:package.json:39.
Local Development
Run the worker locally:Testing Locally
Test the worker with curl:Setting Secrets Locally
Create a.dev.vars file for local secrets:
.dev.vars
Deployment
Authentication
Log in to Cloudflare:Setting Production Secrets
Set secrets for production:Deploying the Worker
Deploy to Cloudflare:Custom Domain
Add a custom route in the Cloudflare dashboard:- Go to Workers & Pages > 5stack > Settings > Triggers
- Add a route:
demos.5stack.gg/* - Select your zone:
5stack.gg
wrangler.jsonc:
Monitoring
Observability
Observability is enabled inwrangler.jsonc:10-12:
- Real-time logs
- Request traces
- Performance metrics
Viewing Logs
Stream worker logs:Cloudflare Dashboard
View metrics in the Cloudflare dashboard:- Requests: Total requests over time
- Errors: Error rate and types
- Latency: Response time percentiles
- CPU Time: Worker execution time
Advanced Configuration
Smart Placement
Enable smart placement for optimal performance:wrangler.jsonc
Service Bindings
Connect multiple workers:KV Storage
Add key-value storage:Durable Objects
Add stateful coordination:Dependencies
The worker uses theaws4fetch package for AWS request signing:
package.json
package.json
Troubleshooting
401 Unauthorized from Backblaze
401 Unauthorized from Backblaze
403 Forbidden from Backblaze
403 Forbidden from Backblaze
Common causes:
- File doesn’t exist
- Incorrect bucket name
- Wrong endpoint
- Verify file exists in Backblaze bucket
- Check
BUCKET_NAMEmatches your bucket - Ensure
S3_ENDPOINTis correct for your region
Worker exceeds CPU limit
Worker exceeds CPU limit
Workers have CPU time limits. Optimize by:
- Minimizing request processing
- Using streaming responses for large files
- Caching results when possible
Deployment fails
Deployment fails
Common issues:
- Not logged in: Run
yarn wrangler login - Invalid configuration: Check
wrangler.jsoncsyntax - Missing dependencies: Run
yarn install
Security Best Practices
-
Never expose secrets in code
- Use
wrangler secret putfor sensitive values - Don’t include secrets in
wrangler.jsonc
- Use
-
Validate all inputs
- Check file parameter exists
- Sanitize file paths to prevent directory traversal
-
Limit allowed methods
- Only allow necessary HTTP methods (GET, HEAD)
-
Use CORS headers carefully
- Restrict origins in production
- Don’t use wildcard
*for credentials
-
Implement rate limiting
- Use Cloudflare rate limiting rules
- Prevent abuse and excessive costs
Cost Optimization
Cloudflare Workers pricing:- Free tier: 100,000 requests/day
- Paid plan: $5/month for 10 million requests
- Additional requests: $0.50 per million
- Cache responses - Use Cloudflare cache API
- Minimize CPU time - Optimize code execution
- Use HEAD requests - Check file existence without downloading
- Implement compression - Reduce bandwidth usage
Next Steps
Wrangler Documentation
Official Wrangler CLI documentation
Workers Runtime
Cloudflare Workers runtime APIs
Environment Variables
Configure environment variables
Docker Deployment
Deploy the main application