Comparing version 1.0.1 to 1.1.0
72
index.js
var parse = require('url').parse; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var url = require('url') | ||
var parse = url.parse | ||
var Url = url.Url | ||
/** | ||
* Pattern for a simple path case. | ||
* See: https://github.com/joyent/node/pull/7878 | ||
*/ | ||
var simplePathRegExp = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/ | ||
var tryFastRegExp = /^\/[^\\#]*$/ | ||
/** | ||
* Parse the `req` url with memoization. | ||
@@ -9,19 +23,53 @@ * | ||
* @return {Object} | ||
* @api private | ||
* @api public | ||
*/ | ||
module.exports = function parseUrl(req){ | ||
var parsed = req._parsedUrl; | ||
if (parsed && parsed.href == req.url) { | ||
return parsed; | ||
} else { | ||
parsed = parse(req.url); | ||
var parsed = req._parsedUrl | ||
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')); | ||
} | ||
if (parsed instanceof Url && parsed.href === req.url) { | ||
return parsed | ||
} | ||
return req._parsedUrl = parsed; | ||
parsed = fastparse(req.url) | ||
if (parsed.auth && !parsed.protocol && parsed.href.indexOf('//') !== -1) { | ||
// This parses pathnames, and a strange pathname like //r@e should work | ||
parsed = fastparse(req.url.replace(/@/g, '%40')) | ||
} | ||
return req._parsedUrl = parsed | ||
}; | ||
/** | ||
* Parse the `str` url with fast-path short-cut. | ||
* | ||
* @param {string} str | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function fastparse(str) { | ||
if (typeof str === 'string' && tryFastRegExp.test(str)) { | ||
// Try fast path regexp | ||
// See: https://github.com/joyent/node/pull/7878 | ||
var simplePath = simplePathRegExp.exec(str) | ||
// Construct simple URL | ||
if (simplePath) { | ||
var url = new Url() | ||
url.path = str | ||
url.href = str | ||
url.pathname = simplePath[1] | ||
if (simplePath[2]) { | ||
url.search = simplePath[2]; | ||
url.query = url.search.substr(1); | ||
} | ||
return url | ||
} | ||
} | ||
return parse(str) | ||
} |
{ | ||
"name": "parseurl", | ||
"description": "parse a url with memoization", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jonathan Ong", |
@@ -7,5 +7,5 @@ # parseurl | ||
### var pathname = parseurl(req) | ||
### var parsedUrl = parseurl(req) | ||
`pathname` can then be passed to a router or something. | ||
`parsedUrl` is basically a `url.parse()` object. | ||
@@ -35,2 +35,2 @@ ## LICENSE | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4038
5
58
35
1