import type { GitStore } from "../git/store";
const BINARY_SNIFF = 8000;
export type BoundedBlob = { size: number; bytes: Uint8Array | null; binary: boolean };
/**
* Reads a blob only when it fits `maxBytes`: the size comes from the blob handle before any bytes are
* copied, so a huge file costs a metadata read rather than a full buffer. Oversized blobs still get a
* binary sniff from a bounded slice.
*/
export async function readBounded(store: GitStore, sha: string, maxBytes: number): Promise<BoundedBlob | null> {
const handle = await store.blobStream(sha);
if (!handle) return null;
if (handle.size > maxBytes) {
const head = new Uint8Array(await handle.slice(0, BINARY_SNIFF).arrayBuffer());
return { size: handle.size, bytes: null, binary: head.includes(0) };
}
const bytes = new Uint8Array(await handle.arrayBuffer());
return { size: handle.size, bytes, binary: bytes.subarray(0, BINARY_SNIFF).includes(0) };
}