nest icon indicating copy to clipboard operation
nest copied to clipboard

Ability to define the max body size using property in NestApplicationOptions

Open filipvh opened this issue 3 years ago • 5 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

There seems no easy way to change the body size limit of the default body parser. The function getBodyParserOptions (internal) that is used in nestjs allows setting of options (like limit: '50mb')

Describe the solution you'd like

A simple bodyLimit: string | number | undefined; in the config would be nice. I think body parser's default is 100kb.

Teachability, documentation, adoption, migration strategy

Doesn't break anything. Is a simple and clean addition. We might want to provide a warning, that is certain cases this might hold performance risks. Long term we might want to investigate a way to be able to target specific routes?

What is the motivation / use case for changing the behavior?

Github webhooks can be up to 25MB. 100KB is small. I have a custom solution in place, but this seems silly, since it's a small code change (as far as I can see)

filipvh avatar Oct 13 '22 19:10 filipvh

Also, if this seems like a good idea, I would like to try to implement it myself.

filipvh avatar Oct 13 '22 19:10 filipvh

I like the idea for it! I'd say go ahead and make a PR, the API looks clean enough. Make sure it works for fastify and express please

jmcdo29 avatar Oct 13 '22 19:10 jmcdo29

@jmcdo29 It seems express and fastify do this different. In the case of fastify you can provide the bodyLimit using that config property when instantiating fastify

  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({bodyLimit: 26214400 /*25MB*/})
  );

While with express, you need to provide it when setting up the body parser middleware.

Any suggestions how something like this would typically be resolved in nestjs?

filipvh avatar Oct 13 '22 20:10 filipvh

instead of adding another option, just disable the built-in one and plug your own body parser middleware like this: #9427

micalevisk avatar Oct 13 '22 20:10 micalevisk

instead of adding another option, just disable the built-in one and plug your own body parser middleware like this: #9427

@micalevisk already did this, also needed to enable rawBody (not it that example I think) but it seems like something I would expect to be less "work". O well.

filipvh avatar Oct 14 '22 08:10 filipvh

Thanks for your suggestion!

There are no plans to implement it in the foreseeable future.

If you think your request could live outside Nest's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

kamilmysliwiec avatar Oct 21 '22 15:10 kamilmysliwiec