Skip to content

Commit

Permalink
improve performance for large files
Browse files Browse the repository at this point in the history
Using `buf = Buffer.concat([buf, data]);` on the data event copies the
data in a new larger buffer everytime. Instead, store the intermediate
small buffers in an array and concat only at the end.
  • Loading branch information
targos committed Mar 8, 2017
1 parent ac309ee commit e23f337
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ function processMultipart(options, req, res, next) {

// Build req.files fields
busboy.on('file', function(fieldname, file, filename, encoding, mime) {
let buf = new Buffer(0);
const buffers = [];
let safeFileNameRegex = /[^\w-]/g;

file.on('data', function(data) {
buf = Buffer.concat([buf, data]);
buffers.push(data);

if (options.debug)
return console.log('Uploading %s -> %s', fieldname, filename);
Expand All @@ -81,6 +81,7 @@ function processMultipart(options, req, res, next) {
if (!req.files)
req.files = {};

const buf = Buffer.concat(buffers);
// see: https://github.com/richardgirges/express-fileupload/issues/14
// firefox uploads empty file in case of cache miss when f5ing page.
// resulting in unexpected behavior. if there is no file data, the file is invalid.
Expand Down

0 comments on commit e23f337

Please sign in to comment.