diff --git a/lib/make-middleware.js b/lib/make-middleware.js index ee509886..d4a78b69 100644 --- a/lib/make-middleware.js +++ b/lib/make-middleware.js @@ -123,10 +123,18 @@ function makeMiddleware (setup) { handleRequestFailure(new Error('Request closed')) }) + var busboyLimits = limits + if (limits && Object.prototype.hasOwnProperty.call(limits, 'fileSize') && limits.fileSize != null) { + // Busboy emits 'limit' once a file reaches exactly `fileSize` bytes, even + // when the file is not actually larger than the limit. Bump the value by + // one so the event only fires when the limit is genuinely exceeded. + busboyLimits = { ...limits, fileSize: limits.fileSize + 1 } + } + try { busboy = Busboy({ headers: req.headers, - limits: limits, + limits: busboyLimits, preservePath: preservePath, defParamCharset: defParamCharset }) diff --git a/test/error-handling.js b/test/error-handling.js index 597e8039..600d0c00 100644 --- a/test/error-handling.js +++ b/test/error-handling.js @@ -66,6 +66,21 @@ describe('Error Handling', function () { }) }) + it('should accept a file whose size equals the file size limit', function (done) { + var form = new FormData() + var parser = withLimits({ fileSize: 122 }, [ + { name: 'tiny0', maxCount: 1 } + ]) + + form.append('tiny0', util.file('tiny0.dat')) + + util.submitForm(parser, form, function (err, req) { + assert.ifError(err) + assert.strictEqual(req.files.tiny0[0].size, 122) + done() + }) + }) + it('should respect file count limit', function (done) { var form = new FormData() var parser = withLimits({ files: 1 }, [