nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Allow to specify `limit` for `raw` parser

Open gastonelhordoy opened this issue 2 years ago • 2 comments

When using the RawBodyMiddleware middleware, it doesn't allow to provide a configurable limit parameter.

@Injectable()
export class RawBodyMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: () => any) {
    raw({ type: '*/*' })(req, res, next);
  }
}

In case of large payloads, it could cause raw-body library to throw a PayloadTooLarge exception.

function readStream (stream, encoding, length, limit, callback) {
  var complete = false
  var sync = true

  // check the length and limit options.
  // note: we intentionally leave the stream paused,
  // so users should handle the stream themselves.
  if (limit !== null && length !== null && length > limit) {
    return done(createError(413, 'request entity too large', {
      expected: length,
      length: length,
      limit: limit,
      type: 'entity.too.large'
    }))
  }

Is there any way to work around this restriction?

gastonelhordoy avatar May 11 '23 22:05 gastonelhordoy

@gastonelhordoy I believe this could be tweaked and possibly allow developers to inject that and our module infer through DI. Do you think you could potentially push this feature? I would be happy to review and push but currently i do not have enough time to develop such feature

underfisk avatar Jul 04 '23 14:07 underfisk