| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { spawn, type |
| 3 | import { Readable, type |
| 4 | import { |
| 5 | protocolEnv, |
| 6 | toGitBinary, |
| 7 | type GitServiceName, |
| 8 | } from '../../../lib/git/protocol/git-service.js'; |
| 9 | import { spawnPack } |
| 10 | |
| 11 | interface StreamOptions { |
| 12 | repoDirectory: string; |
| 13 | /** The client's request body: `want`/`have` lines or a packfile. */ |
| 14 | input: Readable |
| 15 | /** What the client asked for in `Git-Protocol` or `GIT_PROTOCOL`. */ |
| 16 | protocol?: string |
| 17 | } |
| 18 | |
| 19 | /** Runs `git upload-pack` / `git receive-pack` in `--stateless-rpc` mode over Readables, knowing nothing of HTTP so a test can drive it with `Readable.from(buffer)`. */ |
| 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 | protocol, |
| 37 | }: { |
| 38 | repoDirectory: string |
| 39 | service: GitServiceName |
| 40 | protocol?: string |
| 41 | }): ChildProcessByStdio |
| 42 | return spawn('git |
| 43 | stdio: ['pipe', |
| 44 | env: { ...process |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | private spawnBinary({ |
| 49 | service, |
| 50 | ...options |
| 51 | }: StreamOptions |
| 52 | return spawnPack({ |
| 53 | ...options, |
| 54 | service, |
| 55 | warn: (message) |
| 56 | }); |
| 57 | } |
| 58 | } |