Skip to content

Commit

Permalink
skipping invalid hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 16, 2018
1 parent 9f94c4d commit 57ac33a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
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.8.3",
"version": "0.8.4",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down
56 changes: 30 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,38 @@
// Missing default hosts are merged with the existing ones:
if (Array.isArray(defaults.hosts)) {
var hosts = Array.isArray(this.hosts) ? this.hosts : [];
var obj, found;
defaults.hosts.forEach(function (dh) {
found = false;
for (var i = 0; i < hosts.length; i++) {
var thisHost = fullHostName(hosts[i]), defHost = fullHostName(dh);
if (equalStrings(thisHost, defHost)) {
found = true;
break;
}
}
if (!found) {
obj = {};
if (isText(dh.name)) {
obj.name = dh.name;
obj.isIPv6 = !!dh.isIPv6;
defaults.hosts.filter(function (d) {
return d && typeof d === 'object';
})
.forEach(function (dh) {
var found = false;
for (var i = 0; i < hosts.length; i++) {
var thisHost = fullHostName(hosts[i]), defHost = fullHostName(dh);
if (equalStrings(thisHost, defHost)) {
found = true;
break;
}
}
var port = parseInt(dh.port);
if (port > 0 && port < 65536) {
obj.port = port;
if (!found) {
var obj = {};
if (isText(dh.name)) {
obj.name = dh.name;
obj.isIPv6 = !!dh.isIPv6;
}
var port = parseInt(dh.port);
if (port > 0 && port < 65536) {
obj.port = port;
}
if (obj.name || obj.port) {
Object.defineProperty(obj, 'toString', {
value: fullHostName.bind(null, obj)
});
hosts.push(obj);
}
}
Object.defineProperty(obj, 'toString', {
value: fullHostName.bind(null, obj)
});
hosts.push(obj);
}
});
if (obj) {
this.hosts = hosts; // If anything changed;
});
if (hosts.length) {
this.hosts = hosts;
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ describe('setDefaults', () => {
expect(parse('').setDefaults({segments: ['', 123, true, ' ']})).toEqual({});
expect(parse('').setDefaults({segments: 123})).toEqual({});
});
it('must ignore invalid and empty hosts', () => {
expect(parse('').setDefaults({hosts: 1})).toEqual({});
expect(parse('').setDefaults({hosts: []})).toEqual({});
expect(parse('').setDefaults({hosts: [1, 2, 3]})).toEqual({});
expect(parse('').setDefaults({hosts: [{}, {}, {}]})).toEqual({});
});

});

Expand Down

0 comments on commit 57ac33a

Please sign in to comment.