Skip to main content

bynk_emit/emitter/
containers.rs

1//! #3477 (slice C1): the Durable Object classes of a Worker's `Container`
2//! instances.
3//!
4//! One file per Worker, `containers.ts`, re-exported from `index.ts` because
5//! Cloudflare resolves a `class_name` against the exports of the Worker's
6//! `main`. Each class is built by the runtime's `containerClass` from the
7//! declaration's literals, so the generated file holds data and no logic, as
8//! the agent classes' `serialiseAgentClass` wrapper does.
9
10use bynk_check::worker_bindings::BindingSource;
11use bynk_ts::{TsDecl, TsExpr, TsLit, TsProgram, TsStmt};
12
13use crate::emitter::bindings::WorkerBinding;
14
15/// The Worker-relative specifier the runtime is imported from: the same depth
16/// as `compose.ts` (`workers/<worker>/containers.ts`).
17const RUNTIME_SPECIFIER: &str = "../../runtime.js";
18
19/// The classes `index.ts` re-exports, one per container binding, in binding
20/// order. Empty when the Worker has no container.
21pub fn container_class_names(bindings: &[WorkerBinding]) -> Vec<String> {
22    bindings
23        .iter()
24        .filter_map(|b| {
25            let BindingSource::ContainerInstance { class_name, .. } = &b.source else {
26                return None;
27            };
28            Some(class_name.clone())
29        })
30        .collect()
31}
32
33/// `containers.ts`, or `None` when the Worker has no container.
34pub fn emit_containers_ts(bindings: &[WorkerBinding]) -> Option<TsProgram> {
35    let containers: Vec<&BindingSource> = bindings
36        .iter()
37        .map(|b| &b.source)
38        .filter(|s| matches!(s, BindingSource::ContainerInstance { .. }))
39        .collect();
40    if containers.is_empty() {
41        return None;
42    }
43    let mut program = TsProgram::new();
44    program.push(TsStmt::comment(
45        "Generated by bynk — do not edit by hand.",
46        None,
47    ));
48    program.push(TsStmt::comment(
49        "Durable Object classes of the Worker's containers (#3477).",
50        None,
51    ));
52    for stmt in crate::emitter::runtime_files::runtime_import_stmts(
53        false,
54        &["__containerClass".to_string()],
55        RUNTIME_SPECIFIER,
56    ) {
57        program.push(stmt);
58    }
59    for source in containers {
60        let BindingSource::ContainerInstance {
61            capability,
62            class_name,
63            port,
64            sleep_after,
65            instance_type,
66            forward,
67            ..
68        } = source
69        else {
70            continue;
71        };
72        let mut settings = vec![
73            (
74                "image".to_string(),
75                TsExpr::Lit(TsLit::Str(capability.clone())),
76            ),
77            (
78                "port".to_string(),
79                TsExpr::Lit(TsLit::Num(port.to_string())),
80            ),
81        ];
82        if let Some(seconds) = sleep_after {
83            settings.push((
84                "sleepAfterSeconds".to_string(),
85                TsExpr::Lit(TsLit::Num(seconds.to_string())),
86            ));
87        }
88        if let Some(instance) = instance_type {
89            settings.push((
90                "instanceType".to_string(),
91                TsExpr::Lit(TsLit::Str(instance.clone())),
92            ));
93        }
94        settings.push((
95            "forward".to_string(),
96            TsExpr::array(
97                forward
98                    .iter()
99                    .map(|name| TsExpr::Lit(TsLit::Str(name.clone())))
100                    .collect(),
101            ),
102        ));
103        program.push(TsStmt::decl(
104            TsDecl::Export(Box::new(TsDecl::ConstDecl {
105                name: class_name.clone(),
106                ty: None,
107                init: TsExpr::Call {
108                    callee: Box::new(TsExpr::Ident("__containerClass".to_string())),
109                    args: vec![TsExpr::object(settings)],
110                },
111            })),
112            None,
113        ));
114    }
115    Some(program)
116}