import { env, waitUntil } from "cloudflare:workers";
import { handlePatchRoutes } from "./patches";
import { addCounter } from "./counters";
import { safeDecode } from "~/lib/safe-decode";
import { attachmentDisposition } from "~/lib/content-disposition";
import { meteredDownload } from "./metering/git";
import { paymentRequiredResponse, planBlock } from "./metering/quota";
import { openArtifactsRepo } from "./git/artifacts";
import { artifactsCall } from "./git/artifacts-call";
import { hasPermission, loadRepo, loadRepoFor } from "./repos";
import { requestCredential, scopedRepoPermission } from "./token-scope";
import { all } from "./db";
import { repoDb } from "./scoped-db";
import { registerLocator } from "./scoped-db/locators";
import { scopedId } from "./scoped-db/ids";
import { limitBrowse, limitHeavy, numberVar } from "./limits";
import { HttpError } from "./errors";
import { deleteObjects } from "./storage";
import { listRefs, resolveRevision, splitRevisionPath } from "./git/refs";
import { GitStore } from "./git/store";
import { isReachableCommit } from "./git/reachable";
import { lfsContentFor, lfsImageType } from "./lfs/content";
import { TAR_BLOCK, tarHeader } from "./tar";
import { MAX_POINTER_BYTES } from "./lfs/pointer";
import type { Permission, RepoRow, TreeEntry, Viewer } from "~/lib/types";
import { EMAIL_UNVERIFIED_MESSAGE, canWriteAsViewer } from "~/lib/email-verification";
const RAW_PATH = /^\/([^/]+)\/([^/]+)\/raw\/(.+)$/;
const ARCHIVE_PATH = /^\/([^/]+)\/([^/]+)\/archive\/(.+)\.(tar\.gz|tgz)$/;
const ASSET_DOWNLOAD_PATH = /^\/([^/]+)\/([^/]+)\/releases\/download\/([^/]+)\/([^/]+)$/;
const ASSET_UPLOAD_PATH = /^\/([^/]+)\/([^/]+)\/releases\/([a-z0-9]+)\/assets$/;
const MAX_ARCHIVE_FILES = 5000;
const MAX_ARCHIVE_BYTES = 200 * 1024 * 1024;
const MAX_ASSET_BYTES = 2 * 1024 * 1024 * 1024;
const MAX_ASSETS_PER_RELEASE = 50;
const GIB = 1024 * 1024 * 1024;
const HTTP_INSUFFICIENT_STORAGE = 507;
const SAFE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif", "image/x-icon"]);
async function viewerFor(request: Request): Promise<Viewer | null> {