import { env } from "cloudflare:workers";
import type { EditorContainer } from "./container-do";
/** Containers are opt-in per deployment (EDITOR_CONTAINERS=on) and then per session, by the user. */
export function containersEnabled(): boolean {
return env.EDITOR_CONTAINERS === "on";
}
function positive(raw: string | undefined, fallback: number): number {
const value = Number(raw);
return Number.isFinite(value) && value > 0 ? value : fallback;
}
/** Session limits. The monthly allowance comes from the account's metering plan (compute.editor). */
export function containerLimits() {
return {
idleMinutes: positive(env.EDITOR_CONTAINER_IDLE_MINUTES, 15),
capHours: positive(env.EDITOR_CONTAINER_CAP_HOURS, 8),
/** Concurrent containers one account may run; keep well under the namespace's max_instances. */
perAccount: positive(env.EDITOR_CONTAINERS_PER_ACCOUNT, 3),
};
}
/** Containers are per (account, repo, branch): co-editors each get their own, billed to themselves. */
export function containerStub(accountId: string, repoId: string, branch: string): DurableObjectStub<EditorContainer> {
return env.EDITOR_CONTAINER.getByName(`${accountId}:${repoId}:${branch}`);
}