389 lines · 16.4 KB
import { z } from "zod";
import { badRequest, conflict, forbidden, HttpError, notFoundError } from "../errors";
import { editorRoomStub } from "./room-client";
import { repoSearch } from "../code/repo-search-do";
import { handleRoomRoute, handleRoomSocket } from "./room-api";
import { handleContainerRoute, handleContainerTerminal } from "./container-api";
import { CORS_HEADERS, json } from "./http";
import { branchSha, listRefs } from "../git/refs";
import { withStore } from "../git/store";
import type { GitStore } from "../git/store";
import { commitFileChanges } from "../git/web-commit";
import { limitBrowse, limitWrite } from "../limits";
import { MAX_POINTER_BYTES, parseLfsPointer } from "../lfs/pointer";
import { assertEditableChanges } from "./write-guard";
import { readBounded } from "./blob-read";
import type { BoundedBlob } from "./blob-read";
import { openPull } from "../pull-create";
import { publicUrl } from "../events/emit";
import { canWrite, editorSessionFromRequest, needsPullRequest } from "./session";
import type { EditorSession } from "./session";
import { compileQuery, matchLines, MAX_PATTERN_LENGTH } from "~/lib/text-search";
import type { LineMatch } from "~/lib/text-search";

export const EDITOR_API_PREFIX = "/api/editor/";

const MAX_TREE_ENTRIES = 5000;
const MAX_BLOB_BYTES = 1024 * 1024;
const MAX_CHANGES = 100;
const MAX_COMMIT_BYTES = 5 * 1024 * 1024;
const MAX_BODY_BYTES = MAX_COMMIT_BYTES * 2;
const MAX_PATCH_BRANCH_TRIES = 50;
const MAX_SEARCH_RESULTS = 2000;
const BINARY_SNIFF = 8000;
/** Above this many blobs outside the index, prefetch the pack instead of reading blob by blob. */
const PREFETCH_ABOVE = 50;
/** A search stops (and says so) after this long, so one query can't hold the request near the CPU limit. */
const SEARCH_BUDGET_MS = 10_000;
const decoder = new TextDecoder();
const encoder = new TextEncoder();