Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decompose-url

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decompose-url - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

benchmark/absolute-url.js

6

bower.json
{
"name": "decompose-url",
"description": "A quick and easy URL decomposer for breaking URLs into their constituent parts.",
"version": "0.1.1",
"version": "0.1.2",
"ignore": [

@@ -9,3 +9,5 @@ "test",

"**/.*",
"node_modules"
"node_modules",
"index.js",
"package.json"
],

@@ -12,0 +14,0 @@ "main": "decompose-url.js",

@@ -0,0 +0,0 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.decomposeUrl=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var simplePathRegExp = /^(\/?\/?(?!\/)[^\?#\s]*)(\?[^#\s]*)?(#[^\s]*)?$/,
protocolPattern = /^([a-z0-9.+-]+:)?(\/\/(?!\/))/i,
hostPattern = /^[a-z0-9_\-\.]{0,63}\.[a-z]+|localhost(?=:|\/)/i,
authPattern = /^([a-z0-9_\-]+):([a-z0-9_\-]+)@/i,
portPattern = /^:([0-9]*)/,
separator = '&',
equals = '=',
colon = ':';
giantPattern = /^(([a-z0-9.+-]+):)?\/\/(?!\/)((.+):(.+)@)?([a-z0-9_\-\.]{0,63}|localhost(?=:|\/))?(:([0-9]*))?(.*)/i,
queryStringPattern = /\??([^\?\=\&]+)\=?([^\=\&]+)?/g;
var slash = 0x2F,
question = 0x3F,
octothorpe = 0x23,
colon = 0x3A;
function Url() {

@@ -27,13 +27,20 @@ this.protocol = null;

module.exports = function (url, template) {
var parsedUrl = decompose(url);
module.exports = function (str, template) {
var url = new Url();
url.href = str;
if (!str || typeof str !== 'string') {
return url;
}
url = decompose(url, str);
if (template) {
return parseTemplate(parsedUrl, template);
return parseTemplate(url, template);
}
return parsedUrl;
return url;
};
function decompose(str) {
var url = new Url();
function decompose(url, str) {
if (!str || typeof str !== 'string') {

@@ -43,44 +50,63 @@ return url;

var simplePath = typeof str === 'string' && simplePathRegExp.exec(str);
var charCode = str.charCodeAt(0),
matches;
if (simplePath) {
var search = simplePath[2] ? simplePath[2].substr(1) : null,
hash = simplePath[3] || null;
if (charCode === slash) {
if (str.charCodeAt(1) === slash) {
// Protocol Relative
return decomposeUrl(url, str);
} else {
// Root Relative
url = decomposePath(url, str);
}
} else if (charCode === question) {
// Query String
url.query = parseQueryString(str);
} else if (charCode === octothorpe) {
// Hash value
url.hash = str.substring(1, str.length);
} else if (matches = giantPattern.exec(str)) {
// Full URL
return decomposeUrl(url, matches);
} else {
// Document Relative
url = decomposePath(url, str);
}
var parsed = simplePath[1],
protocol = protocolPattern.exec(parsed);
return url;
}
if (protocol) {
url.protocol = protocol[1] && protocol[1].substr(0, protocol[1].length - 1);
parsed = parsed.substr(protocol[0].length);
function decomposeUrl(url, str) {
var matches = str;
if (typeof matches === 'string') {
matches = giantPattern.exec(str);
}
var auth = authPattern.exec(parsed);
if (auth) {
url.username = auth[1];
url.password = auth[2];
parsed = parsed.substr(auth[0].length);
}
url.protocol = matches[2] || null;
url.username = matches[4] || null;
url.password = matches[5] || null;
url.hostname = matches[6] || null;
url.host = url.hostname ? url.hostname.split('.') : null;
var host = hostPattern.exec(parsed);
url.hostname = host[0];
url.host = host[0].split('.');
// Remove the TLD, if there is one
if (url.host.length > 1) {
url.tld = url.host[url.host.length - 1];
url.host.splice(url.host.length - 1);
}
parsed = parsed.substr(host[0].length);
if (url.host && url.host.length > 1) {
var tld = url.host && url.host.slice(url.host.length - 1)[0];
url.tld = !isNumberic(tld) && tld;
}
var port = portPattern.exec(parsed);
if (port) {
url.port = port[1];
parsed = parsed.substr(port[0].length);
} else {
url.port = '80';
}
}
url.port = matches[8] || '80';
url.pathname = parsed;
url = decompose(url, matches[9]);
return url;
}
var path = parsed.split('/');
function decomposePath(url, str) {
var simplePath = simplePathRegExp.exec(str);
if (simplePath) {
var search = simplePath[2],
hash = simplePath[3] || null;
url.pathname = simplePath[1];
var path = simplePath[1].split('/');
// Handle leading slash

@@ -91,8 +117,6 @@ if (!path[0]) {

url.path = path;
url.href = str;
url.search = search;
url.hash = hash && hash.substr(1);
url.query = parseQueryString(search);
url = decompose(url, hash);
url = decompose(url, search);
}
return url;

@@ -102,30 +126,11 @@ }

function parseQueryString(str) {
if (!str || typeof str !== 'string') {
return null;
}
var obj = {};
var spacesRegExp = /\+/g,
obj = {},
qs = str.split(separator),
len = qs.length,
pair,
equalsIndex,
var matches = queryStringPattern.exec(str),
key,
value;
while (matches && queryStringPattern.lastIndex) {
key = decodeURIComponent(matches[1]);
value = decodeURIComponent(matches[2] || '');
for (var i = 0; i < len; i++) {
pair = qs[i].replace(spacesRegExp, '%20');
equalsIndex = pair.indexOf(equals);
if (equalsIndex >= 0) {
key = pair.substr(0, equalsIndex);
value = pair.substr(equalsIndex + 1);
} else {
key = pair;
value = '';
}
key = decodeURIComponent(key);
value = decodeURIComponent(value);
if (!obj.hasOwnProperty(key)) {

@@ -138,4 +143,5 @@ obj[key] = value;

}
matches = queryStringPattern.exec(str);
}
return obj;

@@ -162,3 +168,3 @@ }

key = split[i];
if (key[0] === colon) {
if (key.charCodeAt(0) === colon) {
key = key.substr(1);

@@ -170,2 +176,6 @@ url.params[key] = url.path[i];

return url;
}
function isNumberic(str) {
return (str - parseFloat(str) + 1) >= 0
}
{
"name": "decompose-url",
"description": "A quick and easy URL decomposer for breaking URLs into their constituent parts.",
"version": "0.1.1",
"version": "0.1.2",
"author": "David Pate <me@davidtpate.com> (http://davidtpate.com)",

@@ -23,5 +23,9 @@ "keywords": [

"browserify": "^5.x",
"benchmark": "^1.x"
"benchmark": "^1.x",
"beautify-benchmark": "^0.x",
"parseurl": "^1.x",
"fast-url-parser": "^1.x"
},
"scripts": {
"bench": "node benchmark/index.js",
"test": "mocha --check-leaks --bail --reporter spec test/",

@@ -28,0 +32,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot test/",

@@ -60,2 +60,79 @@ # decompose-url

## Benchmark
```bash
$ npm run-script bench
> decompose-url@0.1.1 bench decompose-url
> node benchmark/index.js
> node benchmark\absolute-url.js
Parsing URL http://test.example.com:8000/one/two/three?value=abc&value2=123#david-rules
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
decomposeUrl x 184,267 ops/sec ±0.35% (197 runs sampled)
nativeUrl x 33,648 ops/sec ±0.56% (192 runs sampled)
parseUrl x 50,891 ops/sec ±0.26% (196 runs sampled)
fastUrlParser x 218,160 ops/sec ±0.23% (197 runs sampled)
> node benchmark\document-relative-url.js
Parsing URL one/two/three?value=abc&value2=123#david-rules
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
decomposeUrl x 290,588 ops/sec ±0.14% (197 runs sampled)
nativeUrl x 52,087 ops/sec ±0.51% (195 runs sampled)
parseUrl x 93,881 ops/sec ±0.29% (196 runs sampled)
fastUrlParser x 244,401 ops/sec ±0.17% (196 runs sampled)
> node benchmark\full-url.js
Parsing URL http://username:password@test.example.com:8000/one/two/three?value=abc&value2=123#david-rules
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
decomposeUrl x 175,061 ops/sec ±0.18% (196 runs sampled)
nativeUrl x 31,373 ops/sec ±0.46% (195 runs sampled)
parseUrl x 40,676 ops/sec ±1.89% (194 runs sampled)
fastUrlParser x 199,756 ops/sec ±0.24% (196 runs sampled)
> node benchmark\protocol-relative-url.js
Parsing URL //test.example.com/one/two/three?value=abc&value2=123#david-rules
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
decomposeUrl x 195,416 ops/sec ±0.22% (194 runs sampled)
nativeUrl x 51,650 ops/sec ±0.91% (192 runs sampled)
parseUrl x 85,423 ops/sec ±0.30% (194 runs sampled)
fastUrlParser x 227,996 ops/sec ±0.29% (197 runs sampled)
> node benchmark\root-relative-url.js
Parsing URL /one/two/three?value=abc&value2=123#david-rules
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
decomposeUrl x 261,973 ops/sec ±1.30% (193 runs sampled)
nativeUrl x 48,762 ops/sec ±1.45% (184 runs sampled)
parseUrl x 82,902 ops/sec ±1.40% (190 runs sampled)
fastUrlParser x 218,049 ops/sec ±1.56% (188 runs sampled)
```

@@ -62,0 +139,0 @@ ## License

@@ -21,3 +21,3 @@ var decomposeUrl = require('..'),

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -28,2 +28,26 @@ parsed.query.value2.should.be.exactly('123');

});
it('should parse an absolute URL with an IP address', function () {
var parsed = decomposeUrl('http://127.0.0.1:8000/one/two/three?value=abc&value2=123#david-rules');
parsed.protocol.should.be.exactly('http');
(!!!parsed.username).should.be.true;
(!!!parsed.password).should.be.true;
parsed.hostname.should.be.exactly('127.0.0.1');
parsed.host[0].should.be.exactly('127');
parsed.host[1].should.be.exactly('0');
parsed.host[2].should.be.exactly('0');
parsed.host[3].should.be.exactly('1');
(!!!parsed.tld).should.be.true;
parsed.port.should.be.exactly('8000');
parsed.pathname.should.be.exactly('/one/two/three');
parsed.path[0].should.be.exactly('one');
parsed.path[1].should.be.exactly('two');
parsed.path[2].should.be.exactly('three');
(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');
parsed.query.value2.should.be.exactly('123');
parsed.hash.should.be.exactly('david-rules');
parsed.href.should.be.exactly('http://127.0.0.1:8000/one/two/three?value=abc&value2=123#david-rules');
});
it('should parse an absolute URL with authentication', function () {

@@ -45,3 +69,3 @@ var parsed = decomposeUrl('http://username:password@test.example.com:8000/one/two/three?value=abc&value2=123#david-rules');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -68,3 +92,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -91,3 +115,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -114,3 +138,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -137,3 +161,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -144,2 +168,19 @@ parsed.query.value2.should.be.exactly('123');

});
it('should parse a protocol relative URL pointing to the root', function () {
var parsed = decomposeUrl('//');
(!!!parsed.protocol).should.be.true;
(!!!parsed.username).should.be.true;
(!!!parsed.hostname).should.be.true;
(!!!parsed.host).should.be.true;
(!!!parsed.tld).should.be.true;
parsed.port.should.be.exactly('80');
(!!!parsed.pathname).should.be.true;
(!!!parsed.path).should.be.true;
(!!!parsed.params).should.be.true;
(!!!parsed.search).should.be.true;
(!!!parsed.query).should.be.true;
(!!!parsed.hash).should.be.true;
parsed.href.should.be.exactly('//');
});
it('should parse a root relative URL', function () {

@@ -160,3 +201,3 @@ var parsed = decomposeUrl('/one/two/three?value=abc&value2=123#david-rules');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -182,3 +223,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -204,3 +245,3 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value=123&value2=123');
parsed.search.should.be.exactly('?value=abc&value=123&value2=123');
parsed.query.value[0].should.be.exactly('abc');

@@ -227,3 +268,3 @@ parsed.query.value[1].should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value=123&value=123');
parsed.search.should.be.exactly('?value=abc&value=123&value=123');
parsed.query.value[0].should.be.exactly('abc');

@@ -250,3 +291,3 @@ parsed.query.value[1].should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=');
parsed.search.should.be.exactly('?value=');
parsed.query.value.should.be.exactly('');

@@ -271,3 +312,3 @@ (!!!parsed.hash).should.be.true;

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value');
parsed.search.should.be.exactly('?value');
parsed.query.value.should.be.exactly('');

@@ -292,4 +333,4 @@ (!!!parsed.hash).should.be.true;

(!!!parsed.params).should.be.true;
(!!!parsed.search).should.be.true;
(!!!parsed.query).should.be.true;
parsed.search.should.be.exactly('?');
JSON.stringify(parsed.query).should.be.exactly('{}');
(!!!parsed.hash).should.be.true;

@@ -313,3 +354,3 @@ parsed.href.should.be.exactly('one/two/three?');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=123&');
parsed.search.should.be.exactly('?value=123&');
parsed.query.value.should.be.exactly('123');

@@ -354,3 +395,3 @@ (!!!parsed.hash).should.be.true;

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value=123&value2=123');
parsed.search.should.be.exactly('?value=abc&value=123&value2=123');
parsed.query.value[0].should.be.exactly('abc');

@@ -397,3 +438,3 @@ parsed.query.value[1].should.be.exactly('123');

(!!!parsed.hash).should.be.true;
(!!!parsed.href).should.be.true;
parsed.href.should.be.exactly('');
});

@@ -415,2 +456,19 @@ it('shouldn\'t parse a malformed URL', function () {

(!!!parsed.hash).should.be.true;
parsed.href.should.be.exactly('(╯°□°)╯︵ ┻━┻');
});
it('shouldn\'t parse an undefined URL', function () {
var parsed = decomposeUrl(undefined);
(!!!parsed.protocol).should.be.true;
(!!!parsed.username).should.be.true;
(!!!parsed.hostname).should.be.true;
(!!!parsed.host).should.be.true;
(!!!parsed.tld).should.be.true;
(!!!parsed.port).should.be.true;
(!!!parsed.pathname).should.be.true;
(!!!parsed.path).should.be.true;
(!!!parsed.params).should.be.true;
(!!!parsed.search).should.be.true;
(!!!parsed.query).should.be.true;
(!!!parsed.hash).should.be.true;
(!!!parsed.href).should.be.true;

@@ -439,3 +497,3 @@ });

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -466,3 +524,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -491,3 +549,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -516,3 +574,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -540,3 +598,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -564,3 +622,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -587,3 +645,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value2.should.be.exactly('two');
parsed.search.should.be.exactly('value=abc&value=123&value2=123');
parsed.search.should.be.exactly('?value=abc&value=123&value2=123');
parsed.query.value[0].should.be.exactly('abc');

@@ -610,3 +668,3 @@ parsed.query.value[1].should.be.exactly('123');

(!!!parsed.params).should.be.true;
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -634,3 +692,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -657,3 +715,3 @@ parsed.query.value2.should.be.exactly('123');

parsed.params.value3.should.be.exactly('three');
parsed.search.should.be.exactly('value=abc&value2=123');
parsed.search.should.be.exactly('?value=abc&value2=123');
parsed.query.value.should.be.exactly('abc');

@@ -696,4 +754,4 @@ parsed.query.value2.should.be.exactly('123');

(!!!parsed.hash).should.be.true;
(!!!parsed.href).should.be.true;
parsed.href.should.be.exactly('(╯°□°)╯︵ ┻━┻');
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc