Skip to content

Commit

Permalink
initial implementation finished
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 17, 2019
1 parent 5be6d7e commit ea17d6d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
declare enum HostType {
domain = 'domain',
socket = 'socket',
IPv4 = 'IPv4',
IPv6 = 'IPv6'
domain,
socket,
IPv4,
IPv6
}

interface IEncodingOptions {
Expand Down
27 changes: 18 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// TODO: setDefaults issue accepting :: or [::], inconsistent with the host parsing.

(function (window) {
'use strict';

var invalidDefaults = 'Invalid \'defaults\' parameter!';

var hostType = {
domain: 'domain',
socket: 'socket',
IPv4: 'IPv4',
IPv6: 'IPv6'
};

function ConnectionString(cs, defaults) {

if (!(this instanceof ConnectionString)) {
Expand Down Expand Up @@ -125,14 +134,14 @@
if (m[2]) {
if (isIPv6) {
h.name = m[2];
h.type = 'IPv6';
h.type = hostType.IPv6;
} else {
if (m[2].match(/([0-9]{1,3}.){3}[0-9]{1,3}/)) {
if (m[2].match(/([0-9]{1,3}\.){3}[0-9]{1,3}/)) {
h.name = m[2];
h.type = 'IPv4';
h.type = hostType.IPv4;
} else {
h.name = decode(m[2]);
h.type = m[2].match(/.*\.sock$/i) ? 'socket' : 'domain';
h.type = m[2].match(/.*\.sock$/i) ? hostType.socket : hostType.domain;
}
}
}
Expand Down Expand Up @@ -231,14 +240,13 @@
var obj = {};
if (isText(dh.name)) {
obj.name = dh.name;
var types = ['domain', 'socket', 'IPv4', 'IPv6'];
if (!dh.type || types.indexOf(dh.type) === -1) {
if (dh.type && dh.type in hostType) {
obj.type = dh.type;
} else {
var t = parseHost(dh.name);
if (t) {
obj.type = t.type;
}
} else {
obj.type = dh.type;
}
}
var port = parseInt(dh.port);
Expand Down Expand Up @@ -302,7 +310,7 @@
options = options || {};
var a = '';
if (obj.name) {
if (obj.type === 'IPv6') {
if (obj.type === hostType.IPv6) {
a = '[' + obj.name + ']';
} else {
a = encode(obj.name, options);
Expand Down Expand Up @@ -344,6 +352,7 @@
});

ConnectionString.ConnectionString = ConnectionString; // To make it more TypeScript-friendly
ConnectionString.HostType = hostType;

/* istanbul ignore else */
if (typeof module === 'object' && module && typeof module.exports === 'object') {
Expand Down
31 changes: 29 additions & 2 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ describe('hosts', () => {
}]
});
});
it('must recognize valid IPv4 addresses', () => {
expect(parse('255.255.255.255')).toEqual({
hosts: [{
name: '255.255.255.255',
type: 'IPv4'
}]
});
expect(parse('255.255.255')).toEqual({
hosts: [{
name: '255.255.255',
type: 'domain'
}]
});
expect(parse('255.255.255.255:123')).toEqual({
hosts: [{
name: '255.255.255.255',
port: 123,
type: 'IPv4'
}]
});
});
it('must recognize IPv6 addresses', () => {
expect(parse('[2001:0db8:0000:0000:0000:FF00:0042:8329]')).toEqual({
hosts: [{
Expand Down Expand Up @@ -421,12 +442,18 @@ describe('setDefaults', () => {
expect(parse('').setDefaults({protocol: 'abc'})).toEqual({protocol: 'abc'});
});
it('must set the default hostname and port', () => {
expect(parse('my-host').setDefaults({hosts: [{name: '::', type: 'IPv6'}]})).toEqual({
expect(parse('').setDefaults({hosts: [{name: '::', type: 'IPv6'}]})).toEqual({
hosts: [{
name: '::',
type: 'IPv6'
}]
});
expect(parse('my-host').setDefaults({hosts: [{name: '::', type: 'bla-bla'}]})).toEqual({
hosts: [{
name: 'my-host',
type: 'domain'
}, {
name: '::', type: 'IPv6'
name: '::'
}]
});
expect(parse('my-host').setDefaults({hosts: [{name: 'my-host'}]})).toEqual({
Expand Down

0 comments on commit ea17d6d

Please sign in to comment.