pax_global_header00006660000000000000000000000064123715544410014520gustar00rootroot0000000000000052 comment=03b7ccca240e2bef5df6c25797e99175d28fb2cb parseurl-1.3.0/000077500000000000000000000000001237155444100133565ustar00rootroot00000000000000parseurl-1.3.0/.gitignore000066400000000000000000000000651237155444100153470ustar00rootroot00000000000000.DS_Store* coverage node_modules npm-debug.log *.log parseurl-1.3.0/.npmignore000066400000000000000000000000471237155444100153560ustar00rootroot00000000000000benchmark/ coverage/ test/ .travis.yml parseurl-1.3.0/.travis.yml000066400000000000000000000003711237155444100154700ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.11" matrix: allow_failures: - node_js: "0.11" fast_finish: true script: "npm run-script test-travis" after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" parseurl-1.3.0/HISTORY.md000066400000000000000000000013731237155444100150450ustar00rootroot000000000000001.3.0 / 2014-08-09 ================== * Add `parseurl.original` for parsing `req.originalUrl` with fallback * Return `undefined` if `req.url` is `undefined` 1.2.0 / 2014-07-21 ================== * Cache URLs based on original value * Remove no-longer-needed URL mis-parse work-around * Simplify the "fast-path" `RegExp` 1.1.3 / 2014-07-08 ================== * Fix typo 1.1.2 / 2014-07-08 ================== * Seriously fix Node.js 0.8 compatibility 1.1.1 / 2014-07-08 ================== * Fix Node.js 0.8 compatibility 1.1.0 / 2014-07-08 ================== * Incorporate URL href-only parse fast-path 1.0.1 / 2014-03-08 ================== * Add missing `require` 1.0.0 / 2014-03-08 ================== * Genesis from `connect` parseurl-1.3.0/LICENSE000066400000000000000000000022201237155444100143570ustar00rootroot00000000000000 (The MIT License) Copyright (c) 2014 Jonathan Ong Copyright (c) 2014 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. parseurl-1.3.0/README.md000066400000000000000000000056131237155444100146420ustar00rootroot00000000000000# parseurl [![NPM version](https://badge.fury.io/js/parseurl.svg)](http://badge.fury.io/js/parseurl) [![Build Status](https://travis-ci.org/expressjs/parseurl.svg?branch=master)](https://travis-ci.org/expressjs/parseurl) [![Coverage Status](https://img.shields.io/coveralls/expressjs/parseurl.svg?branch=master)](https://coveralls.io/r/expressjs/parseurl) Parse a URL with memoization. ## Install ```bash $ npm install parseurl ``` ## API ```js var parseurl = require('parseurl') ``` ### parseurl(req) Parse the URL of the given request object (looks at the `req.url` property) and return the result. The result is the same as `url.parse` in Node.js core. Calling this function multiple times on the same `req` where `req.url` does not change will return a cached parsed object, rather than parsing again. ### parseurl.original(req) Parse the original URL of the given request object and return the result. This works by trying to parse `req.originalUrl` if it is a string, otherwise parses `req.url`. The result is the same as `url.parse` in Node.js core. Calling this function multiple times on the same `req` where `req.originalUrl` does not change will return a cached parsed object, rather than parsing again. ## Benchmark ```bash $ npm run-script bench > parseurl@1.3.0 bench nodejs-parseurl > node benchmark/index.js > node benchmark/fullurl.js Parsing URL "http://localhost:8888/foo/bar?user=tj&pet=fluffy" 1 test completed. 2 tests completed. 3 tests completed. fasturl x 1,290,780 ops/sec ±0.46% (195 runs sampled) nativeurl x 56,401 ops/sec ±0.22% (196 runs sampled) parseurl x 55,231 ops/sec ±0.22% (194 runs sampled) > node benchmark/pathquery.js Parsing URL "/foo/bar?user=tj&pet=fluffy" 1 test completed. 2 tests completed. 3 tests completed. fasturl x 1,986,668 ops/sec ±0.27% (190 runs sampled) nativeurl x 98,740 ops/sec ±0.21% (195 runs sampled) parseurl x 2,628,171 ops/sec ±0.36% (195 runs sampled) > node benchmark/samerequest.js Parsing URL "/foo/bar?user=tj&pet=fluffy" on same request object 1 test completed. 2 tests completed. 3 tests completed. fasturl x 2,184,468 ops/sec ±0.40% (194 runs sampled) nativeurl x 99,437 ops/sec ±0.71% (194 runs sampled) parseurl x 10,498,005 ops/sec ±0.61% (186 runs sampled) > node benchmark/simplepath.js Parsing URL "/foo/bar" 1 test completed. 2 tests completed. 3 tests completed. fasturl x 4,535,825 ops/sec ±0.27% (191 runs sampled) nativeurl x 98,769 ops/sec ±0.54% (191 runs sampled) parseurl x 4,164,865 ops/sec ±0.34% (192 runs sampled) > node benchmark/slash.js Parsing URL "/" 1 test completed. 2 tests completed. 3 tests completed. fasturl x 4,908,405 ops/sec ±0.42% (191 runs sampled) nativeurl x 100,945 ops/sec ±0.59% (188 runs sampled) parseurl x 4,333,208 ops/sec ±0.27% (194 runs sampled) ``` ## License [MIT](LICENSE) parseurl-1.3.0/benchmark/000077500000000000000000000000001237155444100153105ustar00rootroot00000000000000parseurl-1.3.0/benchmark/fullurl.js000066400000000000000000000023561237155444100173410ustar00rootroot00000000000000 /** * Globals for benchmark.js */ global.assert = require('assert') global.createReq = createReq global.fasturl = require('fast-url-parser') global.nativeurl = require('url') global.parseurl = require('..') global.url = 'http://localhost:8888/foo/bar?user=tj&pet=fluffy' /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var assertValues = 'assert.strictEqual(obj.pathname, "/foo/bar"); assert.strictEqual(obj.query, "user=tj&pet=fluffy");' var suite = new benchmark.Suite suite.add({ name: 'fasturl', minSamples: 100, fn: 'var obj = fasturl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'nativeurl', minSamples: 100, fn: 'var obj = nativeurl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'parseurl', minSamples: 100, fn: 'var obj = parseurl(createReq(url));' + assertValues }) suite.on('start', function onCycle(event) { process.stdout.write(' Parsing URL ' + JSON.stringify(url) + '\n\n') }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) function createReq(url) { return { url: url }; } parseurl-1.3.0/benchmark/index.js000066400000000000000000000012361237155444100167570ustar00rootroot00000000000000var fs = require('fs'); var path = require('path'); var spawn = require('child_process').spawn; var exe = process.argv[0]; var cwd = process.cwd(); runScripts(fs.readdirSync(__dirname)); function runScripts(fileNames) { var fileName = fileNames.shift(); if (!fileName) return; if (!/\.js$/i.test(fileName)) return runScripts(fileNames); if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames); var fullPath = path.join(__dirname, fileName); console.log('> %s %s', exe, path.relative(cwd, fullPath)); var proc = spawn(exe, [fullPath], { 'stdio': 'inherit' }); proc.on('exit', function () { runScripts(fileNames); }); } parseurl-1.3.0/benchmark/pathquery.js000066400000000000000000000023311237155444100176670ustar00rootroot00000000000000 /** * Globals for benchmark.js */ global.assert = require('assert') global.createReq = createReq global.fasturl = require('fast-url-parser') global.nativeurl = require('url') global.parseurl = require('..') global.url = '/foo/bar?user=tj&pet=fluffy' /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var assertValues = 'assert.strictEqual(obj.pathname, "/foo/bar"); assert.strictEqual(obj.query, "user=tj&pet=fluffy");' var suite = new benchmark.Suite suite.add({ name: 'fasturl', minSamples: 100, fn: 'var obj = fasturl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'nativeurl', minSamples: 100, fn: 'var obj = nativeurl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'parseurl', minSamples: 100, fn: 'var obj = parseurl(createReq(url));' + assertValues }) suite.on('start', function onCycle(event) { process.stdout.write(' Parsing URL ' + JSON.stringify(url) + '\n\n') }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) function createReq(url) { return { url: url }; } parseurl-1.3.0/benchmark/samerequest.js000066400000000000000000000024621237155444100202100ustar00rootroot00000000000000 /** * Globals for benchmark.js */ global.assert = require('assert') global.createReq = createReq global.fasturl = require('fast-url-parser') global.nativeurl = require('url') global.parseurl = require('..') global.url = '/foo/bar?user=tj&pet=fluffy' /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var assertValues = 'assert.strictEqual(obj.pathname, "/foo/bar"); assert.strictEqual(obj.query, "user=tj&pet=fluffy");' var suite = new benchmark.Suite suite.add({ name: 'fasturl', minSamples: 100, fn: 'var obj = fasturl.parse(req.url);' + assertValues, setup: 'req = createReq(url)' }) suite.add({ name: 'nativeurl', minSamples: 100, fn: 'var obj = nativeurl.parse(req.url);' + assertValues, setup: 'req = createReq(url)' }) suite.add({ name: 'parseurl', minSamples: 100, fn: 'var obj = parseurl(req);' + assertValues, setup: 'req = createReq(url)' }) suite.on('start', function onCycle(event) { process.stdout.write(' Parsing URL ' + JSON.stringify(url) + ' on same request object\n\n') }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) function createReq(url) { return { url: url }; } parseurl-1.3.0/benchmark/simplepath.js000066400000000000000000000022661237155444100200220ustar00rootroot00000000000000 /** * Globals for benchmark.js */ global.assert = require('assert') global.createReq = createReq global.fasturl = require('fast-url-parser') global.nativeurl = require('url') global.parseurl = require('..') global.url = '/foo/bar' /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var assertValues = 'assert.strictEqual(obj.pathname, "/foo/bar"); assert.strictEqual(obj.query, null);' var suite = new benchmark.Suite suite.add({ name: 'fasturl', minSamples: 100, fn: 'var obj = fasturl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'nativeurl', minSamples: 100, fn: 'var obj = nativeurl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'parseurl', minSamples: 100, fn: 'var obj = parseurl(createReq(url));' + assertValues }) suite.on('start', function onCycle(event) { process.stdout.write(' Parsing URL ' + JSON.stringify(url) + '\n\n') }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) function createReq(url) { return { url: url }; } parseurl-1.3.0/benchmark/slash.js000066400000000000000000000022501237155444100167570ustar00rootroot00000000000000 /** * Globals for benchmark.js */ global.assert = require('assert') global.createReq = createReq global.fasturl = require('fast-url-parser') global.nativeurl = require('url') global.parseurl = require('..') global.url = '/' /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var assertValues = 'assert.strictEqual(obj.pathname, "/"); assert.strictEqual(obj.query, null);' var suite = new benchmark.Suite suite.add({ name: 'fasturl', minSamples: 100, fn: 'var obj = fasturl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'nativeurl', minSamples: 100, fn: 'var obj = nativeurl.parse(createReq(url).url);' + assertValues }) suite.add({ name: 'parseurl', minSamples: 100, fn: 'var obj = parseurl(createReq(url));' + assertValues }) suite.on('start', function onCycle(event) { process.stdout.write(' Parsing URL ' + JSON.stringify(url) + '\n\n') }) suite.on('cycle', function onCycle(event) { benchmarks.add(event.target); }) suite.on('complete', function onComplete() { benchmarks.log(); }) suite.run({async: false}) function createReq(url) { return { url: url }; } parseurl-1.3.0/index.js000066400000000000000000000045531237155444100150320ustar00rootroot00000000000000/*! * parseurl * Copyright(c) 2014 Jonathan Ong * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * 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]*)?$/ /** * Exports. */ module.exports = parseurl module.exports.original = originalurl /** * Parse the `req` url with memoization. * * @param {ServerRequest} req * @return {Object} * @api public */ function parseurl(req) { var url = req.url if (url === undefined) { // URL is undefined return undefined } var parsed = req._parsedUrl if (fresh(url, parsed)) { // Return cached URL parse return parsed } // Parse the URL parsed = fastparse(url) parsed._raw = url return req._parsedUrl = parsed }; /** * Parse the `req` original url with fallback and memoization. * * @param {ServerRequest} req * @return {Object} * @api public */ function originalurl(req) { var url = req.originalUrl if (typeof url !== 'string') { // Fallback return parseurl(req) } var parsed = req._parsedOriginalUrl if (fresh(url, parsed)) { // Return cached URL parse return parsed } // Parse the URL parsed = fastparse(url) parsed._raw = url return req._parsedOriginalUrl = parsed }; /** * Parse the `str` url with fast-path short-cut. * * @param {string} str * @return {Object} * @api private */ function fastparse(str) { // Try fast path regexp // See: https://github.com/joyent/node/pull/7878 var simplePath = typeof str === 'string' && simplePathRegExp.exec(str) // Construct simple URL if (simplePath) { var pathname = simplePath[1] var search = simplePath[2] || null var url = Url !== undefined ? new Url() : {} url.path = str url.href = str url.pathname = pathname url.search = search url.query = search && search.substr(1) return url } return parse(str) } /** * Determine if parsed is still fresh for url. * * @param {string} url * @param {object} parsedUrl * @return {boolean} * @api private */ function fresh(url, parsedUrl) { return typeof parsedUrl === 'object' && parsedUrl !== null && (Url === undefined || parsedUrl instanceof Url) && parsedUrl._raw === url } parseurl-1.3.0/package.json000066400000000000000000000014761237155444100156540ustar00rootroot00000000000000{ "name": "parseurl", "description": "parse a url with memoization", "version": "1.3.0", "author": "Jonathan Ong (http://jongleberry.com)", "contributors": [ "Douglas Christopher Wilson " ], "repository": "expressjs/parseurl", "license": "MIT", "devDependencies": { "benchmark": "1.0.0", "beautify-benchmark": "0.2.4", "fast-url-parser": "~1.0.0", "istanbul": "0.3.0", "mocha": "~1.21.4" }, "scripts": { "bench": "node benchmark/index.js", "test": "mocha --check-leaks --bail --reporter spec test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec test/" } } parseurl-1.3.0/test/000077500000000000000000000000001237155444100143355ustar00rootroot00000000000000parseurl-1.3.0/test/test.js000066400000000000000000000151361237155444100156600ustar00rootroot00000000000000 var assert = require('assert') var parseurl = require('..') describe('parseurl(req)', function () { it('should parse the requrst URL', function () { var req = createReq('/foo/bar') var url = parseurl(req) assert.equal(url.host, null) assert.equal(url.hostname, null) assert.equal(url.href, '/foo/bar') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, null) assert.equal(url.query, null) assert.equal(url.search, null) }) it('should parse with query string', function () { var req = createReq('/foo/bar?fizz=buzz') var url = parseurl(req) assert.equal(url.host, null) assert.equal(url.hostname, null) assert.equal(url.href, '/foo/bar?fizz=buzz') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, null) assert.equal(url.query, 'fizz=buzz') assert.equal(url.search, '?fizz=buzz') }) it('should parse a full URL', function () { var req = createReq('http://localhost:8888/foo/bar') var url = parseurl(req) assert.equal(url.host, 'localhost:8888') assert.equal(url.hostname, 'localhost') assert.equal(url.href, 'http://localhost:8888/foo/bar') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, '8888') assert.equal(url.query, null) assert.equal(url.search, null) }) it('should not choke on auth-looking URL', function () { var req = createReq('//todo@txt') assert.equal(parseurl(req).pathname, '//todo@txt') }) it('should return undefined missing url', function () { var req = createReq() var url = parseurl(req) assert.strictEqual(url, undefined) }) describe('when using the same request', function () { it('should parse multiple times', function () { var req = createReq('/foo/bar') assert.equal(parseurl(req).pathname, '/foo/bar') assert.equal(parseurl(req).pathname, '/foo/bar') assert.equal(parseurl(req).pathname, '/foo/bar') }) it('should reflect url changes', function () { var req = createReq('/foo/bar') var url = parseurl(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') req.url = '/bar/baz' url = parseurl(req) assert.equal(url._token, undefined) assert.equal(parseurl(req).pathname, '/bar/baz') }) it('should cache parsing', function () { var req = createReq('/foo/bar') var url = parseurl(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') url = parseurl(req) assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') }) it('should cache parsing where href does not match', function () { var req = createReq('/foo/bar ') var url = parseurl(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') url = parseurl(req) assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') }) }) }) describe('parseurl.original(req)', function () { it('should parse the request original URL', function () { var req = createReq('/foo/bar', '/foo/bar') var url = parseurl.original(req) assert.equal(url.host, null) assert.equal(url.hostname, null) assert.equal(url.href, '/foo/bar') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, null) assert.equal(url.query, null) assert.equal(url.search, null) }) it('should parse originalUrl when different', function () { var req = createReq('/bar', '/foo/bar') var url = parseurl.original(req) assert.equal(url.host, null) assert.equal(url.hostname, null) assert.equal(url.href, '/foo/bar') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, null) assert.equal(url.query, null) assert.equal(url.search, null) }) it('should parse req.url when originalUrl missing', function () { var req = createReq('/foo/bar') var url = parseurl.original(req) assert.equal(url.host, null) assert.equal(url.hostname, null) assert.equal(url.href, '/foo/bar') assert.equal(url.pathname, '/foo/bar') assert.equal(url.port, null) assert.equal(url.query, null) assert.equal(url.search, null) }) it('should return undefined missing req.url and originalUrl', function () { var req = createReq() var url = parseurl.original(req) assert.strictEqual(url, undefined) }) describe('when using the same request', function () { it('should parse multiple times', function () { var req = createReq('/foo/bar', '/foo/bar') assert.equal(parseurl.original(req).pathname, '/foo/bar') assert.equal(parseurl.original(req).pathname, '/foo/bar') assert.equal(parseurl.original(req).pathname, '/foo/bar') }) it('should reflect changes', function () { var req = createReq('/foo/bar', '/foo/bar') var url = parseurl.original(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') req.originalUrl = '/bar/baz' url = parseurl.original(req) assert.equal(url._token, undefined) assert.equal(parseurl.original(req).pathname, '/bar/baz') }) it('should cache parsing', function () { var req = createReq('/foo/bar', '/foo/bar') var url = parseurl.original(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') url = parseurl.original(req) assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') }) it('should cache parsing if req.url changes', function () { var req = createReq('/foo/bar', '/foo/bar') var url = parseurl.original(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') req.url = '/baz' url = parseurl.original(req) assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') }) it('should cache parsing where href does not match', function () { var req = createReq('/foo/bar ', '/foo/bar ') var url = parseurl.original(req) var val = Math.random() url._token = val assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') url = parseurl.original(req) assert.equal(url._token, val) assert.equal(url.pathname, '/foo/bar') }) }) }) function createReq(url, originalUrl) { return { originalUrl: originalUrl, url: url }; }