| 1 | import { Injectable, Logger, NestMiddleware } from '@nestjs/common'; |
| 2 | import type { NextFunction, |
| 3 | import { createGunzip, createInflate |
| 4 | |
| 5 | import { spoolToFile } |
| 6 | import { GitAuthenticatedBufferedRequest } |
| 7 | |
| 8 | /** Spools the request to a temp file before anything awaits: an unattended stream drops whatever arrives meanwhile, and git sends its command section in its own socket write. Never buffered, since a push can be gigabytes. */ |
| 9 | @Injectable() |
| 10 | export class GitRawBodyMiddleware |
| 11 | private readonly logger |
| 12 | |
| 13 | async use( |
| 14 | req: GitAuthenticatedBufferedRequest |
| 15 | res: Response |
| 16 | next: NextFunction |
| 17 | ) { |
| 18 | try { |
| 19 | const spooled = |
| 20 | |
| 21 | req.gitBody = |
| 22 | this.logger.debug |
| 23 | `${req.method} |
| 24 | ); |
| 25 | |
| 26 | res.on('close |
| 27 | spooled |
| 28 | .discard() |
| 29 | .catch((error: |
| 30 | this.logger.warn |
| 31 | ); |
| 32 | }); |
| 33 | |
| 34 | next(); |
| 35 | } catch (error) |
| 36 | next(error); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function decode(req: |
| 42 | switch (req.headers |
| 43 | case 'gzip': |
| 44 | return req.pipe |
| 45 | case 'deflate': |
| 46 | return req.pipe |
| 47 | default: |
| 48 | return req; |
| 49 | } |
| 50 | } |