| 1 | import { createWriteStream, mkdtempSync } from 'node:fs'; |
| 2 | import { rm } |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import type { Readable |
| 6 | import { pipeline } |
| 7 | |
| 8 | import { fileBody, type |
| 9 | |
| 10 | export interface SpooledBody |
| 11 | body: GitRequestBody |
| 12 | /** Removes the temp directory. Safe to call twice. */ |
| 13 | discard(): Promise |
| 14 | } |
| 15 | |
| 16 | /** Writes a request to a temp file so it can be read twice - once to parse the ref commands, once to feed git - without ever holding a packfile in memory. */ |
| 17 | export async function spoolToFile |
| 18 | // Synchronous on purpose: the caller has already started the stream, and an await here loses whatever arrives - or errors - before the pipeline is attached. |
| 19 | const directory = |
| 20 | |
| 21 | try { |
| 22 | const file = |
| 23 | const written = |
| 24 | await pipeline(input |
| 25 | |
| 26 | return { |
| 27 | body: fileBody(file |
| 28 | discard: () => |
| 29 | }; |
| 30 | } catch (error) |
| 31 | await rm(directory |
| 32 | throw error; |
| 33 | } |
| 34 | } |