iterate.com / cloud

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

cloud / internal-packages / installer / src / update-consent.tsmain at aacbc74

// update-consent.ts — WHAT AN UPDATE FROM A DEPLOYMENT'S CARD LEAVES OFF that a mind made today
// has, and the consent that turns it on (Deployments.tsx `AskUpdate`). The wizard's mind is born
// with telemetry and with its own Cloudflare connection (Wizard.tsx `authorizeHref`). An older
// mind has neither, and an update turns each on only when the person's access to Cloudflare came
// from a consent for it (worker/index.js `telemetry`, `cloudflareToken`; container/run.sh
// `stack_updatable`). So the card offers that consent before the update, and the update without it.

/** What a mind made today has and an older one may not. */
export type Default = "telemetry" | "cloudflare";

/** The consent for both: the wizard's full consent. Either one asks the scope that makes API
 *  tokens (worker/scopes.js `TELEMETRY_SCOPE`), which Cloudflare's page leaves for the person to
 *  tick. A new consent replaces the access before it, so it asks for both whichever is missing. */
export const BOTH_CONSENT = "/connect?level=full&telemetry=1&cloudflare=1";

/** What an update with this access leaves off: telemetry when the stack has none and the access
 *  may not turn it on, and the mind's Cloudflare connection likewise, or its narrowing when its
 *  token is older. Empty: nothing. */
export function leftOffOf({
  telemetry,
  connected,
  older = false,
  mayTurnOnTelemetry,
  mayConnectCloudflare,
}: {
  /** The stack has its telemetry Worker. */
  telemetry: boolean;
  /** The mind has its Cloudflare connection, by this Cloud's last run of it. */
  connected: boolean;
  /** Its token is of an older release (worker/scopes.js `CLOUDFLARE_TOKEN_EDITION`): an update
   *  with this access keeps it as it is, and a mind of this release does not use it, so the
   *  connection counts as left off. */
  older?: boolean;
  /** The person's access came from a consent for telemetry (GET /api/me `telemetry`). */
  mayTurnOnTelemetry: boolean;
  /** The person's access came from a consent for the connection (GET /api/me `cloudflareToken`). */
  mayConnectCloudflare: boolean;
}): Default[] {
  return [
    ...(!telemetry && !mayTurnOnTelemetry ? (["telemetry"] as const) : []),
    ...((!connected || older) && !mayConnectCloudflare ? (["cloudflare"] as const) : []),
  ];
}