hono icon indicating copy to clipboard operation
hono copied to clipboard

feat: introduce Body Limit Middleware using stream

Open yusukebe opened this issue 1 year ago • 0 comments

This is an implementation of the feature proposed in #2077 using stream.

How to use

Simply specify the maximum size.

app.post(
  '/hello',
  bodyLimit({
    maxSize: 15 * Unit.b, // byte,
    onError: (c) => {
      return c.text('oveflow :(', 413)
    },
  }),
  (c) => {
    return c.text('pass :)')
  }
)

Using stream

The problem discussed in #2077 that a body must be read at a time is solved by using stream.

c.req.bodyCache

It uses c.req.bodyCache used by the Validator implementation. By setting the loaded ArrayBuffer to c.req.bodyCache.arrayBuffer, such as c.req.json() will use that cached body from then on.

Author should do the followings, if applicable

  • [x] Add tests
  • [x] Run tests
  • [x] yarn denoify to generate files for Deno

yusukebe avatar Jan 27 '24 20:01 yusukebe