Comparing version 0.1.0 to 0.1.1
45
index.js
@@ -7,2 +7,3 @@ var os = require('os'); | ||
var mkdirp = require('mkdirp'); | ||
var qs = require('qs'); | ||
@@ -34,2 +35,5 @@ module.exports = function(options) { | ||
var readFinished = false; | ||
var fileCount = 0; | ||
req.body = req.body || {}; | ||
@@ -73,5 +77,8 @@ req.files = req.files || {}; | ||
// don't attach to the files object, if there is file | ||
// don't attach to the files object, if there is no file | ||
if (!filename) return fileStream.resume(); | ||
// defines is processing a new file | ||
fileCount++; | ||
ext = '.' + filename.split('.').slice(-1)[0]; | ||
@@ -94,3 +101,10 @@ newFilename = rename(fieldname, filename.replace(ext, '')) + ext; | ||
// trigger "file upload start" event | ||
if (options.onFileUploadStart) { options.onFileUploadStart(file); } | ||
if (options.onFileUploadStart) { | ||
var proceed = options.onFileUploadStart(file); | ||
// if the onFileUploadStart handler returned null, it means we should proceed further, discard the file! | ||
if (proceed == false) { | ||
fileCount--; | ||
return fileStream.resume(); | ||
} | ||
} | ||
@@ -106,3 +120,3 @@ var ws = fs.createWriteStream(newFilePath); | ||
fileStream.on('end', function() { | ||
ws.on('finish', function() { | ||
file.truncated = fileStream.truncated; | ||
@@ -113,2 +127,6 @@ if (!req.files[fieldname]) { req.files[fieldname] = []; } | ||
if (options.onFileUploadComplete) { options.onFileUploadComplete(file); } | ||
// defines has completed processing one more file | ||
fileCount--; | ||
onFinish(); | ||
}); | ||
@@ -143,2 +161,13 @@ | ||
busboy.on('finish', function() { | ||
readFinished = true; | ||
onFinish(); | ||
}); | ||
/** | ||
* Pass the control to the next middleware in stack | ||
* only if the read and write stream are finished | ||
*/ | ||
var onFinish = function () { | ||
if (!readFinished || fileCount > 0) return; | ||
for (var field in req.files) { | ||
@@ -149,6 +178,10 @@ if (req.files[field].length === 1) { | ||
} | ||
// Parse the body and create a best structure | ||
req.body = qs.parse(req.body); | ||
// when done parsing the form, pass the control to the next middleware in stack | ||
if (options.onParseEnd) { options.onParseEnd(); } | ||
next(); | ||
}); | ||
if (options.onParseEnd) { options.onParseEnd(req, next); } | ||
else { next(); } | ||
}; | ||
@@ -155,0 +188,0 @@ req.pipe(busboy); |
{ | ||
"name": "multer", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Connect middleware for handling multipart/form-data", | ||
@@ -28,4 +28,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"busboy": "^0.2.6", | ||
"mkdirp": "~0.3.5", | ||
"busboy": "^0.2.6" | ||
"qs": "^0.6.6", | ||
"busboy": "^0.2.7" | ||
}, | ||
@@ -32,0 +34,0 @@ "devDependencies": { |
@@ -61,3 +61,3 @@ # Multer [![Build Status](https://travis-ci.org/expressjs/multer.svg?branch=master)](https://travis-ci.org/expressjs/multer) [![NPM version](https://badge.fury.io/js/multer.svg)](https://badge.fury.io/js/multer) | ||
* `onParseStart()` | ||
* `onParseEnd()` | ||
* `onParseEnd(req, next)` | ||
* `onError()` | ||
@@ -91,3 +91,3 @@ * `onFilesLimit()` | ||
An object specifying the size limits of the following optional properties. | ||
An object specifying the size limits of the following optional properties. This object is passed to busboy directly, and the details of properties can be found on [busboy's page](https://github.com/mscdex/busboy#busboy-methods) | ||
@@ -97,3 +97,3 @@ * `fieldNameSize` - integer - Max field name size (Default: 100 bytes) | ||
* `fields` - integer - Max number of non-file fields (Default: Infinity) | ||
* `fileSize` - integer - For multipart forms, the max file size (Default: Infinity) | ||
* `fileSize` - integer - For multipart forms, the max file size (in bytes) (Default: Infinity) | ||
* `files` - integer - For multipart forms, the max number of file fields (Default: Infinity) | ||
@@ -133,2 +133,10 @@ * `parts` - integer - For multipart forms, the max number of parts (fields + files) (Default: Infinity) | ||
You can even stop a file from being uploaded - just return `false` from the event handler. The file won't be processed or reach the file system. | ||
```js | ||
onFileUploadStart: function (file) { | ||
if (file.originalname == 'virus.exe') return false; | ||
} | ||
``` | ||
### onFileUploadData(file, data) | ||
@@ -164,12 +172,20 @@ | ||
### onParseEnd() | ||
### onParseEnd(req, next) | ||
Event handler triggered when the form parsing completes. | ||
Event handler triggered when the form parsing completes. The `request` object and the `next` objects are are passed to the function. | ||
```js | ||
onParseStart: function () { | ||
console.log('Form parsing completed at: ', new Date()) | ||
onParseEnd: function (req, next) { | ||
console.log('Form parsing completed at: ', new Date()); | ||
// usage example: custom body parse | ||
req.body = require('qs').parse(req.body); | ||
// call the next middleware | ||
next(); | ||
} | ||
``` | ||
**Note**: If you have created a `onParseEnd` event listener, you must manually call the `next()` function, else the request will be left hanging. | ||
### onError() | ||
@@ -201,3 +217,3 @@ | ||
```js | ||
onFilesLimit: function () { | ||
onFieldsLimit: function () { | ||
console.log('Crossed fields limit!') | ||
@@ -212,3 +228,3 @@ } | ||
```js | ||
onFilesLimit: function () { | ||
onPartsLimit: function () { | ||
console.log('Crossed parts limit!') | ||
@@ -215,0 +231,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
246
1
1
14901
3
5
144
+ Addedqs@^0.6.6
+ Addedqs@0.6.6(transitive)
Updatedbusboy@^0.2.7