| 1 | import type { Readable } from 'node:stream'; |
| 2 | |
| 3 | import { isUtf8 } |
| 4 | |
| 5 | import { runGit, runGitBuffer |
| 6 | |
| 7 | /** Past this a file is a download, not something a page renders. */ |
| 8 | export const MAX_BLOB_BYTES = |
| 9 | |
| 10 | export interface Blob |
| 11 | oid: string; |
| 12 | size: number; |
| 13 | /** null when the file is larger than { |
| 14 | content: Buffer |
| 15 | } |
| 16 | |
| 17 | /** Object id and size of a file at a ref, or null when the path is missing or is not a file - one 404 for the caller either way. */ |
| 18 | export async function statBlob |
| 19 | gitDir, |
| 20 | ref, |
| 21 | path, |
| 22 | }: { |
| 23 | gitDir: string |
| 24 | ref: string |
| 25 | path: string |
| 26 | }): Promise<{ |
| 27 | const stat = |
| 28 | args: ['cat-file', |
| 29 | gitDir, |
| 30 | // stdin keeps the revision out of argv, where a path could pass for a flag |
| 31 | input: Buffer.from |
| 32 | }); |
| 33 | |
| 34 | // "<oid> <type> <size>", or "<rev> missing" |
| 35 | const [oid, |
| 36 | return type === |
| 37 | } |
| 38 | |
| 39 | /** A single file at a ref, read into memory up to {@ |
| 40 | export async function readBlob |
| 41 | gitDir: string |
| 42 | ref: string |
| 43 | path: string |
| 44 | }): Promise<Blob |
| 45 | const blob = |
| 46 | if (!blob) |
| 47 | if (blob.size |
| 48 | |
| 49 | return { |
| 50 | ...blob, |
| 51 | content: await runGitBuffer |
| 52 | args: ['cat-file', |
| 53 | gitDir: args.gitDir |
| 54 | }), |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | /** The same bytes, streamed, with no size limit. */ |
| 59 | export function streamBlob({ |
| 60 | gitDir, |
| 61 | oid, |
| 62 | }: { |
| 63 | gitDir: string |
| 64 | oid: string |
| 65 | }): Readable { |
| 66 | return runGitReadable({ |
| 67 | } |
| 68 | |
| 69 | /** git's own heuristic: a NUL anywhere means binary. */ |
| 70 | export function isTextBlob(content |
| 71 | return content !== |
| 72 | } |