| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { spawn } |
| 3 | import { PassThrough, Readable |
| 4 | import { |
| 5 | FLUSH_PACKET, |
| 6 | toGitBinary, |
| 7 | type GitServiceName, |
| 8 | } from '../../../git/git.constants.js'; |
| 9 | |
| 10 | @Injectable() |
| 11 | export class RefAdvertisementService |
| 12 | private readonly logger |
| 13 | |
| 14 | /** pkt-line: 4 hex bytes of total length, then the payload. */ |
| 15 | convertToPacketLine(str: |
| 16 | const len = |
| 17 | return len + |
| 18 | } |
| 19 | |
| 20 | /** The `GET /info/refs` body: the service header, a flush packet, then the advertisement. SSH sends the advertisement alone - the header exists so an HTTP client can tell which service answered. */ |
| 21 | advertise({ |
| 22 | repoDirectory, |
| 23 | service, |
| 24 | }: { |
| 25 | repoDirectory: string |
| 26 | service: GitServiceName |
| 27 | }): Readable |
| 28 | const output = |
| 29 | |
| 30 | output.write(this |
| 31 | output.write(FLUSH_PACKET |
| 32 | this.advertiseRefs({ |
| 33 | |
| 34 | return output; |
| 35 | } |
| 36 | |
| 37 | /** Exactly what `--advertise-refs` prints: the refs, their capabilities, and a flush packet. */ |
| 38 | advertiseRefs({ |
| 39 | repoDirectory, |
| 40 | service, |
| 41 | }: { |
| 42 | repoDirectory: string |
| 43 | service: GitServiceName |
| 44 | }): Readable |
| 45 | const binary = |
| 46 | const output = |
| 47 | |
| 48 | const child = |
| 49 | binary, |
| 50 | '--stateless-rpc', |
| 51 | '--advertise-refs', |
| 52 | repoDirectory, |
| 53 | ]); |
| 54 | child.stdout.pipe |
| 55 | child.stderr.on |
| 56 | this.logger.warn |
| 57 | ); |
| 58 | child.on('error |
| 59 | |
| 60 | return output; |
| 61 | } |
| 62 | } |