1import type * as Cloudflare from "@/Cloudflare";
2import { Container, getContainer } from "@cloudflare/containers";
3import type { InferredClassWorker } from "./stack.ts";
4
5/**
6 * Container-backed Durable Object class hosted by a plain async Worker. The
7 * class is named after the env key, so the stack omits `className` — the
8 * shape the Containers guide documents and the one issue #1321 reported.
9 */
10export class Probe extends Container {
11 defaultPort = 8080;
12}
13
14// InferEnv maps the Container binding to DurableObjectNamespace<Probe>, which
15// is what `getContainer` expects — a lost binding is a type error here too.
16type Env = Cloudflare.InferEnv<typeof InferredClassWorker>;
17
18export default {
19 async fetch(request: Request, env: Env) {
20 const url = new URL(request.url);
21 // What `env.Probe` actually IS at runtime: a namespace has `idFromName`;
22 // a Worker that lost the binding echoes the Container declaration it
23 // uploaded as json instead (`{"_id":"Effect","op":"alchemy/EffectClass"}`).
24 if (url.pathname === "/binding") {
25 const binding = env.Probe as unknown;
26 return Response.json({
27 kind:
28 typeof (binding as { idFromName?: unknown } | null)?.idFromName ===
29 "function"
30 ? "durable_object_namespace"
31 : JSON.stringify(binding),
32 });
33 }
34 if (url.pathname === "/hello") {
35 return getContainer(env.Probe, "default").fetch(request);
36 }
37 return new Response("ok");
38 },
39};