/**
 * Parted (https://github.com/chjj/parted)
 * Streaming Body Parsers
 * Copyright (c) 2011, Christopher Jeffrey (MIT License)
 */

/**
 * Faster Require Cache
 */

var modules = {};
var load = function(name) {
  if (modules[name]) return modules[name];
  return modules[name] = require(name);
};

/**
 * Middleware
 */

exports = function(options) {
  options = options || {};

  // LEGACY
  options.limit = options.encodedLimit
               || options.jsonLimit
               || options.limit;

  options.diskLimit = options.multipartLimit
                   || options.diskLimit
                   || options.limit;

  // LEGACY
  if (typeof options === 'string') {
    return new exports.multipart(arguments[0], arguments[1]);
  }

  return function(req, res, next) {
    if (req.method === 'GET'
        || req.method === 'HEAD'
        || req.body) return next();

    req.body = {};

    var type = req.headers['content-type'];
    if (!type) return next();

    type = type.split(';')[0].trim().toLowerCase();

    switch (type) {
      case 'multipart/form-data':
        load('./multipart').handle(req, res, next, options);
        break;
      case 'application/x-www-form-urlencoded':
        load('./encoded').handle(req, res, next, options);
        break;
      case 'application/json':
        load('./json').handle(req, res, next, options);
        break;
      default:
        next();
        break;
    }
  };
};

/**
 * Individual Access
 */

exports.__defineGetter__('multipart', function() {
  return load('./multipart');
});

exports.__defineGetter__('encoded', function() {
  return load('./encoded');
});

exports.__defineGetter__('json', function() {
  return load('./json');
});

/**
 * Legacy
 */

exports.__defineGetter__('root', function() {
  return exports.multipart.root;
});

exports.__defineSetter__('root', function(val) {
  return exports.multipart.root = val;
});

exports.__defineGetter__('middleware', function() {
  return exports.multipart.middleware;
});

/**
 * Expose
 */

module.exports = exports;

@@ -128,16 +128,78 @@
 e)%0A */%0A%0A
+var StringDecoder = require('string_decoder').StringDecoder;%0A%0A
 /**%0A * F
@@ -352,24 +352,162 @@
 (name);%0A%7D;%0A%0A
+/**%0A * Parsers%0A */%0A%0Avar json = JSON.parse%0A  , qs;%0A%0Atry %7B%0A  qs = require('qs').parse;%0A%7D catch(e) %7B%0A  qs = require('querystring').parse;%0A%7D%0A%0A
 /**%0A * Middl
@@ -1226,34 +1226,20 @@
     
-switch (type) %7B%0A      case
+if (type ===
  'mu
@@ -1256,20 +1256,20 @@
 rm-data'
-:%0A  
+) %7B%0A
       lo
@@ -1321,32 +1321,58 @@
 ons);%0A      
-  break;
+return;%0A    %7D%0A%0A    switch (type) %7B
 %0A      case 
@@ -1407,32 +1407,66 @@
 ncoded':%0A       
+ return options.stream%0A          ?
  load('./encoded
@@ -1491,33 +1491,32 @@
 , next, options)
-;
 %0A        break;%0A
@@ -1500,37 +1500,66 @@
 ptions)%0A        
-break
+  : handle(req, next, options, qs)
 ;%0A      case 'ap
@@ -1578,24 +1578,58 @@
 on':%0A       
+ return options.stream%0A          ?
  load('./jso
@@ -1667,44 +1667,471 @@
 ons)
-;%0A        break;%0A      default:%0A    
+%0A          : handle(req, next, options, json);%0A      default:%0A        return next();%0A    %7D%0A  %7D;%0A%7D;%0A%0Avar handle = function(req, next, options, parser) %7B%0A  var decode = new StringDecoder('utf8')%0A    , total = 0%0A    , body = '';%0A%0A  req.on('data', function(data) %7B%0A    if (total += data.length %3E options.limit) %7B%0A      return req.emit('error', new Error('Overflow.'));%0A    %7D%0A    body += decode.write(data);%0A  %7D);%0A%0A  req.on('error', function(err) %7B%0A    req.destroy();%0A
     
@@ -2139,35 +2139,181 @@
 ext(
+err
 );%0A  
-      break;%0A    %7D%0A  %7D
+%7D);%0A%0A  req.on('end', function() %7B%0A    try %7B%0A      if (body) %7B%0A        req.body = parser(body);%0A      %7D%0A    %7D catch (e) %7B%0A      return next(e);%0A    %7D%0A    next();%0A  %7D)
 ;%0A%7D;

make streaming optional