restify/node-restify

bodyParser ignores maxBodySize and buffers whole file in memory for file uploads

Open

#956 建立於 2015年11月23日

在 GitHub 查看
 (5 留言) (2 反應) (0 負責人)JavaScript (983 fork)batch import
BugCriticalGood First IssueHelp Wanted

倉庫指標

Star
 (10,695 star)
PR 合併指標
 (平均合併 3小時 56分鐘) (30 天內合併 2 個 PR)

描述

The bodyParser middleware seems to buffer the whole file in memory for file uploads.

Check the following minimum example:

'use strict';

var os = require( 'os' );
var restify = require( 'restify' );

var server = restify.createServer( {} );

server.use( restify.bodyParser( {
  // this should supposedly limit maximum upload size to 10 KiB
  maxBodySize: 10 * 1024,
  mapParms: true,
  mapFiles: true,
  keepExtensions: true,
  uploadDir: os.tmpdir()
} ) );

server.get( '/', function( req, res, next ) {
  res.json( { message: 'hello' } );
  next();
} );

server.post( '/upload', function( req, res, next ) {
  console.log( 'Received files:', req.files );
  res.send( 200 );
  next();
} );

server.listen( 8000 );

Then, upload a really big file to it:

# bigfile.dat has a couple of GiB
curl -F "file=@/path/to/bigfile.dat" localhost:8000/upload

This will send the process into accumulating tons of memory until it eventually runs out of memory and crashes with:

FATAL ERROR: JS Allocation failed - process out of memory

Am I making a mistake in setting up the middleware or is this a bug?

貢獻者指南