Permalink
Browse files

remove streaming qs and json parsers, remove legacy code

  • Loading branch information...
1 parent 675ca77 commit 925820e907e4c769b66c6ea9fea585dce3cbbf57 @chjj committed Nov 6, 2011
Showing with 15 additions and 879 deletions.
  1. +0 −196 lib/encoded.js
  2. +9 −45 lib/index.js
  3. +0 −543 lib/json.js
  4. +6 −95 test/index.js
View
@@ -1,196 +0,0 @@
-/**
- * Parted Encoded/QS Parser
- * Copyright (c) 2011, Christopher Jeffrey (MIT License)
- */
-
-var EventEmitter = require('events').EventEmitter
- , StringDecoder = require('string_decoder').StringDecoder;
-
-var AMP = '&'.charCodeAt(0)
- , EQUAL = '='.charCodeAt(0);
-
-/**
- * Parser
- */
-
-var Parser = function(options) {
- if (!(this instanceof Parser)) {
- return new Parser();
- }
-
- EventEmitter.call(this);
-
- this.readable = true;
- this.writable = true;
-
- this.options = options || {};
-
- this.state = 'key';
- this.buff = '';
- this.key = '';
- this.decode = new StringDecoder('utf8');
- this.written = 0;
-};
-
-Parser.prototype.__proto__ = EventEmitter.prototype;
-
-Parser.prototype.write = function(data) {
- if (!this.writable) return;
-
- try {
- this._parse(data);
- this.written += data.length;
- } catch(e) {
- this._error(e);
- }
-
- this.emit('data', data);
-};
-
-Parser.prototype.end = function(data) {
- if (!this.writable) return;
- if (data) this.write(data);
-
- // always left in the buffer
- if (this.buff) {
- if (this.key) {
- this.emit('value', this.key, unescape(this.buff));
- } else {
- this.emit('value', unescape(this.buff), '');
- }
- }
-
- this.emit('end');
-};
-
-Parser.prototype._parse = function(data) {
- var i = 0
- , k = 0
- , l = data.length
- , ch;
-
- for (; i < l; i++) {
- ch = data[i];
- switch (this.state) {
- case 'key':
- switch (ch) {
- case EQUAL:
- this.state = 'value';
- this.buff += this.decode.write(data.slice(k, i));
- this.key = unescape(this.buff);
- this.buff = '';
- k = i + 1;
- break;
- case AMP:
- return this._error('Unexpected AMP.');
- }
- break;
- case 'value':
- switch (ch) {
- case AMP:
- this.state = 'key';
- this.buff += this.decode.write(data.slice(k, i));
- this.emit('value', this.key, unescape(this.buff));
- this.key = '';
- this.buff = '';
- k = i + 1;
- break;
- case EQUAL:
- return this._error('Unexpected EQUAL.');
- }
- break;
- }
- }
-
- if (k < data.length) {
- this.buff += this.decode.write(data.slice(k));
- }
-};
-
-Parser.prototype._error = function(err) {
- this.destroy();
- this.emit('error', typeof err === 'string'
- ? new Error(err)
- : err);
-};
-
-Parser.prototype.destroy = function() {
- this.writable = false;
- this.readable = false;
-};
-
-var unescape = function(str) {
- try {
- str = decodeURIComponent(str.replace(/\+/g, ' '));
- } finally {
- return str.replace(/\0/g, '');
- }
-};
-
-/**
- * Middleware
- */
-
-Parser.middleware = function(options) {
- return function(req, res, next) {
- if (req.method === 'GET'
- || req.method === 'HEAD'
- || req._encoded) return next();
-
- req._encoded = true;
-
- var type = req.headers['content-type'];
-
- if (type) type = type.split(';')[0].trim().toLowerCase();
-
- if (type == 'application/x-www-form-urlencoded') {
- Parser.handle(req, res, next, options);
- } else {
- if (options.ensureBody) {
- req.body = {};
- }
- next();
- }
- };
-};
-
-/**
- * Handler
- */
-
-Parser.handle = function(req, res, next, options) {
- var parser = new Parser(options)
- , data = {}
- , limit = options.limit;
-
- parser.on('value', function(key, value) {
- data[key] = value;
- });
-
- parser.on('end', function() {
- next();
- });
-
- parser.on('error', function(err) {
- req.destroy();
- next(err);
- });
-
- if (limit) {
- parser.on('data', function() {
- if (this.written > limit) {
- this.emit('error', new Error('Overflow.'));
- this.destroy();
- }
- });
- }
-
- req.body = data;
- req.pipe(parser);
-};
-
-/**
- * Expose
- */
-
-module.exports = Parser;
View
@@ -36,28 +36,16 @@ try {
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'];
+ var type = req.headers['content-type']
+ , parser;
+
if (!type) return next();
type = type.split(';')[0].trim().toLowerCase();
@@ -69,16 +57,16 @@ exports = function(options) {
switch (type) {
case 'application/x-www-form-urlencoded':
- return options.stream
- ? load('./encoded').handle(req, res, next, options)
- : handle(req, next, options, qs);
+ parser = qs;
+ break;
case 'application/json':
- return options.stream
- ? load('./json').handle(req, res, next, options)
- : handle(req, next, options, json);
+ parser = json;
+ break;
default:
return next();
}
+
+ handle(req, next, options, parser);
};
};
@@ -119,30 +107,6 @@ 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
*/
Oops, something went wrong.

0 comments on commit 925820e

Please sign in to comment.