import { repoDb, stmt } from "../scoped-db";
import { accountsById } from "../people";
import { randomId } from "../crypto";
import { conflict, forbidden, notFoundError } from "../errors";
import { publicUrl } from "../events/emit";
import { concatBytes, parsePacket } from "../git/pktline";
import { applyDelta, parsePack } from "../git/pack";
import type { UnresolvedDelta } from "../git/pack";
import { shortSha, withStore } from "../git/store";
import { detectorName, redactFindings, scannableText, scanText } from "./detectors";
import type { RedactedFinding } from "./detectors";
import { allowedFingerprints } from "./secret-alerts";
import { loadSecuritySettings, nextSecurityNumber } from "./settings";
import { notifyPushProtectionBypass } from "../notify/security";
import type { RepoRow } from "~/lib/types";
const MAX_BUFFERED_PUSH_BYTES = 16 * 1024 * 1024;
const MAX_INFLATED_PUSH_BYTES = 48 * 1024 * 1024;
const MAX_PACK_OBJECT_BYTES = 8 * 1024 * 1024;
const MAX_PACK_OBJECTS = 20_000;
const MAX_SCANNED_TOTAL_BYTES = 16 * 1024 * 1024;
const MAX_BLOCKS_PER_PUSH = 20;
const MAX_THIN_BASES = 500;
const PACK_SIGNATURE = "PACK";
export type Buffered = { bytes: Uint8Array<ArrayBuffer>; complete: boolean; body: ReadableStream<Uint8Array> };
/** Reads up to `cap` bytes; when the stream is longer it re-joins the prefix with the unread tail. */
export async function bufferUpTo(stream: ReadableStream<Uint8Array>, cap: number): Promise<Buffered> {
const reader = stream.getReader();
const chunks: Uint8Array[] = [];
let total = 0;
while (total <= cap) {
const { value, done } = await reader.read();
if (done) {
const bytes = concatBytes(chunks);
return { bytes, complete: true, body: new Blob([bytes]).stream() };
}
chunks.push(value);
total += value.byteLength;