Skip to content

Commit

Permalink
improving parsed hosts support
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Aug 30, 2019
1 parent c500a88 commit 1fd4efa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 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": "3.0.0",
"version": "3.0.1",
"description": "Advanced URL Connection String parser + generator.",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
76 changes: 37 additions & 39 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,52 +176,50 @@ export class ConnectionString {
}

if (!('protocol' in this) && isText(defaults.protocol)) {
this.protocol = defaults.protocol && defaults.protocol.trim(); // TODO: non-optimal
this.protocol = defaults.protocol && defaults.protocol.trim();
}

// Missing default hosts are merged with the existing ones:
if (Array.isArray(defaults.hosts)) {
const hosts = Array.isArray(this.hosts) ? this.hosts : [];
defaults.hosts.filter(d => {
return d && typeof d === 'object';
})
.forEach(dh => {
const dhName = isText(dh.name) && dh.name ? dh.name.trim() : undefined; // TODO: non-optimal
const h: IHost = {name: dhName, port: dh.port, type: dh.type};
let found = false;
for (let i = 0; i < hosts.length; i++) {
const thisHost = fullHostName(hosts[i]), defHost = fullHostName(h);
if (thisHost.toLowerCase() === defHost.toLowerCase()) {
found = true;
break;
}
const dhHosts = <IHost[]>defaults.hosts.filter(d => d && typeof d === 'object');
dhHosts.forEach(dh => {
const dhName = isText(dh.name) && dh.name ? dh.name.trim() : undefined;
const h: IHost = {name: dhName, port: dh.port, type: dh.type};
let found = false;
for (let i = 0; i < hosts.length; i++) {
const thisHost = fullHostName(hosts[i]), defHost = fullHostName(h);
if (thisHost.toLowerCase() === defHost.toLowerCase()) {
found = true;
break;
}
if (!found) {
const obj: IParsedHost = {};
if (h.name) {
if (h.type && h.type in HostType) {
obj.name = h.name;
obj.type = h.type;
} else {
const t = parseHost(h.name, true);
if (t) {
obj.name = t.name;
obj.type = t.type;
}
}
if (!found) {
const obj: IParsedHost = {};
if (h.name) {
if (h.type && h.type in HostType) {
obj.name = h.name;
obj.type = h.type;
} else {
const t = parseHost(h.name, true);
if (t) {
obj.name = t.name;
obj.type = t.type;
}
}
const p = h.port;
if (typeof p === 'number' && p > 0 && p < 65536) {
obj.port = p;
}
if (obj.name || obj.port) {
Object.defineProperty(obj, 'toString', {
value: (options: IEncodingOptions) => fullHostName(obj, options)
});
hosts.push(obj);
}
}
});
const p = h.port;
if (typeof p === 'number' && p > 0 && p < 65536) {
obj.port = p;
}
if (obj.name || obj.port) {
Object.defineProperty(obj, 'toString', {
value: (options: IEncodingOptions) => fullHostName(obj, options)
});
hosts.push(obj);
}
}
});
if (hosts.length) {
this.hosts = hosts;
}
Expand All @@ -238,7 +236,7 @@ export class ConnectionString {
// Since the order of path segments is usually important, we set default
// path segments as they are, but only when they are missing completely:
if (!('path' in this) && Array.isArray(defaults.path)) {
var s = defaults.path.filter(isText);
const s = defaults.path.filter(isText);
if (s.length) {
this.path = s;
}
Expand Down Expand Up @@ -331,7 +329,7 @@ function parseHost(host: string, raw?: boolean): IParsedHost | null {
(function () {
// hiding prototype members:
['setDefaults', 'toString', 'hostname', 'port'].forEach(prop => {
let desc = <PropertyDescriptor>Object.getOwnPropertyDescriptor(ConnectionString.prototype, prop);
const desc = <PropertyDescriptor>Object.getOwnPropertyDescriptor(ConnectionString.prototype, prop);
desc.enumerable = false;
Object.defineProperty(ConnectionString.prototype, prop, desc);
});
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IParsedHost extends IHost {

export interface IConnectionDefaults {
protocol?: string;
hosts?: Array<IHost>;
hosts?: Array<IHost | null>;
user?: string;
password?: string;
path?: string[];
Expand Down
2 changes: 1 addition & 1 deletion test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ describe('setDefaults', () => {
expect(parse('').setDefaults({hosts: [{port: 123}]})).to.eql({hosts: [{port: 123}]});
});
it('must skip invalid hosts', () => {
expect(parse('my-host').setDefaults({hosts: [{name: '::'}]})).to.eql({
expect(parse('my-host').setDefaults({hosts: [{name: '::'}, null]})).to.eql({
hosts: [{
name: 'my-host',
type: 'domain'
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"always-prefix"
],
"unnecessary-else": false,
"no-return-await": true
"no-return-await": true,
"prefer-const": "error"
}
}

0 comments on commit 1fd4efa

Please sign in to comment.