117 lines · 4.6 KB
import { env } from "cloudflare:workers";
import { MAX_IN_LIST, chunks, placeholders } from "../db";
import { accountDb } from "../scoped-db";
import { HttpError } from "../errors";
import { numberVar } from "../limits";
import { randomId } from "../crypto";
import { recordUsageSoon } from "../metering/record";
import { planBlockError } from "../metering/quota";
import { checkLfsThreshold } from "../notify/quota";

const GIB = 1024 * 1024 * 1024;
const MIB = 1024 * 1024;
const HTTP_INSUFFICIENT_STORAGE = 507;
const HTTP_TOO_MANY = 429;

export type LfsUsage = {
  storageBytes: number;
  storageLimit: number;
  bandwidthBytes: number;
  bandwidthLimit: number;
  period: string;
};

type UsageRow = { storage_bytes: number; bandwidth_period: string; bandwidth_bytes: number };

export function maxObjectBytes(): number {
  return numberVar(env.LFS_MAX_OBJECT_BYTES, 100 * MIB);
}

function storageLimit(): number {
  return numberVar(env.LFS_STORAGE_BYTES_PER_OWNER, 10 * GIB);
}

function bandwidthLimit(): number {
  return numberVar(env.LFS_BANDWIDTH_BYTES_PER_MONTH, 10 * GIB);
}

/** Bandwidth resets each calendar month (UTC). */
function currentPeriod(now = new Date()): string {
  return now.toISOString().slice(0, 7);