Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added `defer` property to multipart() middleware #664

Merged
merged 3 commits into from

2 participants

@bminer

The newly added defer property allows one to defer mulitpart parsing and immediately execute the next middleware. In this case, Formidable's form object is exposed via req.form, allowing middleware to bind to "progress", "error", and/or "end" events.

Fixes issue #638

bminer added some commits
@bminer bminer Added `defer` property to multipart() middleware
The newly added `defer` property allows one to defer mulitpart parsing and
immediately execute the next middleware. In this case, Formidable's form
object is exposed via `req.form`, allowing middleware to bind to "progress",
"error", and/or "end" events.
7626549
@bminer bminer Fixed a bug introduced when adding `defer` option to multipart() midd…
…leware
9f7427a
lib/middleware/multipart.js
@@ -99,8 +102,10 @@ exports = module.exports = function(options){
});
form.on('error', function(err){
- err.status = 400;
- next(err);
+ if(!options.defer) {
@tj Owner
tj added a note

if ( for these, and we need a test or two, rest looks good thanks!

@bminer
bminer added a note

Should be if(!options.defer)... if we don't defer, then it's safe to call next() here. If we do defer, we shouldn't call next() again, right? I've always wondered... what happens if you res.send(...) and then call next(err)???

@tj Owner
tj added a note

no i just mean if ( add a space :D haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bminer bminer Minor style changes
Added tests for `defer` option in multipart() middleware
ff36e83
@bminer

@visionmedia - Made a few of your suggested changes. Please let me know if you need anything else. :)

@tj tj merged commit cb80fcd into senchalabs:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 2, 2012
  1. @bminer

    Added `defer` property to multipart() middleware

    bminer authored
    The newly added `defer` property allows one to defer mulitpart parsing and
    immediately execute the next middleware. In this case, Formidable's form
    object is exposed via `req.form`, allowing middleware to bind to "progress",
    "error", and/or "end" events.
  2. @bminer
  3. @bminer

    Minor style changes

    bminer authored
    Added tests for `defer` option in multipart() middleware
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 5 deletions.
  1. +14 −4 lib/middleware/multipart.js
  2. +31 −1 test/multipart.js
View
18 lib/middleware/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
32 test/multipart.js
@@ -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();
+ });
+ })
+
})
-})
+})
Something went wrong with that request. Please try again.