mscdex / busboy

A streaming parser for HTML form data for node.js
MIT License
2.84k stars 213 forks source link

Get files count before on('file') ? #322

Closed SourceBoy closed 1 year ago

SourceBoy commented 1 year ago

Any way to get the files count before listening to on('file')?

I've got a use case where I'm calling .end(request.rawBody) instead of piping due to using serverless functions. on('file') is wrapped inside of a new Promise and can only be resolved once all the files are processed.

I'm using a counter inside of on('file') but doesn't have a value to compare the counter to in order to resolve the promise.

(Using on/once close/end/finish results in prematurely resolving the promise as the processing isn't complete yet.)

mscdex commented 1 year ago

Due to the nature of multipart/form-data, there is no way to know how many files or fields beforehand. You could require a field at the very beginning of the form to hold such a count, but if the form is coming from an arbitrary user you cannot necessarily trust its value.