Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,23 @@ config. `main` is bundled with esbuild, as Wrangler does. The supported fields
are `name`, `main`, `compatibility_date`, `compatibility_flags`, `vars`,
`durable_objects`, `exports`, `migrations`, `no_bundle`, `$schema` and, with
`--experimental`, `kv_namespaces`, `r2_buckets`, `triggers.crons`, `queues`,
`workflows`, `d1_databases` and `assets`; any other field is an error. With
`assets`, `main` may be left out to serve files only, and then fields that need
a Worker are an error. Each KV namespace and each D1 database is one Durable
Object, so its throughput is that of one object; each R2 object is an object in
the store. A cluster's bucket should have a lifecycle rule that aborts incomplete
multipart uploads, or the parts of R2 uploads that never finish stay forever.
`workflows`, `d1_databases`, `secrets_store_secrets`, `ratelimits` and
`assets`; any other field is an error. With `assets`, `main` may be left out to
serve files only, and then fields that need a Worker are an error. Each KV
namespace and each D1 database is one Durable Object, so its throughput is that
of one object; each R2 object is an object in the store. A cluster's bucket
should have a lifecycle rule that aborts incomplete multipart uploads, or the
parts of R2 uploads that never finish stay forever. A Secrets Store secret's
value is the environment variable `WORKERD_SECRET_` plus its `secret_name`,
upper-cased, with other characters than letters and digits as `_` (`api-key` is
`WORKERD_SECRET_API_KEY`), read when the server starts and never written to
disk, so a name has one value in every store. A Worker can only `get()` it, as
on Cloudflare; wrangler's local `secrets-store secret` commands write
Miniflare's state and do not set it. Secrets Store secrets and rate limits are
called over JS RPC, so the Worker needs a compatibility date of 2024-04-03 or
later, or the `rpc` flag, and for secrets before 2024-03-26 also
`fetcher_no_get_put_delete`. Rate limits count in the server's memory, and a
restart forgets the counts.

A simple "Hello World!" config file might look like:

Expand Down
2 changes: 1 addition & 1 deletion src/workerd/server/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Bazel labels: `//src/workerd/server:workerd`, `//src/workerd/server:workerd-cli`
| `process.rs` | Executable discovery, the compiled-binary format, `Process` (the files to watch, re-exec) |
| `socket_fd.rs` | `--socket-fd` / `--control-fd` descriptors, made owned; the platform halves are `socket_fd/{unix,windows}.rs` |
| `watch.rs` | `--watch`: the file watcher |
| `wrangler.rs` | A `wrangler.json(c)` config: the supported fields translated into a `Config`, `main` bundled with esbuild (or scanned under `no_bundle`), or for `deploy` into a services-only deployment. `kv_namespaces` (with `--experimental`) become `kvNamespace` bindings to the Worker `<name>-kv`, which runs the bundled module `/workerd/kv.js` (`kv/kv.js`), plus disks for its objects and blobs; `r2_buckets` become `r2Bucket` bindings to `<name>-r2` (`r2/r2.js`), whose objects are in a local `bucket` store; `queues` become `queue` bindings to `<name>-queues` (`queues/queues.js`), whose consumers deliver to the Worker; `workflows` run on the engine `<name>-workflows` (`workflows/workflows.js`), the Worker's `workflowsEngine`; `d1_databases` become D1 bindings to `<name>-d1` (`d1/d1.js`); tests in `tests/kv/`, `tests/r2/`, `tests/queues/`, `tests/workflows/` and `tests/d1/` |
| `wrangler.rs` | A `wrangler.json(c)` config: the supported fields translated into a `Config`, `main` bundled with esbuild (or scanned under `no_bundle`), or for `deploy` into a services-only deployment. `kv_namespaces` (with `--experimental`) become `kvNamespace` bindings to the Worker `<name>-kv`, which runs the bundled module `/workerd/kv.js` (`kv/kv.js`), plus disks for its objects and blobs; `r2_buckets` become `r2Bucket` bindings to `<name>-r2` (`r2/r2.js`), whose objects are in a local `bucket` store; `queues` become `queue` bindings to `<name>-queues` (`queues/queues.js`), whose consumers deliver to the Worker; `workflows` run on the engine `<name>-workflows` (`workflows/workflows.js`), the Worker's `workflowsEngine`; `d1_databases` become D1 bindings to `<name>-d1` (`d1/d1.js`); `secrets_store_secrets` become service bindings to the `SecretsStoreSecret` entrypoint of `<name>-secrets-store` (`secrets-store/secrets-store.js`), values from the variables `WORKERD_SECRET_<NAME>`; `ratelimits` become service bindings to the `RateLimit` entrypoint of `<name>-ratelimit` (`ratelimit/ratelimit.js`), settings in props, which counts in memory; tests in `tests/kv/`, `tests/r2/`, `tests/queues/`, `tests/workflows/`, `tests/d1/`, `tests/secrets-store/` and `tests/ratelimit/` |
| `bridge.rs` | The cxx bridge (`workerd::server::cli`) to `config-compiler.h` and the build facts in `factory/bootstrap.h`; header `<workerd/server/cli/bridge.rs.h>` |

