-
-
Notifications
You must be signed in to change notification settings - Fork 681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vanilla NodeJS request.on('end') is being called before
formidable.parse` completes
#673
Comments
Kind of make sense to me. Everything is event-driven async flow, so... you can't just do that. Try adding formidable.parse(request, (error, fields, files) => {
// Process multi-part form data
request.on('end', () => {
response.write(files);
});
}); Also, please try v2 canary ( And a tip, you can just |
@tunnckoCore thanks for the tips. I've updated to I dug into this a bit more and the reason
The solution I've gone with for the moment is to not listen to the // Handle incoming HTTP requests
httpServer.on('request', (request, response) => {
let body;
if (request.headers['content-type'].includes('multipart/form-data')) {
formidable.parse(request, (error, fields, files) => {
// Process multi-part form data
body = files;
});
// Do something with body
response.write(body);
}
else {
request.on('data', data => {
// Process other
body = data;
});
// Handle completion of the read operation above
request.on('end', async () => {
// Do something with body
response.write(body);
});
}
}); I feel the correct solution is to do something like this: // Handle incoming HTTP requests
httpServer.on('request', (request, response) => {
let body;
const isMultiPart = request.headers['content-type'].includes('multipart/form-data');
request.on('data', data => {
if (isMultiPart) {
body = Buffer.concat([body, data]);
}
else {
body += data;
}
});
// Handle completion of the read operation above
request.on('end', async () => {
let result;
if (isMultiPart) {
formidable.parse(body, (error, fields, files) => {
// Process multi-part form data
result = files;
});
}
else {
// Parse string body
result = JSON.parse(body);
}
// Do something with the result
response.write(result);
});
}); But So, some food for thought. I also hope that by not listening to |
I still don't understand why you need to listen to the
Very true. I remember that and probably it's the reason, yep. The solution that works for the moment is definitely signaling how it should not be done :D Sorry, don't want to be rude or aggressive or anything, it just doesn't feel and look good, it's not how async flow goes. offtopic... The whole problem is we are doing a lot more than just the MultipartParser and I kind of hate that, it can be a ton simpler and easier. Haha, anyway. |
Great. That's what I wanted to know.
You've probably not read the code right, because 'not listening to 'end'' is exactly what I too suggested.
Yes, absolutely agree. |
@NoelAbrahams yep, something like #635 looks beautiful and modern. |
@tunnckoCore I'm going to close this issue, as it was really more of a question, and there doesn't seem to be anything to fix. |
Support plan
Context
What are you trying to achieve or the steps to reproduce?
Trying to upload a simple file around 100 KB. There is a branch in the code below for handling multi-part form data. We want
formidable
to handle that. We want to handle all other requests via the second branch.What was the result you got?
This works some of the time. But there is a race condition because the last bit
request.on('end')
is being called beforeformidable.parse
completes.What result did you expect?
One that works — I know that's controversial.
The text was updated successfully, but these errors were encountered: