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
10 changes: 9 additions & 1 deletion lib/make-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
15 changes: 15 additions & 0 deletions test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, [
Expand Down