### `server/` (`workerd-server`)
Expand Down
5 changes: 5 additions & 0 deletions src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ wd_rust_crate(
cxx_bridge_src = "cli/bridge.rs",
link_deps = [":config-compiler"],
test_compile_data = ["//samples:wrangler/wrangler.jsonc"],
# Its tests run one at a time (RUST_TEST_THREADS=1); on Windows CI they took over the 15 s of
# "medium".
test_size = "large",
deps = [
":workerd_capnp_rust",
"//src/rust/cxx/kj-rs-io",
Expand Down Expand Up @@ -258,6 +261,8 @@ wd_cc_library(
"//src/workerd/server/kv:kv-js",
"//src/workerd/server/queues:queues-js",
"//src/workerd/server/r2:r2-js",
"//src/workerd/server/ratelimit:ratelimit-js",
"//src/workerd/server/secrets-store:secrets-store-js",
"//src/workerd/server/workflows:workflows-js",
"//src/workerd/util:perfetto",
"@capnp-cpp//src/kj/compat:kj-gzip",
Expand Down
214 changes: 214 additions & 0 deletions src/workerd/server/cli/wrangler-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,220 @@ fn d1_databases_bind_the_d1_worker() {
assert_eq!(bound, [d1("A", "id-a"), d1("B", "B"), d1("C", "p")]);
}

#[test]
fn secrets_store_secrets_bind_the_secrets_store_worker() {
let wrangler = r#"{ "name": "app", "main": "index.js", "compatibility_date": "2026-01-01",
"no_bundle": true, "secrets_store_secrets": [
{ "binding": "A", "store_id": "s1", "secret_name": "api-key" },
{ "binding": "B", "store_id": "s2", "secret_name": "api-key" },
{ "binding": "C", "store_id": "s1", "secret_name": "Db_2" }] }"#;
let dir = project(&[("wrangler.jsonc", wrangler), ("index.js", "")]);
let process = Process::new(None, None, Vec::new());
let path = dir.join("wrangler.jsonc");
let error = load(path.to_str().unwrap(), &process, false).err().unwrap();
assert!(error.contains("secrets_store_secrets"), "{error}");
let error = load_deployment(path.to_str().unwrap(), Some("app"), &process)
.err()
.unwrap();
assert!(error.contains("secrets_store_secrets"), "{error}");
let message = read(load(path.to_str().unwrap(), &process, true).unwrap());
let services = message
.get_root::<config::Reader>()
.unwrap()
.get_services()
.unwrap();
let names: Vec<_> = services
.iter()
.map(|s| s.get_name().unwrap().to_str().unwrap().to_owned())
.collect();
assert_eq!(names, ["app", "app-secrets-store"]);
assert!(!dir.join(".wrangler").exists());
let text = |t: capnp::text::Reader<'_>| t.to_str().unwrap().to_owned();
let bound = |i: u32| -> Vec<String> {
let service::Which::Worker(w) = services.get(i).which().unwrap() else {
panic!("not a worker");
};
(w.unwrap().get_bindings().unwrap().iter())
.map(|b| {
let name = text(b.get_name().unwrap());
let (target, props) = match b.which().unwrap() {
worker::binding::Which::Service(d) => {
let d = d.unwrap();
let entrypoint = text(d.get_entrypoint().unwrap());
(
format!("{} {entrypoint}", text(d.get_name().unwrap())),
d.get_props(),
)
}
worker::binding::Which::FromEnvironment(v) => {
return format!("{name} {}", text(v.unwrap()));
}
_ => panic!("unexpected binding"),
};
let service_designator::props::Which::Json(props) = props.which().unwrap() else {
panic!("no JSON props");
};
format!("{name} {target} {}", text(props.unwrap()))
})
.collect()
};
assert_eq!(
bound(0),
[
r#"A app-secrets-store SecretsStoreSecret {"secret_name":"api-key","store_id":"s1"}"#,
r#"B app-secrets-store SecretsStoreSecret {"secret_name":"api-key","store_id":"s2"}"#,
r#"C app-secrets-store SecretsStoreSecret {"secret_name":"Db_2","store_id":"s1"}"#,
]
);
assert_eq!(
bound(1),
[
"env:Db_2 WORKERD_SECRET_DB_2",
"env:api-key WORKERD_SECRET_API_KEY",
]
);
}

#[test]
fn secrets_store_secrets_cannot_share_a_variable() {
let error = load_project(&[
(
"wrangler.jsonc",
r#"{ "main": "index.js", "compatibility_date": "2026-01-01", "no_bundle": true,
"secrets_store_secrets": [
{ "binding": "A", "store_id": "s", "secret_name": "api-key" },
{ "binding": "B", "store_id": "s", "secret_name": "API_KEY" }] }"#,
),
("index.js", ""),
])
.err()
.unwrap();
assert!(error.contains("WORKERD_SECRET_API_KEY"), "{error}");
}

#[test]
fn bindings_of_every_kind_keep_their_places() {
let wrangler = r#"{ "name": "app", "main": "index.js", "compatibility_date": "2026-01-01",
"no_bundle": true, "vars": { "V": "v" },
"kv_namespaces": [{ "binding": "KV", "id": "kv" }],
"r2_buckets": [{ "binding": "R2", "bucket_name": "r2" }],
"d1_databases": [{ "binding": "D1", "database_id": "d1" }],
"secrets_store_secrets": [{ "binding": "S", "store_id": "s", "secret_name": "x" }],
"ratelimits": [{ "name": "L", "namespace_id": "1", "simple": { "limit": 1, "period": 10 } }],
"assets": { "directory": "public", "binding": "ASSETS" } }"#;
let config = load_project(&[
("wrangler.jsonc", wrangler),
("index.js", ""),
("public/a.txt", "a"),
])
.unwrap();
let message = read(config);
let services = message
.get_root::<config::Reader>()
.unwrap()
.get_services()
.unwrap();
let names: Vec<_> = services
.iter()
.map(|s| s.get_name().unwrap().to_str().unwrap().to_owned())
.collect();
assert_eq!(
names,
[
"app",
"app-kv",
"app-kv-storage",
"app-kv-blobs",
"app-r2",
"app-r2-storage",
"app-r2-blobs",
"app-secrets-store",
"app-d1",
"app-d1-storage",
"app-ratelimit",
"app-assets",
]
);
let service::Which::Worker(w) = services.get(0).which().unwrap() else {
panic!("not a worker");
};
let bound: Vec<_> = (w.unwrap().get_bindings().unwrap().iter())
.map(|b| b.get_name().unwrap().to_str().unwrap().to_owned())
.collect();
assert_eq!(bound, ["V", "KV", "R2", "D1", "S", "L", "ASSETS"]);
}

