Skip to content

Commit

Permalink
improving encoding logic for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 10, 2019
1 parent f2c9e7d commit 2582730
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ access to the first host details, compatible with parsers that do not support mu

### `setDefaults(defaults) => ConnectionString`

The method takes an object with default values, and safely combines it with what's missing in the current object.
The method takes an object with default values (unencoded), and safely combines it with what's missing in the current object.

Please note that while missing defaults for `hosts` and `params` are merged with the existing sets, for the `path` they are not,
since their order is often important, so the defaults for `path` are only used when there are no path segments
Expand Down Expand Up @@ -174,7 +174,7 @@ on purpose. But if you want it parsed again, use a different symbol, for example

### `static parseHost(host) => {name, port, type} | null`

When using an external list of default hosts, you may need to parse them independently, using this method,
When using an external list of default hosts (unencoded), you may need to parse them independently, using this method,
so they can be correctly processed by method `setDefaults`.

```js
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": "2.6.2",
"version": "2.7.0",
"description": "Advanced URL Connection String parser + generator.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
}
}

function parseHost(host, external) {
if (external) {
function parseHost(host, raw) {
if (raw) {
if (typeof host !== 'string') {
throw new TypeError('Invalid "host" parameter: ' + JSON.stringify(host));
}
Expand All @@ -125,7 +125,11 @@
isIPv6 = true;
} else {
// It is either IPv4 or domain/socket
m = host.match(/(([a-z0-9%.$-]*)(?::(-?[0-9a-z]+[^/?]*))?)/i);
if (raw) {
m = host.match(/(([a-z0-9.$-/]*)(?::(-?[0-9a-z]+[^/?]*))?)/i);
} else {
m = host.match(/(([a-z0-9%.$-]*)(?::(-?[0-9a-z]+[^/?]*))?)/i);
}
}
if (m) {
var h = {};
Expand All @@ -138,7 +142,7 @@
h.name = m[2];
h.type = hostType.IPv4;
} else {
h.name = decode(m[2]);
h.name = raw ? m[2] : decode(m[2]);
h.type = h.name.match(/\/|.*\.sock$/i) ? hostType.socket : hostType.domain;
}
}
Expand Down Expand Up @@ -247,7 +251,7 @@
if (h.type && h.type in hostType) {
obj.type = h.type;
} else {
var t = parseHost(h.name);
var t = parseHost(h.name, true);
if (t) {
obj.type = t.type;
}
Expand Down
4 changes: 4 additions & 0 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ describe('parseHost', () => {
it('must parse valid hosts', () => {
expect(parseHost('a')).toEqual({name: 'a', type: 'domain'});
expect(parseHost('a:123')).toEqual({name: 'a', port: 123, type: 'domain'});
expect(parseHost('1.2.3.4:123')).toEqual({name: '1.2.3.4', port: 123, type: 'IPv4'});
expect(parseHost('[::]:123')).toEqual({name: '[::]', port: 123, type: 'IPv6'});
expect(parseHost('a.sock')).toEqual({name: 'a.sock', type: 'socket'});
expect(parseHost('/a')).toEqual({name: '/a', type: 'socket'});
expect(parseHost('a/')).toEqual({name: 'a/', type: 'socket'});
});
});

0 comments on commit 2582730

Please sign in to comment.