formidable
Advanced tools
Comparing version 3.1.5 to 3.2.0
{ | ||
"name": "formidable", | ||
"version": "3.1.5", | ||
"version": "3.2.0", | ||
"license": "MIT", | ||
@@ -16,3 +16,3 @@ "description": "A node.js module for parsing form data, especially file uploads.", | ||
"access": "public", | ||
"tag": "v3" | ||
"tag": "v4" | ||
}, | ||
@@ -19,0 +19,0 @@ "scripts": { |
@@ -313,4 +313,8 @@ <p align="center"> | ||
uploaded file. | ||
- `options.maxFiles` **{number}** - default `Infinity`; | ||
limit the amount of uploaded files. | ||
- `options.maxFileSize` **{number}** - default `200 * 1024 * 1024` (200mb); | ||
limit the size of uploaded file. | ||
limit the size of each uploaded file. | ||
- `options.maxTotalFileSize` **{number}** - default `options.maxFileSize`; | ||
limit the size of the batch of uploaded files. | ||
- `options.maxFields` **{number}** - default `1000`; limit the number of fields, set 0 for unlimited | ||
@@ -317,0 +321,0 @@ - `options.maxFieldsSize` **{number}** - default `20 * 1024 * 1024` (20mb); |
@@ -23,5 +23,7 @@ /* eslint-disable class-methods-use-this */ | ||
maxFieldsSize: 20 * 1024 * 1024, | ||
maxFiles: Infinity, | ||
maxFileSize: 200 * 1024 * 1024, | ||
maxTotalFileSize: undefined, | ||
minFileSize: 1, | ||
allowEmptyFiles: true, | ||
allowEmptyFiles: false, | ||
keepExtensions: false, | ||
@@ -49,2 +51,5 @@ encoding: 'utf-8', | ||
this.options = { ...DEFAULT_OPTIONS, ...options }; | ||
if (!this.options.maxTotalFileSize) { | ||
this.options.maxTotalFileSize = this.options.maxFileSize | ||
} | ||
@@ -74,3 +79,3 @@ const dir = path.resolve( | ||
this._fieldsSize = 0; | ||
this._fileSize = 0; | ||
this._totalFileSize = 0; | ||
this._plugins = []; | ||
@@ -95,2 +100,3 @@ this.openedFiles = []; | ||
this._setUpMaxFields(); | ||
this._setUpMaxFiles(); | ||
this.ended = undefined; | ||
@@ -315,2 +321,3 @@ this.type = undefined; | ||
let fileSize = 0; | ||
const newFilename = this._getNewName(part); | ||
@@ -333,18 +340,10 @@ const filepath = this._joinDirectoryName(newFilename); | ||
part.on('data', (buffer) => { | ||
this._fileSize += buffer.length; | ||
if (this._fileSize < this.options.minFileSize) { | ||
this._totalFileSize += buffer.length; | ||
fileSize += buffer.length; | ||
if (this._totalFileSize > this.options.maxTotalFileSize) { | ||
this._error( | ||
new FormidableError( | ||
`options.minFileSize (${this.options.minFileSize} bytes) inferior, received ${this._fileSize} bytes of file data`, | ||
errors.smallerThanMinFileSize, | ||
400, | ||
), | ||
); | ||
return; | ||
} | ||
if (this._fileSize > this.options.maxFileSize) { | ||
this._error( | ||
new FormidableError( | ||
`options.maxFileSize (${this.options.maxFileSize} bytes) exceeded, received ${this._fileSize} bytes of file data`, | ||
errors.biggerThanMaxFileSize, | ||
`options.maxTotalFileSize (${this.options.maxTotalFileSize} bytes) exceeded, received ${this._totalFileSize} bytes of file data`, | ||
errors.biggerThanTotalMaxFileSize, | ||
413, | ||
@@ -365,3 +364,3 @@ ), | ||
part.on('end', () => { | ||
if (!this.options.allowEmptyFiles && this._fileSize === 0) { | ||
if (!this.options.allowEmptyFiles && fileSize === 0) { | ||
this._error( | ||
@@ -376,2 +375,22 @@ new FormidableError( | ||
} | ||
if (fileSize < this.options.minFileSize) { | ||
this._error( | ||
new FormidableError( | ||
`options.minFileSize (${this.options.minFileSize} bytes) inferior, received ${fileSize} bytes of file data`, | ||
errors.smallerThanMinFileSize, | ||
400, | ||
), | ||
); | ||
return; | ||
} | ||
if (fileSize > this.options.maxFileSize) { | ||
this._error( | ||
new FormidableError( | ||
`options.maxFileSize (${this.options.maxFileSize} bytes), received ${fileSize} bytes of file data`, | ||
errors.biggerThanMaxFileSize, | ||
413, | ||
), | ||
); | ||
return; | ||
} | ||
@@ -594,2 +613,20 @@ file.end(() => { | ||
_setUpMaxFiles() { | ||
if (this.options.maxFiles !== Infinity) { | ||
let fileCount = 0; | ||
this.on('file', () => { | ||
fileCount += 1; | ||
if (fileCount > this.options.maxFiles) { | ||
this._error( | ||
new FormidableError( | ||
`options.maxFiles (${this.options.maxFiles}) exceeded`, | ||
errors.maxFilesExceeded, | ||
413, | ||
), | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
_maybeEnd() { | ||
@@ -596,0 +633,0 @@ // console.log('ended', this.ended); |
@@ -10,3 +10,3 @@ const missingPlugin = 1000; | ||
const smallerThanMinFileSize = 1008; | ||
const biggerThanMaxFileSize = 1009; | ||
const biggerThanTotalMaxFileSize = 1009; | ||
const noEmptyFiles = 1010; | ||
@@ -17,2 +17,4 @@ const missingContentType = 1011; | ||
const unknownTransferEncoding = 1014; | ||
const maxFilesExceeded = 1015; | ||
const biggerThanMaxFileSize = 1016; | ||
const pluginFailed = 1017; | ||
@@ -37,2 +39,3 @@ | ||
maxFieldsExceeded, | ||
maxFilesExceeded, | ||
smallerThanMinFileSize, | ||
@@ -45,2 +48,3 @@ biggerThanMaxFileSize, | ||
unknownTransferEncoding, | ||
biggerThanTotalMaxFileSize, | ||
pluginFailed, | ||
@@ -47,0 +51,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91720
1570
855