Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/make-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ var MulterError = require('./multer-error')
var FileAppender = require('./file-appender')
var removeUploadedFiles = require('./remove-uploaded-files')

function drainStream (stream) {
function drainStream(stream) {
stream.on('readable', () => {
while (stream.read() !== null) {}
while (stream.read() !== null) { }
})
}

function makeMiddleware (setup) {
return function multerMiddleware (req, res, next) {
function makeMiddleware(setup) {
return function multerMiddleware(req, res, next) {
if (!is(req, ['multipart'])) return next()

var options = setup()
Expand All @@ -36,9 +36,9 @@ function makeMiddleware (setup) {
var pendingWrites = new Counter()
var uploadedFiles = []

function done (err) {
function done(err) {
var called = false
function onFinished () {
function onFinished() {
if (called) return
called = true
next(err)
Expand Down Expand Up @@ -69,16 +69,16 @@ function makeMiddleware (setup) {
next(err)
}

function indicateDone () {
function indicateDone() {
if (readFinished && pendingWrites.isZero() && !errorOccured) done()
}

function abortWithError (uploadError, skipPendingWait) {
function abortWithError(uploadError, skipPendingWait) {
if (errorOccured) return
errorOccured = true

function finishAbort () {
function remove (file, cb) {
function finishAbort() {
function remove(file, cb) {
storage._removeFile(req, file, cb)
}

Expand All @@ -97,11 +97,11 @@ function makeMiddleware (setup) {
}
}

function abortWithCode (code, optionalField) {
function abortWithCode(code, optionalField) {
abortWithError(new MulterError(code, optionalField))
}

function handleRequestFailure (err) {
function handleRequestFailure(err) {
if (isDone) return
if (busboy) {
req.unpipe(busboy)
Expand Down Expand Up @@ -208,7 +208,7 @@ function makeMiddleware (setup) {

fileStream.on('limit', function () {
aborting = true
abortWithCode('LIMIT_FILE_SIZE', fieldname)
abortWithError(new MulterError('LIMIT_FILE_SIZE', fieldname, filename))
})

storage._handleFile(req, file, function (err, info) {
Expand Down
3 changes: 2 additions & 1 deletion lib/multer-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ var errorMessages = {
MISSING_FIELD_NAME: 'Field name missing'
}

function MulterError (code, field) {
function MulterError (code, field, filename) {
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = errorMessages[code]
this.code = code
if (field) this.field = field
if (filename) this.filename = filename
}

util.inherits(MulterError, Error)
Expand Down
8 changes: 2 additions & 6 deletions test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Error Handling', function () {
util.submitForm(parser, form, function (err, req) {
assert.strictEqual(err.code, 'LIMIT_FILE_SIZE')
assert.strictEqual(err.field, 'small0')
assert.strictEqual(err.filename, 'small0.dat')
done()
})
})
Expand Down Expand Up @@ -434,11 +435,6 @@ describe('Error Handling', function () {
})

it('should not overflow call stack when cleaning up many files (memory storage sync remove)', function (done) {
// - without setImmediate in remove-uploaded-files, synchronous _removeFile (e.g. memory storage)
// causes handleFile(0) -> remove -> cb() -> handleFile(1) -> ... in one stack,
// leading to "Maximum call stack size exceeded"
// - use enough files to exceed typical node stack depth (~10k - 30k)

this.timeout(10 * 1000)

var fileCount = 25000
Expand All @@ -459,4 +455,4 @@ describe('Error Handling', function () {
done()
})
})
})
})
4 changes: 2 additions & 2 deletions test/functionality.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ describe('Functionality', function () {
util.submitForm(parser, form, function (err, req) {
assert.ifError(err)
assert.strictEqual(req.files.length, 2)
assert.ok(req.files[0].path.indexOf('/testforme-') >= 0)
assert.ok(req.files[1].path.indexOf('/testforme-') >= 0)
assert.ok(req.files[0].path.indexOf('testforme-') >= 0)
assert.ok(req.files[1].path.indexOf('testforme-') >= 0)
done()
})
})
Expand Down