#[test]
fn ratelimits_bind_the_ratelimit_worker() {
let wrangler = r#"{ "name": "app", "main": "index.js", "compatibility_date": "2026-01-01",
"no_bundle": true, "ratelimits": [
{ "name": "A", "namespace_id": "1001", "simple": { "limit": 100, "period": 60 } },
{ "name": "B", "namespace_id": "1002", "simple": { "limit": 5, "period": 10 } }] }"#;
let dir = project(&[("wrangler.jsonc", wrangler), ("index.js", "")]);
let process = Process::new(None, None, Vec::new());
let path = dir.join("wrangler.jsonc");
let error = load(path.to_str().unwrap(), &process, false).err().unwrap();
assert!(error.contains("ratelimits"), "{error}");
let error = load_deployment(path.to_str().unwrap(), Some("app"), &process)
.err()
.unwrap();
assert!(error.contains("ratelimits"), "{error}");
let message = read(load(path.to_str().unwrap(), &process, true).unwrap());
let services = message
.get_root::<config::Reader>()
.unwrap()
.get_services()
.unwrap();
let names: Vec<_> = services
.iter()
.map(|s| s.get_name().unwrap().to_str().unwrap().to_owned())
.collect();
assert_eq!(names, ["app", "app-ratelimit"]);
let service::Which::Worker(w) = services.get(0).which().unwrap() else {
panic!("not a worker");
};
let text = |t: capnp::text::Reader<'_>| t.to_str().unwrap().to_owned();
let bound: Vec<_> = (w.unwrap().get_bindings().unwrap().iter())
.map(|b| {
let worker::binding::Which::Service(d) = b.which().unwrap() else {
panic!("not a service");
};
let d = d.unwrap();
let service_designator::props::Which::Json(props) = d.get_props().which().unwrap()
else {
panic!("no JSON props");
};
let name = text(b.get_name().unwrap());
let service = text(d.get_name().unwrap());
let entrypoint = text(d.get_entrypoint().unwrap());
format!("{name} {service} {entrypoint} {}", text(props.unwrap()))
})
.collect();
assert_eq!(
bound,
[
r#"A app-ratelimit RateLimit {"limit":100,"namespace":"1001","period":60}"#,
r#"B app-ratelimit RateLimit {"limit":5,"namespace":"1002","period":10}"#,
]
);
}

#[test]
fn ratelimits_period_is_10_or_60() {
let error = load_project(&[
(
"wrangler.jsonc",
r#"{ "main": "index.js", "compatibility_date": "2026-01-01", "no_bundle": true,
"ratelimits": [
{ "name": "A", "namespace_id": "1", "simple": { "limit": 1, "period": 30 } }] }"#,
),
("index.js", ""),
])
.err()
.unwrap();
assert!(error.contains("10 or 60"), "{error}");
}

#[test]
fn triggers_crons_require_experimental() {
let dir = project(&[
Expand Down
Loading
Loading