Skip to content

Commit

Permalink
Merge pull request #5 from vitaly-t/with-host
Browse files Browse the repository at this point in the history
improving IPv6 support
  • Loading branch information
vitaly-t committed Jul 15, 2018
2 parents 267c7ec + bc7ad0c commit ba20bf9
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 146 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Advanced URL Connection String Parser, with fully optional syntax.
Takes a URL connection string (with every element being optional):

```
protocol://user:password@hostA:123,hostB:456/seg1/seg2?p1=val1&p2=val2
protocol://user:password@host1:123,[abcd::]:456/seg1/seg2?p1=val1&p2=val2
```

and converts it into an object that contains only what's specified:
Expand All @@ -19,7 +19,10 @@ and converts it into an object that contains only what's specified:
protocol: 'protocol',
user: 'user',
password: 'password',
hosts: [{name: 'hostA', port: 123}, {name: 'hostB', port: 456}],
hosts: [
{name: 'host1', port: 123, isIPv6: false},
{name: 'abcd::', port: 456, isIPv6: true}
],
segments: ['seg1', 'seg2'],
params: {
p1: 'val1',
Expand All @@ -41,10 +44,10 @@ Unlike the standard URL parser, this one supports the following:

**Short-syntax examples:**

* `localhost` => `{hosts: [{name: 'localhost'}]`
* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345}]`
* `1.2.3.4:123` => `{hosts: [name: '1.2.3.4', port: 123}]`
* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123}]`
* `localhost` => `{hosts: [{name: 'localhost', isIPv6: false}]`
* `localhost:12345` => `{hosts: [{name: 'localhost', port: 12345, isIPv6: false}]`
* `1.2.3.4:123` => `{hosts: [name: '1.2.3.4', port: 123, isIPv6: false}]`
* `[12ab:34cd]:123` => `{hosts: [{name: '12ab:34cd', port: 123, isIPv6: true}]`
* `abc:///seg1?p1=val` => `{protocol: 'abc', segments: ['seg1'], params: {p1: 'val'}}`
* `:12345` => `{hosts: [{port: 12345}]`
* `username@` => `{user: 'username'}`
Expand Down Expand Up @@ -73,7 +76,7 @@ const obj1 = parse('my-server:12345');

// with a default value:
parse('my-server:12345', {user: 'admin'});
//=> {user: 'admin', hosts: [{name: 'my-server', port: 12345}]}
//=> {user: 'admin', hosts: [{name: 'my-server', port: 12345, isIPv6: false}]}
```

or as a class:
Expand All @@ -85,7 +88,7 @@ const obj1 = new ConnectionString('my-server:12345');

// with a default value:
const obj2 = new ConnectionString('my-server:12345', {user: 'admin'});
//=> {user: 'admin', hosts: [{name: 'my-server', port: 12345}]}
//=> {user: 'admin', hosts: [{name: 'my-server', port: 12345, isIPv6: false}]}
```

* **Browsers**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connection-string",
"version": "0.6.1",
"version": "0.7.0",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface IHost {
name?: string
port?: number
isIPv6?: boolean
}

interface IConnectionDefaults {
Expand Down
31 changes: 20 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
if (m) {
if (m[2]) {
h.name = isIPv6 ? m[2] : decode(m[2]);
h.isIPv6 = isIPv6;
}
if (m[3]) {
var p = m[3], port = parseInt(p);
Expand Down Expand Up @@ -140,16 +141,7 @@
}
}
if (Array.isArray(this.hosts)) {
s += this.hosts.map(function (h) {
var a = '';
if (h.name) {
a += encode(h.name);
}
if (h.port) {
a += ':' + h.port;
}
return a;
}).join();
s += this.hosts.map(fullHostName).join();
}
if (Array.isArray(this.segments) && this.segments.length) {
this.segments.forEach(function (seg) {
Expand Down Expand Up @@ -185,7 +177,8 @@
defaults.hosts.forEach(function (dh) {
found = false;
for (var i = 0; i < hosts.length; i++) {
if (equalStrings(hosts[i].name, dh.name) && hosts[i].port === dh.port) {
var thisHost = fullHostName(hosts[i]), defHost = fullHostName(dh);
if (equalStrings(thisHost, defHost)) {
found = true;
break;
}
Expand All @@ -194,6 +187,7 @@
obj = {};
if (isText(dh.name)) {
obj.name = dh.name;
obj.isIPv6 = !!dh.isIPv6;
}
var port = parseInt(dh.port);
if (port > 0 && port < 65536) {
Expand Down Expand Up @@ -238,6 +232,21 @@
return this;
}

function fullHostName(obj) {
var a = '';
if (obj.name) {
if (obj.isIPv6) {
a = '[' + obj.name + ']';
} else {
a = encode(obj.name);
}
}
if (obj.port) {
a += ':' + obj.port;
}
return a;
}

function isText(txt) {
return typeof txt === 'string' && /\S/.test(txt);
}
Expand Down
40 changes: 40 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"env": {
"es6": true,
"node": true,
"jasmine": true
},
"extends": "eslint:recommended",
"parserOptions": {
},
"rules": {
"prefer-arrow-callback": "error",
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
"camelcase": "error",
"new-cap": "error",
"no-console": "error",
"comma-dangle": "error",
"no-var": "error",
"object-shorthand": [
"error",
"properties"
],
"indent": [
"error",
4,
{
"SwitchCase": 1
}
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

0 comments on commit ba20bf9

Please sign in to comment.