iterate.com / cloud

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

cloud / core / lib / src / cli / zone-protocols.tsmain at aacbc74

// zone-protocols.ts — THE TWO PROTOCOL SETTINGS THAT `iterate deploy` LEAVES ON EACH ZONE OF A
// MIND: HTTP/3 on, and 0-RTT Connection Resumption off. With 0-RTT, a browser sends a request in
// the TLS handshake's early data, and whoever recorded that data can send the request again: a
// mind answers GET links that work once. Every run reads both settings and writes the ones that
// differ, so a setting that somebody changed on Cloudflare's page is put back at the next deploy.
// They are no resource of the stack (core/alchemy): Alchemy plans nothing for a resource whose
// input did not change, so it would not see such a change.
import { z } from "zod";
import { resultOf, type Cloudflare } from "./cloudflare-access.ts";

/** Each setting by its id in Cloudflare's API (`/zones/<id>/settings/<setting>`), the name on
 *  Cloudflare's page (Speed, Settings, Protocol Optimization), and the value that a deploy
 *  leaves. */
export const ZONE_PROTOCOLS = [
  { id: "http3", name: "HTTP/3", value: "on" },
  { id: "0rtt", name: "0-RTT Connection Resumption", value: "off" },
] as const;

/** A setting of a zone that is not what a deploy leaves. */
export type ProtocolChange = {
  zone: string;
  path: string;
  name: string;
  from: string;
  to: string;
};

/** WHAT A DEPLOY WOULD CHANGE: the settings of `zones` that differ from `ZONE_PROTOCOLS`. Reads
 *  only. An answer that does not say a setting's value is no fact about it, so it stops the run. */
export async function zoneProtocolChanges(
  cf: Cloudflare,
  account: string,
  zones: readonly string[],
): Promise<ProtocolChange[]> {
  const changes: ProtocolChange[] = [];
  for (const zone of zones) {
    const listed = await cf("GET", `/zones?name=${zone}&account.id=${account}`);
    const id = resultOf(z.array(z.object({ id: z.string() })), listed)?.[0]?.id;
    if (!id)
      throw new Error(
        `Cloudflare did not answer with the zone ${zone} of the account ${account} (${listed.status}${listed.error && `: ${listed.error}`}). Nothing was changed. Run the command again.`,
      );
    for (const setting of ZONE_PROTOCOLS) {
      const path = `/zones/${id}/settings/${setting.id}`;
      const answer = await cf("GET", path);
      const read = resultOf(z.object({ value: z.string() }), answer);
      if (answer.status !== 200 || !read)
        throw new Error(
          `Cloudflare did not say whether ${setting.name} is on for ${zone} (${answer.status}${answer.error && `: ${answer.error}`}). Nothing was changed. Run the command again.`,
        );
      if (read.value !== setting.value)
        changes.push({ zone, path, name: setting.name, from: read.value, to: setting.value });
    }
  }
  return changes;
}

/** Writes them, and stops at the first that Cloudflare refuses or answers with another value. */
export async function setZoneProtocols(cf: Cloudflare, changes: readonly ProtocolChange[]) {
  for (const change of changes) {
    const answer = await cf("PATCH", change.path, { value: change.to });
    const now = resultOf(z.object({ value: z.string() }), answer)?.value;
    if (answer.status !== 200 || now !== change.to)
      throw new Error(
        `Cloudflare did not turn ${change.name} ${change.to} for ${change.zone} (${answer.status}${answer.error && `: ${answer.error}`}). The deploy did not start. Run the command again, or turn it ${change.to} on Cloudflare's page for ${change.zone}: Speed, Settings, Protocol Optimization.`,
      );
  }
}

/** One line for a person: what was changed, or what a deploy would change (`done` false). */
export const protocolChangeLine = (change: ProtocolChange, done: boolean) =>
  done
    ? `Turned ${change.name} ${change.to} for ${change.zone} (it was ${change.from}).`
    : `A deploy turns ${change.name} ${change.to} for ${change.zone} (it is ${change.from}).`;