Cotton code
80 lines · 3.1 KB · TypeScriptRaw
/** * Space for photos and files: the launch promises (assets/design/shared/SPACE.md, 23 Nov 2026). * * 2 GB free; each friend who joins adds 1 GB to both, counted once the friend has used an app on 14 separate days; * 2 TB at most; a full space refuses new files only (`space_full`), never a row. Behind `SPACE_RULES` (off | count | * on), off by default: today's cap and nothing new written. An account may be set apart by the admin. A GB here is * 1024³ bytes, so a promise is never short. */import type { PlanState } from "./plans.ts";export type SpaceMode = "off" | "count" | "on";export const GB = 1024 ** 3;export const FREE_BYTES = 2 * GB;export const FRIEND_BYTES = 1 * GB;export const MOST_BYTES = 2 * 1024 * GB;/** Days of use before a friend counts. */export const DAYS_TO_COUNT = 14;/** How long a new account may name who invited it. */export const NAME_INVITER_MS = 30 * 86_400_000;/** From this fraction of the allowance a sync pass says how full the space is. */export const SPACE_WARN_FRACTION = 0.8;/** The object's meta keys. */export const META = {  mode: "space_mode",  floor: "space_floor",  bought: "space_bought",  friends: "space_friends",  days: "space_days",  dayLast: "space_day_last",  told: "space_days14_told",  plan: "space_plan",} as const;export const SPACE_FULL = "space_full";/** The refusal's words (a draft for the owner). */export const SPACE_FULL_WORDS = "Your space for photos and files is full. This one wasn't saved to your account. Everything you already have is safe, and your writing keeps syncing.";/** A switch's value read as a mode; anything else is off. */export function modeOf(value: string | null | undefined): SpaceMode {  return value === "on" || value === "count" ? value : "off";}/** The account's mode: its own when the admin set one, else the Worker's switch. */export function accountMode(own: string | null, worker: string | undefined): SpaceMode {  return own !== null ? modeOf(own) : modeOf(worker);}/** What the account may hold under the rules: 2 GB and 1 GB a friend, or what was bought, never above 2 TB, and never below what it held when the rules came on. */export function allowance(friends: number, bought: number, floor: number): number {  const rule = FREE_BYTES + Math.max(0, Math.floor(friends)) * FRIEND_BYTES;  return Math.max(Math.min(MOST_BYTES, Math.max(rule, bought)), floor);}/** The UTC day of `at`, as `YYYY-MM-DD`. */export function dayOf(at: Date): string {  return at.toISOString().slice(0, 10);}/** One more day of use counted, when `today` is a day not yet counted and fewer than 14 are; the new count, or null when nothing changes. */export function dayCounted(last: string | null, days: number, today: string): number | null {  if (days >= DAYS_TO_COUNT || last === today) return null;  return days + 1;}/** The space as the owner reads it (`GET /v1/space`). */export interface SpaceAnswer {  mode: SpaceMode;  bytes: number;  allowance: number;  free: number;  friends: number;  counting: { days: number; of: number };  bought: number;  floor: number;  /** The plan he holds (src/plans.ts), as it stands; null with none. */  plan: PlanState | null;}