node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

parse() callback never call with some files #923

Closed nikis closed 1 year ago

nikis commented 1 year ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

import http from 'node:http';
import formidable from 'formidable';

const server = http.createServer((req, res) => {
    if (req.url === '/api/test' && req.method.toLowerCase() === 'post') {
        // parse a file upload
        const form = formidable({});

        form.parse(req, (err, fields, files) => {
            if (err) {
                res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
                res.end(String(err));
                return;
            }
            res.writeHead(200, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ fields, files }, null, 2));
        });

        return;
    }

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(``);
});

server.listen(3000, () => {
    console.log('Server listening on http://localhost:3000/ ...');
});

What was the result you got?

parse() callback never called with some random files

curl localhost:3000/api/test -F test='mest' -F video=@/test.mp4

If i call without file, callback is fired and fields its filled

curl localhost:3000/api/test -F test='mest' 

What result did you expect?

parse() callback to be fired always

tunnckoCore commented 1 year ago

@nikis hi, very strange. It's very basic stuff and it's from the readme which is working. Maybe the problem is in the file, or it's too big or something like that.

nikis commented 1 year ago

The file I'm trying with is not big - about 500kb. It also happens with other non-video files. Everything is very random which also makes debugging difficult.

Also i add some listeners fileBegin event is triggered

form.once('error', console.error);

form.on('fileBegin', (formname, file) => {
    console.log('fileBegin', formname, file);
});

form.on('file', (formname, file) => {
    console.log('file', formname, file);
});

form.on('field', (fieldName, fieldValue) => {
    console.log('field', fieldName, fieldValue)
});

form.once('end', () => {
    console.log('Done!');
});

Also if i set uploadDir option , actually the file is created in this directory

nikis commented 1 year ago

The problem occurs only when i use devcontainer (local machine, not in GitHub Codespaces etc). If i run outside, everything it's working as expected.