| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { spawn, type |
| 3 | import { PassThrough, Readable |
| 4 | import { |
| 5 | toGitBinary, |
| 6 | type GitServiceName, |
| 7 | } from '../../../git/git.constants.js'; |
| 8 | |
| 9 | interface StreamOptions { |
| 10 | repoDirectory: string; |
| 11 | /** The client's request body: `want`/`have` lines or a packfile. */ |
| 12 | input: Readable |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Runs `git upload-pack` / `git receive-pack` in `--stateless-rpc` mode. |
| 17 | * |
| 18 | * Deliberately knows nothing about HTTP: it consumes a Readable and returns a Readable, so a test can drive it with `Readable.from(buffer)`. |
| 19 | */ |
| 20 | @Injectable() |
| 21 | export class PackProcessService |
| 22 | private readonly logger |
| 23 | |
| 24 | streamUploadPack(options: |
| 25 | return this.spawnBinary |
| 26 | } |
| 27 | |
| 28 | streamReceivePack(options: |
| 29 | return this.spawnBinary |
| 30 | } |
| 31 | |
| 32 | /** The SSH form: one process per session, speaking the full protocol in both directions. `--stateless-rpc` answers a single HTTP request and then stops listening, which is the wrong shape for a channel that stays open. */ |
| 33 | spawnInteractive({ |
| 34 | repoDirectory, |
| 35 | service, |
| 36 | }: { |
| 37 | repoDirectory: string |
| 38 | service: GitServiceName |
| 39 | }): ChildProcessByStdio |
| 40 | return spawn('git |
| 41 | stdio: ['pipe', |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | private spawnBinary({ |
| 46 | repoDirectory, |
| 47 | input, |
| 48 | service, |
| 49 | }: StreamOptions |
| 50 | const binary = |
| 51 | const output = |
| 52 | const child = |
| 53 | |
| 54 | input.pipe(child |
| 55 | child.stdout.pipe |
| 56 | child.stderr.on |
| 57 | this.logger.warn |
| 58 | ); |
| 59 | child.on('error |
| 60 | |
| 61 | return output; |
| 62 | } |
| 63 | } |