Permalink
Browse files

Merge pull request #664 from bminer/master

Added `defer` property to multipart() middleware
  • Loading branch information...
2 parents 8760300 + ff36e83 commit cb80fcd0314892a53a38598c95cc869dfcbf42ae @tj tj committed Oct 2, 2012
Showing with 45 additions and 5 deletions.
  1. +14 −4 lib/middleware/multipart.js
  2. +31 −1 test/multipart.js
@@ -40,6 +40,9 @@ function noop(req, res, next) {
* Options:
*
* - `limit` byte limit defaulting to none
+ * - `defer` defers processing and exposes the Formidable form object as `req.form`.
+ * `next()` is called without waiting for the form's "end" event.
+ * This option is useful if you need to bind to the "progress" event, for example.
*
* @param {Object} options
* @return {Function}
@@ -99,8 +102,10 @@ exports = module.exports = function(options){
});
form.on('error', function(err){
- err.status = 400;
- next(err);
+ if (!options.defer) {
+ err.status = 400;
+ next(err);
+ }
done = true;
});
@@ -109,13 +114,18 @@ exports = module.exports = function(options){
try {
req.body = qs.parse(data);
req.files = qs.parse(files);
- next();
+ if (!options.defer) next();
} catch (err) {
- next(err);
+ form.emit('error', err);
}
});
form.parse(req);
+
+ if (options.defer) {
+ req.form = form;
+ next();
+ }
});
}
};
View
@@ -266,5 +266,35 @@ describe('connect.multipart()', function(){
});
})
+ it('should defer processing if `defer` is set', function(done){
+ var app = connect();
+
+ app.use(connect.multipart({"defer": true}));
+
+ app.use(function(req, res){
+ JSON.stringify(req.body).should.equal("{}");
+ req.form.on("end", function() {
+ res.end(JSON.stringify(req.body));
+ });
+ });
+
+ app.request()
+ .post('/')
+ .set('Content-Type', 'multipart/form-data; boundary=foo')
+ .write('--foo\r\n')
+ .write('Content-Disposition: form-data; name="user"\r\n')
+ .write('\r\n')
+ .write('Tobi')
+ .write('\r\n--foo\r\n')
+ .write('Content-Disposition: form-data; name="age"\r\n')
+ .write('\r\n')
+ .write('1')
+ .write('\r\n--foo--')
+ .end(function(res){
+ res.body.should.equal('{"user":"Tobi","age":"1"}');
+ done();
+ });
+ })
+
})
-})
+})

0 comments on commit cb80fcd

Please sign in to comment.