iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / scripts / zone-enrollment / zone-resources.tsmain at aacbc74

// zone-resources.ts — WHAT A MIND'S STACK DECLARES FOR ONE ZONE, apart from the stack: the
// resources of the loop "each zone" in core/alchemy/src/stack.ts, with the same logical ids,
// types and props. The stack's own boundary Worker and events queue are given here by name and
// id: over a mind that is deployed, both are there already and the plan leaves them as they are.
// zone-enrollment.test.ts fails when this file and the stack's loop differ.
import * as Cloudflare from "@alchemy.run/cloudflare";
import { adopt } from "alchemy/AdoptPolicy";
import * as Output from "alchemy/Output";
import * as Effect from "effect/Effect";

export type ZoneInput = {
  zoneId: string;
  zoneName: string;
  /** the boundary Worker's name, `iterate-<stage>-boundary` */
  boundaryWorkerName: string;
  /** the id of the stack's queue of Cloudflare's events */
  cloudflareEventsQueueId: string;
  /** false: the stack as it was until 25 Nov 2026, which refused a zone whose Email Sending
   *  domain was there already. */
  takeOverSending?: boolean;
  /** true: the stack as it was until 25 Nov 2026, which turned Email Routing on before it set
   *  the catch-all. */
  routingFirst?: boolean;
};

export const zoneResources = (input: ZoneInput) =>
  Effect.gen(function* () {
    const { zoneId, zoneName, boundaryWorkerName } = input;
    for (const [id, hostname] of [
      ["Apex", zoneName],
      ["Wildcard", `*.${zoneName}`],
    ] as const) {
      yield* Cloudflare.DNS.Record(`${id}Record-${zoneName}`, {
        zoneId,
        name: hostname,
        type: "AAAA",
        content: "100::",
        proxied: true,
      });
      yield* Cloudflare.Workers.WorkerRoute(`${id}Route-${zoneName}`, {
        zoneId,
        pattern: `${hostname}/*`,
        script: boundaryWorkerName,
      });
    }
    if (input.routingFirst) {
      const routing = yield* Cloudflare.Email.Routing(`EmailRouting-${zoneName}`, { zone: zoneId });
      yield* Cloudflare.Email.CatchAll(`EmailCatchAll-${zoneName}`, {
        zone: routing.zoneId,
        actions: [{ type: "worker", value: [boundaryWorkerName] }],
      });
    } else {
      const catchAll = yield* Cloudflare.Email.CatchAll(`EmailCatchAll-${zoneName}`, {
        zone: zoneId,
        actions: [{ type: "worker", value: [boundaryWorkerName] }],
      });
      yield* Cloudflare.Email.Routing(`EmailRouting-${zoneName}`, { zone: catchAll.zoneId });
    }
    const sending = yield* Cloudflare.Email.SendingSubdomain(`EmailSending-${zoneName}`, {
      zoneId,
      name: zoneName,
    }).pipe(input.takeOverSending === false ? (declared) => declared : adopt(true));
    yield* Cloudflare.Queues.Subscription(`MailEvents-${zoneName}`, {
      source: {
        type: "email.sending",
        zoneId: sending.zoneId,
        domain: sending.enabled.pipe(
          Output.mapEffect((enabled) =>
            enabled
              ? Effect.succeed(zoneName)
              : Effect.die(`Email Sending is not enabled on ${zoneName} yet`),
          ),
        ),
      },
      events: ["message.bounced", "message.failed", "message.rejected", "message.complained"],
      queueId: input.cloudflareEventsQueueId,
    });
  });