Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/workerd/configuration.md
1# Workerd Configuration23## Basic Structure4```capnp5using Workerd = import "/workerd/workerd.capnp";67const config :Workerd.Config = (8services = [(name = "main", worker = .mainWorker)],9sockets = [(name = "http", address = "*:8080", http = (), service = "main")]10);1112const mainWorker :Workerd.Worker = (13modules = [(name = "index.js", esModule = embed "src/index.js")],14compatibilityDate = "2024-01-15",15bindings = [...]16);17```1819## Services20**Worker**: Run JS/Wasm code21```capnp22(name = "api", worker = (23modules = [(name = "index.js", esModule = embed "index.js")],24compatibilityDate = "2024-01-15",25bindings = [...]26))27```2829**Network**: Internet access30```capnp31(name = "internet", network = (allow = ["public"], tlsOptions = (trustBrowserCas = true)))32```3334**External**: Reverse proxy35```capnp36(name = "backend", external = (address = "api.com:443", http = (style = tls)))37```3839**Disk**: Static files40```capnp41(name = "assets", disk = (path = "/var/www", writable = false))42```4344## Sockets45```capnp46(name = "http", address = "*:8080", http = (), service = "main")47(name = "https", address = "*:443", https = (options = (), tlsOptions = (keypair = (...))), service = "main")48(name = "app", address = "unix:/tmp/app.sock", http = (), service = "main")49```5051## Worker Formats52```capnp53# ES Modules (recommended)54modules = [(name = "index.js", esModule = embed "src/index.js"), (name = "wasm.wasm", wasm = embed "build/module.wasm")]5556# Service Worker (legacy)57serviceWorkerScript = embed "worker.js"5859# CommonJS60(name = "legacy.js", commonJsModule = embed "legacy.js", namedExports = ["foo"])61```6263## Bindings64Bindings expose resources to workers. ES modules: `env.BINDING`, Service workers: globals.6566### Primitive Types67```capnp68(name = "API_KEY", text = "secret") # String69(name = "CONFIG", json = '{"key":"val"}') # Parsed JSON70(name = "DATA", data = embed "data.bin") # ArrayBuffer71(name = "DATABASE_URL", fromEnvironment = "DB_URL") # System env var72```7374### Service Binding75```capnp76(name = "AUTH", service = "auth-worker") # Basic77(name = "API", service = (78name = "backend",79entrypoint = "adminApi", # Named export80props = (json = '{"role":"admin"}') # ctx.props81))82```8384### Storage85```capnp86(name = "CACHE", kvNamespace = "kv-service") # KV87(name = "STORAGE", r2Bucket = "r2-service") # R288(name = "ROOMS", durableObjectNamespace = (89serviceName = "room-service",90className = "Room"91))92(name = "FAST", memoryCache = (93id = "cache-id",94limits = (maxKeys = 1000, maxValueSize = 1048576)95))96```9798### Other99```capnp100(name = "TASKS", queue = "queue-service")101(name = "ANALYTICS", analyticsEngine = "analytics")102(name = "LOADER", workerLoader = (id = "dynamic"))103(name = "KEY", cryptoKey = (format = raw, algorithm = (name = "HMAC", hash = "SHA-256"), keyData = embed "key.bin", usages = [sign, verify], extractable = false))104(name = "TRACED", wrapped = (moduleName = "tracing", entrypoint = "makeTracer", innerBindings = [(name = "backend", service = "backend")]))105```106107## Compatibility108```capnp109compatibilityDate = "2024-01-15" # Always set!110compatibilityFlags = ["nodejs_compat", "streams_enable_constructors"]111```112113Version = max compat date. Update carefully after testing.114115## Parameter Bindings (Inheritance)116```capnp117const base :Workerd.Worker = (118modules = [...], compatibilityDate = "2024-01-15",119bindings = [(name = "API_URL", parameter = (type = text)), (name = "DB", parameter = (type = service))]120);121122const derived :Workerd.Worker = (123inherit = "base-service",124bindings = [(name = "API_URL", text = "https://api.com"), (name = "DB", service = "postgres")]125);126```127128## Durable Objects Config129```capnp130const worker :Workerd.Worker = (131modules = [...],132compatibilityDate = "2024-01-15",133bindings = [(name = "ROOMS", durableObjectNamespace = "Room")],134durableObjectNamespaces = [(className = "Room", uniqueKey = "v1")],135durableObjectStorage = (localDisk = "/var/do")136);137```138139## Remote Bindings (Development)140141Connect local workerd to production Cloudflare resources:142143```capnp144bindings = [145# Remote KV (requires API token)146(name = "PROD_KV", kvNamespace = (147remote = (148accountId = "your-account-id",149namespaceId = "your-namespace-id",150apiToken = .envVar("CF_API_TOKEN")151)152)),153154# Remote R2155(name = "PROD_R2", r2Bucket = (156remote = (157accountId = "your-account-id",158bucketName = "my-bucket",159apiToken = .envVar("CF_API_TOKEN")160)161)),162163# Remote Durable Object164(name = "PROD_DO", durableObjectNamespace = (165remote = (166accountId = "your-account-id",167scriptName = "my-worker",168className = "MyDO",169apiToken = .envVar("CF_API_TOKEN")170)171))172]173```174175**Note:** Remote bindings require network access and valid Cloudflare API credentials.176177## Logging & Debugging178```capnp179logging = (structuredLogging = true, stdoutPrefix = "OUT: ", stderrPrefix = "ERR: ")180v8Flags = ["--expose-gc", "--max-old-space-size=2048"] # ⚠️ Unsupported in production181```182183See [patterns.md](./patterns.md) for multi-service examples, [gotchas.md](./gotchas.md) for config errors.184