Skip to content

Commit

Permalink
finished implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 13, 2018
1 parent 6b80543 commit 9d39573
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ matrix:
- node_js: "0.12"
script: "npm test"
- node_js: "4"
script: "npm run travis"
script: "npm test"
- node_js: "6"
script: "npm run travis"
- node_js: "8"
script: "npm run travis"
- node_js: "10"
script: "npm run travis"
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connection-string",
"version": "0.5.3",
"version": "0.6.0",
"description": "Advanced URL Connection String Parser.",
"main": "src/index.js",
"typings": "src/index.d.ts",
Expand Down Expand Up @@ -38,9 +38,9 @@
"npm": ">=1.4"
},
"devDependencies": {
"coveralls": "3.0.1",
"eslint": "4.19.1",
"coveralls": "3.0.2",
"eslint": "5.1.0",
"istanbul": "0.4.5",
"jasmine-node": "1.14.5"
"jasmine-node": "1.15.0"
}
}
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@

if (host[0] === '[') {
// It is an IPv6, with [::] being the shortest possible
m = host.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+[^/?]*))?)/i);
m = host.match(/(\[([0-9a-z:%]{2,45})](?::(-?[0-9]+[^/?]*))?)/i);
isIPv6 = true;
} else {
// It is either IPv4 or a name
m = host.match(/(([a-z0-9%.-]*)(?::([0-9]+[^/?]*))?)/i);
m = host.match(/(([a-z0-9%.-]*)(?::(-?[0-9]+[^/?]*))?)/i);
}
if (m) {
if (m[2]) {
h.name = isIPv6 ? m[2] : decode(m[2]);
}
if (m[3]) {
var p = m[3], port = parseInt(p);
if (port > 0 && port <= 65535 && port.toString() === p) {
if (port > 0 && port < 65536 && port.toString() === p) {
h.port = port;
} else {
throw new Error('Invalid port: ' + p);
Expand Down Expand Up @@ -183,22 +183,21 @@
var hosts = Array.isArray(this.hosts) ? this.hosts : [];
var obj, found;
defaults.hosts.forEach(function (dh) {
// TODO: Plus add ignoring case ;)
found = false;
for (var i = 0; i < hosts.length; i++) {
if (hosts[i].name === dh.name && hosts[i].port === dh.port) {
if (equalStrings(hosts[i].name, dh.name) && hosts[i].port === dh.port) {
found = true;
break;
}
}
// TODO: Should do better than just logical check here:
if (!found) {
obj = {};
if (dh.name) {
if (isText(dh.name)) {
obj.name = dh.name;
}
if (dh.port) {
obj.port = dh.port;
var port = parseInt(dh.port);
if (port > 0 && port < 65536) {
obj.port = port;
}
hosts.push(obj);
}
Expand Down Expand Up @@ -247,6 +246,10 @@
return txt.replace(/^[\s]*|[\s]*$/g, '');
}

function equalStrings(str1, str2) {
return (typeof str1 === 'string' && typeof str2 === 'string') && str1.toUpperCase() === str2.toUpperCase();
}

/* istanbul ignore next */
Number.isInteger = Number.isInteger || function (value) {
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
Expand Down
23 changes: 19 additions & 4 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,18 @@ describe('port', function () {
{port: 12345}]
});
});
it('must not allow port=0', function () {
it('must not allow 0 or negative ports', function () {
expect(function () {
parse(':0');
}).toThrow('Invalid port: 0');
expect(function () {
parse(':-1');
}).toThrow('Invalid port: -1');
});
it('must not allow invalid terminators', function () {
//expect(parse(':12345a')).toEqual({});
//expect(parse('@:12345a')).toEqual({});
expect(function () {
parse(':12345a');
}).toThrow('Invalid port: 12345a');
expect(parse(':abc')).toEqual({});
expect(parse('@:abc123')).toEqual({});
});
Expand Down Expand Up @@ -303,6 +307,9 @@ describe('build', function () {
it('must support solo hostname', function () {
expect(parse('server').build()).toBe('server');
});
it('must support solo port', function () {
expect(parse(':123').build()).toBe(':123');
});
it('must support hostname + port', function () {
expect(parse('server:123').build()).toBe('server:123');
});
Expand Down Expand Up @@ -345,7 +352,15 @@ describe('setDefaults', function () {
expect(parse('').setDefaults({protocol: 'abc'})).toEqual({protocol: 'abc'});
});
it('must set the default hostname and port', function () {
expect(parse('').setDefaults({host: 'abc'})).toEqual({}); // cannot set the host directly
expect(parse('my-host').setDefaults({hosts: [{name: 'abc'}]})).toEqual({hosts: [{name: 'my-host'}, {name: 'abc'}]});
expect(parse('my-host').setDefaults({hosts: [{name: 'my-host'}]})).toEqual({hosts: [{name: 'my-host'}]});
expect(parse('my-host').setDefaults({hosts: [{name: 'my-host', port: 222}]})).toEqual({
hosts: [{name: 'my-host'}, {
name: 'my-host',
port: 222
}]
});
expect(parse(':111').setDefaults({hosts: [{port: 123}]})).toEqual({hosts: [{port: 111}, {port: 123}]});
expect(parse('').setDefaults({hosts: [{name: 'abc'}]})).toEqual({hosts: [{name: 'abc'}]});
expect(parse('').setDefaults({hosts: [{port: 123}]})).toEqual({hosts: [{port: 123}]});
});
Expand Down

0 comments on commit 9d39573

Please sign in to comment.