| 1 | import { createReadStream } from 'node:fs'; |
| 2 | import { Readable } |
| 3 | |
| 4 | /** A request body that can be read more than once and never has to exist in memory all at once. Push bodies routinely exceed the 2 GiB ceiling on a single Buffer operation, so nothing downstream may assume a Buffer. */ |
| 5 | export interface GitRequestBody |
| 6 | readonly size: |
| 7 | open(start?: |
| 8 | } |
| 9 | |
| 10 | export function bufferBody(buffer |
| 11 | return { |
| 12 | size: buffer.length, |
| 13 | // an empty body must emit no chunk at all: S3's checksumming stream rejects a zero-length chunk with BadDigest |
| 14 | open: (start |
| 15 | Readable.from(start |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | export function fileBody(path |
| 20 | return { |
| 21 | size, |
| 22 | open: (start |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | /** Emits `header`, then everything `body` holds from `start` onward. */ |
| 27 | export function prefixed(header |
| 28 | return Readable.from |
| 29 | (async function* |
| 30 | yield header; |
| 31 | yield* body.open |
| 32 | })(), |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | export async function readHead |
| 37 | const chunks: |
| 38 | let length = |
| 39 | |
| 40 | for await (const |
| 41 | chunks.push(chunk |
| 42 | length += (chunk |
| 43 | if (length >= |
| 44 | } |
| 45 | |
| 46 | return Buffer.concat |
| 47 | } |