move connect.query() into our repo

Esse commit está contido em:
Roman Shtylman
2014-02-04 10:10:56 -05:00
commit 9bc63d92a0
4 arquivos alterados com 62 adições e 4 exclusões
+2 -3
Ver Arquivo
@@ -2,8 +2,7 @@
* Module dependencies.
*/
var connect = require('connect')
, mixin = require('utils-merge')
var mixin = require('utils-merge')
, escapeHtml = require('escape-html')
, Router = require('./router')
, methods = require('methods')
@@ -99,7 +98,7 @@ app.lazyrouter = function() {
strict: this.enabled('strict routing')
});
this._router.use(connect.query());
this._router.use(middleware.query());
this._router.use(middleware.init(this));
}
};
+35 -1
Ver Arquivo
@@ -3,7 +3,8 @@
* Module dependencies.
*/
var utils = require('./utils');
var parseUrl = require('./utils').parseUrl;
var qs = require('qs');
/**
* Initialization middleware, exposing the
@@ -30,3 +31,36 @@ exports.init = function(app){
next();
}
};
/**
* Query:
*
* Automatically parse the query-string when available,
* populating the `req.query` object using
* [qs](https://github.com/visionmedia/node-querystring).
*
* Examples:
*
* .use(connect.query())
* .use(function(req, res){
* res.end(JSON.stringify(req.query));
* });
*
* The `options` passed are provided to qs.parse function.
*
* @param {Object} options
* @return {Function}
* @api public
*/
exports.query = function query(options){
return function query(req, res, next){
if (!req.query) {
req.query = ~req.url.indexOf('?')
? qs.parse(parseUrl(req).query, options)
: {};
}
next();
};
};
+24
Ver Arquivo
@@ -156,3 +156,27 @@ exports.pathRegexp = function(path, keys, sensitive, strict) {
.replace(/\*/g, '(.*)');
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
}
/**
* Parse the `req` url with memoization.
*
* @param {ServerRequest} req
* @return {Object}
* @api private
*/
exports.parseUrl = function(req){
var parsed = req._parsedUrl;
if (parsed && parsed.href == req.url) {
return parsed;
} else {
parsed = parse(req.url);
if (parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) {
// This parses pathnames, and a strange pathname like //r@e should work
parsed = parse(req.url.replace(/@/g, '%40'));
}
return req._parsedUrl = parsed;
}
};
+1
Ver Arquivo
@@ -35,6 +35,7 @@
"merge-descriptors": "0.0.2",
"utils-merge": "1.0.0",
"escape-html": "1.0.1",
"qs": "0.6.6",
"debug": ">= 0.7.3 < 1"
},
"devDependencies": {