Skip to content
Open
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
21 changes: 21 additions & 0 deletions .changeset/container-environment-records.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"workers-rs": patch
---

Serialize `Container::start` and `Container::exec` options as plain JavaScript
objects, converting their environment maps to `Record<string, string>` at the
binding boundary without changing the Rust options API. Preserve explicitly
set startup fields and serialize startup labels as a record as well.

Previously the calls passed wasm-bindgen-exported Rust option classes whose
`env` getters returned JavaScript `Map` instances. The native Container API
expects record properties and did not pass those entries to the process.
The unused startup object conversion also incorrectly removed populated
entrypoint, Internet, and environment fields.

Add WASM record-shape tests for startup and exec options.

Source: <https://developers.cloudflare.com/containers/api/durable-object-container/#start>
and <https://developers.cloudflare.com/containers/api/durable-object-container/#exec>,
checked 2026-11-12. Both environment options are `Record<string, string>`;
exec inherits only PATH from startup, not the other startup variables.
29 changes: 29 additions & 0 deletions .changeset/durable-object-container-exec-and-sizing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
"workers-rs": minor
---

Add `durable_object`-scheduling-policy `Container::start` options
(`image`, `instance`, `container_snapshot`, `labels`) and a
`Container::exec`/`ExecProcess`/`ExecOutput` binding for running and reading
the exit status of a process inside an already-running container.

`ContainerStartupOptions` previously exposed only `entrypoint`,
`enable_internet`, and `env`, which are the fields the `default` scheduling
policy's `start()` call accepts. The `durable_object` scheduling policy's
`ctx.container.start()` additionally accepts `image` (required unless
`containerSnapshot` is set), `instance` (named size or custom
`{ vcpu, memoryMib, diskMb}`), `containerSnapshot`, and `labels`. These are
now available via `ContainerStartupOptions::set_image`,
`set_instance`/`set_custom_instance`, `set_container_snapshot`, and
`add_label`.

`Container` previously had no binding for `exec()`, so a Rust Worker could
start a container and `wait_for_exit()` but could not run an additional
process inside it or read a granular exit code without destroying the whole
container. `Container::exec(cmd, options)` now returns an `ExecProcess` with
`pid()`, `exit_code()`, `output()` (an `ExecOutput` with `exit_code()`,
`stdout()`, `stderr()`), and `kill()`.

Source: <https://developers.cloudflare.com/containers/api/durable-object-container/>
and <https://developers.cloudflare.com/containers/configuration/scheduling-policy/>,
checked 2026-10-02.
61 changes: 60 additions & 1 deletion worker-sys/src/types/durable_object/container.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use js_sys::{Object, Promise};
use js_sys::{ArrayBuffer, Object, Promise};
use wasm_bindgen::prelude::*;

use crate::Fetcher;
Expand All @@ -11,12 +11,30 @@ use crate::Fetcher;
/// destroy(error?: any): Promise<void>;
/// signal(signo: number): void;
/// getTcpPort(port: number): Fetcher;
/// exec(cmd: string[], options?: ContainerExecOptions): Promise<ExecProcess>;
/// }
///
/// interface ContainerStartupOptions {
/// entrypoint?: string[];
/// enableInternet: boolean;
/// env?: Record<string, string>;
/// // `durable_object` scheduling policy only.
/// image?: string;
/// instance?: string | { vcpu: number; memoryMib: number; diskMb: number };
/// containerSnapshot?: ContainerSnapshotRestoreParams;
/// labels?: Record<string, string>;
/// }
///
/// interface ExecProcess {
/// readonly pid: number;
/// readonly exitCode: Promise<number>;
/// output(): Promise<ExecOutput>;
/// }
///
/// interface ExecOutput {
/// readonly exitCode: number;
/// readonly stdout: ArrayBuffer;
/// readonly stderr: ArrayBuffer;
/// }
/// ```

Expand All @@ -43,4 +61,45 @@ extern "C" {

#[wasm_bindgen(method, catch, js_name=getTcpPort)]
pub fn get_tcp_port(this: &Container, port: u16) -> Result<Fetcher, JsValue>;

/// Starts another process inside an already-running container. `cmd` is the
/// executable followed by its arguments; `options` is a `ContainerExecOptions`
/// object (or `undefined`).
#[wasm_bindgen(method, catch)]
pub fn exec(this: &Container, cmd: Vec<JsValue>, options: &JsValue)
-> Result<Promise, JsValue>;

#[wasm_bindgen(extends=Object)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub type ExecProcess;

#[wasm_bindgen(method, getter, js_name=pid)]
pub fn pid(this: &ExecProcess) -> u32;

/// Resolves with the process's exit code when it exits. Nonzero codes
/// resolve normally instead of rejecting.
#[wasm_bindgen(method, getter, js_name=exitCode)]
pub fn exit_code(this: &ExecProcess) -> Promise;

/// Reads buffered output once. Throws (via the rejected `Promise`) when
/// called more than once or after a readable stream has been consumed.
#[wasm_bindgen(method, catch)]
pub fn output(this: &ExecProcess) -> Result<Promise, JsValue>;

/// Queues a signal for the process; default is `SIGTERM` (15).
#[wasm_bindgen(method, catch)]
pub fn kill(this: &ExecProcess, signal: Option<i32>) -> Result<(), JsValue>;

#[wasm_bindgen(extends=Object)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub type ExecOutput;

#[wasm_bindgen(method, getter, js_name=exitCode)]
pub fn exit_code(this: &ExecOutput) -> u32;

#[wasm_bindgen(method, getter, js_name=stdout)]
pub fn stdout(this: &ExecOutput) -> ArrayBuffer;

#[wasm_bindgen(method, getter, js_name=stderr)]
pub fn stderr(this: &ExecOutput) -> ArrayBuffer;
}
Loading