Node Busboy abort upload Node Busboy abort upload express express

Node Busboy abort upload


Ok, so I had the same problem and I solved it with file.resume();

var fstream;req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {    // Validate file mimetype    if(mimetype != 'image/png'){        file.resume();        return res.json({            success: false,            message: 'Invalid file format'        });    }    // Upload    fstream = fs.createWriteStream(__dirname + '/tmp/' + timestamp + filename);    file.pipe(fstream);    fstream.on('close', function () {        return res.json({            success: true        });    });});req.pipe(req.busboy);


The answer is simple one.

// do the required validation like size check etc.if( size > 500000) {    self.req.unpipe();    return;}

The context is this. I see in busboy code that busboy is implemented as WritableStream and underneath uses Dicer module for parsing which is also implemented as WritableStream. Flow is like this:

req stream ==> busboy ==> dicer ==> dicer raises events ==> busboy raises events on file ==> these events are consumed in your code.

To stop this whole flow of code - the above unpipe should do.


I would think the proper thing to do was to just close the socket and end the request

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {    file.fileRead = [];    var size = 0;    file.on('data', function(chunk) {        size += chunk.length;        /* DO VALIDATION HERE */        if( size > 500000) {            self.req.socket.end();            self.res.end();        }        file.fileRead.push(chunk);    });    file.on('end', function() {        var data = Buffer.concat(file.fileRead, size);        // ... upload to S3    });    self.req.pipe(busboy);});