question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vanilla NodeJS `request.on('end') is being called before `formidable.parse` completes

See original GitHub issue

Support plan

  • which support plan is this issue covered by? Community:
  • is this issue currently blocking your project? yes
  • is this issue affecting a production system? no

Context

  • node version: 15.2.1
  • module (formidable) version: 1.2.2
  • environment (e.g. node, browser, native, OS): node, Windows 10
  • used with (i.e. popular names of modules): Vanilla NodeJS
  • any other relevant information:

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.

import * as formidable from 'formidable';

// 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;
        });
    }
    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);
    });
    
});


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 before formidable.parse completes.

What result did you expect?

One that works — I know that’s controversial.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tunnckoCorecommented, Jan 7, 2021

@NoelAbrahams yep, something like #635 looks beautiful and modern.

0reactions
NoelAbrahamscommented, Jan 7, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

chore(deps): bump ini from 1.3.5 to 1.3.7 (#665) · node-formidable ...
Vanilla NodeJS `request.on('end') is being called before `formidable.parse` completes Vanilla NodeJS `request.on('end') is being called before `formidable.parse ...
Read more >
How to avoid early return on formidable functions in Node js
It works as expected except the early return from the method. There the method returned before fire 'fileBegin' and 'file' events. Could you ......
Read more >
Differences between formidable callbacks, events ... - Medium
As formidable is parsing the request, when the callback is provided, formidable will collect all fields and files and pass to the callback....
Read more >
Node.js Notes for Professionals
Section 3.11: Hook: How to execute code before any req and after any res ... response.end(http. ... When called without arguments, Node.js starts...
Read more >
How to use IncomingForm function in formidable - Tabnine
formParse: function (callback) { var form = new formidable. ... IncomingForm(); form.parse(request, (error, fields, files) => { var oldPath = files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found