| 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 | /** |
| 9 | * Spools the request to a temp file before guards, pipes or handlers get a chance to await anything. |
| 10 | * |
| 11 | * Middleware, not a handler: an unattended stream drops whatever arrives while the handler is busy, and git sends its command section in its own socket write. A push can also be gigabytes, so it is never buffered. |
| 12 | */ |
| 13 | @Injectable() |
| 14 | export class GitRawBodyMiddleware |
| 15 | private readonly logger |
| 16 | |
| 17 | async use( |
| 18 | req: GitAuthenticatedBufferedRequest |
| 19 | res: Response |
| 20 | next: NextFunction |
| 21 | ) { |
| 22 | try { |
| 23 | const spooled = |
| 24 | |
| 25 | req.gitBody = |
| 26 | this.logger.debug |
| 27 | `${req.method} |
| 28 | ); |
| 29 | |
| 30 | res.on('close |
| 31 | spooled |
| 32 | .discard() |
| 33 | .catch((error: |
| 34 | this.logger.warn |
| 35 | ); |
| 36 | }); |
| 37 | |
| 38 | next(); |
| 39 | } catch (error) |
| 40 | next(error); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | function decode(req: |
| 46 | switch (req.headers |
| 47 | case 'gzip': |
| 48 | return req.pipe |
| 49 | case 'deflate': |
| 50 | return req.pipe |
| 51 | default: |
| 52 | return req; |
| 53 | } |
| 54 | } |