sailfork / t3code-github / .repos/ alchemy-effect/ examples/ cloudflare-worker/ src/ SecondaryApi.ts

import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import Agent from "./Agent.ts";

// A second Worker that binds the same `Agent` Durable Object as `Api`. Each
// `yield* Agent` runs the DO's outer init, which calls
// `Cloudflare.Container(Sandbox)` and pushes a binding onto the Sandbox
// ContainerApplication carrying the Agent namespace. With two Workers binding
// the same DO, the Sandbox ends up with two bindings that share a single
// `namespaceId` — the regression case for the dedupe fix in this PR.
export class SecondaryApi extends Cloudflare.Worker<SecondaryApi, {}>()("SecondaryApi") {}

export default SecondaryApi.make(
  {
    main: import.meta.url,
    observability: { enabled: true },
  },
  Effect.gen(function* () {
    const agents = yield* Agent;

    return {
      fetch: Effect.gen(function* () {
        const body = yield* agents.getByName("sandbox-test").hello().pipe(Effect.orDie);
        return HttpServerResponse.text(body);
      }),
    };
  }),
);