m4nuC / async-busboy

Promise based multipart form parser for KoaJS
MIT License
167 stars 58 forks source link

Throw on('limit') ? #25

Closed karladler closed 7 years ago

karladler commented 7 years ago

Would it be possible to implement the file.on('limit') event? I would really like to have an error thrown like shown below or exposing the listeners or having same options for callbacks, not sure what would be the best practice here...


function onFile( //...
//...
  file.on('limit', () => {
    const err = new Error('Reach file size limit');
    err.code = 'Request_file_size_limit';
    err.status = 413;
    throw err;
  })
m4nuC commented 7 years ago

See if the answer to your preview issue (#24) helps with that.

karladler commented 7 years ago

Thanks, I already found that it's possible to set the fileSize limit using const {files, fields} = await asyncBusboy(ctx.req, {limits: fileSize: 1000}}); but this will actually truncate the file only to the given limit. I wan't to know wether the file exceeds the limit while writing. I'm currently checking after reading the files whether the file size is exactly the limit e.g. 1000. It work's in practice but theoretically you can't say whether the file has exactly 1000 bytes or it's a bigger file. That's why I would be happy to have an exception thrown like described above.

m4nuC commented 7 years ago

Hi, I've just merged #18 which allow setting a custom handler for onFile, it should work for your use case. See the doc:

If a custom onFile handler is specified in the options to async-busboy it will only resolve an object containing fields, but instead no temporary files needs to be created since the file stream is directly passed to the application. Note that all file streams need to be consumed for async-busboy to resolve due to the implementation of busboy. If you don't care about a received file stream, simply call stream.resume() to discard the content.

karladler commented 7 years ago

This should do it. Thank you!